2017-02-27 19:12:13 +01:00
|
|
|
// 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")
|
|
|
|
|
2017-12-18 22:20:23 +01:00
|
|
|
zip = pctx.AndroidStaticRule("zip",
|
2017-02-27 19:12:13 +01:00
|
|
|
blueprint.RuleParams{
|
2017-12-18 22:20:23 +01:00
|
|
|
Command: `$parCmd -o $out $args`,
|
|
|
|
CommandDeps: []string{"$parCmd"},
|
2017-02-27 19:12:13 +01:00
|
|
|
},
|
2017-12-18 22:20:23 +01:00
|
|
|
"args")
|
2017-07-12 21:55:28 +02:00
|
|
|
|
2017-12-23 01:12:00 +01:00
|
|
|
combineZip = pctx.AndroidStaticRule("combineZip",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `$mergeParCmd $out $in`,
|
|
|
|
CommandDeps: []string{"$mergeParCmd"},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-12-18 22:20:23 +01:00
|
|
|
hostPar = pctx.AndroidStaticRule("hostPar",
|
2017-07-12 21:55:28 +02:00
|
|
|
blueprint.RuleParams{
|
2022-09-15 00:25:15 +02:00
|
|
|
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 && ` +
|
2021-04-16 10:00:28 +02:00
|
|
|
`echo "#!/usr/bin/env $interp" >${out}.prefix &&` +
|
2022-09-15 00:25:15 +02:00
|
|
|
`$mergeParCmd -p --prefix ${out}.prefix -pm $out.main $out $srcsZips && ` +
|
|
|
|
`chmod +x $out && (rm -f $out.main; rm -f ${out}.prefix)`,
|
|
|
|
CommandDeps: []string{"$mergeParCmd", "build/soong/python/scripts/stub_template_host.txt"},
|
2017-12-18 22:20:23 +01:00
|
|
|
},
|
2022-09-15 00:25:15 +02:00
|
|
|
"interp", "main", "srcsZips", "addTopDirectoriesToPath")
|
2017-12-18 22:20:23 +01:00
|
|
|
|
2022-09-23 03:25:09 +02:00
|
|
|
hostParWithoutAddingEntrypointFolderToPath = pctx.AndroidStaticRule("hostParWithoutAddingEntrypointFolderToPath",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `sed -e 's/%interpreter%/$interp/g' -e 's/%main%/__soong_entrypoint_redirector__.py/g' -e 's/ADD_TOP_DIRECTORIES_TO_PATH/$addTopDirectoriesToPath/g' build/soong/python/scripts/stub_template_host.txt > $out.main && ` +
|
|
|
|
"sed -e 's/ENTRY_POINT/$main/g' build/soong/python/scripts/main_non_embedded.py >`dirname $out`/__soong_entrypoint_redirector__.py && " +
|
|
|
|
"$parCmd -o $out.entrypoint_zip -C `dirname $out` -f `dirname $out`/__soong_entrypoint_redirector__.py && " +
|
|
|
|
`echo "#!/usr/bin/env $interp" >${out}.prefix &&` +
|
|
|
|
`$mergeParCmd -p --prefix ${out}.prefix -pm $out.main $out $srcsZips $out.entrypoint_zip && ` +
|
|
|
|
"chmod +x $out && (rm -f $out.main; rm -f ${out}.prefix; rm -f $out.entrypoint_zip; rm -f `dirname $out`/__soong_entrypoint_redirector__.py)",
|
|
|
|
CommandDeps: []string{"$mergeParCmd", "$parCmd", "build/soong/python/scripts/stub_template_host.txt", "build/soong/python/scripts/main_non_embedded.py"},
|
|
|
|
},
|
|
|
|
"interp", "main", "srcsZips", "addTopDirectoriesToPath")
|
|
|
|
|
2017-12-18 22:20:23 +01:00
|
|
|
embeddedPar = pctx.AndroidStaticRule("embeddedPar",
|
|
|
|
blueprint.RuleParams{
|
2018-11-30 06:39:59 +01:00
|
|
|
Command: `rm -f $out.main && ` +
|
|
|
|
`sed 's/ENTRY_POINT/$main/' build/soong/python/scripts/main.py >$out.main &&` +
|
|
|
|
`$mergeParCmd -p -pm $out.main --prefix $launcher $out $srcsZips && ` +
|
|
|
|
`chmod +x $out && rm -rf $out.main`,
|
2022-09-23 03:25:09 +02:00
|
|
|
CommandDeps: []string{"$mergeParCmd", "build/soong/python/scripts/main.py"},
|
2017-07-12 21:55:28 +02:00
|
|
|
},
|
2018-11-30 06:39:59 +01:00
|
|
|
"main", "srcsZips", "launcher")
|
2019-02-15 08:17:08 +01:00
|
|
|
|
|
|
|
embeddedParNoMain = pctx.AndroidStaticRule("embeddedParNoMain",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `$mergeParCmd -p --prefix $launcher $out $srcsZips && ` +
|
|
|
|
`chmod +x $out`,
|
|
|
|
CommandDeps: []string{"$mergeParCmd"},
|
|
|
|
},
|
|
|
|
"srcsZips", "launcher")
|
2017-02-27 19:12:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
pctx.Import("github.com/google/blueprint/bootstrap")
|
2019-04-03 01:14:11 +02:00
|
|
|
pctx.Import("android/soong/android")
|
2017-02-27 19:12:13 +01:00
|
|
|
|
|
|
|
pctx.HostBinToolVariable("parCmd", "soong_zip")
|
2017-12-18 22:20:23 +01:00
|
|
|
pctx.HostBinToolVariable("mergeParCmd", "merge_zips")
|
2017-02-27 19:12:13 +01:00
|
|
|
}
|
|
|
|
|
2017-12-18 22:20:23 +01:00
|
|
|
func registerBuildActionForParFile(ctx android.ModuleContext, embeddedLauncher bool,
|
2018-09-27 00:14:10 +02:00
|
|
|
launcherPath android.OptionalPath, interpreter, main, binName string,
|
2022-09-23 03:25:09 +02:00
|
|
|
srcsZips android.Paths, addTopDirectoriesToPath bool, dontAddEntrypointFolderToPath bool) android.Path {
|
2017-02-27 19:12:13 +01:00
|
|
|
|
2017-07-12 21:55:28 +02:00
|
|
|
// .intermediate output path for bin executable.
|
2017-02-27 19:12:13 +01:00
|
|
|
binFile := android.PathForModuleOut(ctx, binName)
|
|
|
|
|
|
|
|
// implicit dependency for parFile build action.
|
2017-12-18 22:20:23 +01:00
|
|
|
implicits := srcsZips
|
2017-02-27 19:12:13 +01:00
|
|
|
|
2017-12-18 22:20:23 +01:00
|
|
|
if !embeddedLauncher {
|
2022-09-15 00:25:15 +02:00
|
|
|
addDirsString := "False"
|
|
|
|
if addTopDirectoriesToPath {
|
|
|
|
addDirsString = "True"
|
|
|
|
}
|
2022-09-23 03:25:09 +02:00
|
|
|
if dontAddEntrypointFolderToPath {
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: hostParWithoutAddingEntrypointFolderToPath,
|
|
|
|
Description: "host python archive",
|
|
|
|
Output: binFile,
|
|
|
|
Implicits: implicits,
|
|
|
|
Args: map[string]string{
|
|
|
|
"interp": strings.Replace(interpreter, "/", `\/`, -1),
|
|
|
|
"main": strings.Replace(strings.TrimSuffix(main, pyExt), "/", ".", -1),
|
|
|
|
"srcsZips": strings.Join(srcsZips.Strings(), " "),
|
|
|
|
"addTopDirectoriesToPath": addDirsString,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: hostPar,
|
|
|
|
Description: "host python archive",
|
|
|
|
Output: binFile,
|
|
|
|
Implicits: implicits,
|
|
|
|
Args: map[string]string{
|
|
|
|
"interp": strings.Replace(interpreter, "/", `\/`, -1),
|
|
|
|
"main": strings.Replace(main, "/", `\/`, -1),
|
|
|
|
"srcsZips": strings.Join(srcsZips.Strings(), " "),
|
|
|
|
"addTopDirectoriesToPath": addDirsString,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2018-09-27 00:14:10 +02:00
|
|
|
} else if launcherPath.Valid() {
|
2017-12-18 22:20:23 +01:00
|
|
|
// added launcherPath to the implicits Ninja dependencies.
|
2018-09-27 00:14:10 +02:00
|
|
|
implicits = append(implicits, launcherPath.Path())
|
2017-07-12 21:55:28 +02:00
|
|
|
|
2019-02-15 08:17:08 +01:00
|
|
|
if main == "" {
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: embeddedParNoMain,
|
|
|
|
Description: "embedded python archive",
|
|
|
|
Output: binFile,
|
|
|
|
Implicits: implicits,
|
|
|
|
Args: map[string]string{
|
|
|
|
"srcsZips": strings.Join(srcsZips.Strings(), " "),
|
|
|
|
"launcher": launcherPath.String(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: embeddedPar,
|
|
|
|
Description: "embedded python archive",
|
|
|
|
Output: binFile,
|
|
|
|
Implicits: implicits,
|
|
|
|
Args: map[string]string{
|
|
|
|
"main": strings.Replace(strings.TrimSuffix(main, pyExt), "/", ".", -1),
|
|
|
|
"srcsZips": strings.Join(srcsZips.Strings(), " "),
|
|
|
|
"launcher": launcherPath.String(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2017-07-12 21:55:28 +02:00
|
|
|
}
|
2017-02-27 19:12:13 +01:00
|
|
|
|
|
|
|
return binFile
|
|
|
|
}
|