Merge "Make mk2rbc output paths relative to android root"

This commit is contained in:
Cole Faust
2023-05-11 17:04:46 +00:00
committed by Gerrit Code Review
3 changed files with 36 additions and 4 deletions

View File

@@ -163,6 +163,21 @@ var ignoredDefines = map[string]bool{
var identifierFullMatchRegex = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$") var identifierFullMatchRegex = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
func RelativeToCwd(path string) (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
path, err = filepath.Rel(cwd, path)
if err != nil {
return "", err
}
if strings.HasPrefix(path, "../") {
return "", fmt.Errorf("Could not make path relative to current working directory: " + path)
}
return path, nil
}
// Conversion request parameters // Conversion request parameters
type Request struct { type Request struct {
MkFile string // file to convert MkFile string // file to convert
@@ -320,6 +335,14 @@ func (gctx *generationContext) emitPreamble() {
loadedSubConfigs := make(map[string]string) loadedSubConfigs := make(map[string]string)
for _, mi := range gctx.starScript.inherited { for _, mi := range gctx.starScript.inherited {
uri := mi.path uri := mi.path
if strings.HasPrefix(uri, "/") && !strings.HasPrefix(uri, "//") {
var err error
uri, err = RelativeToCwd(uri)
if err != nil {
panic(err)
}
uri = "//" + uri
}
if m, ok := loadedSubConfigs[uri]; ok { if m, ok := loadedSubConfigs[uri]; ok {
// No need to emit load statement, but fix module name. // No need to emit load statement, but fix module name.
mi.moduleLocalName = m mi.moduleLocalName = m

View File

@@ -187,7 +187,7 @@ func main() {
quit(fmt.Errorf("the product launcher input variables file failed to convert")) quit(fmt.Errorf("the product launcher input variables file failed to convert"))
} }
err := writeGenerated(*launcher, mk2rbc.Launcher(outputFilePath(files[0]), outputFilePath(*inputVariables), err := writeGenerated(*launcher, mk2rbc.Launcher(outputModulePath(files[0]), outputModulePath(*inputVariables),
mk2rbc.MakePath2ModuleName(files[0]))) mk2rbc.MakePath2ModuleName(files[0])))
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s", files[0], err) fmt.Fprintf(os.Stderr, "%s: %s", files[0], err)
@@ -205,7 +205,7 @@ func main() {
quit(fmt.Errorf("the board launcher input variables file failed to convert")) quit(fmt.Errorf("the board launcher input variables file failed to convert"))
} }
err := writeGenerated(*boardlauncher, mk2rbc.BoardLauncher( err := writeGenerated(*boardlauncher, mk2rbc.BoardLauncher(
outputFilePath(files[0]), outputFilePath(*inputVariables))) outputModulePath(files[0]), outputModulePath(*inputVariables)))
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s", files[0], err) fmt.Fprintf(os.Stderr, "%s: %s", files[0], err)
ok = false ok = false
@@ -402,6 +402,15 @@ func outputFilePath(mkFile string) string {
return path return path
} }
func outputModulePath(mkFile string) string {
path := outputFilePath(mkFile)
path, err := mk2rbc.RelativeToCwd(path)
if err != nil {
panic(err)
}
return "//" + path
}
func writeGenerated(path string, contents string) error { func writeGenerated(path string, contents string) error {
if err := os.MkdirAll(filepath.Dir(path), os.ModeDir|os.ModePerm); err != nil { if err := os.MkdirAll(filepath.Dir(path), os.ModeDir|os.ModePerm); err != nil {
return err return err

View File

@@ -42,8 +42,8 @@ func TestSoongVariables(t *testing.T) {
{"BUILD_ID", VarClassSoong, starlarkTypeString}, {"BUILD_ID", VarClassSoong, starlarkTypeString},
{"PLATFORM_SDK_VERSION", VarClassSoong, starlarkTypeInt}, {"PLATFORM_SDK_VERSION", VarClassSoong, starlarkTypeInt},
{"DEVICE_PACKAGE_OVERLAYS", VarClassSoong, starlarkTypeList}, {"DEVICE_PACKAGE_OVERLAYS", VarClassSoong, starlarkTypeList},
{"ENABLE_CFI", VarClassSoong, starlarkTypeBool}, {"ENABLE_CFI", VarClassSoong, starlarkTypeString},
{"ENABLE_PREOPT", VarClassSoong, starlarkTypeBool}, {"ENABLE_PREOPT", VarClassSoong, starlarkTypeString},
}} }}
if !reflect.DeepEqual(expected, actual) { if !reflect.DeepEqual(expected, actual) {
t.Errorf("\nExpected: %v\n Actual: %v", expected, actual) t.Errorf("\nExpected: %v\n Actual: %v", expected, actual)