blob: 353054d96be1bd3547add078f4517b1fa4951aa2 [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
38 hostPar = pctx.AndroidStaticRule("hostPar",
39 blueprint.RuleParams{
40 Command: `sed -e 's/%interpreter%/$interp/g' -e 's/%main%/$main/g' $template > $stub && ` +
41 `$mergeParCmd -p -pm $stub $mergedZip $srcsZips && echo '#!/usr/bin/env python' | cat - $mergedZip > $out && ` +
42 `chmod +x $out && (rm -f $stub; rm -f $mergedZip)`,
43 CommandDeps: []string{"$mergeParCmd"},
44 },
45 "interp", "main", "template", "stub", "mergedZip", "srcsZips")
46
47 embeddedPar = pctx.AndroidStaticRule("embeddedPar",
48 blueprint.RuleParams{
49 Command: `echo '$main' > $entryPoint &&` +
50 `$mergeParCmd -p -e $entryPoint $mergedZip $srcsZips && cat $launcher | cat - $mergedZip > $out && ` +
51 `chmod +x $out && (rm -f $entryPoint; rm -f $mergedZip)`,
52 CommandDeps: []string{"$mergeParCmd"},
53 },
54 "main", "entryPoint", "mergedZip", "srcsZips", "launcher")
Nan Zhangdb0b9a32017-02-27 10:12:13 -080055)
56
57func init() {
58 pctx.Import("github.com/google/blueprint/bootstrap")
59 pctx.Import("android/soong/common")
60
61 pctx.HostBinToolVariable("parCmd", "soong_zip")
Nan Zhang1db85402017-12-18 13:20:23 -080062 pctx.HostBinToolVariable("mergeParCmd", "merge_zips")
Nan Zhangdb0b9a32017-02-27 10:12:13 -080063}
64
Nan Zhang1db85402017-12-18 13:20:23 -080065func registerBuildActionForParFile(ctx android.ModuleContext, embeddedLauncher bool,
66 launcherPath android.Path, interpreter, main, binName string,
67 srcsZips android.Paths) android.Path {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080068
Nan Zhang1db85402017-12-18 13:20:23 -080069 // .intermediate output path for merged zip file.
70 mergedZip := android.PathForModuleOut(ctx, binName+".mergedzip")
Nan Zhangdb0b9a32017-02-27 10:12:13 -080071
Nan Zhangd4e641b2017-07-12 12:55:28 -070072 // .intermediate output path for bin executable.
Nan Zhangdb0b9a32017-02-27 10:12:13 -080073 binFile := android.PathForModuleOut(ctx, binName)
74
75 // implicit dependency for parFile build action.
Nan Zhang1db85402017-12-18 13:20:23 -080076 implicits := srcsZips
Nan Zhangdb0b9a32017-02-27 10:12:13 -080077
Nan Zhang1db85402017-12-18 13:20:23 -080078 if !embeddedLauncher {
Nan Zhangd4e641b2017-07-12 12:55:28 -070079 // the path of stub_template_host.txt from source tree.
80 template := android.PathForSource(ctx, stubTemplateHost)
Nan Zhang1db85402017-12-18 13:20:23 -080081 implicits = append(implicits, template)
Nan Zhangd4e641b2017-07-12 12:55:28 -070082
83 // intermediate output path for __main__.py
84 stub := android.PathForModuleOut(ctx, mainFileName).String()
85
Colin Crossae887032017-10-23 17:16:14 -070086 ctx.Build(pctx, android.BuildParams{
Nan Zhang1db85402017-12-18 13:20:23 -080087 Rule: hostPar,
Nan Zhangd4e641b2017-07-12 12:55:28 -070088 Description: "host python archive",
89 Output: binFile,
90 Implicits: implicits,
91 Args: map[string]string{
Nan Zhang1db85402017-12-18 13:20:23 -080092 "interp": strings.Replace(interpreter, "/", `\/`, -1),
Nan Zhangd4e641b2017-07-12 12:55:28 -070093 // we need remove "runfiles/" suffix since stub script starts
94 // searching for main file in each sub-dir of "runfiles" directory tree.
95 "main": strings.Replace(strings.TrimPrefix(main, runFiles+"/"),
96 "/", `\/`, -1),
Nan Zhang1db85402017-12-18 13:20:23 -080097 "template": template.String(),
98 "stub": stub,
99 "mergedZip": mergedZip.String(),
100 "srcsZips": strings.Join(srcsZips.Strings(), " "),
Nan Zhangd4e641b2017-07-12 12:55:28 -0700101 },
102 })
103 } else {
Nan Zhang1db85402017-12-18 13:20:23 -0800104 // added launcherPath to the implicits Ninja dependencies.
105 implicits = append(implicits, launcherPath)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700106
107 // .intermediate output path for entry_point.txt
108 entryPoint := android.PathForModuleOut(ctx, entryPointFile).String()
109
Colin Crossae887032017-10-23 17:16:14 -0700110 ctx.Build(pctx, android.BuildParams{
Nan Zhang1db85402017-12-18 13:20:23 -0800111 Rule: embeddedPar,
Nan Zhangd4e641b2017-07-12 12:55:28 -0700112 Description: "embedded python archive",
113 Output: binFile,
114 Implicits: implicits,
115 Args: map[string]string{
Nan Zhang1db85402017-12-18 13:20:23 -0800116 "main": main,
117 "entryPoint": entryPoint,
118 "mergedZip": mergedZip.String(),
119 "srcsZips": strings.Join(srcsZips.Strings(), " "),
120 "launcher": launcherPath.String(),
Nan Zhangd4e641b2017-07-12 12:55:28 -0700121 },
122 })
123 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800124
125 return binFile
126}