blob: 7ecd4b5e49b80e70a027dbefd237177e3e4dc2ca [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{
47 Command: `sed -e 's/%interpreter%/$interp/g' -e 's/%main%/$main/g' $template > $stub && ` +
Dan Willemsen263dde72018-11-15 19:15:02 -080048 `echo "#!/usr/bin/env python" >${out}.prefix &&` +
49 `$mergeParCmd -p --prefix ${out}.prefix -pm $stub $out $srcsZips && ` +
50 `chmod +x $out && (rm -f $stub; rm -f ${out}.prefix)`,
Nan Zhang1db85402017-12-18 13:20:23 -080051 CommandDeps: []string{"$mergeParCmd"},
52 },
Dan Willemsen263dde72018-11-15 19:15:02 -080053 "interp", "main", "template", "stub", "srcsZips")
Nan Zhang1db85402017-12-18 13:20:23 -080054
55 embeddedPar = pctx.AndroidStaticRule("embeddedPar",
56 blueprint.RuleParams{
Dan Willemsen54c5b612018-11-29 12:54:20 -080057 // `echo -n` to trim the newline, since the python code just wants the name
58 Command: `echo -n '$main' > $entryPoint &&` +
Dan Willemsen263dde72018-11-15 19:15:02 -080059 `$mergeParCmd -p --prefix $launcher -e $entryPoint $out $srcsZips && ` +
60 `chmod +x $out && (rm -f $entryPoint)`,
Nan Zhang1db85402017-12-18 13:20:23 -080061 CommandDeps: []string{"$mergeParCmd"},
62 },
Dan Willemsen263dde72018-11-15 19:15:02 -080063 "main", "entryPoint", "srcsZips", "launcher")
Nan Zhangdb0b9a32017-02-27 10:12:13 -080064)
65
66func init() {
67 pctx.Import("github.com/google/blueprint/bootstrap")
68 pctx.Import("android/soong/common")
69
70 pctx.HostBinToolVariable("parCmd", "soong_zip")
Nan Zhang1db85402017-12-18 13:20:23 -080071 pctx.HostBinToolVariable("mergeParCmd", "merge_zips")
Nan Zhangdb0b9a32017-02-27 10:12:13 -080072}
73
Nan Zhang1db85402017-12-18 13:20:23 -080074func registerBuildActionForParFile(ctx android.ModuleContext, embeddedLauncher bool,
Nan Zhangcba97e62018-09-26 15:14:10 -070075 launcherPath android.OptionalPath, interpreter, main, binName string,
Nan Zhang1db85402017-12-18 13:20:23 -080076 srcsZips android.Paths) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080077
Nan Zhangd4e641b2017-07-12 12:55:28 -070078 // .intermediate output path for bin executable.
Nan Zhangdb0b9a32017-02-27 10:12:13 -080079 binFile := android.PathForModuleOut(ctx, binName)
80
81 // implicit dependency for parFile build action.
Nan Zhang1db85402017-12-18 13:20:23 -080082 implicits := srcsZips
Nan Zhangdb0b9a32017-02-27 10:12:13 -080083
Nan Zhang1db85402017-12-18 13:20:23 -080084 if !embeddedLauncher {
Nan Zhangd4e641b2017-07-12 12:55:28 -070085 // the path of stub_template_host.txt from source tree.
86 template := android.PathForSource(ctx, stubTemplateHost)
Nan Zhang1db85402017-12-18 13:20:23 -080087 implicits = append(implicits, template)
Nan Zhangd4e641b2017-07-12 12:55:28 -070088
89 // intermediate output path for __main__.py
90 stub := android.PathForModuleOut(ctx, mainFileName).String()
91
Colin Crossae887032017-10-23 17:16:14 -070092 ctx.Build(pctx, android.BuildParams{
Nan Zhang1db85402017-12-18 13:20:23 -080093 Rule: hostPar,
Nan Zhangd4e641b2017-07-12 12:55:28 -070094 Description: "host python archive",
95 Output: binFile,
96 Implicits: implicits,
97 Args: map[string]string{
Dan Willemsen263dde72018-11-15 19:15:02 -080098 "interp": strings.Replace(interpreter, "/", `\/`, -1),
99 "main": strings.Replace(main, "/", `\/`, -1),
100 "template": template.String(),
101 "stub": stub,
102 "srcsZips": strings.Join(srcsZips.Strings(), " "),
Nan Zhangd4e641b2017-07-12 12:55:28 -0700103 },
104 })
Nan Zhangcba97e62018-09-26 15:14:10 -0700105 } else if launcherPath.Valid() {
Nan Zhang1db85402017-12-18 13:20:23 -0800106 // added launcherPath to the implicits Ninja dependencies.
Nan Zhangcba97e62018-09-26 15:14:10 -0700107 implicits = append(implicits, launcherPath.Path())
Nan Zhangd4e641b2017-07-12 12:55:28 -0700108
109 // .intermediate output path for entry_point.txt
110 entryPoint := android.PathForModuleOut(ctx, entryPointFile).String()
111
Colin Crossae887032017-10-23 17:16:14 -0700112 ctx.Build(pctx, android.BuildParams{
Nan Zhang1db85402017-12-18 13:20:23 -0800113 Rule: embeddedPar,
Nan Zhangd4e641b2017-07-12 12:55:28 -0700114 Description: "embedded python archive",
115 Output: binFile,
116 Implicits: implicits,
117 Args: map[string]string{
Dan Willemsen54c5b612018-11-29 12:54:20 -0800118 "main": strings.Replace(strings.TrimSuffix(main, pyExt), "/", ".", -1),
Nan Zhang1db85402017-12-18 13:20:23 -0800119 "entryPoint": entryPoint,
Nan Zhang1db85402017-12-18 13:20:23 -0800120 "srcsZips": strings.Join(srcsZips.Strings(), " "),
121 "launcher": launcherPath.String(),
Nan Zhangd4e641b2017-07-12 12:55:28 -0700122 },
123 })
124 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800125
126 return binFile
127}