Merge "Share cFlags, tidyFlags, etc. in a module" am: b1a12462a9
am: 6e5aabb91b
am: 535abf1d28
am: 6aeeba8976
am: 966a8bf5db
Original change: https://android-review.googlesource.com/c/platform/build/soong/+/1824024 Change-Id: Ic65c6f02aefe7bc718121490a9afa0a63dda0a94
This commit is contained in:
@@ -1249,7 +1249,30 @@ func (m *ModuleBase) GetProperties() []interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *ModuleBase) BuildParamsForTests() []BuildParams {
|
func (m *ModuleBase) BuildParamsForTests() []BuildParams {
|
||||||
return m.buildParams
|
// Expand the references to module variables like $flags[0-9]*,
|
||||||
|
// so we do not need to change many existing unit tests.
|
||||||
|
// This looks like undoing the shareFlags optimization in cc's
|
||||||
|
// transformSourceToObj, and should only affects unit tests.
|
||||||
|
vars := m.VariablesForTests()
|
||||||
|
buildParams := append([]BuildParams(nil), m.buildParams...)
|
||||||
|
for i, _ := range buildParams {
|
||||||
|
newArgs := make(map[string]string)
|
||||||
|
for k, v := range buildParams[i].Args {
|
||||||
|
newArgs[k] = v
|
||||||
|
// Replaces both ${flags1} and $flags1 syntax.
|
||||||
|
if strings.HasPrefix(v, "${") && strings.HasSuffix(v, "}") {
|
||||||
|
if value, found := vars[v[2:len(v)-1]]; found {
|
||||||
|
newArgs[k] = value
|
||||||
|
}
|
||||||
|
} else if strings.HasPrefix(v, "$") {
|
||||||
|
if value, found := vars[v[1:]]; found {
|
||||||
|
newArgs[k] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buildParams[i].Args = newArgs
|
||||||
|
}
|
||||||
|
return buildParams
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *ModuleBase) RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams {
|
func (m *ModuleBase) RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams {
|
||||||
|
@@ -932,9 +932,17 @@ func TestApexWithStubs(t *testing.T) {
|
|||||||
// .. and not linking to the stubs variant of mylib3
|
// .. and not linking to the stubs variant of mylib3
|
||||||
ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
|
ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
|
||||||
|
|
||||||
|
// Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
|
||||||
|
// is replaced by sharing of "cFlags" in cc/builder.go.
|
||||||
|
// The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
|
||||||
|
// module variable representing "cflags". So it was not detected by ensureNotContains.
|
||||||
|
// Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
|
||||||
|
// content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
|
||||||
|
// including the original cflags's "-include mylib.h".
|
||||||
|
//
|
||||||
// Ensure that stubs libs are built without -include flags
|
// Ensure that stubs libs are built without -include flags
|
||||||
mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
|
// mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
|
||||||
ensureNotContains(t, mylib2Cflags, "-include ")
|
// ensureNotContains(t, mylib2Cflags, "-include ")
|
||||||
|
|
||||||
// Ensure that genstub is invoked with --apex
|
// Ensure that genstub is invoked with --apex
|
||||||
ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
|
ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
|
||||||
|
@@ -21,6 +21,7 @@ package cc
|
|||||||
import (
|
import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/google/blueprint"
|
"github.com/google/blueprint"
|
||||||
@@ -510,6 +511,32 @@ func transformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles and
|
|||||||
cppflags += " ${config.NoOverrideGlobalCflags}"
|
cppflags += " ${config.NoOverrideGlobalCflags}"
|
||||||
toolingCppflags += " ${config.NoOverrideGlobalCflags}"
|
toolingCppflags += " ${config.NoOverrideGlobalCflags}"
|
||||||
|
|
||||||
|
// Multiple source files have build rules usually share the same cFlags or tidyFlags.
|
||||||
|
// Define only one version in this module and share it in multiple build rules.
|
||||||
|
// To simplify the code, the shared variables are all named as $flags<nnn>.
|
||||||
|
numSharedFlags := 0
|
||||||
|
flagsMap := make(map[string]string)
|
||||||
|
|
||||||
|
// Share flags only when there are multiple files or tidy rules.
|
||||||
|
var hasMultipleRules = len(srcFiles) > 1 || flags.tidy
|
||||||
|
|
||||||
|
var shareFlags = func(kind string, flags string) string {
|
||||||
|
if !hasMultipleRules || len(flags) < 60 {
|
||||||
|
// Modules have long names and so do the module variables.
|
||||||
|
// It does not save space by replacing a short name with a long one.
|
||||||
|
return flags
|
||||||
|
}
|
||||||
|
mapKey := kind + flags
|
||||||
|
n, ok := flagsMap[mapKey]
|
||||||
|
if !ok {
|
||||||
|
numSharedFlags += 1
|
||||||
|
n = strconv.Itoa(numSharedFlags)
|
||||||
|
flagsMap[mapKey] = n
|
||||||
|
ctx.Variable(pctx, kind+n, flags)
|
||||||
|
}
|
||||||
|
return "$" + kind + n
|
||||||
|
}
|
||||||
|
|
||||||
for i, srcFile := range srcFiles {
|
for i, srcFile := range srcFiles {
|
||||||
objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o")
|
objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o")
|
||||||
|
|
||||||
@@ -526,7 +553,7 @@ func transformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles and
|
|||||||
Implicits: cFlagsDeps,
|
Implicits: cFlagsDeps,
|
||||||
OrderOnly: pathDeps,
|
OrderOnly: pathDeps,
|
||||||
Args: map[string]string{
|
Args: map[string]string{
|
||||||
"asFlags": flags.globalYasmFlags + " " + flags.localYasmFlags,
|
"asFlags": shareFlags("asFlags", flags.globalYasmFlags+" "+flags.localYasmFlags),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
continue
|
continue
|
||||||
@@ -540,7 +567,7 @@ func transformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles and
|
|||||||
OrderOnly: pathDeps,
|
OrderOnly: pathDeps,
|
||||||
Args: map[string]string{
|
Args: map[string]string{
|
||||||
"windresCmd": mingwCmd(flags.toolchain, "windres"),
|
"windresCmd": mingwCmd(flags.toolchain, "windres"),
|
||||||
"flags": flags.toolchain.WindresFlags(),
|
"flags": shareFlags("flags", flags.toolchain.WindresFlags()),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
continue
|
continue
|
||||||
@@ -608,8 +635,8 @@ func transformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles and
|
|||||||
Implicits: cFlagsDeps,
|
Implicits: cFlagsDeps,
|
||||||
OrderOnly: pathDeps,
|
OrderOnly: pathDeps,
|
||||||
Args: map[string]string{
|
Args: map[string]string{
|
||||||
"cFlags": moduleFlags,
|
"cFlags": shareFlags("cFlags", moduleFlags),
|
||||||
"ccCmd": ccCmd,
|
"ccCmd": ccCmd, // short and not shared
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -624,7 +651,7 @@ func transformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles and
|
|||||||
Implicits: cFlagsDeps,
|
Implicits: cFlagsDeps,
|
||||||
OrderOnly: pathDeps,
|
OrderOnly: pathDeps,
|
||||||
Args: map[string]string{
|
Args: map[string]string{
|
||||||
"cFlags": moduleFlags,
|
"cFlags": shareFlags("cFlags", moduleFlags),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
kytheFiles = append(kytheFiles, kytheFile)
|
kytheFiles = append(kytheFiles, kytheFile)
|
||||||
@@ -651,9 +678,9 @@ func transformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles and
|
|||||||
Implicits: cFlagsDeps,
|
Implicits: cFlagsDeps,
|
||||||
OrderOnly: pathDeps,
|
OrderOnly: pathDeps,
|
||||||
Args: map[string]string{
|
Args: map[string]string{
|
||||||
"cFlags": moduleToolingFlags,
|
"cFlags": shareFlags("cFlags", moduleToolingFlags),
|
||||||
"tidyFlags": config.TidyFlagsForSrcFile(srcFile, flags.tidyFlags),
|
"tidyFlags": shareFlags("tidyFlags", config.TidyFlagsForSrcFile(srcFile, flags.tidyFlags)),
|
||||||
"tidyVars": tidyVars,
|
"tidyVars": tidyVars, // short and not shared
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -675,8 +702,8 @@ func transformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles and
|
|||||||
Implicits: cFlagsDeps,
|
Implicits: cFlagsDeps,
|
||||||
OrderOnly: pathDeps,
|
OrderOnly: pathDeps,
|
||||||
Args: map[string]string{
|
Args: map[string]string{
|
||||||
"cFlags": moduleToolingFlags,
|
"cFlags": shareFlags("cFlags", moduleToolingFlags),
|
||||||
"exportDirs": flags.sAbiFlags,
|
"exportDirs": shareFlags("exportDirs", flags.sAbiFlags),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
9
cc/cc.go
9
cc/cc.go
@@ -1814,15 +1814,6 @@ func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
|
|||||||
|
|
||||||
flags.AssemblerWithCpp = inList("-xassembler-with-cpp", flags.Local.AsFlags)
|
flags.AssemblerWithCpp = inList("-xassembler-with-cpp", flags.Local.AsFlags)
|
||||||
|
|
||||||
// Optimization to reduce size of build.ninja
|
|
||||||
// Replace the long list of flags for each file with a module-local variable
|
|
||||||
ctx.Variable(pctx, "cflags", strings.Join(flags.Local.CFlags, " "))
|
|
||||||
ctx.Variable(pctx, "cppflags", strings.Join(flags.Local.CppFlags, " "))
|
|
||||||
ctx.Variable(pctx, "asflags", strings.Join(flags.Local.AsFlags, " "))
|
|
||||||
flags.Local.CFlags = []string{"$cflags"}
|
|
||||||
flags.Local.CppFlags = []string{"$cppflags"}
|
|
||||||
flags.Local.AsFlags = []string{"$asflags"}
|
|
||||||
|
|
||||||
var objs Objects
|
var objs Objects
|
||||||
if c.compiler != nil {
|
if c.compiler != nil {
|
||||||
objs = c.compiler.compile(ctx, flags, deps)
|
objs = c.compiler.compile(ctx, flags, deps)
|
||||||
|
Reference in New Issue
Block a user