builder.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. precompile = pctx.AndroidStaticRule("precompilePython", blueprint.RuleParams{
  64. Command: `LD_LIBRARY_PATH="$ldLibraryPath" ` +
  65. `PYTHONPATH=$stdlibZip/internal/stdlib ` +
  66. `$launcher build/soong/python/scripts/precompile_python.py $in $out`,
  67. CommandDeps: []string{
  68. "$stdlibZip",
  69. "$launcher",
  70. "build/soong/python/scripts/precompile_python.py",
  71. },
  72. }, "stdlibZip", "launcher", "ldLibraryPath")
  73. )
  74. func init() {
  75. pctx.Import("github.com/google/blueprint/bootstrap")
  76. pctx.Import("android/soong/android")
  77. pctx.HostBinToolVariable("parCmd", "soong_zip")
  78. pctx.HostBinToolVariable("mergeParCmd", "merge_zips")
  79. }
  80. func registerBuildActionForParFile(ctx android.ModuleContext, embeddedLauncher bool,
  81. launcherPath android.OptionalPath, interpreter, main, binName string,
  82. srcsZips android.Paths) android.Path {
  83. // .intermediate output path for bin executable.
  84. binFile := android.PathForModuleOut(ctx, binName)
  85. // implicit dependency for parFile build action.
  86. implicits := srcsZips
  87. if !embeddedLauncher {
  88. ctx.Build(pctx, android.BuildParams{
  89. Rule: hostPar,
  90. Description: "host python archive",
  91. Output: binFile,
  92. Implicits: implicits,
  93. Args: map[string]string{
  94. "interp": strings.Replace(interpreter, "/", `\/`, -1),
  95. "main": strings.Replace(strings.TrimSuffix(main, pyExt), "/", ".", -1),
  96. "srcsZips": strings.Join(srcsZips.Strings(), " "),
  97. },
  98. })
  99. } else if launcherPath.Valid() {
  100. // added launcherPath to the implicits Ninja dependencies.
  101. implicits = append(implicits, launcherPath.Path())
  102. if main == "" {
  103. ctx.Build(pctx, android.BuildParams{
  104. Rule: embeddedParNoMain,
  105. Description: "embedded python archive",
  106. Output: binFile,
  107. Implicits: implicits,
  108. Args: map[string]string{
  109. "srcsZips": strings.Join(srcsZips.Strings(), " "),
  110. "launcher": launcherPath.String(),
  111. },
  112. })
  113. } else {
  114. ctx.Build(pctx, android.BuildParams{
  115. Rule: embeddedPar,
  116. Description: "embedded python archive",
  117. Output: binFile,
  118. Implicits: implicits,
  119. Args: map[string]string{
  120. "main": strings.Replace(strings.TrimSuffix(main, pyExt), "/", ".", -1),
  121. "srcsZips": strings.Join(srcsZips.Strings(), " "),
  122. "launcher": launcherPath.String(),
  123. },
  124. })
  125. }
  126. }
  127. return binFile
  128. }