rule_builder_test.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. // Copyright 2019 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. "fmt"
  17. "path/filepath"
  18. "regexp"
  19. "strings"
  20. "testing"
  21. "github.com/google/blueprint"
  22. "android/soong/shared"
  23. )
  24. func builderContext() BuilderContext {
  25. return BuilderContextForTesting(TestConfig("out", nil, "", map[string][]byte{
  26. "ld": nil,
  27. "a.o": nil,
  28. "b.o": nil,
  29. "cp": nil,
  30. "a": nil,
  31. "b": nil,
  32. "ls": nil,
  33. "ln": nil,
  34. "turbine": nil,
  35. "java": nil,
  36. "javac": nil,
  37. }))
  38. }
  39. func ExampleRuleBuilder() {
  40. ctx := builderContext()
  41. rule := NewRuleBuilder(pctx, ctx)
  42. rule.Command().
  43. Tool(PathForSource(ctx, "ld")).
  44. Inputs(PathsForTesting("a.o", "b.o")).
  45. FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
  46. rule.Command().Text("echo success")
  47. // To add the command to the build graph:
  48. // rule.Build("link", "link")
  49. fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
  50. fmt.Printf("tools: %q\n", rule.Tools())
  51. fmt.Printf("inputs: %q\n", rule.Inputs())
  52. fmt.Printf("outputs: %q\n", rule.Outputs())
  53. // Output:
  54. // commands: "ld a.o b.o -o out/linked && echo success"
  55. // tools: ["ld"]
  56. // inputs: ["a.o" "b.o"]
  57. // outputs: ["out/linked"]
  58. }
  59. func ExampleRuleBuilder_SymlinkOutputs() {
  60. ctx := builderContext()
  61. rule := NewRuleBuilder(pctx, ctx)
  62. rule.Command().
  63. Tool(PathForSource(ctx, "ln")).
  64. FlagWithInput("-s ", PathForTesting("a.o")).
  65. SymlinkOutput(PathForOutput(ctx, "a"))
  66. rule.Command().Text("cp out/a out/b").
  67. ImplicitSymlinkOutput(PathForOutput(ctx, "b"))
  68. fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
  69. fmt.Printf("tools: %q\n", rule.Tools())
  70. fmt.Printf("inputs: %q\n", rule.Inputs())
  71. fmt.Printf("outputs: %q\n", rule.Outputs())
  72. fmt.Printf("symlink_outputs: %q\n", rule.SymlinkOutputs())
  73. // Output:
  74. // commands: "ln -s a.o out/a && cp out/a out/b"
  75. // tools: ["ln"]
  76. // inputs: ["a.o"]
  77. // outputs: ["out/a" "out/b"]
  78. // symlink_outputs: ["out/a" "out/b"]
  79. }
  80. func ExampleRuleBuilder_Temporary() {
  81. ctx := builderContext()
  82. rule := NewRuleBuilder(pctx, ctx)
  83. rule.Command().
  84. Tool(PathForSource(ctx, "cp")).
  85. Input(PathForSource(ctx, "a")).
  86. Output(PathForOutput(ctx, "b"))
  87. rule.Command().
  88. Tool(PathForSource(ctx, "cp")).
  89. Input(PathForOutput(ctx, "b")).
  90. Output(PathForOutput(ctx, "c"))
  91. rule.Temporary(PathForOutput(ctx, "b"))
  92. fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
  93. fmt.Printf("tools: %q\n", rule.Tools())
  94. fmt.Printf("inputs: %q\n", rule.Inputs())
  95. fmt.Printf("outputs: %q\n", rule.Outputs())
  96. // Output:
  97. // commands: "cp a out/b && cp out/b out/c"
  98. // tools: ["cp"]
  99. // inputs: ["a"]
  100. // outputs: ["out/c"]
  101. }
  102. func ExampleRuleBuilder_DeleteTemporaryFiles() {
  103. ctx := builderContext()
  104. rule := NewRuleBuilder(pctx, ctx)
  105. rule.Command().
  106. Tool(PathForSource(ctx, "cp")).
  107. Input(PathForSource(ctx, "a")).
  108. Output(PathForOutput(ctx, "b"))
  109. rule.Command().
  110. Tool(PathForSource(ctx, "cp")).
  111. Input(PathForOutput(ctx, "b")).
  112. Output(PathForOutput(ctx, "c"))
  113. rule.Temporary(PathForOutput(ctx, "b"))
  114. rule.DeleteTemporaryFiles()
  115. fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
  116. fmt.Printf("tools: %q\n", rule.Tools())
  117. fmt.Printf("inputs: %q\n", rule.Inputs())
  118. fmt.Printf("outputs: %q\n", rule.Outputs())
  119. // Output:
  120. // commands: "cp a out/b && cp out/b out/c && rm -f out/b"
  121. // tools: ["cp"]
  122. // inputs: ["a"]
  123. // outputs: ["out/c"]
  124. }
  125. func ExampleRuleBuilder_Installs() {
  126. ctx := builderContext()
  127. rule := NewRuleBuilder(pctx, ctx)
  128. out := PathForOutput(ctx, "linked")
  129. rule.Command().
  130. Tool(PathForSource(ctx, "ld")).
  131. Inputs(PathsForTesting("a.o", "b.o")).
  132. FlagWithOutput("-o ", out)
  133. rule.Install(out, "/bin/linked")
  134. rule.Install(out, "/sbin/linked")
  135. fmt.Printf("rule.Installs().String() = %q\n", rule.Installs().String())
  136. // Output:
  137. // rule.Installs().String() = "out/linked:/bin/linked out/linked:/sbin/linked"
  138. }
  139. func ExampleRuleBuilderCommand() {
  140. ctx := builderContext()
  141. rule := NewRuleBuilder(pctx, ctx)
  142. // chained
  143. rule.Command().
  144. Tool(PathForSource(ctx, "ld")).
  145. Inputs(PathsForTesting("a.o", "b.o")).
  146. FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
  147. // unchained
  148. cmd := rule.Command()
  149. cmd.Tool(PathForSource(ctx, "ld"))
  150. cmd.Inputs(PathsForTesting("a.o", "b.o"))
  151. cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
  152. // mixed:
  153. cmd = rule.Command().Tool(PathForSource(ctx, "ld"))
  154. cmd.Inputs(PathsForTesting("a.o", "b.o"))
  155. cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
  156. }
  157. func ExampleRuleBuilderCommand_Flag() {
  158. ctx := builderContext()
  159. fmt.Println(NewRuleBuilder(pctx, ctx).Command().
  160. Tool(PathForSource(ctx, "ls")).Flag("-l"))
  161. // Output:
  162. // ls -l
  163. }
  164. func ExampleRuleBuilderCommand_Flags() {
  165. ctx := builderContext()
  166. fmt.Println(NewRuleBuilder(pctx, ctx).Command().
  167. Tool(PathForSource(ctx, "ls")).Flags([]string{"-l", "-a"}))
  168. // Output:
  169. // ls -l -a
  170. }
  171. func ExampleRuleBuilderCommand_FlagWithArg() {
  172. ctx := builderContext()
  173. fmt.Println(NewRuleBuilder(pctx, ctx).Command().
  174. Tool(PathForSource(ctx, "ls")).
  175. FlagWithArg("--sort=", "time"))
  176. // Output:
  177. // ls --sort=time
  178. }
  179. func ExampleRuleBuilderCommand_FlagForEachArg() {
  180. ctx := builderContext()
  181. fmt.Println(NewRuleBuilder(pctx, ctx).Command().
  182. Tool(PathForSource(ctx, "ls")).
  183. FlagForEachArg("--sort=", []string{"time", "size"}))
  184. // Output:
  185. // ls --sort=time --sort=size
  186. }
  187. func ExampleRuleBuilderCommand_FlagForEachInput() {
  188. ctx := builderContext()
  189. fmt.Println(NewRuleBuilder(pctx, ctx).Command().
  190. Tool(PathForSource(ctx, "turbine")).
  191. FlagForEachInput("--classpath ", PathsForTesting("a.jar", "b.jar")))
  192. // Output:
  193. // turbine --classpath a.jar --classpath b.jar
  194. }
  195. func ExampleRuleBuilderCommand_FlagWithInputList() {
  196. ctx := builderContext()
  197. fmt.Println(NewRuleBuilder(pctx, ctx).Command().
  198. Tool(PathForSource(ctx, "java")).
  199. FlagWithInputList("-classpath=", PathsForTesting("a.jar", "b.jar"), ":"))
  200. // Output:
  201. // java -classpath=a.jar:b.jar
  202. }
  203. func ExampleRuleBuilderCommand_FlagWithInput() {
  204. ctx := builderContext()
  205. fmt.Println(NewRuleBuilder(pctx, ctx).Command().
  206. Tool(PathForSource(ctx, "java")).
  207. FlagWithInput("-classpath=", PathForSource(ctx, "a")))
  208. // Output:
  209. // java -classpath=a
  210. }
  211. func ExampleRuleBuilderCommand_FlagWithList() {
  212. ctx := builderContext()
  213. fmt.Println(NewRuleBuilder(pctx, ctx).Command().
  214. Tool(PathForSource(ctx, "ls")).
  215. FlagWithList("--sort=", []string{"time", "size"}, ","))
  216. // Output:
  217. // ls --sort=time,size
  218. }
  219. func ExampleRuleBuilderCommand_FlagWithRspFileInputList() {
  220. ctx := builderContext()
  221. fmt.Println(NewRuleBuilder(pctx, ctx).Command().
  222. Tool(PathForSource(ctx, "javac")).
  223. FlagWithRspFileInputList("@", PathForOutput(ctx, "foo.rsp"), PathsForTesting("a.java", "b.java")).
  224. String())
  225. // Output:
  226. // javac @out/foo.rsp
  227. }
  228. func ExampleRuleBuilderCommand_String() {
  229. ctx := builderContext()
  230. fmt.Println(NewRuleBuilder(pctx, ctx).Command().
  231. Text("FOO=foo").
  232. Text("echo $FOO").
  233. String())
  234. // Output:
  235. // FOO=foo echo $FOO
  236. }
  237. func TestRuleBuilder(t *testing.T) {
  238. fs := map[string][]byte{
  239. "dep_fixer": nil,
  240. "input": nil,
  241. "Implicit": nil,
  242. "Input": nil,
  243. "OrderOnly": nil,
  244. "OrderOnlys": nil,
  245. "Tool": nil,
  246. "input2": nil,
  247. "tool2": nil,
  248. "input3": nil,
  249. }
  250. pathCtx := PathContextForTesting(TestConfig("out_local", nil, "", fs))
  251. ctx := builderContextForTests{
  252. PathContext: pathCtx,
  253. }
  254. addCommands := func(rule *RuleBuilder) {
  255. cmd := rule.Command().
  256. DepFile(PathForOutput(ctx, "module/DepFile")).
  257. Flag("Flag").
  258. FlagWithArg("FlagWithArg=", "arg").
  259. FlagWithDepFile("FlagWithDepFile=", PathForOutput(ctx, "module/depfile")).
  260. FlagWithInput("FlagWithInput=", PathForSource(ctx, "input")).
  261. FlagWithOutput("FlagWithOutput=", PathForOutput(ctx, "module/output")).
  262. FlagWithRspFileInputList("FlagWithRspFileInputList=", PathForOutput(ctx, "rsp"),
  263. Paths{
  264. PathForSource(ctx, "RspInput"),
  265. PathForOutput(ctx, "other/RspOutput2"),
  266. }).
  267. Implicit(PathForSource(ctx, "Implicit")).
  268. ImplicitDepFile(PathForOutput(ctx, "module/ImplicitDepFile")).
  269. ImplicitOutput(PathForOutput(ctx, "module/ImplicitOutput")).
  270. Input(PathForSource(ctx, "Input")).
  271. Output(PathForOutput(ctx, "module/Output")).
  272. OrderOnly(PathForSource(ctx, "OrderOnly")).
  273. SymlinkOutput(PathForOutput(ctx, "module/SymlinkOutput")).
  274. ImplicitSymlinkOutput(PathForOutput(ctx, "module/ImplicitSymlinkOutput")).
  275. Text("Text").
  276. Tool(PathForSource(ctx, "Tool"))
  277. rule.Command().
  278. Text("command2").
  279. DepFile(PathForOutput(ctx, "module/depfile2")).
  280. Input(PathForSource(ctx, "input2")).
  281. Output(PathForOutput(ctx, "module/output2")).
  282. OrderOnlys(PathsForSource(ctx, []string{"OrderOnlys"})).
  283. Tool(PathForSource(ctx, "tool2"))
  284. // Test updates to the first command after the second command has been started
  285. cmd.Text("after command2")
  286. // Test updating a command when the previous update did not replace the cmd variable
  287. cmd.Text("old cmd")
  288. // Test a command that uses the output of a previous command as an input
  289. rule.Command().
  290. Text("command3").
  291. Input(PathForSource(ctx, "input3")).
  292. Input(PathForOutput(ctx, "module/output2")).
  293. Output(PathForOutput(ctx, "module/output3")).
  294. Text(cmd.PathForInput(PathForSource(ctx, "input3"))).
  295. Text(cmd.PathForOutput(PathForOutput(ctx, "module/output2")))
  296. }
  297. wantInputs := PathsForSource(ctx, []string{"Implicit", "Input", "input", "input2", "input3"})
  298. wantRspFileInputs := Paths{PathForSource(ctx, "RspInput"),
  299. PathForOutput(ctx, "other/RspOutput2")}
  300. wantOutputs := PathsForOutput(ctx, []string{
  301. "module/ImplicitOutput", "module/ImplicitSymlinkOutput", "module/Output", "module/SymlinkOutput",
  302. "module/output", "module/output2", "module/output3"})
  303. wantDepFiles := PathsForOutput(ctx, []string{
  304. "module/DepFile", "module/depfile", "module/ImplicitDepFile", "module/depfile2"})
  305. wantTools := PathsForSource(ctx, []string{"Tool", "tool2"})
  306. wantOrderOnlys := PathsForSource(ctx, []string{"OrderOnly", "OrderOnlys"})
  307. wantSymlinkOutputs := PathsForOutput(ctx, []string{
  308. "module/ImplicitSymlinkOutput", "module/SymlinkOutput"})
  309. t.Run("normal", func(t *testing.T) {
  310. rule := NewRuleBuilder(pctx, ctx)
  311. addCommands(rule)
  312. wantCommands := []string{
  313. "out_local/module/DepFile Flag FlagWithArg=arg FlagWithDepFile=out_local/module/depfile " +
  314. "FlagWithInput=input FlagWithOutput=out_local/module/output FlagWithRspFileInputList=out_local/rsp " +
  315. "Input out_local/module/Output out_local/module/SymlinkOutput Text Tool after command2 old cmd",
  316. "command2 out_local/module/depfile2 input2 out_local/module/output2 tool2",
  317. "command3 input3 out_local/module/output2 out_local/module/output3 input3 out_local/module/output2",
  318. }
  319. wantDepMergerCommand := "out_local/host/" + ctx.Config().PrebuiltOS() + "/bin/dep_fixer " +
  320. "out_local/module/DepFile out_local/module/depfile out_local/module/ImplicitDepFile out_local/module/depfile2"
  321. AssertDeepEquals(t, "rule.Commands()", wantCommands, rule.Commands())
  322. AssertDeepEquals(t, "rule.Inputs()", wantInputs, rule.Inputs())
  323. AssertDeepEquals(t, "rule.RspfileInputs()", wantRspFileInputs, rule.RspFileInputs())
  324. AssertDeepEquals(t, "rule.Outputs()", wantOutputs, rule.Outputs())
  325. AssertDeepEquals(t, "rule.SymlinkOutputs()", wantSymlinkOutputs, rule.SymlinkOutputs())
  326. AssertDeepEquals(t, "rule.DepFiles()", wantDepFiles, rule.DepFiles())
  327. AssertDeepEquals(t, "rule.Tools()", wantTools, rule.Tools())
  328. AssertDeepEquals(t, "rule.OrderOnlys()", wantOrderOnlys, rule.OrderOnlys())
  329. AssertSame(t, "rule.depFileMergerCmd()", wantDepMergerCommand, rule.depFileMergerCmd(rule.DepFiles()).String())
  330. })
  331. t.Run("sbox", func(t *testing.T) {
  332. rule := NewRuleBuilder(pctx, ctx).Sbox(PathForOutput(ctx, "module"),
  333. PathForOutput(ctx, "sbox.textproto"))
  334. addCommands(rule)
  335. wantCommands := []string{
  336. "__SBOX_SANDBOX_DIR__/out/DepFile Flag FlagWithArg=arg FlagWithDepFile=__SBOX_SANDBOX_DIR__/out/depfile " +
  337. "FlagWithInput=input FlagWithOutput=__SBOX_SANDBOX_DIR__/out/output " +
  338. "FlagWithRspFileInputList=out_local/rsp Input __SBOX_SANDBOX_DIR__/out/Output " +
  339. "__SBOX_SANDBOX_DIR__/out/SymlinkOutput Text Tool after command2 old cmd",
  340. "command2 __SBOX_SANDBOX_DIR__/out/depfile2 input2 __SBOX_SANDBOX_DIR__/out/output2 tool2",
  341. "command3 input3 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/out/output3 input3 __SBOX_SANDBOX_DIR__/out/output2",
  342. }
  343. wantDepMergerCommand := "out_local/host/" + ctx.Config().PrebuiltOS() + "/bin/dep_fixer __SBOX_SANDBOX_DIR__/out/DepFile __SBOX_SANDBOX_DIR__/out/depfile __SBOX_SANDBOX_DIR__/out/ImplicitDepFile __SBOX_SANDBOX_DIR__/out/depfile2"
  344. AssertDeepEquals(t, "rule.Commands()", wantCommands, rule.Commands())
  345. AssertDeepEquals(t, "rule.Inputs()", wantInputs, rule.Inputs())
  346. AssertDeepEquals(t, "rule.RspfileInputs()", wantRspFileInputs, rule.RspFileInputs())
  347. AssertDeepEquals(t, "rule.Outputs()", wantOutputs, rule.Outputs())
  348. AssertDeepEquals(t, "rule.SymlinkOutputs()", wantSymlinkOutputs, rule.SymlinkOutputs())
  349. AssertDeepEquals(t, "rule.DepFiles()", wantDepFiles, rule.DepFiles())
  350. AssertDeepEquals(t, "rule.Tools()", wantTools, rule.Tools())
  351. AssertDeepEquals(t, "rule.OrderOnlys()", wantOrderOnlys, rule.OrderOnlys())
  352. AssertSame(t, "rule.depFileMergerCmd()", wantDepMergerCommand, rule.depFileMergerCmd(rule.DepFiles()).String())
  353. })
  354. t.Run("sbox tools", func(t *testing.T) {
  355. rule := NewRuleBuilder(pctx, ctx).Sbox(PathForOutput(ctx, "module"),
  356. PathForOutput(ctx, "sbox.textproto")).SandboxTools()
  357. addCommands(rule)
  358. wantCommands := []string{
  359. "__SBOX_SANDBOX_DIR__/out/DepFile Flag FlagWithArg=arg FlagWithDepFile=__SBOX_SANDBOX_DIR__/out/depfile " +
  360. "FlagWithInput=input FlagWithOutput=__SBOX_SANDBOX_DIR__/out/output " +
  361. "FlagWithRspFileInputList=out_local/rsp Input __SBOX_SANDBOX_DIR__/out/Output " +
  362. "__SBOX_SANDBOX_DIR__/out/SymlinkOutput Text __SBOX_SANDBOX_DIR__/tools/src/Tool after command2 old cmd",
  363. "command2 __SBOX_SANDBOX_DIR__/out/depfile2 input2 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/tools/src/tool2",
  364. "command3 input3 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/out/output3 input3 __SBOX_SANDBOX_DIR__/out/output2",
  365. }
  366. wantDepMergerCommand := "__SBOX_SANDBOX_DIR__/tools/out/bin/dep_fixer __SBOX_SANDBOX_DIR__/out/DepFile __SBOX_SANDBOX_DIR__/out/depfile __SBOX_SANDBOX_DIR__/out/ImplicitDepFile __SBOX_SANDBOX_DIR__/out/depfile2"
  367. AssertDeepEquals(t, "rule.Commands()", wantCommands, rule.Commands())
  368. AssertDeepEquals(t, "rule.Inputs()", wantInputs, rule.Inputs())
  369. AssertDeepEquals(t, "rule.RspfileInputs()", wantRspFileInputs, rule.RspFileInputs())
  370. AssertDeepEquals(t, "rule.Outputs()", wantOutputs, rule.Outputs())
  371. AssertDeepEquals(t, "rule.SymlinkOutputs()", wantSymlinkOutputs, rule.SymlinkOutputs())
  372. AssertDeepEquals(t, "rule.DepFiles()", wantDepFiles, rule.DepFiles())
  373. AssertDeepEquals(t, "rule.Tools()", wantTools, rule.Tools())
  374. AssertDeepEquals(t, "rule.OrderOnlys()", wantOrderOnlys, rule.OrderOnlys())
  375. AssertSame(t, "rule.depFileMergerCmd()", wantDepMergerCommand, rule.depFileMergerCmd(rule.DepFiles()).String())
  376. })
  377. t.Run("sbox inputs", func(t *testing.T) {
  378. rule := NewRuleBuilder(pctx, ctx).Sbox(PathForOutput(ctx, "module"),
  379. PathForOutput(ctx, "sbox.textproto")).SandboxInputs()
  380. addCommands(rule)
  381. wantCommands := []string{
  382. "__SBOX_SANDBOX_DIR__/out/DepFile Flag FlagWithArg=arg FlagWithDepFile=__SBOX_SANDBOX_DIR__/out/depfile " +
  383. "FlagWithInput=input FlagWithOutput=__SBOX_SANDBOX_DIR__/out/output " +
  384. "FlagWithRspFileInputList=__SBOX_SANDBOX_DIR__/out/rsp Input __SBOX_SANDBOX_DIR__/out/Output " +
  385. "__SBOX_SANDBOX_DIR__/out/SymlinkOutput Text __SBOX_SANDBOX_DIR__/tools/src/Tool after command2 old cmd",
  386. "command2 __SBOX_SANDBOX_DIR__/out/depfile2 input2 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/tools/src/tool2",
  387. "command3 input3 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/out/output3 input3 __SBOX_SANDBOX_DIR__/out/output2",
  388. }
  389. wantDepMergerCommand := "__SBOX_SANDBOX_DIR__/tools/out/bin/dep_fixer __SBOX_SANDBOX_DIR__/out/DepFile __SBOX_SANDBOX_DIR__/out/depfile __SBOX_SANDBOX_DIR__/out/ImplicitDepFile __SBOX_SANDBOX_DIR__/out/depfile2"
  390. AssertDeepEquals(t, "rule.Commands()", wantCommands, rule.Commands())
  391. AssertDeepEquals(t, "rule.Inputs()", wantInputs, rule.Inputs())
  392. AssertDeepEquals(t, "rule.RspfileInputs()", wantRspFileInputs, rule.RspFileInputs())
  393. AssertDeepEquals(t, "rule.Outputs()", wantOutputs, rule.Outputs())
  394. AssertDeepEquals(t, "rule.SymlinkOutputs()", wantSymlinkOutputs, rule.SymlinkOutputs())
  395. AssertDeepEquals(t, "rule.DepFiles()", wantDepFiles, rule.DepFiles())
  396. AssertDeepEquals(t, "rule.Tools()", wantTools, rule.Tools())
  397. AssertDeepEquals(t, "rule.OrderOnlys()", wantOrderOnlys, rule.OrderOnlys())
  398. AssertSame(t, "rule.depFileMergerCmd()", wantDepMergerCommand, rule.depFileMergerCmd(rule.DepFiles()).String())
  399. })
  400. }
  401. func testRuleBuilderFactory() Module {
  402. module := &testRuleBuilderModule{}
  403. module.AddProperties(&module.properties)
  404. InitAndroidModule(module)
  405. return module
  406. }
  407. type testRuleBuilderModule struct {
  408. ModuleBase
  409. properties struct {
  410. Srcs []string
  411. Restat bool
  412. Sbox bool
  413. Sbox_inputs bool
  414. }
  415. }
  416. func (t *testRuleBuilderModule) GenerateAndroidBuildActions(ctx ModuleContext) {
  417. in := PathsForSource(ctx, t.properties.Srcs)
  418. out := PathForModuleOut(ctx, "gen", ctx.ModuleName())
  419. outDep := PathForModuleOut(ctx, "gen", ctx.ModuleName()+".d")
  420. outDir := PathForModuleOut(ctx, "gen")
  421. rspFile := PathForModuleOut(ctx, "rsp")
  422. rspFile2 := PathForModuleOut(ctx, "rsp2")
  423. rspFileContents := PathsForSource(ctx, []string{"rsp_in"})
  424. rspFileContents2 := PathsForSource(ctx, []string{"rsp_in2"})
  425. manifestPath := PathForModuleOut(ctx, "sbox.textproto")
  426. testRuleBuilder_Build(ctx, in, out, outDep, outDir, manifestPath, t.properties.Restat,
  427. t.properties.Sbox, t.properties.Sbox_inputs, rspFile, rspFileContents,
  428. rspFile2, rspFileContents2)
  429. }
  430. type testRuleBuilderSingleton struct{}
  431. func testRuleBuilderSingletonFactory() Singleton {
  432. return &testRuleBuilderSingleton{}
  433. }
  434. func (t *testRuleBuilderSingleton) GenerateBuildActions(ctx SingletonContext) {
  435. in := PathForSource(ctx, "bar")
  436. out := PathForOutput(ctx, "singleton/gen/baz")
  437. outDep := PathForOutput(ctx, "singleton/gen/baz.d")
  438. outDir := PathForOutput(ctx, "singleton/gen")
  439. rspFile := PathForOutput(ctx, "singleton/rsp")
  440. rspFile2 := PathForOutput(ctx, "singleton/rsp2")
  441. rspFileContents := PathsForSource(ctx, []string{"rsp_in"})
  442. rspFileContents2 := PathsForSource(ctx, []string{"rsp_in2"})
  443. manifestPath := PathForOutput(ctx, "singleton/sbox.textproto")
  444. testRuleBuilder_Build(ctx, Paths{in}, out, outDep, outDir, manifestPath, true, false, false,
  445. rspFile, rspFileContents, rspFile2, rspFileContents2)
  446. }
  447. func testRuleBuilder_Build(ctx BuilderContext, in Paths, out, outDep, outDir, manifestPath WritablePath,
  448. restat, sbox, sboxInputs bool,
  449. rspFile WritablePath, rspFileContents Paths, rspFile2 WritablePath, rspFileContents2 Paths) {
  450. rule := NewRuleBuilder(pctx, ctx)
  451. if sbox {
  452. rule.Sbox(outDir, manifestPath)
  453. if sboxInputs {
  454. rule.SandboxInputs()
  455. }
  456. }
  457. rule.Command().
  458. Tool(PathForSource(ctx, "cp")).
  459. Inputs(in).
  460. Output(out).
  461. ImplicitDepFile(outDep).
  462. FlagWithRspFileInputList("@", rspFile, rspFileContents).
  463. FlagWithRspFileInputList("@", rspFile2, rspFileContents2)
  464. if restat {
  465. rule.Restat()
  466. }
  467. rule.Build("rule", "desc")
  468. }
  469. var prepareForRuleBuilderTest = FixtureRegisterWithContext(func(ctx RegistrationContext) {
  470. ctx.RegisterModuleType("rule_builder_test", testRuleBuilderFactory)
  471. ctx.RegisterSingletonType("rule_builder_test", testRuleBuilderSingletonFactory)
  472. })
  473. func TestRuleBuilder_Build(t *testing.T) {
  474. fs := MockFS{
  475. "bar": nil,
  476. "cp": nil,
  477. }
  478. bp := `
  479. rule_builder_test {
  480. name: "foo",
  481. srcs: ["bar"],
  482. restat: true,
  483. }
  484. rule_builder_test {
  485. name: "foo_sbox",
  486. srcs: ["bar"],
  487. sbox: true,
  488. }
  489. rule_builder_test {
  490. name: "foo_sbox_inputs",
  491. srcs: ["bar"],
  492. sbox: true,
  493. sbox_inputs: true,
  494. }
  495. `
  496. result := GroupFixturePreparers(
  497. prepareForRuleBuilderTest,
  498. FixtureWithRootAndroidBp(bp),
  499. fs.AddToFixture(),
  500. ).RunTest(t)
  501. check := func(t *testing.T, params TestingBuildParams, rspFile2Params TestingBuildParams,
  502. wantCommand, wantOutput, wantDepfile, wantRspFile, wantRspFile2 string,
  503. wantRestat bool, extraImplicits, extraCmdDeps []string) {
  504. t.Helper()
  505. command := params.RuleParams.Command
  506. re := regexp.MustCompile(" # hash of input list: [a-z0-9]*$")
  507. command = re.ReplaceAllLiteralString(command, "")
  508. AssertStringEquals(t, "RuleParams.Command", wantCommand, command)
  509. wantDeps := append([]string{"cp"}, extraCmdDeps...)
  510. AssertArrayString(t, "RuleParams.CommandDeps", wantDeps, params.RuleParams.CommandDeps)
  511. AssertBoolEquals(t, "RuleParams.Restat", wantRestat, params.RuleParams.Restat)
  512. wantInputs := []string{"rsp_in"}
  513. AssertArrayString(t, "Inputs", wantInputs, params.Inputs.Strings())
  514. wantImplicits := append([]string{"bar"}, extraImplicits...)
  515. // The second rsp file and the files listed in it should be in implicits
  516. wantImplicits = append(wantImplicits, "rsp_in2", wantRspFile2)
  517. AssertPathsRelativeToTopEquals(t, "Implicits", wantImplicits, params.Implicits)
  518. wantRspFileContent := "$in"
  519. AssertStringEquals(t, "RspfileContent", wantRspFileContent, params.RuleParams.RspfileContent)
  520. AssertStringEquals(t, "Rspfile", wantRspFile, params.RuleParams.Rspfile)
  521. AssertPathRelativeToTopEquals(t, "Output", wantOutput, params.Output)
  522. if len(params.ImplicitOutputs) != 0 {
  523. t.Errorf("want ImplicitOutputs = [], got %q", params.ImplicitOutputs.Strings())
  524. }
  525. AssertPathRelativeToTopEquals(t, "Depfile", wantDepfile, params.Depfile)
  526. if params.Deps != blueprint.DepsGCC {
  527. t.Errorf("want Deps = %q, got %q", blueprint.DepsGCC, params.Deps)
  528. }
  529. rspFile2Content := ContentFromFileRuleForTests(t, rspFile2Params)
  530. AssertStringEquals(t, "rspFile2 content", "rsp_in2\n", rspFile2Content)
  531. }
  532. t.Run("module", func(t *testing.T) {
  533. outFile := "out/soong/.intermediates/foo/gen/foo"
  534. rspFile := "out/soong/.intermediates/foo/rsp"
  535. rspFile2 := "out/soong/.intermediates/foo/rsp2"
  536. module := result.ModuleForTests("foo", "")
  537. check(t, module.Rule("rule"), module.Output(rspFile2),
  538. "cp bar "+outFile+" @"+rspFile+" @"+rspFile2,
  539. outFile, outFile+".d", rspFile, rspFile2, true, nil, nil)
  540. })
  541. t.Run("sbox", func(t *testing.T) {
  542. outDir := "out/soong/.intermediates/foo_sbox"
  543. outFile := filepath.Join(outDir, "gen/foo_sbox")
  544. depFile := filepath.Join(outDir, "gen/foo_sbox.d")
  545. rspFile := filepath.Join(outDir, "rsp")
  546. rspFile2 := filepath.Join(outDir, "rsp2")
  547. manifest := filepath.Join(outDir, "sbox.textproto")
  548. sbox := filepath.Join("out", "soong", "host", result.Config.PrebuiltOS(), "bin/sbox")
  549. sandboxPath := shared.TempDirForOutDir("out/soong")
  550. cmd := `rm -rf ` + outDir + `/gen && ` +
  551. sbox + ` --sandbox-path ` + sandboxPath + ` --manifest ` + manifest
  552. module := result.ModuleForTests("foo_sbox", "")
  553. check(t, module.Output("gen/foo_sbox"), module.Output(rspFile2),
  554. cmd, outFile, depFile, rspFile, rspFile2, false, []string{manifest}, []string{sbox})
  555. })
  556. t.Run("sbox_inputs", func(t *testing.T) {
  557. outDir := "out/soong/.intermediates/foo_sbox_inputs"
  558. outFile := filepath.Join(outDir, "gen/foo_sbox_inputs")
  559. depFile := filepath.Join(outDir, "gen/foo_sbox_inputs.d")
  560. rspFile := filepath.Join(outDir, "rsp")
  561. rspFile2 := filepath.Join(outDir, "rsp2")
  562. manifest := filepath.Join(outDir, "sbox.textproto")
  563. sbox := filepath.Join("out", "soong", "host", result.Config.PrebuiltOS(), "bin/sbox")
  564. sandboxPath := shared.TempDirForOutDir("out/soong")
  565. cmd := `rm -rf ` + outDir + `/gen && ` +
  566. sbox + ` --sandbox-path ` + sandboxPath + ` --manifest ` + manifest
  567. module := result.ModuleForTests("foo_sbox_inputs", "")
  568. check(t, module.Output("gen/foo_sbox_inputs"), module.Output(rspFile2),
  569. cmd, outFile, depFile, rspFile, rspFile2, false, []string{manifest}, []string{sbox})
  570. })
  571. t.Run("singleton", func(t *testing.T) {
  572. outFile := filepath.Join("out/soong/singleton/gen/baz")
  573. rspFile := filepath.Join("out/soong/singleton/rsp")
  574. rspFile2 := filepath.Join("out/soong/singleton/rsp2")
  575. singleton := result.SingletonForTests("rule_builder_test")
  576. check(t, singleton.Rule("rule"), singleton.Output(rspFile2),
  577. "cp bar "+outFile+" @"+rspFile+" @"+rspFile2,
  578. outFile, outFile+".d", rspFile, rspFile2, true, nil, nil)
  579. })
  580. }
  581. func TestRuleBuilderHashInputs(t *testing.T) {
  582. // The basic idea here is to verify that the command (in the case of a
  583. // non-sbox rule) or the sbox textproto manifest contain a hash of the
  584. // inputs.
  585. // By including a hash of the inputs, we cause the rule to re-run if
  586. // the list of inputs changes because the command line or a dependency
  587. // changes.
  588. bp := `
  589. rule_builder_test {
  590. name: "hash0",
  591. srcs: ["in1.txt", "in2.txt"],
  592. }
  593. rule_builder_test {
  594. name: "hash0_sbox",
  595. srcs: ["in1.txt", "in2.txt"],
  596. sbox: true,
  597. }
  598. rule_builder_test {
  599. name: "hash1",
  600. srcs: ["in1.txt", "in2.txt", "in3.txt"],
  601. }
  602. rule_builder_test {
  603. name: "hash1_sbox",
  604. srcs: ["in1.txt", "in2.txt", "in3.txt"],
  605. sbox: true,
  606. }
  607. `
  608. testcases := []struct {
  609. name string
  610. expectedHash string
  611. }{
  612. {
  613. name: "hash0",
  614. // sha256 value obtained from: echo -en 'in1.txt\nin2.txt' | sha256sum
  615. expectedHash: "18da75b9b1cc74b09e365b4ca2e321b5d618f438cc632b387ad9dc2ab4b20e9d",
  616. },
  617. {
  618. name: "hash1",
  619. // sha256 value obtained from: echo -en 'in1.txt\nin2.txt\nin3.txt' | sha256sum
  620. expectedHash: "a38d432a4b19df93140e1f1fe26c97ff0387dae01fe506412b47208f0595fb45",
  621. },
  622. }
  623. result := GroupFixturePreparers(
  624. prepareForRuleBuilderTest,
  625. FixtureWithRootAndroidBp(bp),
  626. ).RunTest(t)
  627. for _, test := range testcases {
  628. t.Run(test.name, func(t *testing.T) {
  629. t.Run("sbox", func(t *testing.T) {
  630. gen := result.ModuleForTests(test.name+"_sbox", "")
  631. manifest := RuleBuilderSboxProtoForTests(t, gen.Output("sbox.textproto"))
  632. hash := manifest.Commands[0].GetInputHash()
  633. AssertStringEquals(t, "hash", test.expectedHash, hash)
  634. })
  635. t.Run("", func(t *testing.T) {
  636. gen := result.ModuleForTests(test.name+"", "")
  637. command := gen.Output("gen/" + test.name).RuleParams.Command
  638. if g, w := command, " # hash of input list: "+test.expectedHash; !strings.HasSuffix(g, w) {
  639. t.Errorf("Expected command line to end with %q, got %q", w, g)
  640. }
  641. })
  642. })
  643. }
  644. }