rule_builder.go 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. // Copyright 2018 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. "crypto/sha256"
  17. "fmt"
  18. "path/filepath"
  19. "sort"
  20. "strings"
  21. "testing"
  22. "github.com/golang/protobuf/proto"
  23. "github.com/google/blueprint"
  24. "github.com/google/blueprint/proptools"
  25. "android/soong/cmd/sbox/sbox_proto"
  26. "android/soong/remoteexec"
  27. "android/soong/response"
  28. "android/soong/shared"
  29. )
  30. const sboxSandboxBaseDir = "__SBOX_SANDBOX_DIR__"
  31. const sboxOutSubDir = "out"
  32. const sboxToolsSubDir = "tools"
  33. const sboxOutDir = sboxSandboxBaseDir + "/" + sboxOutSubDir
  34. // RuleBuilder provides an alternative to ModuleContext.Rule and ModuleContext.Build to add a command line to the build
  35. // graph.
  36. type RuleBuilder struct {
  37. pctx PackageContext
  38. ctx BuilderContext
  39. commands []*RuleBuilderCommand
  40. installs RuleBuilderInstalls
  41. temporariesSet map[WritablePath]bool
  42. restat bool
  43. sbox bool
  44. highmem bool
  45. remoteable RemoteRuleSupports
  46. rbeParams *remoteexec.REParams
  47. outDir WritablePath
  48. sboxTools bool
  49. sboxInputs bool
  50. sboxManifestPath WritablePath
  51. missingDeps []string
  52. }
  53. // NewRuleBuilder returns a newly created RuleBuilder.
  54. func NewRuleBuilder(pctx PackageContext, ctx BuilderContext) *RuleBuilder {
  55. return &RuleBuilder{
  56. pctx: pctx,
  57. ctx: ctx,
  58. temporariesSet: make(map[WritablePath]bool),
  59. }
  60. }
  61. // RuleBuilderInstall is a tuple of install from and to locations.
  62. type RuleBuilderInstall struct {
  63. From Path
  64. To string
  65. }
  66. type RuleBuilderInstalls []RuleBuilderInstall
  67. // String returns the RuleBuilderInstalls in the form used by $(call copy-many-files) in Make, a space separated
  68. // list of from:to tuples.
  69. func (installs RuleBuilderInstalls) String() string {
  70. sb := strings.Builder{}
  71. for i, install := range installs {
  72. if i != 0 {
  73. sb.WriteRune(' ')
  74. }
  75. sb.WriteString(install.From.String())
  76. sb.WriteRune(':')
  77. sb.WriteString(install.To)
  78. }
  79. return sb.String()
  80. }
  81. // MissingDeps adds modules to the list of missing dependencies. If MissingDeps
  82. // is called with a non-empty input, any call to Build will result in a rule
  83. // that will print an error listing the missing dependencies and fail.
  84. // MissingDeps should only be called if Config.AllowMissingDependencies() is
  85. // true.
  86. func (r *RuleBuilder) MissingDeps(missingDeps []string) {
  87. r.missingDeps = append(r.missingDeps, missingDeps...)
  88. }
  89. // Restat marks the rule as a restat rule, which will be passed to ModuleContext.Rule in BuildParams.Restat.
  90. //
  91. // Restat is not compatible with Sbox()
  92. func (r *RuleBuilder) Restat() *RuleBuilder {
  93. if r.sbox {
  94. panic("Restat() is not compatible with Sbox()")
  95. }
  96. r.restat = true
  97. return r
  98. }
  99. // HighMem marks the rule as a high memory rule, which will limit how many run in parallel with other high memory
  100. // rules.
  101. func (r *RuleBuilder) HighMem() *RuleBuilder {
  102. r.highmem = true
  103. return r
  104. }
  105. // Remoteable marks the rule as supporting remote execution.
  106. func (r *RuleBuilder) Remoteable(supports RemoteRuleSupports) *RuleBuilder {
  107. r.remoteable = supports
  108. return r
  109. }
  110. // Rewrapper marks the rule as running inside rewrapper using the given params in order to support
  111. // running on RBE. During RuleBuilder.Build the params will be combined with the inputs, outputs
  112. // and tools known to RuleBuilder to prepend an appropriate rewrapper command line to the rule's
  113. // command line.
  114. func (r *RuleBuilder) Rewrapper(params *remoteexec.REParams) *RuleBuilder {
  115. if !r.sboxInputs {
  116. panic(fmt.Errorf("RuleBuilder.Rewrapper must be called after RuleBuilder.SandboxInputs"))
  117. }
  118. r.rbeParams = params
  119. return r
  120. }
  121. // Sbox marks the rule as needing to be wrapped by sbox. The outputDir should point to the output
  122. // directory that sbox will wipe. It should not be written to by any other rule. manifestPath should
  123. // point to a location where sbox's manifest will be written and must be outside outputDir. sbox
  124. // will ensure that all outputs have been written, and will discard any output files that were not
  125. // specified.
  126. //
  127. // Sbox is not compatible with Restat()
  128. func (r *RuleBuilder) Sbox(outputDir WritablePath, manifestPath WritablePath) *RuleBuilder {
  129. if r.sbox {
  130. panic("Sbox() may not be called more than once")
  131. }
  132. if len(r.commands) > 0 {
  133. panic("Sbox() may not be called after Command()")
  134. }
  135. if r.restat {
  136. panic("Sbox() is not compatible with Restat()")
  137. }
  138. r.sbox = true
  139. r.outDir = outputDir
  140. r.sboxManifestPath = manifestPath
  141. return r
  142. }
  143. // SandboxTools enables tool sandboxing for the rule by copying any referenced tools into the
  144. // sandbox.
  145. func (r *RuleBuilder) SandboxTools() *RuleBuilder {
  146. if !r.sbox {
  147. panic("SandboxTools() must be called after Sbox()")
  148. }
  149. if len(r.commands) > 0 {
  150. panic("SandboxTools() may not be called after Command()")
  151. }
  152. r.sboxTools = true
  153. return r
  154. }
  155. // SandboxInputs enables input sandboxing for the rule by copying any referenced inputs into the
  156. // sandbox. It also implies SandboxTools().
  157. //
  158. // Sandboxing inputs requires RuleBuilder to be aware of all references to input paths. Paths
  159. // that are passed to RuleBuilder outside of the methods that expect inputs, for example
  160. // FlagWithArg, must use RuleBuilderCommand.PathForInput to translate the path to one that matches
  161. // the sandbox layout.
  162. func (r *RuleBuilder) SandboxInputs() *RuleBuilder {
  163. if !r.sbox {
  164. panic("SandboxInputs() must be called after Sbox()")
  165. }
  166. if len(r.commands) > 0 {
  167. panic("SandboxInputs() may not be called after Command()")
  168. }
  169. r.sboxTools = true
  170. r.sboxInputs = true
  171. return r
  172. }
  173. // Install associates an output of the rule with an install location, which can be retrieved later using
  174. // RuleBuilder.Installs.
  175. func (r *RuleBuilder) Install(from Path, to string) {
  176. r.installs = append(r.installs, RuleBuilderInstall{from, to})
  177. }
  178. // Command returns a new RuleBuilderCommand for the rule. The commands will be ordered in the rule by when they were
  179. // created by this method. That can be mutated through their methods in any order, as long as the mutations do not
  180. // race with any call to Build.
  181. func (r *RuleBuilder) Command() *RuleBuilderCommand {
  182. command := &RuleBuilderCommand{
  183. rule: r,
  184. }
  185. r.commands = append(r.commands, command)
  186. return command
  187. }
  188. // Temporary marks an output of a command as an intermediate file that will be used as an input to another command
  189. // in the same rule, and should not be listed in Outputs.
  190. func (r *RuleBuilder) Temporary(path WritablePath) {
  191. r.temporariesSet[path] = true
  192. }
  193. // DeleteTemporaryFiles adds a command to the rule that deletes any outputs that have been marked using Temporary
  194. // when the rule runs. DeleteTemporaryFiles should be called after all calls to Temporary.
  195. func (r *RuleBuilder) DeleteTemporaryFiles() {
  196. var temporariesList WritablePaths
  197. for intermediate := range r.temporariesSet {
  198. temporariesList = append(temporariesList, intermediate)
  199. }
  200. sort.Slice(temporariesList, func(i, j int) bool {
  201. return temporariesList[i].String() < temporariesList[j].String()
  202. })
  203. r.Command().Text("rm").Flag("-f").Outputs(temporariesList)
  204. }
  205. // Inputs returns the list of paths that were passed to the RuleBuilderCommand methods that take
  206. // input paths, such as RuleBuilderCommand.Input, RuleBuilderCommand.Implicit, or
  207. // RuleBuilderCommand.FlagWithInput. Inputs to a command that are also outputs of another command
  208. // in the same RuleBuilder are filtered out. The list is sorted and duplicates removed.
  209. func (r *RuleBuilder) Inputs() Paths {
  210. outputs := r.outputSet()
  211. depFiles := r.depFileSet()
  212. inputs := make(map[string]Path)
  213. for _, c := range r.commands {
  214. for _, input := range append(c.inputs, c.implicits...) {
  215. inputStr := input.String()
  216. if _, isOutput := outputs[inputStr]; !isOutput {
  217. if _, isDepFile := depFiles[inputStr]; !isDepFile {
  218. inputs[input.String()] = input
  219. }
  220. }
  221. }
  222. }
  223. var inputList Paths
  224. for _, input := range inputs {
  225. inputList = append(inputList, input)
  226. }
  227. sort.Slice(inputList, func(i, j int) bool {
  228. return inputList[i].String() < inputList[j].String()
  229. })
  230. return inputList
  231. }
  232. // OrderOnlys returns the list of paths that were passed to the RuleBuilderCommand.OrderOnly or
  233. // RuleBuilderCommand.OrderOnlys. The list is sorted and duplicates removed.
  234. func (r *RuleBuilder) OrderOnlys() Paths {
  235. orderOnlys := make(map[string]Path)
  236. for _, c := range r.commands {
  237. for _, orderOnly := range c.orderOnlys {
  238. orderOnlys[orderOnly.String()] = orderOnly
  239. }
  240. }
  241. var orderOnlyList Paths
  242. for _, orderOnly := range orderOnlys {
  243. orderOnlyList = append(orderOnlyList, orderOnly)
  244. }
  245. sort.Slice(orderOnlyList, func(i, j int) bool {
  246. return orderOnlyList[i].String() < orderOnlyList[j].String()
  247. })
  248. return orderOnlyList
  249. }
  250. func (r *RuleBuilder) outputSet() map[string]WritablePath {
  251. outputs := make(map[string]WritablePath)
  252. for _, c := range r.commands {
  253. for _, output := range c.outputs {
  254. outputs[output.String()] = output
  255. }
  256. }
  257. return outputs
  258. }
  259. // Outputs returns the list of paths that were passed to the RuleBuilderCommand methods that take
  260. // output paths, such as RuleBuilderCommand.Output, RuleBuilderCommand.ImplicitOutput, or
  261. // RuleBuilderCommand.FlagWithInput. The list is sorted and duplicates removed.
  262. func (r *RuleBuilder) Outputs() WritablePaths {
  263. outputs := r.outputSet()
  264. var outputList WritablePaths
  265. for _, output := range outputs {
  266. if !r.temporariesSet[output] {
  267. outputList = append(outputList, output)
  268. }
  269. }
  270. sort.Slice(outputList, func(i, j int) bool {
  271. return outputList[i].String() < outputList[j].String()
  272. })
  273. return outputList
  274. }
  275. func (r *RuleBuilder) symlinkOutputSet() map[string]WritablePath {
  276. symlinkOutputs := make(map[string]WritablePath)
  277. for _, c := range r.commands {
  278. for _, symlinkOutput := range c.symlinkOutputs {
  279. symlinkOutputs[symlinkOutput.String()] = symlinkOutput
  280. }
  281. }
  282. return symlinkOutputs
  283. }
  284. // SymlinkOutputs returns the list of paths that the executor (Ninja) would
  285. // verify, after build edge completion, that:
  286. //
  287. // 1) Created output symlinks match the list of paths in this list exactly (no more, no fewer)
  288. // 2) Created output files are *not* declared in this list.
  289. //
  290. // These symlink outputs are expected to be a subset of outputs or implicit
  291. // outputs, or they would fail validation at build param construction time
  292. // later, to support other non-rule-builder approaches for constructing
  293. // statements.
  294. func (r *RuleBuilder) SymlinkOutputs() WritablePaths {
  295. symlinkOutputs := r.symlinkOutputSet()
  296. var symlinkOutputList WritablePaths
  297. for _, symlinkOutput := range symlinkOutputs {
  298. symlinkOutputList = append(symlinkOutputList, symlinkOutput)
  299. }
  300. sort.Slice(symlinkOutputList, func(i, j int) bool {
  301. return symlinkOutputList[i].String() < symlinkOutputList[j].String()
  302. })
  303. return symlinkOutputList
  304. }
  305. func (r *RuleBuilder) depFileSet() map[string]WritablePath {
  306. depFiles := make(map[string]WritablePath)
  307. for _, c := range r.commands {
  308. for _, depFile := range c.depFiles {
  309. depFiles[depFile.String()] = depFile
  310. }
  311. }
  312. return depFiles
  313. }
  314. // DepFiles returns the list of paths that were passed to the RuleBuilderCommand methods that take depfile paths, such
  315. // as RuleBuilderCommand.DepFile or RuleBuilderCommand.FlagWithDepFile.
  316. func (r *RuleBuilder) DepFiles() WritablePaths {
  317. var depFiles WritablePaths
  318. for _, c := range r.commands {
  319. for _, depFile := range c.depFiles {
  320. depFiles = append(depFiles, depFile)
  321. }
  322. }
  323. return depFiles
  324. }
  325. // Installs returns the list of tuples passed to Install.
  326. func (r *RuleBuilder) Installs() RuleBuilderInstalls {
  327. return append(RuleBuilderInstalls(nil), r.installs...)
  328. }
  329. func (r *RuleBuilder) toolsSet() map[string]Path {
  330. tools := make(map[string]Path)
  331. for _, c := range r.commands {
  332. for _, tool := range c.tools {
  333. tools[tool.String()] = tool
  334. }
  335. }
  336. return tools
  337. }
  338. // Tools returns the list of paths that were passed to the RuleBuilderCommand.Tool method. The
  339. // list is sorted and duplicates removed.
  340. func (r *RuleBuilder) Tools() Paths {
  341. toolsSet := r.toolsSet()
  342. var toolsList Paths
  343. for _, tool := range toolsSet {
  344. toolsList = append(toolsList, tool)
  345. }
  346. sort.Slice(toolsList, func(i, j int) bool {
  347. return toolsList[i].String() < toolsList[j].String()
  348. })
  349. return toolsList
  350. }
  351. // RspFileInputs returns the list of paths that were passed to the RuleBuilderCommand.FlagWithRspFileInputList method.
  352. func (r *RuleBuilder) RspFileInputs() Paths {
  353. var rspFileInputs Paths
  354. for _, c := range r.commands {
  355. if c.rspFileInputs != nil {
  356. if rspFileInputs != nil {
  357. panic("Multiple commands in a rule may not have rsp file inputs")
  358. }
  359. rspFileInputs = c.rspFileInputs
  360. }
  361. }
  362. return rspFileInputs
  363. }
  364. // RspFile returns the path to the rspfile that was passed to the RuleBuilderCommand.FlagWithRspFileInputList method.
  365. func (r *RuleBuilder) RspFile() WritablePath {
  366. var rspFile WritablePath
  367. for _, c := range r.commands {
  368. if c.rspFile != nil {
  369. if rspFile != nil {
  370. panic("Multiple commands in a rule may not have rsp file inputs")
  371. }
  372. rspFile = c.rspFile
  373. }
  374. }
  375. return rspFile
  376. }
  377. // Commands returns a slice containing the built command line for each call to RuleBuilder.Command.
  378. func (r *RuleBuilder) Commands() []string {
  379. var commands []string
  380. for _, c := range r.commands {
  381. commands = append(commands, c.String())
  382. }
  383. return commands
  384. }
  385. // BuilderContext is a subset of ModuleContext and SingletonContext.
  386. type BuilderContext interface {
  387. PathContext
  388. Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule
  389. Build(PackageContext, BuildParams)
  390. }
  391. var _ BuilderContext = ModuleContext(nil)
  392. var _ BuilderContext = SingletonContext(nil)
  393. func (r *RuleBuilder) depFileMergerCmd(depFiles WritablePaths) *RuleBuilderCommand {
  394. return r.Command().
  395. BuiltTool("dep_fixer").
  396. Inputs(depFiles.Paths())
  397. }
  398. // Build adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for
  399. // Outputs.
  400. func (r *RuleBuilder) Build(name string, desc string) {
  401. name = ninjaNameEscape(name)
  402. if len(r.missingDeps) > 0 {
  403. r.ctx.Build(pctx, BuildParams{
  404. Rule: ErrorRule,
  405. Outputs: r.Outputs(),
  406. OrderOnly: r.OrderOnlys(),
  407. Description: desc,
  408. Args: map[string]string{
  409. "error": "missing dependencies: " + strings.Join(r.missingDeps, ", "),
  410. },
  411. })
  412. return
  413. }
  414. var depFile WritablePath
  415. var depFormat blueprint.Deps
  416. if depFiles := r.DepFiles(); len(depFiles) > 0 {
  417. depFile = depFiles[0]
  418. depFormat = blueprint.DepsGCC
  419. if len(depFiles) > 1 {
  420. // Add a command locally that merges all depfiles together into the first depfile.
  421. r.depFileMergerCmd(depFiles)
  422. if r.sbox {
  423. // Check for Rel() errors, as all depfiles should be in the output dir. Errors
  424. // will be reported to the ctx.
  425. for _, path := range depFiles[1:] {
  426. Rel(r.ctx, r.outDir.String(), path.String())
  427. }
  428. }
  429. }
  430. }
  431. tools := r.Tools()
  432. commands := r.Commands()
  433. outputs := r.Outputs()
  434. inputs := r.Inputs()
  435. rspFileInputs := r.RspFileInputs()
  436. rspFilePath := r.RspFile()
  437. if len(commands) == 0 {
  438. return
  439. }
  440. if len(outputs) == 0 {
  441. panic("No outputs specified from any Commands")
  442. }
  443. commandString := strings.Join(commands, " && ")
  444. if r.sbox {
  445. // If running the command inside sbox, write the rule data out to an sbox
  446. // manifest.textproto.
  447. manifest := sbox_proto.Manifest{}
  448. command := sbox_proto.Command{}
  449. manifest.Commands = append(manifest.Commands, &command)
  450. command.Command = proto.String(commandString)
  451. if depFile != nil {
  452. manifest.OutputDepfile = proto.String(depFile.String())
  453. }
  454. // If sandboxing tools is enabled, add copy rules to the manifest to copy each tool
  455. // into the sbox directory.
  456. if r.sboxTools {
  457. for _, tool := range tools {
  458. command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{
  459. From: proto.String(tool.String()),
  460. To: proto.String(sboxPathForToolRel(r.ctx, tool)),
  461. })
  462. }
  463. for _, c := range r.commands {
  464. for _, tool := range c.packagedTools {
  465. command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{
  466. From: proto.String(tool.srcPath.String()),
  467. To: proto.String(sboxPathForPackagedToolRel(tool)),
  468. Executable: proto.Bool(tool.executable),
  469. })
  470. tools = append(tools, tool.srcPath)
  471. }
  472. }
  473. }
  474. // If sandboxing inputs is enabled, add copy rules to the manifest to copy each input
  475. // into the sbox directory.
  476. if r.sboxInputs {
  477. for _, input := range inputs {
  478. command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{
  479. From: proto.String(input.String()),
  480. To: proto.String(r.sboxPathForInputRel(input)),
  481. })
  482. }
  483. // If using an rsp file copy it into the sbox directory.
  484. if rspFilePath != nil {
  485. command.RspFiles = append(command.RspFiles, &sbox_proto.RspFile{
  486. File: proto.String(rspFilePath.String()),
  487. // These have to match the logic in sboxPathForInputRel
  488. PathMappings: []*sbox_proto.PathMapping{
  489. {
  490. From: proto.String(r.outDir.String()),
  491. To: proto.String(sboxOutSubDir),
  492. },
  493. {
  494. From: proto.String(PathForOutput(r.ctx).String()),
  495. To: proto.String(sboxOutSubDir),
  496. },
  497. },
  498. })
  499. }
  500. command.Chdir = proto.Bool(true)
  501. }
  502. // Add copy rules to the manifest to copy each output file from the sbox directory.
  503. // to the output directory after running the commands.
  504. sboxOutputs := make([]string, len(outputs))
  505. for i, output := range outputs {
  506. rel := Rel(r.ctx, r.outDir.String(), output.String())
  507. sboxOutputs[i] = filepath.Join(sboxOutDir, rel)
  508. command.CopyAfter = append(command.CopyAfter, &sbox_proto.Copy{
  509. From: proto.String(filepath.Join(sboxOutSubDir, rel)),
  510. To: proto.String(output.String()),
  511. })
  512. }
  513. // Outputs that were marked Temporary will not be checked that they are in the output
  514. // directory by the loop above, check them here.
  515. for path := range r.temporariesSet {
  516. Rel(r.ctx, r.outDir.String(), path.String())
  517. }
  518. // Add a hash of the list of input files to the manifest so that the textproto file
  519. // changes when the list of input files changes and causes the sbox rule that
  520. // depends on it to rerun.
  521. command.InputHash = proto.String(hashSrcFiles(inputs))
  522. // Verify that the manifest textproto is not inside the sbox output directory, otherwise
  523. // it will get deleted when the sbox rule clears its output directory.
  524. _, manifestInOutDir := MaybeRel(r.ctx, r.outDir.String(), r.sboxManifestPath.String())
  525. if manifestInOutDir {
  526. ReportPathErrorf(r.ctx, "sbox rule %q manifestPath %q must not be in outputDir %q",
  527. name, r.sboxManifestPath.String(), r.outDir.String())
  528. }
  529. // Create a rule to write the manifest as a the textproto.
  530. WriteFileRule(r.ctx, r.sboxManifestPath, proto.MarshalTextString(&manifest))
  531. // Generate a new string to use as the command line of the sbox rule. This uses
  532. // a RuleBuilderCommand as a convenience method of building the command line, then
  533. // converts it to a string to replace commandString.
  534. sboxCmd := &RuleBuilderCommand{
  535. rule: &RuleBuilder{
  536. ctx: r.ctx,
  537. },
  538. }
  539. sboxCmd.Text("rm -rf").Output(r.outDir)
  540. sboxCmd.Text("&&")
  541. sboxCmd.BuiltTool("sbox").
  542. Flag("--sandbox-path").Text(shared.TempDirForOutDir(PathForOutput(r.ctx).String())).
  543. Flag("--manifest").Input(r.sboxManifestPath)
  544. // Replace the command string, and add the sbox tool and manifest textproto to the
  545. // dependencies of the final sbox rule.
  546. commandString = sboxCmd.buf.String()
  547. tools = append(tools, sboxCmd.tools...)
  548. inputs = append(inputs, sboxCmd.inputs...)
  549. if r.rbeParams != nil {
  550. // RBE needs a list of input files to copy to the remote builder. For inputs already
  551. // listed in an rsp file, pass the rsp file directly to rewrapper. For the rest,
  552. // create a new rsp file to pass to rewrapper.
  553. var remoteRspFiles Paths
  554. var remoteInputs Paths
  555. remoteInputs = append(remoteInputs, inputs...)
  556. remoteInputs = append(remoteInputs, tools...)
  557. if rspFilePath != nil {
  558. remoteInputs = append(remoteInputs, rspFilePath)
  559. remoteRspFiles = append(remoteRspFiles, rspFilePath)
  560. }
  561. if len(remoteInputs) > 0 {
  562. inputsListFile := r.sboxManifestPath.ReplaceExtension(r.ctx, "rbe_inputs.list")
  563. writeRspFileRule(r.ctx, inputsListFile, remoteInputs)
  564. remoteRspFiles = append(remoteRspFiles, inputsListFile)
  565. // Add the new rsp file as an extra input to the rule.
  566. inputs = append(inputs, inputsListFile)
  567. }
  568. r.rbeParams.OutputFiles = outputs.Strings()
  569. r.rbeParams.RSPFiles = remoteRspFiles.Strings()
  570. rewrapperCommand := r.rbeParams.NoVarTemplate(r.ctx.Config().RBEWrapper())
  571. commandString = rewrapperCommand + " bash -c '" + strings.ReplaceAll(commandString, `'`, `'\''`) + "'"
  572. }
  573. } else {
  574. // If not using sbox the rule will run the command directly, put the hash of the
  575. // list of input files in a comment at the end of the command line to ensure ninja
  576. // reruns the rule when the list of input files changes.
  577. commandString += " # hash of input list: " + hashSrcFiles(inputs)
  578. }
  579. // Ninja doesn't like multiple outputs when depfiles are enabled, move all but the first output to
  580. // ImplicitOutputs. RuleBuilder doesn't use "$out", so the distinction between Outputs and
  581. // ImplicitOutputs doesn't matter.
  582. output := outputs[0]
  583. implicitOutputs := outputs[1:]
  584. var rspFile, rspFileContent string
  585. if rspFilePath != nil {
  586. rspFile = rspFilePath.String()
  587. // Use "$in" for rspFileContent to avoid duplicating the list of files in the dependency
  588. // list and in the contents of the rsp file. Inputs to the rule that are not in the
  589. // rsp file will be listed in Implicits instead of Inputs so they don't show up in "$in".
  590. rspFileContent = "$in"
  591. }
  592. var pool blueprint.Pool
  593. if r.ctx.Config().UseGoma() && r.remoteable.Goma {
  594. // When USE_GOMA=true is set and the rule is supported by goma, allow jobs to run outside the local pool.
  595. } else if r.ctx.Config().UseRBE() && r.remoteable.RBE {
  596. // When USE_RBE=true is set and the rule is supported by RBE, use the remotePool.
  597. pool = remotePool
  598. } else if r.highmem {
  599. pool = highmemPool
  600. } else if r.ctx.Config().UseRemoteBuild() {
  601. pool = localPool
  602. }
  603. r.ctx.Build(r.pctx, BuildParams{
  604. Rule: r.ctx.Rule(pctx, name, blueprint.RuleParams{
  605. Command: proptools.NinjaEscape(commandString),
  606. CommandDeps: proptools.NinjaEscapeList(tools.Strings()),
  607. Restat: r.restat,
  608. Rspfile: proptools.NinjaEscape(rspFile),
  609. RspfileContent: rspFileContent,
  610. Pool: pool,
  611. }),
  612. Inputs: rspFileInputs,
  613. Implicits: inputs,
  614. Output: output,
  615. ImplicitOutputs: implicitOutputs,
  616. SymlinkOutputs: r.SymlinkOutputs(),
  617. Depfile: depFile,
  618. Deps: depFormat,
  619. Description: desc,
  620. })
  621. }
  622. // RuleBuilderCommand is a builder for a command in a command line. It can be mutated by its methods to add to the
  623. // command and track dependencies. The methods mutate the RuleBuilderCommand in place, as well as return the
  624. // RuleBuilderCommand, so they can be used chained or unchained. All methods that add text implicitly add a single
  625. // space as a separator from the previous method.
  626. type RuleBuilderCommand struct {
  627. rule *RuleBuilder
  628. buf strings.Builder
  629. inputs Paths
  630. implicits Paths
  631. orderOnlys Paths
  632. outputs WritablePaths
  633. symlinkOutputs WritablePaths
  634. depFiles WritablePaths
  635. tools Paths
  636. packagedTools []PackagingSpec
  637. rspFileInputs Paths
  638. rspFile WritablePath
  639. }
  640. func (c *RuleBuilderCommand) addInput(path Path) string {
  641. c.inputs = append(c.inputs, path)
  642. return c.PathForInput(path)
  643. }
  644. func (c *RuleBuilderCommand) addImplicit(path Path) {
  645. c.implicits = append(c.implicits, path)
  646. }
  647. func (c *RuleBuilderCommand) addOrderOnly(path Path) {
  648. c.orderOnlys = append(c.orderOnlys, path)
  649. }
  650. // PathForInput takes an input path and returns the appropriate path to use on the command line. If
  651. // sbox was enabled via a call to RuleBuilder.Sbox() and the path was an output path it returns a
  652. // path with the placeholder prefix used for outputs in sbox. If sbox is not enabled it returns the
  653. // original path.
  654. func (c *RuleBuilderCommand) PathForInput(path Path) string {
  655. if c.rule.sbox {
  656. rel, inSandbox := c.rule._sboxPathForInputRel(path)
  657. if inSandbox {
  658. rel = filepath.Join(sboxSandboxBaseDir, rel)
  659. }
  660. return rel
  661. }
  662. return path.String()
  663. }
  664. // PathsForInputs takes a list of input paths and returns the appropriate paths to use on the
  665. // command line. If sbox was enabled via a call to RuleBuilder.Sbox() a path was an output path, it
  666. // returns the path with the placeholder prefix used for outputs in sbox. If sbox is not enabled it
  667. // returns the original paths.
  668. func (c *RuleBuilderCommand) PathsForInputs(paths Paths) []string {
  669. ret := make([]string, len(paths))
  670. for i, path := range paths {
  671. ret[i] = c.PathForInput(path)
  672. }
  673. return ret
  674. }
  675. // PathForOutput takes an output path and returns the appropriate path to use on the command
  676. // line. If sbox was enabled via a call to RuleBuilder.Sbox(), it returns a path with the
  677. // placeholder prefix used for outputs in sbox. If sbox is not enabled it returns the
  678. // original path.
  679. func (c *RuleBuilderCommand) PathForOutput(path WritablePath) string {
  680. if c.rule.sbox {
  681. // Errors will be handled in RuleBuilder.Build where we have a context to report them
  682. rel, _, _ := maybeRelErr(c.rule.outDir.String(), path.String())
  683. return filepath.Join(sboxOutDir, rel)
  684. }
  685. return path.String()
  686. }
  687. // SboxPathForTool takes a path to a tool, which may be an output file or a source file, and returns
  688. // the corresponding path for the tool in the sbox sandbox. It assumes that sandboxing and tool
  689. // sandboxing are enabled.
  690. func SboxPathForTool(ctx BuilderContext, path Path) string {
  691. return filepath.Join(sboxSandboxBaseDir, sboxPathForToolRel(ctx, path))
  692. }
  693. func sboxPathForToolRel(ctx BuilderContext, path Path) string {
  694. // Errors will be handled in RuleBuilder.Build where we have a context to report them
  695. relOut, isRelOut, _ := maybeRelErr(PathForOutput(ctx, "host", ctx.Config().PrebuiltOS()).String(), path.String())
  696. if isRelOut {
  697. // The tool is in the output directory, it will be copied to __SBOX_OUT_DIR__/tools/out
  698. return filepath.Join(sboxToolsSubDir, "out", relOut)
  699. }
  700. // The tool is in the source directory, it will be copied to __SBOX_OUT_DIR__/tools/src
  701. return filepath.Join(sboxToolsSubDir, "src", path.String())
  702. }
  703. func (r *RuleBuilder) _sboxPathForInputRel(path Path) (rel string, inSandbox bool) {
  704. // Errors will be handled in RuleBuilder.Build where we have a context to report them
  705. rel, isRelSboxOut, _ := maybeRelErr(r.outDir.String(), path.String())
  706. if isRelSboxOut {
  707. return filepath.Join(sboxOutSubDir, rel), true
  708. }
  709. if r.sboxInputs {
  710. // When sandboxing inputs all inputs have to be copied into the sandbox. Input files that
  711. // are outputs of other rules could be an arbitrary absolute path if OUT_DIR is set, so they
  712. // will be copied to relative paths under __SBOX_OUT_DIR__/out.
  713. rel, isRelOut, _ := maybeRelErr(PathForOutput(r.ctx).String(), path.String())
  714. if isRelOut {
  715. return filepath.Join(sboxOutSubDir, rel), true
  716. }
  717. }
  718. return path.String(), false
  719. }
  720. func (r *RuleBuilder) sboxPathForInputRel(path Path) string {
  721. rel, _ := r._sboxPathForInputRel(path)
  722. return rel
  723. }
  724. func (r *RuleBuilder) sboxPathsForInputsRel(paths Paths) []string {
  725. ret := make([]string, len(paths))
  726. for i, path := range paths {
  727. ret[i] = r.sboxPathForInputRel(path)
  728. }
  729. return ret
  730. }
  731. // SboxPathForPackagedTool takes a PackageSpec for a tool and returns the corresponding path for the
  732. // tool after copying it into the sandbox. This can be used on the RuleBuilder command line to
  733. // reference the tool.
  734. func SboxPathForPackagedTool(spec PackagingSpec) string {
  735. return filepath.Join(sboxSandboxBaseDir, sboxPathForPackagedToolRel(spec))
  736. }
  737. func sboxPathForPackagedToolRel(spec PackagingSpec) string {
  738. return filepath.Join(sboxToolsSubDir, "out", spec.relPathInPackage)
  739. }
  740. // PathForTool takes a path to a tool, which may be an output file or a source file, and returns
  741. // the corresponding path for the tool in the sbox sandbox if sbox is enabled, or the original path
  742. // if it is not. This can be used on the RuleBuilder command line to reference the tool.
  743. func (c *RuleBuilderCommand) PathForTool(path Path) string {
  744. if c.rule.sbox && c.rule.sboxTools {
  745. return filepath.Join(sboxSandboxBaseDir, sboxPathForToolRel(c.rule.ctx, path))
  746. }
  747. return path.String()
  748. }
  749. // PackagedTool adds the specified tool path to the command line. It can only be used with tool
  750. // sandboxing enabled by SandboxTools(), and will copy the tool into the sandbox.
  751. func (c *RuleBuilderCommand) PackagedTool(spec PackagingSpec) *RuleBuilderCommand {
  752. if !c.rule.sboxTools {
  753. panic("PackagedTool() requires SandboxTools()")
  754. }
  755. c.packagedTools = append(c.packagedTools, spec)
  756. c.Text(sboxPathForPackagedToolRel(spec))
  757. return c
  758. }
  759. // ImplicitPackagedTool copies the specified tool into the sandbox without modifying the command
  760. // line. It can only be used with tool sandboxing enabled by SandboxTools().
  761. func (c *RuleBuilderCommand) ImplicitPackagedTool(spec PackagingSpec) *RuleBuilderCommand {
  762. if !c.rule.sboxTools {
  763. panic("ImplicitPackagedTool() requires SandboxTools()")
  764. }
  765. c.packagedTools = append(c.packagedTools, spec)
  766. return c
  767. }
  768. // ImplicitPackagedTools copies the specified tools into the sandbox without modifying the command
  769. // line. It can only be used with tool sandboxing enabled by SandboxTools().
  770. func (c *RuleBuilderCommand) ImplicitPackagedTools(specs []PackagingSpec) *RuleBuilderCommand {
  771. if !c.rule.sboxTools {
  772. panic("ImplicitPackagedTools() requires SandboxTools()")
  773. }
  774. c.packagedTools = append(c.packagedTools, specs...)
  775. return c
  776. }
  777. // Text adds the specified raw text to the command line. The text should not contain input or output paths or the
  778. // rule will not have them listed in its dependencies or outputs.
  779. func (c *RuleBuilderCommand) Text(text string) *RuleBuilderCommand {
  780. if c.buf.Len() > 0 {
  781. c.buf.WriteByte(' ')
  782. }
  783. c.buf.WriteString(text)
  784. return c
  785. }
  786. // Textf adds the specified formatted text to the command line. The text should not contain input or output paths or
  787. // the rule will not have them listed in its dependencies or outputs.
  788. func (c *RuleBuilderCommand) Textf(format string, a ...interface{}) *RuleBuilderCommand {
  789. return c.Text(fmt.Sprintf(format, a...))
  790. }
  791. // Flag adds the specified raw text to the command line. The text should not contain input or output paths or the
  792. // rule will not have them listed in its dependencies or outputs.
  793. func (c *RuleBuilderCommand) Flag(flag string) *RuleBuilderCommand {
  794. return c.Text(flag)
  795. }
  796. // OptionalFlag adds the specified raw text to the command line if it is not nil. The text should not contain input or
  797. // output paths or the rule will not have them listed in its dependencies or outputs.
  798. func (c *RuleBuilderCommand) OptionalFlag(flag *string) *RuleBuilderCommand {
  799. if flag != nil {
  800. c.Text(*flag)
  801. }
  802. return c
  803. }
  804. // Flags adds the specified raw text to the command line. The text should not contain input or output paths or the
  805. // rule will not have them listed in its dependencies or outputs.
  806. func (c *RuleBuilderCommand) Flags(flags []string) *RuleBuilderCommand {
  807. for _, flag := range flags {
  808. c.Text(flag)
  809. }
  810. return c
  811. }
  812. // FlagWithArg adds the specified flag and argument text to the command line, with no separator between them. The flag
  813. // and argument should not contain input or output paths or the rule will not have them listed in its dependencies or
  814. // outputs.
  815. func (c *RuleBuilderCommand) FlagWithArg(flag, arg string) *RuleBuilderCommand {
  816. return c.Text(flag + arg)
  817. }
  818. // FlagForEachArg adds the specified flag joined with each argument to the command line. The result is identical to
  819. // calling FlagWithArg for argument.
  820. func (c *RuleBuilderCommand) FlagForEachArg(flag string, args []string) *RuleBuilderCommand {
  821. for _, arg := range args {
  822. c.FlagWithArg(flag, arg)
  823. }
  824. return c
  825. }
  826. // FlagWithList adds the specified flag and list of arguments to the command line, with the arguments joined by sep
  827. // and no separator between the flag and arguments. The flag and arguments should not contain input or output paths or
  828. // the rule will not have them listed in its dependencies or outputs.
  829. func (c *RuleBuilderCommand) FlagWithList(flag string, list []string, sep string) *RuleBuilderCommand {
  830. return c.Text(flag + strings.Join(list, sep))
  831. }
  832. // Tool adds the specified tool path to the command line. The path will be also added to the dependencies returned by
  833. // RuleBuilder.Tools.
  834. func (c *RuleBuilderCommand) Tool(path Path) *RuleBuilderCommand {
  835. c.tools = append(c.tools, path)
  836. return c.Text(c.PathForTool(path))
  837. }
  838. // Tool adds the specified tool path to the dependencies returned by RuleBuilder.Tools.
  839. func (c *RuleBuilderCommand) ImplicitTool(path Path) *RuleBuilderCommand {
  840. c.tools = append(c.tools, path)
  841. return c
  842. }
  843. // Tool adds the specified tool path to the dependencies returned by RuleBuilder.Tools.
  844. func (c *RuleBuilderCommand) ImplicitTools(paths Paths) *RuleBuilderCommand {
  845. c.tools = append(c.tools, paths...)
  846. return c
  847. }
  848. // BuiltTool adds the specified tool path that was built using a host Soong module to the command line. The path will
  849. // be also added to the dependencies returned by RuleBuilder.Tools.
  850. //
  851. // It is equivalent to:
  852. // cmd.Tool(ctx.Config().HostToolPath(ctx, tool))
  853. func (c *RuleBuilderCommand) BuiltTool(tool string) *RuleBuilderCommand {
  854. return c.Tool(c.rule.ctx.Config().HostToolPath(c.rule.ctx, tool))
  855. }
  856. // PrebuiltBuildTool adds the specified tool path from prebuils/build-tools. The path will be also added to the
  857. // dependencies returned by RuleBuilder.Tools.
  858. //
  859. // It is equivalent to:
  860. // cmd.Tool(ctx.Config().PrebuiltBuildTool(ctx, tool))
  861. func (c *RuleBuilderCommand) PrebuiltBuildTool(ctx PathContext, tool string) *RuleBuilderCommand {
  862. return c.Tool(ctx.Config().PrebuiltBuildTool(ctx, tool))
  863. }
  864. // Input adds the specified input path to the command line. The path will also be added to the dependencies returned by
  865. // RuleBuilder.Inputs.
  866. func (c *RuleBuilderCommand) Input(path Path) *RuleBuilderCommand {
  867. return c.Text(c.addInput(path))
  868. }
  869. // Inputs adds the specified input paths to the command line, separated by spaces. The paths will also be added to the
  870. // dependencies returned by RuleBuilder.Inputs.
  871. func (c *RuleBuilderCommand) Inputs(paths Paths) *RuleBuilderCommand {
  872. for _, path := range paths {
  873. c.Input(path)
  874. }
  875. return c
  876. }
  877. // Implicit adds the specified input path to the dependencies returned by RuleBuilder.Inputs without modifying the
  878. // command line.
  879. func (c *RuleBuilderCommand) Implicit(path Path) *RuleBuilderCommand {
  880. c.addImplicit(path)
  881. return c
  882. }
  883. // Implicits adds the specified input paths to the dependencies returned by RuleBuilder.Inputs without modifying the
  884. // command line.
  885. func (c *RuleBuilderCommand) Implicits(paths Paths) *RuleBuilderCommand {
  886. for _, path := range paths {
  887. c.addImplicit(path)
  888. }
  889. return c
  890. }
  891. // GetImplicits returns the command's implicit inputs.
  892. func (c *RuleBuilderCommand) GetImplicits() Paths {
  893. return c.implicits
  894. }
  895. // OrderOnly adds the specified input path to the dependencies returned by RuleBuilder.OrderOnlys
  896. // without modifying the command line.
  897. func (c *RuleBuilderCommand) OrderOnly(path Path) *RuleBuilderCommand {
  898. c.addOrderOnly(path)
  899. return c
  900. }
  901. // OrderOnlys adds the specified input paths to the dependencies returned by RuleBuilder.OrderOnlys
  902. // without modifying the command line.
  903. func (c *RuleBuilderCommand) OrderOnlys(paths Paths) *RuleBuilderCommand {
  904. for _, path := range paths {
  905. c.addOrderOnly(path)
  906. }
  907. return c
  908. }
  909. // Output adds the specified output path to the command line. The path will also be added to the outputs returned by
  910. // RuleBuilder.Outputs.
  911. func (c *RuleBuilderCommand) Output(path WritablePath) *RuleBuilderCommand {
  912. c.outputs = append(c.outputs, path)
  913. return c.Text(c.PathForOutput(path))
  914. }
  915. // Outputs adds the specified output paths to the command line, separated by spaces. The paths will also be added to
  916. // the outputs returned by RuleBuilder.Outputs.
  917. func (c *RuleBuilderCommand) Outputs(paths WritablePaths) *RuleBuilderCommand {
  918. for _, path := range paths {
  919. c.Output(path)
  920. }
  921. return c
  922. }
  923. // OutputDir adds the output directory to the command line. This is only available when used with RuleBuilder.Sbox,
  924. // and will be the temporary output directory managed by sbox, not the final one.
  925. func (c *RuleBuilderCommand) OutputDir() *RuleBuilderCommand {
  926. if !c.rule.sbox {
  927. panic("OutputDir only valid with Sbox")
  928. }
  929. return c.Text(sboxOutDir)
  930. }
  931. // DepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles and adds it to the command
  932. // line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles are added to
  933. // commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the depfiles together.
  934. func (c *RuleBuilderCommand) DepFile(path WritablePath) *RuleBuilderCommand {
  935. c.depFiles = append(c.depFiles, path)
  936. return c.Text(c.PathForOutput(path))
  937. }
  938. // ImplicitOutput adds the specified output path to the dependencies returned by RuleBuilder.Outputs without modifying
  939. // the command line.
  940. func (c *RuleBuilderCommand) ImplicitOutput(path WritablePath) *RuleBuilderCommand {
  941. c.outputs = append(c.outputs, path)
  942. return c
  943. }
  944. // ImplicitOutputs adds the specified output paths to the dependencies returned by RuleBuilder.Outputs without modifying
  945. // the command line.
  946. func (c *RuleBuilderCommand) ImplicitOutputs(paths WritablePaths) *RuleBuilderCommand {
  947. c.outputs = append(c.outputs, paths...)
  948. return c
  949. }
  950. // ImplicitSymlinkOutput declares the specified path as an implicit output that
  951. // will be a symlink instead of a regular file. Does not modify the command
  952. // line.
  953. func (c *RuleBuilderCommand) ImplicitSymlinkOutput(path WritablePath) *RuleBuilderCommand {
  954. c.symlinkOutputs = append(c.symlinkOutputs, path)
  955. return c.ImplicitOutput(path)
  956. }
  957. // ImplicitSymlinkOutputs declares the specified paths as implicit outputs that
  958. // will be a symlinks instead of regular files. Does not modify the command
  959. // line.
  960. func (c *RuleBuilderCommand) ImplicitSymlinkOutputs(paths WritablePaths) *RuleBuilderCommand {
  961. for _, path := range paths {
  962. c.ImplicitSymlinkOutput(path)
  963. }
  964. return c
  965. }
  966. // SymlinkOutput declares the specified path as an output that will be a symlink
  967. // instead of a regular file. Modifies the command line.
  968. func (c *RuleBuilderCommand) SymlinkOutput(path WritablePath) *RuleBuilderCommand {
  969. c.symlinkOutputs = append(c.symlinkOutputs, path)
  970. return c.Output(path)
  971. }
  972. // SymlinkOutputsl declares the specified paths as outputs that will be symlinks
  973. // instead of regular files. Modifies the command line.
  974. func (c *RuleBuilderCommand) SymlinkOutputs(paths WritablePaths) *RuleBuilderCommand {
  975. for _, path := range paths {
  976. c.SymlinkOutput(path)
  977. }
  978. return c
  979. }
  980. // ImplicitDepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles without modifying
  981. // the command line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles
  982. // are added to commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the
  983. // depfiles together.
  984. func (c *RuleBuilderCommand) ImplicitDepFile(path WritablePath) *RuleBuilderCommand {
  985. c.depFiles = append(c.depFiles, path)
  986. return c
  987. }
  988. // FlagWithInput adds the specified flag and input path to the command line, with no separator between them. The path
  989. // will also be added to the dependencies returned by RuleBuilder.Inputs.
  990. func (c *RuleBuilderCommand) FlagWithInput(flag string, path Path) *RuleBuilderCommand {
  991. return c.Text(flag + c.addInput(path))
  992. }
  993. // FlagWithInputList adds the specified flag and input paths to the command line, with the inputs joined by sep
  994. // and no separator between the flag and inputs. The input paths will also be added to the dependencies returned by
  995. // RuleBuilder.Inputs.
  996. func (c *RuleBuilderCommand) FlagWithInputList(flag string, paths Paths, sep string) *RuleBuilderCommand {
  997. strs := make([]string, len(paths))
  998. for i, path := range paths {
  999. strs[i] = c.addInput(path)
  1000. }
  1001. return c.FlagWithList(flag, strs, sep)
  1002. }
  1003. // FlagForEachInput adds the specified flag joined with each input path to the command line. The input paths will also
  1004. // be added to the dependencies returned by RuleBuilder.Inputs. The result is identical to calling FlagWithInput for
  1005. // each input path.
  1006. func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths Paths) *RuleBuilderCommand {
  1007. for _, path := range paths {
  1008. c.FlagWithInput(flag, path)
  1009. }
  1010. return c
  1011. }
  1012. // FlagWithOutput adds the specified flag and output path to the command line, with no separator between them. The path
  1013. // will also be added to the outputs returned by RuleBuilder.Outputs.
  1014. func (c *RuleBuilderCommand) FlagWithOutput(flag string, path WritablePath) *RuleBuilderCommand {
  1015. c.outputs = append(c.outputs, path)
  1016. return c.Text(flag + c.PathForOutput(path))
  1017. }
  1018. // FlagWithDepFile adds the specified flag and depfile path to the command line, with no separator between them. The path
  1019. // will also be added to the outputs returned by RuleBuilder.Outputs.
  1020. func (c *RuleBuilderCommand) FlagWithDepFile(flag string, path WritablePath) *RuleBuilderCommand {
  1021. c.depFiles = append(c.depFiles, path)
  1022. return c.Text(flag + c.PathForOutput(path))
  1023. }
  1024. // FlagWithRspFileInputList adds the specified flag and path to an rspfile to the command line, with no separator
  1025. // between them. The paths will be written to the rspfile. If sbox is enabled, the rspfile must
  1026. // be outside the sbox directory.
  1027. func (c *RuleBuilderCommand) FlagWithRspFileInputList(flag string, rspFile WritablePath, paths Paths) *RuleBuilderCommand {
  1028. if c.rspFileInputs != nil {
  1029. panic("FlagWithRspFileInputList cannot be called if rsp file inputs have already been provided")
  1030. }
  1031. // Use an empty slice if paths is nil, the non-nil slice is used as an indicator that the rsp file must be
  1032. // generated.
  1033. if paths == nil {
  1034. paths = Paths{}
  1035. }
  1036. c.rspFileInputs = paths
  1037. c.rspFile = rspFile
  1038. if c.rule.sbox {
  1039. if _, isRel, _ := maybeRelErr(c.rule.outDir.String(), rspFile.String()); isRel {
  1040. panic(fmt.Errorf("FlagWithRspFileInputList rspfile %q must not be inside out dir %q",
  1041. rspFile.String(), c.rule.outDir.String()))
  1042. }
  1043. }
  1044. c.FlagWithArg(flag, c.PathForInput(rspFile))
  1045. return c
  1046. }
  1047. // String returns the command line.
  1048. func (c *RuleBuilderCommand) String() string {
  1049. return c.buf.String()
  1050. }
  1051. // RuleBuilderSboxProtoForTests takes the BuildParams for the manifest passed to RuleBuilder.Sbox()
  1052. // and returns sbox testproto generated by the RuleBuilder.
  1053. func RuleBuilderSboxProtoForTests(t *testing.T, params TestingBuildParams) *sbox_proto.Manifest {
  1054. t.Helper()
  1055. content := ContentFromFileRuleForTests(t, params)
  1056. manifest := sbox_proto.Manifest{}
  1057. err := proto.UnmarshalText(content, &manifest)
  1058. if err != nil {
  1059. t.Fatalf("failed to unmarshal manifest: %s", err.Error())
  1060. }
  1061. return &manifest
  1062. }
  1063. func ninjaNameEscape(s string) string {
  1064. b := []byte(s)
  1065. escaped := false
  1066. for i, c := range b {
  1067. valid := (c >= 'a' && c <= 'z') ||
  1068. (c >= 'A' && c <= 'Z') ||
  1069. (c >= '0' && c <= '9') ||
  1070. (c == '_') ||
  1071. (c == '-') ||
  1072. (c == '.')
  1073. if !valid {
  1074. b[i] = '_'
  1075. escaped = true
  1076. }
  1077. }
  1078. if escaped {
  1079. s = string(b)
  1080. }
  1081. return s
  1082. }
  1083. // hashSrcFiles returns a hash of the list of source files. It is used to ensure the command line
  1084. // or the sbox textproto manifest change even if the input files are not listed on the command line.
  1085. func hashSrcFiles(srcFiles Paths) string {
  1086. h := sha256.New()
  1087. srcFileList := strings.Join(srcFiles.Strings(), "\n")
  1088. h.Write([]byte(srcFileList))
  1089. return fmt.Sprintf("%x", h.Sum(nil))
  1090. }
  1091. // BuilderContextForTesting returns a BuilderContext for the given config that can be used for tests
  1092. // that need to call methods that take a BuilderContext.
  1093. func BuilderContextForTesting(config Config) BuilderContext {
  1094. pathCtx := PathContextForTesting(config)
  1095. return builderContextForTests{
  1096. PathContext: pathCtx,
  1097. }
  1098. }
  1099. type builderContextForTests struct {
  1100. PathContext
  1101. }
  1102. func (builderContextForTests) Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule {
  1103. return nil
  1104. }
  1105. func (builderContextForTests) Build(PackageContext, BuildParams) {}
  1106. func writeRspFileRule(ctx BuilderContext, rspFile WritablePath, paths Paths) {
  1107. buf := &strings.Builder{}
  1108. err := response.WriteRspFile(buf, paths.Strings())
  1109. if err != nil {
  1110. // There should never be I/O errors writing to a bytes.Buffer.
  1111. panic(err)
  1112. }
  1113. WriteFileRule(ctx, rspFile, buf.String())
  1114. }