blob: 08d345c923d889bad20d9349ad11c635f2a2679b [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"
23
24 "github.com/google/blueprint"
25 _ "github.com/google/blueprint/bootstrap"
26)
27
28var (
29 pctx = android.NewPackageContext("android/soong/python")
30
Nan Zhang1db85402017-12-18 13:20:23 -080031 zip = pctx.AndroidStaticRule("zip",
Nan Zhangdb0b9a32017-02-27 10:12:13 -080032 blueprint.RuleParams{
Nan Zhang1db85402017-12-18 13:20:23 -080033 Command: `$parCmd -o $out $args`,
Nan Zhangd4e641b2017-07-12 12:55:28 -070034 CommandDeps: []string{"$parCmd"},
35 },
Nan Zhang1db85402017-12-18 13:20:23 -080036 "args")
37
Nan Zhangb8fa1972017-12-22 16:12:00 -080038 combineZip = pctx.AndroidStaticRule("combineZip",
39 blueprint.RuleParams{
40 Command: `$mergeParCmd $out $in`,
41 CommandDeps: []string{"$mergeParCmd"},
42 },
43 )
44
Nan Zhang1db85402017-12-18 13:20:23 -080045 hostPar = pctx.AndroidStaticRule("hostPar",
46 blueprint.RuleParams{
Cole Faustaf4b13d2022-09-14 15:25:15 -070047 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 && ` +
yangbill3aa29752021-04-16 16:00:28 +080048 `echo "#!/usr/bin/env $interp" >${out}.prefix &&` +
Cole Faustaf4b13d2022-09-14 15:25:15 -070049 `$mergeParCmd -p --prefix ${out}.prefix -pm $out.main $out $srcsZips && ` +
50 `chmod +x $out && (rm -f $out.main; rm -f ${out}.prefix)`,
51 CommandDeps: []string{"$mergeParCmd", "build/soong/python/scripts/stub_template_host.txt"},
Nan Zhang1db85402017-12-18 13:20:23 -080052 },
Cole Faustaf4b13d2022-09-14 15:25:15 -070053 "interp", "main", "srcsZips", "addTopDirectoriesToPath")
Nan Zhang1db85402017-12-18 13:20:23 -080054
55 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`,
61 CommandDeps: []string{"$mergeParCmd", "$parCmd", "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")
Nan Zhangdb0b9a32017-02-27 10:12:13 -080072)
73
74func init() {
75 pctx.Import("github.com/google/blueprint/bootstrap")
Colin Crosscc0ce802019-04-02 16:14:11 -070076 pctx.Import("android/soong/android")
Nan Zhangdb0b9a32017-02-27 10:12:13 -080077
78 pctx.HostBinToolVariable("parCmd", "soong_zip")
Nan Zhang1db85402017-12-18 13:20:23 -080079 pctx.HostBinToolVariable("mergeParCmd", "merge_zips")
Nan Zhangdb0b9a32017-02-27 10:12:13 -080080}
81
Nan Zhang1db85402017-12-18 13:20:23 -080082func registerBuildActionForParFile(ctx android.ModuleContext, embeddedLauncher bool,
Nan Zhangcba97e62018-09-26 15:14:10 -070083 launcherPath android.OptionalPath, interpreter, main, binName string,
Cole Faustaf4b13d2022-09-14 15:25:15 -070084 srcsZips android.Paths, addTopDirectoriesToPath bool) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080085
Nan Zhangd4e641b2017-07-12 12:55:28 -070086 // .intermediate output path for bin executable.
Nan Zhangdb0b9a32017-02-27 10:12:13 -080087 binFile := android.PathForModuleOut(ctx, binName)
88
89 // implicit dependency for parFile build action.
Nan Zhang1db85402017-12-18 13:20:23 -080090 implicits := srcsZips
Nan Zhangdb0b9a32017-02-27 10:12:13 -080091
Nan Zhang1db85402017-12-18 13:20:23 -080092 if !embeddedLauncher {
Cole Faustaf4b13d2022-09-14 15:25:15 -070093 addDirsString := "False"
94 if addTopDirectoriesToPath {
95 addDirsString = "True"
96 }
Colin Crossae887032017-10-23 17:16:14 -070097 ctx.Build(pctx, android.BuildParams{
Nan Zhang1db85402017-12-18 13:20:23 -080098 Rule: hostPar,
Nan Zhangd4e641b2017-07-12 12:55:28 -070099 Description: "host python archive",
100 Output: binFile,
101 Implicits: implicits,
102 Args: map[string]string{
Cole Faustaf4b13d2022-09-14 15:25:15 -0700103 "interp": strings.Replace(interpreter, "/", `\/`, -1),
104 "main": strings.Replace(main, "/", `\/`, -1),
105 "srcsZips": strings.Join(srcsZips.Strings(), " "),
106 "addTopDirectoriesToPath": addDirsString,
Nan Zhangd4e641b2017-07-12 12:55:28 -0700107 },
108 })
Nan Zhangcba97e62018-09-26 15:14:10 -0700109 } else if launcherPath.Valid() {
Nan Zhang1db85402017-12-18 13:20:23 -0800110 // added launcherPath to the implicits Ninja dependencies.
Nan Zhangcba97e62018-09-26 15:14:10 -0700111 implicits = append(implicits, launcherPath.Path())
Nan Zhangd4e641b2017-07-12 12:55:28 -0700112
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800113 if main == "" {
114 ctx.Build(pctx, android.BuildParams{
115 Rule: embeddedParNoMain,
116 Description: "embedded python archive",
117 Output: binFile,
118 Implicits: implicits,
119 Args: map[string]string{
120 "srcsZips": strings.Join(srcsZips.Strings(), " "),
121 "launcher": launcherPath.String(),
122 },
123 })
124 } else {
125 ctx.Build(pctx, android.BuildParams{
126 Rule: embeddedPar,
127 Description: "embedded python archive",
128 Output: binFile,
129 Implicits: implicits,
130 Args: map[string]string{
131 "main": strings.Replace(strings.TrimSuffix(main, pyExt), "/", ".", -1),
132 "srcsZips": strings.Join(srcsZips.Strings(), " "),
133 "launcher": launcherPath.String(),
134 },
135 })
136 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700137 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800138
139 return binFile
140}