| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 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 | |
| 15 | package python |
| 16 | |
| 17 | // This file contains Ninja build actions for building Python program. |
| 18 | |
| 19 | import ( |
| 20 | "strings" |
| 21 | |
| 22 | "android/soong/android" |
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 23 | "github.com/google/blueprint" |
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | var ( |
| 27 | pctx = android.NewPackageContext("android/soong/python") |
| 28 | |
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 29 | zip = pctx.AndroidStaticRule("zip", |
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 30 | blueprint.RuleParams{ |
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 31 | Command: `$parCmd -o $out $args`, |
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 32 | CommandDeps: []string{"$parCmd"}, |
| 33 | }, |
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 34 | "args") |
| 35 | |
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 36 | combineZip = pctx.AndroidStaticRule("combineZip", |
| 37 | blueprint.RuleParams{ |
| 38 | Command: `$mergeParCmd $out $in`, |
| 39 | CommandDeps: []string{"$mergeParCmd"}, |
| 40 | }, |
| 41 | ) |
| 42 | |
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 43 | hostPar = pctx.AndroidStaticRule("hostPar", |
| 44 | blueprint.RuleParams{ |
| Cole Faust | caf766b | 2022-10-21 16:07:56 -0700 | [diff] [blame] | 45 | 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 && ` + |
| Cole Faust | eb3a900 | 2022-09-22 18:25:09 -0700 | [diff] [blame] | 46 | "sed -e 's/ENTRY_POINT/$main/g' build/soong/python/scripts/main_non_embedded.py >`dirname $out`/__soong_entrypoint_redirector__.py && " + |
| 47 | "$parCmd -o $out.entrypoint_zip -C `dirname $out` -f `dirname $out`/__soong_entrypoint_redirector__.py && " + |
| 48 | `echo "#!/usr/bin/env $interp" >${out}.prefix &&` + |
| 49 | `$mergeParCmd -p --prefix ${out}.prefix -pm $out.main $out $srcsZips $out.entrypoint_zip && ` + |
| 50 | "chmod +x $out && (rm -f $out.main; rm -f ${out}.prefix; rm -f $out.entrypoint_zip; rm -f `dirname $out`/__soong_entrypoint_redirector__.py)", |
| 51 | CommandDeps: []string{"$mergeParCmd", "$parCmd", "build/soong/python/scripts/stub_template_host.txt", "build/soong/python/scripts/main_non_embedded.py"}, |
| 52 | }, |
| Cole Faust | caf766b | 2022-10-21 16:07:56 -0700 | [diff] [blame] | 53 | "interp", "main", "srcsZips") |
| Cole Faust | eb3a900 | 2022-09-22 18:25:09 -0700 | [diff] [blame] | 54 | |
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 55 | embeddedPar = pctx.AndroidStaticRule("embeddedPar", |
| 56 | blueprint.RuleParams{ |
| Dan Willemsen | 707542f | 2018-11-29 21:39:59 -0800 | [diff] [blame] | 57 | Command: `rm -f $out.main && ` + |
| 58 | `sed 's/ENTRY_POINT/$main/' build/soong/python/scripts/main.py >$out.main &&` + |
| 59 | `$mergeParCmd -p -pm $out.main --prefix $launcher $out $srcsZips && ` + |
| 60 | `chmod +x $out && rm -rf $out.main`, |
| Cole Faust | eb3a900 | 2022-09-22 18:25:09 -0700 | [diff] [blame] | 61 | CommandDeps: []string{"$mergeParCmd", "build/soong/python/scripts/main.py"}, |
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 62 | }, |
| Dan Willemsen | 707542f | 2018-11-29 21:39:59 -0800 | [diff] [blame] | 63 | "main", "srcsZips", "launcher") |
| Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 64 | |
| 65 | embeddedParNoMain = pctx.AndroidStaticRule("embeddedParNoMain", |
| 66 | blueprint.RuleParams{ |
| 67 | Command: `$mergeParCmd -p --prefix $launcher $out $srcsZips && ` + |
| 68 | `chmod +x $out`, |
| 69 | CommandDeps: []string{"$mergeParCmd"}, |
| 70 | }, |
| 71 | "srcsZips", "launcher") |
| Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 72 | |
| 73 | precompile = pctx.AndroidStaticRule("precompilePython", blueprint.RuleParams{ |
| 74 | Command: `LD_LIBRARY_PATH="$ldLibraryPath" ` + |
| Dan Willemsen | 339a63f | 2023-08-15 22:17:03 -0400 | [diff] [blame] | 75 | `PYTHONPATH=$stdlibZip/internal/$stdlibPkg ` + |
| Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 76 | `$launcher build/soong/python/scripts/precompile_python.py $in $out`, |
| 77 | CommandDeps: []string{ |
| 78 | "$stdlibZip", |
| 79 | "$launcher", |
| 80 | "build/soong/python/scripts/precompile_python.py", |
| 81 | }, |
| Dan Willemsen | 339a63f | 2023-08-15 22:17:03 -0400 | [diff] [blame] | 82 | }, "stdlibZip", "stdlibPkg", "launcher", "ldLibraryPath") |
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 83 | ) |
| 84 | |
| 85 | func init() { |
| Colin Cross | cc0ce80 | 2019-04-02 16:14:11 -0700 | [diff] [blame] | 86 | pctx.Import("android/soong/android") |
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 87 | |
| 88 | pctx.HostBinToolVariable("parCmd", "soong_zip") |
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 89 | pctx.HostBinToolVariable("mergeParCmd", "merge_zips") |
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 92 | func registerBuildActionForParFile(ctx android.ModuleContext, embeddedLauncher bool, |
| Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 93 | launcherPath android.OptionalPath, interpreter, main, binName string, |
| Cole Faust | caf766b | 2022-10-21 16:07:56 -0700 | [diff] [blame] | 94 | srcsZips android.Paths) android.Path { |
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 95 | |
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 96 | // .intermediate output path for bin executable. |
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 97 | binFile := android.PathForModuleOut(ctx, binName) |
| 98 | |
| 99 | // implicit dependency for parFile build action. |
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 100 | implicits := srcsZips |
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 101 | |
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 102 | if !embeddedLauncher { |
| Cole Faust | caf766b | 2022-10-21 16:07:56 -0700 | [diff] [blame] | 103 | ctx.Build(pctx, android.BuildParams{ |
| 104 | Rule: hostPar, |
| 105 | Description: "host python archive", |
| 106 | Output: binFile, |
| 107 | Implicits: implicits, |
| 108 | Args: map[string]string{ |
| 109 | "interp": strings.Replace(interpreter, "/", `\/`, -1), |
| 110 | "main": strings.Replace(strings.TrimSuffix(main, pyExt), "/", ".", -1), |
| 111 | "srcsZips": strings.Join(srcsZips.Strings(), " "), |
| 112 | }, |
| 113 | }) |
| Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 114 | } else if launcherPath.Valid() { |
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 115 | // added launcherPath to the implicits Ninja dependencies. |
| Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 116 | implicits = append(implicits, launcherPath.Path()) |
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 117 | |
| Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 118 | if main == "" { |
| 119 | ctx.Build(pctx, android.BuildParams{ |
| 120 | Rule: embeddedParNoMain, |
| 121 | Description: "embedded python archive", |
| 122 | Output: binFile, |
| 123 | Implicits: implicits, |
| 124 | Args: map[string]string{ |
| 125 | "srcsZips": strings.Join(srcsZips.Strings(), " "), |
| 126 | "launcher": launcherPath.String(), |
| 127 | }, |
| 128 | }) |
| 129 | } else { |
| 130 | ctx.Build(pctx, android.BuildParams{ |
| 131 | Rule: embeddedPar, |
| 132 | Description: "embedded python archive", |
| 133 | Output: binFile, |
| 134 | Implicits: implicits, |
| 135 | Args: map[string]string{ |
| 136 | "main": strings.Replace(strings.TrimSuffix(main, pyExt), "/", ".", -1), |
| 137 | "srcsZips": strings.Join(srcsZips.Strings(), " "), |
| 138 | "launcher": launcherPath.String(), |
| 139 | }, |
| 140 | }) |
| 141 | } |
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 142 | } |
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 143 | |
| 144 | return binFile |
| 145 | } |