blob: 083cf0d08222bb81f4cd35e28efa8d175151d0fd [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// Copyright 2016 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
17import (
18 "github.com/google/blueprint"
Colin Crossb916a382016-07-29 17:28:03 -070019 "github.com/google/blueprint/proptools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070020
21 "android/soong"
22 "android/soong/android"
23)
24
25type BinaryLinkerProperties struct {
26 // compile executable with -static
27 Static_executable *bool `android:"arch_variant"`
28
29 // set the name of the output
30 Stem string `android:"arch_variant"`
31
32 // append to the name of the output
33 Suffix string `android:"arch_variant"`
34
35 // if set, add an extra objcopy --prefix-symbols= step
36 Prefix_symbols string
Colin Cross1e7d3702016-08-24 15:25:47 -070037
38 // if set, install a symlink to the preferred architecture
39 Symlink_preferred_arch bool
Colin Cross522e3732016-09-07 13:14:06 -070040
41 DynamicLinker string `blueprint:"mutated"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070042}
43
44func init() {
45 soong.RegisterModuleType("cc_binary", binaryFactory)
46 soong.RegisterModuleType("cc_binary_host", binaryHostFactory)
47}
48
49// Module factory for binaries
50func binaryFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070051 module, _ := NewBinary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070052 return module.Init()
53}
54
55// Module factory for host binaries
56func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070057 module, _ := NewBinary(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070058 return module.Init()
59}
60
61//
62// Executables
63//
64
Colin Crossb916a382016-07-29 17:28:03 -070065type binaryDecorator struct {
66 *baseLinker
Colin Cross1e7d3702016-08-24 15:25:47 -070067 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -070068 stripper
69
70 Properties BinaryLinkerProperties
71
Dan Willemsen4aa75ca2016-09-28 16:18:03 -070072 toolPath android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -070073}
74
Colin Crossb916a382016-07-29 17:28:03 -070075var _ linker = (*binaryDecorator)(nil)
Colin Cross4d9c2d12016-07-29 12:48:20 -070076
Colin Crossb916a382016-07-29 17:28:03 -070077func (binary *binaryDecorator) linkerProps() []interface{} {
Colin Cross42742b82016-08-01 13:20:05 -070078 return append(binary.baseLinker.linkerProps(),
Colin Cross4d9c2d12016-07-29 12:48:20 -070079 &binary.Properties,
80 &binary.stripper.StripProperties)
81
82}
83
Colin Crossb916a382016-07-29 17:28:03 -070084func (binary *binaryDecorator) getStem(ctx BaseModuleContext) string {
Colin Cross4d9c2d12016-07-29 12:48:20 -070085 stem := ctx.ModuleName()
86 if binary.Properties.Stem != "" {
87 stem = binary.Properties.Stem
88 }
89
90 return stem + binary.Properties.Suffix
91}
92
Colin Crossb916a382016-07-29 17:28:03 -070093func (binary *binaryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross42742b82016-08-01 13:20:05 -070094 deps = binary.baseLinker.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -070095 if ctx.Device() {
96 if !Bool(binary.baseLinker.Properties.Nocrt) {
97 if !ctx.sdk() {
Colin Crossb916a382016-07-29 17:28:03 -070098 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -070099 deps.CrtBegin = "crtbegin_static"
100 } else {
101 deps.CrtBegin = "crtbegin_dynamic"
102 }
103 deps.CrtEnd = "crtend_android"
104 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700105 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700106 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
107 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700108 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700109 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
110 } else {
111 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
112 }
113 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
114 }
115 }
116 }
117
Colin Crossb916a382016-07-29 17:28:03 -0700118 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700119 if inList("libc++_static", deps.StaticLibs) {
120 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
121 }
122 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
123 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
124 // move them to the beginning of deps.LateStaticLibs
125 var groupLibs []string
126 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
127 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
128 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
129 }
130 }
131
Colin Crossb916a382016-07-29 17:28:03 -0700132 if !binary.static() && inList("libc", deps.StaticLibs) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700133 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
134 "from static libs or set static_executable: true")
135 }
136 return deps
137}
138
Colin Crossb916a382016-07-29 17:28:03 -0700139func (binary *binaryDecorator) isDependencyRoot() bool {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700140 return true
141}
142
Colin Crossb916a382016-07-29 17:28:03 -0700143func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700144 module := newModule(hod, android.MultilibFirst)
Colin Crossb916a382016-07-29 17:28:03 -0700145 binary := &binaryDecorator{
Colin Cross1e7d3702016-08-24 15:25:47 -0700146 baseLinker: NewBaseLinker(),
147 baseInstaller: NewBaseInstaller("bin", "", InstallInSystem),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 }
Colin Crossb916a382016-07-29 17:28:03 -0700149 module.compiler = NewBaseCompiler()
150 module.linker = binary
Colin Cross1e7d3702016-08-24 15:25:47 -0700151 module.installer = binary
Colin Crossb916a382016-07-29 17:28:03 -0700152 return module, binary
Colin Cross4d9c2d12016-07-29 12:48:20 -0700153}
154
Colin Crossb916a382016-07-29 17:28:03 -0700155func (binary *binaryDecorator) linkerInit(ctx BaseModuleContext) {
Colin Cross42742b82016-08-01 13:20:05 -0700156 binary.baseLinker.linkerInit(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700157
Colin Cross4d9c2d12016-07-29 12:48:20 -0700158 if ctx.Host() {
159 if ctx.Os() == android.Linux {
160 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
Colin Crossb916a382016-07-29 17:28:03 -0700161 binary.Properties.Static_executable = proptools.BoolPtr(true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162 }
163 } else {
164 // Static executables are not supported on Darwin or Windows
Colin Crossb916a382016-07-29 17:28:03 -0700165 binary.Properties.Static_executable = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700166 }
167 }
Colin Cross1e7d3702016-08-24 15:25:47 -0700168
169 if binary.Properties.Symlink_preferred_arch {
170 if binary.Properties.Stem == "" && binary.Properties.Suffix == "" {
171 ctx.PropertyErrorf("symlink_preferred_arch", "must also specify stem or suffix")
172 }
Colin Cross8b74d172016-09-13 09:59:14 -0700173 if ctx.TargetPrimary() {
Colin Cross1e7d3702016-08-24 15:25:47 -0700174 binary.baseInstaller.Properties.Symlinks = append(binary.baseInstaller.Properties.Symlinks,
175 ctx.ModuleName())
176 }
177 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700178}
179
Colin Crossb916a382016-07-29 17:28:03 -0700180func (binary *binaryDecorator) static() bool {
181 return Bool(binary.Properties.Static_executable)
182}
183
184func (binary *binaryDecorator) staticBinary() bool {
185 return binary.static()
186}
187
188func (binary *binaryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700189 flags = binary.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700190
Colin Crossb916a382016-07-29 17:28:03 -0700191 if ctx.Host() && !binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700192 flags.LdFlags = append(flags.LdFlags, "-pie")
193 if ctx.Os() == android.Windows {
194 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
195 }
196 }
197
198 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
199 // all code is position independent, and then those warnings get promoted to
200 // errors.
201 if ctx.Os() != android.Windows {
202 flags.CFlags = append(flags.CFlags, "-fpie")
203 }
204
205 if ctx.Device() {
Colin Crossb916a382016-07-29 17:28:03 -0700206 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700207 // Clang driver needs -static to create static executable.
208 // However, bionic/linker uses -shared to overwrite.
209 // Linker for x86 targets does not allow coexistance of -static and -shared,
210 // so we add -static only if -shared is not used.
211 if !inList("-shared", flags.LdFlags) {
212 flags.LdFlags = append(flags.LdFlags, "-static")
213 }
214
215 flags.LdFlags = append(flags.LdFlags,
216 "-nostdlib",
217 "-Bstatic",
218 "-Wl,--gc-sections",
219 )
220
221 } else {
222 if flags.DynamicLinker == "" {
Colin Cross522e3732016-09-07 13:14:06 -0700223 if binary.Properties.DynamicLinker != "" {
224 flags.DynamicLinker = binary.Properties.DynamicLinker
225 } else {
226 flags.DynamicLinker = "/system/bin/linker"
227 if flags.Toolchain.Is64Bit() {
228 flags.DynamicLinker += "64"
229 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700230 }
231 }
232
233 flags.LdFlags = append(flags.LdFlags,
234 "-pie",
235 "-nostdlib",
236 "-Bdynamic",
237 "-Wl,--gc-sections",
238 "-Wl,-z,nocopyreloc",
239 )
240 }
241 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700242 if binary.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700243 flags.LdFlags = append(flags.LdFlags, "-static")
244 }
245 if ctx.Darwin() {
246 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
247 }
248 }
249
250 return flags
251}
252
Colin Crossb916a382016-07-29 17:28:03 -0700253func (binary *binaryDecorator) link(ctx ModuleContext,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
255
256 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
257 outputFile := android.PathForModuleOut(ctx, fileName)
258 ret := outputFile
Colin Cross4d9c2d12016-07-29 12:48:20 -0700259
260 var linkerDeps android.Paths
261
262 sharedLibs := deps.SharedLibs
263 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
264
265 if flags.DynamicLinker != "" {
266 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
267 }
268
269 builderFlags := flagsToBuilderFlags(flags)
270
271 if binary.stripper.needsStrip(ctx) {
272 strippedOutputFile := outputFile
273 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
274 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
275 }
276
277 if binary.Properties.Prefix_symbols != "" {
278 afterPrefixSymbols := outputFile
279 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
280 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
281 flagsToBuilderFlags(flags), afterPrefixSymbols)
282 }
283
Colin Cross26c34ed2016-09-30 17:10:16 -0700284 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
285 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
286
Colin Cross4d9c2d12016-07-29 12:48:20 -0700287 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
288 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
289 builderFlags, outputFile)
290
291 return ret
292}
293
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700294func (binary *binaryDecorator) install(ctx ModuleContext, file android.Path) {
295 binary.baseInstaller.install(ctx, file)
296 if ctx.Os().Class == android.Host {
297 binary.toolPath = android.OptionalPathForPath(binary.baseInstaller.path)
298 }
299}
300
301func (binary *binaryDecorator) hostToolPath() android.OptionalPath {
302 return binary.toolPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700303}