Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 1 | // Copyright 2016 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 ( |
Spandan Das | e12d252 | 2023-09-12 21:42:31 +0000 | [diff] [blame] | 18 | "path/filepath" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 19 | "strings" |
| 20 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 21 | "android/soong/android" |
Spandan Das | e12d252 | 2023-09-12 21:42:31 +0000 | [diff] [blame] | 22 | "android/soong/bazel" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | func init() { |
Jooyung Han | b90e491 | 2019-12-09 18:21:48 +0900 | [diff] [blame] | 26 | android.RegisterModuleType("ndk_prebuilt_static_stl", NdkPrebuiltStaticStlFactory) |
Jaewoong Jung | 710756a | 2019-06-04 11:53:47 -0700 | [diff] [blame] | 27 | android.RegisterModuleType("ndk_prebuilt_shared_stl", NdkPrebuiltSharedStlFactory) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | // NDK prebuilt libraries. |
| 31 | // |
| 32 | // These differ from regular prebuilts in that they aren't stripped and usually aren't installed |
| 33 | // either (with the exception of the shared STLs, which are installed to the app's directory rather |
| 34 | // than to the system image). |
| 35 | |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 36 | type ndkPrebuiltStlLinker struct { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 37 | *libraryDecorator |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 40 | func (ndk *ndkPrebuiltStlLinker) linkerProps() []interface{} { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 41 | return append(ndk.libraryDecorator.linkerProps(), &ndk.Properties, &ndk.flagExporter.Properties) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 44 | func (*ndkPrebuiltStlLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 45 | // NDK libraries can't have any dependencies |
| 46 | return deps |
| 47 | } |
| 48 | |
Jiyong Park | e01e228 | 2020-03-09 16:24:11 +0900 | [diff] [blame] | 49 | func (*ndkPrebuiltStlLinker) availableFor(what string) bool { |
| 50 | // ndk prebuilt objects are available to everywhere |
| 51 | return true |
| 52 | } |
| 53 | |
Patrice Arruda | 8c85637 | 2019-04-01 17:29:59 -0700 | [diff] [blame] | 54 | // ndk_prebuilt_shared_stl exports a precompiled ndk shared standard template |
| 55 | // library (stl) library for linking operation. The soong's module name format |
| 56 | // is ndk_<NAME>.so where the library is located under |
| 57 | // ./prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/$(HOST_ARCH)/<NAME>.so. |
Jaewoong Jung | 710756a | 2019-06-04 11:53:47 -0700 | [diff] [blame] | 58 | func NdkPrebuiltSharedStlFactory() android.Module { |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 59 | module, library := NewLibrary(android.DeviceSupported) |
| 60 | library.BuildOnlyShared() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 61 | module.compiler = nil |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 62 | module.linker = &ndkPrebuiltStlLinker{ |
| 63 | libraryDecorator: library, |
| 64 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 65 | module.installer = nil |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 66 | module.Properties.Sdk_version = StringPtr("minimum") |
| 67 | module.Properties.AlwaysSdk = true |
| 68 | module.stl.Properties.Stl = StringPtr("none") |
Spandan Das | e12d252 | 2023-09-12 21:42:31 +0000 | [diff] [blame] | 69 | module.bazelable = true |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 70 | return module.Init() |
| 71 | } |
| 72 | |
Patrice Arruda | 8c85637 | 2019-04-01 17:29:59 -0700 | [diff] [blame] | 73 | // ndk_prebuilt_static_stl exports a precompiled ndk static standard template |
| 74 | // library (stl) library for linking operation. The soong's module name format |
| 75 | // is ndk_<NAME>.a where the library is located under |
| 76 | // ./prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/$(HOST_ARCH)/<NAME>.a. |
Jooyung Han | b90e491 | 2019-12-09 18:21:48 +0900 | [diff] [blame] | 77 | func NdkPrebuiltStaticStlFactory() android.Module { |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 78 | module, library := NewLibrary(android.DeviceSupported) |
| 79 | library.BuildOnlyStatic() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 80 | module.compiler = nil |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 81 | module.linker = &ndkPrebuiltStlLinker{ |
| 82 | libraryDecorator: library, |
| 83 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 84 | module.installer = nil |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 85 | module.Properties.Sdk_version = StringPtr("minimum") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 86 | module.Properties.HideFromMake = true |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 87 | module.Properties.AlwaysSdk = true |
| 88 | module.Properties.Sdk_version = StringPtr("current") |
| 89 | module.stl.Properties.Stl = StringPtr("none") |
Spandan Das | e12d252 | 2023-09-12 21:42:31 +0000 | [diff] [blame] | 90 | module.bazelable = true |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 91 | return module.Init() |
| 92 | } |
| 93 | |
Spandan Das | 6e332d2 | 2023-09-13 17:58:52 +0000 | [diff] [blame] | 94 | const ( |
| 95 | libDir = "current/sources/cxx-stl/llvm-libc++/libs" |
| 96 | ) |
| 97 | |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 98 | func getNdkStlLibDir(ctx android.ModuleContext) android.SourcePath { |
Spandan Das | 6e332d2 | 2023-09-13 17:58:52 +0000 | [diff] [blame] | 99 | return android.PathForSource(ctx, ctx.ModuleDir(), libDir).Join(ctx, ctx.Arch().Abi[0]) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 103 | deps PathDeps, objs Objects) android.Path { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 104 | // A null build step, but it sets up the output path. |
| 105 | if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") { |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 106 | ctx.ModuleErrorf("NDK prebuilt libraries must have an ndk_lib prefixed name") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 109 | ndk.libraryDecorator.flagExporter.exportIncludesAsSystem(ctx) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 110 | |
| 111 | libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_") |
| 112 | libExt := flags.Toolchain.ShlibSuffix() |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 113 | if ndk.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 114 | libExt = staticLibraryExtension |
| 115 | } |
| 116 | |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 117 | libDir := getNdkStlLibDir(ctx) |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 118 | lib := libDir.Join(ctx, libName+libExt) |
| 119 | |
| 120 | ndk.libraryDecorator.flagExporter.setProvider(ctx) |
| 121 | |
| 122 | if ndk.static() { |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 123 | depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(lib).Build() |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 124 | ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{ |
| 125 | StaticLibrary: lib, |
| 126 | |
| 127 | TransitiveStaticLibrariesForOrdering: depSet, |
| 128 | }) |
| 129 | } else { |
| 130 | ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{ |
Liz Kammer | ef6dfea | 2021-06-08 15:37:09 -0400 | [diff] [blame] | 131 | SharedLibrary: lib, |
| 132 | Target: ctx.Target(), |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 133 | }) |
| 134 | } |
| 135 | |
| 136 | return lib |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 137 | } |
Spandan Das | e12d252 | 2023-09-12 21:42:31 +0000 | [diff] [blame] | 138 | |
| 139 | var ( |
| 140 | archToAbiDirMap = map[string]string{ |
| 141 | "android_arm": "armeabi-v7a", |
| 142 | "android_arm64": "arm64-v8a", |
| 143 | "android_riscv64": "riscv64", |
| 144 | "android_x86": "x86", |
| 145 | "android_x86_64": "x86_64", |
| 146 | } |
| 147 | ) |
| 148 | |
| 149 | // stlSrcBp2build returns a bazel label for the checked-in .so/.a file |
| 150 | // It contains a select statement for each ABI |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 151 | func stlSrcBp2build(ctx android.Bp2buildMutatorContext, c *Module) bazel.LabelAttribute { |
Spandan Das | e12d252 | 2023-09-12 21:42:31 +0000 | [diff] [blame] | 152 | libName := strings.TrimPrefix(c.Name(), "ndk_") |
| 153 | libExt := ".so" // TODO - b/201079053: Support windows |
| 154 | if ctx.ModuleType() == "ndk_prebuilt_static_stl" { |
| 155 | libExt = ".a" |
| 156 | } |
| 157 | src := bazel.LabelAttribute{} |
| 158 | for arch, abiDir := range archToAbiDirMap { |
| 159 | srcPath := filepath.Join(libDir, abiDir, libName+libExt) |
| 160 | src.SetSelectValue( |
| 161 | bazel.OsArchConfigurationAxis, |
| 162 | arch, |
| 163 | android.BazelLabelForModuleSrcSingle(ctx, srcPath), |
| 164 | ) |
| 165 | } |
| 166 | return src |
| 167 | } |
| 168 | |
| 169 | // stlIncludesBp2build returns the includes exported by the STL |
| 170 | func stlIncludesBp2build(c *Module) bazel.StringListAttribute { |
| 171 | linker, _ := c.linker.(*ndkPrebuiltStlLinker) |
| 172 | includeDirs := append( |
| 173 | []string{}, |
| 174 | linker.libraryDecorator.flagExporter.Properties.Export_include_dirs..., |
| 175 | ) |
| 176 | includeDirs = append( |
| 177 | includeDirs, |
| 178 | linker.libraryDecorator.flagExporter.Properties.Export_system_include_dirs..., |
| 179 | ) |
| 180 | return bazel.MakeStringListAttribute(android.FirstUniqueStrings(includeDirs)) |
| 181 | } |
| 182 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 183 | func ndkPrebuiltStlBp2build(ctx android.Bp2buildMutatorContext, c *Module) { |
Spandan Das | e12d252 | 2023-09-12 21:42:31 +0000 | [diff] [blame] | 184 | if ctx.ModuleType() == "ndk_prebuilt_static_stl" { |
| 185 | ndkPrebuiltStaticStlBp2build(ctx, c) |
| 186 | } else { |
| 187 | ndkPrebuiltSharedStlBp2build(ctx, c) |
| 188 | } |
| 189 | } |
| 190 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 191 | func ndkPrebuiltStaticStlBp2build(ctx android.Bp2buildMutatorContext, c *Module) { |
Spandan Das | e12d252 | 2023-09-12 21:42:31 +0000 | [diff] [blame] | 192 | props := bazel.BazelTargetModuleProperties{ |
| 193 | Rule_class: "cc_prebuilt_library_static", |
| 194 | Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_library_static.bzl", |
| 195 | } |
| 196 | attrs := &bazelPrebuiltLibraryStaticAttributes{ |
| 197 | Static_library: stlSrcBp2build(ctx, c), |
| 198 | Export_system_includes: stlIncludesBp2build(c), // The exports are always as system |
| 199 | } |
| 200 | // TODO: min_sdk_version |
| 201 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: c.Name()}, attrs) |
| 202 | } |
| 203 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 204 | func ndkPrebuiltSharedStlBp2build(ctx android.Bp2buildMutatorContext, c *Module) { |
Spandan Das | e12d252 | 2023-09-12 21:42:31 +0000 | [diff] [blame] | 205 | props := bazel.BazelTargetModuleProperties{ |
| 206 | Rule_class: "cc_prebuilt_library_shared", |
| 207 | Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_library_shared.bzl", |
| 208 | } |
| 209 | attrs := &bazelPrebuiltLibrarySharedAttributes{ |
| 210 | Shared_library: stlSrcBp2build(ctx, c), |
| 211 | Export_system_includes: stlIncludesBp2build(c), // The exports are always as system |
| 212 | } |
| 213 | // TODO: min_sdk_version |
| 214 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: c.Name()}, attrs) |
| 215 | } |