builder.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2017 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 python
  15. // This file contains Ninja build actions for building Python program.
  16. import (
  17. "strings"
  18. "android/soong/android"
  19. "github.com/google/blueprint"
  20. _ "github.com/google/blueprint/bootstrap"
  21. )
  22. var (
  23. pctx = android.NewPackageContext("android/soong/python")
  24. zip = pctx.AndroidStaticRule("zip",
  25. blueprint.RuleParams{
  26. Command: `$parCmd -o $out $args`,
  27. CommandDeps: []string{"$parCmd"},
  28. },
  29. "args")
  30. combineZip = pctx.AndroidStaticRule("combineZip",
  31. blueprint.RuleParams{
  32. Command: `$mergeParCmd $out $in`,
  33. CommandDeps: []string{"$mergeParCmd"},
  34. },
  35. )
  36. hostPar = pctx.AndroidStaticRule("hostPar",
  37. blueprint.RuleParams{
  38. Command: `sed -e 's/%interpreter%/$interp/g' -e 's/%main%/__soong_entrypoint_redirector__.py/g' build/soong/python/scripts/stub_template_host.txt > $out.main && ` +
  39. "sed -e 's/ENTRY_POINT/$main/g' build/soong/python/scripts/main_non_embedded.py >`dirname $out`/__soong_entrypoint_redirector__.py && " +
  40. "$parCmd -o $out.entrypoint_zip -C `dirname $out` -f `dirname $out`/__soong_entrypoint_redirector__.py && " +
  41. `echo "#!/usr/bin/env $interp" >${out}.prefix &&` +
  42. `$mergeParCmd -p --prefix ${out}.prefix -pm $out.main $out $srcsZips $out.entrypoint_zip && ` +
  43. "chmod +x $out && (rm -f $out.main; rm -f ${out}.prefix; rm -f $out.entrypoint_zip; rm -f `dirname $out`/__soong_entrypoint_redirector__.py)",
  44. CommandDeps: []string{"$mergeParCmd", "$parCmd", "build/soong/python/scripts/stub_template_host.txt", "build/soong/python/scripts/main_non_embedded.py"},
  45. },
  46. "interp", "main", "srcsZips")
  47. embeddedPar = pctx.AndroidStaticRule("embeddedPar",
  48. blueprint.RuleParams{
  49. Command: `rm -f $out.main && ` +
  50. `sed 's/ENTRY_POINT/$main/' build/soong/python/scripts/main.py >$out.main &&` +
  51. `$mergeParCmd -p -pm $out.main --prefix $launcher $out $srcsZips && ` +
  52. `chmod +x $out && rm -rf $out.main`,
  53. CommandDeps: []string{"$mergeParCmd", "build/soong/python/scripts/main.py"},
  54. },
  55. "main", "srcsZips", "launcher")
  56. embeddedParNoMain = pctx.AndroidStaticRule("embeddedParNoMain",
  57. blueprint.RuleParams{
  58. Command: `$mergeParCmd -p --prefix $launcher $out $srcsZips && ` +
  59. `chmod +x $out`,
  60. CommandDeps: []string{"$mergeParCmd"},
  61. },
  62. "srcsZips", "launcher")
  63. )
  64. func init() {
  65. pctx.Import("github.com/google/blueprint/bootstrap")
  66. pctx.Import("android/soong/android")
  67. pctx.HostBinToolVariable("parCmd", "soong_zip")
  68. pctx.HostBinToolVariable("mergeParCmd", "merge_zips")
  69. }
  70. func registerBuildActionForParFile(ctx android.ModuleContext, embeddedLauncher bool,
  71. launcherPath android.OptionalPath, interpreter, main, binName string,
  72. srcsZips android.Paths) android.Path {
  73. // .intermediate output path for bin executable.
  74. binFile := android.PathForModuleOut(ctx, binName)
  75. // implicit dependency for parFile build action.
  76. implicits := srcsZips
  77. if !embeddedLauncher {
  78. ctx.Build(pctx, android.BuildParams{
  79. Rule: hostPar,
  80. Description: "host python archive",
  81. Output: binFile,
  82. Implicits: implicits,
  83. Args: map[string]string{
  84. "interp": strings.Replace(interpreter, "/", `\/`, -1),
  85. "main": strings.Replace(strings.TrimSuffix(main, pyExt), "/", ".", -1),
  86. "srcsZips": strings.Join(srcsZips.Strings(), " "),
  87. },
  88. })
  89. } else if launcherPath.Valid() {
  90. // added launcherPath to the implicits Ninja dependencies.
  91. implicits = append(implicits, launcherPath.Path())
  92. if main == "" {
  93. ctx.Build(pctx, android.BuildParams{
  94. Rule: embeddedParNoMain,
  95. Description: "embedded python archive",
  96. Output: binFile,
  97. Implicits: implicits,
  98. Args: map[string]string{
  99. "srcsZips": strings.Join(srcsZips.Strings(), " "),
  100. "launcher": launcherPath.String(),
  101. },
  102. })
  103. } else {
  104. ctx.Build(pctx, android.BuildParams{
  105. Rule: embeddedPar,
  106. Description: "embedded python archive",
  107. Output: binFile,
  108. Implicits: implicits,
  109. Args: map[string]string{
  110. "main": strings.Replace(strings.TrimSuffix(main, pyExt), "/", ".", -1),
  111. "srcsZips": strings.Join(srcsZips.Strings(), " "),
  112. "launcher": launcherPath.String(),
  113. },
  114. })
  115. }
  116. }
  117. return binFile
  118. }