Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "path/filepath" |
| 19 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 20 | "android/soong/android" |
Dan Albert | 266f991 | 2024-08-13 22:01:23 +0000 | [diff] [blame] | 21 | |
Colin Cross | 8ff1058 | 2023-12-07 13:10:56 -0800 | [diff] [blame] | 22 | "github.com/google/blueprint" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 25 | var ( |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 26 | preprocessNdkHeader = pctx.AndroidStaticRule("preprocessNdkHeader", |
| 27 | blueprint.RuleParams{ |
| 28 | Command: "$preprocessor -o $out $in", |
| 29 | CommandDeps: []string{"$preprocessor"}, |
| 30 | }, |
| 31 | "preprocessor") |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 32 | ) |
| 33 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 34 | // Returns the NDK base include path for use with sdk_version current. Usable with -I. |
Dan Albert | 266f991 | 2024-08-13 22:01:23 +0000 | [diff] [blame] | 35 | func getCurrentIncludePath(ctx android.PathContext) android.OutputPath { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 36 | return getNdkSysrootBase(ctx).Join(ctx, "usr/include") |
| 37 | } |
| 38 | |
Dan Albert | 7122205 | 2018-05-24 15:00:05 -0700 | [diff] [blame] | 39 | type headerProperties struct { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 40 | // 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 Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 51 | From *string |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 52 | |
| 53 | // Install path within the sysroot. This is relative to usr/include. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 54 | To *string |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 55 | |
| 56 | // List of headers to install. Glob compatible. Common case is "include/**/*.h". |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 57 | Srcs []string `android:"path"` |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 58 | |
Dan Albert | 19ff8b4 | 2018-05-24 15:00:48 -0700 | [diff] [blame] | 59 | // Source paths that should be excluded from the srcs glob. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 60 | Exclude_srcs []string `android:"path"` |
Dan Albert | 19ff8b4 | 2018-05-24 15:00:48 -0700 | [diff] [blame] | 61 | |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 62 | // Path to the NOTICE file associated with the headers. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 63 | License *string `android:"path"` |
Dan Albert | 266f991 | 2024-08-13 22:01:23 +0000 | [diff] [blame] | 64 | |
| 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 Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | type headerModule struct { |
| 74 | android.ModuleBase |
| 75 | |
Dan Albert | 7122205 | 2018-05-24 15:00:05 -0700 | [diff] [blame] | 76 | properties headerProperties |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 77 | |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 78 | srcPaths android.Paths |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 79 | installPaths android.Paths |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 80 | licensePath android.Path |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Yu Liu | 95cef3a | 2025-02-25 00:54:20 +0000 | [diff] [blame] | 83 | type NdkHeaderInfo struct { |
| 84 | SrcPaths android.Paths |
| 85 | InstallPaths android.Paths |
| 86 | LicensePath android.Path |
| 87 | // Set to true if the headers installed by this module should skip |
| 88 | // verification. This step ensures that each header is self-contained (can |
| 89 | // be #included alone) and is valid C. This should not be disabled except in |
| 90 | // rare cases. Outside bionic and external, if you're using this option |
| 91 | // you've probably made a mistake. |
| 92 | SkipVerification bool |
| 93 | } |
| 94 | |
| 95 | var NdkHeaderInfoProvider = blueprint.NewProvider[NdkHeaderInfo]() |
| 96 | |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 97 | func getHeaderInstallDir(ctx android.ModuleContext, header android.Path, from string, |
Spandan Das | f280b23 | 2024-04-04 21:25:51 +0000 | [diff] [blame] | 98 | to string) android.OutputPath { |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 99 | // Output path is the sysroot base + "usr/include" + to directory + directory component |
| 100 | // of the file without the leading from directory stripped. |
| 101 | // |
| 102 | // Given: |
| 103 | // sysroot base = "ndk/sysroot" |
| 104 | // from = "include/foo" |
| 105 | // to = "bar" |
| 106 | // header = "include/foo/woodly/doodly.h" |
| 107 | // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h" |
| 108 | |
| 109 | // full/platform/path/to/include/foo |
| 110 | fullFromPath := android.PathForModuleSrc(ctx, from) |
| 111 | |
| 112 | // full/platform/path/to/include/foo/woodly |
| 113 | headerDir := filepath.Dir(header.String()) |
| 114 | |
| 115 | // woodly |
| 116 | strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir) |
| 117 | if err != nil { |
| 118 | ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir, |
| 119 | fullFromPath.String(), err) |
| 120 | } |
| 121 | |
| 122 | // full/platform/path/to/sysroot/usr/include/bar/woodly |
| 123 | installDir := getCurrentIncludePath(ctx).Join(ctx, to, strippedHeaderDir) |
| 124 | |
| 125 | // full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h |
| 126 | return installDir |
| 127 | } |
| 128 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 129 | func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 130 | if String(m.properties.License) == "" { |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 131 | ctx.PropertyErrorf("license", "field is required") |
| 132 | } |
| 133 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 134 | m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License)) |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 135 | |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 136 | m.srcPaths = android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs) |
| 137 | for _, header := range m.srcPaths { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 138 | installDir := getHeaderInstallDir(ctx, header, String(m.properties.From), |
| 139 | String(m.properties.To)) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 140 | installPath := installDir.Join(ctx, header.Base()) |
Spandan Das | f280b23 | 2024-04-04 21:25:51 +0000 | [diff] [blame] | 141 | ctx.Build(pctx, android.BuildParams{ |
| 142 | Rule: android.Cp, |
| 143 | Input: header, |
| 144 | Output: installPath, |
| 145 | }) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 146 | m.installPaths = append(m.installPaths, installPath) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | if len(m.installPaths) == 0 { |
| 150 | ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs) |
| 151 | } |
Yu Liu | 95cef3a | 2025-02-25 00:54:20 +0000 | [diff] [blame] | 152 | |
| 153 | android.SetProvider(ctx, NdkHeaderInfoProvider, NdkHeaderInfo{ |
| 154 | SrcPaths: m.srcPaths, |
| 155 | InstallPaths: m.installPaths, |
| 156 | LicensePath: m.licensePath, |
| 157 | SkipVerification: Bool(m.properties.Skip_verification), |
| 158 | }) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Patrice Arruda | 6ea4211 | 2019-04-03 08:43:30 -0700 | [diff] [blame] | 161 | // ndk_headers installs the sets of ndk headers defined in the srcs property |
| 162 | // to the sysroot base + "usr/include" + to directory + directory component. |
| 163 | // ndk_headers requires the license file to be specified. Example: |
| 164 | // |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 165 | // Given: |
| 166 | // sysroot base = "ndk/sysroot" |
| 167 | // from = "include/foo" |
| 168 | // to = "bar" |
| 169 | // header = "include/foo/woodly/doodly.h" |
| 170 | // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h" |
Spandan Das | 319711b | 2023-09-19 19:04:41 +0000 | [diff] [blame] | 171 | func NdkHeadersFactory() android.Module { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 172 | module := &headerModule{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 173 | module.AddProperties(&module.properties) |
| 174 | android.InitAndroidModule(module) |
| 175 | return module |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 176 | } |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 177 | |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 178 | // preprocessed_ndk_header { |
| 179 | // |
| 180 | // name: "foo", |
| 181 | // preprocessor: "foo.sh", |
| 182 | // srcs: [...], |
| 183 | // to: "android", |
| 184 | // |
| 185 | // } |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 186 | // |
| 187 | // Will invoke the preprocessor as: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 188 | // |
| 189 | // $preprocessor -o $SYSROOT/usr/include/android/needs_preproc.h $src |
| 190 | // |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 191 | // For each src in srcs. |
| 192 | type preprocessedHeadersProperties struct { |
| 193 | // The preprocessor to run. Must be a program inside the source directory |
| 194 | // with no dependencies. |
| 195 | Preprocessor *string |
| 196 | |
| 197 | // Source path to the files to be preprocessed. |
| 198 | Srcs []string |
| 199 | |
| 200 | // Source paths that should be excluded from the srcs glob. |
| 201 | Exclude_srcs []string |
| 202 | |
| 203 | // Install path within the sysroot. This is relative to usr/include. |
| 204 | To *string |
| 205 | |
| 206 | // Path to the NOTICE file associated with the headers. |
| 207 | License *string |
Dan Albert | 266f991 | 2024-08-13 22:01:23 +0000 | [diff] [blame] | 208 | |
| 209 | // Set to true if the headers installed by this module should skip |
| 210 | // verification. This step ensures that each header is self-contained (can |
| 211 | // be #included alone) and is valid C. This should not be disabled except in |
| 212 | // rare cases. Outside bionic and external, if you're using this option |
| 213 | // you've probably made a mistake. |
| 214 | Skip_verification *bool |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | type preprocessedHeadersModule struct { |
| 218 | android.ModuleBase |
| 219 | |
| 220 | properties preprocessedHeadersProperties |
| 221 | |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 222 | srcPaths android.Paths |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 223 | installPaths android.Paths |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 224 | licensePath android.Path |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Yu Liu | 95cef3a | 2025-02-25 00:54:20 +0000 | [diff] [blame] | 227 | var NdkPreprocessedHeaderInfoProvider = blueprint.NewProvider[NdkHeaderInfo]() |
| 228 | |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 229 | func (m *preprocessedHeadersModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 230 | if String(m.properties.License) == "" { |
| 231 | ctx.PropertyErrorf("license", "field is required") |
| 232 | } |
| 233 | |
| 234 | preprocessor := android.PathForModuleSrc(ctx, String(m.properties.Preprocessor)) |
| 235 | m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License)) |
| 236 | |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 237 | m.srcPaths = android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs) |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 238 | installDir := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To)) |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 239 | for _, src := range m.srcPaths { |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 240 | installPath := installDir.Join(ctx, src.Base()) |
| 241 | m.installPaths = append(m.installPaths, installPath) |
| 242 | |
| 243 | ctx.Build(pctx, android.BuildParams{ |
| 244 | Rule: preprocessNdkHeader, |
| 245 | Description: "preprocess " + src.Rel(), |
| 246 | Input: src, |
| 247 | Output: installPath, |
| 248 | Args: map[string]string{ |
| 249 | "preprocessor": preprocessor.String(), |
| 250 | }, |
| 251 | }) |
| 252 | } |
| 253 | |
| 254 | if len(m.installPaths) == 0 { |
| 255 | ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs) |
| 256 | } |
Yu Liu | 95cef3a | 2025-02-25 00:54:20 +0000 | [diff] [blame] | 257 | |
| 258 | android.SetProvider(ctx, NdkPreprocessedHeaderInfoProvider, NdkHeaderInfo{ |
| 259 | SrcPaths: m.srcPaths, |
| 260 | InstallPaths: m.installPaths, |
| 261 | LicensePath: m.licensePath, |
| 262 | SkipVerification: Bool(m.properties.Skip_verification), |
| 263 | }) |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Patrice Arruda | 6ea4211 | 2019-04-03 08:43:30 -0700 | [diff] [blame] | 266 | // preprocessed_ndk_headers preprocesses all the ndk headers listed in the srcs |
| 267 | // property by executing the command defined in the preprocessor property. |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 268 | func preprocessedNdkHeadersFactory() android.Module { |
| 269 | module := &preprocessedHeadersModule{} |
| 270 | |
| 271 | module.AddProperties(&module.properties) |
| 272 | |
Dan Willemsen | 4f644da | 2018-10-10 17:18:08 -0700 | [diff] [blame] | 273 | android.InitAndroidModule(module) |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 274 | |
| 275 | return module |
| 276 | } |