2015-04-08 01:50:10 +02: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.
|
|
|
|
|
|
|
|
package cc
|
|
|
|
|
|
|
|
import (
|
2018-09-05 17:55:20 +02:00
|
|
|
"path/filepath"
|
2019-06-05 02:10:41 +02:00
|
|
|
"strings"
|
2018-09-05 17:55:20 +02:00
|
|
|
|
2015-04-08 01:50:10 +02:00
|
|
|
"github.com/google/blueprint"
|
|
|
|
|
2016-05-19 00:37:25 +02:00
|
|
|
"android/soong/android"
|
2015-04-08 01:50:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-07-24 19:19:23 +02:00
|
|
|
pctx.SourcePathVariable("lexCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/flex")
|
2020-07-23 23:37:35 +02:00
|
|
|
pctx.SourcePathVariable("m4Cmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/m4")
|
|
|
|
|
2016-11-03 22:28:51 +01:00
|
|
|
pctx.HostBinToolVariable("aidlCmd", "aidl-cpp")
|
2018-09-05 17:55:20 +02:00
|
|
|
pctx.HostBinToolVariable("syspropCmd", "sysprop_cpp")
|
2015-04-08 01:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-07-24 19:19:23 +02:00
|
|
|
lex = pctx.AndroidStaticRule("lex",
|
|
|
|
blueprint.RuleParams{
|
2020-07-15 10:58:56 +02:00
|
|
|
Command: "M4=$m4Cmd $lexCmd $flags -o$out $in",
|
2020-07-24 19:19:23 +02:00
|
|
|
CommandDeps: []string{"$lexCmd", "$m4Cmd"},
|
2020-07-15 10:58:56 +02:00
|
|
|
}, "flags")
|
2020-07-24 19:19:23 +02:00
|
|
|
|
2018-09-05 17:55:20 +02:00
|
|
|
sysprop = pctx.AndroidStaticRule("sysprop",
|
|
|
|
blueprint.RuleParams{
|
2019-06-08 13:36:59 +02:00
|
|
|
Command: "$syspropCmd --header-dir=$headerOutDir --public-header-dir=$publicOutDir " +
|
2019-02-08 13:00:45 +01:00
|
|
|
"--source-dir=$srcOutDir --include-name=$includeName $in",
|
2018-09-05 17:55:20 +02:00
|
|
|
CommandDeps: []string{"$syspropCmd"},
|
|
|
|
},
|
2019-06-08 13:36:59 +02:00
|
|
|
"headerOutDir", "publicOutDir", "srcOutDir", "includeName")
|
2015-04-08 01:50:10 +02:00
|
|
|
)
|
|
|
|
|
2019-04-11 07:59:54 +02:00
|
|
|
type YaccProperties struct {
|
|
|
|
// list of module-specific flags that will be used for .y and .yy compiles
|
|
|
|
Flags []string
|
2015-04-08 01:50:10 +02:00
|
|
|
|
2019-04-11 07:59:54 +02:00
|
|
|
// whether the yacc files will produce a location.hh file
|
|
|
|
Gen_location_hh *bool
|
|
|
|
|
|
|
|
// whether the yacc files will product a position.hh file
|
|
|
|
Gen_position_hh *bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func genYacc(ctx android.ModuleContext, rule *android.RuleBuilder, yaccFile android.Path,
|
2020-07-24 19:19:23 +02:00
|
|
|
outFile android.ModuleGenPath, props *YaccProperties) (headerFiles android.Paths) {
|
2019-04-11 07:59:54 +02:00
|
|
|
|
|
|
|
outDir := android.PathForModuleGen(ctx, "yacc")
|
|
|
|
headerFile := android.GenPathWithExt(ctx, "yacc", yaccFile, "h")
|
|
|
|
ret := android.Paths{headerFile}
|
|
|
|
|
|
|
|
cmd := rule.Command()
|
2015-04-08 01:50:10 +02:00
|
|
|
|
2019-04-11 07:59:54 +02:00
|
|
|
// Fix up #line markers to not use the sbox temporary directory
|
2020-11-17 02:32:30 +01:00
|
|
|
// android.sboxPathForOutput(outDir, outDir) returns the sbox placeholder for the out
|
2020-11-14 01:23:53 +01:00
|
|
|
// directory itself, without any filename appended.
|
2020-11-17 02:32:30 +01:00
|
|
|
sboxOutDir := cmd.PathForOutput(outDir)
|
2020-11-14 01:23:53 +01:00
|
|
|
sedCmd := "sed -i.bak 's#" + sboxOutDir + "#" + outDir.String() + "#'"
|
2019-04-11 07:59:54 +02:00
|
|
|
rule.Command().Text(sedCmd).Input(outFile)
|
|
|
|
rule.Command().Text(sedCmd).Input(headerFile)
|
|
|
|
|
|
|
|
var flags []string
|
|
|
|
if props != nil {
|
|
|
|
flags = props.Flags
|
|
|
|
|
|
|
|
if Bool(props.Gen_location_hh) {
|
|
|
|
locationHeader := outFile.InSameDir(ctx, "location.hh")
|
|
|
|
ret = append(ret, locationHeader)
|
|
|
|
cmd.ImplicitOutput(locationHeader)
|
|
|
|
rule.Command().Text(sedCmd).Input(locationHeader)
|
|
|
|
}
|
|
|
|
if Bool(props.Gen_position_hh) {
|
|
|
|
positionHeader := outFile.InSameDir(ctx, "position.hh")
|
|
|
|
ret = append(ret, positionHeader)
|
|
|
|
cmd.ImplicitOutput(positionHeader)
|
|
|
|
rule.Command().Text(sedCmd).Input(positionHeader)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 19:19:23 +02:00
|
|
|
cmd.Text("BISON_PKGDATADIR=prebuilts/build-tools/common/bison").
|
|
|
|
FlagWithInput("M4=", ctx.Config().PrebuiltBuildTool(ctx, "m4")).
|
|
|
|
PrebuiltBuildTool(ctx, "bison").
|
2019-04-11 07:59:54 +02:00
|
|
|
Flag("-d").
|
|
|
|
Flags(flags).
|
|
|
|
FlagWithOutput("--defines=", headerFile).
|
|
|
|
Flag("-o").Output(outFile).Input(yaccFile)
|
|
|
|
|
|
|
|
return ret
|
2015-04-08 01:50:10 +02:00
|
|
|
}
|
|
|
|
|
2021-04-07 14:49:34 +02:00
|
|
|
func genAidl(ctx android.ModuleContext, rule *android.RuleBuilder, aidlFile android.Path, aidlFlags string) (cppFile android.OutputPath, headerFiles android.Paths) {
|
2019-06-05 02:10:41 +02:00
|
|
|
aidlPackage := strings.TrimSuffix(aidlFile.Rel(), aidlFile.Base())
|
|
|
|
baseName := strings.TrimSuffix(aidlFile.Base(), aidlFile.Ext())
|
2019-09-25 02:39:58 +02:00
|
|
|
shortName := baseName
|
|
|
|
// TODO(b/111362593): aidl_to_cpp_common.cpp uses heuristics to figure out if
|
|
|
|
// an interface name has a leading I. Those same heuristics have been
|
|
|
|
// moved here.
|
|
|
|
if len(baseName) >= 2 && baseName[0] == 'I' &&
|
|
|
|
strings.ToUpper(baseName)[1] == baseName[1] {
|
|
|
|
shortName = strings.TrimPrefix(baseName, "I")
|
|
|
|
}
|
2016-11-03 22:28:51 +01:00
|
|
|
|
2019-06-05 02:10:41 +02:00
|
|
|
outDir := android.PathForModuleGen(ctx, "aidl")
|
2021-04-07 14:49:34 +02:00
|
|
|
cppFile = outDir.Join(ctx, aidlPackage, baseName+".cpp")
|
|
|
|
depFile := outDir.Join(ctx, aidlPackage, baseName+".cpp.d")
|
2019-06-05 02:10:41 +02:00
|
|
|
headerI := outDir.Join(ctx, aidlPackage, baseName+".h")
|
|
|
|
headerBn := outDir.Join(ctx, aidlPackage, "Bn"+shortName+".h")
|
|
|
|
headerBp := outDir.Join(ctx, aidlPackage, "Bp"+shortName+".h")
|
|
|
|
|
2019-07-07 09:27:47 +02:00
|
|
|
baseDir := strings.TrimSuffix(aidlFile.String(), aidlFile.Rel())
|
|
|
|
if baseDir != "" {
|
|
|
|
aidlFlags += " -I" + baseDir
|
|
|
|
}
|
|
|
|
|
2019-06-05 02:10:41 +02:00
|
|
|
cmd := rule.Command()
|
2020-11-17 02:32:30 +01:00
|
|
|
cmd.BuiltTool("aidl-cpp").
|
2019-06-05 02:10:41 +02:00
|
|
|
FlagWithDepFile("-d", depFile).
|
|
|
|
Flag("--ninja").
|
|
|
|
Flag(aidlFlags).
|
|
|
|
Input(aidlFile).
|
|
|
|
OutputDir().
|
2021-04-07 14:49:34 +02:00
|
|
|
Output(cppFile).
|
2019-06-05 02:10:41 +02:00
|
|
|
ImplicitOutputs(android.WritablePaths{
|
|
|
|
headerI,
|
|
|
|
headerBn,
|
|
|
|
headerBp,
|
|
|
|
})
|
|
|
|
|
2021-04-07 14:49:34 +02:00
|
|
|
return cppFile, android.Paths{
|
2019-06-05 02:10:41 +02:00
|
|
|
headerI,
|
|
|
|
headerBn,
|
|
|
|
headerBp,
|
|
|
|
}
|
2016-11-03 22:28:51 +01:00
|
|
|
}
|
|
|
|
|
2020-07-15 10:58:56 +02:00
|
|
|
type LexProperties struct {
|
|
|
|
// list of module-specific flags that will be used for .l and .ll compiles
|
|
|
|
Flags []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath, props *LexProperties) {
|
|
|
|
var flags []string
|
|
|
|
if props != nil {
|
|
|
|
flags = props.Flags
|
|
|
|
}
|
|
|
|
flagsString := strings.Join(flags[:], " ")
|
2020-07-24 19:19:23 +02:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: lex,
|
|
|
|
Description: "lex " + lexFile.Rel(),
|
|
|
|
Output: outFile,
|
|
|
|
Input: lexFile,
|
2020-07-15 10:58:56 +02:00
|
|
|
Args: map[string]string{"flags": flagsString},
|
2020-07-24 19:19:23 +02:00
|
|
|
})
|
2015-04-08 01:50:10 +02:00
|
|
|
}
|
|
|
|
|
2020-02-19 02:42:41 +01:00
|
|
|
func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Path, android.Paths) {
|
2018-09-05 17:55:20 +02:00
|
|
|
headerFile := android.PathForModuleGen(ctx, "sysprop", "include", syspropFile.Rel()+".h")
|
2019-06-08 13:36:59 +02:00
|
|
|
publicHeaderFile := android.PathForModuleGen(ctx, "sysprop/public", "include", syspropFile.Rel()+".h")
|
2018-09-05 17:55:20 +02:00
|
|
|
cppFile := android.PathForModuleGen(ctx, "sysprop", syspropFile.Rel()+".cpp")
|
|
|
|
|
2020-02-19 02:42:41 +01:00
|
|
|
headers := android.WritablePaths{headerFile, publicHeaderFile}
|
|
|
|
|
2018-09-05 17:55:20 +02:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2020-02-19 02:42:41 +01:00
|
|
|
Rule: sysprop,
|
|
|
|
Description: "sysprop " + syspropFile.Rel(),
|
|
|
|
Output: cppFile,
|
|
|
|
ImplicitOutputs: headers,
|
|
|
|
Input: syspropFile,
|
2018-09-05 17:55:20 +02:00
|
|
|
Args: map[string]string{
|
|
|
|
"headerOutDir": filepath.Dir(headerFile.String()),
|
2019-06-08 13:36:59 +02:00
|
|
|
"publicOutDir": filepath.Dir(publicHeaderFile.String()),
|
2018-09-05 17:55:20 +02:00
|
|
|
"srcOutDir": filepath.Dir(cppFile.String()),
|
|
|
|
"includeName": syspropFile.Rel() + ".h",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2020-02-19 02:42:41 +01:00
|
|
|
return cppFile, headers.Paths()
|
2018-09-05 17:55:20 +02:00
|
|
|
}
|
|
|
|
|
2021-02-19 14:49:08 +01:00
|
|
|
// Used to communicate information from the genSources method back to the library code that uses
|
|
|
|
// it.
|
|
|
|
type generatedSourceInfo struct {
|
|
|
|
// The headers created from .proto files
|
|
|
|
protoHeaders android.Paths
|
|
|
|
|
|
|
|
// The files that can be used as order only dependencies in order to ensure that the proto header
|
|
|
|
// files are up to date.
|
|
|
|
protoOrderOnlyDeps android.Paths
|
|
|
|
|
|
|
|
// The headers created from .aidl files
|
|
|
|
aidlHeaders android.Paths
|
|
|
|
|
|
|
|
// The files that can be used as order only dependencies in order to ensure that the aidl header
|
|
|
|
// files are up to date.
|
|
|
|
aidlOrderOnlyDeps android.Paths
|
|
|
|
|
|
|
|
// The headers created from .sysprop files
|
|
|
|
syspropHeaders android.Paths
|
|
|
|
|
|
|
|
// The files that can be used as order only dependencies in order to ensure that the sysprop
|
|
|
|
// header files are up to date.
|
|
|
|
syspropOrderOnlyDeps android.Paths
|
|
|
|
}
|
|
|
|
|
2020-07-24 19:19:23 +02:00
|
|
|
func genSources(ctx android.ModuleContext, srcFiles android.Paths,
|
2021-02-19 14:49:08 +01:00
|
|
|
buildFlags builderFlags) (android.Paths, android.Paths, generatedSourceInfo) {
|
|
|
|
|
|
|
|
var info generatedSourceInfo
|
2015-04-08 01:50:10 +02:00
|
|
|
|
2016-05-19 00:37:25 +02:00
|
|
|
var deps android.Paths
|
2017-05-02 02:37:24 +02:00
|
|
|
var rsFiles android.Paths
|
|
|
|
|
2019-06-05 02:10:41 +02:00
|
|
|
var aidlRule *android.RuleBuilder
|
|
|
|
|
2019-04-11 07:59:54 +02:00
|
|
|
var yaccRule_ *android.RuleBuilder
|
|
|
|
yaccRule := func() *android.RuleBuilder {
|
|
|
|
if yaccRule_ == nil {
|
2020-11-17 02:32:30 +01:00
|
|
|
yaccRule_ = android.NewRuleBuilder(pctx, ctx).Sbox(android.PathForModuleGen(ctx, "yacc"),
|
2020-11-12 17:29:30 +01:00
|
|
|
android.PathForModuleGen(ctx, "yacc.sbox.textproto"))
|
2019-04-11 07:59:54 +02:00
|
|
|
}
|
|
|
|
return yaccRule_
|
|
|
|
}
|
|
|
|
|
2015-04-08 01:50:10 +02:00
|
|
|
for i, srcFile := range srcFiles {
|
2015-09-24 00:26:20 +02:00
|
|
|
switch srcFile.Ext() {
|
2016-03-02 00:15:26 +01:00
|
|
|
case ".y":
|
2016-11-03 04:43:13 +01:00
|
|
|
cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c")
|
2016-03-02 00:15:26 +01:00
|
|
|
srcFiles[i] = cFile
|
2020-07-24 19:19:23 +02:00
|
|
|
deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cFile, buildFlags.yacc)...)
|
2016-03-02 00:15:26 +01:00
|
|
|
case ".yy":
|
2016-11-03 04:43:13 +01:00
|
|
|
cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp")
|
2015-04-08 01:50:10 +02:00
|
|
|
srcFiles[i] = cppFile
|
2020-07-24 19:19:23 +02:00
|
|
|
deps = append(deps, genYacc(ctx, yaccRule(), srcFile, cppFile, buildFlags.yacc)...)
|
2016-03-02 00:15:26 +01:00
|
|
|
case ".l":
|
2016-11-03 04:43:13 +01:00
|
|
|
cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c")
|
2016-03-02 00:15:26 +01:00
|
|
|
srcFiles[i] = cFile
|
2020-07-15 10:58:56 +02:00
|
|
|
genLex(ctx, srcFile, cFile, buildFlags.lex)
|
2016-03-02 00:15:26 +01:00
|
|
|
case ".ll":
|
2016-11-03 04:43:13 +01:00
|
|
|
cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp")
|
2015-04-08 01:50:10 +02:00
|
|
|
srcFiles[i] = cppFile
|
2020-07-15 10:58:56 +02:00
|
|
|
genLex(ctx, srcFile, cppFile, buildFlags.lex)
|
2016-10-21 01:11:43 +02:00
|
|
|
case ".proto":
|
2018-11-17 06:05:32 +01:00
|
|
|
ccFile, headerFile := genProto(ctx, srcFile, buildFlags)
|
2017-09-20 21:59:05 +02:00
|
|
|
srcFiles[i] = ccFile
|
2021-02-19 14:49:08 +01:00
|
|
|
info.protoHeaders = append(info.protoHeaders, headerFile)
|
|
|
|
// Use the generated header as an order only dep to ensure that it is up to date when needed.
|
|
|
|
info.protoOrderOnlyDeps = append(info.protoOrderOnlyDeps, headerFile)
|
2016-11-03 22:28:51 +01:00
|
|
|
case ".aidl":
|
2019-06-05 02:10:41 +02:00
|
|
|
if aidlRule == nil {
|
2020-11-17 02:32:30 +01:00
|
|
|
aidlRule = android.NewRuleBuilder(pctx, ctx).Sbox(android.PathForModuleGen(ctx, "aidl"),
|
2020-11-12 17:29:30 +01:00
|
|
|
android.PathForModuleGen(ctx, "aidl.sbox.textproto"))
|
2019-06-05 02:10:41 +02:00
|
|
|
}
|
2021-04-07 14:49:34 +02:00
|
|
|
cppFile, aidlHeaders := genAidl(ctx, aidlRule, srcFile, buildFlags.aidlFlags)
|
2016-11-03 22:28:51 +01:00
|
|
|
srcFiles[i] = cppFile
|
2021-04-07 14:49:34 +02:00
|
|
|
|
2021-02-19 14:49:08 +01:00
|
|
|
info.aidlHeaders = append(info.aidlHeaders, aidlHeaders...)
|
|
|
|
// Use the generated headers as order only deps to ensure that they are up to date when
|
|
|
|
// needed.
|
|
|
|
// TODO: Reduce the size of the ninja file by using one order only dep for the whole rule
|
|
|
|
info.aidlOrderOnlyDeps = append(info.aidlOrderOnlyDeps, aidlHeaders...)
|
2019-07-16 04:08:37 +02:00
|
|
|
case ".rscript", ".fs":
|
2017-05-02 02:37:24 +02:00
|
|
|
cppFile := rsGeneratedCppFile(ctx, srcFile)
|
|
|
|
rsFiles = append(rsFiles, srcFiles[i])
|
|
|
|
srcFiles[i] = cppFile
|
2018-09-05 17:55:20 +02:00
|
|
|
case ".sysprop":
|
2020-02-19 02:42:41 +01:00
|
|
|
cppFile, headerFiles := genSysprop(ctx, srcFile)
|
2018-09-05 17:55:20 +02:00
|
|
|
srcFiles[i] = cppFile
|
2021-02-19 14:49:08 +01:00
|
|
|
info.syspropHeaders = append(info.syspropHeaders, headerFiles...)
|
|
|
|
// Use the generated headers as order only deps to ensure that they are up to date when
|
|
|
|
// needed.
|
|
|
|
info.syspropOrderOnlyDeps = append(info.syspropOrderOnlyDeps, headerFiles...)
|
2015-04-08 01:50:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 02:10:41 +02:00
|
|
|
if aidlRule != nil {
|
2020-11-17 02:32:30 +01:00
|
|
|
aidlRule.Build("aidl", "gen aidl")
|
2019-06-05 02:10:41 +02:00
|
|
|
}
|
|
|
|
|
2019-04-11 07:59:54 +02:00
|
|
|
if yaccRule_ != nil {
|
2020-11-17 02:32:30 +01:00
|
|
|
yaccRule_.Build("yacc", "gen yacc")
|
2019-04-11 07:59:54 +02:00
|
|
|
}
|
|
|
|
|
2021-02-19 14:49:08 +01:00
|
|
|
deps = append(deps, info.protoOrderOnlyDeps...)
|
|
|
|
deps = append(deps, info.aidlOrderOnlyDeps...)
|
|
|
|
deps = append(deps, info.syspropOrderOnlyDeps...)
|
|
|
|
|
2017-05-02 02:37:24 +02:00
|
|
|
if len(rsFiles) > 0 {
|
|
|
|
deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...)
|
|
|
|
}
|
|
|
|
|
2021-02-19 14:49:08 +01:00
|
|
|
return srcFiles, deps, info
|
2015-04-08 01:50:10 +02:00
|
|
|
}
|