binary.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. "android/soong/bazel/cquery"
  18. "github.com/google/blueprint"
  19. "github.com/google/blueprint/proptools"
  20. "android/soong/android"
  21. "android/soong/bazel"
  22. )
  23. type BinaryLinkerProperties struct {
  24. // compile executable with -static
  25. Static_executable *bool `android:"arch_variant"`
  26. // set the name of the output
  27. Stem *string `android:"arch_variant"`
  28. // append to the name of the output
  29. Suffix *string `android:"arch_variant"`
  30. // if set, add an extra objcopy --prefix-symbols= step
  31. Prefix_symbols *string
  32. // if set, install a symlink to the preferred architecture
  33. Symlink_preferred_arch *bool `android:"arch_variant"`
  34. // install symlinks to the binary. Symlink names will have the suffix and the binary
  35. // extension (if any) appended
  36. Symlinks []string `android:"arch_variant"`
  37. // override the dynamic linker
  38. DynamicLinker string `blueprint:"mutated"`
  39. // Names of modules to be overridden. Listed modules can only be other binaries
  40. // (in Make or Soong).
  41. // This does not completely prevent installation of the overridden binaries, but if both
  42. // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
  43. // from PRODUCT_PACKAGES.
  44. Overrides []string
  45. // Inject boringssl hash into the shared library. This is only intended for use by external/boringssl.
  46. Inject_bssl_hash *bool `android:"arch_variant"`
  47. }
  48. func init() {
  49. RegisterBinaryBuildComponents(android.InitRegistrationContext)
  50. }
  51. func RegisterBinaryBuildComponents(ctx android.RegistrationContext) {
  52. ctx.RegisterModuleType("cc_binary", BinaryFactory)
  53. ctx.RegisterModuleType("cc_binary_host", BinaryHostFactory)
  54. }
  55. // cc_binary produces a binary that is runnable on a device.
  56. func BinaryFactory() android.Module {
  57. module, _ := newBinary(android.HostAndDeviceSupported, true)
  58. module.bazelHandler = &ccBinaryBazelHandler{module: module}
  59. return module.Init()
  60. }
  61. // cc_binary_host produces a binary that is runnable on a host.
  62. func BinaryHostFactory() android.Module {
  63. module, _ := newBinary(android.HostSupported, true)
  64. return module.Init()
  65. }
  66. //
  67. // Executables
  68. //
  69. // binaryDecorator is a decorator containing information for C++ binary modules.
  70. type binaryDecorator struct {
  71. *baseLinker
  72. *baseInstaller
  73. stripper Stripper
  74. Properties BinaryLinkerProperties
  75. toolPath android.OptionalPath
  76. // Location of the linked, unstripped binary
  77. unstrippedOutputFile android.Path
  78. // Names of symlinks to be installed for use in LOCAL_MODULE_SYMLINKS
  79. symlinks []string
  80. // If the module has symlink_preferred_arch set, the name of the symlink to the
  81. // binary for the preferred arch.
  82. preferredArchSymlink string
  83. // Output archive of gcno coverage information
  84. coverageOutputFile android.OptionalPath
  85. // Location of the files that should be copied to dist dir when requested
  86. distFiles android.TaggedDistFiles
  87. // Action command lines to run directly after the binary is installed. For example,
  88. // may be used to symlink runtime dependencies (such as bionic) alongside installation.
  89. postInstallCmds []string
  90. }
  91. var _ linker = (*binaryDecorator)(nil)
  92. // linkerProps returns the list of individual properties objects relevant
  93. // for this binary.
  94. func (binary *binaryDecorator) linkerProps() []interface{} {
  95. return append(binary.baseLinker.linkerProps(),
  96. &binary.Properties,
  97. &binary.stripper.StripProperties)
  98. }
  99. // getStemWithoutSuffix returns the main section of the name to use for the symlink of
  100. // the main output file of this binary module. This may be derived from the module name
  101. // or other property overrides.
  102. // For the full symlink name, the `Suffix` property of a binary module must be appended.
  103. func (binary *binaryDecorator) getStemWithoutSuffix(ctx BaseModuleContext) string {
  104. stem := ctx.baseModuleName()
  105. if String(binary.Properties.Stem) != "" {
  106. stem = String(binary.Properties.Stem)
  107. }
  108. return stem
  109. }
  110. // getStem returns the full name to use for the symlink of the main output file of this binary
  111. // module. This may be derived from the module name and/or other property overrides.
  112. func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
  113. return binary.getStemWithoutSuffix(ctx) + String(binary.Properties.Suffix)
  114. }
  115. // linkerDeps augments and returns the given `deps` to contain dependencies on
  116. // modules common to most binaries, such as bionic libraries.
  117. func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
  118. deps = binary.baseLinker.linkerDeps(ctx, deps)
  119. if binary.baseLinker.Properties.crt() {
  120. if binary.static() {
  121. deps.CrtBegin = ctx.toolchain().CrtBeginStaticBinary()
  122. deps.CrtEnd = ctx.toolchain().CrtEndStaticBinary()
  123. } else {
  124. deps.CrtBegin = ctx.toolchain().CrtBeginSharedBinary()
  125. deps.CrtEnd = ctx.toolchain().CrtEndSharedBinary()
  126. }
  127. }
  128. if binary.static() {
  129. deps.StaticLibs = append(deps.StaticLibs, deps.SystemSharedLibs...)
  130. }
  131. if ctx.toolchain().Bionic() {
  132. if binary.static() {
  133. if ctx.selectedStl() == "libc++_static" {
  134. deps.StaticLibs = append(deps.StaticLibs, "libm", "libc")
  135. }
  136. // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
  137. // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
  138. // move them to the beginning of deps.LateStaticLibs
  139. var groupLibs []string
  140. deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
  141. []string{"libc", "libc_nomalloc", "libcompiler_rt"})
  142. deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
  143. }
  144. if ctx.Os() == android.LinuxBionic && !binary.static() {
  145. deps.DynamicLinker = "linker"
  146. }
  147. }
  148. if !binary.static() && inList("libc", deps.StaticLibs) {
  149. ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
  150. "from static libs or set static_executable: true")
  151. }
  152. return deps
  153. }
  154. // NewBinary builds and returns a new Module corresponding to a C++ binary.
  155. // Individual module implementations which comprise a C++ binary should call this function,
  156. // set some fields on the result, and then call the Init function.
  157. func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
  158. return newBinary(hod, true)
  159. }
  160. func newBinary(hod android.HostOrDeviceSupported, bazelable bool) (*Module, *binaryDecorator) {
  161. module := newModule(hod, android.MultilibFirst)
  162. binary := &binaryDecorator{
  163. baseLinker: NewBaseLinker(module.sanitize),
  164. baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
  165. }
  166. module.compiler = NewBaseCompiler()
  167. module.linker = binary
  168. module.installer = binary
  169. module.bazelable = bazelable
  170. // Allow module to be added as member of an sdk/module_exports.
  171. module.sdkMemberTypes = []android.SdkMemberType{
  172. ccBinarySdkMemberType,
  173. }
  174. return module, binary
  175. }
  176. // linkerInit initializes dynamic properties of the linker (such as runpath) based
  177. // on properties of this binary.
  178. func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
  179. binary.baseLinker.linkerInit(ctx)
  180. if ctx.Os().Linux() && ctx.Host() {
  181. // Unless explicitly specified otherwise, host static binaries are built with -static
  182. // if HostStaticBinaries is true for the product configuration.
  183. if binary.Properties.Static_executable == nil && ctx.Config().HostStaticBinaries() {
  184. binary.Properties.Static_executable = BoolPtr(true)
  185. }
  186. }
  187. if ctx.Darwin() || ctx.Windows() {
  188. // Static executables are not supported on Darwin or Windows
  189. binary.Properties.Static_executable = nil
  190. }
  191. }
  192. func (binary *binaryDecorator) static() bool {
  193. return Bool(binary.Properties.Static_executable)
  194. }
  195. func (binary *binaryDecorator) staticBinary() bool {
  196. return binary.static()
  197. }
  198. func (binary *binaryDecorator) binary() bool {
  199. return true
  200. }
  201. // linkerFlags returns a Flags object containing linker flags that are defined
  202. // by this binary, or that are implied by attributes of this binary. These flags are
  203. // combined with the given flags.
  204. func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
  205. flags = binary.baseLinker.linkerFlags(ctx, flags)
  206. // Passing -pie to clang for Windows binaries causes a warning that -pie is unused.
  207. if ctx.Host() && !ctx.Windows() && !binary.static() {
  208. if !ctx.Config().IsEnvTrue("DISABLE_HOST_PIE") {
  209. flags.Global.LdFlags = append(flags.Global.LdFlags, "-pie")
  210. }
  211. }
  212. // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
  213. // all code is position independent, and then those warnings get promoted to
  214. // errors.
  215. if !ctx.Windows() {
  216. flags.Global.CFlags = append(flags.Global.CFlags, "-fPIE")
  217. }
  218. if ctx.toolchain().Bionic() {
  219. if binary.static() {
  220. // Clang driver needs -static to create static executable.
  221. // However, bionic/linker uses -shared to overwrite.
  222. // Linker for x86 targets does not allow coexistance of -static and -shared,
  223. // so we add -static only if -shared is not used.
  224. if !inList("-shared", flags.Local.LdFlags) {
  225. flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
  226. }
  227. flags.Global.LdFlags = append(flags.Global.LdFlags,
  228. "-nostdlib",
  229. "-Bstatic",
  230. "-Wl,--gc-sections",
  231. )
  232. } else { // not static
  233. if flags.DynamicLinker == "" {
  234. if binary.Properties.DynamicLinker != "" {
  235. flags.DynamicLinker = binary.Properties.DynamicLinker
  236. } else {
  237. switch ctx.Os() {
  238. case android.Android:
  239. if ctx.bootstrap() && !ctx.inRecovery() && !ctx.inRamdisk() && !ctx.inVendorRamdisk() {
  240. flags.DynamicLinker = "/system/bin/bootstrap/linker"
  241. } else {
  242. flags.DynamicLinker = "/system/bin/linker"
  243. }
  244. if flags.Toolchain.Is64Bit() {
  245. flags.DynamicLinker += "64"
  246. }
  247. case android.LinuxBionic:
  248. flags.DynamicLinker = ""
  249. default:
  250. ctx.ModuleErrorf("unknown dynamic linker")
  251. }
  252. }
  253. if ctx.Os() == android.LinuxBionic {
  254. // Use the dlwrap entry point, but keep _start around so
  255. // that it can be used by host_bionic_inject
  256. flags.Global.LdFlags = append(flags.Global.LdFlags,
  257. "-Wl,--entry=__dlwrap__start",
  258. "-Wl,--undefined=_start",
  259. )
  260. }
  261. }
  262. flags.Global.LdFlags = append(flags.Global.LdFlags,
  263. "-pie",
  264. "-nostdlib",
  265. "-Bdynamic",
  266. "-Wl,--gc-sections",
  267. "-Wl,-z,nocopyreloc",
  268. )
  269. }
  270. } else { // not bionic
  271. if binary.static() {
  272. flags.Global.LdFlags = append(flags.Global.LdFlags, "-static")
  273. }
  274. if ctx.Darwin() {
  275. flags.Global.LdFlags = append(flags.Global.LdFlags, "-Wl,-headerpad_max_install_names")
  276. }
  277. }
  278. return flags
  279. }
  280. // link registers actions to link this binary, and sets various fields
  281. // on this binary to reflect information that should be exported up the build
  282. // tree (for example, exported flags and include paths).
  283. func (binary *binaryDecorator) link(ctx ModuleContext,
  284. flags Flags, deps PathDeps, objs Objects) android.Path {
  285. fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
  286. outputFile := android.PathForModuleOut(ctx, fileName)
  287. ret := outputFile
  288. var linkerDeps android.Paths
  289. if flags.DynamicLinker != "" {
  290. flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-dynamic-linker,"+flags.DynamicLinker)
  291. } else if (ctx.toolchain().Bionic() || ctx.toolchain().Musl()) && !binary.static() {
  292. flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-dynamic-linker")
  293. }
  294. if ctx.Darwin() && deps.DarwinSecondArchOutput.Valid() {
  295. fatOutputFile := outputFile
  296. outputFile = android.PathForModuleOut(ctx, "pre-fat", fileName)
  297. transformDarwinUniversalBinary(ctx, fatOutputFile, outputFile, deps.DarwinSecondArchOutput.Path())
  298. }
  299. builderFlags := flagsToBuilderFlags(flags)
  300. stripFlags := flagsToStripFlags(flags)
  301. if binary.stripper.NeedsStrip(ctx) {
  302. if ctx.Darwin() {
  303. stripFlags.StripUseGnuStrip = true
  304. }
  305. strippedOutputFile := outputFile
  306. outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
  307. binary.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, stripFlags)
  308. }
  309. binary.unstrippedOutputFile = outputFile
  310. if String(binary.Properties.Prefix_symbols) != "" {
  311. afterPrefixSymbols := outputFile
  312. outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
  313. transformBinaryPrefixSymbols(ctx, String(binary.Properties.Prefix_symbols), outputFile,
  314. builderFlags, afterPrefixSymbols)
  315. }
  316. outputFile = maybeInjectBoringSSLHash(ctx, outputFile, binary.Properties.Inject_bssl_hash, fileName)
  317. // If use_version_lib is true, make an android::build::GetBuildNumber() function available.
  318. if Bool(binary.baseLinker.Properties.Use_version_lib) {
  319. if ctx.Host() {
  320. versionedOutputFile := outputFile
  321. outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
  322. binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
  323. } else {
  324. // When dist'ing a library or binary that has use_version_lib set, always
  325. // distribute the stamped version, even for the device.
  326. versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
  327. binary.distFiles = android.MakeDefaultDistFiles(versionedOutputFile)
  328. if binary.stripper.NeedsStrip(ctx) {
  329. out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
  330. binary.distFiles = android.MakeDefaultDistFiles(out)
  331. binary.stripper.StripExecutableOrSharedLib(ctx, versionedOutputFile, out, stripFlags)
  332. }
  333. binary.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
  334. }
  335. }
  336. var validations android.Paths
  337. // Handle host bionic linker symbols.
  338. if ctx.Os() == android.LinuxBionic && !binary.static() {
  339. verifyFile := android.PathForModuleOut(ctx, "host_bionic_verify.stamp")
  340. if !deps.DynamicLinker.Valid() {
  341. panic("Non-static host bionic modules must have a dynamic linker")
  342. }
  343. binary.verifyHostBionicLinker(ctx, outputFile, deps.DynamicLinker.Path(), verifyFile)
  344. validations = append(validations, verifyFile)
  345. }
  346. var sharedLibs android.Paths
  347. // Ignore shared libs for static executables.
  348. if !binary.static() {
  349. sharedLibs = deps.EarlySharedLibs
  350. sharedLibs = append(sharedLibs, deps.SharedLibs...)
  351. sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
  352. linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
  353. linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
  354. linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
  355. linkerDeps = append(linkerDeps, ndkSharedLibDeps(ctx)...)
  356. }
  357. validations = append(validations, objs.tidyDepFiles...)
  358. linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
  359. // Register link action.
  360. transformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, deps.StaticLibs,
  361. deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
  362. builderFlags, outputFile, nil, validations)
  363. objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
  364. objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
  365. binary.coverageOutputFile = transformCoverageFilesToZip(ctx, objs, binary.getStem(ctx))
  366. // Need to determine symlinks early since some targets (ie APEX) need this
  367. // information but will not call 'install'
  368. binary.setSymlinkList(ctx)
  369. return ret
  370. }
  371. func (binary *binaryDecorator) unstrippedOutputFilePath() android.Path {
  372. return binary.unstrippedOutputFile
  373. }
  374. func (binary *binaryDecorator) setSymlinkList(ctx ModuleContext) {
  375. for _, symlink := range binary.Properties.Symlinks {
  376. binary.symlinks = append(binary.symlinks,
  377. symlink+String(binary.Properties.Suffix)+ctx.toolchain().ExecutableSuffix())
  378. }
  379. if Bool(binary.Properties.Symlink_preferred_arch) {
  380. if String(binary.Properties.Suffix) == "" {
  381. ctx.PropertyErrorf("symlink_preferred_arch", "must also specify suffix")
  382. }
  383. if ctx.TargetPrimary() {
  384. // Install a symlink to the preferred architecture
  385. symlinkName := binary.getStemWithoutSuffix(ctx)
  386. binary.symlinks = append(binary.symlinks, symlinkName)
  387. binary.preferredArchSymlink = symlinkName
  388. }
  389. }
  390. }
  391. func (binary *binaryDecorator) symlinkList() []string {
  392. return binary.symlinks
  393. }
  394. func (binary *binaryDecorator) nativeCoverage() bool {
  395. return true
  396. }
  397. func (binary *binaryDecorator) coverageOutputFilePath() android.OptionalPath {
  398. return binary.coverageOutputFile
  399. }
  400. // /system/bin/linker -> /apex/com.android.runtime/bin/linker
  401. func (binary *binaryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
  402. dir := binary.baseInstaller.installDir(ctx)
  403. dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
  404. target := "/" + filepath.Join("apex", "com.android.runtime", dir.Base(), file.Base())
  405. ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
  406. binary.postInstallCmds = append(binary.postInstallCmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
  407. for _, symlink := range binary.symlinks {
  408. ctx.InstallAbsoluteSymlink(dir, symlink, target)
  409. binary.postInstallCmds = append(binary.postInstallCmds, makeSymlinkCmd(dirOnDevice, symlink, target))
  410. }
  411. }
  412. func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
  413. // Bionic binaries (e.g. linker) is installed to the bootstrap subdirectory.
  414. // The original path becomes a symlink to the corresponding file in the
  415. // runtime APEX.
  416. translatedArch := ctx.Target().NativeBridge == android.NativeBridgeEnabled
  417. if InstallToBootstrap(ctx.baseModuleName(), ctx.Config()) && !ctx.Host() && ctx.directlyInAnyApex() &&
  418. !translatedArch && ctx.apexVariationName() == "" && !ctx.inRamdisk() && !ctx.inRecovery() &&
  419. !ctx.inVendorRamdisk() {
  420. if ctx.Device() && isBionic(ctx.baseModuleName()) {
  421. binary.installSymlinkToRuntimeApex(ctx, file)
  422. }
  423. binary.baseInstaller.subDir = "bootstrap"
  424. }
  425. binary.baseInstaller.install(ctx, file)
  426. var preferredArchSymlinkPath android.OptionalPath
  427. for _, symlink := range binary.symlinks {
  428. installedSymlink := ctx.InstallSymlink(binary.baseInstaller.installDir(ctx), symlink,
  429. binary.baseInstaller.path)
  430. if symlink == binary.preferredArchSymlink {
  431. // If this is the preferred arch symlink, save the installed path for use as the
  432. // tool path.
  433. preferredArchSymlinkPath = android.OptionalPathForPath(installedSymlink)
  434. }
  435. }
  436. if ctx.Os().Class == android.Host {
  437. // If the binary is multilib with a symlink to the preferred architecture, use the
  438. // symlink instead of the binary because that's the more "canonical" name.
  439. if preferredArchSymlinkPath.Valid() {
  440. binary.toolPath = preferredArchSymlinkPath
  441. } else {
  442. binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
  443. }
  444. }
  445. }
  446. func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
  447. return binary.toolPath
  448. }
  449. func (binary *binaryDecorator) overriddenModules() []string {
  450. return binary.Properties.Overrides
  451. }
  452. var _ overridable = (*binaryDecorator)(nil)
  453. func init() {
  454. pctx.HostBinToolVariable("verifyHostBionicCmd", "host_bionic_verify")
  455. }
  456. var verifyHostBionic = pctx.AndroidStaticRule("verifyHostBionic",
  457. blueprint.RuleParams{
  458. Command: "$verifyHostBionicCmd -i $in -l $linker && touch $out",
  459. CommandDeps: []string{"$verifyHostBionicCmd"},
  460. }, "linker")
  461. func (binary *binaryDecorator) verifyHostBionicLinker(ctx ModuleContext, in, linker android.Path, out android.WritablePath) {
  462. ctx.Build(pctx, android.BuildParams{
  463. Rule: verifyHostBionic,
  464. Description: "verify host bionic",
  465. Input: in,
  466. Implicit: linker,
  467. Output: out,
  468. Args: map[string]string{
  469. "linker": linker.String(),
  470. },
  471. })
  472. }
  473. type ccBinaryBazelHandler struct {
  474. module *Module
  475. }
  476. var _ BazelHandler = (*ccBinaryBazelHandler)(nil)
  477. func (handler *ccBinaryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
  478. bazelCtx := ctx.Config().BazelContext
  479. bazelCtx.QueueBazelRequest(label, cquery.GetCcUnstrippedInfo, android.GetConfigKeyApexVariant(ctx, GetApexConfigKey(ctx)))
  480. }
  481. func (handler *ccBinaryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
  482. bazelCtx := ctx.Config().BazelContext
  483. info, err := bazelCtx.GetCcUnstrippedInfo(label, android.GetConfigKeyApexVariant(ctx, GetApexConfigKey(ctx)))
  484. if err != nil {
  485. ctx.ModuleErrorf(err.Error())
  486. return
  487. }
  488. var outputFilePath android.Path = android.PathForBazelOut(ctx, info.OutputFile)
  489. if len(info.TidyFiles) > 0 {
  490. handler.module.tidyFiles = android.PathsForBazelOut(ctx, info.TidyFiles)
  491. outputFilePath = android.AttachValidationActions(ctx, outputFilePath, handler.module.tidyFiles)
  492. }
  493. handler.module.outputFile = android.OptionalPathForPath(outputFilePath)
  494. handler.module.linker.(*binaryDecorator).unstrippedOutputFile = android.PathForBazelOut(ctx, info.UnstrippedOutput)
  495. handler.module.setAndroidMkVariablesFromCquery(info.CcAndroidMkInfo)
  496. }
  497. func binaryBp2buildAttrs(ctx android.TopDownMutatorContext, m *Module) binaryAttributes {
  498. baseAttrs := bp2BuildParseBaseProps(ctx, m)
  499. binaryLinkerAttrs := bp2buildBinaryLinkerProps(ctx, m)
  500. if proptools.BoolDefault(binaryLinkerAttrs.Linkshared, true) {
  501. baseAttrs.implementationDynamicDeps.Add(baseAttrs.protoDependency)
  502. } else {
  503. baseAttrs.implementationDeps.Add(baseAttrs.protoDependency)
  504. }
  505. // binaries don't have implementation_whole_archive_deps
  506. baseAttrs.wholeArchiveDeps.Append(baseAttrs.implementationWholeArchiveDeps)
  507. attrs := binaryAttributes{
  508. binaryLinkerAttrs: binaryLinkerAttrs,
  509. Srcs: baseAttrs.srcs,
  510. Srcs_c: baseAttrs.cSrcs,
  511. Srcs_as: baseAttrs.asSrcs,
  512. Copts: baseAttrs.copts,
  513. Cppflags: baseAttrs.cppFlags,
  514. Conlyflags: baseAttrs.conlyFlags,
  515. Asflags: baseAttrs.asFlags,
  516. Deps: baseAttrs.implementationDeps,
  517. Dynamic_deps: baseAttrs.implementationDynamicDeps,
  518. Whole_archive_deps: baseAttrs.wholeArchiveDeps,
  519. System_deps: baseAttrs.systemDynamicDeps,
  520. Runtime_deps: baseAttrs.runtimeDeps,
  521. Local_includes: baseAttrs.localIncludes,
  522. Absolute_includes: baseAttrs.absoluteIncludes,
  523. Linkopts: baseAttrs.linkopts,
  524. Use_version_lib: baseAttrs.useVersionLib,
  525. Rtti: baseAttrs.rtti,
  526. Stl: baseAttrs.stl,
  527. Cpp_std: baseAttrs.cppStd,
  528. Additional_linker_inputs: baseAttrs.additionalLinkerInputs,
  529. Strip: stripAttributes{
  530. Keep_symbols: baseAttrs.stripKeepSymbols,
  531. Keep_symbols_and_debug_frame: baseAttrs.stripKeepSymbolsAndDebugFrame,
  532. Keep_symbols_list: baseAttrs.stripKeepSymbolsList,
  533. All: baseAttrs.stripAll,
  534. None: baseAttrs.stripNone,
  535. },
  536. Features: baseAttrs.features,
  537. sdkAttributes: bp2BuildParseSdkAttributes(m),
  538. Native_coverage: baseAttrs.Native_coverage,
  539. }
  540. m.convertTidyAttributes(ctx, &attrs.tidyAttributes)
  541. return attrs
  542. }
  543. func binaryBp2build(ctx android.TopDownMutatorContext, m *Module) {
  544. // shared with cc_test
  545. binaryAttrs := binaryBp2buildAttrs(ctx, m)
  546. tags := android.ApexAvailableTagsWithoutTestApexes(ctx, m)
  547. ctx.CreateBazelTargetModule(bazel.BazelTargetModuleProperties{
  548. Rule_class: "cc_binary",
  549. Bzl_load_location: "//build/bazel/rules/cc:cc_binary.bzl",
  550. },
  551. android.CommonAttributes{Name: m.Name(), Tags: tags},
  552. &binaryAttrs)
  553. }
  554. // binaryAttributes contains Bazel attributes corresponding to a cc binary
  555. type binaryAttributes struct {
  556. binaryLinkerAttrs
  557. Srcs bazel.LabelListAttribute
  558. Srcs_c bazel.LabelListAttribute
  559. Srcs_as bazel.LabelListAttribute
  560. Copts bazel.StringListAttribute
  561. Cppflags bazel.StringListAttribute
  562. Conlyflags bazel.StringListAttribute
  563. Asflags bazel.StringListAttribute
  564. Deps bazel.LabelListAttribute
  565. Dynamic_deps bazel.LabelListAttribute
  566. Whole_archive_deps bazel.LabelListAttribute
  567. System_deps bazel.LabelListAttribute
  568. Runtime_deps bazel.LabelListAttribute
  569. Local_includes bazel.StringListAttribute
  570. Absolute_includes bazel.StringListAttribute
  571. Linkopts bazel.StringListAttribute
  572. Additional_linker_inputs bazel.LabelListAttribute
  573. Use_version_lib bazel.BoolAttribute
  574. Rtti bazel.BoolAttribute
  575. Stl *string
  576. Cpp_std *string
  577. Strip stripAttributes
  578. Features bazel.StringListAttribute
  579. sdkAttributes
  580. tidyAttributes
  581. Native_coverage *bool
  582. }