• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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 placeholder 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 (
56	"android/soong/android"
57)
58
59func init() {
60	RegisterNdkModuleTypes(android.InitRegistrationContext)
61	pctx.Import("android/soong/android")
62}
63
64func RegisterNdkModuleTypes(ctx android.RegistrationContext) {
65	ctx.RegisterModuleType("ndk_headers", ndkHeadersFactory)
66	ctx.RegisterModuleType("ndk_library", NdkLibraryFactory)
67	ctx.RegisterModuleType("versioned_ndk_headers", versionedNdkHeadersFactory)
68	ctx.RegisterModuleType("preprocessed_ndk_headers", preprocessedNdkHeadersFactory)
69	ctx.RegisterSingletonType("ndk", NdkSingleton)
70}
71
72func getNdkInstallBase(ctx android.PathContext) android.InstallPath {
73	return android.PathForNdkInstall(ctx)
74}
75
76// Returns the main install directory for the NDK sysroot. Usable with --sysroot.
77func getNdkSysrootBase(ctx android.PathContext) android.InstallPath {
78	return getNdkInstallBase(ctx).Join(ctx, "sysroot")
79}
80
81// The base timestamp file depends on the NDK headers and stub shared libraries,
82// but not the static libraries. This distinction is needed because the static
83// libraries themselves might need to depend on the base sysroot.
84func getNdkBaseTimestampFile(ctx android.PathContext) android.WritablePath {
85	return android.PathForOutput(ctx, "ndk_base.timestamp")
86}
87
88// The headers timestamp file depends only on the NDK headers.
89// This is used mainly for .tidy files that do not need any stub libraries.
90func getNdkHeadersTimestampFile(ctx android.PathContext) android.WritablePath {
91	return android.PathForOutput(ctx, "ndk_headers.timestamp")
92}
93
94// The full timestamp file depends on the base timestamp *and* the static
95// libraries.
96func getNdkFullTimestampFile(ctx android.PathContext) android.WritablePath {
97	return android.PathForOutput(ctx, "ndk.timestamp")
98}
99
100func NdkSingleton() android.Singleton {
101	return &ndkSingleton{}
102}
103
104type ndkSingleton struct{}
105
106func (n *ndkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
107	var staticLibInstallPaths android.Paths
108	var headerPaths android.Paths
109	var installPaths android.Paths
110	var licensePaths android.Paths
111	ctx.VisitAllModules(func(module android.Module) {
112		if m, ok := module.(android.Module); ok && !m.Enabled() {
113			return
114		}
115
116		if m, ok := module.(*headerModule); ok {
117			headerPaths = append(headerPaths, m.installPaths...)
118			installPaths = append(installPaths, m.installPaths...)
119			licensePaths = append(licensePaths, m.licensePath)
120		}
121
122		if m, ok := module.(*versionedHeaderModule); ok {
123			headerPaths = append(headerPaths, m.installPaths...)
124			installPaths = append(installPaths, m.installPaths...)
125			licensePaths = append(licensePaths, m.licensePath)
126		}
127
128		if m, ok := module.(*preprocessedHeadersModule); ok {
129			headerPaths = append(headerPaths, m.installPaths...)
130			installPaths = append(installPaths, m.installPaths...)
131			licensePaths = append(licensePaths, m.licensePath)
132		}
133
134		if m, ok := module.(*Module); ok {
135			if installer, ok := m.installer.(*stubDecorator); ok && m.library.buildStubs() {
136				installPaths = append(installPaths, installer.installPath)
137			}
138
139			if library, ok := m.linker.(*libraryDecorator); ok {
140				if library.ndkSysrootPath != nil {
141					staticLibInstallPaths = append(
142						staticLibInstallPaths, library.ndkSysrootPath)
143				}
144			}
145
146			if object, ok := m.linker.(*objectLinker); ok {
147				if object.ndkSysrootPath != nil {
148					staticLibInstallPaths = append(
149						staticLibInstallPaths, object.ndkSysrootPath)
150				}
151			}
152		}
153	})
154
155	// Include only a single copy of each license file. The Bionic NOTICE is
156	// long and is referenced by multiple Bionic modules.
157	licensePaths = android.FirstUniquePaths(licensePaths)
158
159	combinedLicense := getNdkInstallBase(ctx).Join(ctx, "NOTICE")
160	ctx.Build(pctx, android.BuildParams{
161		Rule:        android.Cat,
162		Description: "combine licenses",
163		Output:      combinedLicense,
164		Inputs:      licensePaths,
165	})
166
167	baseDepPaths := append(installPaths, combinedLicense)
168
169	ctx.Build(pctx, android.BuildParams{
170		Rule:       android.Touch,
171		Output:     getNdkBaseTimestampFile(ctx),
172		Implicits:  baseDepPaths,
173		Validation: getNdkAbiDiffTimestampFile(ctx),
174	})
175
176	ctx.Build(pctx, android.BuildParams{
177		Rule:      android.Touch,
178		Output:    getNdkHeadersTimestampFile(ctx),
179		Implicits: headerPaths,
180	})
181
182	fullDepPaths := append(staticLibInstallPaths, getNdkBaseTimestampFile(ctx))
183
184	// There's a phony "ndk" rule defined in core/main.mk that depends on this.
185	// `m ndk` will build the sysroots for the architectures in the current
186	// lunch target. `build/soong/scripts/build-ndk-prebuilts.sh` will build the
187	// sysroots for all the NDK architectures and package them so they can be
188	// imported into the NDK's build.
189	ctx.Build(pctx, android.BuildParams{
190		Rule:      android.Touch,
191		Output:    getNdkFullTimestampFile(ctx),
192		Implicits: fullDepPaths,
193	})
194}
195