blob: 70b15c1fbce7da6742fc26385d9b565637027a1e [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
36// libraries with dummy implementations of everything for use at build time
37// 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() {
Colin Cross798bfce2016-10-12 14:28:16 -070060 android.RegisterModuleType("ndk_headers", ndkHeadersFactory)
Jooyung Hanb90e4912019-12-09 18:21:48 +090061 android.RegisterModuleType("ndk_library", NdkLibraryFactory)
Dan Albert97f9c962018-05-24 15:02:16 -070062 android.RegisterModuleType("versioned_ndk_headers", versionedNdkHeadersFactory)
Dan Albertcb1b4b22018-05-24 15:06:11 -070063 android.RegisterModuleType("preprocessed_ndk_headers", preprocessedNdkHeadersFactory)
Colin Cross798bfce2016-10-12 14:28:16 -070064 android.RegisterSingletonType("ndk", NdkSingleton)
Dan Albert914449f2016-06-17 16:45:24 -070065
Colin Crosscc0ce802019-04-02 16:14:11 -070066 pctx.Import("android/soong/android")
Dan Albert914449f2016-06-17 16:45:24 -070067}
68
Colin Cross70dda7e2019-10-01 22:05:35 -070069func getNdkInstallBase(ctx android.PathContext) android.InstallPath {
70 return android.PathForNdkInstall(ctx)
Dan Albert914449f2016-06-17 16:45:24 -070071}
72
73// Returns the main install directory for the NDK sysroot. Usable with --sysroot.
Colin Cross70dda7e2019-10-01 22:05:35 -070074func getNdkSysrootBase(ctx android.PathContext) android.InstallPath {
Dan Albert914449f2016-06-17 16:45:24 -070075 return getNdkInstallBase(ctx).Join(ctx, "sysroot")
76}
77
Dan Albert6ab43d82017-12-13 15:05:04 -080078// The base timestamp file depends on the NDK headers and stub shared libraries,
79// but not the static libraries. This distinction is needed because the static
80// libraries themselves might need to depend on the base sysroot.
81func getNdkBaseTimestampFile(ctx android.PathContext) android.WritablePath {
82 return android.PathForOutput(ctx, "ndk_base.timestamp")
83}
84
85// The full timestamp file depends on the base timestamp *and* the static
86// libraries.
87func getNdkFullTimestampFile(ctx android.PathContext) android.WritablePath {
Dan Albert914449f2016-06-17 16:45:24 -070088 return android.PathForOutput(ctx, "ndk.timestamp")
89}
90
Colin Cross0875c522017-11-28 17:34:01 -080091func NdkSingleton() android.Singleton {
Dan Albert914449f2016-06-17 16:45:24 -070092 return &ndkSingleton{}
93}
94
95type ndkSingleton struct{}
96
Colin Cross0875c522017-11-28 17:34:01 -080097func (n *ndkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Dan Albert6ab43d82017-12-13 15:05:04 -080098 var staticLibInstallPaths android.Paths
Colin Cross0875c522017-11-28 17:34:01 -080099 var installPaths android.Paths
100 var licensePaths android.Paths
101 ctx.VisitAllModules(func(module android.Module) {
Dan Willemsen95f4dbb2017-05-05 23:26:01 -0700102 if m, ok := module.(android.Module); ok && !m.Enabled() {
103 return
104 }
105
Dan Albert914449f2016-06-17 16:45:24 -0700106 if m, ok := module.(*headerModule); ok {
Dan Albert23d37e02018-11-28 08:30:10 -0800107 if ctx.Config().ExcludeDraftNdkApis() && m.properties.Draft {
108 return
109 }
110
Dan Albert914449f2016-06-17 16:45:24 -0700111 installPaths = append(installPaths, m.installPaths...)
Colin Cross0875c522017-11-28 17:34:01 -0800112 licensePaths = append(licensePaths, m.licensePath)
Dan Albert914449f2016-06-17 16:45:24 -0700113 }
Dan Albert914449f2016-06-17 16:45:24 -0700114
Dan Albert97f9c962018-05-24 15:02:16 -0700115 if m, ok := module.(*versionedHeaderModule); ok {
Dan Albert23d37e02018-11-28 08:30:10 -0800116 if ctx.Config().ExcludeDraftNdkApis() && m.properties.Draft {
117 return
118 }
119
Dan Albert269fab82017-02-15 17:31:33 -0800120 installPaths = append(installPaths, m.installPaths...)
Colin Cross0875c522017-11-28 17:34:01 -0800121 licensePaths = append(licensePaths, m.licensePath)
Dan Albert269fab82017-02-15 17:31:33 -0800122 }
123
Dan Albertcb1b4b22018-05-24 15:06:11 -0700124 if m, ok := module.(*preprocessedHeadersModule); ok {
Dan Albert23d37e02018-11-28 08:30:10 -0800125 if ctx.Config().ExcludeDraftNdkApis() && m.properties.Draft {
126 return
127 }
128
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 Albert23d37e02018-11-28 08:30:10 -0800135 if ctx.Config().ExcludeDraftNdkApis() &&
136 installer.properties.Draft {
137 return
138 }
Dan Albert914449f2016-06-17 16:45:24 -0700139 installPaths = append(installPaths, installer.installPath)
140 }
Dan Albertf563d252017-10-13 00:29:00 -0700141
142 if library, ok := m.linker.(*libraryDecorator); ok {
Colin Cross0875c522017-11-28 17:34:01 -0800143 if library.ndkSysrootPath != nil {
Dan Albert6ab43d82017-12-13 15:05:04 -0800144 staticLibInstallPaths = append(
145 staticLibInstallPaths, library.ndkSysrootPath)
Dan Albertf563d252017-10-13 00:29:00 -0700146 }
147 }
Dan Albert914449f2016-06-17 16:45:24 -0700148 }
149 })
150
Ryan Prichardb1c9d402018-08-20 22:06:01 -0700151 // Include only a single copy of each license file. The Bionic NOTICE is
152 // long and is referenced by multiple Bionic modules.
153 licensePaths = android.FirstUniquePaths(licensePaths)
154
Dan Albertc6345fb2016-10-20 01:36:11 -0700155 combinedLicense := getNdkInstallBase(ctx).Join(ctx, "NOTICE")
Colin Cross0875c522017-11-28 17:34:01 -0800156 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700157 Rule: android.Cat,
158 Description: "combine licenses",
Colin Cross0875c522017-11-28 17:34:01 -0800159 Output: combinedLicense,
Colin Cross67a5c132017-05-09 13:45:28 -0700160 Inputs: licensePaths,
Dan Albertc6345fb2016-10-20 01:36:11 -0700161 })
162
Dan Albert6ab43d82017-12-13 15:05:04 -0800163 baseDepPaths := append(installPaths, combinedLicense)
Dan Albertc6345fb2016-10-20 01:36:11 -0700164
Dan Albert914449f2016-06-17 16:45:24 -0700165 // There's a dummy "ndk" rule defined in ndk/Android.mk that depends on
166 // this. `m ndk` will build the sysroots.
Colin Cross0875c522017-11-28 17:34:01 -0800167 ctx.Build(pctx, android.BuildParams{
Dan Albert914449f2016-06-17 16:45:24 -0700168 Rule: android.Touch,
Dan Albert6ab43d82017-12-13 15:05:04 -0800169 Output: getNdkBaseTimestampFile(ctx),
170 Implicits: baseDepPaths,
171 })
172
173 fullDepPaths := append(staticLibInstallPaths, getNdkBaseTimestampFile(ctx))
174
175 ctx.Build(pctx, android.BuildParams{
176 Rule: android.Touch,
177 Output: getNdkFullTimestampFile(ctx),
178 Implicits: fullDepPaths,
Dan Albert914449f2016-06-17 16:45:24 -0700179 })
180}