Refactor cc

Refactor all of cc in order to use composition instead of inheritance.
All cc module types exported by cc are now *cc.Module objects, with
compilation, linking, and installing steps delegated to different
objects in order to form the full module type.  Additional features that
modify dependencies and flags can be inserted in a features object list,
and custom module types can be created by adding a Customizer object
that can modify properties.

Change-Id: Ie1283d14920f7856f6947b0530606b2f4d58fab0
This commit is contained in:
Colin Cross 2016-01-04 14:34:37 -08:00
parent 7253e0b8a4
commit ca860ac720
7 changed files with 1468 additions and 1199 deletions

View File

@ -124,6 +124,7 @@ bootstrap_go_package {
"cc/cc.go",
"cc/clang.go",
"cc/gen.go",
"cc/stl.go",
"cc/toolchain.go",
"cc/util.go",

View File

@ -22,16 +22,50 @@ import (
"android/soong/common"
)
func (c *CCLibrary) AndroidMk() (ret common.AndroidMkData, err error) {
if c.static() {
func (c *Module) AndroidMk() (ret common.AndroidMkData, err error) {
ret.OutputFile = c.outputFile
ret.Extra = append(ret.Extra, func(w io.Writer, outputFile common.Path) (err error) {
if len(c.deps.SharedLibs) > 0 {
fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES := "+strings.Join(c.deps.SharedLibs, " "))
}
return nil
})
callSubAndroidMk := func(obj interface{}) {
if obj != nil {
if androidmk, ok := obj.(interface {
AndroidMk(*common.AndroidMkData)
}); ok {
androidmk.AndroidMk(&ret)
}
}
}
for _, feature := range c.features {
callSubAndroidMk(feature)
}
callSubAndroidMk(c.compiler)
callSubAndroidMk(c.linker)
callSubAndroidMk(c.installer)
return ret, nil
}
func (library *baseLinker) AndroidMk(ret *common.AndroidMkData) {
if library.static() {
ret.Class = "STATIC_LIBRARIES"
} else {
ret.Class = "SHARED_LIBRARIES"
}
ret.OutputFile = c.outputFile()
ret.Extra = func(w io.Writer, outputFile common.Path) error {
exportedIncludes := []string{}
for _, flag := range c.exportedFlags() {
}
func (library *libraryLinker) AndroidMk(ret *common.AndroidMkData) {
library.baseLinker.AndroidMk(ret)
ret.Extra = append(ret.Extra, func(w io.Writer, outputFile common.Path) error {
exportedIncludes := library.exportedFlags()
for _, flag := range library.exportedFlags() {
if flag != "" {
exportedIncludes = append(exportedIncludes, strings.TrimPrefix(flag, "-I"))
}
@ -40,49 +74,36 @@ func (c *CCLibrary) AndroidMk() (ret common.AndroidMkData, err error) {
fmt.Fprintln(w, "LOCAL_EXPORT_C_INCLUDE_DIRS :=", strings.Join(exportedIncludes, " "))
}
fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX :=", outputFile.Ext())
if len(c.savedDepNames.SharedLibs) > 0 {
fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES :=", strings.Join(c.savedDepNames.SharedLibs, " "))
}
if c.Properties.Relative_install_path != "" {
fmt.Fprintln(w, "LOCAL_MODULE_RELATIVE_PATH :=", c.Properties.Relative_install_path)
}
fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
// These are already included in LOCAL_SHARED_LIBRARIES
fmt.Fprintln(w, "LOCAL_CXX_STL := none")
fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
return nil
}
return
})
}
func (c *ccObject) AndroidMk() (ret common.AndroidMkData, err error) {
ret.OutputFile = c.outputFile()
func (object *objectLinker) AndroidMk(ret *common.AndroidMkData) {
ret.Custom = func(w io.Writer, name, prefix string) error {
out := c.outputFile().Path()
out := ret.OutputFile.Path()
fmt.Fprintln(w, "\n$("+prefix+"OUT_INTERMEDIATE_LIBRARIES)/"+name+objectExtension+":", out.String(), "| $(ACP)")
fmt.Fprintln(w, "\t$(copy-file-to-target)")
return nil
}
return
}
func (c *CCBinary) AndroidMk() (ret common.AndroidMkData, err error) {
func (binary *binaryLinker) AndroidMk(ret *common.AndroidMkData) {
ret.Class = "EXECUTABLES"
ret.Extra = func(w io.Writer, outputFile common.Path) error {
ret.Extra = append(ret.Extra, func(w io.Writer, outputFile common.Path) error {
fmt.Fprintln(w, "LOCAL_CXX_STL := none")
fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES :=", strings.Join(c.savedDepNames.SharedLibs, " "))
if c.Properties.Relative_install_path != "" {
fmt.Fprintln(w, "LOCAL_MODULE_RELATIVE_PATH :=", c.Properties.Relative_install_path)
}
return nil
}
ret.OutputFile = c.outputFile()
return
})
}
func (test *testLinker) AndroidMk(ret *common.AndroidMkData) {
ret.Disabled = true
}

2314
cc/cc.go

File diff suppressed because it is too large Load Diff

198
cc/stl.go Normal file
View File

@ -0,0 +1,198 @@
// Copyright 2016 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 (
"android/soong/common"
"fmt"
)
type StlProperties struct {
// select the STL library to use. Possible values are "libc++", "libc++_static",
// "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
// default
Stl string
SelectedStl string `blueprint:"mutated"`
}
type stlFeature struct {
Properties StlProperties
}
var _ feature = (*stlFeature)(nil)
func (stl *stlFeature) props() []interface{} {
return []interface{}{&stl.Properties}
}
func (stl *stlFeature) begin(ctx BaseModuleContext) {
stl.Properties.SelectedStl = func() string {
if ctx.sdk() && ctx.Device() {
switch stl.Properties.Stl {
case "":
return "ndk_system"
case "c++_shared", "c++_static",
"stlport_shared", "stlport_static",
"gnustl_static":
return "ndk_lib" + stl.Properties.Stl
default:
ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", stl.Properties.Stl)
return ""
}
} else if ctx.HostType() == common.Windows {
switch stl.Properties.Stl {
case "libc++", "libc++_static", "libstdc++", "":
// libc++ is not supported on mingw
return "libstdc++"
case "none":
return ""
default:
ctx.ModuleErrorf("stl: %q is not a supported STL", stl.Properties.Stl)
return ""
}
} else {
switch stl.Properties.Stl {
case "libc++", "libc++_static",
"libstdc++":
return stl.Properties.Stl
case "none":
return ""
case "":
if ctx.static() {
return "libc++_static"
} else {
return "libc++"
}
default:
ctx.ModuleErrorf("stl: %q is not a supported STL", stl.Properties.Stl)
return ""
}
}
}()
}
func (stl *stlFeature) deps(ctx BaseModuleContext, deps Deps) Deps {
switch stl.Properties.SelectedStl {
case "libstdc++":
if ctx.Device() {
deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl)
}
case "libc++", "libc++_static":
if stl.Properties.SelectedStl == "libc++" {
deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl)
} else {
deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl)
}
if ctx.Device() {
if ctx.Arch().ArchType == common.Arm {
deps.StaticLibs = append(deps.StaticLibs, "libunwind_llvm")
}
if ctx.staticBinary() {
deps.StaticLibs = append(deps.StaticLibs, "libdl")
} else {
deps.SharedLibs = append(deps.SharedLibs, "libdl")
}
}
case "":
// None or error.
case "ndk_system":
// TODO: Make a system STL prebuilt for the NDK.
// The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have
// its own includes. The includes are handled in CCBase.Flags().
deps.SharedLibs = append([]string{"libstdc++"}, deps.SharedLibs...)
case "ndk_libc++_shared", "ndk_libstlport_shared":
deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl)
case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl)
default:
panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl))
}
return deps
}
func (stl *stlFeature) flags(ctx ModuleContext, flags Flags) Flags {
switch stl.Properties.SelectedStl {
case "libc++", "libc++_static":
flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
if ctx.Host() {
flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
flags.LdFlags = append(flags.LdFlags, "-lpthread", "-lm")
if ctx.staticBinary() {
flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
} else {
flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
}
} else {
if ctx.Arch().ArchType == common.Arm {
flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
}
}
case "libstdc++":
// Using bionic's basic libstdc++. Not actually an STL. Only around until the
// tree is in good enough shape to not need it.
// Host builds will use GNU libstdc++.
if ctx.Device() {
flags.CFlags = append(flags.CFlags, "-I"+common.PathForSource(ctx, "bionic/libstdc++/include").String())
} else {
// Host builds will use the system C++. libc++ on Darwin, GNU libstdc++ everywhere else
flags.CppFlags = append(flags.CppFlags, flags.Toolchain.SystemCppCppflags())
flags.LdFlags = append(flags.LdFlags, flags.Toolchain.SystemCppLdflags())
}
case "ndk_system":
ndkSrcRoot := common.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String())
case "ndk_libc++_shared", "ndk_libc++_static":
// TODO(danalbert): This really shouldn't be here...
flags.CppFlags = append(flags.CppFlags, "-std=c++11")
case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
// Nothing
case "":
// None or error.
if ctx.Host() {
flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
if ctx.staticBinary() {
flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
} else {
flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
}
}
default:
panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl))
}
return flags
}
var hostDynamicGccLibs, hostStaticGccLibs map[common.HostType][]string
func init() {
hostDynamicGccLibs = map[common.HostType][]string{
common.Linux: []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
common.Darwin: []string{"-lc", "-lSystem"},
common.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname",
"-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
"-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
"-lmsvcrt"},
}
hostStaticGccLibs = map[common.HostType][]string{
common.Linux: []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"},
common.Darwin: []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"},
common.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"},
}
}

