Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 1 | // Copyright 2024 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 ( |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 18 | "bytes" |
| 19 | _ "embed" |
| 20 | "fmt" |
| 21 | "path/filepath" |
| 22 | "slices" |
| 23 | "sort" |
| 24 | "strings" |
| 25 | "text/template" |
| 26 | |
mrziwang | f95cfa6 | 2024-06-18 10:11:39 -0700 | [diff] [blame] | 27 | "android/soong/android" |
| 28 | |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 29 | "github.com/google/blueprint" |
| 30 | "github.com/google/blueprint/proptools" |
| 31 | ) |
| 32 | |
| 33 | const veryVerbose bool = false |
| 34 | |
| 35 | //go:embed cmake_main.txt |
| 36 | var templateCmakeMainRaw string |
| 37 | var templateCmakeMain *template.Template = parseTemplate(templateCmakeMainRaw) |
| 38 | |
| 39 | //go:embed cmake_module_cc.txt |
| 40 | var templateCmakeModuleCcRaw string |
| 41 | var templateCmakeModuleCc *template.Template = parseTemplate(templateCmakeModuleCcRaw) |
| 42 | |
| 43 | //go:embed cmake_module_aidl.txt |
| 44 | var templateCmakeModuleAidlRaw string |
| 45 | var templateCmakeModuleAidl *template.Template = parseTemplate(templateCmakeModuleAidlRaw) |
| 46 | |
| 47 | //go:embed cmake_ext_add_aidl_library.txt |
| 48 | var cmakeExtAddAidlLibrary string |
| 49 | |
| 50 | //go:embed cmake_ext_append_flags.txt |
| 51 | var cmakeExtAppendFlags string |
| 52 | |
| 53 | var defaultUnportableFlags []string = []string{ |
Tomasz Wasilczyk | cd67473 | 2024-06-25 10:30:30 -0700 | [diff] [blame^] | 54 | "-Wno-c99-designator", |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 55 | "-Wno-class-memaccess", |
| 56 | "-Wno-exit-time-destructors", |
Tomasz Wasilczyk | cd67473 | 2024-06-25 10:30:30 -0700 | [diff] [blame^] | 57 | "-Winconsistent-missing-override", |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 58 | "-Wno-inconsistent-missing-override", |
| 59 | "-Wreorder-init-list", |
| 60 | "-Wno-reorder-init-list", |
| 61 | "-Wno-restrict", |
| 62 | "-Wno-stringop-overread", |
| 63 | "-Wno-subobject-linkage", |
| 64 | } |
| 65 | |
| 66 | var ignoredSystemLibs []string = []string{ |
Tomasz Wasilczyk | 2493fcc | 2024-06-20 15:29:09 -0700 | [diff] [blame] | 67 | "crtbegin_dynamic", |
| 68 | "crtend_android", |
| 69 | "libc", |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 70 | "libc++", |
| 71 | "libc++_static", |
Tomasz Wasilczyk | 7cec7e6 | 2024-07-01 11:31:47 -0700 | [diff] [blame] | 72 | "libc_musl", |
| 73 | "libc_musl_crtbegin_so", |
| 74 | "libc_musl_crtbegin_static", |
| 75 | "libc_musl_crtend", |
| 76 | "libc_musl_crtend_so", |
Tomasz Wasilczyk | 2493fcc | 2024-06-20 15:29:09 -0700 | [diff] [blame] | 77 | "libdl", |
| 78 | "libm", |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 79 | "prebuilt_libclang_rt.builtins", |
| 80 | "prebuilt_libclang_rt.ubsan_minimal", |
| 81 | } |
| 82 | |
| 83 | // Mapping entry between Android's library name and the one used when building outside Android tree. |
| 84 | type LibraryMappingProperty struct { |
| 85 | // Android library name. |
| 86 | Android_name string |
| 87 | |
| 88 | // Library name used when building outside Android. |
| 89 | Mapped_name string |
| 90 | |
| 91 | // If the make file is already present in Android source tree, specify its location. |
| 92 | Package_pregenerated string |
| 93 | |
| 94 | // If the package is expected to be installed on the build host OS, specify its name. |
| 95 | Package_system string |
| 96 | } |
| 97 | |
| 98 | type CmakeSnapshotProperties struct { |
| 99 | // Modules to add to the snapshot package. Their dependencies are pulled in automatically. |
| 100 | Modules []string |
| 101 | |
| 102 | // Host prebuilts to bundle with the snapshot. These are tools needed to build outside Android. |
| 103 | Prebuilts []string |
| 104 | |
| 105 | // Global cflags to add when building outside Android. |
| 106 | Cflags []string |
| 107 | |
| 108 | // Flags to skip when building outside Android. |
| 109 | Cflags_ignored []string |
| 110 | |
| 111 | // Mapping between library names used in Android tree and externally. |
| 112 | Library_mapping []LibraryMappingProperty |
| 113 | |
| 114 | // List of cflags that are not portable between compilers that could potentially be used to |
| 115 | // build a generated package. If left empty, it's initialized with a default list. |
| 116 | Unportable_flags []string |
| 117 | |
| 118 | // Whether to include source code as part of the snapshot package. |
| 119 | Include_sources bool |
| 120 | } |
| 121 | |
| 122 | var cmakeSnapshotSourcesProvider = blueprint.NewProvider[android.Paths]() |
| 123 | |
| 124 | type CmakeSnapshot struct { |
| 125 | android.ModuleBase |
| 126 | |
| 127 | Properties CmakeSnapshotProperties |
| 128 | |
| 129 | zipPath android.WritablePath |
| 130 | } |
| 131 | |
| 132 | type cmakeProcessedProperties struct { |
| 133 | LibraryMapping map[string]LibraryMappingProperty |
| 134 | PregeneratedPackages []string |
| 135 | SystemPackages []string |
| 136 | } |
| 137 | |
| 138 | type cmakeSnapshotDependencyTag struct { |
| 139 | blueprint.BaseDependencyTag |
| 140 | name string |
| 141 | } |
| 142 | |
| 143 | var ( |
| 144 | cmakeSnapshotModuleTag = cmakeSnapshotDependencyTag{name: "cmake-snapshot-module"} |
| 145 | cmakeSnapshotPrebuiltTag = cmakeSnapshotDependencyTag{name: "cmake-snapshot-prebuilt"} |
| 146 | ) |
| 147 | |
| 148 | func parseTemplate(templateContents string) *template.Template { |
| 149 | funcMap := template.FuncMap{ |
| 150 | "setList": func(name string, nameSuffix string, itemPrefix string, items []string) string { |
| 151 | var list strings.Builder |
| 152 | list.WriteString("set(" + name + nameSuffix) |
| 153 | templateListBuilder(&list, itemPrefix, items) |
| 154 | return list.String() |
| 155 | }, |
| 156 | "toStrings": func(files android.Paths) []string { |
Colin Cross | 7de1db7 | 2024-06-25 14:36:24 -0700 | [diff] [blame] | 157 | return files.Strings() |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 158 | }, |
| 159 | "concat5": func(list1 []string, list2 []string, list3 []string, list4 []string, list5 []string) []string { |
| 160 | return append(append(append(append(list1, list2...), list3...), list4...), list5...) |
| 161 | }, |
| 162 | "cflagsList": func(name string, nameSuffix string, flags []string, |
| 163 | unportableFlags []string, ignoredFlags []string) string { |
| 164 | if len(unportableFlags) == 0 { |
| 165 | unportableFlags = defaultUnportableFlags |
| 166 | } |
| 167 | |
| 168 | var filteredPortable []string |
| 169 | var filteredUnportable []string |
| 170 | for _, flag := range flags { |
| 171 | if slices.Contains(ignoredFlags, flag) { |
| 172 | continue |
| 173 | } else if slices.Contains(unportableFlags, flag) { |
| 174 | filteredUnportable = append(filteredUnportable, flag) |
| 175 | } else { |
| 176 | filteredPortable = append(filteredPortable, flag) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | var list strings.Builder |
| 181 | |
| 182 | list.WriteString("set(" + name + nameSuffix) |
| 183 | templateListBuilder(&list, "", filteredPortable) |
| 184 | |
| 185 | if len(filteredUnportable) > 0 { |
| 186 | list.WriteString("\nappend_cxx_flags_if_supported(" + name + nameSuffix) |
| 187 | templateListBuilder(&list, "", filteredUnportable) |
| 188 | } |
| 189 | |
| 190 | return list.String() |
| 191 | }, |
| 192 | "getSources": func(m *Module) android.Paths { |
| 193 | return m.compiler.(CompiledInterface).Srcs() |
| 194 | }, |
| 195 | "getModuleType": getModuleType, |
| 196 | "getCompilerProperties": func(m *Module) BaseCompilerProperties { |
| 197 | return m.compiler.baseCompilerProps() |
| 198 | }, |
Cole Faust | e96c16a | 2024-06-13 14:51:14 -0700 | [diff] [blame] | 199 | "getCflagsProperty": func(ctx android.ModuleContext, m *Module) []string { |
| 200 | cflags := m.compiler.baseCompilerProps().Cflags |
| 201 | return cflags.GetOrDefault(ctx, nil) |
| 202 | }, |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 203 | "getLinkerProperties": func(m *Module) BaseLinkerProperties { |
| 204 | return m.linker.baseLinkerProps() |
| 205 | }, |
| 206 | "getExtraLibs": getExtraLibs, |
| 207 | "getIncludeDirs": getIncludeDirs, |
Tomasz Wasilczyk | 1e831bf | 2024-05-10 15:15:21 -0700 | [diff] [blame] | 208 | "mapLibraries": func(ctx android.ModuleContext, m *Module, libs []string, mapping map[string]LibraryMappingProperty) []string { |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 209 | var mappedLibs []string |
| 210 | for _, lib := range libs { |
| 211 | mappedLib, exists := mapping[lib] |
| 212 | if exists { |
| 213 | lib = mappedLib.Mapped_name |
| 214 | } else { |
Tomasz Wasilczyk | 1e831bf | 2024-05-10 15:15:21 -0700 | [diff] [blame] | 215 | if !ctx.OtherModuleExists(lib) { |
| 216 | ctx.OtherModuleErrorf(m, "Dependency %s doesn't exist", lib) |
| 217 | } |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 218 | lib = "android::" + lib |
| 219 | } |
| 220 | if lib == "" { |
| 221 | continue |
| 222 | } |
| 223 | mappedLibs = append(mappedLibs, lib) |
| 224 | } |
| 225 | sort.Strings(mappedLibs) |
| 226 | mappedLibs = slices.Compact(mappedLibs) |
| 227 | return mappedLibs |
| 228 | }, |
Tomasz Wasilczyk | 1e831bf | 2024-05-10 15:15:21 -0700 | [diff] [blame] | 229 | "getAidlSources": func(m *Module) []string { |
| 230 | aidlInterface := m.compiler.baseCompilerProps().AidlInterface |
| 231 | aidlRoot := aidlInterface.AidlRoot + string(filepath.Separator) |
| 232 | if aidlInterface.AidlRoot == "" { |
| 233 | aidlRoot = "" |
| 234 | } |
| 235 | var sources []string |
| 236 | for _, src := range aidlInterface.Sources { |
| 237 | if !strings.HasPrefix(src, aidlRoot) { |
| 238 | panic(fmt.Sprintf("Aidl source '%v' doesn't start with '%v'", src, aidlRoot)) |
| 239 | } |
| 240 | sources = append(sources, src[len(aidlRoot):]) |
| 241 | } |
| 242 | return sources |
| 243 | }, |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | return template.Must(template.New("").Delims("<<", ">>").Funcs(funcMap).Parse(templateContents)) |
| 247 | } |
| 248 | |
| 249 | func sliceWithPrefix(prefix string, slice []string) []string { |
| 250 | output := make([]string, len(slice)) |
| 251 | for i, elem := range slice { |
| 252 | output[i] = prefix + elem |
| 253 | } |
| 254 | return output |
| 255 | } |
| 256 | |
| 257 | func templateListBuilder(builder *strings.Builder, itemPrefix string, items []string) { |
| 258 | if len(items) > 0 { |
| 259 | builder.WriteString("\n") |
| 260 | for _, item := range items { |
| 261 | builder.WriteString(" " + itemPrefix + item + "\n") |
| 262 | } |
| 263 | } |
| 264 | builder.WriteString(")") |
| 265 | } |
| 266 | |
| 267 | func executeTemplate(templ *template.Template, buffer *bytes.Buffer, data any) string { |
| 268 | buffer.Reset() |
| 269 | if err := templ.Execute(buffer, data); err != nil { |
| 270 | panic(err) |
| 271 | } |
| 272 | output := strings.TrimSpace(buffer.String()) |
| 273 | buffer.Reset() |
| 274 | return output |
| 275 | } |
| 276 | |
| 277 | func (m *CmakeSnapshot) DepsMutator(ctx android.BottomUpMutatorContext) { |
Tomasz Wasilczyk | 9288b51 | 2024-06-27 09:22:12 -0700 | [diff] [blame] | 278 | hostVariations := ctx.Config().BuildOSTarget.Variations() |
| 279 | ctx.AddVariationDependencies(hostVariations, cmakeSnapshotModuleTag, m.Properties.Modules...) |
Tomasz Wasilczyk | 2493fcc | 2024-06-20 15:29:09 -0700 | [diff] [blame] | 280 | |
| 281 | if len(m.Properties.Prebuilts) > 0 { |
| 282 | prebuilts := append(m.Properties.Prebuilts, "libc++") |
Tomasz Wasilczyk | 9288b51 | 2024-06-27 09:22:12 -0700 | [diff] [blame] | 283 | ctx.AddVariationDependencies(hostVariations, cmakeSnapshotPrebuiltTag, prebuilts...) |
Tomasz Wasilczyk | 2493fcc | 2024-06-20 15:29:09 -0700 | [diff] [blame] | 284 | } |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | func (m *CmakeSnapshot) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 288 | var templateBuffer bytes.Buffer |
| 289 | var pprop cmakeProcessedProperties |
| 290 | m.zipPath = android.PathForModuleOut(ctx, ctx.ModuleName()+".zip") |
| 291 | |
| 292 | // Process Library_mapping for more efficient lookups |
| 293 | pprop.LibraryMapping = map[string]LibraryMappingProperty{} |
| 294 | for _, elem := range m.Properties.Library_mapping { |
| 295 | pprop.LibraryMapping[elem.Android_name] = elem |
| 296 | |
| 297 | if elem.Package_pregenerated != "" { |
| 298 | pprop.PregeneratedPackages = append(pprop.PregeneratedPackages, elem.Package_pregenerated) |
| 299 | } |
| 300 | sort.Strings(pprop.PregeneratedPackages) |
| 301 | pprop.PregeneratedPackages = slices.Compact(pprop.PregeneratedPackages) |
| 302 | |
| 303 | if elem.Package_system != "" { |
| 304 | pprop.SystemPackages = append(pprop.SystemPackages, elem.Package_system) |
| 305 | } |
| 306 | sort.Strings(pprop.SystemPackages) |
| 307 | pprop.SystemPackages = slices.Compact(pprop.SystemPackages) |
| 308 | } |
| 309 | |
| 310 | // Generating CMakeLists.txt rules for all modules in dependency tree |
| 311 | moduleDirs := map[string][]string{} |
| 312 | sourceFiles := map[string]android.Path{} |
| 313 | visitedModules := map[string]bool{} |
| 314 | var pregeneratedModules []*Module |
| 315 | ctx.WalkDeps(func(dep_a android.Module, parent android.Module) bool { |
| 316 | moduleName := ctx.OtherModuleName(dep_a) |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 317 | if visited := visitedModules[moduleName]; visited { |
| 318 | return false // visit only once |
| 319 | } |
| 320 | visitedModules[moduleName] = true |
Tomasz Wasilczyk | 1e831bf | 2024-05-10 15:15:21 -0700 | [diff] [blame] | 321 | dep, ok := dep_a.(*Module) |
| 322 | if !ok { |
| 323 | return false // not a cc module |
| 324 | } |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 325 | if mapping, ok := pprop.LibraryMapping[moduleName]; ok { |
| 326 | if mapping.Package_pregenerated != "" { |
| 327 | pregeneratedModules = append(pregeneratedModules, dep) |
| 328 | } |
| 329 | return false // mapped to system or pregenerated (we'll handle these later) |
| 330 | } |
| 331 | if ctx.OtherModuleDependencyTag(dep) == cmakeSnapshotPrebuiltTag { |
| 332 | return false // we'll handle cmakeSnapshotPrebuiltTag later |
| 333 | } |
| 334 | if slices.Contains(ignoredSystemLibs, moduleName) { |
| 335 | return false // system libs built in-tree for Android |
| 336 | } |
| 337 | if dep.compiler == nil { |
| 338 | return false // unsupported module type (e.g. prebuilt) |
| 339 | } |
| 340 | isAidlModule := dep.compiler.baseCompilerProps().AidlInterface.Lang != "" |
| 341 | |
| 342 | if !proptools.Bool(dep.Properties.Cmake_snapshot_supported) { |
| 343 | ctx.OtherModulePropertyErrorf(dep, "cmake_snapshot_supported", |
| 344 | "CMake snapshots not supported, despite being a dependency for %s", |
| 345 | ctx.OtherModuleName(parent)) |
| 346 | return false |
| 347 | } |
| 348 | |
| 349 | if veryVerbose { |
| 350 | fmt.Println("WalkDeps: " + ctx.OtherModuleName(parent) + " -> " + moduleName) |
| 351 | } |
| 352 | |
| 353 | // Generate CMakeLists.txt fragment for this module |
| 354 | templateToUse := templateCmakeModuleCc |
| 355 | if isAidlModule { |
| 356 | templateToUse = templateCmakeModuleAidl |
| 357 | } |
| 358 | moduleFragment := executeTemplate(templateToUse, &templateBuffer, struct { |
| 359 | Ctx *android.ModuleContext |
| 360 | M *Module |
| 361 | Snapshot *CmakeSnapshot |
| 362 | Pprop *cmakeProcessedProperties |
| 363 | }{ |
| 364 | &ctx, |
| 365 | dep, |
| 366 | m, |
| 367 | &pprop, |
| 368 | }) |
| 369 | moduleDir := ctx.OtherModuleDir(dep) |
| 370 | moduleDirs[moduleDir] = append(moduleDirs[moduleDir], moduleFragment) |
| 371 | |
| 372 | if m.Properties.Include_sources { |
| 373 | files, _ := android.OtherModuleProvider(ctx, dep, cmakeSnapshotSourcesProvider) |
| 374 | for _, file := range files { |
| 375 | sourceFiles[file.String()] = file |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // if it's AIDL module, no need to dive into their dependencies |
| 380 | return !isAidlModule |
| 381 | }) |
| 382 | |
| 383 | // Enumerate sources for pregenerated modules |
| 384 | if m.Properties.Include_sources { |
| 385 | for _, dep := range pregeneratedModules { |
| 386 | if !proptools.Bool(dep.Properties.Cmake_snapshot_supported) { |
| 387 | ctx.OtherModulePropertyErrorf(dep, "cmake_snapshot_supported", |
| 388 | "Pregenerated CMake snapshots not supported, despite being requested for %s", |
| 389 | ctx.ModuleName()) |
| 390 | continue |
| 391 | } |
| 392 | |
| 393 | files, _ := android.OtherModuleProvider(ctx, dep, cmakeSnapshotSourcesProvider) |
| 394 | for _, file := range files { |
| 395 | sourceFiles[file.String()] = file |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | // Merging CMakeLists.txt contents for every module directory |
| 401 | var makefilesList android.Paths |
Colin Cross | 7de1db7 | 2024-06-25 14:36:24 -0700 | [diff] [blame] | 402 | for _, moduleDir := range android.SortedKeys(moduleDirs) { |
| 403 | fragments := moduleDirs[moduleDir] |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 404 | moduleCmakePath := android.PathForModuleGen(ctx, moduleDir, "CMakeLists.txt") |
| 405 | makefilesList = append(makefilesList, moduleCmakePath) |
| 406 | sort.Strings(fragments) |
| 407 | android.WriteFileRule(ctx, moduleCmakePath, strings.Join(fragments, "\n\n\n")) |
| 408 | } |
| 409 | |
| 410 | // Generating top-level CMakeLists.txt |
| 411 | mainCmakePath := android.PathForModuleGen(ctx, "CMakeLists.txt") |
| 412 | makefilesList = append(makefilesList, mainCmakePath) |
| 413 | mainContents := executeTemplate(templateCmakeMain, &templateBuffer, struct { |
| 414 | Ctx *android.ModuleContext |
| 415 | M *CmakeSnapshot |
| 416 | ModuleDirs map[string][]string |
| 417 | Pprop *cmakeProcessedProperties |
| 418 | }{ |
| 419 | &ctx, |
| 420 | m, |
| 421 | moduleDirs, |
| 422 | &pprop, |
| 423 | }) |
| 424 | android.WriteFileRule(ctx, mainCmakePath, mainContents) |
| 425 | |
| 426 | // Generating CMake extensions |
| 427 | extPath := android.PathForModuleGen(ctx, "cmake", "AppendCxxFlagsIfSupported.cmake") |
| 428 | makefilesList = append(makefilesList, extPath) |
| 429 | android.WriteFileRuleVerbatim(ctx, extPath, cmakeExtAppendFlags) |
| 430 | extPath = android.PathForModuleGen(ctx, "cmake", "AddAidlLibrary.cmake") |
| 431 | makefilesList = append(makefilesList, extPath) |
| 432 | android.WriteFileRuleVerbatim(ctx, extPath, cmakeExtAddAidlLibrary) |
| 433 | |
| 434 | // Generating the final zip file |
| 435 | zipRule := android.NewRuleBuilder(pctx, ctx) |
| 436 | zipCmd := zipRule.Command(). |
| 437 | BuiltTool("soong_zip"). |
| 438 | FlagWithOutput("-o ", m.zipPath) |
| 439 | |
| 440 | // Packaging all sources into the zip file |
| 441 | if m.Properties.Include_sources { |
| 442 | var sourcesList android.Paths |
Colin Cross | 7de1db7 | 2024-06-25 14:36:24 -0700 | [diff] [blame] | 443 | for _, file := range android.SortedKeys(sourceFiles) { |
| 444 | path := sourceFiles[file] |
| 445 | sourcesList = append(sourcesList, path) |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | sourcesRspFile := android.PathForModuleObj(ctx, ctx.ModuleName()+"_sources.rsp") |
| 449 | zipCmd.FlagWithRspFileInputList("-r ", sourcesRspFile, sourcesList) |
| 450 | } |
| 451 | |
| 452 | // Packaging all make files into the zip file |
| 453 | makefilesRspFile := android.PathForModuleObj(ctx, ctx.ModuleName()+"_makefiles.rsp") |
| 454 | zipCmd. |
| 455 | FlagWithArg("-C ", android.PathForModuleGen(ctx).OutputPath.String()). |
| 456 | FlagWithRspFileInputList("-r ", makefilesRspFile, makefilesList) |
| 457 | |
| 458 | // Packaging all prebuilts into the zip file |
| 459 | if len(m.Properties.Prebuilts) > 0 { |
| 460 | var prebuiltsList android.Paths |
| 461 | |
| 462 | ctx.VisitDirectDepsWithTag(cmakeSnapshotPrebuiltTag, func(dep android.Module) { |
| 463 | for _, file := range dep.FilesToInstall() { |
| 464 | prebuiltsList = append(prebuiltsList, file) |
| 465 | } |
| 466 | }) |
| 467 | |
| 468 | prebuiltsRspFile := android.PathForModuleObj(ctx, ctx.ModuleName()+"_prebuilts.rsp") |
| 469 | zipCmd. |
| 470 | FlagWithArg("-C ", android.PathForArbitraryOutput(ctx).String()). |
| 471 | FlagWithArg("-P ", "prebuilts"). |
| 472 | FlagWithRspFileInputList("-r ", prebuiltsRspFile, prebuiltsList) |
| 473 | } |
| 474 | |
| 475 | // Finish generating the final zip file |
| 476 | zipRule.Build(m.zipPath.String(), "archiving "+ctx.ModuleName()) |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 477 | |
mrziwang | f95cfa6 | 2024-06-18 10:11:39 -0700 | [diff] [blame] | 478 | ctx.SetOutputFiles(android.Paths{m.zipPath}, "") |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | func (m *CmakeSnapshot) AndroidMkEntries() []android.AndroidMkEntries { |
| 482 | return []android.AndroidMkEntries{{ |
| 483 | Class: "DATA", |
| 484 | OutputFile: android.OptionalPathForPath(m.zipPath), |
| 485 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 486 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 487 | entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", true) |
| 488 | }, |
| 489 | }, |
| 490 | }} |
| 491 | } |
| 492 | |
| 493 | func getModuleType(m *Module) string { |
| 494 | switch m.linker.(type) { |
| 495 | case *binaryDecorator: |
| 496 | return "executable" |
| 497 | case *libraryDecorator: |
| 498 | return "library" |
| 499 | case *testBinary: |
Tomasz Wasilczyk | c3177e0 | 2024-06-10 14:38:45 -0700 | [diff] [blame] | 500 | return "test" |
Tomasz Wasilczyk | 6e2b8c0 | 2024-05-30 07:48:40 -0700 | [diff] [blame] | 501 | case *benchmarkDecorator: |
Tomasz Wasilczyk | c3177e0 | 2024-06-10 14:38:45 -0700 | [diff] [blame] | 502 | return "test" |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 503 | } |
Tomasz Wasilczyk | 6e2b8c0 | 2024-05-30 07:48:40 -0700 | [diff] [blame] | 504 | panic(fmt.Sprintf("Unexpected module type: %T", m.linker)) |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | func getExtraLibs(m *Module) []string { |
| 508 | switch decorator := m.linker.(type) { |
| 509 | case *testBinary: |
| 510 | if decorator.testDecorator.gtest() { |
Tomasz Wasilczyk | 6e2b8c0 | 2024-05-30 07:48:40 -0700 | [diff] [blame] | 511 | return []string{ |
| 512 | "libgtest", |
| 513 | "libgtest_main", |
| 514 | } |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 515 | } |
Tomasz Wasilczyk | 6e2b8c0 | 2024-05-30 07:48:40 -0700 | [diff] [blame] | 516 | case *benchmarkDecorator: |
| 517 | return []string{"libgoogle-benchmark"} |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 518 | } |
| 519 | return nil |
| 520 | } |
| 521 | |
| 522 | func getIncludeDirs(ctx android.ModuleContext, m *Module) []string { |
| 523 | moduleDir := ctx.OtherModuleDir(m) + string(filepath.Separator) |
| 524 | switch decorator := m.compiler.(type) { |
| 525 | case *libraryDecorator: |
Aleks Todorov | c9becde | 2024-06-10 12:51:53 +0100 | [diff] [blame] | 526 | return sliceWithPrefix(moduleDir, decorator.flagExporter.Properties.Export_include_dirs.GetOrDefault(ctx, nil)) |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 527 | } |
| 528 | return nil |
| 529 | } |
| 530 | |
Tomasz Wasilczyk | d848dcc | 2024-05-10 09:16:37 -0700 | [diff] [blame] | 531 | func cmakeSnapshotLoadHook(ctx android.LoadHookContext) { |
| 532 | props := struct { |
| 533 | Target struct { |
| 534 | Darwin struct { |
| 535 | Enabled *bool |
| 536 | } |
| 537 | Windows struct { |
| 538 | Enabled *bool |
| 539 | } |
| 540 | } |
| 541 | }{} |
| 542 | props.Target.Darwin.Enabled = proptools.BoolPtr(false) |
| 543 | props.Target.Windows.Enabled = proptools.BoolPtr(false) |
| 544 | ctx.AppendProperties(&props) |
| 545 | } |
| 546 | |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 547 | // cmake_snapshot allows defining source packages for release outside of Android build tree. |
| 548 | // As a result of cmake_snapshot module build, a zip file is generated with CMake build definitions |
| 549 | // for selected source modules, their dependencies and optionally also the source code itself. |
| 550 | func CmakeSnapshotFactory() android.Module { |
| 551 | module := &CmakeSnapshot{} |
| 552 | module.AddProperties(&module.Properties) |
Tomasz Wasilczyk | d848dcc | 2024-05-10 09:16:37 -0700 | [diff] [blame] | 553 | android.AddLoadHook(module, cmakeSnapshotLoadHook) |
| 554 | android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst) |
Hao Chen | 1c8ea5b | 2023-10-20 23:03:45 +0000 | [diff] [blame] | 555 | return module |
| 556 | } |
| 557 | |
| 558 | func init() { |
| 559 | android.InitRegistrationContext.RegisterModuleType("cc_cmake_snapshot", CmakeSnapshotFactory) |
| 560 | } |