blob: 74819540bebf39910080fe5f76928d6366831c73 [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
17import (
18 "path/filepath"
19
Dan Albert914449f2016-06-17 16:45:24 -070020 "android/soong/android"
Dan Albert266f9912024-08-13 22:01:23 +000021
Colin Cross8ff10582023-12-07 13:10:56 -080022 "github.com/google/blueprint"
Dan Albert914449f2016-06-17 16:45:24 -070023)
24
Dan Albert269fab82017-02-15 17:31:33 -080025var (
Dan Albertcb1b4b22018-05-24 15:06:11 -070026 preprocessNdkHeader = pctx.AndroidStaticRule("preprocessNdkHeader",
27 blueprint.RuleParams{
28 Command: "$preprocessor -o $out $in",
29 CommandDeps: []string{"$preprocessor"},
30 },
31 "preprocessor")
Dan Albert269fab82017-02-15 17:31:33 -080032)
33
Dan Albert914449f2016-06-17 16:45:24 -070034// Returns the NDK base include path for use with sdk_version current. Usable with -I.
Dan Albert266f9912024-08-13 22:01:23 +000035func getCurrentIncludePath(ctx android.PathContext) android.OutputPath {
Dan Albert914449f2016-06-17 16:45:24 -070036 return getNdkSysrootBase(ctx).Join(ctx, "usr/include")
37}
38
Dan Albert71222052018-05-24 15:00:05 -070039type headerProperties struct {
Dan Albert914449f2016-06-17 16:45:24 -070040 // Base directory of the headers being installed. As an example:
41 //
42 // ndk_headers {
43 // name: "foo",
44 // from: "include",
45 // to: "",
46 // srcs: ["include/foo/bar/baz.h"],
47 // }
48 //
49 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
50 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
Nan Zhang0007d812017-11-07 10:57:05 -080051 From *string
Dan Albert914449f2016-06-17 16:45:24 -070052
53 // Install path within the sysroot. This is relative to usr/include.
Nan Zhang0007d812017-11-07 10:57:05 -080054 To *string
Dan Albert914449f2016-06-17 16:45:24 -070055
56 // List of headers to install. Glob compatible. Common case is "include/**/*.h".
Colin Cross27b922f2019-03-04 22:35:41 -080057 Srcs []string `android:"path"`
Dan Albertc6345fb2016-10-20 01:36:11 -070058
Dan Albert19ff8b42018-05-24 15:00:48 -070059 // Source paths that should be excluded from the srcs glob.
Colin Cross27b922f2019-03-04 22:35:41 -080060 Exclude_srcs []string `android:"path"`
Dan Albert19ff8b42018-05-24 15:00:48 -070061
Dan Albertc6345fb2016-10-20 01:36:11 -070062 // Path to the NOTICE file associated with the headers.
Colin Cross27b922f2019-03-04 22:35:41 -080063 License *string `android:"path"`
Dan Albert266f9912024-08-13 22:01:23 +000064
65 // Set to true if the headers installed by this module should skip
66 // verification. This step ensures that each header is self-contained (can
67 // be #included alone) and is valid C. This should not be disabled except in
68 // rare cases. Outside bionic and external, if you're using this option
69 // you've probably made a mistake.
70 Skip_verification *bool
Dan Albert914449f2016-06-17 16:45:24 -070071}
72
73type headerModule struct {
74 android.ModuleBase
75
Dan Albert71222052018-05-24 15:00:05 -070076 properties headerProperties
Dan Albert914449f2016-06-17 16:45:24 -070077
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +000078 srcPaths android.Paths
Colin Cross0875c522017-11-28 17:34:01 -080079 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -080080 licensePath android.Path
Dan Albert914449f2016-06-17 16:45:24 -070081}
82
Dan Albert269fab82017-02-15 17:31:33 -080083func getHeaderInstallDir(ctx android.ModuleContext, header android.Path, from string,
Spandan Dasf280b232024-04-04 21:25:51 +000084 to string) android.OutputPath {
Dan Albert269fab82017-02-15 17:31:33 -080085 // Output path is the sysroot base + "usr/include" + to directory + directory component
86 // of the file without the leading from directory stripped.
87 //
88 // Given:
89 // sysroot base = "ndk/sysroot"
90 // from = "include/foo"
91 // to = "bar"
92 // header = "include/foo/woodly/doodly.h"
93 // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
94
95 // full/platform/path/to/include/foo
96 fullFromPath := android.PathForModuleSrc(ctx, from)
97
98 // full/platform/path/to/include/foo/woodly
99 headerDir := filepath.Dir(header.String())
100
101 // woodly
102 strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir)
103 if err != nil {
104 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir,
105 fullFromPath.String(), err)
106 }
107
108 // full/platform/path/to/sysroot/usr/include/bar/woodly
109 installDir := getCurrentIncludePath(ctx).Join(ctx, to, strippedHeaderDir)
110
111 // full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h
112 return installDir
113}
114
Dan Albert914449f2016-06-17 16:45:24 -0700115func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhang0007d812017-11-07 10:57:05 -0800116 if String(m.properties.License) == "" {
Dan Albertc6345fb2016-10-20 01:36:11 -0700117 ctx.PropertyErrorf("license", "field is required")
118 }
119
Nan Zhang0007d812017-11-07 10:57:05 -0800120 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albertc6345fb2016-10-20 01:36:11 -0700121
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000122 m.srcPaths = android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
123 for _, header := range m.srcPaths {
Nan Zhang0007d812017-11-07 10:57:05 -0800124 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From),
125 String(m.properties.To))
Dan Albert269fab82017-02-15 17:31:33 -0800126 installPath := installDir.Join(ctx, header.Base())
Spandan Dasf280b232024-04-04 21:25:51 +0000127 ctx.Build(pctx, android.BuildParams{
128 Rule: android.Cp,
129 Input: header,
130 Output: installPath,
131 })
Colin Cross0875c522017-11-28 17:34:01 -0800132 m.installPaths = append(m.installPaths, installPath)
Dan Albert914449f2016-06-17 16:45:24 -0700133 }
134
135 if len(m.installPaths) == 0 {
136 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
137 }
138}
139
Patrice Arruda6ea42112019-04-03 08:43:30 -0700140// ndk_headers installs the sets of ndk headers defined in the srcs property
141// to the sysroot base + "usr/include" + to directory + directory component.
142// ndk_headers requires the license file to be specified. Example:
143//
Colin Crossd079e0b2022-08-16 10:27:33 -0700144// Given:
145// sysroot base = "ndk/sysroot"
146// from = "include/foo"
147// to = "bar"
148// header = "include/foo/woodly/doodly.h"
149// output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
Spandan Das319711b2023-09-19 19:04:41 +0000150func NdkHeadersFactory() android.Module {
Dan Albert914449f2016-06-17 16:45:24 -0700151 module := &headerModule{}
Colin Cross36242852017-06-23 15:06:31 -0700152 module.AddProperties(&module.properties)
153 android.InitAndroidModule(module)
154 return module
Dan Albert914449f2016-06-17 16:45:24 -0700155}
Dan Albert269fab82017-02-15 17:31:33 -0800156
Spandan Das0773a602022-08-16 00:55:11 +0000157// preprocessed_ndk_header {
158//
159// name: "foo",
160// preprocessor: "foo.sh",
161// srcs: [...],
162// to: "android",
163//
164// }
Dan Albertcb1b4b22018-05-24 15:06:11 -0700165//
166// Will invoke the preprocessor as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700167//
168// $preprocessor -o $SYSROOT/usr/include/android/needs_preproc.h $src
169//
Dan Albertcb1b4b22018-05-24 15:06:11 -0700170// For each src in srcs.
171type preprocessedHeadersProperties struct {
172 // The preprocessor to run. Must be a program inside the source directory
173 // with no dependencies.
174 Preprocessor *string
175
176 // Source path to the files to be preprocessed.
177 Srcs []string
178
179 // Source paths that should be excluded from the srcs glob.
180 Exclude_srcs []string
181
182 // Install path within the sysroot. This is relative to usr/include.
183 To *string
184
185 // Path to the NOTICE file associated with the headers.
186 License *string
Dan Albert266f9912024-08-13 22:01:23 +0000187
188 // Set to true if the headers installed by this module should skip
189 // verification. This step ensures that each header is self-contained (can
190 // be #included alone) and is valid C. This should not be disabled except in
191 // rare cases. Outside bionic and external, if you're using this option
192 // you've probably made a mistake.
193 Skip_verification *bool
Dan Albertcb1b4b22018-05-24 15:06:11 -0700194}
195
196type preprocessedHeadersModule struct {
197 android.ModuleBase
198
199 properties preprocessedHeadersProperties
200
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000201 srcPaths android.Paths
Dan Albertcb1b4b22018-05-24 15:06:11 -0700202 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -0800203 licensePath android.Path
Dan Albertcb1b4b22018-05-24 15:06:11 -0700204}
205
Dan Albertcb1b4b22018-05-24 15:06:11 -0700206func (m *preprocessedHeadersModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
207 if String(m.properties.License) == "" {
208 ctx.PropertyErrorf("license", "field is required")
209 }
210
211 preprocessor := android.PathForModuleSrc(ctx, String(m.properties.Preprocessor))
212 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
213
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000214 m.srcPaths = android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
Dan Albertcb1b4b22018-05-24 15:06:11 -0700215 installDir := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000216 for _, src := range m.srcPaths {
Dan Albertcb1b4b22018-05-24 15:06:11 -0700217 installPath := installDir.Join(ctx, src.Base())
218 m.installPaths = append(m.installPaths, installPath)
219
220 ctx.Build(pctx, android.BuildParams{
221 Rule: preprocessNdkHeader,
222 Description: "preprocess " + src.Rel(),
223 Input: src,
224 Output: installPath,
225 Args: map[string]string{
226 "preprocessor": preprocessor.String(),
227 },
228 })
229 }
230
231 if len(m.installPaths) == 0 {
232 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
233 }
234}
235
Patrice Arruda6ea42112019-04-03 08:43:30 -0700236// preprocessed_ndk_headers preprocesses all the ndk headers listed in the srcs
237// property by executing the command defined in the preprocessor property.
Dan Albertcb1b4b22018-05-24 15:06:11 -0700238func preprocessedNdkHeadersFactory() android.Module {
239 module := &preprocessedHeadersModule{}
240
241 module.AddProperties(&module.properties)
242
Dan Willemsen4f644da2018-10-10 17:18:08 -0700243 android.InitAndroidModule(module)
Dan Albertcb1b4b22018-05-24 15:06:11 -0700244
245 return module
246}