bazel_handler.go 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. // Copyright 2020 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 android
  15. import (
  16. "bytes"
  17. "errors"
  18. "fmt"
  19. "io/ioutil"
  20. "os"
  21. "os/exec"
  22. "path"
  23. "path/filepath"
  24. "runtime"
  25. "strings"
  26. "sync"
  27. "android/soong/android/allowlists"
  28. "android/soong/bazel/cquery"
  29. "android/soong/shared"
  30. "github.com/google/blueprint"
  31. "android/soong/bazel"
  32. )
  33. var (
  34. writeBazelFile = pctx.AndroidStaticRule("bazelWriteFileRule", blueprint.RuleParams{
  35. Command: `sed "s/\\\\n/\n/g" ${out}.rsp >${out}`,
  36. Rspfile: "${out}.rsp",
  37. RspfileContent: "${content}",
  38. }, "content")
  39. _ = pctx.HostBinToolVariable("bazelBuildRunfilesTool", "build-runfiles")
  40. buildRunfilesRule = pctx.AndroidStaticRule("bazelBuildRunfiles", blueprint.RuleParams{
  41. Command: "${bazelBuildRunfilesTool} ${in} ${outDir}",
  42. Depfile: "",
  43. Description: "",
  44. CommandDeps: []string{"${bazelBuildRunfilesTool}"},
  45. }, "outDir")
  46. )
  47. func init() {
  48. RegisterMixedBuildsMutator(InitRegistrationContext)
  49. }
  50. func RegisterMixedBuildsMutator(ctx RegistrationContext) {
  51. ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) {
  52. ctx.BottomUp("mixed_builds_prep", mixedBuildsPrepareMutator).Parallel()
  53. })
  54. }
  55. func mixedBuildsPrepareMutator(ctx BottomUpMutatorContext) {
  56. if m := ctx.Module(); m.Enabled() {
  57. if mixedBuildMod, ok := m.(MixedBuildBuildable); ok {
  58. if mixedBuildMod.IsMixedBuildSupported(ctx) && MixedBuildsEnabled(ctx) {
  59. mixedBuildMod.QueueBazelCall(ctx)
  60. }
  61. }
  62. }
  63. }
  64. type cqueryRequest interface {
  65. // Name returns a string name for this request type. Such request type names must be unique,
  66. // and must only consist of alphanumeric characters.
  67. Name() string
  68. // StarlarkFunctionBody returns a starlark function body to process this request type.
  69. // The returned string is the body of a Starlark function which obtains
  70. // all request-relevant information about a target and returns a string containing
  71. // this information.
  72. // The function should have the following properties:
  73. // - `target` is the only parameter to this function (a configured target).
  74. // - The return value must be a string.
  75. // - The function body should not be indented outside of its own scope.
  76. StarlarkFunctionBody() string
  77. }
  78. // Portion of cquery map key to describe target configuration.
  79. type configKey struct {
  80. arch string
  81. osType OsType
  82. }
  83. func (c configKey) String() string {
  84. return fmt.Sprintf("%s::%s", c.arch, c.osType)
  85. }
  86. // Map key to describe bazel cquery requests.
  87. type cqueryKey struct {
  88. label string
  89. requestType cqueryRequest
  90. configKey configKey
  91. }
  92. func makeCqueryKey(label string, cqueryRequest cqueryRequest, cfgKey configKey) cqueryKey {
  93. if strings.HasPrefix(label, "//") {
  94. // Normalize Bazel labels to specify main repository explicitly.
  95. label = "@" + label
  96. }
  97. return cqueryKey{label, cqueryRequest, cfgKey}
  98. }
  99. func (c cqueryKey) String() string {
  100. return fmt.Sprintf("cquery(%s,%s,%s)", c.label, c.requestType.Name(), c.configKey)
  101. }
  102. // BazelContext is a context object useful for interacting with Bazel during
  103. // the course of a build. Use of Bazel to evaluate part of the build graph
  104. // is referred to as a "mixed build". (Some modules are managed by Soong,
  105. // some are managed by Bazel). To facilitate interop between these build
  106. // subgraphs, Soong may make requests to Bazel and evaluate their responses
  107. // so that Soong modules may accurately depend on Bazel targets.
  108. type BazelContext interface {
  109. // Add a cquery request to the bazel request queue. All queued requests
  110. // will be sent to Bazel on a subsequent invocation of InvokeBazel.
  111. QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey)
  112. // ** Cquery Results Retrieval Functions
  113. // The below functions pertain to retrieving cquery results from a prior
  114. // InvokeBazel function call and parsing the results.
  115. // Returns result files built by building the given bazel target label.
  116. GetOutputFiles(label string, cfgKey configKey) ([]string, error)
  117. // Returns the results of GetOutputFiles and GetCcObjectFiles in a single query (in that order).
  118. GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error)
  119. // Returns the executable binary resultant from building together the python sources
  120. // TODO(b/232976601): Remove.
  121. GetPythonBinary(label string, cfgKey configKey) (string, error)
  122. // Returns the results of the GetApexInfo query (including output files)
  123. GetApexInfo(label string, cfgkey configKey) (cquery.ApexCqueryInfo, error)
  124. // Returns the results of the GetCcUnstrippedInfo query
  125. GetCcUnstrippedInfo(label string, cfgkey configKey) (cquery.CcUnstrippedInfo, error)
  126. // ** end Cquery Results Retrieval Functions
  127. // Issues commands to Bazel to receive results for all cquery requests
  128. // queued in the BazelContext.
  129. InvokeBazel(config Config) error
  130. // Returns true if Bazel handling is enabled for the module with the given name.
  131. // Note that this only implies "bazel mixed build" allowlisting. The caller
  132. // should independently verify the module is eligible for Bazel handling
  133. // (for example, that it is MixedBuildBuildable).
  134. BazelAllowlisted(moduleName string) bool
  135. // Returns the bazel output base (the root directory for all bazel intermediate outputs).
  136. OutputBase() string
  137. // Returns build statements which should get registered to reflect Bazel's outputs.
  138. BuildStatementsToRegister() []bazel.BuildStatement
  139. // Returns the depsets defined in Bazel's aquery response.
  140. AqueryDepsets() []bazel.AqueryDepset
  141. }
  142. type bazelRunner interface {
  143. createBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand, extraFlags ...string) *exec.Cmd
  144. issueBazelCommand(bazelCmd *exec.Cmd) (output string, errorMessage string, error error)
  145. }
  146. type bazelPaths struct {
  147. homeDir string
  148. bazelPath string
  149. outputBase string
  150. workspaceDir string
  151. soongOutDir string
  152. metricsDir string
  153. bazelDepsFile string
  154. }
  155. // A context object which tracks queued requests that need to be made to Bazel,
  156. // and their results after the requests have been made.
  157. type bazelContext struct {
  158. bazelRunner
  159. paths *bazelPaths
  160. requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel
  161. requestMutex sync.Mutex // requests can be written in parallel
  162. results map[cqueryKey]string // Results of cquery requests after Bazel invocations
  163. // Build statements which should get registered to reflect Bazel's outputs.
  164. buildStatements []bazel.BuildStatement
  165. // Depsets which should be used for Bazel's build statements.
  166. depsets []bazel.AqueryDepset
  167. // Per-module allowlist/denylist functionality to control whether analysis of
  168. // modules are handled by Bazel. For modules which do not have a Bazel definition
  169. // (or do not sufficiently support bazel handling via MixedBuildBuildable),
  170. // this allowlist will have no effect, even if the module is explicitly allowlisted here.
  171. // Per-module denylist to opt modules out of bazel handling.
  172. bazelDisabledModules map[string]bool
  173. // Per-module allowlist to opt modules in to bazel handling.
  174. bazelEnabledModules map[string]bool
  175. // If true, modules are bazel-enabled by default, unless present in bazelDisabledModules.
  176. modulesDefaultToBazel bool
  177. }
  178. var _ BazelContext = &bazelContext{}
  179. // A bazel context to use when Bazel is disabled.
  180. type noopBazelContext struct{}
  181. var _ BazelContext = noopBazelContext{}
  182. // A bazel context to use for tests.
  183. type MockBazelContext struct {
  184. OutputBaseDir string
  185. LabelToOutputFiles map[string][]string
  186. LabelToCcInfo map[string]cquery.CcInfo
  187. LabelToPythonBinary map[string]string
  188. LabelToApexInfo map[string]cquery.ApexCqueryInfo
  189. LabelToCcBinary map[string]cquery.CcUnstrippedInfo
  190. }
  191. func (m MockBazelContext) QueueBazelRequest(_ string, _ cqueryRequest, _ configKey) {
  192. panic("unimplemented")
  193. }
  194. func (m MockBazelContext) GetOutputFiles(label string, _ configKey) ([]string, error) {
  195. result, _ := m.LabelToOutputFiles[label]
  196. return result, nil
  197. }
  198. func (m MockBazelContext) GetCcInfo(label string, _ configKey) (cquery.CcInfo, error) {
  199. result, _ := m.LabelToCcInfo[label]
  200. return result, nil
  201. }
  202. func (m MockBazelContext) GetPythonBinary(label string, _ configKey) (string, error) {
  203. result, _ := m.LabelToPythonBinary[label]
  204. return result, nil
  205. }
  206. func (n MockBazelContext) GetApexInfo(_ string, _ configKey) (cquery.ApexCqueryInfo, error) {
  207. panic("unimplemented")
  208. }
  209. func (m MockBazelContext) GetCcUnstrippedInfo(label string, _ configKey) (cquery.CcUnstrippedInfo, error) {
  210. result, _ := m.LabelToCcBinary[label]
  211. return result, nil
  212. }
  213. func (m MockBazelContext) InvokeBazel(_ Config) error {
  214. panic("unimplemented")
  215. }
  216. func (m MockBazelContext) BazelAllowlisted(moduleName string) bool {
  217. return true
  218. }
  219. func (m MockBazelContext) OutputBase() string { return m.OutputBaseDir }
  220. func (m MockBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
  221. return []bazel.BuildStatement{}
  222. }
  223. func (m MockBazelContext) AqueryDepsets() []bazel.AqueryDepset {
  224. return []bazel.AqueryDepset{}
  225. }
  226. var _ BazelContext = MockBazelContext{}
  227. func (bazelCtx *bazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) {
  228. key := makeCqueryKey(label, requestType, cfgKey)
  229. bazelCtx.requestMutex.Lock()
  230. defer bazelCtx.requestMutex.Unlock()
  231. bazelCtx.requests[key] = true
  232. }
  233. func (bazelCtx *bazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) {
  234. key := makeCqueryKey(label, cquery.GetOutputFiles, cfgKey)
  235. if rawString, ok := bazelCtx.results[key]; ok {
  236. bazelOutput := strings.TrimSpace(rawString)
  237. return cquery.GetOutputFiles.ParseResult(bazelOutput), nil
  238. }
  239. return nil, fmt.Errorf("no bazel response found for %v", key)
  240. }
  241. func (bazelCtx *bazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) {
  242. key := makeCqueryKey(label, cquery.GetCcInfo, cfgKey)
  243. if rawString, ok := bazelCtx.results[key]; ok {
  244. bazelOutput := strings.TrimSpace(rawString)
  245. return cquery.GetCcInfo.ParseResult(bazelOutput)
  246. }
  247. return cquery.CcInfo{}, fmt.Errorf("no bazel response found for %v", key)
  248. }
  249. func (bazelCtx *bazelContext) GetPythonBinary(label string, cfgKey configKey) (string, error) {
  250. key := makeCqueryKey(label, cquery.GetPythonBinary, cfgKey)
  251. if rawString, ok := bazelCtx.results[key]; ok {
  252. bazelOutput := strings.TrimSpace(rawString)
  253. return cquery.GetPythonBinary.ParseResult(bazelOutput), nil
  254. }
  255. return "", fmt.Errorf("no bazel response found for %v", key)
  256. }
  257. func (bazelCtx *bazelContext) GetApexInfo(label string, cfgKey configKey) (cquery.ApexCqueryInfo, error) {
  258. key := makeCqueryKey(label, cquery.GetApexInfo, cfgKey)
  259. if rawString, ok := bazelCtx.results[key]; ok {
  260. return cquery.GetApexInfo.ParseResult(strings.TrimSpace(rawString)), nil
  261. }
  262. return cquery.ApexCqueryInfo{}, fmt.Errorf("no bazel response found for %v", key)
  263. }
  264. func (bazelCtx *bazelContext) GetCcUnstrippedInfo(label string, cfgKey configKey) (cquery.CcUnstrippedInfo, error) {
  265. key := makeCqueryKey(label, cquery.GetCcUnstrippedInfo, cfgKey)
  266. if rawString, ok := bazelCtx.results[key]; ok {
  267. return cquery.GetCcUnstrippedInfo.ParseResult(strings.TrimSpace(rawString)), nil
  268. }
  269. return cquery.CcUnstrippedInfo{}, fmt.Errorf("no bazel response for %s", key)
  270. }
  271. func (n noopBazelContext) QueueBazelRequest(_ string, _ cqueryRequest, _ configKey) {
  272. panic("unimplemented")
  273. }
  274. func (n noopBazelContext) GetOutputFiles(_ string, _ configKey) ([]string, error) {
  275. panic("unimplemented")
  276. }
  277. func (n noopBazelContext) GetCcInfo(_ string, _ configKey) (cquery.CcInfo, error) {
  278. panic("unimplemented")
  279. }
  280. func (n noopBazelContext) GetPythonBinary(_ string, _ configKey) (string, error) {
  281. panic("unimplemented")
  282. }
  283. func (n noopBazelContext) GetApexInfo(_ string, _ configKey) (cquery.ApexCqueryInfo, error) {
  284. panic("unimplemented")
  285. }
  286. func (n noopBazelContext) GetCcUnstrippedInfo(_ string, _ configKey) (cquery.CcUnstrippedInfo, error) {
  287. //TODO implement me
  288. panic("implement me")
  289. }
  290. func (n noopBazelContext) InvokeBazel(_ Config) error {
  291. panic("unimplemented")
  292. }
  293. func (m noopBazelContext) OutputBase() string {
  294. return ""
  295. }
  296. func (n noopBazelContext) BazelAllowlisted(moduleName string) bool {
  297. return false
  298. }
  299. func (m noopBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
  300. return []bazel.BuildStatement{}
  301. }
  302. func (m noopBazelContext) AqueryDepsets() []bazel.AqueryDepset {
  303. return []bazel.AqueryDepset{}
  304. }
  305. func NewBazelContext(c *config) (BazelContext, error) {
  306. var modulesDefaultToBazel bool
  307. disabledModules := map[string]bool{}
  308. enabledModules := map[string]bool{}
  309. switch c.BuildMode {
  310. case BazelProdMode:
  311. modulesDefaultToBazel = false
  312. for _, enabledProdModule := range allowlists.ProdMixedBuildsEnabledList {
  313. enabledModules[enabledProdModule] = true
  314. }
  315. case BazelStagingMode:
  316. modulesDefaultToBazel = false
  317. for _, enabledStagingMode := range allowlists.StagingMixedBuildsEnabledList {
  318. enabledModules[enabledStagingMode] = true
  319. }
  320. case BazelDevMode:
  321. modulesDefaultToBazel = true
  322. // Don't use partially-converted cc_library targets in mixed builds,
  323. // since mixed builds would generally rely on both static and shared
  324. // variants of a cc_library.
  325. for staticOnlyModule, _ := range GetBp2BuildAllowList().ccLibraryStaticOnly {
  326. disabledModules[staticOnlyModule] = true
  327. }
  328. for _, disabledDevModule := range allowlists.MixedBuildsDisabledList {
  329. disabledModules[disabledDevModule] = true
  330. }
  331. default:
  332. return noopBazelContext{}, nil
  333. }
  334. p, err := bazelPathsFromConfig(c)
  335. if err != nil {
  336. return nil, err
  337. }
  338. return &bazelContext{
  339. bazelRunner: &builtinBazelRunner{},
  340. paths: p,
  341. requests: make(map[cqueryKey]bool),
  342. modulesDefaultToBazel: modulesDefaultToBazel,
  343. bazelEnabledModules: enabledModules,
  344. bazelDisabledModules: disabledModules,
  345. }, nil
  346. }
  347. func bazelPathsFromConfig(c *config) (*bazelPaths, error) {
  348. p := bazelPaths{
  349. soongOutDir: c.soongOutDir,
  350. }
  351. var missingEnvVars []string
  352. if len(c.Getenv("BAZEL_HOME")) > 1 {
  353. p.homeDir = c.Getenv("BAZEL_HOME")
  354. } else {
  355. missingEnvVars = append(missingEnvVars, "BAZEL_HOME")
  356. }
  357. if len(c.Getenv("BAZEL_PATH")) > 1 {
  358. p.bazelPath = c.Getenv("BAZEL_PATH")
  359. } else {
  360. missingEnvVars = append(missingEnvVars, "BAZEL_PATH")
  361. }
  362. if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 {
  363. p.outputBase = c.Getenv("BAZEL_OUTPUT_BASE")
  364. } else {
  365. missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE")
  366. }
  367. if len(c.Getenv("BAZEL_WORKSPACE")) > 1 {
  368. p.workspaceDir = c.Getenv("BAZEL_WORKSPACE")
  369. } else {
  370. missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE")
  371. }
  372. if len(c.Getenv("BAZEL_METRICS_DIR")) > 1 {
  373. p.metricsDir = c.Getenv("BAZEL_METRICS_DIR")
  374. } else {
  375. missingEnvVars = append(missingEnvVars, "BAZEL_METRICS_DIR")
  376. }
  377. if len(c.Getenv("BAZEL_DEPS_FILE")) > 1 {
  378. p.bazelDepsFile = c.Getenv("BAZEL_DEPS_FILE")
  379. } else {
  380. missingEnvVars = append(missingEnvVars, "BAZEL_DEPS_FILE")
  381. }
  382. if len(missingEnvVars) > 0 {
  383. return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars))
  384. } else {
  385. return &p, nil
  386. }
  387. }
  388. func (p *bazelPaths) BazelMetricsDir() string {
  389. return p.metricsDir
  390. }
  391. func (context *bazelContext) BazelAllowlisted(moduleName string) bool {
  392. if context.bazelDisabledModules[moduleName] {
  393. return false
  394. }
  395. if context.bazelEnabledModules[moduleName] {
  396. return true
  397. }
  398. return context.modulesDefaultToBazel
  399. }
  400. func pwdPrefix() string {
  401. // Darwin doesn't have /proc
  402. if runtime.GOOS != "darwin" {
  403. return "PWD=/proc/self/cwd"
  404. }
  405. return ""
  406. }
  407. type bazelCommand struct {
  408. command string
  409. // query or label
  410. expression string
  411. }
  412. type mockBazelRunner struct {
  413. bazelCommandResults map[bazelCommand]string
  414. // use *exec.Cmd as a key to get the bazelCommand, the map will be used in issueBazelCommand()
  415. // Register createBazelCommand() invocations. Later, an
  416. // issueBazelCommand() invocation can be mapped to the *exec.Cmd instance
  417. // and then to the expected result via bazelCommandResults
  418. tokens map[*exec.Cmd]bazelCommand
  419. commands []bazelCommand
  420. extraFlags []string
  421. }
  422. func (r *mockBazelRunner) createBazelCommand(paths *bazelPaths, runName bazel.RunName,
  423. command bazelCommand, extraFlags ...string) *exec.Cmd {
  424. r.commands = append(r.commands, command)
  425. r.extraFlags = append(r.extraFlags, strings.Join(extraFlags, " "))
  426. cmd := &exec.Cmd{}
  427. if r.tokens == nil {
  428. r.tokens = make(map[*exec.Cmd]bazelCommand)
  429. }
  430. r.tokens[cmd] = command
  431. return cmd
  432. }
  433. func (r *mockBazelRunner) issueBazelCommand(bazelCmd *exec.Cmd) (string, string, error) {
  434. if command, ok := r.tokens[bazelCmd]; ok {
  435. return r.bazelCommandResults[command], "", nil
  436. }
  437. return "", "", nil
  438. }
  439. type builtinBazelRunner struct{}
  440. // Issues the given bazel command with given build label and additional flags.
  441. // Returns (stdout, stderr, error). The first and second return values are strings
  442. // containing the stdout and stderr of the run command, and an error is returned if
  443. // the invocation returned an error code.
  444. func (r *builtinBazelRunner) issueBazelCommand(bazelCmd *exec.Cmd) (string, string, error) {
  445. stderr := &bytes.Buffer{}
  446. bazelCmd.Stderr = stderr
  447. if output, err := bazelCmd.Output(); err != nil {
  448. return "", string(stderr.Bytes()),
  449. fmt.Errorf("bazel command failed. command: [%s], env: [%s], error [%s]", bazelCmd, bazelCmd.Env, stderr)
  450. } else {
  451. return string(output), string(stderr.Bytes()), nil
  452. }
  453. }
  454. func (r *builtinBazelRunner) createBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand,
  455. extraFlags ...string) *exec.Cmd {
  456. cmdFlags := []string{
  457. "--output_base=" + absolutePath(paths.outputBase),
  458. command.command,
  459. command.expression,
  460. // TODO(asmundak): is it needed in every build?
  461. "--profile=" + shared.BazelMetricsFilename(paths, runName),
  462. // Set default platforms to canonicalized values for mixed builds requests.
  463. // If these are set in the bazelrc, they will have values that are
  464. // non-canonicalized to @sourceroot labels, and thus be invalid when
  465. // referenced from the buildroot.
  466. //
  467. // The actual platform values here may be overridden by configuration
  468. // transitions from the buildroot.
  469. fmt.Sprintf("--extra_toolchains=%s", "//prebuilts/clang/host/linux-x86:all"),
  470. // This should be parameterized on the host OS, but let's restrict to linux
  471. // to keep things simple for now.
  472. fmt.Sprintf("--host_platform=%s", "//build/bazel/platforms:linux_x86_64"),
  473. // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network.
  474. "--experimental_repository_disable_download",
  475. // Suppress noise
  476. "--ui_event_filters=-INFO",
  477. "--noshow_progress"}
  478. cmdFlags = append(cmdFlags, extraFlags...)
  479. bazelCmd := exec.Command(paths.bazelPath, cmdFlags...)
  480. bazelCmd.Dir = absolutePath(paths.syntheticWorkspaceDir())
  481. extraEnv := []string{
  482. "HOME=" + paths.homeDir,
  483. pwdPrefix(),
  484. "BUILD_DIR=" + absolutePath(paths.soongOutDir),
  485. // Make OUT_DIR absolute here so build/bazel/bin/bazel uses the correct
  486. // OUT_DIR at <root>/out, instead of <root>/out/soong/workspace/out.
  487. "OUT_DIR=" + absolutePath(paths.outDir()),
  488. // Disables local host detection of gcc; toolchain information is defined
  489. // explicitly in BUILD files.
  490. "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1",
  491. }
  492. bazelCmd.Env = append(os.Environ(), extraEnv...)
  493. return bazelCmd
  494. }
  495. func printableCqueryCommand(bazelCmd *exec.Cmd) string {
  496. outputString := strings.Join(bazelCmd.Env, " ") + " \"" + strings.Join(bazelCmd.Args, "\" \"") + "\""
  497. return outputString
  498. }
  499. func (context *bazelContext) mainBzlFileContents() []byte {
  500. // TODO(cparsons): Define configuration transitions programmatically based
  501. // on available archs.
  502. contents := `
  503. #####################################################
  504. # This file is generated by soong_build. Do not edit.
  505. #####################################################
  506. def _config_node_transition_impl(settings, attr):
  507. return {
  508. "//command_line_option:platforms": "@//build/bazel/platforms:%s_%s" % (attr.os, attr.arch),
  509. }
  510. _config_node_transition = transition(
  511. implementation = _config_node_transition_impl,
  512. inputs = [],
  513. outputs = [
  514. "//command_line_option:platforms",
  515. ],
  516. )
  517. def _passthrough_rule_impl(ctx):
  518. return [DefaultInfo(files = depset(ctx.files.deps))]
  519. config_node = rule(
  520. implementation = _passthrough_rule_impl,
  521. attrs = {
  522. "arch" : attr.string(mandatory = True),
  523. "os" : attr.string(mandatory = True),
  524. "deps" : attr.label_list(cfg = _config_node_transition, allow_files = True),
  525. "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
  526. },
  527. )
  528. # Rule representing the root of the build, to depend on all Bazel targets that
  529. # are required for the build. Building this target will build the entire Bazel
  530. # build tree.
  531. mixed_build_root = rule(
  532. implementation = _passthrough_rule_impl,
  533. attrs = {
  534. "deps" : attr.label_list(),
  535. },
  536. )
  537. def _phony_root_impl(ctx):
  538. return []
  539. # Rule to depend on other targets but build nothing.
  540. # This is useful as follows: building a target of this rule will generate
  541. # symlink forests for all dependencies of the target, without executing any
  542. # actions of the build.
  543. phony_root = rule(
  544. implementation = _phony_root_impl,
  545. attrs = {"deps" : attr.label_list()},
  546. )
  547. `
  548. return []byte(contents)
  549. }
  550. func (context *bazelContext) mainBuildFileContents() []byte {
  551. // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded
  552. // architecture mapping.
  553. formatString := `
  554. # This file is generated by soong_build. Do not edit.
  555. load(":main.bzl", "config_node", "mixed_build_root", "phony_root")
  556. %s
  557. mixed_build_root(name = "buildroot",
  558. deps = [%s],
  559. )
  560. phony_root(name = "phonyroot",
  561. deps = [":buildroot"],
  562. )
  563. `
  564. configNodeFormatString := `
  565. config_node(name = "%s",
  566. arch = "%s",
  567. os = "%s",
  568. deps = [%s],
  569. )
  570. `
  571. configNodesSection := ""
  572. labelsByConfig := map[string][]string{}
  573. for val := range context.requests {
  574. labelString := fmt.Sprintf("\"@%s\"", val.label)
  575. configString := getConfigString(val)
  576. labelsByConfig[configString] = append(labelsByConfig[configString], labelString)
  577. }
  578. allLabels := []string{}
  579. for configString, labels := range labelsByConfig {
  580. configTokens := strings.Split(configString, "|")
  581. if len(configTokens) != 2 {
  582. panic(fmt.Errorf("Unexpected config string format: %s", configString))
  583. }
  584. archString := configTokens[0]
  585. osString := configTokens[1]
  586. targetString := fmt.Sprintf("%s_%s", osString, archString)
  587. allLabels = append(allLabels, fmt.Sprintf("\":%s\"", targetString))
  588. labelsString := strings.Join(labels, ",\n ")
  589. configNodesSection += fmt.Sprintf(configNodeFormatString, targetString, archString, osString, labelsString)
  590. }
  591. return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(allLabels, ",\n ")))
  592. }
  593. func indent(original string) string {
  594. result := ""
  595. for _, line := range strings.Split(original, "\n") {
  596. result += " " + line + "\n"
  597. }
  598. return result
  599. }
  600. // Returns the file contents of the buildroot.cquery file that should be used for the cquery
  601. // expression in order to obtain information about buildroot and its dependencies.
  602. // The contents of this file depend on the bazelContext's requests; requests are enumerated
  603. // and grouped by their request type. The data retrieved for each label depends on its
  604. // request type.
  605. func (context *bazelContext) cqueryStarlarkFileContents() []byte {
  606. requestTypeToCqueryIdEntries := map[cqueryRequest][]string{}
  607. for val := range context.requests {
  608. cqueryId := getCqueryId(val)
  609. mapEntryString := fmt.Sprintf("%q : True", cqueryId)
  610. requestTypeToCqueryIdEntries[val.requestType] =
  611. append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString)
  612. }
  613. labelRegistrationMapSection := ""
  614. functionDefSection := ""
  615. mainSwitchSection := ""
  616. mapDeclarationFormatString := `
  617. %s = {
  618. %s
  619. }
  620. `
  621. functionDefFormatString := `
  622. def %s(target):
  623. %s
  624. `
  625. mainSwitchSectionFormatString := `
  626. if id_string in %s:
  627. return id_string + ">>" + %s(target)
  628. `
  629. for requestType := range requestTypeToCqueryIdEntries {
  630. labelMapName := requestType.Name() + "_Labels"
  631. functionName := requestType.Name() + "_Fn"
  632. labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString,
  633. labelMapName,
  634. strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n "))
  635. functionDefSection += fmt.Sprintf(functionDefFormatString,
  636. functionName,
  637. indent(requestType.StarlarkFunctionBody()))
  638. mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString,
  639. labelMapName, functionName)
  640. }
  641. formatString := `
  642. # This file is generated by soong_build. Do not edit.
  643. # a drop-in replacement for json.encode(), not available in cquery environment
  644. # TODO(cparsons): bring json module in and remove this function
  645. def json_encode(input):
  646. # Avoiding recursion by limiting
  647. # - a dict to contain anything except a dict
  648. # - a list to contain only primitives
  649. def encode_primitive(p):
  650. t = type(p)
  651. if t == "string" or t == "int":
  652. return repr(p)
  653. fail("unsupported value '%%s' of type '%%s'" %% (p, type(p)))
  654. def encode_list(list):
  655. return "[%%s]" %% ", ".join([encode_primitive(item) for item in list])
  656. def encode_list_or_primitive(v):
  657. return encode_list(v) if type(v) == "list" else encode_primitive(v)
  658. if type(input) == "dict":
  659. # TODO(juu): the result is read line by line so can't use '\n' yet
  660. kv_pairs = [("%%s: %%s" %% (encode_primitive(k), encode_list_or_primitive(v))) for (k, v) in input.items()]
  661. return "{ %%s }" %% ", ".join(kv_pairs)
  662. else:
  663. return encode_list_or_primitive(input)
  664. # Label Map Section
  665. %s
  666. # Function Def Section
  667. %s
  668. def get_arch(target):
  669. # TODO(b/199363072): filegroups and file targets aren't associated with any
  670. # specific platform architecture in mixed builds. This is consistent with how
  671. # Soong treats filegroups, but it may not be the case with manually-written
  672. # filegroup BUILD targets.
  673. buildoptions = build_options(target)
  674. if buildoptions == None:
  675. # File targets do not have buildoptions. File targets aren't associated with
  676. # any specific platform architecture in mixed builds, so use the host.
  677. return "x86_64|linux"
  678. platforms = build_options(target)["//command_line_option:platforms"]
  679. if len(platforms) != 1:
  680. # An individual configured target should have only one platform architecture.
  681. # Note that it's fine for there to be multiple architectures for the same label,
  682. # but each is its own configured target.
  683. fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms))
  684. platform_name = build_options(target)["//command_line_option:platforms"][0].name
  685. if platform_name == "host":
  686. return "HOST"
  687. elif platform_name.startswith("android_"):
  688. return platform_name[len("android_"):] + "|" + platform_name[:len("android_")-1]
  689. elif platform_name.startswith("linux_"):
  690. return platform_name[len("linux_"):] + "|" + platform_name[:len("linux_")-1]
  691. else:
  692. fail("expected platform name of the form 'android_<arch>' or 'linux_<arch>', but was " + str(platforms))
  693. return "UNKNOWN"
  694. def format(target):
  695. id_string = str(target.label) + "|" + get_arch(target)
  696. # TODO(b/248106697): Remove once Bazel is updated to always normalize labels.
  697. if id_string.startswith("//"):
  698. id_string = "@" + id_string
  699. # Main switch section
  700. %s
  701. # This target was not requested via cquery, and thus must be a dependency
  702. # of a requested target.
  703. return id_string + ">>NONE"
  704. `
  705. return []byte(fmt.Sprintf(formatString, labelRegistrationMapSection, functionDefSection,
  706. mainSwitchSection))
  707. }
  708. // Returns a path containing build-related metadata required for interfacing
  709. // with Bazel. Example: out/soong/bazel.
  710. func (p *bazelPaths) intermediatesDir() string {
  711. return filepath.Join(p.soongOutDir, "bazel")
  712. }
  713. // Returns the path where the contents of the @soong_injection repository live.
  714. // It is used by Soong to tell Bazel things it cannot over the command line.
  715. func (p *bazelPaths) injectedFilesDir() string {
  716. return filepath.Join(p.soongOutDir, bazel.SoongInjectionDirName)
  717. }
  718. // Returns the path of the synthetic Bazel workspace that contains a symlink
  719. // forest composed the whole source tree and BUILD files generated by bp2build.
  720. func (p *bazelPaths) syntheticWorkspaceDir() string {
  721. return filepath.Join(p.soongOutDir, "workspace")
  722. }
  723. // Returns the path to the top level out dir ($OUT_DIR).
  724. func (p *bazelPaths) outDir() string {
  725. return filepath.Dir(p.soongOutDir)
  726. }
  727. // Issues commands to Bazel to receive results for all cquery requests
  728. // queued in the BazelContext.
  729. func (context *bazelContext) InvokeBazel(config Config) error {
  730. context.results = make(map[cqueryKey]string)
  731. var err error
  732. soongInjectionPath := absolutePath(context.paths.injectedFilesDir())
  733. mixedBuildsPath := filepath.Join(soongInjectionPath, "mixed_builds")
  734. if _, err := os.Stat(mixedBuildsPath); os.IsNotExist(err) {
  735. err = os.MkdirAll(mixedBuildsPath, 0777)
  736. }
  737. if err != nil {
  738. return err
  739. }
  740. if metricsDir := context.paths.BazelMetricsDir(); metricsDir != "" {
  741. err = os.MkdirAll(metricsDir, 0777)
  742. if err != nil {
  743. return err
  744. }
  745. }
  746. if err = ioutil.WriteFile(filepath.Join(soongInjectionPath, "WORKSPACE.bazel"), []byte{}, 0666); err != nil {
  747. return err
  748. }
  749. if err = ioutil.WriteFile(filepath.Join(mixedBuildsPath, "main.bzl"), context.mainBzlFileContents(), 0666); err != nil {
  750. return err
  751. }
  752. if err = ioutil.WriteFile(filepath.Join(mixedBuildsPath, "BUILD.bazel"), context.mainBuildFileContents(), 0666); err != nil {
  753. return err
  754. }
  755. cqueryFileRelpath := filepath.Join(context.paths.injectedFilesDir(), "buildroot.cquery")
  756. if err = ioutil.WriteFile(absolutePath(cqueryFileRelpath), context.cqueryStarlarkFileContents(), 0666); err != nil {
  757. return err
  758. }
  759. const buildrootLabel = "@soong_injection//mixed_builds:buildroot"
  760. cqueryCmd := bazelCommand{"cquery", fmt.Sprintf("deps(%s, 2)", buildrootLabel)}
  761. cqueryCommandWithFlag := context.createBazelCommand(context.paths, bazel.CqueryBuildRootRunName, cqueryCmd,
  762. "--output=starlark", "--starlark:file="+absolutePath(cqueryFileRelpath))
  763. cqueryOutput, cqueryErr, err := context.issueBazelCommand(cqueryCommandWithFlag)
  764. if err != nil {
  765. return err
  766. }
  767. cqueryCommandPrint := fmt.Sprintf("cquery command line:\n %s \n\n\n", printableCqueryCommand(cqueryCommandWithFlag))
  768. if err = ioutil.WriteFile(filepath.Join(soongInjectionPath, "cquery.out"), []byte(cqueryCommandPrint+cqueryOutput), 0666); err != nil {
  769. return err
  770. }
  771. cqueryResults := map[string]string{}
  772. for _, outputLine := range strings.Split(cqueryOutput, "\n") {
  773. if strings.Contains(outputLine, ">>") {
  774. splitLine := strings.SplitN(outputLine, ">>", 2)
  775. cqueryResults[splitLine[0]] = splitLine[1]
  776. }
  777. }
  778. for val := range context.requests {
  779. if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok {
  780. context.results[val] = cqueryResult
  781. } else {
  782. return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]",
  783. getCqueryId(val), cqueryOutput, cqueryErr)
  784. }
  785. }
  786. // Issue an aquery command to retrieve action information about the bazel build tree.
  787. //
  788. // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's
  789. // proto sources, which would add a number of unnecessary dependencies.
  790. extraFlags := []string{"--output=jsonproto", "--include_file_write_contents"}
  791. if Bool(config.productVariables.ClangCoverage) {
  792. extraFlags = append(extraFlags, "--collect_code_coverage")
  793. paths := make([]string, 0, 2)
  794. if p := config.productVariables.NativeCoveragePaths; len(p) > 0 {
  795. paths = append(paths, JoinWithPrefixAndSeparator(p, "+", ","))
  796. }
  797. if p := config.productVariables.NativeCoverageExcludePaths; len(p) > 0 {
  798. paths = append(paths, JoinWithPrefixAndSeparator(p, "-", ","))
  799. }
  800. if len(paths) > 0 {
  801. extraFlags = append(extraFlags, "--instrumentation_filter="+strings.Join(paths, ","))
  802. }
  803. }
  804. aqueryCmd := bazelCommand{"aquery", fmt.Sprintf("deps(%s)", buildrootLabel)}
  805. if aqueryOutput, _, err := context.issueBazelCommand(context.createBazelCommand(context.paths, bazel.AqueryBuildRootRunName, aqueryCmd,
  806. extraFlags...)); err == nil {
  807. context.buildStatements, context.depsets, err = bazel.AqueryBuildStatements([]byte(aqueryOutput))
  808. }
  809. if err != nil {
  810. return err
  811. }
  812. // Issue a build command of the phony root to generate symlink forests for dependencies of the
  813. // Bazel build. This is necessary because aquery invocations do not generate this symlink forest,
  814. // but some of symlinks may be required to resolve source dependencies of the build.
  815. buildCmd := bazelCommand{"build", "@soong_injection//mixed_builds:phonyroot"}
  816. if _, _, err = context.issueBazelCommand(context.createBazelCommand(context.paths, bazel.BazelBuildPhonyRootRunName, buildCmd)); err != nil {
  817. return err
  818. }
  819. // Clear requests.
  820. context.requests = map[cqueryKey]bool{}
  821. return nil
  822. }
  823. func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
  824. return context.buildStatements
  825. }
  826. func (context *bazelContext) AqueryDepsets() []bazel.AqueryDepset {
  827. return context.depsets
  828. }
  829. func (context *bazelContext) OutputBase() string {
  830. return context.paths.outputBase
  831. }
  832. // Singleton used for registering BUILD file ninja dependencies (needed
  833. // for correctness of builds which use Bazel.
  834. func BazelSingleton() Singleton {
  835. return &bazelSingleton{}
  836. }
  837. type bazelSingleton struct{}
  838. func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
  839. // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled.
  840. if !ctx.Config().IsMixedBuildsEnabled() {
  841. return
  842. }
  843. // Add ninja file dependencies for files which all bazel invocations require.
  844. bazelBuildList := absolutePath(filepath.Join(
  845. filepath.Dir(ctx.Config().moduleListFile), "bazel.list"))
  846. ctx.AddNinjaFileDeps(bazelBuildList)
  847. data, err := ioutil.ReadFile(bazelBuildList)
  848. if err != nil {
  849. ctx.Errorf(err.Error())
  850. }
  851. files := strings.Split(strings.TrimSpace(string(data)), "\n")
  852. for _, file := range files {
  853. ctx.AddNinjaFileDeps(file)
  854. }
  855. for _, depset := range ctx.Config().BazelContext.AqueryDepsets() {
  856. var outputs []Path
  857. for _, depsetDepHash := range depset.TransitiveDepSetHashes {
  858. otherDepsetName := bazelDepsetName(depsetDepHash)
  859. outputs = append(outputs, PathForPhony(ctx, otherDepsetName))
  860. }
  861. for _, artifactPath := range depset.DirectArtifacts {
  862. outputs = append(outputs, PathForBazelOut(ctx, artifactPath))
  863. }
  864. thisDepsetName := bazelDepsetName(depset.ContentHash)
  865. ctx.Build(pctx, BuildParams{
  866. Rule: blueprint.Phony,
  867. Outputs: []WritablePath{PathForPhony(ctx, thisDepsetName)},
  868. Implicits: outputs,
  869. })
  870. }
  871. executionRoot := path.Join(ctx.Config().BazelContext.OutputBase(), "execroot", "__main__")
  872. bazelOutDir := path.Join(executionRoot, "bazel-out")
  873. for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() {
  874. if len(buildStatement.Command) > 0 {
  875. rule := NewRuleBuilder(pctx, ctx)
  876. createCommand(rule.Command(), buildStatement, executionRoot, bazelOutDir, ctx)
  877. desc := fmt.Sprintf("%s: %s", buildStatement.Mnemonic, buildStatement.OutputPaths)
  878. rule.Build(fmt.Sprintf("bazel %d", index), desc)
  879. continue
  880. }
  881. // Certain actions returned by aquery (for instance FileWrite) do not contain a command
  882. // and thus require special treatment. If BuildStatement were an interface implementing
  883. // buildRule(ctx) function, the code here would just call it.
  884. // Unfortunately, the BuildStatement is defined in
  885. // the 'bazel' package, which cannot depend on 'android' package where ctx is defined,
  886. // because this would cause circular dependency. So, until we move aquery processing
  887. // to the 'android' package, we need to handle special cases here.
  888. if buildStatement.Mnemonic == "FileWrite" || buildStatement.Mnemonic == "SourceSymlinkManifest" {
  889. // Pass file contents as the value of the rule's "content" argument.
  890. // Escape newlines and $ in the contents (the action "writeBazelFile" restores "\\n"
  891. // back to the newline, and Ninja reads $$ as $.
  892. escaped := strings.ReplaceAll(strings.ReplaceAll(buildStatement.FileContents, "\n", "\\n"),
  893. "$", "$$")
  894. ctx.Build(pctx, BuildParams{
  895. Rule: writeBazelFile,
  896. Output: PathForBazelOut(ctx, buildStatement.OutputPaths[0]),
  897. Description: fmt.Sprintf("%s %s", buildStatement.Mnemonic, buildStatement.OutputPaths[0]),
  898. Args: map[string]string{
  899. "content": escaped,
  900. },
  901. })
  902. } else if buildStatement.Mnemonic == "SymlinkTree" {
  903. // build-runfiles arguments are the manifest file and the target directory
  904. // where it creates the symlink tree according to this manifest (and then
  905. // writes the MANIFEST file to it).
  906. outManifest := PathForBazelOut(ctx, buildStatement.OutputPaths[0])
  907. outManifestPath := outManifest.String()
  908. if !strings.HasSuffix(outManifestPath, "MANIFEST") {
  909. panic("the base name of the symlink tree action should be MANIFEST, got " + outManifestPath)
  910. }
  911. outDir := filepath.Dir(outManifestPath)
  912. ctx.Build(pctx, BuildParams{
  913. Rule: buildRunfilesRule,
  914. Output: outManifest,
  915. Inputs: []Path{PathForBazelOut(ctx, buildStatement.InputPaths[0])},
  916. Description: "symlink tree for " + outDir,
  917. Args: map[string]string{
  918. "outDir": outDir,
  919. },
  920. })
  921. } else {
  922. panic(fmt.Sprintf("unhandled build statement: %v", buildStatement))
  923. }
  924. }
  925. }
  926. // Register bazel-owned build statements (obtained from the aquery invocation).
  927. func createCommand(cmd *RuleBuilderCommand, buildStatement bazel.BuildStatement, executionRoot string, bazelOutDir string, ctx PathContext) {
  928. // executionRoot is the action cwd.
  929. cmd.Text(fmt.Sprintf("cd '%s' &&", executionRoot))
  930. // Remove old outputs, as some actions might not rerun if the outputs are detected.
  931. if len(buildStatement.OutputPaths) > 0 {
  932. cmd.Text("rm -f")
  933. for _, outputPath := range buildStatement.OutputPaths {
  934. cmd.Text(fmt.Sprintf("'%s'", outputPath))
  935. }
  936. cmd.Text("&&")
  937. }
  938. for _, pair := range buildStatement.Env {
  939. // Set per-action env variables, if any.
  940. cmd.Flag(pair.Key + "=" + pair.Value)
  941. }
  942. // The actual Bazel action.
  943. cmd.Text(buildStatement.Command)
  944. for _, outputPath := range buildStatement.OutputPaths {
  945. cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath))
  946. }
  947. for _, inputPath := range buildStatement.InputPaths {
  948. cmd.Implicit(PathForBazelOut(ctx, inputPath))
  949. }
  950. for _, inputDepsetHash := range buildStatement.InputDepsetHashes {
  951. otherDepsetName := bazelDepsetName(inputDepsetHash)
  952. cmd.Implicit(PathForPhony(ctx, otherDepsetName))
  953. }
  954. if depfile := buildStatement.Depfile; depfile != nil {
  955. // The paths in depfile are relative to `executionRoot`.
  956. // Hence, they need to be corrected by replacing "bazel-out"
  957. // with the full `bazelOutDir`.
  958. // Otherwise, implicit outputs and implicit inputs under "bazel-out/"
  959. // would be deemed missing.
  960. // (Note: The regexp uses a capture group because the version of sed
  961. // does not support a look-behind pattern.)
  962. replacement := fmt.Sprintf(`&& sed -i'' -E 's@(^|\s|")bazel-out/@\1%s/@g' '%s'`,
  963. bazelOutDir, *depfile)
  964. cmd.Text(replacement)
  965. cmd.ImplicitDepFile(PathForBazelOut(ctx, *depfile))
  966. }
  967. for _, symlinkPath := range buildStatement.SymlinkPaths {
  968. cmd.ImplicitSymlinkOutput(PathForBazelOut(ctx, symlinkPath))
  969. }
  970. }
  971. func getCqueryId(key cqueryKey) string {
  972. return key.label + "|" + getConfigString(key)
  973. }
  974. func getConfigString(key cqueryKey) string {
  975. arch := key.configKey.arch
  976. if len(arch) == 0 || arch == "common" {
  977. if key.configKey.osType.Class == Device {
  978. // For the generic Android, the expected result is "target|android", which
  979. // corresponds to the product_variable_config named "android_target" in
  980. // build/bazel/platforms/BUILD.bazel.
  981. arch = "target"
  982. } else {
  983. // Use host platform, which is currently hardcoded to be x86_64.
  984. arch = "x86_64"
  985. }
  986. }
  987. osName := key.configKey.osType.Name
  988. if len(osName) == 0 || osName == "common_os" || osName == "linux_glibc" {
  989. // Use host OS, which is currently hardcoded to be linux.
  990. osName = "linux"
  991. }
  992. return arch + "|" + osName
  993. }
  994. func GetConfigKey(ctx BaseModuleContext) configKey {
  995. return configKey{
  996. // use string because Arch is not a valid key in go
  997. arch: ctx.Arch().String(),
  998. osType: ctx.Os(),
  999. }
  1000. }
  1001. func bazelDepsetName(contentHash string) string {
  1002. return fmt.Sprintf("bazel_depset_%s", contentHash)
  1003. }