View File

@ -72,7 +72,7 @@ func moduleToLibName(module string) (string, error) {
return matches[1], nil
}
func ccFlagsToBuilderFlags(in CCFlags) builderFlags {
func flagsToBuilderFlags(in Flags) builderFlags {
return builderFlags{
globalFlags: strings.Join(in.GlobalFlags, " "),
asFlags: strings.Join(in.AsFlags, " "),

View File

@ -39,10 +39,11 @@ type AndroidMkDataProvider interface {
type AndroidMkData struct {
Class string
OutputFile OptionalPath
Disabled bool
Custom func(w io.Writer, name, prefix string) error
Extra func(w io.Writer, outputFile Path) error
Extra []func(w io.Writer, outputFile Path) error
}
func AndroidMkSingleton() blueprint.Singleton {
@ -166,6 +167,10 @@ func translateAndroidMkModule(ctx blueprint.SingletonContext, w io.Writer, mod b
return data.Custom(w, name, prefix)
}
if data.Disabled {
return nil
}
if !data.OutputFile.Valid() {
return err
}
@ -189,8 +194,8 @@ func translateAndroidMkModule(ctx blueprint.SingletonContext, w io.Writer, mod b
fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
}
if data.Extra != nil {
err = data.Extra(w, data.OutputFile.Path())
for _, extra := range data.Extra {
err = extra(w, data.OutputFile.Path())
if err != nil {
return err
}

View File

@ -557,12 +557,14 @@ func InitArchModule(m AndroidModule,
for _, properties := range base.generalProperties {
propertiesValue := reflect.ValueOf(properties)
if propertiesValue.Kind() != reflect.Ptr {
panic("properties must be a pointer to a struct")
panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
propertiesValue.Interface()))
}
propertiesValue = propertiesValue.Elem()
if propertiesValue.Kind() != reflect.Struct {
panic("properties must be a pointer to a struct")
panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
propertiesValue.Interface()))
}
archProperties := &archProperties{}