blob: feb3880376e901c60221f17f52a998d3e0cc26e1 [file] [log] [blame]
Dan Albert914449f2016-06-17 16:45:24 -07001// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// The platform needs to provide the following artifacts for the NDK:
18// 1. Bionic headers.
19// 2. Platform API headers.
20// 3. NDK stub shared libraries.
21// 4. Bionic static libraries.
22//
23// TODO(danalbert): All of the above need to include NOTICE files.
24//
25// Components 1 and 2: Headers
26// The bionic and platform API headers are generalized into a single
27// `ndk_headers` rule. This rule has a `from` property that indicates a base
28// directory from which headers are to be taken, and a `to` property that
29// indicates where in the sysroot they should reside relative to usr/include.
30// There is also a `srcs` property that is glob compatible for specifying which
31// headers to include.
32//
33// Component 3: Stub Libraries
34// The shared libraries in the NDK are not the actual shared libraries they
35// refer to (to prevent people from accidentally loading them), but stub
Joe Onoratob4638c12021-10-27 15:47:06 -070036// libraries with placeholder implementations of everything for use at build time
Dan Albert914449f2016-06-17 16:45:24 -070037// only.
38//
39// Since we don't actually need to know anything about the stub libraries aside
40// from a list of functions and globals to be exposed, we can create these for
41// every platform level in the current tree. This is handled by the
42// ndk_library rule.
43//
44// Component 4: Static Libraries
45// The NDK only provides static libraries for bionic, not the platform APIs.
46// Since these need to be the actual implementation, we can't build old versions
47// in the current platform tree. As such, legacy versions are checked in
48// prebuilt to development/ndk, and a current version is built and archived as
49// part of the platform build. The platfrom already builds these libraries, our
50// NDK build rules only need to archive them for retrieval so they can be added
51// to the prebuilts.
52//
53// TODO(danalbert): Write `ndk_static_library` rule.
54
55import (
Dan Albert914449f2016-06-17 16:45:24 -070056 "android/soong/android"
57)
58
59func init() {
Spandan Das0773a602022-08-16 00:55:11 +000060 RegisterNdkModuleTypes(android.InitRegistrationContext)
Dan Albert914449f2016-06-17 16:45:24 -070061}
62
Spandan Das0773a602022-08-16 00:55:11 +000063func RegisterNdkModuleTypes(ctx android.RegistrationContext) {
64 ctx.RegisterModuleType("ndk_headers", ndkHeadersFactory)
65 ctx.RegisterModuleType("ndk_library", NdkLibraryFactory)
66 ctx.RegisterModuleType("versioned_ndk_headers", versionedNdkHeadersFactory)
67 ctx.RegisterModuleType("preprocessed_ndk_headers", preprocessedNdkHeadersFactory)
LaMont Jones0c10e4d2023-05-16 00:58:37 +000068 ctx.RegisterParallelSingletonType("ndk", NdkSingleton)
Spandan Das0773a602022-08-16 00:55:11 +000069}
70
Colin Cross70dda7e2019-10-01 22:05:35 -070071func getNdkInstallBase(ctx android.PathContext) android.InstallPath {
72 return android.PathForNdkInstall(ctx)
Dan Albert914449f2016-06-17 16:45:24 -070073}
74
75// Returns the main install directory for the NDK sysroot. Usable with --sysroot.
Colin Cross70dda7e2019-10-01 22:05:35 -070076func getNdkSysrootBase(ctx android.PathContext) android.InstallPath {
Dan Albert914449f2016-06-17 16:45:24 -070077 return getNdkInstallBase(ctx).Join(ctx, "sysroot")
78}
79
Dan Albert6ab43d82017-12-13 15:05:04 -080080// The base timestamp file depends on the NDK headers and stub shared libraries,
81// but not the static libraries. This distinction is needed because the static
82// libraries themselves might need to depend on the base sysroot.
83func getNdkBaseTimestampFile(ctx android.PathContext) android.WritablePath {
84 return android.PathForOutput(ctx, "ndk_base.timestamp")
85}
86
Chih-Hung Hsieh5b721532021-11-30 17:31:23 -080087// The headers timestamp file depends only on the NDK headers.
88// This is used mainly for .tidy files that do not need any stub libraries.
89func getNdkHeadersTimestampFile(ctx android.PathContext) android.WritablePath {
90 return android.PathForOutput(ctx, "ndk_headers.timestamp")
91}
92
Dan Albert6ab43d82017-12-13 15:05:04 -080093// The full timestamp file depends on the base timestamp *and* the static
94// libraries.
95func getNdkFullTimestampFile(ctx android.PathContext) android.WritablePath {
Dan Albert914449f2016-06-17 16:45:24 -070096 return android.PathForOutput(ctx, "ndk.timestamp")
97}
98
Colin Cross0875c522017-11-28 17:34:01 -080099func NdkSingleton() android.Singleton {
Dan Albert914449f2016-06-17 16:45:24 -0700100 return &ndkSingleton{}
101}
102
103type ndkSingleton struct{}
104
Colin Cross0875c522017-11-28 17:34:01 -0800105func (n *ndkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Dan Albert6ab43d82017-12-13 15:05:04 -0800106 var staticLibInstallPaths android.Paths
Chih-Hung Hsieh5b721532021-11-30 17:31:23 -0800107 var headerPaths android.Paths
Colin Cross0875c522017-11-28 17:34:01 -0800108 var installPaths android.Paths
109 var licensePaths android.Paths
110 ctx.VisitAllModules(func(module android.Module) {
Dan Willemsen95f4dbb2017-05-05 23:26:01 -0700111 if m, ok := module.(android.Module); ok && !m.Enabled() {
112 return
113 }
114
Dan Albert914449f2016-06-17 16:45:24 -0700115 if m, ok := module.(*headerModule); ok {
Chih-Hung Hsieh5b721532021-11-30 17:31:23 -0800116 headerPaths = append(headerPaths, m.installPaths...)
Dan Albert914449f2016-06-17 16:45:24 -0700117 installPaths = append(installPaths, m.installPaths...)
Colin Cross0875c522017-11-28 17:34:01 -0800118 licensePaths = append(licensePaths, m.licensePath)
Dan Albert914449f2016-06-17 16:45:24 -0700119 }
Dan Albert914449f2016-06-17 16:45:24 -0700120
Dan Albert97f9c962018-05-24 15:02:16 -0700121 if m, ok := module.(*versionedHeaderModule); ok {
Chih-Hung Hsieh5b721532021-11-30 17:31:23 -0800122 headerPaths = append(headerPaths, m.installPaths...)
Dan Albert269fab82017-02-15 17:31:33 -0800123 installPaths = append(installPaths, m.installPaths...)
Colin Cross0875c522017-11-28 17:34:01 -0800124 licensePaths = append(licensePaths, m.licensePath)
Dan Albert269fab82017-02-15 17:31:33 -0800125 }
126
Dan Albertcb1b4b22018-05-24 15:06:11 -0700127 if m, ok := module.(*preprocessedHeadersModule); ok {
Chih-Hung Hsieh5b721532021-11-30 17:31:23 -0800128 headerPaths = append(headerPaths, m.installPaths...)
Dan Albertcb1b4b22018-05-24 15:06:11 -0700129 installPaths = append(installPaths, m.installPaths...)
130 licensePaths = append(licensePaths, m.licensePath)
131 }
132
Dan Albert914449f2016-06-17 16:45:24 -0700133 if m, ok := module.(*Module); ok {
Colin Cross31076b32020-10-23 17:22:06 -0700134 if installer, ok := m.installer.(*stubDecorator); ok && m.library.buildStubs() {
Dan Albert914449f2016-06-17 16:45:24 -0700135 installPaths = append(installPaths, installer.installPath)
136 }
Dan Albertf563d252017-10-13 00:29:00 -0700137
138 if library, ok := m.linker.(*libraryDecorator); ok {
Colin Cross0875c522017-11-28 17:34:01 -0800139 if library.ndkSysrootPath != nil {
Dan Albert6ab43d82017-12-13 15:05:04 -0800140 staticLibInstallPaths = append(
141 staticLibInstallPaths, library.ndkSysrootPath)
Dan Albertf563d252017-10-13 00:29:00 -0700142 }
143 }
Dan Albert5b0d4f32023-04-04 23:22:11 +0000144
145 if object, ok := m.linker.(*objectLinker); ok {
146 if object.ndkSysrootPath != nil {
147 staticLibInstallPaths = append(
148 staticLibInstallPaths, object.ndkSysrootPath)
149 }
150 }
Dan Albert914449f2016-06-17 16:45:24 -0700151 }
152 })
153
Ryan Prichardb1c9d402018-08-20 22:06:01 -0700154 // Include only a single copy of each license file. The Bionic NOTICE is
155 // long and is referenced by multiple Bionic modules.
156 licensePaths = android.FirstUniquePaths(licensePaths)
157
Dan Albertc6345fb2016-10-20 01:36:11 -0700158 combinedLicense := getNdkInstallBase(ctx).Join(ctx, "NOTICE")
Colin Cross0875c522017-11-28 17:34:01 -0800159 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700160 Rule: android.Cat,
161 Description: "combine licenses",
Colin Cross0875c522017-11-28 17:34:01 -0800162 Output: combinedLicense,
Colin Cross67a5c132017-05-09 13:45:28 -0700163 Inputs: licensePaths,
Dan Albertc6345fb2016-10-20 01:36:11 -0700164 })
165
Dan Albert49227032021-06-15 13:25:25 -0700166 baseDepPaths := append(installPaths, combinedLicense)
Dan Albertc6345fb2016-10-20 01:36:11 -0700167
Colin Cross0875c522017-11-28 17:34:01 -0800168 ctx.Build(pctx, android.BuildParams{
Dan Albert49227032021-06-15 13:25:25 -0700169 Rule: android.Touch,
170 Output: getNdkBaseTimestampFile(ctx),
171 Implicits: baseDepPaths,
172 Validation: getNdkAbiDiffTimestampFile(ctx),
Dan Albert6ab43d82017-12-13 15:05:04 -0800173 })
174
Chih-Hung Hsieh5b721532021-11-30 17:31:23 -0800175 ctx.Build(pctx, android.BuildParams{
176 Rule: android.Touch,
177 Output: getNdkHeadersTimestampFile(ctx),
178 Implicits: headerPaths,
179 })
180
Dan Albert6ab43d82017-12-13 15:05:04 -0800181 fullDepPaths := append(staticLibInstallPaths, getNdkBaseTimestampFile(ctx))
182
Dan Albertf1d14c72020-07-30 14:32:55 -0700183 // There's a phony "ndk" rule defined in core/main.mk that depends on this.
184 // `m ndk` will build the sysroots for the architectures in the current
185 // lunch target. `build/soong/scripts/build-ndk-prebuilts.sh` will build the
186 // sysroots for all the NDK architectures and package them so they can be
187 // imported into the NDK's build.
Dan Albert6ab43d82017-12-13 15:05:04 -0800188 ctx.Build(pctx, android.BuildParams{
189 Rule: android.Touch,
190 Output: getNdkFullTimestampFile(ctx),
191 Implicits: fullDepPaths,
Dan Albert914449f2016-06-17 16:45:24 -0700192 })
193}