blob: d64eb7a92aa485d26a34cdf56128fa537aeb42ec [file] [log] [blame]
Dan Willemsen9fe14102021-07-13 21:52:04 -07001// Copyright (C) 2021 The Android Open Source Project
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 android_sdk
16
17import (
18 "fmt"
19 "io"
20 "path/filepath"
21 "strings"
22
23 "github.com/google/blueprint"
24 "github.com/google/blueprint/pathtools"
25 "github.com/google/blueprint/proptools"
26
27 "android/soong/android"
28 "android/soong/cc/config"
29)
30
31var pctx = android.NewPackageContext("android/soong/android_sdk")
32
33func init() {
34 registerBuildComponents(android.InitRegistrationContext)
35}
36
37func registerBuildComponents(ctx android.RegistrationContext) {
38 ctx.RegisterModuleType("android_sdk_repo_host", SdkRepoHostFactory)
39}
40
41type sdkRepoHost struct {
42 android.ModuleBase
43 android.PackagingBase
44
45 properties sdkRepoHostProperties
46
47 outputBaseName string
48 outputFile android.OptionalPath
49}
50
51type remapProperties struct {
52 From string
53 To string
54}
55
56type sdkRepoHostProperties struct {
57 // The top level directory to use for the SDK repo.
58 Base_dir *string
59
60 // List of src:dst mappings to rename files from `deps`.
61 Deps_remap []remapProperties `android:"arch_variant"`
62
63 // List of zip files to merge into the SDK repo.
64 Merge_zips []string `android:"arch_variant,path"`
65
66 // List of sources to include into the SDK repo. These are usually raw files, filegroups,
67 // or genrules, as most built modules should be referenced via `deps`.
68 Srcs []string `android:"arch_variant,path"`
69
70 // List of files to strip. This should be a list of files, not modules. This happens after
71 // `deps_remap` and `merge_zips` are applied, but before the `base_dir` is added.
72 Strip_files []string `android:"arch_variant"`
73}
74
75// android_sdk_repo_host defines an Android SDK repo containing host tools.
76//
77// This implementation is trying to be a faithful reproduction of how these sdk-repos were produced
78// in the Make system, which may explain some of the oddities (like `strip_files` not being
79// automatic)
80func SdkRepoHostFactory() android.Module {
81 return newSdkRepoHostModule()
82}
83
84func newSdkRepoHostModule() *sdkRepoHost {
85 s := &sdkRepoHost{}
86 s.AddProperties(&s.properties)
87 android.InitPackageModule(s)
88 android.InitAndroidMultiTargetsArchModule(s, android.HostSupported, android.MultilibCommon)
89 return s
90}
91
92type dependencyTag struct {
93 blueprint.BaseDependencyTag
94 android.PackagingItemAlwaysDepTag
95}
96
97// TODO(b/201696252): Evaluate whether licenses should be propagated through this dependency.
98func (d dependencyTag) PropagateLicenses() bool {
99 return false
100}
101
102var depTag = dependencyTag{}
103
104func (s *sdkRepoHost) DepsMutator(ctx android.BottomUpMutatorContext) {
105 s.AddDeps(ctx, depTag)
106}
107
108func (s *sdkRepoHost) GenerateAndroidBuildActions(ctx android.ModuleContext) {
109 dir := android.PathForModuleOut(ctx, "zip")
110 builder := android.NewRuleBuilder(pctx, ctx).
111 Sbox(dir, android.PathForModuleOut(ctx, "out.sbox.textproto")).
112 SandboxInputs()
113
114 // Get files from modules listed in `deps`
115 packageSpecs := s.GatherPackagingSpecs(ctx)
116
117 // Handle `deps_remap` renames
118 err := remapPackageSpecs(packageSpecs, s.properties.Deps_remap)
119 if err != nil {
120 ctx.PropertyErrorf("deps_remap", "%s", err.Error())
121 }
122
123 s.CopySpecsToDir(ctx, builder, packageSpecs, dir)
124
125 // Collect licenses to write into NOTICE.txt
126 noticeMap := map[string]android.Paths{}
127 for path, pkgSpec := range packageSpecs {
128 licenseFiles := pkgSpec.EffectiveLicenseFiles()
129 if len(licenseFiles) > 0 {
130 noticeMap[path] = pkgSpec.EffectiveLicenseFiles()
131 }
132 }
133 notices := android.BuildNotices(ctx, noticeMap)
134 builder.Command().Text("cp").
135 Input(notices.TxtOutput.Path()).
136 Text(filepath.Join(dir.String(), "NOTICE.txt"))
137
138 // Handle `merge_zips` by extracting their contents into our tmpdir
139 for _, zip := range android.PathsForModuleSrc(ctx, s.properties.Merge_zips) {
140 builder.Command().
141 Text("unzip").
142 Flag("-DD").
143 Flag("-q").
144 FlagWithArg("-d ", dir.String()).
145 Input(zip)
146 }
147
148 // Copy files from `srcs` into our tmpdir
149 for _, src := range android.PathsForModuleSrc(ctx, s.properties.Srcs) {
150 builder.Command().
151 Text("cp").Input(src).Flag(dir.Join(ctx, src.Rel()).String())
152 }
153
154 // Handle `strip_files` by calling the necessary strip commands
155 //
156 // Note: this stripping logic was copied over from the old Make implementation
157 // It's not using the same flags as the regular stripping support, nor does it
158 // support the array of per-module stripping options. It would be nice if we
159 // pulled the stripped versions from the CC modules, but that doesn't exist
160 // for host tools today. (And not all the things we strip are CC modules today)
161 if ctx.Darwin() {
162 macStrip := config.MacStripPath(ctx)
163 for _, strip := range s.properties.Strip_files {
164 builder.Command().
165 Text(macStrip).Flag("-x").
166 Flag(dir.Join(ctx, strip).String())
167 }
168 } else {
169 llvmStrip := config.ClangPath(ctx, "bin/llvm-strip")
170 llvmLib := config.ClangPath(ctx, "lib64/libc++.so.1")
171 for _, strip := range s.properties.Strip_files {
172 cmd := builder.Command().Tool(llvmStrip).ImplicitTool(llvmLib)
173 if !ctx.Windows() {
174 cmd.Flag("-x")
175 }
176 cmd.Flag(dir.Join(ctx, strip).String())
177 }
178 }
179
180 // Fix up the line endings of all text files. This also removes executable permissions.
181 builder.Command().
182 Text("find").
183 Flag(dir.String()).
184 Flag("-name '*.aidl' -o -name '*.css' -o -name '*.html' -o -name '*.java'").
185 Flag("-o -name '*.js' -o -name '*.prop' -o -name '*.template'").
186 Flag("-o -name '*.txt' -o -name '*.windows' -o -name '*.xml' -print0").
187 // Using -n 500 for xargs to limit the max number of arguments per call to line_endings
188 // to 500. This avoids line_endings failing with "arguments too long".
189 Text("| xargs -0 -n 500 ").
190 BuiltTool("line_endings").
191 Flag("unix")
192
193 // Exclude some file types (roughly matching sdk.exclude.atree)
194 builder.Command().
195 Text("find").
196 Flag(dir.String()).
197 Flag("'('").
198 Flag("-name '.*' -o -name '*~' -o -name 'Makefile' -o -name 'Android.mk' -o").
199 Flag("-name '.*.swp' -o -name '.DS_Store' -o -name '*.pyc' -o -name 'OWNERS' -o").
200 Flag("-name 'MODULE_LICENSE_*' -o -name '*.ezt' -o -name 'Android.bp'").
201 Flag("')' -print0").
202 Text("| xargs -0 -r rm -rf")
203 builder.Command().
204 Text("find").
205 Flag(dir.String()).
206 Flag("-name '_*' ! -name '__*' -print0").
207 Text("| xargs -0 -r rm -rf")
208
209 if ctx.Windows() {
210 // Fix EOL chars to make window users happy
211 builder.Command().
212 Text("find").
213 Flag(dir.String()).
214 Flag("-maxdepth 2 -name '*.bat' -type f -print0").
215 Text("| xargs -0 -r unix2dos")
216 }
217
218 // Zip up our temporary directory as the sdk-repo
219 outputZipFile := dir.Join(ctx, "output.zip")
220 builder.Command().
221 BuiltTool("soong_zip").
222 FlagWithOutput("-o ", outputZipFile).
223 FlagWithArg("-P ", proptools.StringDefault(s.properties.Base_dir, ".")).
224 FlagWithArg("-C ", dir.String()).
225 FlagWithArg("-D ", dir.String())
226 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
227
228 builder.Build("build_sdk_repo", "Creating sdk-repo-"+s.BaseModuleName())
229
230 osName := ctx.Os().String()
231 if osName == "linux_glibc" {
232 osName = "linux"
233 }
234 name := fmt.Sprintf("sdk-repo-%s-%s", osName, s.BaseModuleName())
235
236 s.outputBaseName = name
237 s.outputFile = android.OptionalPathForPath(outputZipFile)
238 ctx.InstallFile(android.PathForModuleInstall(ctx, "sdk-repo"), name+".zip", outputZipFile)
239}
240
241func (s *sdkRepoHost) AndroidMk() android.AndroidMkData {
242 return android.AndroidMkData{
243 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Dan Willemsen9fe14102021-07-13 21:52:04 -0700244 fmt.Fprintln(w, ".PHONY:", name, "sdk_repo", "sdk-repo-"+name)
245 fmt.Fprintln(w, "sdk_repo", "sdk-repo-"+name+":", strings.Join(s.FilesToInstall().Strings(), " "))
246
Dan Willemsenb07ae342021-10-15 13:39:23 -0700247 fmt.Fprintf(w, "$(call dist-for-goals,sdk_repo sdk-repo-%s,%s:%s-$(FILE_NAME_TAG).zip)\n\n", s.BaseModuleName(), s.outputFile.String(), s.outputBaseName)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700248 },
249 }
250}
251
252func remapPackageSpecs(specs map[string]android.PackagingSpec, remaps []remapProperties) error {
253 for _, remap := range remaps {
254 for path, spec := range specs {
255 if match, err := pathtools.Match(remap.From, path); err != nil {
256 return fmt.Errorf("Error parsing %q: %v", remap.From, err)
257 } else if match {
258 newPath := remap.To
259 if pathtools.IsGlob(remap.From) {
260 rel, err := filepath.Rel(constantPartOfPattern(remap.From), path)
261 if err != nil {
262 return fmt.Errorf("Error handling %q", path)
263 }
264 newPath = filepath.Join(remap.To, rel)
265 }
266 delete(specs, path)
267 spec.SetRelPathInPackage(newPath)
268 specs[newPath] = spec
269 }
270 }
271 }
272 return nil
273}
274
275func constantPartOfPattern(pattern string) string {
276 ret := ""
277 for pattern != "" {
278 var first string
279 first, pattern = splitFirst(pattern)
280 if pathtools.IsGlob(first) {
281 return ret
282 }
283 ret = filepath.Join(ret, first)
284 }
285 return ret
286}
287
288func splitFirst(path string) (string, string) {
289 i := strings.IndexRune(path, filepath.Separator)
290 if i < 0 {
291 return path, ""
292 }
293 return path[:i], path[i+1:]
294}