2019-01-24 22:54:21 +00:00
|
|
|
/*
|
|
|
|
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"
|
2019-02-05 14:09:12 +00:00
|
|
|
"os/user"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2019-01-24 22:54:21 +00:00
|
|
|
|
2019-03-14 15:17:32 +00:00
|
|
|
// Google internal base/go package, commented out by copybara
|
2019-01-24 22:54:21 +00:00
|
|
|
"log"
|
|
|
|
crosstoolpb "third_party/com/github/bazelbuild/bazel/src/main/protobuf/crosstool_config_go_proto"
|
2020-03-10 11:40:03 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2019-01-24 22:54:21 +00:00
|
|
|
|
|
|
|
"tools/migration/crosstooltostarlarklib"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
crosstoolLocation = flag.String(
|
|
|
|
"crosstool", "", "Location of the CROSSTOOL file")
|
|
|
|
outputLocation = flag.String(
|
|
|
|
"output_location", "", "Location of the output .bzl file")
|
|
|
|
)
|
|
|
|
|
2019-02-05 14:09:12 +00:00
|
|
|
func toAbsolutePath(pathString string) (string, error) {
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
homeDir := usr.HomeDir
|
|
|
|
|
|
|
|
if strings.HasPrefix(pathString, "~") {
|
|
|
|
return path.Join(homeDir, pathString[1:]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if path.IsAbs(pathString) {
|
|
|
|
return pathString, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
workingDirectory := os.Getenv("BUILD_WORKING_DIRECTORY")
|
|
|
|
return path.Join(workingDirectory, pathString), nil
|
|
|
|
}
|
|
|
|
|
2019-01-24 22:54:21 +00:00
|
|
|
func main() {
|
2019-02-05 13:57:08 +00:00
|
|
|
flag.Parse()
|
2019-01-24 22:54:21 +00:00
|
|
|
|
|
|
|
if *crosstoolLocation == "" {
|
|
|
|
log.Fatalf("Missing mandatory argument 'crosstool'")
|
|
|
|
}
|
2019-02-05 14:09:12 +00:00
|
|
|
crosstoolPath, err := toAbsolutePath(*crosstoolLocation)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error while resolving CROSSTOOL location:", err)
|
|
|
|
}
|
|
|
|
|
2019-01-24 22:54:21 +00:00
|
|
|
if *outputLocation == "" {
|
|
|
|
log.Fatalf("Missing mandatory argument 'output_location'")
|
|
|
|
}
|
2019-02-05 14:09:12 +00:00
|
|
|
outputPath, err := toAbsolutePath(*outputLocation)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error resolving output location:", err)
|
|
|
|
}
|
2019-01-24 22:54:21 +00:00
|
|
|
|
2019-02-05 14:09:12 +00:00
|
|
|
in, err := ioutil.ReadFile(crosstoolPath)
|
2019-01-24 22:54:21 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-02-05 14:09:12 +00:00
|
|
|
file, err := os.Create(outputPath)
|
2019-01-24 22:54:21 +00:00
|
|
|
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!")
|
|
|
|
}
|