prebuilt.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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. package cc
  15. import (
  16. "path/filepath"
  17. "strings"
  18. "android/soong/android"
  19. "android/soong/bazel"
  20. "android/soong/bazel/cquery"
  21. )
  22. func init() {
  23. RegisterPrebuiltBuildComponents(android.InitRegistrationContext)
  24. }
  25. func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) {
  26. ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
  27. ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
  28. ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
  29. ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory)
  30. ctx.RegisterModuleType("cc_prebuilt_object", PrebuiltObjectFactory)
  31. ctx.RegisterModuleType("cc_prebuilt_binary", PrebuiltBinaryFactory)
  32. }
  33. type prebuiltLinkerInterface interface {
  34. Name(string) string
  35. prebuilt() *android.Prebuilt
  36. }
  37. type prebuiltLinkerProperties struct {
  38. // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
  39. Srcs []string `android:"path,arch_variant"`
  40. Sanitized Sanitized `android:"arch_variant"`
  41. // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
  42. // symbols, etc), default true.
  43. Check_elf_files *bool
  44. // if set, add an extra objcopy --prefix-symbols= step
  45. Prefix_symbols *string
  46. // Optionally provide an import library if this is a Windows PE DLL prebuilt.
  47. // This is needed only if this library is linked by other modules in build time.
  48. // Only makes sense for the Windows target.
  49. Windows_import_lib *string `android:"path,arch_variant"`
  50. // MixedBuildsDisabled is true if and only if building this prebuilt is explicitly disabled in mixed builds for either
  51. // its static or shared version on the current build variant. This is to prevent Bazel targets for build variants with
  52. // which either the static or shared version is incompatible from participating in mixed buiods. Please note that this
  53. // is an override and does not fully determine whether Bazel or Soong will be used. For the full determination, see
  54. // cc.ProcessBazelQueryResponse, cc.QueueBazelCall, and cc.MixedBuildsDisabled.
  55. MixedBuildsDisabled bool `blueprint:"mutated"`
  56. }
  57. type prebuiltLinker struct {
  58. android.Prebuilt
  59. properties prebuiltLinkerProperties
  60. }
  61. func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
  62. return &p.Prebuilt
  63. }
  64. func (p *prebuiltLinker) PrebuiltSrcs() []string {
  65. return p.properties.Srcs
  66. }
  67. type prebuiltLibraryInterface interface {
  68. libraryInterface
  69. prebuiltLinkerInterface
  70. disablePrebuilt()
  71. }
  72. type prebuiltLibraryLinker struct {
  73. *libraryDecorator
  74. prebuiltLinker
  75. }
  76. var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
  77. var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
  78. func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
  79. func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
  80. return p.libraryDecorator.linkerDeps(ctx, deps)
  81. }
  82. func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
  83. return flags
  84. }
  85. func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
  86. return p.libraryDecorator.linkerProps()
  87. }
  88. func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
  89. flags Flags, deps PathDeps, objs Objects) android.Path {
  90. p.libraryDecorator.flagExporter.exportIncludes(ctx)
  91. p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
  92. p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
  93. p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
  94. p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
  95. p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
  96. p.libraryDecorator.flagExporter.setProvider(ctx)
  97. // TODO(ccross): verify shared library dependencies
  98. srcs := p.prebuiltSrcs(ctx)
  99. if len(srcs) > 0 {
  100. if len(srcs) > 1 {
  101. ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
  102. return nil
  103. }
  104. p.libraryDecorator.exportVersioningMacroIfNeeded(ctx)
  105. in := android.PathForModuleSrc(ctx, srcs[0])
  106. if String(p.prebuiltLinker.properties.Prefix_symbols) != "" {
  107. prefixed := android.PathForModuleOut(ctx, "prefixed", srcs[0])
  108. transformBinaryPrefixSymbols(ctx, String(p.prebuiltLinker.properties.Prefix_symbols),
  109. in, flagsToBuilderFlags(flags), prefixed)
  110. in = prefixed
  111. }
  112. if p.static() {
  113. depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
  114. ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
  115. StaticLibrary: in,
  116. TransitiveStaticLibrariesForOrdering: depSet,
  117. })
  118. return in
  119. }
  120. if p.shared() {
  121. p.unstrippedOutputFile = in
  122. libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
  123. outputFile := android.PathForModuleOut(ctx, libName)
  124. var implicits android.Paths
  125. if p.stripper.NeedsStrip(ctx) {
  126. stripFlags := flagsToStripFlags(flags)
  127. stripped := android.PathForModuleOut(ctx, "stripped", libName)
  128. p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
  129. in = stripped
  130. }
  131. // Optimize out relinking against shared libraries whose interface hasn't changed by
  132. // depending on a table of contents file instead of the library itself.
  133. tocFile := android.PathForModuleOut(ctx, libName+".toc")
  134. p.tocFile = android.OptionalPathForPath(tocFile)
  135. TransformSharedObjectToToc(ctx, outputFile, tocFile)
  136. if ctx.Windows() && p.properties.Windows_import_lib != nil {
  137. // Consumers of this library actually links to the import library in build
  138. // time and dynamically links to the DLL in run time. i.e.
  139. // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
  140. importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
  141. importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
  142. importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
  143. implicits = append(implicits, importLibOutputFile)
  144. ctx.Build(pctx, android.BuildParams{
  145. Rule: android.Cp,
  146. Description: "prebuilt import library",
  147. Input: importLibSrc,
  148. Output: importLibOutputFile,
  149. Args: map[string]string{
  150. "cpFlags": "-L",
  151. },
  152. })
  153. }
  154. ctx.Build(pctx, android.BuildParams{
  155. Rule: android.Cp,
  156. Description: "prebuilt shared library",
  157. Implicits: implicits,
  158. Input: in,
  159. Output: outputFile,
  160. Args: map[string]string{
  161. "cpFlags": "-L",
  162. },
  163. })
  164. ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
  165. SharedLibrary: outputFile,
  166. Target: ctx.Target(),
  167. TableOfContents: p.tocFile,
  168. })
  169. // TODO(b/220898484): Mainline module sdk prebuilts of stub libraries use a stub
  170. // library as their source and must not be installed, but libclang_rt.* libraries
  171. // have stubs because they are LLNDK libraries, but use an implementation library
  172. // as their source and need to be installed. This discrepancy should be resolved
  173. // without the prefix hack below.
  174. if p.hasStubsVariants() && !p.buildStubs() && !ctx.Host() &&
  175. !strings.HasPrefix(ctx.baseModuleName(), "libclang_rt.") {
  176. ctx.Module().MakeUninstallable()
  177. }
  178. return outputFile
  179. }
  180. }
  181. if p.header() {
  182. ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{})
  183. // Need to return an output path so that the AndroidMk logic doesn't skip
  184. // the prebuilt header. For compatibility, in case Android.mk files use a
  185. // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as
  186. // placeholder, just like non-prebuilt header modules do in linkStatic().
  187. ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
  188. transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil)
  189. return ph
  190. }
  191. return nil
  192. }
  193. func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
  194. sanitize := ctx.Module().(*Module).sanitize
  195. srcs := p.properties.Srcs
  196. srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
  197. if p.static() {
  198. srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
  199. srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
  200. }
  201. if p.shared() {
  202. srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
  203. srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
  204. }
  205. return srcs
  206. }
  207. func (p *prebuiltLibraryLinker) shared() bool {
  208. return p.libraryDecorator.shared()
  209. }
  210. func (p *prebuiltLibraryLinker) nativeCoverage() bool {
  211. return false
  212. }
  213. func (p *prebuiltLibraryLinker) disablePrebuilt() {
  214. p.properties.Srcs = nil
  215. p.properties.MixedBuildsDisabled = true
  216. }
  217. // Implements versionedInterface
  218. func (p *prebuiltLibraryLinker) implementationModuleName(name string) string {
  219. return android.RemoveOptionalPrebuiltPrefix(name)
  220. }
  221. func NewPrebuiltLibrary(hod android.HostOrDeviceSupported, srcsProperty string) (*Module, *libraryDecorator) {
  222. module, library := NewLibrary(hod)
  223. module.compiler = nil
  224. module.bazelable = true
  225. module.bazelHandler = &prebuiltLibraryBazelHandler{module: module, library: library}
  226. prebuilt := &prebuiltLibraryLinker{
  227. libraryDecorator: library,
  228. }
  229. module.linker = prebuilt
  230. module.library = prebuilt
  231. module.AddProperties(&prebuilt.properties)
  232. if srcsProperty == "" {
  233. android.InitPrebuiltModuleWithoutSrcs(module)
  234. } else {
  235. srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
  236. return prebuilt.prebuiltSrcs(ctx)
  237. }
  238. android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty)
  239. }
  240. return module, library
  241. }
  242. // cc_prebuilt_library installs a precompiled shared library that are
  243. // listed in the srcs property in the device's directory.
  244. func PrebuiltLibraryFactory() android.Module {
  245. module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
  246. // Prebuilt shared libraries can be included in APEXes
  247. android.InitApexModule(module)
  248. return module.Init()
  249. }
  250. // cc_prebuilt_library_shared installs a precompiled shared library that are
  251. // listed in the srcs property in the device's directory.
  252. func PrebuiltSharedLibraryFactory() android.Module {
  253. module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
  254. return module.Init()
  255. }
  256. // cc_prebuilt_test_library_shared installs a precompiled shared library
  257. // to be used as a data dependency of a test-related module (such as cc_test, or
  258. // cc_test_library).
  259. func PrebuiltSharedTestLibraryFactory() android.Module {
  260. module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
  261. library.BuildOnlyShared()
  262. library.baseInstaller = NewTestInstaller()
  263. return module.Init()
  264. }
  265. func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
  266. module, library := NewPrebuiltLibrary(hod, "srcs")
  267. library.BuildOnlyShared()
  268. // Prebuilt shared libraries can be included in APEXes
  269. android.InitApexModule(module)
  270. return module, library
  271. }
  272. // cc_prebuilt_library_static installs a precompiled static library that are
  273. // listed in the srcs property in the device's directory.
  274. func PrebuiltStaticLibraryFactory() android.Module {
  275. module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
  276. return module.Init()
  277. }
  278. func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
  279. module, library := NewPrebuiltLibrary(hod, "srcs")
  280. library.BuildOnlyStatic()
  281. return module, library
  282. }
  283. type bazelPrebuiltLibraryStaticAttributes struct {
  284. Static_library bazel.LabelAttribute
  285. Export_includes bazel.StringListAttribute
  286. Export_system_includes bazel.StringListAttribute
  287. }
  288. // TODO(b/228623543): The below is not entirely true until the bug is fixed. For now, both targets are always generated
  289. // Implements bp2build for cc_prebuilt_library modules. This will generate:
  290. // - Only a cc_prebuilt_library_static if the shared.enabled property is set to false across all variants.
  291. // - Only a cc_prebuilt_library_shared if the static.enabled property is set to false across all variants
  292. // - Both a cc_prebuilt_library_static and cc_prebuilt_library_shared if the aforementioned properties are not false across
  293. // all variants
  294. //
  295. // In all cases, cc_prebuilt_library_static target names will be appended with "_bp2build_cc_library_static".
  296. func prebuiltLibraryBp2Build(ctx android.TopDownMutatorContext, module *Module) {
  297. prebuiltLibraryStaticBp2Build(ctx, module, true)
  298. prebuiltLibrarySharedBp2Build(ctx, module)
  299. }
  300. func prebuiltLibraryStaticBp2Build(ctx android.TopDownMutatorContext, module *Module, fullBuild bool) {
  301. prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, true)
  302. exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, nil)
  303. attrs := &bazelPrebuiltLibraryStaticAttributes{
  304. Static_library: prebuiltAttrs.Src,
  305. Export_includes: exportedIncludes.Includes,
  306. Export_system_includes: exportedIncludes.SystemIncludes,
  307. }
  308. props := bazel.BazelTargetModuleProperties{
  309. Rule_class: "cc_prebuilt_library_static",
  310. Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_library_static.bzl",
  311. }
  312. name := android.RemoveOptionalPrebuiltPrefix(module.Name())
  313. if fullBuild {
  314. name += "_bp2build_cc_library_static"
  315. }
  316. tags := android.ApexAvailableTags(module)
  317. ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name, Tags: tags}, attrs, prebuiltAttrs.Enabled)
  318. }
  319. type bazelPrebuiltLibrarySharedAttributes struct {
  320. Shared_library bazel.LabelAttribute
  321. }
  322. func prebuiltLibrarySharedBp2Build(ctx android.TopDownMutatorContext, module *Module) {
  323. prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, false)
  324. attrs := &bazelPrebuiltLibrarySharedAttributes{
  325. Shared_library: prebuiltAttrs.Src,
  326. }
  327. props := bazel.BazelTargetModuleProperties{
  328. Rule_class: "cc_prebuilt_library_shared",
  329. Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_library_shared.bzl",
  330. }
  331. name := android.RemoveOptionalPrebuiltPrefix(module.Name())
  332. tags := android.ApexAvailableTags(module)
  333. ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name, Tags: tags}, attrs, prebuiltAttrs.Enabled)
  334. }
  335. type prebuiltObjectProperties struct {
  336. Srcs []string `android:"path,arch_variant"`
  337. }
  338. type prebuiltObjectLinker struct {
  339. android.Prebuilt
  340. objectLinker
  341. properties prebuiltObjectProperties
  342. }
  343. type prebuiltLibraryBazelHandler struct {
  344. module *Module
  345. library *libraryDecorator
  346. }
  347. var _ BazelHandler = (*prebuiltLibraryBazelHandler)(nil)
  348. func (h *prebuiltLibraryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
  349. if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled {
  350. return
  351. }
  352. bazelCtx := ctx.Config().BazelContext
  353. bazelCtx.QueueBazelRequest(label, cquery.GetCcInfo, android.GetConfigKey(ctx))
  354. }
  355. func (h *prebuiltLibraryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
  356. if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled {
  357. return
  358. }
  359. bazelCtx := ctx.Config().BazelContext
  360. ccInfo, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
  361. if err != nil {
  362. ctx.ModuleErrorf(err.Error())
  363. return
  364. }
  365. if h.module.static() {
  366. if ok := h.processStaticBazelQueryResponse(ctx, label, ccInfo); !ok {
  367. return
  368. }
  369. } else if h.module.Shared() {
  370. if ok := h.processSharedBazelQueryResponse(ctx, label, ccInfo); !ok {
  371. return
  372. }
  373. } else {
  374. return
  375. }
  376. h.module.maybeUnhideFromMake()
  377. h.module.setAndroidMkVariablesFromCquery(ccInfo.CcAndroidMkInfo)
  378. }
  379. func (h *prebuiltLibraryBazelHandler) processStaticBazelQueryResponse(ctx android.ModuleContext, label string, ccInfo cquery.CcInfo) bool {
  380. staticLibs := ccInfo.CcStaticLibraryFiles
  381. if len(staticLibs) > 1 {
  382. ctx.ModuleErrorf("expected 1 static library from bazel target %q, got %s", label, staticLibs)
  383. return false
  384. }
  385. // TODO(b/184543518): cc_prebuilt_library_static may have properties for re-exporting flags
  386. // TODO(eakammer):Add stub-related flags if this library is a stub library.
  387. // h.library.exportVersioningMacroIfNeeded(ctx)
  388. // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
  389. // validation will fail. For now, set this to an empty list.
  390. // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
  391. h.library.collectedSnapshotHeaders = android.Paths{}
  392. if len(staticLibs) == 0 {
  393. h.module.outputFile = android.OptionalPath{}
  394. return true
  395. }
  396. var outputPath android.Path = android.PathForBazelOut(ctx, staticLibs[0])
  397. if len(ccInfo.TidyFiles) > 0 {
  398. h.module.tidyFiles = android.PathsForBazelOut(ctx, ccInfo.TidyFiles)
  399. outputPath = android.AttachValidationActions(ctx, outputPath, h.module.tidyFiles)
  400. }
  401. h.module.outputFile = android.OptionalPathForPath(outputPath)
  402. depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(outputPath).Build()
  403. ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
  404. StaticLibrary: outputPath,
  405. TransitiveStaticLibrariesForOrdering: depSet,
  406. })
  407. return true
  408. }
  409. func (h *prebuiltLibraryBazelHandler) processSharedBazelQueryResponse(ctx android.ModuleContext, label string, ccInfo cquery.CcInfo) bool {
  410. sharedLibs := ccInfo.CcSharedLibraryFiles
  411. if len(sharedLibs) > 1 {
  412. ctx.ModuleErrorf("expected 1 shared library from bazel target %s, got %q", label, sharedLibs)
  413. return false
  414. }
  415. // TODO(b/184543518): cc_prebuilt_library_shared may have properties for re-exporting flags
  416. // TODO(eakammer):Add stub-related flags if this library is a stub library.
  417. // h.library.exportVersioningMacroIfNeeded(ctx)
  418. if len(sharedLibs) == 0 {
  419. h.module.outputFile = android.OptionalPath{}
  420. return true
  421. }
  422. var outputPath android.Path = android.PathForBazelOut(ctx, sharedLibs[0])
  423. if len(ccInfo.TidyFiles) > 0 {
  424. h.module.tidyFiles = android.PathsForBazelOut(ctx, ccInfo.TidyFiles)
  425. outputPath = android.AttachValidationActions(ctx, outputPath, h.module.tidyFiles)
  426. }
  427. h.module.outputFile = android.OptionalPathForPath(outputPath)
  428. // FIXME(b/214600441): We don't yet strip prebuilt shared libraries
  429. h.library.unstrippedOutputFile = outputPath
  430. var toc android.Path
  431. if len(ccInfo.TocFile) > 0 {
  432. toc = android.PathForBazelOut(ctx, ccInfo.TocFile)
  433. } else {
  434. toc = outputPath // Just reuse `out` so ninja still gets an input but won't matter
  435. }
  436. info := SharedLibraryInfo{
  437. SharedLibrary: outputPath,
  438. TableOfContents: android.OptionalPathForPath(toc),
  439. Target: ctx.Target(),
  440. }
  441. ctx.SetProvider(SharedLibraryInfoProvider, info)
  442. h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo)
  443. h.module.maybeUnhideFromMake()
  444. return true
  445. }
  446. func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
  447. return &p.Prebuilt
  448. }
  449. var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
  450. func (p *prebuiltObjectLinker) link(ctx ModuleContext,
  451. flags Flags, deps PathDeps, objs Objects) android.Path {
  452. if len(p.properties.Srcs) > 0 {
  453. // Copy objects to a name matching the final installed name
  454. in := p.Prebuilt.SingleSourcePath(ctx)
  455. outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o")
  456. ctx.Build(pctx, android.BuildParams{
  457. Rule: android.CpExecutable,
  458. Description: "prebuilt",
  459. Output: outputFile,
  460. Input: in,
  461. })
  462. return outputFile
  463. }
  464. return nil
  465. }
  466. func (p *prebuiltObjectLinker) object() bool {
  467. return true
  468. }
  469. func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
  470. module := newObject(hod)
  471. module.bazelHandler = &prebuiltObjectBazelHandler{module: module}
  472. module.bazelable = true
  473. prebuilt := &prebuiltObjectLinker{
  474. objectLinker: objectLinker{
  475. baseLinker: NewBaseLinker(nil),
  476. },
  477. }
  478. module.linker = prebuilt
  479. module.AddProperties(&prebuilt.properties)
  480. android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
  481. return module
  482. }
  483. type prebuiltObjectBazelHandler struct {
  484. module *Module
  485. }
  486. var _ BazelHandler = (*prebuiltObjectBazelHandler)(nil)
  487. func (h *prebuiltObjectBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
  488. bazelCtx := ctx.Config().BazelContext
  489. bazelCtx.QueueBazelRequest(label, cquery.GetOutputFiles, android.GetConfigKey(ctx))
  490. }
  491. func (h *prebuiltObjectBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
  492. bazelCtx := ctx.Config().BazelContext
  493. outputs, err := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx))
  494. if err != nil {
  495. ctx.ModuleErrorf(err.Error())
  496. return
  497. }
  498. if len(outputs) != 1 {
  499. ctx.ModuleErrorf("Expected a single output for `%s`, but got:\n%v", label, outputs)
  500. return
  501. }
  502. out := android.PathForBazelOut(ctx, outputs[0])
  503. h.module.outputFile = android.OptionalPathForPath(out)
  504. h.module.maybeUnhideFromMake()
  505. }
  506. type bazelPrebuiltObjectAttributes struct {
  507. Src bazel.LabelAttribute
  508. }
  509. func prebuiltObjectBp2Build(ctx android.TopDownMutatorContext, module *Module) {
  510. prebuiltAttrs := bp2BuildParsePrebuiltObjectProps(ctx, module)
  511. attrs := &bazelPrebuiltObjectAttributes{
  512. Src: prebuiltAttrs.Src,
  513. }
  514. props := bazel.BazelTargetModuleProperties{
  515. Rule_class: "cc_prebuilt_object",
  516. Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_object.bzl",
  517. }
  518. name := android.RemoveOptionalPrebuiltPrefix(module.Name())
  519. tags := android.ApexAvailableTags(module)
  520. ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name, Tags: tags}, attrs)
  521. }
  522. func PrebuiltObjectFactory() android.Module {
  523. module := NewPrebuiltObject(android.HostAndDeviceSupported)
  524. return module.Init()
  525. }
  526. type prebuiltBinaryLinker struct {
  527. *binaryDecorator
  528. prebuiltLinker
  529. toolPath android.OptionalPath
  530. }
  531. var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
  532. func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
  533. return p.toolPath
  534. }
  535. func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
  536. flags Flags, deps PathDeps, objs Objects) android.Path {
  537. // TODO(ccross): verify shared library dependencies
  538. if len(p.properties.Srcs) > 0 {
  539. fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
  540. in := p.Prebuilt.SingleSourcePath(ctx)
  541. outputFile := android.PathForModuleOut(ctx, fileName)
  542. p.unstrippedOutputFile = in
  543. if ctx.Host() {
  544. // Host binaries are symlinked to their prebuilt source locations. That
  545. // way they are executed directly from there so the linker resolves their
  546. // shared library dependencies relative to that location (using
  547. // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
  548. // repository can supply the expected versions of the shared libraries
  549. // without interference from what is in the out tree.
  550. // These shared lib paths may point to copies of the libs in
  551. // .intermediates, which isn't where the binary will load them from, but
  552. // it's fine for dependency tracking. If a library dependency is updated,
  553. // the symlink will get a new timestamp, along with any installed symlinks
  554. // handled in make.
  555. sharedLibPaths := deps.EarlySharedLibs
  556. sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
  557. sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
  558. var fromPath = in.String()
  559. if !filepath.IsAbs(fromPath) {
  560. fromPath = "$$PWD/" + fromPath
  561. }
  562. ctx.Build(pctx, android.BuildParams{
  563. Rule: android.Symlink,
  564. Output: outputFile,
  565. Input: in,
  566. Implicits: sharedLibPaths,
  567. Args: map[string]string{
  568. "fromPath": fromPath,
  569. },
  570. })
  571. p.toolPath = android.OptionalPathForPath(outputFile)
  572. } else {
  573. if p.stripper.NeedsStrip(ctx) {
  574. stripped := android.PathForModuleOut(ctx, "stripped", fileName)
  575. p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
  576. in = stripped
  577. }
  578. // Copy binaries to a name matching the final installed name
  579. ctx.Build(pctx, android.BuildParams{
  580. Rule: android.CpExecutable,
  581. Description: "prebuilt",
  582. Output: outputFile,
  583. Input: in,
  584. })
  585. }
  586. return outputFile
  587. }
  588. return nil
  589. }
  590. func (p *prebuiltBinaryLinker) binary() bool {
  591. return true
  592. }
  593. // cc_prebuilt_binary installs a precompiled executable in srcs property in the
  594. // device's directory, for both the host and device
  595. func PrebuiltBinaryFactory() android.Module {
  596. module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
  597. return module.Init()
  598. }
  599. type prebuiltBinaryBazelHandler struct {
  600. module *Module
  601. decorator *binaryDecorator
  602. }
  603. func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
  604. module, binary := newBinary(hod, true)
  605. module.compiler = nil
  606. module.bazelHandler = &prebuiltBinaryBazelHandler{module, binary}
  607. prebuilt := &prebuiltBinaryLinker{
  608. binaryDecorator: binary,
  609. }
  610. module.linker = prebuilt
  611. module.installer = prebuilt
  612. module.AddProperties(&prebuilt.properties)
  613. android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
  614. return module, binary
  615. }
  616. var _ BazelHandler = (*prebuiltBinaryBazelHandler)(nil)
  617. func (h *prebuiltBinaryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
  618. bazelCtx := ctx.Config().BazelContext
  619. bazelCtx.QueueBazelRequest(label, cquery.GetOutputFiles, android.GetConfigKey(ctx))
  620. }
  621. func (h *prebuiltBinaryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
  622. bazelCtx := ctx.Config().BazelContext
  623. outputs, err := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx))
  624. if err != nil {
  625. ctx.ModuleErrorf(err.Error())
  626. return
  627. }
  628. if len(outputs) != 1 {
  629. ctx.ModuleErrorf("Expected a single output for `%s`, but got:\n%v", label, outputs)
  630. return
  631. }
  632. out := android.PathForBazelOut(ctx, outputs[0])
  633. h.module.outputFile = android.OptionalPathForPath(out)
  634. h.module.maybeUnhideFromMake()
  635. }
  636. type bazelPrebuiltBinaryAttributes struct {
  637. Src bazel.LabelAttribute
  638. Strip stripAttributes
  639. }
  640. func prebuiltBinaryBp2Build(ctx android.TopDownMutatorContext, module *Module) {
  641. prebuiltAttrs := bp2BuildParsePrebuiltBinaryProps(ctx, module)
  642. var la linkerAttributes
  643. la.convertStripProps(ctx, module)
  644. attrs := &bazelPrebuiltBinaryAttributes{
  645. Src: prebuiltAttrs.Src,
  646. Strip: stripAttrsFromLinkerAttrs(&la),
  647. }
  648. props := bazel.BazelTargetModuleProperties{
  649. Rule_class: "cc_prebuilt_binary",
  650. Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_binary.bzl",
  651. }
  652. name := android.RemoveOptionalPrebuiltPrefix(module.Name())
  653. tags := android.ApexAvailableTags(module)
  654. ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name, Tags: tags}, attrs)
  655. }
  656. type Sanitized struct {
  657. None struct {
  658. Srcs []string `android:"path,arch_variant"`
  659. } `android:"arch_variant"`
  660. Address struct {
  661. Srcs []string `android:"path,arch_variant"`
  662. } `android:"arch_variant"`
  663. Hwaddress struct {
  664. Srcs []string `android:"path,arch_variant"`
  665. } `android:"arch_variant"`
  666. }
  667. func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
  668. if sanitize == nil {
  669. return nil
  670. }
  671. if sanitize.isSanitizerEnabled(Asan) && sanitized.Address.Srcs != nil {
  672. return sanitized.Address.Srcs
  673. }
  674. if sanitize.isSanitizerEnabled(Hwasan) && sanitized.Hwaddress.Srcs != nil {
  675. return sanitized.Hwaddress.Srcs
  676. }
  677. return sanitized.None.Srcs
  678. }