2015-03-18 21:28:46 +01:00
// Copyright 2015 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.
2020-11-21 03:30:13 +01:00
// A genrule module takes a list of source files ("srcs" property), an optional
// list of tools ("tools" property), and a command line ("cmd" property), to
// generate output files ("out" property).
2015-03-18 21:28:46 +01:00
package genrule
import (
2016-11-04 23:32:58 +01:00
"fmt"
2019-03-19 06:15:32 +01:00
"io"
2020-11-14 01:23:53 +01:00
"path/filepath"
2019-09-24 00:55:30 +02:00
"strconv"
2016-11-04 23:32:58 +01:00
"strings"
2016-09-29 01:19:10 +02:00
2022-05-10 19:50:12 +02:00
"android/soong/bazel/cquery"
2022-08-20 07:26:38 +02:00
2015-03-23 20:57:34 +01:00
"github.com/google/blueprint"
2017-09-14 01:07:44 +02:00
"github.com/google/blueprint/bootstrap"
2017-11-09 06:20:04 +01:00
"github.com/google/blueprint/proptools"
2015-03-18 21:28:46 +01:00
2016-05-19 00:37:25 +02:00
"android/soong/android"
2020-11-19 11:38:02 +01:00
"android/soong/bazel"
2015-03-18 21:28:46 +01:00
)
2015-06-17 23:20:06 +02:00
func init ( ) {
2020-11-11 03:12:15 +01:00
RegisterGenruleBuildComponents ( android . InitRegistrationContext )
2020-01-16 16:12:04 +01:00
}
2021-03-03 03:30:37 +01:00
// Test fixture preparer that will register most genrule build components.
//
// Singletons and mutators should only be added here if they are needed for a majority of genrule
// module types, otherwise they should be added under a separate preparer to allow them to be
// selected only when needed to reduce test execution time.
//
// Module types do not have much of an overhead unless they are used so this should include as many
// module types as possible. The exceptions are those module types that require mutators and/or
// singletons in order to function in which case they should be kept together in a separate
// preparer.
var PrepareForTestWithGenRuleBuildComponents = android . GroupFixturePreparers (
android . FixtureRegisterWithContext ( RegisterGenruleBuildComponents ) ,
)
// Prepare a fixture to use all genrule module types, mutators and singletons fully.
//
// This should only be used by tests that want to run with as much of the build enabled as possible.
var PrepareForIntegrationTestWithGenrule = android . GroupFixturePreparers (
PrepareForTestWithGenRuleBuildComponents ,
)
2020-11-11 03:12:15 +01:00
func RegisterGenruleBuildComponents ( ctx android . RegistrationContext ) {
2020-01-16 16:12:04 +01:00
ctx . RegisterModuleType ( "genrule_defaults" , defaultsFactory )
ctx . RegisterModuleType ( "gensrcs" , GenSrcsFactory )
ctx . RegisterModuleType ( "genrule" , GenRuleFactory )
2018-12-10 17:13:18 +01:00
2020-01-16 16:12:04 +01:00
ctx . FinalDepsMutators ( func ( ctx android . RegisterMutatorsContext ) {
ctx . BottomUp ( "genrule_tool_deps" , toolDepsMutator ) . Parallel ( )
} )
2021-01-26 15:18:53 +01:00
}
2015-03-18 21:28:46 +01:00
var (
2016-05-19 00:37:25 +02:00
pctx = android . NewPackageContext ( "android/soong/genrule" )
2019-09-24 00:55:30 +02:00
2020-11-21 03:30:13 +01:00
// Used by gensrcs when there is more than 1 shard to merge the outputs
// of each shard into a zip file.
2019-09-24 00:55:30 +02:00
gensrcsMerge = pctx . AndroidStaticRule ( "gensrcsMerge" , blueprint . RuleParams {
Command : "${soongZip} -o ${tmpZip} @${tmpZip}.rsp && ${zipSync} -d ${genDir} ${tmpZip}" ,
CommandDeps : [ ] string { "${soongZip}" , "${zipSync}" } ,
Rspfile : "${tmpZip}.rsp" ,
RspfileContent : "${zipArgs}" ,
} , "tmpZip" , "genDir" , "zipArgs" )
2015-03-18 21:28:46 +01:00
)
2017-03-30 02:29:06 +02:00
func init ( ) {
Expose all deps on genrules
For remote builds, we actually need to know that we depend on all
outputs from a genrule, not just the first one. But having every user
depend on every genrule output increases the ninja file size by >40%.
So to work around the increase in file size, use a ninja phony rule to
produce a single alias to all of the files. Unlike make, where phony
rules are always dirty, ninja does not attempt to stat phony rules as
long as they have inputs, so they act as an alias. (If they don't have
inputs, it will stat the file an only consider it dirty if it doesn't
exist.)
My remote build tooling can then see that it's a phony rule with inputs,
and collapse all of these inputs onto the actual rule.
This only applies to genrules that have >6 outputs, in order to keep the
graph simpler and easier to read for the common case. That's ~10% of the
genrules in AOSP, and it increases the ninja file size by ~1.3%.
Test: manual ninja file inspection
Test: treehugger
Change-Id: I1a26a02fe983f0c92ce726ada3133707223e662e
2019-08-10 01:21:29 +02:00
pctx . Import ( "android/soong/android" )
2019-09-24 00:55:30 +02:00
pctx . HostBinToolVariable ( "soongZip" , "soong_zip" )
pctx . HostBinToolVariable ( "zipSync" , "zipsync" )
2017-03-30 02:29:06 +02:00
}
2015-03-18 21:28:46 +01:00
type SourceFileGenerator interface {
2016-05-19 00:37:25 +02:00
GeneratedSourceFiles ( ) android . Paths
2016-11-22 21:55:55 +01:00
GeneratedHeaderDirs ( ) android . Paths
2018-02-22 03:28:18 +01:00
GeneratedDeps ( ) android . Paths
2015-03-18 21:28:46 +01:00
}
2019-03-29 03:30:56 +01:00
// Alias for android.HostToolProvider
// Deprecated: use android.HostToolProvider instead.
2015-04-28 22:25:36 +02:00
type HostToolProvider interface {
2019-03-29 03:30:56 +01:00
android . HostToolProvider
2015-04-28 22:25:36 +02:00
}
2015-03-18 21:28:46 +01:00
2017-09-14 00:46:47 +02:00
type hostToolDependencyTag struct {
blueprint . BaseDependencyTag
2021-12-11 00:05:02 +01:00
android . LicenseAnnotationToolchainDependencyTag
2018-10-05 08:29:14 +02:00
label string
2017-09-14 00:46:47 +02:00
}
2022-01-13 00:18:30 +01:00
func ( t hostToolDependencyTag ) AllowDisabledModuleDependency ( target android . Module ) bool {
// Allow depending on a disabled module if it's replaced by a prebuilt
// counterpart. We get the prebuilt through android.PrebuiltGetPreferred in
// GenerateAndroidBuildActions.
return target . IsReplacedByPrebuilt ( )
}
var _ android . AllowDisabledModuleDependency = ( * hostToolDependencyTag ) ( nil )
2015-05-11 22:39:40 +02:00
type generatorProperties struct {
2021-07-29 20:26:39 +02:00
// The command to run on one or more input files. Cmd supports substitution of a few variables.
2017-03-30 02:29:06 +02:00
//
// Available variables for substitution:
//
2021-07-29 20:26:39 +02:00
// $(location): the path to the first entry in tools or tool_files.
// $(location <label>): the path to the tool, tool_file, input or output with name <label>. Use $(location) if <label> refers to a rule that outputs exactly one file.
// $(locations <label>): the paths to the tools, tool_files, inputs or outputs with name <label>. Use $(locations) if <label> refers to a rule that outputs two or more files.
// $(in): one or more input files.
// $(out): a single output file.
// $(depfile): a file to which dependencies will be written, if the depfile property is set to true.
// $(genDir): the sandbox directory for this tool; contains $(out).
2017-10-18 06:38:14 +02:00
// $$: a literal $
2017-11-09 06:20:04 +01:00
Cmd * string
2015-05-11 22:39:40 +02:00
2016-11-22 02:23:08 +01:00
// Enable reading a file containing dependencies in gcc format after the command completes
2017-11-09 06:20:04 +01:00
Depfile * bool
2016-11-22 02:23:08 +01:00
2016-11-04 23:32:58 +01:00
// name of the modules (if any) that produces the host executable. Leave empty for
2015-05-11 22:39:40 +02:00
// prebuilts or scripts that do not need a module to build them.
2016-11-04 23:32:58 +01:00
Tools [ ] string
2016-04-20 23:54:32 +02:00
// Local file that is used as the tool
2019-03-05 07:35:41 +01:00
Tool_files [ ] string ` android:"path" `
2016-11-22 21:55:55 +01:00
// List of directories to export generated headers from
Export_include_dirs [ ] string
2017-01-14 03:05:49 +01:00
// list of input files
2019-03-05 07:35:41 +01:00
Srcs [ ] string ` android:"path,arch_variant" `
2018-11-17 23:01:18 +01:00
// input files to exclude
2019-03-05 07:35:41 +01:00
Exclude_srcs [ ] string ` android:"path,arch_variant" `
2020-09-29 08:23:17 +02:00
}
2020-11-21 03:30:13 +01:00
2017-09-14 03:37:08 +02:00
type Module struct {
2016-05-19 00:37:25 +02:00
android . ModuleBase
2018-12-10 17:13:18 +01:00
android . DefaultableModuleBase
2021-02-17 16:17:28 +01:00
android . BazelModuleBase
2019-06-12 06:27:29 +02:00
android . ApexModuleBase
2015-03-18 21:28:46 +01:00
2017-09-14 03:37:08 +02:00
// For other packages to make their own genrules with extra
// properties
Extra interface { }
2021-09-28 00:15:06 +02:00
// CmdModifier can be set by wrappers around genrule to modify the command, for example to
// prefix environment variables to it.
CmdModifier func ( ctx android . ModuleContext , cmd string ) string
2019-11-19 01:00:16 +01:00
android . ImageInterface
2017-09-14 03:37:08 +02:00
2015-05-11 22:39:40 +02:00
properties generatorProperties
2015-04-28 22:25:36 +02:00
2020-11-21 03:30:13 +01:00
// For the different tasks that genrule and gensrc generate. genrule will
// generate 1 task, and gensrc will generate 1 or more tasks based on the
// number of shards the input files are sharded into.
2017-11-08 21:38:00 +01:00
taskGenerator taskFunc
2015-04-28 22:25:36 +02:00
2019-09-24 00:55:30 +02:00
rule blueprint . Rule
rawCommands [ ] string
2015-04-28 22:25:36 +02:00
2016-11-22 21:55:55 +01:00
exportedIncludeDirs android . Paths
2016-04-20 23:21:14 +02:00
2016-05-19 00:37:25 +02:00
outputFiles android . Paths
2018-02-22 03:28:18 +01:00
outputDeps android . Paths
2019-03-19 06:15:32 +01:00
subName string
2019-09-24 00:55:30 +02:00
subDir string
2020-05-21 04:11:59 +02:00
// Collect the module directory for IDE info in java/jdeps.go.
modulePaths [ ] string
2015-03-18 21:28:46 +01:00
}
2022-05-10 19:50:12 +02:00
var _ android . MixedBuildBuildable = ( * Module ) ( nil )
2019-09-24 00:55:30 +02:00
type taskFunc func ( ctx android . ModuleContext , rawCommand string , srcFiles android . Paths ) [ ] generateTask
2015-03-18 21:28:46 +01:00
2015-04-28 22:25:36 +02:00
type generateTask struct {
2020-11-24 22:07:27 +01:00
in android . Paths
out android . WritablePaths
depFile android . WritablePath
2020-11-21 03:30:13 +01:00
copyTo android . WritablePaths // For gensrcs to set on gensrcsMerge rule.
2020-11-24 22:07:27 +01:00
genDir android . WritablePath
extraTools android . Paths // dependencies on tools used by the generator
2020-11-21 03:30:13 +01:00
cmd string
// For gensrsc sharding.
2020-11-24 22:07:27 +01:00
shard int
shards int
2015-03-18 21:28:46 +01:00
}
2017-09-14 03:37:08 +02:00
func ( g * Module ) GeneratedSourceFiles ( ) android . Paths {
2015-04-28 22:25:36 +02:00
return g . outputFiles
}
2015-03-18 21:28:46 +01:00
2017-09-14 03:37:08 +02:00
func ( g * Module ) Srcs ( ) android . Paths {
2018-03-28 01:19:42 +02:00
return append ( android . Paths { } , g . outputFiles ... )
2016-12-14 00:23:47 +01:00
}
2017-09-14 03:37:08 +02:00
func ( g * Module ) GeneratedHeaderDirs ( ) android . Paths {
2016-11-22 21:55:55 +01:00
return g . exportedIncludeDirs
2016-04-20 23:21:14 +02:00
}
2018-02-22 03:28:18 +01:00
func ( g * Module ) GeneratedDeps ( ) android . Paths {
return g . outputDeps
}
2021-06-28 10:35:58 +02:00
func ( g * Module ) OutputFiles ( tag string ) ( android . Paths , error ) {
if tag == "" {
return append ( android . Paths { } , g . outputFiles ... ) , nil
}
// otherwise, tag should match one of outputs
for _ , outputFile := range g . outputFiles {
if outputFile . Rel ( ) == tag {
return android . Paths { outputFile } , nil
}
}
return nil , fmt . Errorf ( "unsupported module reference tag %q" , tag )
}
var _ android . SourceFileProducer = ( * Module ) ( nil )
var _ android . OutputFileProducer = ( * Module ) ( nil )
2020-01-16 16:12:04 +01:00
func toolDepsMutator ( ctx android . BottomUpMutatorContext ) {
2017-09-14 03:37:08 +02:00
if g , ok := ctx . Module ( ) . ( * Module ) ; ok {
2018-10-05 08:29:14 +02:00
for _ , tool := range g . properties . Tools {
tag := hostToolDependencyTag { label : tool }
if m := android . SrcIsModule ( tool ) ; m != "" {
tool = m
}
2019-10-16 20:03:10 +02:00
ctx . AddFarVariationDependencies ( ctx . Config ( ) . BuildOSTarget . Variations ( ) , tag , tool )
2015-10-29 23:25:03 +01:00
}
2015-04-28 22:25:36 +02:00
}
2015-03-18 21:28:46 +01:00
}
2022-05-10 19:50:12 +02:00
func ( g * Module ) ProcessBazelQueryResponse ( ctx android . ModuleContext ) {
g . generateCommonBuildActions ( ctx )
label := g . GetBazelLabel ( ctx , g )
2020-09-29 08:23:17 +02:00
bazelCtx := ctx . Config ( ) . BazelContext
2022-05-10 19:50:12 +02:00
filePaths , err := bazelCtx . GetOutputFiles ( label , android . GetConfigKey ( ctx ) )
if err != nil {
ctx . ModuleErrorf ( err . Error ( ) )
return
}
var bazelOutputFiles android . Paths
exportIncludeDirs := map [ string ] bool { }
for _ , bazelOutputFile := range filePaths {
2022-06-02 21:11:12 +02:00
bazelOutputFiles = append ( bazelOutputFiles , android . PathForBazelOutRelative ( ctx , ctx . ModuleDir ( ) , bazelOutputFile ) )
2022-05-10 19:50:12 +02:00
exportIncludeDirs [ filepath . Dir ( bazelOutputFile ) ] = true
}
g . outputFiles = bazelOutputFiles
g . outputDeps = bazelOutputFiles
for includePath , _ := range exportIncludeDirs {
g . exportedIncludeDirs = append ( g . exportedIncludeDirs , android . PathForBazelOut ( ctx , includePath ) )
2020-09-29 08:23:17 +02:00
}
}
2020-11-21 00:28:30 +01:00
2022-05-10 19:50:12 +02:00
// generateCommonBuildActions contains build action generation logic
// common to both the mixed build case and the legacy case of genrule processing.
// To fully support genrule in mixed builds, the contents of this function should
// approach zero; there should be no genrule action registration done directly
// by Soong logic in the mixed-build case.
func ( g * Module ) generateCommonBuildActions ( ctx android . ModuleContext ) {
2019-03-19 06:15:32 +01:00
g . subName = ctx . ModuleSubDir ( )
2020-05-21 04:11:59 +02:00
// Collect the module directory for IDE info in java/jdeps.go.
g . modulePaths = append ( g . modulePaths , ctx . ModuleDir ( ) )
2016-11-22 21:55:55 +01:00
if len ( g . properties . Export_include_dirs ) > 0 {
for _ , dir := range g . properties . Export_include_dirs {
g . exportedIncludeDirs = append ( g . exportedIncludeDirs ,
2019-09-24 00:55:30 +02:00
android . PathForModuleGen ( ctx , g . subDir , ctx . ModuleDir ( ) , dir ) )
2016-11-22 21:55:55 +01:00
}
} else {
2019-09-24 00:55:30 +02:00
g . exportedIncludeDirs = append ( g . exportedIncludeDirs , android . PathForModuleGen ( ctx , g . subDir ) )
2016-11-22 21:55:55 +01:00
}
2016-09-29 01:19:10 +02:00
2021-03-24 06:30:35 +01:00
locationLabels := map [ string ] location { }
2018-10-05 08:29:14 +02:00
firstLabel := ""
2021-03-24 06:30:35 +01:00
addLocationLabel := func ( label string , loc location ) {
2018-10-05 08:29:14 +02:00
if firstLabel == "" {
firstLabel = label
}
if _ , exists := locationLabels [ label ] ; ! exists {
2021-03-24 06:30:35 +01:00
locationLabels [ label ] = loc
2018-10-05 08:29:14 +02:00
} else {
2021-10-08 17:13:10 +02:00
ctx . ModuleErrorf ( "multiple locations for label %q: %q and %q (do you have duplicate srcs entries?)" ,
2021-03-24 06:30:35 +01:00
label , locationLabels [ label ] , loc )
2018-10-05 08:29:14 +02:00
}
}
2016-09-29 01:19:10 +02:00
2020-11-25 01:32:22 +01:00
var tools android . Paths
var packagedTools [ ] android . PackagingSpec
2016-11-04 23:32:58 +01:00
if len ( g . properties . Tools ) > 0 {
2019-03-18 20:12:48 +01:00
seenTools := make ( map [ string ] bool )
2017-11-16 09:11:20 +01:00
ctx . VisitDirectDepsBlueprint ( func ( module blueprint . Module ) {
2018-10-05 08:29:14 +02:00
switch tag := ctx . OtherModuleDependencyTag ( module ) . ( type ) {
case hostToolDependencyTag :
2017-09-14 00:46:47 +02:00
tool := ctx . OtherModuleName ( module )
2022-01-13 00:18:30 +01:00
if m , ok := module . ( android . Module ) ; ok {
// Necessary to retrieve any prebuilt replacement for the tool, since
// toolDepsMutator runs too late for the prebuilt mutators to have
// replaced the dependency.
module = android . PrebuiltGetPreferred ( ctx , m )
}
2017-09-14 00:46:47 +02:00
2020-11-25 01:32:22 +01:00
switch t := module . ( type ) {
case android . HostToolProvider :
// A HostToolProvider provides the path to a tool, which will be copied
// into the sandbox.
2017-11-16 09:11:20 +01:00
if ! t . ( android . Module ) . Enabled ( ) {
2017-11-29 09:27:14 +01:00
if ctx . Config ( ) . AllowMissingDependencies ( ) {
2017-11-16 09:11:20 +01:00
ctx . AddMissingDependencies ( [ ] string { tool } )
} else {
ctx . ModuleErrorf ( "depends on disabled module %q" , tool )
}
2020-11-25 01:32:22 +01:00
return
2017-11-16 09:11:20 +01:00
}
2020-11-25 01:32:22 +01:00
path := t . HostToolPath ( )
if ! path . Valid ( ) {
ctx . ModuleErrorf ( "host tool %q missing output file" , tool )
return
}
if specs := t . TransitivePackagingSpecs ( ) ; specs != nil {
// If the HostToolProvider has PackgingSpecs, which are definitions of the
// required relative locations of the tool and its dependencies, use those
// instead. They will be copied to those relative locations in the sbox
// sandbox.
packagedTools = append ( packagedTools , specs ... )
// Assume that the first PackagingSpec of the module is the tool.
2021-03-24 06:30:35 +01:00
addLocationLabel ( tag . label , packagedToolLocation { specs [ 0 ] } )
2020-11-25 01:32:22 +01:00
} else {
tools = append ( tools , path . Path ( ) )
2021-03-24 06:30:35 +01:00
addLocationLabel ( tag . label , toolLocation { android . Paths { path . Path ( ) } } )
2020-11-25 01:32:22 +01:00
}
case bootstrap . GoBinaryTool :
// A GoBinaryTool provides the install path to a tool, which will be copied.
2021-10-26 00:36:21 +02:00
p := android . PathForGoBinary ( ctx , t )
tools = append ( tools , p )
addLocationLabel ( tag . label , toolLocation { android . Paths { p } } )
2020-11-25 01:32:22 +01:00
default :
2017-09-14 00:46:47 +02:00
ctx . ModuleErrorf ( "%q is not a host tool provider" , tool )
2020-11-25 01:32:22 +01:00
return
2017-09-14 01:07:44 +02:00
}
2020-11-25 01:32:22 +01:00
seenTools [ tag . label ] = true
2015-04-28 22:25:36 +02:00
}
2016-04-20 23:54:32 +02:00
} )
2019-03-18 20:12:48 +01:00
// If AllowMissingDependencies is enabled, the build will not have stopped when
// AddFarVariationDependencies was called on a missing tool, which will result in nonsensical
2020-07-28 20:32:07 +02:00
// "cmd: unknown location label ..." errors later. Add a placeholder file to the local label.
// The command that uses this placeholder file will never be executed because the rule will be
// replaced with an android.Error rule reporting the missing dependencies.
2019-03-18 20:12:48 +01:00
if ctx . Config ( ) . AllowMissingDependencies ( ) {
for _ , tool := range g . properties . Tools {
if ! seenTools [ tool ] {
2021-03-24 06:30:35 +01:00
addLocationLabel ( tool , errorLocation { "***missing tool " + tool + "***" } )
2019-03-18 20:12:48 +01:00
}
}
}
2016-04-20 23:54:32 +02:00
}
2015-03-18 21:28:46 +01:00
2017-09-14 00:46:47 +02:00
if ctx . Failed ( ) {
return
}
2018-10-05 08:29:14 +02:00
for _ , toolFile := range g . properties . Tool_files {
2019-03-06 07:25:09 +01:00
paths := android . PathsForModuleSrc ( ctx , [ ] string { toolFile } )
2020-11-25 01:32:22 +01:00
tools = append ( tools , paths ... )
2021-03-24 06:30:35 +01:00
addLocationLabel ( toolFile , toolLocation { paths } )
2016-11-04 23:32:58 +01:00
}
2022-01-28 21:13:39 +01:00
includeDirInPaths := ctx . DeviceConfig ( ) . BuildBrokenInputDir ( g . Name ( ) )
2018-10-05 08:29:14 +02:00
var srcFiles android . Paths
for _ , in := range g . properties . Srcs {
2022-01-28 21:13:39 +01:00
paths , missingDeps := android . PathsAndMissingDepsRelativeToModuleSourceDir ( android . SourceInput {
Context : ctx , Paths : [ ] string { in } , ExcludePaths : g . properties . Exclude_srcs , IncludeDirs : includeDirInPaths ,
} )
2019-03-18 20:12:48 +01:00
if len ( missingDeps ) > 0 {
if ! ctx . Config ( ) . AllowMissingDependencies ( ) {
panic ( fmt . Errorf ( "should never get here, the missing dependencies %q should have been reported in DepsMutator" ,
missingDeps ) )
}
// If AllowMissingDependencies is enabled, the build will not have stopped when
// the dependency was added on a missing SourceFileProducer module, which will result in nonsensical
2020-07-28 20:32:07 +02:00
// "cmd: label ":..." has no files" errors later. Add a placeholder file to the local label.
// The command that uses this placeholder file will never be executed because the rule will be
// replaced with an android.Error rule reporting the missing dependencies.
2019-03-18 20:12:48 +01:00
ctx . AddMissingDependencies ( missingDeps )
2021-03-24 06:30:35 +01:00
addLocationLabel ( in , errorLocation { "***missing srcs " + in + "***" } )
2019-03-18 20:12:48 +01:00
} else {
srcFiles = append ( srcFiles , paths ... )
2021-03-24 06:30:35 +01:00
addLocationLabel ( in , inputLocation { paths } )
2019-03-18 20:12:48 +01:00
}
2018-10-05 08:29:14 +02:00
}
2017-10-27 23:59:27 +02:00
2019-09-24 00:55:30 +02:00
var copyFrom android . Paths
var outputFiles android . WritablePaths
var zipArgs strings . Builder
2018-10-05 08:29:14 +02:00
2021-09-28 00:15:06 +02:00
cmd := String ( g . properties . Cmd )
if g . CmdModifier != nil {
cmd = g . CmdModifier ( ctx , cmd )
}
2020-11-21 03:30:13 +01:00
// Generate tasks, either from genrule or gensrcs.
2021-09-28 00:15:06 +02:00
for _ , task := range g . taskGenerator ( ctx , cmd , srcFiles ) {
2020-11-14 01:23:53 +01:00
if len ( task . out ) == 0 {
ctx . ModuleErrorf ( "must have at least one output file" )
return
}
2020-11-17 02:32:30 +01:00
// Pick a unique path outside the task.genDir for the sbox manifest textproto,
// a unique rule name, and the user-visible description.
manifestName := "genrule.sbox.textproto"
desc := "generate"
name := "generator"
if task . shards > 0 {
manifestName = "genrule_" + strconv . Itoa ( task . shard ) + ".sbox.textproto"
desc += " " + strconv . Itoa ( task . shard )
name += strconv . Itoa ( task . shard )
} else if len ( task . out ) == 1 {
desc += " " + task . out [ 0 ] . Base ( )
}
manifestPath := android . PathForModuleOut ( ctx , manifestName )
// Use a RuleBuilder to create a rule that runs the command inside an sbox sandbox.
2020-11-25 01:32:22 +01:00
rule := android . NewRuleBuilder ( pctx , ctx ) . Sbox ( task . genDir , manifestPath ) . SandboxTools ( )
2020-11-17 02:32:30 +01:00
cmd := rule . Command ( )
2019-09-24 00:55:30 +02:00
for _ , out := range task . out {
2021-03-24 06:30:35 +01:00
addLocationLabel ( out . Rel ( ) , outputLocation { out } )
2018-07-09 18:45:06 +02:00
}
2019-09-24 00:55:30 +02:00
referencedDepfile := false
2020-11-14 01:23:53 +01:00
rawCommand , err := android . Expand ( task . cmd , func ( name string ) ( string , error ) {
2019-09-24 00:55:30 +02:00
// report the error directly without returning an error to android.Expand to catch multiple errors in a
// single run
2020-11-14 01:23:53 +01:00
reportError := func ( fmt string , args ... interface { } ) ( string , error ) {
2019-09-24 00:55:30 +02:00
ctx . PropertyErrorf ( "cmd" , fmt , args ... )
2020-11-14 01:23:53 +01:00
return "SOONG_ERROR" , nil
2016-11-22 02:23:08 +01:00
}
2019-09-24 00:55:30 +02:00
2022-08-20 07:26:38 +02:00
// Apply shell escape to each cases to prevent source file paths containing $ from being evaluated in shell
2019-09-24 00:55:30 +02:00
switch name {
case "location" :
if len ( g . properties . Tools ) == 0 && len ( g . properties . Tool_files ) == 0 {
return reportError ( "at least one `tools` or `tool_files` is required if $(location) is used" )
}
2021-03-24 06:30:35 +01:00
loc := locationLabels [ firstLabel ]
paths := loc . Paths ( cmd )
2019-09-24 00:55:30 +02:00
if len ( paths ) == 0 {
return reportError ( "default label %q has no files" , firstLabel )
} else if len ( paths ) > 1 {
return reportError ( "default label %q has multiple files, use $(locations %s) to reference it" ,
firstLabel , firstLabel )
}
2022-08-20 07:26:38 +02:00
return proptools . ShellEscape ( paths [ 0 ] ) , nil
2019-09-24 00:55:30 +02:00
case "in" :
2022-08-20 07:26:38 +02:00
return strings . Join ( proptools . ShellEscapeList ( cmd . PathsForInputs ( srcFiles ) ) , " " ) , nil
2019-09-24 00:55:30 +02:00
case "out" :
2020-11-14 01:23:53 +01:00
var sandboxOuts [ ] string
for _ , out := range task . out {
2020-11-17 02:32:30 +01:00
sandboxOuts = append ( sandboxOuts , cmd . PathForOutput ( out ) )
2020-11-14 01:23:53 +01:00
}
2022-08-20 07:26:38 +02:00
return strings . Join ( proptools . ShellEscapeList ( sandboxOuts ) , " " ) , nil
2019-09-24 00:55:30 +02:00
case "depfile" :
referencedDepfile = true
if ! Bool ( g . properties . Depfile ) {
return reportError ( "$(depfile) used without depfile property" )
2016-11-04 23:32:58 +01:00
}
2020-11-14 01:23:53 +01:00
return "__SBOX_DEPFILE__" , nil
2019-09-24 00:55:30 +02:00
case "genDir" :
2022-08-20 07:26:38 +02:00
return proptools . ShellEscape ( cmd . PathForOutput ( task . genDir ) ) , nil
2019-09-24 00:55:30 +02:00
default :
if strings . HasPrefix ( name , "location " ) {
label := strings . TrimSpace ( strings . TrimPrefix ( name , "location " ) )
2021-03-24 06:30:35 +01:00
if loc , ok := locationLabels [ label ] ; ok {
paths := loc . Paths ( cmd )
2019-09-24 00:55:30 +02:00
if len ( paths ) == 0 {
return reportError ( "label %q has no files" , label )
} else if len ( paths ) > 1 {
return reportError ( "label %q has multiple files, use $(locations %s) to reference it" ,
label , label )
}
2022-08-20 07:26:38 +02:00
return proptools . ShellEscape ( paths [ 0 ] ) , nil
2019-09-24 00:55:30 +02:00
} else {
2022-02-23 12:42:38 +01:00
return reportError ( "unknown location label %q is not in srcs, out, tools or tool_files." , label )
2019-09-24 00:55:30 +02:00
}
} else if strings . HasPrefix ( name , "locations " ) {
label := strings . TrimSpace ( strings . TrimPrefix ( name , "locations " ) )
2021-03-24 06:30:35 +01:00
if loc , ok := locationLabels [ label ] ; ok {
paths := loc . Paths ( cmd )
2019-09-24 00:55:30 +02:00
if len ( paths ) == 0 {
return reportError ( "label %q has no files" , label )
}
2022-08-20 07:26:38 +02:00
return proptools . ShellEscape ( strings . Join ( paths , " " ) ) , nil
2019-09-24 00:55:30 +02:00
} else {
2022-02-23 12:42:38 +01:00
return reportError ( "unknown locations label %q is not in srcs, out, tools or tool_files." , label )
2018-10-05 08:29:14 +02:00
}
} else {
2019-09-24 00:55:30 +02:00
return reportError ( "unknown variable '$(%s)'" , name )
2018-10-05 08:29:14 +02:00
}
2016-11-04 23:32:58 +01:00
}
2019-09-24 00:55:30 +02:00
} )
if err != nil {
ctx . PropertyErrorf ( "cmd" , "%s" , err . Error ( ) )
return
2016-11-04 23:32:58 +01:00
}
2019-09-24 00:55:30 +02:00
if Bool ( g . properties . Depfile ) && ! referencedDepfile {
ctx . PropertyErrorf ( "cmd" , "specified depfile=true but did not include a reference to '${depfile}' in cmd" )
return
}
g . rawCommands = append ( g . rawCommands , rawCommand )
2020-02-14 00:55:10 +01:00
2020-11-14 01:23:53 +01:00
cmd . Text ( rawCommand )
cmd . ImplicitOutputs ( task . out )
cmd . Implicits ( task . in )
2020-11-25 01:32:22 +01:00
cmd . ImplicitTools ( tools )
cmd . ImplicitTools ( task . extraTools )
cmd . ImplicitPackagedTools ( packagedTools )
2019-09-24 00:55:30 +02:00
if Bool ( g . properties . Depfile ) {
2020-11-14 01:23:53 +01:00
cmd . ImplicitDepFile ( task . depFile )
2019-09-24 00:55:30 +02:00
}
2017-11-06 23:15:16 +01:00
2020-11-14 01:23:53 +01:00
// Create the rule to run the genrule command inside sbox.
2020-11-17 02:32:30 +01:00
rule . Build ( name , desc )
2017-03-30 02:29:06 +02:00
2019-09-24 00:55:30 +02:00
if len ( task . copyTo ) > 0 {
2020-11-14 01:23:53 +01:00
// If copyTo is set, multiple shards need to be copied into a single directory.
// task.out contains the per-shard paths, and copyTo contains the corresponding
// final path. The files need to be copied into the final directory by a
// single rule so it can remove the directory before it starts to ensure no
// old files remain. zipsync already does this, so build up zipArgs that
// zip all the per-shard directories into a single zip.
2019-09-24 00:55:30 +02:00
outputFiles = append ( outputFiles , task . copyTo ... )
copyFrom = append ( copyFrom , task . out . Paths ( ) ... )
zipArgs . WriteString ( " -C " + task . genDir . String ( ) )
zipArgs . WriteString ( android . JoinWithPrefix ( task . out . Strings ( ) , " -f " ) )
} else {
outputFiles = append ( outputFiles , task . out ... )
}
2016-11-22 02:23:08 +01:00
}
2019-09-24 00:55:30 +02:00
if len ( copyFrom ) > 0 {
2020-11-14 01:23:53 +01:00
// Create a rule that zips all the per-shard directories into a single zip and then
// uses zipsync to unzip it into the final directory.
2019-09-24 00:55:30 +02:00
ctx . Build ( pctx , android . BuildParams {
2020-11-21 00:28:30 +01:00
Rule : gensrcsMerge ,
Implicits : copyFrom ,
Outputs : outputFiles ,
Description : "merge shards" ,
2019-09-24 00:55:30 +02:00
Args : map [ string ] string {
"zipArgs" : zipArgs . String ( ) ,
"tmpZip" : android . PathForModuleGen ( ctx , g . subDir + ".zip" ) . String ( ) ,
"genDir" : android . PathForModuleGen ( ctx , g . subDir ) . String ( ) ,
} ,
} )
2016-11-22 02:23:08 +01:00
}
2016-11-04 23:32:58 +01:00
2019-09-24 00:55:30 +02:00
g . outputFiles = outputFiles . Paths ( )
2022-05-10 19:50:12 +02:00
}
2019-09-24 00:55:30 +02:00
2022-05-10 19:50:12 +02:00
func ( g * Module ) GenerateAndroidBuildActions ( ctx android . ModuleContext ) {
2022-06-10 20:23:27 +02:00
// Allowlist genrule to use depfile until we have a solution to remove it.
// TODO(b/235582219): Remove allowlist for genrule
if ctx . ModuleType ( ) == "gensrcs" &&
! ctx . DeviceConfig ( ) . BuildBrokenDepfile ( ) &&
Bool ( g . properties . Depfile ) {
ctx . PropertyErrorf (
"depfile" ,
"Deprecated to ensure the module type is convertible to Bazel. " +
"Try specifying the dependencies explicitly so that there is no need to use depfile. " +
"If not possible, the escape hatch is to use BUILD_BROKEN_DEPFILE to bypass the error." )
}
2022-05-10 19:50:12 +02:00
g . generateCommonBuildActions ( ctx )
// For <= 6 outputs, just embed those directly in the users. Right now, that covers >90% of
// the genrules on AOSP. That will make things simpler to look at the graph in the common
// case. For larger sets of outputs, inject a phony target in between to limit ninja file
// growth.
if len ( g . outputFiles ) <= 6 {
g . outputDeps = g . outputFiles
} else {
phonyFile := android . PathForModuleGen ( ctx , "genrule-phony" )
ctx . Build ( pctx , android . BuildParams {
Rule : blueprint . Phony ,
Output : phonyFile ,
Inputs : g . outputFiles ,
} )
g . outputDeps = android . Paths { phonyFile }
2019-09-24 00:55:30 +02:00
}
2015-04-28 22:25:36 +02:00
}
2022-05-10 19:50:12 +02:00
func ( g * Module ) QueueBazelCall ( ctx android . BaseModuleContext ) {
bazelCtx := ctx . Config ( ) . BazelContext
bazelCtx . QueueBazelRequest ( g . GetBazelLabel ( ctx , g ) , cquery . GetOutputFiles , android . GetConfigKey ( ctx ) )
}
func ( g * Module ) IsMixedBuildSupported ( ctx android . BaseModuleContext ) bool {
return true
}
2018-08-16 00:35:38 +02:00
// Collect information for opening IDE project files in java/jdeps.go.
func ( g * Module ) IDEInfo ( dpInfo * android . IdeInfo ) {
dpInfo . Srcs = append ( dpInfo . Srcs , g . Srcs ( ) . Strings ( ) ... )
for _ , src := range g . properties . Srcs {
if strings . HasPrefix ( src , ":" ) {
src = strings . Trim ( src , ":" )
dpInfo . Deps = append ( dpInfo . Deps , src )
}
}
2020-05-21 04:11:59 +02:00
dpInfo . Paths = append ( dpInfo . Paths , g . modulePaths ... )
2018-08-16 00:35:38 +02:00
}
2019-03-19 06:15:32 +01:00
func ( g * Module ) AndroidMk ( ) android . AndroidMkData {
return android . AndroidMkData {
2020-10-30 17:34:45 +01:00
Class : "ETC" ,
2019-03-19 06:15:32 +01:00
OutputFile : android . OptionalPathForPath ( g . outputFiles [ 0 ] ) ,
SubName : g . subName ,
Extra : [ ] android . AndroidMkExtraFunc {
func ( w io . Writer , outputFile android . Path ) {
2020-10-30 17:34:45 +01:00
fmt . Fprintln ( w , "LOCAL_UNINSTALLABLE_MODULE := true" )
2019-03-19 06:15:32 +01:00
} ,
} ,
Custom : func ( w io . Writer , name , prefix , moduleDir string , data android . AndroidMkData ) {
android . WriteAndroidMkData ( w , data )
if data . SubName != "" {
fmt . Fprintln ( w , ".PHONY:" , name )
fmt . Fprintln ( w , name , ":" , name + g . subName )
}
} ,
}
}
2020-12-15 14:29:02 +01:00
var _ android . ApexModule = ( * Module ) ( nil )
// Implements android.ApexModule
2020-07-23 07:32:17 +02:00
func ( g * Module ) ShouldSupportSdkVersion ( ctx android . BaseModuleContext ,
sdkVersion android . ApiLevel ) error {
2020-04-15 04:03:39 +02:00
// Because generated outputs are checked by client modules(e.g. cc_library, ...)
// we can safely ignore the check here.
return nil
}
2017-11-08 21:38:00 +01:00
func generatorFactory ( taskGenerator taskFunc , props ... interface { } ) * Module {
2017-09-14 03:37:08 +02:00
module := & Module {
2017-11-08 21:38:00 +01:00
taskGenerator : taskGenerator ,
2015-03-18 21:28:46 +01:00
}
2015-04-28 22:25:36 +02:00
2017-06-24 00:06:31 +02:00
module . AddProperties ( props ... )
module . AddProperties ( & module . properties )
2015-04-28 22:25:36 +02:00
2019-11-19 01:00:16 +01:00
module . ImageInterface = noopImageInterface { }
2017-06-24 00:06:31 +02:00
return module
2015-03-18 21:28:46 +01:00
}
2019-11-19 01:00:16 +01:00
type noopImageInterface struct { }
func ( x noopImageInterface ) ImageMutatorBegin ( android . BaseModuleContext ) { }
func ( x noopImageInterface ) CoreVariantNeeded ( android . BaseModuleContext ) bool { return false }
2020-01-22 00:53:22 +01:00
func ( x noopImageInterface ) RamdiskVariantNeeded ( android . BaseModuleContext ) bool { return false }
2020-10-22 00:17:56 +02:00
func ( x noopImageInterface ) VendorRamdiskVariantNeeded ( android . BaseModuleContext ) bool { return false }
2021-04-08 14:13:22 +02:00
func ( x noopImageInterface ) DebugRamdiskVariantNeeded ( android . BaseModuleContext ) bool { return false }
2019-11-19 01:00:16 +01:00
func ( x noopImageInterface ) RecoveryVariantNeeded ( android . BaseModuleContext ) bool { return false }
func ( x noopImageInterface ) ExtraImageVariations ( ctx android . BaseModuleContext ) [ ] string { return nil }
func ( x noopImageInterface ) SetImageVariation ( ctx android . BaseModuleContext , variation string , module android . Module ) {
}
2017-09-14 03:37:08 +02:00
func NewGenSrcs ( ) * Module {
2015-04-28 22:25:36 +02:00
properties := & genSrcsProperties { }
2020-11-21 00:28:30 +01:00
// finalSubDir is the name of the subdirectory that output files will be generated into.
// It is used so that per-shard directories can be placed alongside it an then finally
// merged into it.
const finalSubDir = "gensrcs"
2019-09-24 00:55:30 +02:00
taskGenerator := func ( ctx android . ModuleContext , rawCommand string , srcFiles android . Paths ) [ ] generateTask {
shardSize := defaultShardSize
if s := properties . Shard_size ; s != nil {
shardSize = int ( * s )
}
2020-11-21 00:28:30 +01:00
// gensrcs rules can easily hit command line limits by repeating the command for
// every input file. Shard the input files into groups.
2019-09-24 00:55:30 +02:00
shards := android . ShardPaths ( srcFiles , shardSize )
var generateTasks [ ] generateTask
for i , shard := range shards {
var commands [ ] string
var outFiles android . WritablePaths
2020-11-24 22:07:27 +01:00
var commandDepFiles [ ] string
2019-09-24 00:55:30 +02:00
var copyTo android . WritablePaths
2020-11-21 00:28:30 +01:00
// When sharding is enabled (i.e. len(shards) > 1), the sbox rules for each
// shard will be write to their own directories and then be merged together
// into finalSubDir. If sharding is not enabled (i.e. len(shards) == 1),
// the sbox rule will write directly to finalSubDir.
genSubDir := finalSubDir
2019-09-24 00:55:30 +02:00
if len ( shards ) > 1 {
2020-11-21 00:28:30 +01:00
genSubDir = strconv . Itoa ( i )
2017-11-08 21:38:00 +01:00
}
2020-11-21 00:28:30 +01:00
genDir := android . PathForModuleGen ( ctx , genSubDir )
2020-11-17 02:32:30 +01:00
// TODO(ccross): this RuleBuilder is a hack to be able to call
// rule.Command().PathForOutput. Replace this with passing the rule into the
// generator.
2020-11-25 01:32:22 +01:00
rule := android . NewRuleBuilder ( pctx , ctx ) . Sbox ( genDir , nil ) . SandboxTools ( )
2020-11-21 00:28:30 +01:00
2020-11-24 22:07:27 +01:00
for _ , in := range shard {
2020-11-21 00:28:30 +01:00
outFile := android . GenPathWithExt ( ctx , finalSubDir , in , String ( properties . Output_extension ) )
2017-11-08 21:38:00 +01:00
2020-11-21 00:28:30 +01:00
// If sharding is enabled, then outFile is the path to the output file in
// the shard directory, and copyTo is the path to the output file in the
// final directory.
2019-09-24 00:55:30 +02:00
if len ( shards ) > 1 {
2020-11-21 00:28:30 +01:00
shardFile := android . GenPathWithExt ( ctx , genSubDir , in , String ( properties . Output_extension ) )
2019-09-24 00:55:30 +02:00
copyTo = append ( copyTo , outFile )
outFile = shardFile
}
outFiles = append ( outFiles , outFile )
2020-11-21 00:28:30 +01:00
// pre-expand the command line to replace $in and $out with references to
// a single input and output file.
2019-09-24 00:55:30 +02:00
command , err := android . Expand ( rawCommand , func ( name string ) ( string , error ) {
switch name {
case "in" :
return in . String ( ) , nil
case "out" :
2020-11-17 02:32:30 +01:00
return rule . Command ( ) . PathForOutput ( outFile ) , nil
2020-11-24 22:07:27 +01:00
case "depfile" :
// Generate a depfile for each output file. Store the list for
// later in order to combine them all into a single depfile.
2020-11-17 02:32:30 +01:00
depFile := rule . Command ( ) . PathForOutput ( outFile . ReplaceExtension ( ctx , "d" ) )
2020-11-24 22:07:27 +01:00
commandDepFiles = append ( commandDepFiles , depFile )
return depFile , nil
2019-09-24 00:55:30 +02:00
default :
return "$(" + name + ")" , nil
}
} )
if err != nil {
ctx . PropertyErrorf ( "cmd" , err . Error ( ) )
}
// escape the command in case for example it contains '#', an odd number of '"', etc
command = fmt . Sprintf ( "bash -c %v" , proptools . ShellEscape ( command ) )
commands = append ( commands , command )
}
fullCommand := strings . Join ( commands , " && " )
2020-11-24 22:07:27 +01:00
var outputDepfile android . WritablePath
var extraTools android . Paths
if len ( commandDepFiles ) > 0 {
// Each command wrote to a depfile, but ninja can only handle one
// depfile per rule. Use the dep_fixer tool at the end of the
// command to combine all the depfiles into a single output depfile.
outputDepfile = android . PathForModuleGen ( ctx , genSubDir , "gensrcs.d" )
depFixerTool := ctx . Config ( ) . HostToolPath ( ctx , "dep_fixer" )
fullCommand += fmt . Sprintf ( " && %s -o $(depfile) %s" ,
2021-03-24 06:30:35 +01:00
rule . Command ( ) . PathForTool ( depFixerTool ) ,
2020-11-25 01:32:22 +01:00
strings . Join ( commandDepFiles , " " ) )
2020-11-24 22:07:27 +01:00
extraTools = append ( extraTools , depFixerTool )
}
2019-09-24 00:55:30 +02:00
generateTasks = append ( generateTasks , generateTask {
2020-11-24 22:07:27 +01:00
in : shard ,
out : outFiles ,
depFile : outputDepfile ,
copyTo : copyTo ,
genDir : genDir ,
cmd : fullCommand ,
shard : i ,
shards : len ( shards ) ,
extraTools : extraTools ,
2019-09-24 00:55:30 +02:00
} )
2015-04-28 22:25:36 +02:00
}
2019-09-24 00:55:30 +02:00
return generateTasks
2015-04-28 22:25:36 +02:00
}
2015-03-18 21:28:46 +01:00
2019-09-24 00:55:30 +02:00
g := generatorFactory ( taskGenerator , properties )
2020-11-21 00:28:30 +01:00
g . subDir = finalSubDir
2019-09-24 00:55:30 +02:00
return g
2015-04-28 22:25:36 +02:00
}
2017-10-10 00:34:10 +02:00
func GenSrcsFactory ( ) android . Module {
2017-09-14 03:37:08 +02:00
m := NewGenSrcs ( )
android . InitAndroidModule ( m )
2022-05-21 00:54:09 +02:00
android . InitBazelModule ( m )
2017-09-14 03:37:08 +02:00
return m
}
2015-04-28 22:25:36 +02:00
type genSrcsProperties struct {
2015-05-11 22:39:40 +02:00
// extension that will be substituted for each output file
2017-11-10 07:42:32 +01:00
Output_extension * string
2019-09-24 00:55:30 +02:00
// maximum number of files that will be passed on a single command line.
Shard_size * int64
2015-04-28 22:25:36 +02:00
}
2022-05-21 00:54:09 +02:00
type bazelGensrcsAttributes struct {
Srcs bazel . LabelListAttribute
Output_extension * string
Tools bazel . LabelListAttribute
Cmd string
}
2020-12-03 03:55:09 +01:00
const defaultShardSize = 50
2019-09-24 00:55:30 +02:00
2017-09-14 03:37:08 +02:00
func NewGenRule ( ) * Module {
2015-04-28 22:25:36 +02:00
properties := & genRuleProperties { }
2019-09-24 00:55:30 +02:00
taskGenerator := func ( ctx android . ModuleContext , rawCommand string , srcFiles android . Paths ) [ ] generateTask {
2016-09-29 01:21:00 +02:00
outs := make ( android . WritablePaths , len ( properties . Out ) )
2020-11-14 01:23:53 +01:00
var depFile android . WritablePath
2016-09-29 01:21:00 +02:00
for i , out := range properties . Out {
2020-11-14 01:23:53 +01:00
outPath := android . PathForModuleGen ( ctx , out )
if i == 0 {
depFile = outPath . ReplaceExtension ( ctx , "d" )
}
outs [ i ] = outPath
2016-09-29 01:21:00 +02:00
}
2019-09-24 00:55:30 +02:00
return [ ] generateTask { {
2020-11-14 01:23:53 +01:00
in : srcFiles ,
out : outs ,
depFile : depFile ,
genDir : android . PathForModuleGen ( ctx ) ,
cmd : rawCommand ,
2019-09-24 00:55:30 +02:00
} }
2015-04-28 22:25:36 +02:00
}
2017-11-08 21:38:00 +01:00
return generatorFactory ( taskGenerator , properties )
2015-04-28 22:25:36 +02:00
}
2017-10-10 00:34:10 +02:00
func GenRuleFactory ( ) android . Module {
2017-09-14 03:37:08 +02:00
m := NewGenRule ( )
android . InitAndroidModule ( m )
2018-12-10 17:13:18 +01:00
android . InitDefaultableModule ( m )
2021-02-17 16:17:28 +01:00
android . InitBazelModule ( m )
2017-09-14 03:37:08 +02:00
return m
}
2015-04-28 22:25:36 +02:00
type genRuleProperties struct {
2016-09-29 01:21:00 +02:00
// names of the output files that will be generated
2018-10-23 20:27:50 +02:00
Out [ ] string ` android:"arch_variant" `
2015-03-18 21:28:46 +01:00
}
2017-11-09 06:20:04 +01:00
2020-12-14 15:09:52 +01:00
type bazelGenruleAttributes struct {
2021-03-15 11:02:43 +01:00
Srcs bazel . LabelListAttribute
2020-12-14 15:09:52 +01:00
Outs [ ] string
2021-03-15 11:02:43 +01:00
Tools bazel . LabelListAttribute
2020-12-14 15:09:52 +01:00
Cmd string
}
2021-11-01 20:32:43 +01:00
// ConvertWithBp2build converts a Soong module -> Bazel target.
func ( m * Module ) ConvertWithBp2build ( ctx android . TopDownMutatorContext ) {
2021-01-26 15:18:53 +01:00
// Bazel only has the "tools" attribute.
2021-03-15 11:02:43 +01:00
tools_prop := android . BazelLabelForModuleDeps ( ctx , m . properties . Tools )
tool_files_prop := android . BazelLabelForModuleSrc ( ctx , m . properties . Tool_files )
tools_prop . Append ( tool_files_prop )
2021-01-26 15:18:53 +01:00
2021-03-15 11:02:43 +01:00
tools := bazel . MakeLabelListAttribute ( tools_prop )
srcs := bazel . MakeLabelListAttribute ( android . BazelLabelForModuleSrc ( ctx , m . properties . Srcs ) )
2021-01-26 15:18:53 +01:00
var allReplacements bazel . LabelList
2021-03-15 11:02:43 +01:00
allReplacements . Append ( tools . Value )
allReplacements . Append ( srcs . Value )
2021-01-26 15:18:53 +01:00
// Replace in and out variables with $< and $@
var cmd string
if m . properties . Cmd != nil {
2022-05-21 00:54:09 +02:00
if ctx . ModuleType ( ) == "gensrcs" {
cmd = strings . ReplaceAll ( * m . properties . Cmd , "$(in)" , "$(SRC)" )
cmd = strings . ReplaceAll ( cmd , "$(out)" , "$(OUT)" )
} else {
cmd = strings . Replace ( * m . properties . Cmd , "$(in)" , "$(SRCS)" , - 1 )
cmd = strings . Replace ( cmd , "$(out)" , "$(OUTS)" , - 1 )
}
2021-09-17 01:57:28 +02:00
genDir := "$(GENDIR)"
2022-01-11 22:55:46 +01:00
if t := ctx . ModuleType ( ) ; t == "cc_genrule" || t == "java_genrule" || t == "java_genrule_host" {
2021-09-17 01:57:28 +02:00
genDir = "$(RULEDIR)"
}
cmd = strings . Replace ( cmd , "$(genDir)" , genDir , - 1 )
2021-03-15 11:02:43 +01:00
if len ( tools . Value . Includes ) > 0 {
cmd = strings . Replace ( cmd , "$(location)" , fmt . Sprintf ( "$(location %s)" , tools . Value . Includes [ 0 ] . Label ) , - 1 )
cmd = strings . Replace ( cmd , "$(locations)" , fmt . Sprintf ( "$(locations %s)" , tools . Value . Includes [ 0 ] . Label ) , - 1 )
2020-12-14 15:09:52 +01:00
}
2021-01-26 15:18:53 +01:00
for _ , l := range allReplacements . Includes {
2021-04-19 07:00:15 +02:00
bpLoc := fmt . Sprintf ( "$(location %s)" , l . OriginalModuleName )
bpLocs := fmt . Sprintf ( "$(locations %s)" , l . OriginalModuleName )
2021-01-26 15:18:53 +01:00
bazelLoc := fmt . Sprintf ( "$(location %s)" , l . Label )
bazelLocs := fmt . Sprintf ( "$(locations %s)" , l . Label )
cmd = strings . Replace ( cmd , bpLoc , bazelLoc , - 1 )
cmd = strings . Replace ( cmd , bpLocs , bazelLocs , - 1 )
2020-12-14 15:09:52 +01:00
}
2021-01-26 15:18:53 +01:00
}
2020-12-14 15:09:52 +01:00
2022-05-21 00:54:09 +02:00
if ctx . ModuleType ( ) == "gensrcs" {
// The Output_extension prop is not in an immediately accessible field
// in the Module struct, so use GetProperties and cast it
// to the known struct prop.
var outputExtension * string
for _ , propIntf := range m . GetProperties ( ) {
if props , ok := propIntf . ( * genSrcsProperties ) ; ok {
outputExtension = props . Output_extension
break
}
2021-01-26 15:18:53 +01:00
}
2022-05-21 00:54:09 +02:00
props := bazel . BazelTargetModuleProperties {
Rule_class : "gensrcs" ,
Bzl_load_location : "//build/bazel/rules:gensrcs.bzl" ,
}
attrs := & bazelGensrcsAttributes {
Srcs : srcs ,
Output_extension : outputExtension ,
Cmd : cmd ,
Tools : tools ,
}
ctx . CreateBazelTargetModule ( props , android . CommonAttributes { Name : m . Name ( ) } , attrs )
} else {
// The Out prop is not in an immediately accessible field
// in the Module struct, so use GetProperties and cast it
// to the known struct prop.
var outs [ ] string
for _ , propIntf := range m . GetProperties ( ) {
if props , ok := propIntf . ( * genRuleProperties ) ; ok {
outs = props . Out
break
}
}
attrs := & bazelGenruleAttributes {
Srcs : srcs ,
Outs : outs ,
Cmd : cmd ,
Tools : tools ,
}
props := bazel . BazelTargetModuleProperties {
Rule_class : "genrule" ,
}
ctx . CreateBazelTargetModule ( props , android . CommonAttributes { Name : m . Name ( ) } , attrs )
2020-12-14 15:09:52 +01:00
}
}
2017-11-09 06:20:04 +01:00
var Bool = proptools . Bool
var String = proptools . String
2018-12-10 17:13:18 +01:00
// Defaults
type Defaults struct {
android . ModuleBase
android . DefaultsModuleBase
}
func defaultsFactory ( ) android . Module {
return DefaultsFactory ( )
}
func DefaultsFactory ( props ... interface { } ) android . Module {
module := & Defaults { }
module . AddProperties ( props ... )
module . AddProperties (
& generatorProperties { } ,
& genRuleProperties { } ,
)
android . InitDefaultsModule ( module )
return module
}