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