blob: 8afe87a75ad7e4166b03ad76effb670c0f11f0e8 [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"
Nan Zhangdb0b9a32017-02-27 10:12:13 -080024)
25
26var (
27 pctx = android.NewPackageContext("android/soong/python")
28
Nan Zhang1db85402017-12-18 13:20:23 -080029 zip = pctx.AndroidStaticRule("zip",
Nan Zhangdb0b9a32017-02-27 10:12:13 -080030 blueprint.RuleParams{
Nan Zhang1db85402017-12-18 13:20:23 -080031 Command: `$parCmd -o $out $args`,
Nan Zhangd4e641b2017-07-12 12:55:28 -070032 CommandDeps: []string{"$parCmd"},
33 },
Nan Zhang1db85402017-12-18 13:20:23 -080034 "args")
35
Nan Zhangb8fa1972017-12-22 16:12:00 -080036 combineZip = pctx.AndroidStaticRule("combineZip",
37 blueprint.RuleParams{
38 Command: `$mergeParCmd $out $in`,
39 CommandDeps: []string{"$mergeParCmd"},
40 },
41 )
42
Nan Zhang1db85402017-12-18 13:20:23 -080043 hostPar = pctx.AndroidStaticRule("hostPar",
44 blueprint.RuleParams{
Cole Faustcaf766b2022-10-21 16:07:56 -070045 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 -070046 "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 Faustcaf766b2022-10-21 16:07:56 -070053 "interp", "main", "srcsZips")
Cole Fausteb3a9002022-09-22 18:25:09 -070054
Nan Zhang1db85402017-12-18 13:20:23 -080055 embeddedPar = pctx.AndroidStaticRule("embeddedPar",
56 blueprint.RuleParams{
Dan Willemsen707542f2018-11-29 21:39:59 -080057 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 Fausteb3a9002022-09-22 18:25:09 -070061 CommandDeps: []string{"$mergeParCmd", "build/soong/python/scripts/main.py"},
Nan Zhang1db85402017-12-18 13:20:23 -080062 },
Dan Willemsen707542f2018-11-29 21:39:59 -080063 "main", "srcsZips", "launcher")
Dan Willemsen6ca390f2019-02-14 23:17:08 -080064
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 Faust5c503d12023-01-24 11:48:08 -080072
73 precompile = pctx.AndroidStaticRule("precompilePython", blueprint.RuleParams{
74 Command: `LD_LIBRARY_PATH="$ldLibraryPath" ` +
Dan Willemsen339a63f2023-08-15 22:17:03 -040075 `PYTHONPATH=$stdlibZip/internal/$stdlibPkg ` +
Cole Faust5c503d12023-01-24 11:48:08 -080076 `$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 Willemsen339a63f2023-08-15 22:17:03 -040082 }, "stdlibZip", "stdlibPkg", "launcher", "ldLibraryPath")
Nan Zhangdb0b9a32017-02-27 10:12:13 -080083)
84
85func init() {
Colin Crosscc0ce802019-04-02 16:14:11 -070086 pctx.Import("android/soong/android")
Nan Zhangdb0b9a32017-02-27 10:12:13 -080087
88 pctx.HostBinToolVariable("parCmd", "soong_zip")
Nan Zhang1db85402017-12-18 13:20:23 -080089 pctx.HostBinToolVariable("mergeParCmd", "merge_zips")
Nan Zhangdb0b9a32017-02-27 10:12:13 -080090}
91
Nan Zhang1db85402017-12-18 13:20:23 -080092func registerBuildActionForParFile(ctx android.ModuleContext, embeddedLauncher bool,
Nan Zhangcba97e62018-09-26 15:14:10 -070093 launcherPath android.OptionalPath, interpreter, main, binName string,
Cole Faustcaf766b2022-10-21 16:07:56 -070094 srcsZips android.Paths) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080095
Nan Zhangd4e641b2017-07-12 12:55:28 -070096 // .intermediate output path for bin executable.
Nan Zhangdb0b9a32017-02-27 10:12:13 -080097 binFile := android.PathForModuleOut(ctx, binName)
98
99 // implicit dependency for parFile build action.
Nan Zhang1db85402017-12-18 13:20:23 -0800100 implicits := srcsZips
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800101
Nan Zhang1db85402017-12-18 13:20:23 -0800102 if !embeddedLauncher {
Cole Faustcaf766b2022-10-21 16:07:56 -0700103 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 Zhangcba97e62018-09-26 15:14:10 -0700114 } else if launcherPath.Valid() {
Nan Zhang1db85402017-12-18 13:20:23 -0800115 // added launcherPath to the implicits Ninja dependencies.
Nan Zhangcba97e62018-09-26 15:14:10 -0700116 implicits = append(implicits, launcherPath.Path())
Nan Zhangd4e641b2017-07-12 12:55:28 -0700117
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800118 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 Zhangd4e641b2017-07-12 12:55:28 -0700142 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800143
144 return binFile
145}