Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 336ad7a6 authored by Colin Cross's avatar Colin Cross
Browse files

Fix java_import and android_library_import conversions

java_import and android_library_import modules can't be handled
directly in androidmk because the results may depend on properties
that haven't been parsed yet.  Add a bpfix pass (which is
automatically included at the end of androidmk) to select
android_library_import vs. java_import based on the extension
of the prebuilt file, and convert the srcs property to jars or aars
as appropriate.

Bug: 73724997
Test: androidmk_test.go
Change-Id: I1024742e9e96d5e1e88c3cc139eeb0d5a2f6849b
parent 3fad895e
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
@@ -492,6 +492,36 @@ include $(call all-makefiles-under,$(LOCAL_PATH))
			}
		`,
	},
	{
		desc: "java prebuilt",
		in: `
			include $(CLEAR_VARS)
			LOCAL_SRC_FILES := test.jar
			LOCAL_MODULE_CLASS := JAVA_LIBRARIES
			include $(BUILD_PREBUILT)
		`,
		expected: `
			java_import {
				jars: ["test.jar"],

			}
		`,
	},
	{
		desc: "aar prebuilt",
		in: `
			include $(CLEAR_VARS)
			LOCAL_SRC_FILES := test.aar
			LOCAL_MODULE_CLASS := JAVA_LIBRARIES
			include $(BUILD_PREBUILT)
		`,
		expected: `
			android_library_import {
				aars: ["test.aar"],

			}
		`,
	},
}

func reformatBlueprint(input string) string {
+50 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package bpfix
import (
	"bytes"
	"fmt"
	"path/filepath"

	"github.com/google/blueprint/parser"
)
@@ -27,6 +28,7 @@ import (
// A FixRequest doesn't specify whether to do a dry run or where to write the results; that's in cmd/bpfix.go
type FixRequest struct {
	simplifyKnownRedundantVariables    bool
	rewriteIncorrectAndroidmkPrebuilts bool
}

func NewFixRequest() FixRequest {
@@ -36,6 +38,7 @@ func NewFixRequest() FixRequest {
func (r FixRequest) AddAll() (result FixRequest) {
	result = r
	result.simplifyKnownRedundantVariables = true
	result.rewriteIncorrectAndroidmkPrebuilts = true
	return result
}

@@ -87,6 +90,12 @@ func fixTreeOnce(tree *parser.File, config FixRequest) error {
			return err
		}
	}
	if config.rewriteIncorrectAndroidmkPrebuilts {
		err := rewriteIncorrectAndroidmkPrebuilts(tree)
		if err != nil {
			return err
		}
	}
	return nil
}

@@ -95,6 +104,38 @@ func simplifyKnownPropertiesDuplicatingEachOther(tree *parser.File) error {
	return removeMatchingModuleListProperties(tree, "export_include_dirs", "local_include_dirs")
}

func rewriteIncorrectAndroidmkPrebuilts(tree *parser.File) error {
	for _, def := range tree.Defs {
		mod, ok := def.(*parser.Module)
		if !ok {
			continue
		}
		if mod.Type != "java_import" {
			continue
		}
		srcs, ok := getLiteralListProperty(mod, "srcs")
		if !ok {
			continue
		}
		if len(srcs.Values) == 0 {
			continue
		}
		src, ok := srcs.Values[0].(*parser.String)
		if !ok {
			continue
		}
		switch filepath.Ext(src.Value) {
		case ".jar":
			renameProperty(mod, "srcs", "jars")
		case ".aar":
			renameProperty(mod, "srcs", "aars")
			mod.Type = "android_library_import"
		}
	}

	return nil
}

// removes from <items> every item present in <removals>
func filterExpressionList(items *parser.List, removals *parser.List) {
	writeIndex := 0
@@ -146,3 +187,11 @@ func getLiteralListProperty(mod *parser.Module, name string) (list *parser.List,
	list, ok = prop.Value.(*parser.List)
	return list, ok
}

func renameProperty(mod *parser.Module, from, to string) {
	for _, prop := range mod.Properties {
		if prop.Name == from {
			prop.Name = to
		}
	}
}