1// 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 outputZipFile := dir.Join(ctx, "output.zip") 111 builder := android.NewRuleBuilder(pctx, ctx). 112 Sbox(dir, android.PathForModuleOut(ctx, "out.sbox.textproto")). 113 SandboxInputs() 114 115 // Get files from modules listed in `deps` 116 packageSpecs := s.GatherPackagingSpecs(ctx) 117 118 // Handle `deps_remap` renames 119 err := remapPackageSpecs(packageSpecs, s.properties.Deps_remap) 120 if err != nil { 121 ctx.PropertyErrorf("deps_remap", "%s", err.Error()) 122 } 123 124 s.CopySpecsToDir(ctx, builder, packageSpecs, dir) 125 126 noticeFile := android.PathForModuleOut(ctx, "NOTICES.txt") 127 android.BuildNoticeTextOutputFromLicenseMetadata( 128 ctx, noticeFile, "", "", 129 []string{ 130 android.PathForModuleInstall(ctx, "sdk-repo").String() + "/", 131 outputZipFile.String(), 132 }) 133 builder.Command().Text("cp"). 134 Input(noticeFile). 135 Text(filepath.Join(dir.String(), "NOTICE.txt")) 136 137 // Handle `merge_zips` by extracting their contents into our tmpdir 138 for _, zip := range android.PathsForModuleSrc(ctx, s.properties.Merge_zips) { 139 builder.Command(). 140 Text("unzip"). 141 Flag("-DD"). 142 Flag("-q"). 143 FlagWithArg("-d ", dir.String()). 144 Input(zip) 145 } 146 147 // Copy files from `srcs` into our tmpdir 148 for _, src := range android.PathsForModuleSrc(ctx, s.properties.Srcs) { 149 builder.Command(). 150 Text("cp").Input(src).Flag(dir.Join(ctx, src.Rel()).String()) 151 } 152 153 // Handle `strip_files` by calling the necessary strip commands 154 // 155 // Note: this stripping logic was copied over from the old Make implementation 156 // It's not using the same flags as the regular stripping support, nor does it 157 // support the array of per-module stripping options. It would be nice if we 158 // pulled the stripped versions from the CC modules, but that doesn't exist 159 // for host tools today. (And not all the things we strip are CC modules today) 160 if ctx.Darwin() { 161 macStrip := config.MacStripPath(ctx) 162 for _, strip := range s.properties.Strip_files { 163 builder.Command(). 164 Text(macStrip).Flag("-x"). 165 Flag(dir.Join(ctx, strip).String()) 166 } 167 } else { 168 llvmObjCopy := config.ClangPath(ctx, "bin/llvm-objcopy") 169 llvmStrip := config.ClangPath(ctx, "bin/llvm-strip") 170 llvmLib := config.ClangPath(ctx, "lib/x86_64-unknown-linux-gnu/libc++.so") 171 for _, strip := range s.properties.Strip_files { 172 cmd := builder.Command().Tool(llvmStrip).ImplicitTool(llvmLib).ImplicitTool(llvmObjCopy) 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 builder.Command(). 220 BuiltTool("soong_zip"). 221 FlagWithOutput("-o ", outputZipFile). 222 FlagWithArg("-P ", proptools.StringDefault(s.properties.Base_dir, ".")). 223 FlagWithArg("-C ", dir.String()). 224 FlagWithArg("-D ", dir.String()) 225 builder.Command().Text("rm").Flag("-rf").Text(dir.String()) 226 227 builder.Build("build_sdk_repo", "Creating sdk-repo-"+s.BaseModuleName()) 228 229 osName := ctx.Os().String() 230 if osName == "linux_glibc" { 231 osName = "linux" 232 } 233 name := fmt.Sprintf("sdk-repo-%s-%s", osName, s.BaseModuleName()) 234 235 s.outputBaseName = name 236 s.outputFile = android.OptionalPathForPath(outputZipFile) 237 ctx.InstallFile(android.PathForModuleInstall(ctx, "sdk-repo"), name+".zip", outputZipFile) 238} 239 240func (s *sdkRepoHost) AndroidMk() android.AndroidMkData { 241 return android.AndroidMkData{ 242 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { 243 fmt.Fprintln(w, ".PHONY:", name, "sdk_repo", "sdk-repo-"+name) 244 fmt.Fprintln(w, "sdk_repo", "sdk-repo-"+name+":", strings.Join(s.FilesToInstall().Strings(), " ")) 245 246 fmt.Fprintf(w, "$(call dist-for-goals,sdk_repo sdk-repo-%s,%s:%s-FILE_NAME_TAG_PLACEHOLDER.zip)\n\n", s.BaseModuleName(), s.outputFile.String(), s.outputBaseName) 247 }, 248 } 249} 250 251func remapPackageSpecs(specs map[string]android.PackagingSpec, remaps []remapProperties) error { 252 for _, remap := range remaps { 253 for path, spec := range specs { 254 if match, err := pathtools.Match(remap.From, path); err != nil { 255 return fmt.Errorf("Error parsing %q: %v", remap.From, err) 256 } else if match { 257 newPath := remap.To 258 if pathtools.IsGlob(remap.From) { 259 rel, err := filepath.Rel(constantPartOfPattern(remap.From), path) 260 if err != nil { 261 return fmt.Errorf("Error handling %q", path) 262 } 263 newPath = filepath.Join(remap.To, rel) 264 } 265 delete(specs, path) 266 spec.SetRelPathInPackage(newPath) 267 specs[newPath] = spec 268 } 269 } 270 } 271 return nil 272} 273 274func constantPartOfPattern(pattern string) string { 275 ret := "" 276 for pattern != "" { 277 var first string 278 first, pattern = splitFirst(pattern) 279 if pathtools.IsGlob(first) { 280 return ret 281 } 282 ret = filepath.Join(ret, first) 283 } 284 return ret 285} 286 287func splitFirst(path string) (string, string) { 288 i := strings.IndexRune(path, filepath.Separator) 289 if i < 0 { 290 return path, "" 291 } 292 return path[:i], path[i+1:] 293} 294