2016-06-18 01:45:24 +02:00
|
|
|
// Copyright 2016 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 cc
|
|
|
|
|
|
|
|
// The platform needs to provide the following artifacts for the NDK:
|
|
|
|
// 1. Bionic headers.
|
|
|
|
// 2. Platform API headers.
|
|
|
|
// 3. NDK stub shared libraries.
|
|
|
|
// 4. Bionic static libraries.
|
|
|
|
//
|
|
|
|
// TODO(danalbert): All of the above need to include NOTICE files.
|
|
|
|
//
|
|
|
|
// Components 1 and 2: Headers
|
|
|
|
// The bionic and platform API headers are generalized into a single
|
|
|
|
// `ndk_headers` rule. This rule has a `from` property that indicates a base
|
|
|
|
// directory from which headers are to be taken, and a `to` property that
|
|
|
|
// indicates where in the sysroot they should reside relative to usr/include.
|
|
|
|
// There is also a `srcs` property that is glob compatible for specifying which
|
|
|
|
// headers to include.
|
|
|
|
//
|
|
|
|
// Component 3: Stub Libraries
|
|
|
|
// The shared libraries in the NDK are not the actual shared libraries they
|
|
|
|
// refer to (to prevent people from accidentally loading them), but stub
|
2021-10-28 00:47:06 +02:00
|
|
|
// libraries with placeholder implementations of everything for use at build time
|
2016-06-18 01:45:24 +02:00
|
|
|
// only.
|
|
|
|
//
|
|
|
|
// Since we don't actually need to know anything about the stub libraries aside
|
|
|
|
// from a list of functions and globals to be exposed, we can create these for
|
|
|
|
// every platform level in the current tree. This is handled by the
|
|
|
|
// ndk_library rule.
|
|
|
|
//
|
|
|
|
// Component 4: Static Libraries
|
|
|
|
// The NDK only provides static libraries for bionic, not the platform APIs.
|
|
|
|
// Since these need to be the actual implementation, we can't build old versions
|
|
|
|
// in the current platform tree. As such, legacy versions are checked in
|
|
|
|
// prebuilt to development/ndk, and a current version is built and archived as
|
|
|
|
// part of the platform build. The platfrom already builds these libraries, our
|
|
|
|
// NDK build rules only need to archive them for retrieval so they can be added
|
|
|
|
// to the prebuilts.
|
|
|
|
//
|
|
|
|
// TODO(danalbert): Write `ndk_static_library` rule.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"android/soong/android"
|
2023-08-23 12:06:35 +02:00
|
|
|
"strings"
|
2016-06-18 01:45:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-08-16 02:55:11 +02:00
|
|
|
RegisterNdkModuleTypes(android.InitRegistrationContext)
|
2016-06-18 01:45:24 +02:00
|
|
|
}
|
|
|
|
|
2022-08-16 02:55:11 +02:00
|
|
|
func RegisterNdkModuleTypes(ctx android.RegistrationContext) {
|
2023-09-19 21:04:41 +02:00
|
|
|
ctx.RegisterModuleType("ndk_headers", NdkHeadersFactory)
|
2022-08-16 02:55:11 +02:00
|
|
|
ctx.RegisterModuleType("ndk_library", NdkLibraryFactory)
|
|
|
|
ctx.RegisterModuleType("versioned_ndk_headers", versionedNdkHeadersFactory)
|
|
|
|
ctx.RegisterModuleType("preprocessed_ndk_headers", preprocessedNdkHeadersFactory)
|
2023-05-16 02:58:37 +02:00
|
|
|
ctx.RegisterParallelSingletonType("ndk", NdkSingleton)
|
2022-08-16 02:55:11 +02:00
|
|
|
}
|
|
|
|
|
2019-10-02 07:05:35 +02:00
|
|
|
func getNdkInstallBase(ctx android.PathContext) android.InstallPath {
|
|
|
|
return android.PathForNdkInstall(ctx)
|
2016-06-18 01:45:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the main install directory for the NDK sysroot. Usable with --sysroot.
|
2019-10-02 07:05:35 +02:00
|
|
|
func getNdkSysrootBase(ctx android.PathContext) android.InstallPath {
|
2016-06-18 01:45:24 +02:00
|
|
|
return getNdkInstallBase(ctx).Join(ctx, "sysroot")
|
|
|
|
}
|
|
|
|
|
2017-12-14 00:05:04 +01:00
|
|
|
// The base timestamp file depends on the NDK headers and stub shared libraries,
|
|
|
|
// but not the static libraries. This distinction is needed because the static
|
|
|
|
// libraries themselves might need to depend on the base sysroot.
|
|
|
|
func getNdkBaseTimestampFile(ctx android.PathContext) android.WritablePath {
|
|
|
|
return android.PathForOutput(ctx, "ndk_base.timestamp")
|
|
|
|
}
|
|
|
|
|
2021-12-01 02:31:23 +01:00
|
|
|
// The headers timestamp file depends only on the NDK headers.
|
|
|
|
// This is used mainly for .tidy files that do not need any stub libraries.
|
|
|
|
func getNdkHeadersTimestampFile(ctx android.PathContext) android.WritablePath {
|
|
|
|
return android.PathForOutput(ctx, "ndk_headers.timestamp")
|
|
|
|
}
|
|
|
|
|
2017-12-14 00:05:04 +01:00
|
|
|
// The full timestamp file depends on the base timestamp *and* the static
|
|
|
|
// libraries.
|
|
|
|
func getNdkFullTimestampFile(ctx android.PathContext) android.WritablePath {
|
2016-06-18 01:45:24 +02:00
|
|
|
return android.PathForOutput(ctx, "ndk.timestamp")
|
|
|
|
}
|
|
|
|
|
2023-08-23 12:06:35 +02:00
|
|
|
// The list of all NDK headers as they are located in the repo.
|
|
|
|
// Used for ABI monitoring to track only structures defined in NDK headers.
|
|
|
|
func getNdkABIHeadersFile(ctx android.PathContext) android.WritablePath {
|
|
|
|
return android.PathForOutput(ctx, "ndk_abi_headers.txt")
|
|
|
|
}
|
|
|
|
|
2017-11-29 02:34:01 +01:00
|
|
|
func NdkSingleton() android.Singleton {
|
2016-06-18 01:45:24 +02:00
|
|
|
return &ndkSingleton{}
|
|
|
|
}
|
|
|
|
|
2023-08-23 12:06:35 +02:00
|
|
|
// Collect all NDK exported headers paths into a file that is used to
|
|
|
|
// detect public types that should be ABI monitored.
|
|
|
|
//
|
|
|
|
// Assume that we have the following code in exported header:
|
|
|
|
//
|
|
|
|
// typedef struct Context Context;
|
|
|
|
// typedef struct Output {
|
|
|
|
// ...
|
|
|
|
// } Output;
|
|
|
|
// void DoSomething(Context* ctx, Output* output);
|
|
|
|
//
|
|
|
|
// If none of public headers exported to end-users contain definition of
|
|
|
|
// "struct Context", then "struct Context" layout and members shouldn't be
|
|
|
|
// monitored. However we use DWARF information from a real library, which
|
|
|
|
// may have access to the definition of "string Context" from
|
|
|
|
// implementation headers, and it will leak to ABI.
|
|
|
|
//
|
|
|
|
// STG tool doesn't access source and header files, only DWARF information
|
|
|
|
// from compiled library. And the DWARF contains file name where a type is
|
|
|
|
// defined. So we need a rule to build a list of paths to public headers,
|
|
|
|
// so STG can distinguish private types from public and do not monitor
|
|
|
|
// private types that are not accessible to library users.
|
|
|
|
func writeNdkAbiSrcFilter(ctx android.BuilderContext,
|
|
|
|
headerSrcPaths android.Paths, outputFile android.WritablePath) {
|
|
|
|
var filterBuilder strings.Builder
|
|
|
|
filterBuilder.WriteString("[decl_file_allowlist]\n")
|
|
|
|
for _, headerSrcPath := range headerSrcPaths {
|
|
|
|
filterBuilder.WriteString(headerSrcPath.String())
|
|
|
|
filterBuilder.WriteString("\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
android.WriteFileRule(ctx, outputFile, filterBuilder.String())
|
|
|
|
}
|
|
|
|
|
2016-06-18 01:45:24 +02:00
|
|
|
type ndkSingleton struct{}
|
|
|
|
|
2017-11-29 02:34:01 +01:00
|
|
|
func (n *ndkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
|
2017-12-14 00:05:04 +01:00
|
|
|
var staticLibInstallPaths android.Paths
|
2023-08-23 12:06:35 +02:00
|
|
|
var headerSrcPaths android.Paths
|
|
|
|
var headerInstallPaths android.Paths
|
2017-11-29 02:34:01 +01:00
|
|
|
var installPaths android.Paths
|
|
|
|
var licensePaths android.Paths
|
|
|
|
ctx.VisitAllModules(func(module android.Module) {
|
2017-05-06 08:26:01 +02:00
|
|
|
if m, ok := module.(android.Module); ok && !m.Enabled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-18 01:45:24 +02:00
|
|
|
if m, ok := module.(*headerModule); ok {
|
2023-08-23 12:06:35 +02:00
|
|
|
headerSrcPaths = append(headerSrcPaths, m.srcPaths...)
|
|
|
|
headerInstallPaths = append(headerInstallPaths, m.installPaths...)
|
2016-06-18 01:45:24 +02:00
|
|
|
installPaths = append(installPaths, m.installPaths...)
|
2017-11-29 02:34:01 +01:00
|
|
|
licensePaths = append(licensePaths, m.licensePath)
|
2016-06-18 01:45:24 +02:00
|
|
|
}
|
|
|
|
|
2018-05-25 00:02:16 +02:00
|
|
|
if m, ok := module.(*versionedHeaderModule); ok {
|
2023-08-23 12:06:35 +02:00
|
|
|
headerSrcPaths = append(headerSrcPaths, m.srcPaths...)
|
|
|
|
headerInstallPaths = append(headerInstallPaths, m.installPaths...)
|
2017-02-16 02:31:33 +01:00
|
|
|
installPaths = append(installPaths, m.installPaths...)
|
2017-11-29 02:34:01 +01:00
|
|
|
licensePaths = append(licensePaths, m.licensePath)
|
2017-02-16 02:31:33 +01:00
|
|
|
}
|
|
|
|
|
2018-05-25 00:06:11 +02:00
|
|
|
if m, ok := module.(*preprocessedHeadersModule); ok {
|
2023-08-23 12:06:35 +02:00
|
|
|
headerSrcPaths = append(headerSrcPaths, m.srcPaths...)
|
|
|
|
headerInstallPaths = append(headerInstallPaths, m.installPaths...)
|
2018-05-25 00:06:11 +02:00
|
|
|
installPaths = append(installPaths, m.installPaths...)
|
|
|
|
licensePaths = append(licensePaths, m.licensePath)
|
|
|
|
}
|
|
|
|
|
2016-06-18 01:45:24 +02:00
|
|
|
if m, ok := module.(*Module); ok {
|
2020-10-24 02:22:06 +02:00
|
|
|
if installer, ok := m.installer.(*stubDecorator); ok && m.library.buildStubs() {
|
2016-06-18 01:45:24 +02:00
|
|
|
installPaths = append(installPaths, installer.installPath)
|
|
|
|
}
|
2017-10-13 09:29:00 +02:00
|
|
|
|
|
|
|
if library, ok := m.linker.(*libraryDecorator); ok {
|
2017-11-29 02:34:01 +01:00
|
|
|
if library.ndkSysrootPath != nil {
|
2017-12-14 00:05:04 +01:00
|
|
|
staticLibInstallPaths = append(
|
|
|
|
staticLibInstallPaths, library.ndkSysrootPath)
|
2017-10-13 09:29:00 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-05 01:22:11 +02:00
|
|
|
|
|
|
|
if object, ok := m.linker.(*objectLinker); ok {
|
|
|
|
if object.ndkSysrootPath != nil {
|
|
|
|
staticLibInstallPaths = append(
|
|
|
|
staticLibInstallPaths, object.ndkSysrootPath)
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 01:45:24 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-08-21 07:06:01 +02:00
|
|
|
// Include only a single copy of each license file. The Bionic NOTICE is
|
|
|
|
// long and is referenced by multiple Bionic modules.
|
|
|
|
licensePaths = android.FirstUniquePaths(licensePaths)
|
|
|
|
|
2016-10-20 10:36:11 +02:00
|
|
|
combinedLicense := getNdkInstallBase(ctx).Join(ctx, "NOTICE")
|
2017-11-29 02:34:01 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2017-05-09 22:45:28 +02:00
|
|
|
Rule: android.Cat,
|
|
|
|
Description: "combine licenses",
|
2017-11-29 02:34:01 +01:00
|
|
|
Output: combinedLicense,
|
2017-05-09 22:45:28 +02:00
|
|
|
Inputs: licensePaths,
|
2016-10-20 10:36:11 +02:00
|
|
|
})
|
|
|
|
|
2021-06-15 22:25:25 +02:00
|
|
|
baseDepPaths := append(installPaths, combinedLicense)
|
2016-10-20 10:36:11 +02:00
|
|
|
|
2017-11-29 02:34:01 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2021-06-15 22:25:25 +02:00
|
|
|
Rule: android.Touch,
|
|
|
|
Output: getNdkBaseTimestampFile(ctx),
|
|
|
|
Implicits: baseDepPaths,
|
|
|
|
Validation: getNdkAbiDiffTimestampFile(ctx),
|
2017-12-14 00:05:04 +01:00
|
|
|
})
|
|
|
|
|
2021-12-01 02:31:23 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: android.Touch,
|
|
|
|
Output: getNdkHeadersTimestampFile(ctx),
|
2023-08-23 12:06:35 +02:00
|
|
|
Implicits: headerInstallPaths,
|
2021-12-01 02:31:23 +01:00
|
|
|
})
|
|
|
|
|
2023-08-23 12:06:35 +02:00
|
|
|
writeNdkAbiSrcFilter(ctx, headerSrcPaths, getNdkABIHeadersFile(ctx))
|
|
|
|
|
2017-12-14 00:05:04 +01:00
|
|
|
fullDepPaths := append(staticLibInstallPaths, getNdkBaseTimestampFile(ctx))
|
|
|
|
|
2020-07-30 23:32:55 +02:00
|
|
|
// There's a phony "ndk" rule defined in core/main.mk that depends on this.
|
|
|
|
// `m ndk` will build the sysroots for the architectures in the current
|
|
|
|
// lunch target. `build/soong/scripts/build-ndk-prebuilts.sh` will build the
|
|
|
|
// sysroots for all the NDK architectures and package them so they can be
|
|
|
|
// imported into the NDK's build.
|
2017-12-14 00:05:04 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: android.Touch,
|
|
|
|
Output: getNdkFullTimestampFile(ctx),
|
|
|
|
Implicits: fullDepPaths,
|
2016-06-18 01:45:24 +02:00
|
|
|
})
|
|
|
|
}
|