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 llvmStrip := config.ClangPath(ctx, "bin/llvm-strip") 169 llvmLib := config.ClangPath(ctx, "lib64/libc++.so.1") 170 for _, strip := range s.properties.Strip_files { 171 cmd := builder.Command().Tool(llvmStrip).ImplicitTool(llvmLib) 172 if !ctx.Windows() { 173 cmd.Flag("-x") 174 } 175 cmd.Flag(dir.Join(ctx, strip).String()) 176 } 177 } 178 179 // Fix up the line endings of all text files. This also removes executable permissions. 180 builder.Command(). 181 Text("find"). 182 Flag(dir.String()). 183 Flag("-name '*.aidl' -o -name '*.css' -o -name '*.html' -o -name '*.java'"). 184 Flag("-o -name '*.js' -o -name '*.prop' -o -name '*.template'"). 185 Flag("-o -name '*.txt' -o -name '*.windows' -o -name '*.xml' -print0"). 186 // Using -n 500 for xargs to limit the max number of arguments per call to line_endings 187 // to 500. This avoids line_endings failing with "arguments too long". 188 Text("| xargs -0 -n 500 "). 189 BuiltTool("line_endings"). 190 Flag("unix") 191 192 // Exclude some file types (roughly matching sdk.exclude.atree) 193 builder.Command(). 194 Text("find"). 195 Flag(dir.String()). 196 Flag("'('"). 197 Flag("-name '.*' -o -name '*~' -o -name 'Makefile' -o -name 'Android.mk' -o"). 198 Flag("-name '.*.swp' -o -name '.DS_Store' -o -name '*.pyc' -o -name 'OWNERS' -o"). 199 Flag("-name 'MODULE_LICENSE_*' -o -name '*.ezt' -o -name 'Android.bp'"). 200 Flag("')' -print0"). 201 Text("| xargs -0 -r rm -rf") 202 builder.Command(). 203 Text("find"). 204 Flag(dir.String()). 205 Flag("-name '_*' ! -name '__*' -print0"). 206 Text("| xargs -0 -r rm -rf") 207 208 if ctx.Windows() { 209 // Fix EOL chars to make window users happy 210 builder.Command(). 211 Text("find"). 212 Flag(dir.String()). 213 Flag("-maxdepth 2 -name '*.bat' -type f -print0"). 214 Text("| xargs -0 -r unix2dos") 215 } 216 217 // Zip up our temporary directory as the sdk-repo 218 builder.Command(). 219 BuiltTool("soong_zip"). 220 FlagWithOutput("-o ", outputZipFile). 221 FlagWithArg("-P ", proptools.StringDefault(s.properties.Base_dir, ".")). 222 FlagWithArg("-C ", dir.String()). 223 FlagWithArg("-D ", dir.String()) 224 builder.Command().Text("rm").Flag("-rf").Text(dir.String()) 225 226 builder.Build("build_sdk_repo", "Creating sdk-repo-"+s.BaseModuleName()) 227 228 osName := ctx.Os().String() 229 if osName == "linux_glibc" { 230 osName = "linux" 231 } 232 name := fmt.Sprintf("sdk-repo-%s-%s", osName, s.BaseModuleName()) 233 234 s.outputBaseName = name 235 s.outputFile = android.OptionalPathForPath(outputZipFile) 236 ctx.InstallFile(android.PathForModuleInstall(ctx, "sdk-repo"), name+".zip", outputZipFile) 237} 238 239func (s *sdkRepoHost) AndroidMk() android.AndroidMkData { 240 return android.AndroidMkData{ 241 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { 242 fmt.Fprintln(w, ".PHONY:", name, "sdk_repo", "sdk-repo-"+name) 243 fmt.Fprintln(w, "sdk_repo", "sdk-repo-"+name+":", strings.Join(s.FilesToInstall().Strings(), " ")) 244 245 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) 246 }, 247 } 248} 249 250func remapPackageSpecs(specs map[string]android.PackagingSpec, remaps []remapProperties) error { 251 for _, remap := range remaps { 252 for path, spec := range specs { 253 if match, err := pathtools.Match(remap.From, path); err != nil { 254 return fmt.Errorf("Error parsing %q: %v", remap.From, err) 255 } else if match { 256 newPath := remap.To 257 if pathtools.IsGlob(remap.From) { 258 rel, err := filepath.Rel(constantPartOfPattern(remap.From), path) 259 if err != nil { 260 return fmt.Errorf("Error handling %q", path) 261 } 262 newPath = filepath.Join(remap.To, rel) 263 } 264 delete(specs, path) 265 spec.SetRelPathInPackage(newPath) 266 specs[newPath] = spec 267 } 268 } 269 } 270 return nil 271} 272 273func constantPartOfPattern(pattern string) string { 274 ret := "" 275 for pattern != "" { 276 var first string 277 first, pattern = splitFirst(pattern) 278 if pathtools.IsGlob(first) { 279 return ret 280 } 281 ret = filepath.Join(ret, first) 282 } 283 return ret 284} 285 286func splitFirst(path string) (string, string) { 287 i := strings.IndexRune(path, filepath.Separator) 288 if i < 0 { 289 return path, "" 290 } 291 return path[:i], path[i+1:] 292} 293