blob: 8fc428cd23f5c6feba07a56443effbab56ef12bf [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file generates the final rules for compiling all C/C++. All properties related to
18// compiling should have been translated into builderFlags or another argument to the Transform*
19// functions.
20
21import (
22 "android/soong/common"
23
Colin Cross3f40fa42015-01-30 17:27:36 -080024 "path/filepath"
25 "strings"
Colin Crossed4cf0b2015-03-26 14:43:45 -070026
27 "github.com/google/blueprint"
28 "github.com/google/blueprint/pathtools"
Colin Cross3f40fa42015-01-30 17:27:36 -080029)
30
31const (
32 sharedLibraryExtension = ".so"
33 staticLibraryExtension = ".a"
34)
35
36var (
37 pctx = blueprint.NewPackageContext("android/soong/cc")
38
39 cc = pctx.StaticRule("cc",
40 blueprint.RuleParams{
41 Depfile: "${out}.d",
42 Deps: blueprint.DepsGCC,
43 Command: "$ccCmd $incFlags -c $cFlags -MD -MF ${out}.d -o $out $in",
44 Description: "cc $out",
45 },
46 "ccCmd", "incFlags", "cFlags")
47
48 ld = pctx.StaticRule("ld",
49 blueprint.RuleParams{
Colin Cross7d21c442015-03-30 17:47:53 -070050 Command: "$ldCmd ${ldDirFlags} ${crtBegin} @${out}.rsp " +
Colin Cross3f40fa42015-01-30 17:27:36 -080051 "${libFlags} ${crtEnd} -o ${out} ${ldFlags} ${ldLibs}",
Colin Cross7d21c442015-03-30 17:47:53 -070052 Description: "ld $out",
53 Rspfile: "${out}.rsp",
54 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -080055 },
56 "ldCmd", "ldDirFlags", "crtBegin", "libFlags", "crtEnd", "ldFlags", "ldLibs")
57
58 partialLd = pctx.StaticRule("partialLd",
59 blueprint.RuleParams{
60 Command: "$ldCmd -r ${in} -o ${out}",
61 Description: "partialLd $out",
62 },
63 "ldCmd")
64
65 ar = pctx.StaticRule("ar",
66 blueprint.RuleParams{
Colin Cross7d21c442015-03-30 17:47:53 -070067 Command: "rm -f ${out} && $arCmd $arFlags $out @${out}.rsp",
68 Description: "ar $out",
69 Rspfile: "${out}.rsp",
70 RspfileContent: "${in}",
Colin Cross3f40fa42015-01-30 17:27:36 -080071 },
72 "arCmd", "arFlags")
73
Colin Crossbfae8852015-03-26 14:44:11 -070074 prefixSymbols = pctx.StaticRule("prefixSymbols",
75 blueprint.RuleParams{
76 Command: "$objcopyCmd --prefix-symbols=${prefix} ${in} ${out}",
77 Description: "prefixSymbols $out",
78 },
79 "objcopyCmd", "prefix")
80
Colin Cross3f40fa42015-01-30 17:27:36 -080081 copyGccLibPath = pctx.StaticVariable("copyGccLibPath", "${SrcDir}/build/soong/copygcclib.sh")
82
83 copyGccLib = pctx.StaticRule("copyGccLib",
84 blueprint.RuleParams{
85 Depfile: "${out}.d",
86 Deps: blueprint.DepsGCC,
87 Command: "$copyGccLibPath $out $ccCmd $cFlags -print-file-name=${libName}",
88 Description: "copy gcc $out",
89 },
90 "ccCmd", "cFlags", "libName")
91)
92
93type builderFlags struct {
94 globalFlags string
95 asFlags string
96 cFlags string
97 conlyFlags string
98 cppFlags string
99 ldFlags string
100 ldLibs string
101 incFlags string
Colin Cross581c1892015-04-07 16:50:10 -0700102 yaccFlags string
Colin Cross3f40fa42015-01-30 17:27:36 -0800103 nocrt bool
Colin Cross97ba0732015-03-23 17:50:24 -0700104 toolchain Toolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800105 clang bool
106}
107
108// Generate rules for compiling multiple .c, .cpp, or .S files to individual .o files
109func TransformSourceToObj(ctx common.AndroidModuleContext, subdir string, srcFiles []string,
Colin Cross581c1892015-04-07 16:50:10 -0700110 flags builderFlags, deps []string) (objFiles []string) {
111
112 srcRoot := ctx.Config().(Config).SrcDir()
113 intermediatesRoot := ctx.Config().(Config).IntermediatesDir()
Colin Cross3f40fa42015-01-30 17:27:36 -0800114
115 objFiles = make([]string, len(srcFiles))
116 objDir := common.ModuleObjDir(ctx)
117 if subdir != "" {
118 objDir = filepath.Join(objDir, subdir)
119 }
120
121 cflags := flags.globalFlags + " " + flags.cFlags + " " + flags.conlyFlags
122 cppflags := flags.globalFlags + " " + flags.cFlags + " " + flags.cppFlags
123 asflags := flags.globalFlags + " " + flags.asFlags
124
125 for i, srcFile := range srcFiles {
Colin Cross581c1892015-04-07 16:50:10 -0700126 var objFile string
127 if strings.HasPrefix(srcFile, srcRoot) {
128 objFile = strings.TrimPrefix(srcFile, srcRoot)
129 objFile = filepath.Join(objDir, objFile)
130 } else if strings.HasPrefix(srcFile, intermediatesRoot) {
131 objFile = strings.TrimPrefix(srcFile, intermediatesRoot)
132 objFile = filepath.Join(objDir, "gen", objFile)
133 } else {
134 ctx.ModuleErrorf("source file %q is not in source directory %q", srcFile, srcRoot)
135 continue
136 }
137
Colin Cross3f40fa42015-01-30 17:27:36 -0800138 objFile = pathtools.ReplaceExtension(objFile, "o")
139
140 objFiles[i] = objFile
141
142 var moduleCflags string
143 var ccCmd string
144
145 switch filepath.Ext(srcFile) {
146 case ".S", ".s":
147 ccCmd = "gcc"
148 moduleCflags = asflags
149 case ".c":
150 ccCmd = "gcc"
151 moduleCflags = cflags
152 case ".cpp", ".cc":
153 ccCmd = "g++"
154 moduleCflags = cppflags
155 default:
156 ctx.ModuleErrorf("File %s has unknown extension", srcFile)
157 continue
158 }
159
160 if flags.clang {
161 switch ccCmd {
162 case "gcc":
163 ccCmd = "clang"
164 case "g++":
165 ccCmd = "clang++"
166 default:
167 panic("unrecoginzied ccCmd")
168 }
169
170 ccCmd = "${clangPath}" + ccCmd
171 } else {
172 ccCmd = gccCmd(flags.toolchain, ccCmd)
173 }
174
Colin Cross581c1892015-04-07 16:50:10 -0700175 deps = append([]string{ccCmd}, deps...)
176
Colin Cross3f40fa42015-01-30 17:27:36 -0800177 ctx.Build(pctx, blueprint.BuildParams{
178 Rule: cc,
179 Outputs: []string{objFile},
180 Inputs: []string{srcFile},
Colin Cross581c1892015-04-07 16:50:10 -0700181 Implicits: deps,
Colin Cross3f40fa42015-01-30 17:27:36 -0800182 Args: map[string]string{
183 "cFlags": moduleCflags,
184 "incFlags": flags.incFlags,
185 "ccCmd": ccCmd,
186 },
187 })
188 }
189
190 return objFiles
191}
192
193// Generate a rule for compiling multiple .o files to a static library (.a)
194func TransformObjToStaticLib(ctx common.AndroidModuleContext, objFiles []string,
195 flags builderFlags, outputFile string) {
196
197 arCmd := gccCmd(flags.toolchain, "ar")
198 arFlags := "crsPD"
199
200 ctx.Build(pctx, blueprint.BuildParams{
201 Rule: ar,
202 Outputs: []string{outputFile},
203 Inputs: objFiles,
204 Implicits: []string{arCmd},
205 Args: map[string]string{
206 "arFlags": arFlags,
207 "arCmd": arCmd,
208 },
209 })
210}
211
212// Generate a rule for compiling multiple .o files, plus static libraries, whole static libraries,
213// and shared libraires, to a shared library (.so) or dynamic executable
214func TransformObjToDynamicBinary(ctx common.AndroidModuleContext,
Colin Cross77b00fa2015-03-16 16:15:49 -0700215 objFiles, sharedLibs, staticLibs, lateStaticLibs, wholeStaticLibs []string,
Colin Crossed4cf0b2015-03-26 14:43:45 -0700216 crtBegin, crtEnd string, groupLate bool, flags builderFlags, outputFile string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800217
218 var ldCmd string
219 if flags.clang {
220 ldCmd = "${clangPath}clang++"
221 } else {
222 ldCmd = gccCmd(flags.toolchain, "g++")
223 }
224
225 var ldDirs []string
226 var libFlagsList []string
227
228 if len(wholeStaticLibs) > 0 {
229 libFlagsList = append(libFlagsList, "-Wl,--whole-archive ")
230 libFlagsList = append(libFlagsList, wholeStaticLibs...)
231 libFlagsList = append(libFlagsList, "-Wl,--no-whole-archive ")
232 }
233
234 libFlagsList = append(libFlagsList, staticLibs...)
235
236 for _, lib := range sharedLibs {
237 dir, file := filepath.Split(lib)
238 if !strings.HasPrefix(file, "lib") {
239 panic("shared library " + lib + " does not start with lib")
240 }
241 if !strings.HasSuffix(file, sharedLibraryExtension) {
242 panic("shared library " + lib + " does not end with " + sharedLibraryExtension)
243 }
244 libFlagsList = append(libFlagsList,
245 "-l"+strings.TrimSuffix(strings.TrimPrefix(file, "lib"), sharedLibraryExtension))
246 ldDirs = append(ldDirs, dir)
247 }
248
Colin Crossed4cf0b2015-03-26 14:43:45 -0700249 if groupLate {
250 libFlagsList = append(libFlagsList, "-Wl,--start-group")
251 }
Colin Cross77b00fa2015-03-16 16:15:49 -0700252 libFlagsList = append(libFlagsList, lateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700253 if groupLate {
254 libFlagsList = append(libFlagsList, "-Wl,--end-group")
255 }
Colin Cross77b00fa2015-03-16 16:15:49 -0700256
Colin Cross3f40fa42015-01-30 17:27:36 -0800257 deps := []string{ldCmd}
258 deps = append(deps, sharedLibs...)
259 deps = append(deps, staticLibs...)
Colin Cross3075ad02015-03-17 10:47:08 -0700260 deps = append(deps, lateStaticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800261 deps = append(deps, wholeStaticLibs...)
262 if crtBegin != "" {
263 deps = append(deps, crtBegin, crtEnd)
264 }
265
266 ctx.Build(pctx, blueprint.BuildParams{
267 Rule: ld,
268 Outputs: []string{outputFile},
269 Inputs: objFiles,
270 Implicits: deps,
271 Args: map[string]string{
272 "ldCmd": ldCmd,
273 "ldDirFlags": ldDirsToFlags(ldDirs),
274 "crtBegin": crtBegin,
275 "libFlags": strings.Join(libFlagsList, " "),
276 "ldFlags": flags.ldFlags,
277 "crtEnd": crtEnd,
278 "ldLibs": flags.ldLibs,
279 },
280 })
281}
282
283// Generate a rule for compiling multiple .o files to a .o using ld partial linking
284func TransformObjsToObj(ctx common.AndroidModuleContext, objFiles []string,
285 flags builderFlags, outputFile string) {
286
287 ldCmd := gccCmd(flags.toolchain, "ld")
288
289 deps := []string{ldCmd}
290
291 ctx.Build(pctx, blueprint.BuildParams{
292 Rule: partialLd,
293 Outputs: []string{outputFile},
294 Inputs: objFiles,
295 Implicits: deps,
296 Args: map[string]string{
297 "ldCmd": ldCmd,
298 },
299 })
300}
301
Colin Crossbfae8852015-03-26 14:44:11 -0700302// Generate a rule for runing objcopy --prefix-symbols on a binary
303func TransformBinaryPrefixSymbols(ctx common.AndroidModuleContext, prefix string, inputFile string,
304 flags builderFlags, outputFile string) {
305
306 objcopyCmd := gccCmd(flags.toolchain, "objcopy")
307
308 ctx.Build(pctx, blueprint.BuildParams{
309 Rule: prefixSymbols,
310 Outputs: []string{outputFile},
311 Inputs: []string{inputFile},
312 Implicits: []string{objcopyCmd},
313 Args: map[string]string{
314 "objcopyCmd": objcopyCmd,
315 "prefix": prefix,
316 },
317 })
318}
319
Colin Cross3f40fa42015-01-30 17:27:36 -0800320func CopyGccLib(ctx common.AndroidModuleContext, libName string,
321 flags builderFlags, outputFile string) {
322
323 ctx.Build(pctx, blueprint.BuildParams{
324 Rule: copyGccLib,
325 Outputs: []string{outputFile},
326 Implicits: []string{
327 "$copyGccLibPath",
328 gccCmd(flags.toolchain, "gcc"),
329 },
330 Args: map[string]string{
331 "ccCmd": gccCmd(flags.toolchain, "gcc"),
332 "cFlags": flags.globalFlags,
333 "libName": libName,
334 },
335 })
336}
337
Colin Cross97ba0732015-03-23 17:50:24 -0700338func gccCmd(toolchain Toolchain, cmd string) string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800339 return filepath.Join(toolchain.GccRoot(), "bin", toolchain.GccTriple()+"-"+cmd)
340}