2020-07-15 12:06:41 +02:00
|
|
|
// Copyright 2020 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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-09-07 08:47:21 +02:00
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
"android/soong/bp2build"
|
2020-07-15 12:06:41 +02:00
|
|
|
)
|
|
|
|
|
2021-02-17 19:22:03 +01:00
|
|
|
func createBazelQueryView(ctx *bp2build.CodegenContext, bazelQueryViewDir string) error {
|
2021-09-07 08:47:21 +02:00
|
|
|
os.RemoveAll(bazelQueryViewDir)
|
2020-11-26 01:06:39 +01:00
|
|
|
ruleShims := bp2build.CreateRuleShims(android.ModuleTypeFactories())
|
2021-02-19 06:48:40 +01:00
|
|
|
|
2021-08-26 14:37:59 +02:00
|
|
|
res, err := bp2build.GenerateBazelTargets(ctx, true)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-10-19 13:49:50 +02:00
|
|
|
|
2021-08-26 14:37:59 +02:00
|
|
|
filesToWrite := bp2build.CreateBazelFiles(ruleShims, res.BuildDirToTargets(), bp2build.QueryView)
|
2020-11-26 01:06:39 +01:00
|
|
|
for _, f := range filesToWrite {
|
|
|
|
if err := writeReadOnlyFile(bazelQueryViewDir, f); err != nil {
|
Generate .bzl rule definitions for every module type in Soong, and
surface module properties as attributes.
This CL maps int, bool, string and string_list module props from Soong
modules into their respective Bazel targets.
With this CL, you can now query modules based on their properties. For
example:
$ bazel query 'attr(nocrt, 1, //...)'
$ bazel query 'attr(apex_available, //apex_available:platform, //...)'
$ bazel query //art/dalvikvm:dalvikvm--linux_glibc_x86_64 --output=build | grep compile_multilib
Test: m bazel_overlay && cd out/soong/bazel_overlay && bazel cquery 'attr(apex_available, com.android.runtime, //...)'
Test: soong_build tests
Fixes: 162720644
Fixes: 164320355
Change-Id: Iea8e594b952feccac3281f36dd6bdee8e7d62c3a
2020-08-27 11:40:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
return nil
|
2020-07-15 12:06:41 +02:00
|
|
|
}
|
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
// The auto-conversion directory should be read-only, sufficient for bazel query. The files
|
|
|
|
// are not intended to be edited by end users.
|
|
|
|
func writeReadOnlyFile(dir string, f bp2build.BazelFile) error {
|
|
|
|
dir = filepath.Join(dir, f.Dir)
|
|
|
|
if err := createDirectoryIfNonexistent(dir); err != nil {
|
|
|
|
return err
|
2020-07-15 12:06:41 +02:00
|
|
|
}
|
2020-11-26 01:06:39 +01:00
|
|
|
pathToFile := filepath.Join(dir, f.Basename)
|
2020-07-15 12:06:41 +02:00
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
// 0444 is read-only
|
|
|
|
err := ioutil.WriteFile(pathToFile, []byte(f.Contents), 0444)
|
2020-07-15 12:06:41 +02:00
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
return err
|
2020-07-15 12:06:41 +02:00
|
|
|
}
|
|
|
|
|
2020-11-26 01:06:39 +01:00
|
|
|
func createDirectoryIfNonexistent(dir string) error {
|
2020-10-19 13:49:50 +02:00
|
|
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
2020-11-26 01:06:39 +01:00
|
|
|
return os.MkdirAll(dir, os.ModePerm)
|
|
|
|
} else {
|
|
|
|
return err
|
Surface module properties as Bazel BUILD target attributes in the Bazel overlay
This patchset changes bazel_overlay to generate soong_module as a macro,
instead of a rule, and generate module properties in the BUILD files as
kwargs to the soong_module macro.
Here's a sample of the new BUILD files with module properties:
bionic/libdl/BUILD.bazel:
https://paste.googleplex.com/6484466996346880?raw
art/build/apex/BUILD.bazel:
https://paste.googleplex.com/5461276001042432?raw
bionic/apex/BUILD.bazel:
https://paste.googleplex.com/4932795173437440?raw
soong_module is now a macro that conditionally expands to underlying
soong_<module type> rules with statically defined attributes. In this
CL, we are starting with a hardcoded filegroup rule definition to
demonstrate the conditional rule loading within the soong_module macro.
If the module_type matches an existing Bazel rule, soong_module forwards
the entire **kwargs into the rule, which Bazel typechecks.
Non-filegroup module types will be expanded into generic_soong_module,
but with the kwargs dropped.
This approach allows us to:
1) Programmtically generate soong_<module type> rules for all module
types available in Soong, together with the statically defined attribute
types in `attrs`.
2) Incrementally migrate and test individual module types from
generic_soong_module to their module rule shims.
3) Swap out the module rule shims to the actual Bazel rules (e.g
cc_library, java_library) and perform attribute manipulation in Starlark
itself.
Example of querying against the 'srcs' attribute in soong_filegroup:
```
$ bazel cquery 'kind(soong_filegroup, //...)' | wc -l
590
$ bazel cquery --output=build 'attr(srcs, "linker.cpp",
kind(soong_filegroup, //bionic/...))'
INFO: Analyzed 3907 targets (0 packages loaded, 0 targets configured).
INFO: Found 3907 targets...
/usr/local/google/home/jingwen/aosp/out/soong/bazel_overlay/bionic/linker/BUILD.bazel:4144:13
soong_filegroup(
name = "linker_sources",
generator_name = "linker_sources",
generator_function = "soong_module",
generator_location = "bionic/linker/BUILD.bazel:4144:13",
srcs = ["dlfcn.cpp", "linker.cpp", "linker_block_allocator.cpp",
"linker_dlwarning.cpp", "linker_cfi.cpp", "linker_config.cpp",
"linker_debug.cpp", "linker_gdb_support.cpp", "linker_globals.cpp",
"linker_libc_support.c", "linker_libcxx_support.cpp",
"linker_namespaces.cpp", "linker_logger.cpp",
"linker_mapped_file_fragment.cpp", "linker_phdr.cpp",
"linker_relocate.cpp", "linker_sdk_versions.cpp", "linker_soinfo.cpp",
"linker_tls.cpp", "linker_utils.cpp", "rt.cpp"],
deps = [],
)
/usr/local/google/home/jingwen/aosp/out/soong/bazel_overlay/soong_module.bzl:32:23
in <toplevel>
```
This CL is known to be lacking the following features, and will be looked at in follow up CLs:
1) Pretty printing reflect.Interface properties, like arch, multilib and
dists.
2) Generating module Bazel rule shims for all module types, instead of
hardcoding them like `soong_filegroup`.
Bug: 162720644
Test: bazel_overlay_test.go (soong build test)
Test: m bazel_overlay && cd out/soong/bazel_overlay && bazel cquery //...
Signed-off-by: Jingwen Chen <jingwen@google.com>
Change-Id: Ic1e448887eb540ed15a55bc4090cf75a4d832d41
2020-08-07 16:16:34 +02:00
|
|
|
}
|
|
|
|
}
|