From fb9a7f99be021096ede865b1fa2a427af4845c11 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Tue, 6 Jul 2021 17:18:42 +0100 Subject: [PATCH] Support generating sdk snapshots that can be selected by Soong config vars Allows the builder of an sdk snapshot to specify a Soong config variable that can be used to control whether the snapshot's prebuilts are used or not. Bug: 193523070 Test: m nothing Change-Id: Ib09500ba61befc1202dff61dc06847c730ba9253 --- sdk/sdk_test.go | 43 +++++++++++++++++++++++++++++++++++++++++ sdk/update.go | 46 +++++++++++++++++++++++++++++++++++++++++++- ui/build/dumpvars.go | 1 + 3 files changed, 89 insertions(+), 1 deletion(-) diff --git a/sdk/sdk_test.go b/sdk/sdk_test.go index a13b0d7d0..97fb2485f 100644 --- a/sdk/sdk_test.go +++ b/sdk/sdk_test.go @@ -556,6 +556,49 @@ java_import { jars: ["java/myjavalib.jar"], } +sdk_snapshot { + name: "mysdk@current", + visibility: ["//visibility:public"], + java_header_libs: ["mysdk_myjavalib@current"], +} + `), + ) + }) + + t.Run("SOONG_SDK_SNAPSHOT_USE_SOURCE_CONFIG_VAR=module:build_from_source", func(t *testing.T) { + result := android.GroupFixturePreparers( + preparer, + android.FixtureMergeEnv(map[string]string{ + "SOONG_SDK_SNAPSHOT_USE_SOURCE_CONFIG_VAR": "module:build_from_source", + }), + ).RunTest(t) + + checkZipFile(t, result, "out/soong/.intermediates/mysdk/common_os/mysdk-current.zip") + + CheckSnapshot(t, result, "mysdk", "", + checkAndroidBpContents(` +// This is auto-generated. DO NOT EDIT. + +java_import { + name: "mysdk_myjavalib@current", + sdk_member_name: "myjavalib", + visibility: ["//visibility:public"], + apex_available: ["//apex_available:platform"], + jars: ["java/myjavalib.jar"], +} + +java_import { + name: "myjavalib", + prefer: false, + use_source_config_var: { + config_namespace: "module", + var_name: "build_from_source", + }, + visibility: ["//visibility:public"], + apex_available: ["//apex_available:platform"], + jars: ["java/myjavalib.jar"], +} + sdk_snapshot { name: "mysdk@current", visibility: ["//visibility:public"], diff --git a/sdk/update.go b/sdk/update.go index b146b62c8..6da3756be 100644 --- a/sdk/update.go +++ b/sdk/update.go @@ -35,6 +35,37 @@ import ( // By default every unversioned module in the generated snapshot has prefer: false. Building it // with SOONG_SDK_SNAPSHOT_PREFER=true will force them to use prefer: true. // +// SOONG_SDK_SNAPSHOT_USE_SOURCE_CONFIG_VAR +// If set this specifies the Soong config var that can be used to control whether the prebuilt +// modules from the generated snapshot or the original source modules. Values must be a colon +// separated pair of strings, the first of which is the Soong config namespace, and the second +// is the name of the variable within that namespace. +// +// The config namespace and var name are used to set the `use_source_config_var` property. That +// in turn will cause the generated prebuilts to use the soong config variable to select whether +// source or the prebuilt is used. +// e.g. If an sdk snapshot is built using: +// m SOONG_SDK_SNAPSHOT_USE_SOURCE_CONFIG_VAR=acme:build_from_source sdkextensions-sdk +// Then the resulting snapshot will include: +// use_source_config_var: { +// config_namespace: "acme", +// var_name: "build_from_source", +// } +// +// Assuming that the config variable is defined in .mk using something like: +// $(call add_soong_config_namespace,acme) +// $(call add_soong_config_var_value,acme,build_from_source,true) +// +// Then when the snapshot is unpacked in the repository it will have the following behavior: +// m droid - will use the sdkextensions-sdk prebuilts if present. Otherwise, it will use the +// sources. +// m SOONG_CONFIG_acme_build_from_source=true droid - will use the sdkextensions-sdk +// sources, if present. Otherwise, it will use the prebuilts. +// +// This is a temporary mechanism to control the prefer flags and will be removed once a more +// maintainable solution has been implemented. +// TODO(b/174997203): Remove when no longer necessary. +// // SOONG_SDK_SNAPSHOT_VERSION // This provides control over the version of the generated snapshot. // @@ -760,6 +791,8 @@ func (t unversionedToVersionedTransformation) transformModule(module *bpModule) module.insertAfter("name", "sdk_member_name", name) // Remove the prefer property if present as versioned modules never need marking with prefer. module.removeProperty("prefer") + // Ditto for use_source_config_var + module.removeProperty("use_source_config_var") return module } @@ -1627,13 +1660,24 @@ func (s *sdk) createMemberSnapshot(ctx *memberContext, member *sdkMember, bpModu // snapshot to be created that sets prefer: true. // TODO(b/174997203): Remove once the ability to select the modules to prefer can be done // dynamically at build time not at snapshot generation time. - prefer := ctx.sdkMemberContext.Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_PREFER") + config := ctx.sdkMemberContext.Config() + prefer := config.IsEnvTrue("SOONG_SDK_SNAPSHOT_PREFER") // Set prefer. Setting this to false is not strictly required as that is the default but it does // provide a convenient hook to post-process the generated Android.bp file, e.g. in tests to // check the behavior when a prebuilt is preferred. It also makes it explicit what the default // behavior is for the module. bpModule.insertAfter("name", "prefer", prefer) + + configVar := config.Getenv("SOONG_SDK_SNAPSHOT_USE_SOURCE_CONFIG_VAR") + if configVar != "" { + parts := strings.Split(configVar, ":") + cfp := android.ConfigVarProperties{ + Config_namespace: proptools.StringPtr(parts[0]), + Var_name: proptools.StringPtr(parts[1]), + } + bpModule.insertAfter("prefer", "use_source_config_var", cfp) + } } // Group the variants by os type. diff --git a/ui/build/dumpvars.go b/ui/build/dumpvars.go index 83c8865de..f3c442ec8 100644 --- a/ui/build/dumpvars.go +++ b/ui/build/dumpvars.go @@ -163,6 +163,7 @@ var BannerVars = []string{ "AUX_OS_VARIANT_LIST", "PRODUCT_SOONG_NAMESPACES", "SOONG_SDK_SNAPSHOT_PREFER", + "SOONG_SDK_SNAPSHOT_USE_SOURCE_CONFIG_VAR", "SOONG_SDK_SNAPSHOT_VERSION", }