Add a script for converting CROSSTOOL files to Starlark rules

Progress towards issue #5380

RELNOTES: None
PiperOrigin-RevId: 230795058
This commit is contained in:
rosica 2019-01-24 14:54:21 -08:00 committed by Copybara-Service
parent 0f72a1209b
commit b3a83d701a
6 changed files with 2582 additions and 0 deletions

View File

@ -52,3 +52,16 @@ http_archive(
"""echo 'py_library(name = "mock", srcs = ["__init__.py"], visibility = ["//visibility:public"],)' > py/mock/BUILD""",
],
)
# Go rules and proto support
http_archive(
name = "io_bazel_rules_go",
sha256 = "8be57ff66da79d9e4bd434c860dce589195b9101b2c187d144014bbca23b5166",
strip_prefix = "rules_go-0.16.3",
urls = [
"https://github.com/bazelbuild/rules_go/archive/0.16.3.tar.gz",
],
)
load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains")
go_rules_dependencies()
go_register_toolchains()

View File

@ -1,6 +1,7 @@
licenses(["notice"]) # Apache 2.0
load("@com_google_protobuf//:protobuf.bzl", "py_proto_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
py_proto_library(
name = "crosstool_config_py_pb2",
@ -9,3 +10,20 @@ py_proto_library(
"//tools/migration:__pkg__",
],
)
proto_library(
name = "crosstool_config_pb2",
srcs = ["crosstool_config.proto"],
visibility = [
"//tools/migration:__pkg__",
],
)
go_proto_library(
name = "crosstool_config_go_proto",
importpath = "third_party/com/github/bazelbuild/bazel/src/main/protobuf/crosstool_config_go_proto",
proto = ":crosstool_config_pb2",
visibility = [
"//tools/migration:__pkg__",
],
)

View File

@ -14,6 +14,9 @@
package(default_visibility = ["//visibility:public"])
# Go rules
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
licenses(["notice"]) # Apache 2.0
py_binary(
@ -82,3 +85,31 @@ py_test(
"@py_mock//py/mock",
],
)
go_binary(
name = "convert_crosstool_to_starlark",
srcs = ["convert_crosstool_to_starlark.go"],
deps = [
":crosstooltostarlarklib",
"//third_party/com/github/bazelbuild/bazel/src/main/protobuf:crosstool_config_go_proto",
"@com_github_golang_protobuf//proto:go_default_library",
],
)
go_library(
name = "crosstooltostarlarklib",
srcs = ["crosstool_to_starlark_lib.go"],
importpath = "tools/migration/crosstooltostarlarklib",
deps = ["//third_party/com/github/bazelbuild/bazel/src/main/protobuf:crosstool_config_go_proto"],
)
go_test(
name = "crosstooltostarlarklib_test",
size = "small",
srcs = ["crosstool_to_starlark_lib_test.go"],
embed = [":crosstooltostarlarklib"],
deps = [
"//third_party/com/github/bazelbuild/bazel/src/main/protobuf:crosstool_config_go_proto",
"@com_github_golang_protobuf//proto:go_default_library",
],
)

View File

@ -0,0 +1,68 @@
/*
The convert_crosstool_to_starlark script takes in a CROSSTOOL file and
generates a Starlark rule.
See https://github.com/bazelbuild/bazel/issues/5380
Example usage:
bazel run \
@rules_cc//tools/migration:convert_crosstool_to_starlark -- \
--crosstool=/path/to/CROSSTOOL \
--output_location=/path/to/cc_config.bzl
*/
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"log"
"github.com/golang/protobuf/proto"
crosstoolpb "third_party/com/github/bazelbuild/bazel/src/main/protobuf/crosstool_config_go_proto"
"tools/migration/crosstooltostarlarklib"
)
var (
crosstoolLocation = flag.String(
"crosstool", "", "Location of the CROSSTOOL file")
outputLocation = flag.String(
"output_location", "", "Location of the output .bzl file")
)
func main() {
if *crosstoolLocation == "" {
log.Fatalf("Missing mandatory argument 'crosstool'")
}
if *outputLocation == "" {
log.Fatalf("Missing mandatory argument 'output_location'")
}
in, err := ioutil.ReadFile(*crosstoolLocation)
if err != nil {
log.Fatalf("Error reading CROSSTOOL file:", err)
}
crosstool := &crosstoolpb.CrosstoolRelease{}
if err := proto.UnmarshalText(string(in), crosstool); err != nil {
log.Fatalf("Failed to parse CROSSTOOL:", err)
}
file, err := os.Create(*outputLocation)
if err != nil {
log.Fatalf("Error creating output file:", err)
}
defer file.Close()
rule, err := crosstooltostarlarklib.Transform(crosstool)
if err != nil {
log.Fatalf("Error converting CROSSTOOL to a Starlark rule:", err)
}
if _, err := file.WriteString(rule); err != nil {
log.Fatalf("Error converting CROSSTOOL to a Starlark rule:", err)
}
fmt.Println("Success!")
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff