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