2015-01-31 09:27:36 +08: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
|
|
|
|
|
|
|
|
// This file generates the final rules for compiling all C/C++. All properties related to
|
|
|
|
// compiling should have been translated into builderFlags or another argument to the Transform*
|
|
|
|
// functions.
|
|
|
|
|
|
|
|
import (
|
2015-05-01 07:36:18 +08:00
|
|
|
"fmt"
|
2016-07-30 04:44:28 +08:00
|
|
|
"path/filepath"
|
2015-05-01 07:36:18 +08:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2015-01-31 09:27:36 +08:00
|
|
|
"strings"
|
2015-03-27 05:43:45 +08:00
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
2016-07-30 04:44:28 +08:00
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
"android/soong/cc/config"
|
2015-01-31 09:27:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-04-29 09:17:56 +08:00
|
|
|
objectExtension = ".o"
|
2015-01-31 09:27:36 +08:00
|
|
|
staticLibraryExtension = ".a"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-05-19 06:37:25 +08:00
|
|
|
pctx = android.NewPackageContext("android/soong/cc")
|
2015-01-31 09:27:36 +08:00
|
|
|
|
2016-08-30 07:14:13 +08:00
|
|
|
cc = pctx.AndroidGomaStaticRule("cc",
|
2015-01-31 09:27:36 +08:00
|
|
|
blueprint.RuleParams{
|
|
|
|
Depfile: "${out}.d",
|
|
|
|
Deps: blueprint.DepsGCC,
|
2016-08-27 03:55:49 +08:00
|
|
|
Command: "$relPwd ${config.CcWrapper}$ccCmd -c $cFlags -MD -MF ${out}.d -o $out $in",
|
2015-11-18 07:27:28 +08:00
|
|
|
CommandDeps: []string{"$ccCmd"},
|
2015-01-31 09:27:36 +08:00
|
|
|
Description: "cc $out",
|
|
|
|
},
|
2015-11-18 07:19:46 +08:00
|
|
|
"ccCmd", "cFlags")
|
2015-01-31 09:27:36 +08:00
|
|
|
|
2016-08-30 07:14:13 +08:00
|
|
|
ld = pctx.AndroidStaticRule("ld",
|
2015-01-31 09:27:36 +08:00
|
|
|
blueprint.RuleParams{
|
2016-07-22 04:16:49 +08:00
|
|
|
Command: "$ldCmd ${crtBegin} @${out}.rsp " +
|
2015-04-23 04:07:53 +08:00
|
|
|
"${libFlags} ${crtEnd} -o ${out} ${ldFlags}",
|
2015-11-18 07:27:28 +08:00
|
|
|
CommandDeps: []string{"$ldCmd"},
|
2015-03-31 08:47:53 +08:00
|
|
|
Description: "ld $out",
|
|
|
|
Rspfile: "${out}.rsp",
|
|
|
|
RspfileContent: "${in}",
|
2015-01-31 09:27:36 +08:00
|
|
|
},
|
2016-07-22 04:16:49 +08:00
|
|
|
"ldCmd", "crtBegin", "libFlags", "crtEnd", "ldFlags")
|
2015-01-31 09:27:36 +08:00
|
|
|
|
2016-08-30 07:14:13 +08:00
|
|
|
partialLd = pctx.AndroidStaticRule("partialLd",
|
2015-01-31 09:27:36 +08:00
|
|
|
blueprint.RuleParams{
|
2015-11-24 06:01:42 +08:00
|
|
|
Command: "$ldCmd -nostdlib -Wl,-r ${in} -o ${out} ${ldFlags}",
|
2015-11-18 07:27:28 +08:00
|
|
|
CommandDeps: []string{"$ldCmd"},
|
2015-01-31 09:27:36 +08:00
|
|
|
Description: "partialLd $out",
|
|
|
|
},
|
2015-11-24 06:01:42 +08:00
|
|
|
"ldCmd", "ldFlags")
|
2015-01-31 09:27:36 +08:00
|
|
|
|
2016-08-30 07:14:13 +08:00
|
|
|
ar = pctx.AndroidStaticRule("ar",
|
2015-01-31 09:27:36 +08:00
|
|
|
blueprint.RuleParams{
|
2015-03-31 08:47:53 +08:00
|
|
|
Command: "rm -f ${out} && $arCmd $arFlags $out @${out}.rsp",
|
2015-11-18 07:27:28 +08:00
|
|
|
CommandDeps: []string{"$arCmd"},
|
2015-03-31 08:47:53 +08:00
|
|
|
Description: "ar $out",
|
|
|
|
Rspfile: "${out}.rsp",
|
|
|
|
RspfileContent: "${in}",
|
2015-01-31 09:27:36 +08:00
|
|
|
},
|
|
|
|
"arCmd", "arFlags")
|
|
|
|
|
2016-08-30 07:14:13 +08:00
|
|
|
darwinAr = pctx.AndroidStaticRule("darwinAr",
|
2015-05-01 07:36:18 +08:00
|
|
|
blueprint.RuleParams{
|
2016-07-30 04:44:28 +08:00
|
|
|
Command: "rm -f ${out} && ${config.MacArPath} $arFlags $out $in",
|
|
|
|
CommandDeps: []string{"${config.MacArPath}"},
|
2015-05-01 07:36:18 +08:00
|
|
|
Description: "ar $out",
|
|
|
|
},
|
2016-05-04 06:10:29 +08:00
|
|
|
"arFlags")
|
2015-05-01 07:36:18 +08:00
|
|
|
|
2016-08-30 07:14:13 +08:00
|
|
|
darwinAppendAr = pctx.AndroidStaticRule("darwinAppendAr",
|
2015-05-01 07:36:18 +08:00
|
|
|
blueprint.RuleParams{
|
2016-07-30 04:44:28 +08:00
|
|
|
Command: "cp -f ${inAr} ${out}.tmp && ${config.MacArPath} $arFlags ${out}.tmp $in && mv ${out}.tmp ${out}",
|
|
|
|
CommandDeps: []string{"${config.MacArPath}", "${inAr}"},
|
2015-05-01 07:36:18 +08:00
|
|
|
Description: "ar $out",
|
|
|
|
},
|
2016-05-04 06:10:29 +08:00
|
|
|
"arFlags", "inAr")
|
|
|
|
|
2016-08-30 07:14:13 +08:00
|
|
|
darwinStrip = pctx.AndroidStaticRule("darwinStrip",
|
2016-05-04 06:10:29 +08:00
|
|
|
blueprint.RuleParams{
|
2016-08-02 06:42:38 +08:00
|
|
|
Command: "${config.MacStripPath} -u -r -o $out $in",
|
|
|
|
CommandDeps: []string{"${config.MacStripPath}"},
|
2016-05-04 06:10:29 +08:00
|
|
|
Description: "strip $out",
|
|
|
|
})
|
2015-05-01 07:36:18 +08:00
|
|
|
|
2016-08-30 07:14:13 +08:00
|
|
|
prefixSymbols = pctx.AndroidStaticRule("prefixSymbols",
|
2015-03-27 05:44:11 +08:00
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: "$objcopyCmd --prefix-symbols=${prefix} ${in} ${out}",
|
2015-11-18 07:27:28 +08:00
|
|
|
CommandDeps: []string{"$objcopyCmd"},
|
2015-03-27 05:44:11 +08:00
|
|
|
Description: "prefixSymbols $out",
|
|
|
|
},
|
|
|
|
"objcopyCmd", "prefix")
|
|
|
|
|
2016-04-29 05:50:03 +08:00
|
|
|
stripPath = pctx.SourcePathVariable("stripPath", "build/soong/scripts/strip.sh")
|
|
|
|
|
2016-08-30 07:14:13 +08:00
|
|
|
strip = pctx.AndroidStaticRule("strip",
|
2016-04-29 05:50:03 +08:00
|
|
|
blueprint.RuleParams{
|
|
|
|
Depfile: "${out}.d",
|
|
|
|
Deps: blueprint.DepsGCC,
|
|
|
|
Command: "CROSS_COMPILE=$crossCompile $stripPath ${args} -i ${in} -o ${out} -d ${out}.d",
|
|
|
|
CommandDeps: []string{"$stripPath"},
|
|
|
|
Description: "strip $out",
|
|
|
|
},
|
|
|
|
"args", "crossCompile")
|
|
|
|
|
2016-08-30 07:14:13 +08:00
|
|
|
emptyFile = pctx.AndroidStaticRule("emptyFile",
|
2016-05-14 05:05:09 +08:00
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: "rm -f $out && touch $out",
|
|
|
|
Description: "empty file $out",
|
|
|
|
})
|
|
|
|
|
2016-04-28 07:10:38 +08:00
|
|
|
copyGccLibPath = pctx.SourcePathVariable("copyGccLibPath", "build/soong/scripts/copygcclib.sh")
|
2015-01-31 09:27:36 +08:00
|
|
|
|
2016-08-30 07:14:13 +08:00
|
|
|
copyGccLib = pctx.AndroidStaticRule("copyGccLib",
|
2015-01-31 09:27:36 +08:00
|
|
|
blueprint.RuleParams{
|
|
|
|
Depfile: "${out}.d",
|
|
|
|
Deps: blueprint.DepsGCC,
|
|
|
|
Command: "$copyGccLibPath $out $ccCmd $cFlags -print-file-name=${libName}",
|
2015-11-18 07:27:28 +08:00
|
|
|
CommandDeps: []string{"$copyGccLibPath", "$ccCmd"},
|
2015-01-31 09:27:36 +08:00
|
|
|
Description: "copy gcc $out",
|
|
|
|
},
|
|
|
|
"ccCmd", "cFlags", "libName")
|
2016-10-01 08:10:16 +08:00
|
|
|
|
|
|
|
tocPath = pctx.SourcePathVariable("tocPath", "build/soong/scripts/toc.sh")
|
|
|
|
|
|
|
|
toc = pctx.AndroidStaticRule("toc",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Depfile: "${out}.d",
|
|
|
|
Deps: blueprint.DepsGCC,
|
|
|
|
Command: "CROSS_COMPILE=$crossCompile $tocPath -i ${in} -o ${out} -d ${out}.d",
|
|
|
|
CommandDeps: []string{"$tocPath"},
|
|
|
|
Restat: true,
|
|
|
|
},
|
|
|
|
"crossCompile")
|
2016-09-27 06:45:04 +08:00
|
|
|
|
|
|
|
clangTidy = pctx.AndroidStaticRule("clangTidy",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: "rm -f $out && ${config.ClangBin}/clang-tidy $tidyFlags $in -- $cFlags && touch $out",
|
|
|
|
CommandDeps: []string{"${config.ClangBin}/clang-tidy"},
|
|
|
|
Description: "tidy $out",
|
|
|
|
},
|
|
|
|
"cFlags", "tidyFlags")
|
2016-12-03 09:13:24 +08:00
|
|
|
|
|
|
|
yasmCmd = pctx.SourcePathVariable("yasmCmd", "prebuilts/misc/${config.HostPrebuiltTag}/yasm/yasm")
|
|
|
|
|
|
|
|
yasm = pctx.AndroidStaticRule("yasm",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: "$yasmCmd $asFlags -o $out $in",
|
|
|
|
CommandDeps: []string{"$yasmCmd"},
|
|
|
|
Description: "yasm $out",
|
|
|
|
},
|
|
|
|
"asFlags")
|
2015-01-31 09:27:36 +08:00
|
|
|
)
|
|
|
|
|
2015-11-18 07:19:46 +08:00
|
|
|
func init() {
|
|
|
|
// We run gcc/clang with PWD=/proc/self/cwd to remove $TOP from the
|
|
|
|
// debug output. That way two builds in two different directories will
|
|
|
|
// create the same output.
|
|
|
|
if runtime.GOOS != "darwin" {
|
|
|
|
pctx.StaticVariable("relPwd", "PWD=/proc/self/cwd")
|
|
|
|
} else {
|
|
|
|
// Darwin doesn't have /proc
|
|
|
|
pctx.StaticVariable("relPwd", "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-31 09:27:36 +08:00
|
|
|
type builderFlags struct {
|
|
|
|
globalFlags string
|
|
|
|
asFlags string
|
|
|
|
cFlags string
|
|
|
|
conlyFlags string
|
|
|
|
cppFlags string
|
|
|
|
ldFlags string
|
2016-01-07 06:41:07 +08:00
|
|
|
libFlags string
|
2015-04-08 07:50:10 +08:00
|
|
|
yaccFlags string
|
2016-10-21 07:11:43 +08:00
|
|
|
protoFlags string
|
2016-09-27 06:45:04 +08:00
|
|
|
tidyFlags string
|
2016-12-03 09:13:24 +08:00
|
|
|
yasmFlags string
|
2016-11-04 05:28:51 +08:00
|
|
|
aidlFlags string
|
2016-07-30 04:44:28 +08:00
|
|
|
toolchain config.Toolchain
|
2015-01-31 09:27:36 +08:00
|
|
|
clang bool
|
2016-09-27 06:45:04 +08:00
|
|
|
tidy bool
|
2017-02-10 08:16:31 +08:00
|
|
|
coverage bool
|
2016-04-29 05:50:03 +08:00
|
|
|
|
2016-12-02 06:45:23 +08:00
|
|
|
groupStaticLibs bool
|
|
|
|
|
2016-04-29 05:50:03 +08:00
|
|
|
stripKeepSymbols bool
|
|
|
|
stripKeepMiniDebugInfo bool
|
|
|
|
stripAddGnuDebuglink bool
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
2016-09-27 08:33:01 +08:00
|
|
|
type Objects struct {
|
2017-02-10 08:16:31 +08:00
|
|
|
objFiles android.Paths
|
|
|
|
tidyFiles android.Paths
|
|
|
|
coverageFiles android.Paths
|
2016-09-27 08:33:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a Objects) Copy() Objects {
|
|
|
|
return Objects{
|
2017-02-10 08:16:31 +08:00
|
|
|
objFiles: append(android.Paths{}, a.objFiles...),
|
|
|
|
tidyFiles: append(android.Paths{}, a.tidyFiles...),
|
|
|
|
coverageFiles: append(android.Paths{}, a.coverageFiles...),
|
2016-09-27 08:33:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a Objects) Append(b Objects) Objects {
|
|
|
|
return Objects{
|
2017-02-10 08:16:31 +08:00
|
|
|
objFiles: append(a.objFiles, b.objFiles...),
|
|
|
|
tidyFiles: append(a.tidyFiles, b.tidyFiles...),
|
|
|
|
coverageFiles: append(a.coverageFiles, b.coverageFiles...),
|
2016-09-27 08:33:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-31 09:27:36 +08:00
|
|
|
// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
|
2016-05-19 06:37:25 +08:00
|
|
|
func TransformSourceToObj(ctx android.ModuleContext, subdir string, srcFiles android.Paths,
|
2016-09-27 08:33:01 +08:00
|
|
|
flags builderFlags, deps android.Paths) Objects {
|
2015-04-08 07:50:10 +08:00
|
|
|
|
2016-09-27 08:33:01 +08:00
|
|
|
objFiles := make(android.Paths, len(srcFiles))
|
2016-09-27 06:45:04 +08:00
|
|
|
var tidyFiles android.Paths
|
|
|
|
if flags.tidy && flags.clang {
|
|
|
|
tidyFiles = make(android.Paths, 0, len(srcFiles))
|
|
|
|
}
|
2017-02-10 08:16:31 +08:00
|
|
|
var coverageFiles android.Paths
|
|
|
|
if flags.coverage {
|
|
|
|
coverageFiles = make(android.Paths, 0, len(srcFiles))
|
|
|
|
}
|
2015-01-31 09:27:36 +08:00
|
|
|
|
|
|
|
cflags := flags.globalFlags + " " + flags.cFlags + " " + flags.conlyFlags
|
|
|
|
cppflags := flags.globalFlags + " " + flags.cFlags + " " + flags.cppFlags
|
|
|
|
asflags := flags.globalFlags + " " + flags.asFlags
|
|
|
|
|
2016-03-04 09:21:04 +08:00
|
|
|
if flags.clang {
|
2016-07-30 04:44:28 +08:00
|
|
|
cflags += " ${config.NoOverrideClangGlobalCflags}"
|
|
|
|
cppflags += " ${config.NoOverrideClangGlobalCflags}"
|
2016-03-04 09:21:04 +08:00
|
|
|
} else {
|
2016-07-30 04:44:28 +08:00
|
|
|
cflags += " ${config.NoOverrideGlobalCflags}"
|
|
|
|
cppflags += " ${config.NoOverrideGlobalCflags}"
|
2016-03-04 09:21:04 +08:00
|
|
|
}
|
|
|
|
|
2015-01-31 09:27:36 +08:00
|
|
|
for i, srcFile := range srcFiles {
|
2016-11-03 11:43:13 +08:00
|
|
|
objFile := android.ObjPathWithExt(ctx, subdir, srcFile, "o")
|
2015-01-31 09:27:36 +08:00
|
|
|
|
|
|
|
objFiles[i] = objFile
|
|
|
|
|
2016-12-03 09:13:24 +08:00
|
|
|
if srcFile.Ext() == ".asm" {
|
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
|
|
|
Rule: yasm,
|
|
|
|
Output: objFile,
|
|
|
|
Input: srcFile,
|
|
|
|
OrderOnly: deps,
|
|
|
|
Args: map[string]string{
|
|
|
|
"asFlags": flags.yasmFlags,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-01-31 09:27:36 +08:00
|
|
|
var moduleCflags string
|
|
|
|
var ccCmd string
|
2016-09-27 06:45:04 +08:00
|
|
|
tidy := flags.tidy && flags.clang
|
2017-02-10 08:16:31 +08:00
|
|
|
coverage := flags.coverage
|
2015-01-31 09:27:36 +08:00
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
switch srcFile.Ext() {
|
2015-01-31 09:27:36 +08:00
|
|
|
case ".S", ".s":
|
|
|
|
ccCmd = "gcc"
|
|
|
|
moduleCflags = asflags
|
2016-09-27 06:45:04 +08:00
|
|
|
tidy = false
|
2017-02-10 08:16:31 +08:00
|
|
|
coverage = false
|
2015-01-31 09:27:36 +08:00
|
|
|
case ".c":
|
|
|
|
ccCmd = "gcc"
|
|
|
|
moduleCflags = cflags
|
2016-12-02 07:31:22 +08:00
|
|
|
case ".cpp", ".cc", ".mm":
|
2015-01-31 09:27:36 +08:00
|
|
|
ccCmd = "g++"
|
|
|
|
moduleCflags = cppflags
|
|
|
|
default:
|
|
|
|
ctx.ModuleErrorf("File %s has unknown extension", srcFile)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if flags.clang {
|
|
|
|
switch ccCmd {
|
|
|
|
case "gcc":
|
|
|
|
ccCmd = "clang"
|
|
|
|
case "g++":
|
|
|
|
ccCmd = "clang++"
|
|
|
|
default:
|
|
|
|
panic("unrecoginzied ccCmd")
|
|
|
|
}
|
|
|
|
|
2016-07-30 04:44:28 +08:00
|
|
|
ccCmd = "${config.ClangBin}/" + ccCmd
|
2015-01-31 09:27:36 +08:00
|
|
|
} else {
|
|
|
|
ccCmd = gccCmd(flags.toolchain, ccCmd)
|
|
|
|
}
|
|
|
|
|
2017-02-10 08:16:31 +08:00
|
|
|
var implicitOutputs android.WritablePaths
|
|
|
|
if coverage {
|
|
|
|
gcnoFile := android.ObjPathWithExt(ctx, subdir, srcFile, "gcno")
|
|
|
|
implicitOutputs = append(implicitOutputs, gcnoFile)
|
|
|
|
coverageFiles = append(coverageFiles, gcnoFile)
|
|
|
|
}
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2017-02-10 08:16:31 +08:00
|
|
|
Rule: cc,
|
|
|
|
Output: objFile,
|
|
|
|
ImplicitOutputs: implicitOutputs,
|
|
|
|
Input: srcFile,
|
|
|
|
OrderOnly: deps,
|
2015-01-31 09:27:36 +08:00
|
|
|
Args: map[string]string{
|
2015-04-23 04:07:53 +08:00
|
|
|
"cFlags": moduleCflags,
|
|
|
|
"ccCmd": ccCmd,
|
2015-01-31 09:27:36 +08:00
|
|
|
},
|
|
|
|
})
|
2016-09-27 06:45:04 +08:00
|
|
|
|
|
|
|
if tidy {
|
2016-11-03 11:43:13 +08:00
|
|
|
tidyFile := android.ObjPathWithExt(ctx, subdir, srcFile, "tidy")
|
2016-09-27 06:45:04 +08:00
|
|
|
tidyFiles = append(tidyFiles, tidyFile)
|
|
|
|
|
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
|
|
|
Rule: clangTidy,
|
|
|
|
Output: tidyFile,
|
|
|
|
Input: srcFile,
|
|
|
|
// We must depend on objFile, since clang-tidy doesn't
|
|
|
|
// support exporting dependencies.
|
|
|
|
Implicit: objFile,
|
|
|
|
Args: map[string]string{
|
|
|
|
"cFlags": moduleCflags,
|
|
|
|
"tidyFlags": flags.tidyFlags,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
2016-09-27 08:33:01 +08:00
|
|
|
return Objects{
|
2017-02-10 08:16:31 +08:00
|
|
|
objFiles: objFiles,
|
|
|
|
tidyFiles: tidyFiles,
|
|
|
|
coverageFiles: coverageFiles,
|
2016-09-27 08:33:01 +08:00
|
|
|
}
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a rule for compiling multiple .o files to a static library (.a)
|
2016-05-19 06:37:25 +08:00
|
|
|
func TransformObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
|
2016-09-27 06:45:04 +08:00
|
|
|
flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
|
2015-01-31 09:27:36 +08:00
|
|
|
|
2017-02-10 08:16:31 +08:00
|
|
|
if ctx.Darwin() {
|
|
|
|
transformDarwinObjToStaticLib(ctx, objFiles, flags, outputFile, deps)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-31 09:27:36 +08:00
|
|
|
arCmd := gccCmd(flags.toolchain, "ar")
|
|
|
|
arFlags := "crsPD"
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2016-09-27 06:45:04 +08:00
|
|
|
Rule: ar,
|
|
|
|
Output: outputFile,
|
|
|
|
Inputs: objFiles,
|
|
|
|
Implicits: deps,
|
2015-01-31 09:27:36 +08:00
|
|
|
Args: map[string]string{
|
|
|
|
"arFlags": arFlags,
|
|
|
|
"arCmd": arCmd,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-01 07:36:18 +08:00
|
|
|
// Generate a rule for compiling multiple .o files to a static library (.a) on
|
|
|
|
// darwin. The darwin ar tool doesn't support @file for list files, and has a
|
|
|
|
// very small command line length limit, so we have to split the ar into multiple
|
|
|
|
// steps, each appending to the previous one.
|
2017-02-10 08:16:31 +08:00
|
|
|
func transformDarwinObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
|
2016-09-27 06:45:04 +08:00
|
|
|
flags builderFlags, outputPath android.ModuleOutPath, deps android.Paths) {
|
2015-05-01 07:36:18 +08:00
|
|
|
|
|
|
|
arFlags := "cqs"
|
|
|
|
|
2016-05-14 05:05:09 +08:00
|
|
|
if len(objFiles) == 0 {
|
2016-05-19 06:37:25 +08:00
|
|
|
dummy := android.PathForModuleOut(ctx, "dummy"+objectExtension)
|
|
|
|
dummyAr := android.PathForModuleOut(ctx, "dummy"+staticLibraryExtension)
|
2016-05-14 05:05:09 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2016-09-27 06:45:04 +08:00
|
|
|
Rule: emptyFile,
|
|
|
|
Output: dummy,
|
|
|
|
Implicits: deps,
|
2016-05-14 05:05:09 +08:00
|
|
|
})
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2016-05-14 05:05:09 +08:00
|
|
|
Rule: darwinAr,
|
|
|
|
Output: dummyAr,
|
|
|
|
Input: dummy,
|
|
|
|
Args: map[string]string{
|
|
|
|
"arFlags": arFlags,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2016-05-14 05:05:09 +08:00
|
|
|
Rule: darwinAppendAr,
|
|
|
|
Output: outputPath,
|
|
|
|
Input: dummy,
|
|
|
|
Args: map[string]string{
|
|
|
|
"arFlags": "d",
|
|
|
|
"inAr": dummyAr.String(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-01 07:36:18 +08:00
|
|
|
// ARG_MAX on darwin is 262144, use half that to be safe
|
2015-09-24 06:26:20 +08:00
|
|
|
objFilesLists, err := splitListForSize(objFiles.Strings(), 131072)
|
2015-05-01 07:36:18 +08:00
|
|
|
if err != nil {
|
|
|
|
ctx.ModuleErrorf("%s", err.Error())
|
|
|
|
}
|
|
|
|
|
2015-09-24 06:26:20 +08:00
|
|
|
outputFile := outputPath.String()
|
|
|
|
|
2015-05-01 07:36:18 +08:00
|
|
|
var in, out string
|
|
|
|
for i, l := range objFilesLists {
|
|
|
|
in = out
|
|
|
|
out = outputFile
|
|
|
|
if i != len(objFilesLists)-1 {
|
|
|
|
out += "." + strconv.Itoa(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if in == "" {
|
|
|
|
ctx.Build(pctx, blueprint.BuildParams{
|
2016-09-27 06:45:04 +08:00
|
|
|
Rule: darwinAr,
|
|
|
|
Outputs: []string{out},
|
|
|
|
Inputs: l,
|
|
|
|
Implicits: deps.Strings(),
|
2015-05-01 07:36:18 +08:00
|
|
|
Args: map[string]string{
|
|
|
|
"arFlags": arFlags,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
ctx.Build(pctx, blueprint.BuildParams{
|
2016-05-19 06:37:25 +08:00
|
|
|
Rule: darwinAppendAr,
|
|
|
|
Outputs: []string{out},
|
|
|
|
Inputs: l,
|
2015-05-01 07:36:18 +08:00
|
|
|
Args: map[string]string{
|
|
|
|
"arFlags": arFlags,
|
|
|
|
"inAr": in,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-31 09:27:36 +08:00
|
|
|
// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
|
|
|
|
// and shared libraires, to a shared library (.so) or dynamic executable
|
2016-05-19 06:37:25 +08:00
|
|
|
func TransformObjToDynamicBinary(ctx android.ModuleContext,
|
|
|
|
objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs, deps android.Paths,
|
|
|
|
crtBegin, crtEnd android.OptionalPath, groupLate bool, flags builderFlags, outputFile android.WritablePath) {
|
2015-01-31 09:27:36 +08:00
|
|
|
|
|
|
|
var ldCmd string
|
|
|
|
if flags.clang {
|
2016-07-30 04:44:28 +08:00
|
|
|
ldCmd = "${config.ClangBin}/clang++"
|
2015-01-31 09:27:36 +08:00
|
|
|
} else {
|
|
|
|
ldCmd = gccCmd(flags.toolchain, "g++")
|
|
|
|
}
|
|
|
|
|
|
|
|
var libFlagsList []string
|
|
|
|
|
2016-01-07 06:41:07 +08:00
|
|
|
if len(flags.libFlags) > 0 {
|
|
|
|
libFlagsList = append(libFlagsList, flags.libFlags)
|
|
|
|
}
|
|
|
|
|
2015-01-31 09:27:36 +08:00
|
|
|
if len(wholeStaticLibs) > 0 {
|
2015-11-25 09:53:15 +08:00
|
|
|
if ctx.Host() && ctx.Darwin() {
|
2016-05-19 06:37:25 +08:00
|
|
|
libFlagsList = append(libFlagsList, android.JoinWithPrefix(wholeStaticLibs.Strings(), "-force_load "))
|
2015-05-01 07:36:18 +08:00
|
|
|
} else {
|
|
|
|
libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
|
2015-09-24 06:26:20 +08:00
|
|
|
libFlagsList = append(libFlagsList, wholeStaticLibs.Strings()...)
|
2015-05-01 07:36:18 +08:00
|
|
|
libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
|
|
|
|
}
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
2016-12-06 10:47:39 +08:00
|
|
|
if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
|
2016-12-02 06:45:23 +08:00
|
|
|
libFlagsList = append(libFlagsList, "-Wl,--start-group")
|
|
|
|
}
|
2015-09-24 06:26:20 +08:00
|
|
|
libFlagsList = append(libFlagsList, staticLibs.Strings()...)
|
2016-12-06 10:47:39 +08:00
|
|
|
if flags.groupStaticLibs && !ctx.Darwin() && len(staticLibs) > 0 {
|
2016-12-02 06:45:23 +08:00
|
|
|
libFlagsList = append(libFlagsList, "-Wl,--end-group")
|
|
|
|
}
|
2015-01-31 09:27:36 +08:00
|
|
|
|
2016-07-19 06:54:54 +08:00
|
|
|
if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
|
2015-07-09 04:02:23 +08:00
|
|
|
libFlagsList = append(libFlagsList, "-Wl,--start-group")
|
|
|
|
}
|
2015-09-24 06:26:20 +08:00
|
|
|
libFlagsList = append(libFlagsList, lateStaticLibs.Strings()...)
|
2016-07-19 06:54:54 +08:00
|
|
|
if groupLate && !ctx.Darwin() && len(lateStaticLibs) > 0 {
|
2015-07-09 04:02:23 +08:00
|
|
|
libFlagsList = append(libFlagsList, "-Wl,--end-group")
|
|
|
|
}
|
|
|
|
|
2015-01-31 09:27:36 +08:00
|
|
|
for _, lib := range sharedLibs {
|
2016-07-21 23:47:33 +08:00
|
|
|
libFlagsList = append(libFlagsList, lib.String())
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
deps = append(deps, staticLibs...)
|
2015-03-18 01:47:08 +08:00
|
|
|
deps = append(deps, lateStaticLibs...)
|
2015-01-31 09:27:36 +08:00
|
|
|
deps = append(deps, wholeStaticLibs...)
|
2015-09-24 06:26:20 +08:00
|
|
|
if crtBegin.Valid() {
|
|
|
|
deps = append(deps, crtBegin.Path(), crtEnd.Path())
|
2015-01-31 09:27:36 +08:00
|
|
|
}
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2015-01-31 09:27:36 +08:00
|
|
|
Rule: ld,
|
2015-09-24 06:26:20 +08:00
|
|
|
Output: outputFile,
|
2015-01-31 09:27:36 +08:00
|
|
|
Inputs: objFiles,
|
|
|
|
Implicits: deps,
|
|
|
|
Args: map[string]string{
|
2016-07-22 04:16:49 +08:00
|
|
|
"ldCmd": ldCmd,
|
|
|
|
"crtBegin": crtBegin.String(),
|
|
|
|
"libFlags": strings.Join(libFlagsList, " "),
|
|
|
|
"ldFlags": flags.ldFlags,
|
|
|
|
"crtEnd": crtEnd.String(),
|
2015-01-31 09:27:36 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-01 08:10:16 +08:00
|
|
|
// Generate a rule for extract a table of contents from a shared library (.so)
|
|
|
|
func TransformSharedObjectToToc(ctx android.ModuleContext, inputFile android.WritablePath,
|
|
|
|
outputFile android.WritablePath, flags builderFlags) {
|
|
|
|
|
|
|
|
crossCompile := gccCmd(flags.toolchain, "")
|
|
|
|
|
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
|
|
|
Rule: toc,
|
|
|
|
Output: outputFile,
|
|
|
|
Input: inputFile,
|
|
|
|
Args: map[string]string{
|
|
|
|
"crossCompile": crossCompile,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-01-31 09:27:36 +08:00
|
|
|
// Generate a rule for compiling multiple .o files to a .o using ld partial linking
|
2016-05-19 06:37:25 +08:00
|
|
|
func TransformObjsToObj(ctx android.ModuleContext, objFiles android.Paths,
|
|
|
|
flags builderFlags, outputFile android.WritablePath) {
|
2015-01-31 09:27:36 +08:00
|
|
|
|
2015-11-24 06:01:42 +08:00
|
|
|
var ldCmd string
|
|
|
|
if flags.clang {
|
2016-07-30 04:44:28 +08:00
|
|
|
ldCmd = "${config.ClangBin}/clang++"
|
2015-11-24 06:01:42 +08:00
|
|
|
} else {
|
|
|
|
ldCmd = gccCmd(flags.toolchain, "g++")
|
|
|
|
}
|
2015-01-31 09:27:36 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2015-09-24 06:26:20 +08:00
|
|
|
Rule: partialLd,
|
|
|
|
Output: outputFile,
|
|
|
|
Inputs: objFiles,
|
2015-01-31 09:27:36 +08:00
|
|
|
Args: map[string]string{
|
2015-09-24 06:26:20 +08:00
|
|
|
"ldCmd": ldCmd,
|
2015-11-24 06:01:42 +08:00
|
|
|
"ldFlags": flags.ldFlags,
|
2015-01-31 09:27:36 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-27 05:44:11 +08:00
|
|
|
// Generate a rule for runing objcopy --prefix-symbols on a binary
|
2016-05-19 06:37:25 +08:00
|
|
|
func TransformBinaryPrefixSymbols(ctx android.ModuleContext, prefix string, inputFile android.Path,
|
|
|
|
flags builderFlags, outputFile android.WritablePath) {
|
2015-03-27 05:44:11 +08:00
|
|
|
|
|
|
|
objcopyCmd := gccCmd(flags.toolchain, "objcopy")
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2015-09-24 06:26:20 +08:00
|
|
|
Rule: prefixSymbols,
|
|
|
|
Output: outputFile,
|
|
|
|
Input: inputFile,
|
2015-03-27 05:44:11 +08:00
|
|
|
Args: map[string]string{
|
|
|
|
"objcopyCmd": objcopyCmd,
|
|
|
|
"prefix": prefix,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
func TransformStrip(ctx android.ModuleContext, inputFile android.Path,
|
|
|
|
outputFile android.WritablePath, flags builderFlags) {
|
2016-04-29 05:50:03 +08:00
|
|
|
|
|
|
|
crossCompile := gccCmd(flags.toolchain, "")
|
|
|
|
args := ""
|
|
|
|
if flags.stripAddGnuDebuglink {
|
|
|
|
args += " --add-gnu-debuglink"
|
|
|
|
}
|
|
|
|
if flags.stripKeepMiniDebugInfo {
|
|
|
|
args += " --keep-mini-debug-info"
|
|
|
|
}
|
|
|
|
if flags.stripKeepSymbols {
|
|
|
|
args += " --keep-symbols"
|
|
|
|
}
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2016-04-29 05:50:03 +08:00
|
|
|
Rule: strip,
|
|
|
|
Output: outputFile,
|
|
|
|
Input: inputFile,
|
|
|
|
Args: map[string]string{
|
|
|
|
"crossCompile": crossCompile,
|
|
|
|
"args": args,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
func TransformDarwinStrip(ctx android.ModuleContext, inputFile android.Path,
|
|
|
|
outputFile android.WritablePath) {
|
2016-05-04 06:10:29 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2016-05-04 06:10:29 +08:00
|
|
|
Rule: darwinStrip,
|
|
|
|
Output: outputFile,
|
|
|
|
Input: inputFile,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-10 08:16:31 +08:00
|
|
|
func TransformCoverageFilesToLib(ctx android.ModuleContext,
|
|
|
|
inputs Objects, flags builderFlags, baseName string) android.OptionalPath {
|
|
|
|
|
|
|
|
if len(inputs.coverageFiles) > 0 {
|
|
|
|
outputFile := android.PathForModuleOut(ctx, baseName+".gcnodir")
|
|
|
|
|
|
|
|
TransformObjToStaticLib(ctx, inputs.coverageFiles, flags, outputFile, nil)
|
|
|
|
|
|
|
|
return android.OptionalPathForPath(outputFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
return android.OptionalPath{}
|
|
|
|
}
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
func CopyGccLib(ctx android.ModuleContext, libName string,
|
|
|
|
flags builderFlags, outputFile android.WritablePath) {
|
2015-01-31 09:27:36 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2015-09-24 06:26:20 +08:00
|
|
|
Rule: copyGccLib,
|
|
|
|
Output: outputFile,
|
2015-01-31 09:27:36 +08:00
|
|
|
Args: map[string]string{
|
|
|
|
"ccCmd": gccCmd(flags.toolchain, "gcc"),
|
|
|
|
"cFlags": flags.globalFlags,
|
|
|
|
"libName": libName,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-30 04:44:28 +08:00
|
|
|
func gccCmd(toolchain config.Toolchain, cmd string) string {
|
2015-01-31 09:27:36 +08:00
|
|
|
return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
|
|
|
|
}
|
2015-05-01 07:36:18 +08:00
|
|
|
|
|
|
|
func splitListForSize(list []string, limit int) (lists [][]string, err error) {
|
|
|
|
var i int
|
|
|
|
|
|
|
|
start := 0
|
|
|
|
bytes := 0
|
|
|
|
for i = range list {
|
|
|
|
l := len(list[i])
|
|
|
|
if l > limit {
|
|
|
|
return nil, fmt.Errorf("list element greater than size limit (%d)", limit)
|
|
|
|
}
|
|
|
|
if bytes+l > limit {
|
|
|
|
lists = append(lists, list[start:i])
|
|
|
|
start = i
|
|
|
|
bytes = 0
|
|
|
|
}
|
|
|
|
bytes += l + 1 // count a space between each list element
|
|
|
|
}
|
|
|
|
|
|
|
|
lists = append(lists, list[start:])
|
|
|
|
|
|
|
|
totalLen := 0
|
|
|
|
for _, l := range lists {
|
|
|
|
totalLen += len(l)
|
|
|
|
}
|
|
|
|
if totalLen != len(list) {
|
|
|
|
panic(fmt.Errorf("Failed breaking up list, %d != %d", len(list), totalLen))
|
|
|
|
}
|
|
|
|
return lists, nil
|
|
|
|
}
|