aboutsummaryrefslogtreecommitdiffstats
path: root/python/builder.go
diff options
context:
space:
mode:
authorNan Zhang <nanzhang@google.com>2017-02-27 10:12:13 -0800
committerNan Zhang <nanzhang@google.com>2017-05-05 13:27:56 -0700
commitdb0b9a3cf3c9965929c988f1292f892bfc5deec5 (patch)
treefe145e5c2d864983915eaf98e1f8e4648e40c355 /python/builder.go
parent7c34c4c8eba1ec0d5b8cf50926e40aee3291604e (diff)
downloadbuild_soong-db0b9a3cf3c9965929c988f1292f892bfc5deec5.tar.gz
build_soong-db0b9a3cf3c9965929c988f1292f892bfc5deec5.tar.bz2
build_soong-db0b9a3cf3c9965929c988f1292f892bfc5deec5.zip
Supported python build in host side.
The base module handles all the common functionalites, such as version compatibilty check, version variations split, source file format check, source/data file duplicate check. The library/binary module focuses on how to generate binary build actions, such as setting up stub script, zipping, filling in __init__.py in runfiles dir tree. Bug: b/31676493 Test: go test under python package Change-Id: I06608369f350f7195873d459e1c8d1bdb811e77e
Diffstat (limited to 'python/builder.go')
-rw-r--r--python/builder.go146
1 files changed, 146 insertions, 0 deletions
diff --git a/python/builder.go b/python/builder.go
new file mode 100644
index 00000000..62234486
--- /dev/null
+++ b/python/builder.go
@@ -0,0 +1,146 @@
+// Copyright 2017 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package python
+
+// This file contains Ninja build actions for building Python program.
+
+import (
+ "strings"
+
+ "android/soong/android"
+
+ "github.com/google/blueprint"
+ _ "github.com/google/blueprint/bootstrap"
+)
+
+var (
+ pctx = android.NewPackageContext("android/soong/python")
+
+ par = pctx.AndroidStaticRule("par",
+ blueprint.RuleParams{
+ Command: `touch $initFile && ` +
+ `sed -e 's/%interpreter%/$interp/g' -e 's/%main%/$main/g' $template > $stub && ` +
+ `$parCmd -o $parFile $parArgs && echo '#!/usr/bin/env python' | cat - $parFile > $out && ` +
+ `chmod +x $out && (rm -f $initFile; rm -f $stub; rm -f $parFile)`,
+ CommandDeps: []string{"$parCmd", "$template"},
+ Description: "build par $out",
+ },
+ "initFile", "interp", "main", "template", "stub", "parCmd", "parFile", "parArgs")
+)
+
+func init() {
+ pctx.Import("github.com/google/blueprint/bootstrap")
+ pctx.Import("android/soong/common")
+
+ pctx.HostBinToolVariable("parCmd", "soong_zip")
+}
+
+type fileListSpec struct {
+ fileList android.Path
+ relativeRoot string
+}
+
+type parSpec struct {
+ rootPrefix string
+
+ fileListSpecs []fileListSpec
+}
+
+func (p parSpec) soongParArgs() string {
+ ret := "-P " + p.rootPrefix
+
+ for _, spec := range p.fileListSpecs {
+ ret += " -C " + spec.relativeRoot + " -l " + spec.fileList.String()
+ }
+
+ return ret
+}
+
+func registerBuildActionForModuleFileList(ctx android.ModuleContext,
+ name string, files android.Paths) android.Path {
+ fileList := android.PathForModuleOut(ctx, name+".list")
+
+ content := []string{}
+ for _, file := range files {
+ content = append(content, file.String())
+ }
+
+ ctx.ModuleBuild(pctx, android.ModuleBuildParams{
+ Rule: android.WriteFile,
+ Output: fileList,
+ Implicits: files,
+ Args: map[string]string{
+ "content": strings.Join(content, "\n"),
+ },
+ })
+
+ return fileList
+}
+
+func registerBuildActionForParFile(ctx android.ModuleContext,
+ interpreter, main, binName string, newPyPkgs []string, parSpecs []parSpec) android.Path {
+
+ // intermediate output path for __init__.py
+ initFile := android.PathForModuleOut(ctx, initFileName).String()
+
+ // the path of stub_template_host.txt from source tree.
+ template := android.PathForSource(ctx, stubTemplateHost)
+
+ // intermediate output path for __main__.py
+ stub := android.PathForModuleOut(ctx, mainFileName).String()
+
+ // intermediate output path for par file.
+ parFile := android.PathForModuleOut(ctx, binName+parFileExt)
+
+ // intermediate output path for bin executable.
+ binFile := android.PathForModuleOut(ctx, binName)
+
+ // implicit dependency for parFile build action.
+ implicits := android.Paths{}
+ for _, p := range parSpecs {
+ for _, f := range p.fileListSpecs {
+ implicits = append(implicits, f.fileList)
+ }
+ }
+
+ parArgs := []string{}
+ parArgs = append(parArgs, "-C "+strings.TrimSuffix(stub, mainFileName)+" -f "+stub)
+ parArgs = append(parArgs, "-C "+strings.TrimSuffix(initFile, initFileName)+" -f "+initFile)
+ for _, pkg := range newPyPkgs {
+ parArgs = append(parArgs, "-P "+pkg+" -f "+initFile)
+ }
+ for _, p := range parSpecs {
+ parArgs = append(parArgs, p.soongParArgs())
+ }
+
+ ctx.ModuleBuild(pctx, android.ModuleBuildParams{
+ Rule: par,
+ Output: binFile,
+ Implicits: implicits,
+ Args: map[string]string{
+ "initFile": initFile,
+ // the "\" isn't being interpreted by regex parser, it's being
+ // interpreted in the string literal.
+ "interp": strings.Replace(interpreter, "/", `\/`, -1),
+ "main": strings.Replace(main, "/", `\/`, -1),
+ "template": template.String(),
+ "stub": stub,
+ "parFile": parFile.String(),
+ "parArgs": strings.Join(parArgs, " "),
+ },
+ })
+
+ return binFile
+}