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" |
| 23 | |
| 24 | "github.com/google/blueprint" |
| 25 | _ "github.com/google/blueprint/bootstrap" |
| 26 | ) |
| 27 | |
| 28 | var ( |
| 29 | pctx = android.NewPackageContext("android/soong/python") |
| 30 | |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 31 | zip = pctx.AndroidStaticRule("zip", |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 32 | blueprint.RuleParams{ |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 33 | Command: `$parCmd -o $out $args`, |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 34 | CommandDeps: []string{"$parCmd"}, |
| 35 | }, |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 36 | "args") |
| 37 | |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 38 | combineZip = pctx.AndroidStaticRule("combineZip", |
| 39 | blueprint.RuleParams{ |
| 40 | Command: `$mergeParCmd $out $in`, |
| 41 | CommandDeps: []string{"$mergeParCmd"}, |
| 42 | }, |
| 43 | ) |
| 44 | |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 45 | hostPar = pctx.AndroidStaticRule("hostPar", |
| 46 | blueprint.RuleParams{ |
| 47 | Command: `sed -e 's/%interpreter%/$interp/g' -e 's/%main%/$main/g' $template > $stub && ` + |
| 48 | `$mergeParCmd -p -pm $stub $mergedZip $srcsZips && echo '#!/usr/bin/env python' | cat - $mergedZip > $out && ` + |
| 49 | `chmod +x $out && (rm -f $stub; rm -f $mergedZip)`, |
| 50 | CommandDeps: []string{"$mergeParCmd"}, |
| 51 | }, |
| 52 | "interp", "main", "template", "stub", "mergedZip", "srcsZips") |
| 53 | |
| 54 | embeddedPar = pctx.AndroidStaticRule("embeddedPar", |
| 55 | blueprint.RuleParams{ |
| 56 | Command: `echo '$main' > $entryPoint &&` + |
| 57 | `$mergeParCmd -p -e $entryPoint $mergedZip $srcsZips && cat $launcher | cat - $mergedZip > $out && ` + |
| 58 | `chmod +x $out && (rm -f $entryPoint; rm -f $mergedZip)`, |
| 59 | CommandDeps: []string{"$mergeParCmd"}, |
| 60 | }, |
| 61 | "main", "entryPoint", "mergedZip", "srcsZips", "launcher") |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 62 | ) |
| 63 | |
| 64 | func init() { |
| 65 | pctx.Import("github.com/google/blueprint/bootstrap") |
| 66 | pctx.Import("android/soong/common") |
| 67 | |
| 68 | pctx.HostBinToolVariable("parCmd", "soong_zip") |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 69 | pctx.HostBinToolVariable("mergeParCmd", "merge_zips") |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 72 | func registerBuildActionForParFile(ctx android.ModuleContext, embeddedLauncher bool, |
| 73 | launcherPath android.Path, interpreter, main, binName string, |
| 74 | srcsZips android.Paths) android.Path { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 75 | |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 76 | // .intermediate output path for merged zip file. |
| 77 | mergedZip := android.PathForModuleOut(ctx, binName+".mergedzip") |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 78 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 79 | // .intermediate output path for bin executable. |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 80 | binFile := android.PathForModuleOut(ctx, binName) |
| 81 | |
| 82 | // implicit dependency for parFile build action. |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 83 | implicits := srcsZips |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 84 | |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 85 | if !embeddedLauncher { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 86 | // the path of stub_template_host.txt from source tree. |
| 87 | template := android.PathForSource(ctx, stubTemplateHost) |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 88 | implicits = append(implicits, template) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 89 | |
| 90 | // intermediate output path for __main__.py |
| 91 | stub := android.PathForModuleOut(ctx, mainFileName).String() |
| 92 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 93 | ctx.Build(pctx, android.BuildParams{ |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 94 | Rule: hostPar, |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 95 | Description: "host python archive", |
| 96 | Output: binFile, |
| 97 | Implicits: implicits, |
| 98 | Args: map[string]string{ |
Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 99 | "interp": strings.Replace(interpreter, "/", `\/`, -1), |
| 100 | "main": strings.Replace(main, "/", `\/`, -1), |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 101 | "template": template.String(), |
| 102 | "stub": stub, |
| 103 | "mergedZip": mergedZip.String(), |
| 104 | "srcsZips": strings.Join(srcsZips.Strings(), " "), |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 105 | }, |
| 106 | }) |
| 107 | } else { |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 108 | // added launcherPath to the implicits Ninja dependencies. |
| 109 | implicits = append(implicits, launcherPath) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 110 | |
| 111 | // .intermediate output path for entry_point.txt |
| 112 | entryPoint := android.PathForModuleOut(ctx, entryPointFile).String() |
| 113 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 114 | ctx.Build(pctx, android.BuildParams{ |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 115 | Rule: embeddedPar, |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 116 | Description: "embedded python archive", |
| 117 | Output: binFile, |
| 118 | Implicits: implicits, |
| 119 | Args: map[string]string{ |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 120 | "main": main, |
| 121 | "entryPoint": entryPoint, |
| 122 | "mergedZip": mergedZip.String(), |
| 123 | "srcsZips": strings.Join(srcsZips.Strings(), " "), |
| 124 | "launcher": launcherPath.String(), |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 125 | }, |
| 126 | }) |
| 127 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 128 | |
| 129 | return binFile |
| 130 | } |