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

Commit 2115d351 authored by Wei Li's avatar Wei Li Committed by Gerrit Code Review
Browse files

Merge "Add the initial implementation of bp2build converter for java_library,...

Merge "Add the initial implementation of bp2build converter for java_library, java_library_host, java_binary_host and cc_library_host_shared so signapk tool can be built with Bazel."
parents bc82ba55 bafb6d66
Loading
Loading
Loading
Loading
+33 −3
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
package android

import (
	"bufio"
	"errors"
	"fmt"
	"io/ioutil"
	"path/filepath"
@@ -206,7 +208,8 @@ var (
		"build/bazel/platforms":/* recursive = */ true,
		"build/bazel/product_variables":/* recursive = */ true,
		"build/bazel_common_rules":/* recursive = */ true,
		"build/make/tools":/* recursive = */ true,
		// build/make/tools/signapk BUILD file is generated, so build/make/tools is not recursive.
		"build/make/tools":/* recursive = */ false,
		"build/pesto":/* recursive = */ true,

		// external/bazelbuild-rules_android/... is needed by mixed builds, otherwise mixed builds analysis fails
@@ -237,6 +240,7 @@ var (
		"bootable/recovery/tools/recovery_l10n": Bp2BuildDefaultTrue,
		"build/bazel/examples/soong_config_variables":        Bp2BuildDefaultTrueRecursively,
		"build/bazel/examples/apex/minimal":                  Bp2BuildDefaultTrueRecursively,
		"build/make/tools/signapk":                           Bp2BuildDefaultTrue,
		"build/soong":                                        Bp2BuildDefaultTrue,
		"build/soong/cc/libbuildversion":                     Bp2BuildDefaultTrue, // Skip tests subdir
		"build/soong/cc/ndkstubgen":                          Bp2BuildDefaultTrue,
@@ -279,7 +283,9 @@ var (
		"development/sdk":                                    Bp2BuildDefaultTrueRecursively,
		"external/arm-optimized-routines":                    Bp2BuildDefaultTrueRecursively,
		"external/boringssl":                                 Bp2BuildDefaultTrueRecursively,
		"external/bouncycastle":                              Bp2BuildDefaultTrue,
		"external/brotli":                                    Bp2BuildDefaultTrue,
		"external/conscrypt":                                 Bp2BuildDefaultTrue,
		"external/fmtlib":                                    Bp2BuildDefaultTrueRecursively,
		"external/google-benchmark":                          Bp2BuildDefaultTrueRecursively,
		"external/googletest":                                Bp2BuildDefaultTrueRecursively,
@@ -349,6 +355,8 @@ var (
		"system/timezone/output_data":                        Bp2BuildDefaultTrueRecursively,
		"system/unwinding/libbacktrace":                      Bp2BuildDefaultTrueRecursively,
		"system/unwinding/libunwindstack":                    Bp2BuildDefaultTrueRecursively,
		"tools/apksig":                                       Bp2BuildDefaultTrue,
		"tools/platform-compat/java/android/compat":          Bp2BuildDefaultTrueRecursively,
	}

	// Per-module denylist to always opt modules out of both bp2build and mixed builds.
@@ -404,8 +412,11 @@ var (
		"lib_linker_config_proto_lite", // contains .proto sources

		"libprotobuf-python",               // contains .proto sources
		"libprotobuf-internal-protos",      // we don't handle path property for fileegroups
		"libprotobuf-internal-python-srcs", // we don't handle path property for fileegroups
		"libprotobuf-internal-protos",      // b/210751803, we don't handle path property for filegroups
		"libprotobuf-internal-python-srcs", // b/210751803, we don't handle path property for filegroups
		"libprotobuf-java-full",            // b/210751803, we don't handle path property for filegroups
		"libprotobuf-java-util-full",       // b/210751803, we don't handle path property for filegroups
		"conscrypt",                        // b/210751803, we don't handle path property for filegroups

		"libseccomp_policy", // b/201094425: depends on func_to_syscall_nrs, which depends on py_binary, which is unsupported in mixed builds.
		"libfdtrack",        // depends on unconverted module libunwindstack
@@ -640,3 +651,22 @@ func convertWithBp2build(ctx TopDownMutatorContext) {

	bModule.ConvertWithBp2build(ctx)
}

// GetMainClassInManifest scans the manifest file specified in filepath and returns
// the value of attribute Main-Class in the manifest file if it exists, or returns error.
// WARNING: this is for bp2build converters of java_* modules only.
func GetMainClassInManifest(c Config, filepath string) (string, error) {
	file, err := c.fs.Open(filepath)
	if err != nil {
		return "", err
	}
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := scanner.Text()
		if strings.HasPrefix(line, "Main-Class:") {
			return strings.TrimSpace(line[len("Main-Class:"):]), nil
		}
	}

	return "", errors.New("Main-Class is not found.")
}
+63 −0
Original line number Diff line number Diff line
// Copyright 2021 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bp2build

import (
	"testing"

	"android/soong/android"
	"android/soong/cc"
	"android/soong/java"
)

func runJavaBinaryHostTestCase(t *testing.T, tc bp2buildTestCase) {
	t.Helper()
	(&tc).moduleTypeUnderTest = "java_binary_host"
	(&tc).moduleTypeUnderTestFactory = java.BinaryHostFactory
	runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
		ctx.RegisterModuleType("cc_library_host_shared", cc.LibraryHostSharedFactory)
	}, tc)
}

var fs = map[string]string{
	"test.mf": "Main-Class: com.android.test.MainClass",
	"other/Android.bp": `cc_library_host_shared {
    name: "jni-lib-1",
    stl: "none",
}`,
}

func TestJavaBinaryHost(t *testing.T) {
	runJavaBinaryHostTestCase(t, bp2buildTestCase{
		description: "java_binary_host with srcs, exclude_srcs, jni_libs and manifest.",
		filesystem:  fs,
		blueprint: `java_binary_host {
    name: "java-binary-host-1",
    srcs: ["a.java", "b.java"],
    exclude_srcs: ["b.java"],
    manifest: "test.mf",
    jni_libs: ["jni-lib-1"],
    bazel_module: { bp2build_available: true },
}`,
		expectedBazelTargets: []string{
			makeBazelTarget("java_binary", "java-binary-host-1", attrNameToString{
				"srcs":       `["a.java"]`,
				"main_class": `"com.android.test.MainClass"`,
				"deps":       `["//other:jni-lib-1"]`,
				"jvm_flags":  `["-Djava.library.path=$${RUNPATH}other"]`,
			}),
		},
	})
}
+57 −0
Original line number Diff line number Diff line
// Copyright 2021 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bp2build

import (
	"testing"

	"android/soong/android"
	"android/soong/java"
)

func runJavaLibraryTestCase(t *testing.T, tc bp2buildTestCase) {
	t.Helper()
	(&tc).moduleTypeUnderTest = "java_library"
	(&tc).moduleTypeUnderTestFactory = java.LibraryFactory
	runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
}

func TestJavaLibrary(t *testing.T) {
	runJavaLibraryTestCase(t, bp2buildTestCase{
		description: "java_library with srcs, exclude_srcs and libs",
		blueprint: `java_library {
    name: "java-lib-1",
    srcs: ["a.java", "b.java"],
    exclude_srcs: ["b.java"],
    libs: ["java-lib-2"],
    bazel_module: { bp2build_available: true },
}

java_library {
    name: "java-lib-2",
    srcs: ["b.java"],
    bazel_module: { bp2build_available: true },
}`,
		expectedBazelTargets: []string{
			makeBazelTarget("java_library", "java-lib-1", attrNameToString{
				"srcs": `["a.java"]`,
				"deps": `[":java-lib-2"]`,
			}),
			makeBazelTarget("java_library", "java-lib-2", attrNameToString{
				"srcs": `["b.java"]`,
			}),
		},
	})
}
+57 −0
Original line number Diff line number Diff line
// Copyright 2021 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bp2build

import (
	"testing"

	"android/soong/android"
	"android/soong/java"
)

func runJavaLibraryHostTestCase(t *testing.T, tc bp2buildTestCase) {
	t.Helper()
	(&tc).moduleTypeUnderTest = "java_library_host"
	(&tc).moduleTypeUnderTestFactory = java.LibraryHostFactory
	runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
}

func TestJavaLibraryHost(t *testing.T) {
	runJavaLibraryHostTestCase(t, bp2buildTestCase{
		description: "java_library_host with srcs, exclude_srcs and libs",
		blueprint: `java_library_host {
    name: "java-lib-host-1",
    srcs: ["a.java", "b.java"],
    exclude_srcs: ["b.java"],
    libs: ["java-lib-host-2"],
    bazel_module: { bp2build_available: true },
}

java_library_host {
    name: "java-lib-host-2",
    srcs: ["c.java"],
    bazel_module: { bp2build_available: true },
}`,
		expectedBazelTargets: []string{
			makeBazelTarget("java_library", "java-lib-host-1", attrNameToString{
				"srcs": `["a.java"]`,
				"deps": `[":java-lib-host-2"]`,
			}),
			makeBazelTarget("java_library", "java-lib-host-2", attrNameToString{
				"srcs": `["c.java"]`,
			}),
		},
	})
}
+0 −4
Original line number Diff line number Diff line
@@ -3458,10 +3458,6 @@ func (c *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
	} else if c.Object() {
		objectBp2Build(ctx, c)
	} else if c.CcLibrary() {
		if c.hod == android.HostSupported {
			return
		}

		static := c.BuildStaticVariant()
		shared := c.BuildSharedVariant()
		prebuilt := c.IsPrebuilt()
Loading