prebuilt.go 23 KB

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