blob: 2553a7714cc3b4b0ed237f44212be2cb0122f0bb [file] [log] [blame]
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001// 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
15package python
16
17// This file contains Ninja build actions for building Python program.
18
19import (
20 "strings"
21
22 "android/soong/android"
Nan Zhangdb0b9a32017-02-27 10:12:13 -080023 "github.com/google/blueprint"
24 _ "github.com/google/blueprint/bootstrap"
25)
26
27var (
28 pctx = android.NewPackageContext("android/soong/python")
29
Nan Zhang1db85402017-12-18 13:20:23 -080030 zip = pctx.AndroidStaticRule("zip",
Nan Zhangdb0b9a32017-02-27 10:12:13 -080031 blueprint.RuleParams{
Nan Zhang1db85402017-12-18 13:20:23 -080032 Command: `$parCmd -o $out $args`,
Nan Zhangd4e641b2017-07-12 12:55:28 -070033 CommandDeps: []string{"$parCmd"},
34 },
Nan Zhang1db85402017-12-18 13:20:23 -080035 "args")
36
Nan Zhangb8fa1972017-12-22 16:12:00 -080037 combineZip = pctx.AndroidStaticRule("combineZip",
38 blueprint.RuleParams{
39 Command: `$mergeParCmd $out $in`,
40 CommandDeps: []string{"$mergeParCmd"},
41 },
42 )
43
Nan Zhang1db85402017-12-18 13:20:23 -080044 hostPar = pctx.AndroidStaticRule("hostPar",
45 blueprint.RuleParams{
Cole Faustcaf766b2022-10-21 16:07:56 -070046 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 Fausteb3a9002022-09-22 18:25:09 -070047 "sed -e 's/ENTRY_POINT/$main/g' build/soong/python/scripts/main_non_embedded.py >`dirname $out`/__soong_entrypoint_redirector__.py && " +
48 "$parCmd -o $out.entrypoint_zip -C `dirname $out` -f `dirname $out`/__soong_entrypoint_redirector__.py && " +
49 `echo "#!/usr/bin/env $interp" >${out}.prefix &&` +
50 `$mergeParCmd -p --prefix ${out}.prefix -pm $out.main $out $srcsZips $out.entrypoint_zip && ` +
51 "chmod +x $out && (rm -f $out.main; rm -f ${out}.prefix; rm -f $out.entrypoint_zip; rm -f `dirname $out`/__soong_entrypoint_redirector__.py)",
52 CommandDeps: []string{"$mergeParCmd", "$parCmd", "build/soong/python/scripts/stub_template_host.txt", "build/soong/python/scripts/main_non_embedded.py"},
53 },
Cole Faustcaf766b2022-10-21 16:07:56 -070054 "interp", "main", "srcsZips")
Cole Fausteb3a9002022-09-22 18:25:09 -070055
Nan Zhang1db85402017-12-18 13:20:23 -080056 embeddedPar = pctx.AndroidStaticRule("embeddedPar",
57 blueprint.RuleParams{
Dan Willemsen707542f2018-11-29 21:39:59 -080058 Command: `rm -f $out.main && ` +
59 `sed 's/ENTRY_POINT/$main/' build/soong/python/scripts/main.py >$out.main &&` +
60 `$mergeParCmd -p -pm $out.main --prefix $launcher $out $srcsZips && ` +
61 `chmod +x $out && rm -rf $out.main`,
Cole Fausteb3a9002022-09-22 18:25:09 -070062 CommandDeps: []string{"$mergeParCmd", "build/soong/python/scripts/main.py"},
Nan Zhang1db85402017-12-18 13:20:23 -080063 },
Dan Willemsen707542f2018-11-29 21:39:59 -080064 "main", "srcsZips", "launcher")
Dan Willemsen6ca390f2019-02-14 23:17:08 -080065
66 embeddedParNoMain = pctx.AndroidStaticRule("embeddedParNoMain",
67 blueprint.RuleParams{
68 Command: `$mergeParCmd -p --prefix $launcher $out $srcsZips && ` +
69 `chmod +x $out`,
70 CommandDeps: []string{"$mergeParCmd"},
71 },
72 "srcsZips", "launcher")
Cole Faust5c503d12023-01-24 11:48:08 -080073
74 precompile = pctx.AndroidStaticRule("precompilePython", blueprint.RuleParams{
75 Command: `LD_LIBRARY_PATH="$ldLibraryPath" ` +
Dan Willemsen339a63f2023-08-15 22:17:03 -040076 `PYTHONPATH=$stdlibZip/internal/$stdlibPkg ` +
Cole Faust5c503d12023-01-24 11:48:08 -080077 `$launcher build/soong/python/scripts/precompile_python.py $in $out`,
78 CommandDeps: []string{
79 "$stdlibZip",
80 "$launcher",
81 "build/soong/python/scripts/precompile_python.py",
82 },
Dan Willemsen339a63f2023-08-15 22:17:03 -040083 }, "stdlibZip", "stdlibPkg", "launcher", "ldLibraryPath")
Nan Zhangdb0b9a32017-02-27 10:12:13 -080084)
85
86func init() {
87 pctx.Import("github.com/google/blueprint/bootstrap")
Colin Crosscc0ce802019-04-02 16:14:11 -070088 pctx.Import("android/soong/android")
Nan Zhangdb0b9a32017-02-27 10:12:13 -080089
90 pctx.HostBinToolVariable("parCmd", "soong_zip")
Nan Zhang1db85402017-12-18 13:20:23 -080091 pctx.HostBinToolVariable("mergeParCmd", "merge_zips")
Nan Zhangdb0b9a32017-02-27 10:12:13 -080092}
93
Nan Zhang1db85402017-12-18 13:20:23 -080094func registerBuildActionForParFile(ctx android.ModuleContext, embeddedLauncher bool,
Nan Zhangcba97e62018-09-26 15:14:10 -070095 launcherPath android.OptionalPath, interpreter, main, binName string,
Cole Faustcaf766b2022-10-21 16:07:56 -070096 srcsZips android.Paths) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080097
Nan Zhangd4e641b2017-07-12 12:55:28 -070098 // .intermediate output path for bin executable.
Nan Zhangdb0b9a32017-02-27 10:12:13 -080099 binFile := android.PathForModuleOut(ctx, binName)
100
101 // implicit dependency for parFile build action.
Nan Zhang1db85402017-12-18 13:20:23 -0800102 implicits := srcsZips
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800103
Nan Zhang1db85402017-12-18 13:20:23 -0800104 if !embeddedLauncher {
Cole Faustcaf766b2022-10-21 16:07:56 -0700105 ctx.Build(pctx, android.BuildParams{
106 Rule: hostPar,
107 Description: "host python archive",
108 Output: binFile,
109 Implicits: implicits,
110 Args: map[string]string{
111 "interp": strings.Replace(interpreter, "/", `\/`, -1),
112 "main": strings.Replace(strings.TrimSuffix(main, pyExt), "/", ".", -1),
113 "srcsZips": strings.Join(srcsZips.Strings(), " "),
114 },
115 })
Nan Zhangcba97e62018-09-26 15:14:10 -0700116 } else if launcherPath.Valid() {
Nan Zhang1db85402017-12-18 13:20:23 -0800117 // added launcherPath to the implicits Ninja dependencies.
Nan Zhangcba97e62018-09-26 15:14:10 -0700118 implicits = append(implicits, launcherPath.Path())
Nan Zhangd4e641b2017-07-12 12:55:28 -0700119
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800120 if main == "" {
121 ctx.Build(pctx, android.BuildParams{
122 Rule: embeddedParNoMain,
123 Description: "embedded python archive",
124 Output: binFile,
125 Implicits: implicits,
126 Args: map[string]string{
127 "srcsZips": strings.Join(srcsZips.Strings(), " "),
128 "launcher": launcherPath.String(),
129 },
130 })
131 } else {
132 ctx.Build(pctx, android.BuildParams{
133 Rule: embeddedPar,
134 Description: "embedded python archive",
135 Output: binFile,
136 Implicits: implicits,
137 Args: map[string]string{
138 "main": strings.Replace(strings.TrimSuffix(main, pyExt), "/", ".", -1),
139 "srcsZips": strings.Join(srcsZips.Strings(), " "),
140 "launcher": launcherPath.String(),
141 },
142 })
143 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700144 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800145
146 return binFile
147}