rule_builder_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. }
  414. }
  415. func (t *testRuleBuilderModule) GenerateAndroidBuildActions(ctx ModuleContext) {
  416. in := PathsForSource(ctx, t.properties.Srcs)
  417. out := PathForModuleOut(ctx, "gen", ctx.ModuleName())
  418. outDep := PathForModuleOut(ctx, "gen", ctx.ModuleName()+".d")
  419. outDir := PathForModuleOut(ctx, "gen")
  420. manifestPath := PathForModuleOut(ctx, "sbox.textproto")
  421. testRuleBuilder_Build(ctx, in, out, outDep, outDir, manifestPath, t.properties.Restat, t.properties.Sbox)
  422. }
  423. type testRuleBuilderSingleton struct{}
  424. func testRuleBuilderSingletonFactory() Singleton {
  425. return &testRuleBuilderSingleton{}
  426. }
  427. func (t *testRuleBuilderSingleton) GenerateBuildActions(ctx SingletonContext) {
  428. in := PathForSource(ctx, "bar")
  429. out := PathForOutput(ctx, "singleton/gen/baz")
  430. outDep := PathForOutput(ctx, "singleton/gen/baz.d")
  431. outDir := PathForOutput(ctx, "singleton/gen")
  432. manifestPath := PathForOutput(ctx, "singleton/sbox.textproto")
  433. testRuleBuilder_Build(ctx, Paths{in}, out, outDep, outDir, manifestPath, true, false)
  434. }
  435. func testRuleBuilder_Build(ctx BuilderContext, in Paths, out, outDep, outDir, manifestPath WritablePath, restat, sbox bool) {
  436. rule := NewRuleBuilder(pctx, ctx)
  437. if sbox {
  438. rule.Sbox(outDir, manifestPath)
  439. }
  440. rule.Command().Tool(PathForSource(ctx, "cp")).Inputs(in).Output(out).ImplicitDepFile(outDep)
  441. if restat {
  442. rule.Restat()
  443. }
  444. rule.Build("rule", "desc")
  445. }
  446. var prepareForRuleBuilderTest = FixtureRegisterWithContext(func(ctx RegistrationContext) {
  447. ctx.RegisterModuleType("rule_builder_test", testRuleBuilderFactory)
  448. ctx.RegisterSingletonType("rule_builder_test", testRuleBuilderSingletonFactory)
  449. })
  450. func TestRuleBuilder_Build(t *testing.T) {
  451. fs := MockFS{
  452. "bar": nil,
  453. "cp": nil,
  454. }
  455. bp := `
  456. rule_builder_test {
  457. name: "foo",
  458. srcs: ["bar"],
  459. restat: true,
  460. }
  461. rule_builder_test {
  462. name: "foo_sbox",
  463. srcs: ["bar"],
  464. sbox: true,
  465. }
  466. `
  467. result := GroupFixturePreparers(
  468. prepareForRuleBuilderTest,
  469. FixtureWithRootAndroidBp(bp),
  470. fs.AddToFixture(),
  471. ).RunTest(t)
  472. check := func(t *testing.T, params TestingBuildParams, wantCommand, wantOutput, wantDepfile string, wantRestat bool, extraImplicits, extraCmdDeps []string) {
  473. t.Helper()
  474. command := params.RuleParams.Command
  475. re := regexp.MustCompile(" # hash of input list: [a-z0-9]*$")
  476. command = re.ReplaceAllLiteralString(command, "")
  477. AssertStringEquals(t, "RuleParams.Command", wantCommand, command)
  478. wantDeps := append([]string{"cp"}, extraCmdDeps...)
  479. AssertArrayString(t, "RuleParams.CommandDeps", wantDeps, params.RuleParams.CommandDeps)
  480. AssertBoolEquals(t, "RuleParams.Restat", wantRestat, params.RuleParams.Restat)
  481. wantImplicits := append([]string{"bar"}, extraImplicits...)
  482. AssertPathsRelativeToTopEquals(t, "Implicits", wantImplicits, params.Implicits)
  483. AssertPathRelativeToTopEquals(t, "Output", wantOutput, params.Output)
  484. if len(params.ImplicitOutputs) != 0 {
  485. t.Errorf("want ImplicitOutputs = [], got %q", params.ImplicitOutputs.Strings())
  486. }
  487. AssertPathRelativeToTopEquals(t, "Depfile", wantDepfile, params.Depfile)
  488. if params.Deps != blueprint.DepsGCC {
  489. t.Errorf("want Deps = %q, got %q", blueprint.DepsGCC, params.Deps)
  490. }
  491. }
  492. t.Run("module", func(t *testing.T) {
  493. outFile := "out/soong/.intermediates/foo/gen/foo"
  494. check(t, result.ModuleForTests("foo", "").Rule("rule").RelativeToTop(),
  495. "cp bar "+outFile,
  496. outFile, outFile+".d", true, nil, nil)
  497. })
  498. t.Run("sbox", func(t *testing.T) {
  499. outDir := "out/soong/.intermediates/foo_sbox"
  500. outFile := filepath.Join(outDir, "gen/foo_sbox")
  501. depFile := filepath.Join(outDir, "gen/foo_sbox.d")
  502. manifest := filepath.Join(outDir, "sbox.textproto")
  503. sbox := filepath.Join("out", "soong", "host", result.Config.PrebuiltOS(), "bin/sbox")
  504. sandboxPath := shared.TempDirForOutDir("out/soong")
  505. cmd := `rm -rf ` + outDir + `/gen && ` +
  506. sbox + ` --sandbox-path ` + sandboxPath + ` --manifest ` + manifest
  507. check(t, result.ModuleForTests("foo_sbox", "").Output("gen/foo_sbox").RelativeToTop(),
  508. cmd, outFile, depFile, false, []string{manifest}, []string{sbox})
  509. })
  510. t.Run("singleton", func(t *testing.T) {
  511. outFile := filepath.Join("out/soong/singleton/gen/baz")
  512. check(t, result.SingletonForTests("rule_builder_test").Rule("rule").RelativeToTop(),
  513. "cp bar "+outFile, outFile, outFile+".d", true, nil, nil)
  514. })
  515. }
  516. func TestRuleBuilderHashInputs(t *testing.T) {
  517. // The basic idea here is to verify that the command (in the case of a
  518. // non-sbox rule) or the sbox textproto manifest contain a hash of the
  519. // inputs.
  520. // By including a hash of the inputs, we cause the rule to re-run if
  521. // the list of inputs changes because the command line or a dependency
  522. // changes.
  523. bp := `
  524. rule_builder_test {
  525. name: "hash0",
  526. srcs: ["in1.txt", "in2.txt"],
  527. }
  528. rule_builder_test {
  529. name: "hash0_sbox",
  530. srcs: ["in1.txt", "in2.txt"],
  531. sbox: true,
  532. }
  533. rule_builder_test {
  534. name: "hash1",
  535. srcs: ["in1.txt", "in2.txt", "in3.txt"],
  536. }
  537. rule_builder_test {
  538. name: "hash1_sbox",
  539. srcs: ["in1.txt", "in2.txt", "in3.txt"],
  540. sbox: true,
  541. }
  542. `
  543. testcases := []struct {
  544. name string
  545. expectedHash string
  546. }{
  547. {
  548. name: "hash0",
  549. // sha256 value obtained from: echo -en 'in1.txt\nin2.txt' | sha256sum
  550. expectedHash: "18da75b9b1cc74b09e365b4ca2e321b5d618f438cc632b387ad9dc2ab4b20e9d",
  551. },
  552. {
  553. name: "hash1",
  554. // sha256 value obtained from: echo -en 'in1.txt\nin2.txt\nin3.txt' | sha256sum
  555. expectedHash: "a38d432a4b19df93140e1f1fe26c97ff0387dae01fe506412b47208f0595fb45",
  556. },
  557. }
  558. result := GroupFixturePreparers(
  559. prepareForRuleBuilderTest,
  560. FixtureWithRootAndroidBp(bp),
  561. ).RunTest(t)
  562. for _, test := range testcases {
  563. t.Run(test.name, func(t *testing.T) {
  564. t.Run("sbox", func(t *testing.T) {
  565. gen := result.ModuleForTests(test.name+"_sbox", "")
  566. manifest := RuleBuilderSboxProtoForTests(t, gen.Output("sbox.textproto"))
  567. hash := manifest.Commands[0].GetInputHash()
  568. AssertStringEquals(t, "hash", test.expectedHash, hash)
  569. })
  570. t.Run("", func(t *testing.T) {
  571. gen := result.ModuleForTests(test.name+"", "")
  572. command := gen.Output("gen/" + test.name).RuleParams.Command
  573. if g, w := command, " # hash of input list: "+test.expectedHash; !strings.HasSuffix(g, w) {
  574. t.Errorf("Expected command line to end with %q, got %q", w, g)
  575. }
  576. })
  577. })
  578. }
  579. }