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