blob: 9de2b0579356bdd690eb2ffa1df5d152a5c1e3f5 [file] [log] [blame]
Colin Cross1f7f3bd2016-07-27 10:12:38 -07001// Copyright (C) 2016 The Android Open Source Project
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 art
16
17import (
Colin Cross1f7f3bd2016-07-27 10:12:38 -070018 "android/soong/android"
19 "android/soong/cc"
20 "fmt"
Colin Cross6e95dd52016-09-12 15:37:10 -070021 "sync"
Colin Cross1f7f3bd2016-07-27 10:12:38 -070022
23 "github.com/google/blueprint"
24)
25
26var supportedArches = []string{"arm", "arm64", "mips", "mips64", "x86", "x86_64"}
27
28func globalFlags(ctx android.BaseContext) ([]string, []string) {
29 var cflags []string
30 var asflags []string
31
Colin Crossbe332ed2016-09-21 13:23:53 -070032 opt := envDefault(ctx, "ART_NDEBUG_OPT_FLAG", "-O3")
33 cflags = append(cflags, opt)
34
Colin Cross1f7f3bd2016-07-27 10:12:38 -070035 tlab := false
36
37 gcType := envDefault(ctx, "ART_DEFAULT_GC_TYPE", "CMS")
38
39 if envTrue(ctx, "ART_TEST_DEBUG_GC") {
40 gcType = "SS"
41 tlab = true
42 }
43
44 cflags = append(cflags, "-DART_DEFAULT_GC_TYPE_IS_"+gcType)
45 if tlab {
46 cflags = append(cflags, "-DART_USE_TLAB=1")
47 }
48
David Brazdil7b49e6c2016-09-01 11:06:18 +010049 if !envFalse(ctx, "ART_ENABLE_VDEX") {
50 cflags = append(cflags, "-DART_ENABLE_VDEX")
51 }
52
Colin Cross1f7f3bd2016-07-27 10:12:38 -070053 imtSize := envDefault(ctx, "ART_IMT_SIZE", "43")
54 cflags = append(cflags, "-DIMT_SIZE="+imtSize)
55
56 if envTrue(ctx, "ART_HEAP_POISONING") {
57 cflags = append(cflags, "-DART_HEAP_POISONING=1")
58 asflags = append(asflags, "-DART_HEAP_POISONING=1")
59 }
60
Hiroshi Yamauchida75ad72017-01-24 11:06:47 -080061 if !envFalse(ctx, "ART_USE_READ_BARRIER") && ctx.AConfig().ArtUseReadBarrier() {
Roland Levillainb81e9e92017-04-20 17:35:32 +010062 // Used to change the read barrier type. Valid values are BAKER, BROOKS,
63 // TABLELOOKUP. The default is BAKER.
Colin Cross1f7f3bd2016-07-27 10:12:38 -070064 barrierType := envDefault(ctx, "ART_READ_BARRIER_TYPE", "BAKER")
65 cflags = append(cflags,
66 "-DART_USE_READ_BARRIER=1",
67 "-DART_READ_BARRIER_TYPE_IS_"+barrierType+"=1")
68 asflags = append(asflags,
69 "-DART_USE_READ_BARRIER=1",
70 "-DART_READ_BARRIER_TYPE_IS_"+barrierType+"=1")
Colin Cross1f7f3bd2016-07-27 10:12:38 -070071 }
72
Nicolas Geoffray467d94a2017-03-16 10:24:17 +000073 if envTrue(ctx, "ART_USE_OLD_ARM_BACKEND") {
74 // Used to enable the old, pre-VIXL ARM code generator.
75 cflags = append(cflags, "-DART_USE_OLD_ARM_BACKEND=1")
76 asflags = append(asflags, "-DART_USE_OLD_ARM_BACKEND=1")
Scott Wakelinga7812ae2016-10-17 10:03:36 +010077 }
78
Andreas Gampe655c6fd2017-05-24 21:42:10 -070079 // We need larger stack overflow guards for ASAN, as the compiled code will have
80 // larger frame sizes. For simplicity, just use global not-target-specific cflags.
81 // Note: We increase this for both debug and non-debug, as the overflow gap will
82 // be compiled into managed code. We always preopt (and build core images) with
83 // the debug version. So make the gap consistent (and adjust for the worst).
84 if len(ctx.AConfig().SanitizeDevice()) > 0 || len(ctx.AConfig().SanitizeHost()) > 0 {
85 cflags = append(cflags,
86 "-DART_STACK_OVERFLOW_GAP_arm=8192",
87 "-DART_STACK_OVERFLOW_GAP_arm64=8192",
88 "-DART_STACK_OVERFLOW_GAP_mips=16384",
89 "-DART_STACK_OVERFLOW_GAP_mips64=16384",
90 "-DART_STACK_OVERFLOW_GAP_x86=12288",
91 "-DART_STACK_OVERFLOW_GAP_x86_64=20480")
92 } else {
93 cflags = append(cflags,
94 "-DART_STACK_OVERFLOW_GAP_arm=8192",
95 "-DART_STACK_OVERFLOW_GAP_arm64=8192",
96 "-DART_STACK_OVERFLOW_GAP_mips=16384",
97 "-DART_STACK_OVERFLOW_GAP_mips64=16384",
98 "-DART_STACK_OVERFLOW_GAP_x86=8192",
99 "-DART_STACK_OVERFLOW_GAP_x86_64=8192")
100 }
Andreas Gampebc9f10c2017-05-19 08:28:06 -0700101
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700102 return cflags, asflags
103}
104
Colin Crossbe332ed2016-09-21 13:23:53 -0700105func debugFlags(ctx android.BaseContext) []string {
106 var cflags []string
107
108 opt := envDefault(ctx, "ART_DEBUG_OPT_FLAG", "-O2")
109 cflags = append(cflags, opt)
110
111 return cflags
112}
113
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700114func deviceFlags(ctx android.BaseContext) []string {
115 var cflags []string
116 deviceFrameSizeLimit := 1736
117 if len(ctx.AConfig().SanitizeDevice()) > 0 {
Vishwath Mohan1ecc4fe2016-09-26 09:22:42 -0700118 deviceFrameSizeLimit = 7400
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700119 }
120 cflags = append(cflags,
121 fmt.Sprintf("-Wframe-larger-than=%d", deviceFrameSizeLimit),
122 fmt.Sprintf("-DART_FRAME_SIZE_LIMIT=%d", deviceFrameSizeLimit),
123 )
124
125 cflags = append(cflags, "-DART_BASE_ADDRESS="+ctx.AConfig().LibartImgDeviceBaseAddress())
126 if envTrue(ctx, "ART_TARGET_LINUX") {
127 cflags = append(cflags, "-DART_TARGET_LINUX")
128 } else {
129 cflags = append(cflags, "-DART_TARGET_ANDROID")
130 }
131 minDelta := envDefault(ctx, "LIBART_IMG_TARGET_MIN_BASE_ADDRESS_DELTA", "-0x1000000")
132 maxDelta := envDefault(ctx, "LIBART_IMG_TARGET_MAX_BASE_ADDRESS_DELTA", "0x1000000")
133 cflags = append(cflags, "-DART_BASE_ADDRESS_MIN_DELTA="+minDelta)
134 cflags = append(cflags, "-DART_BASE_ADDRESS_MAX_DELTA="+maxDelta)
135
136 return cflags
137}
138
139func hostFlags(ctx android.BaseContext) []string {
140 var cflags []string
141 hostFrameSizeLimit := 1736
Colin Cross3174b682016-09-19 12:25:31 -0700142 if len(ctx.AConfig().SanitizeHost()) > 0 {
143 // art/test/137-cfi/cfi.cc
144 // error: stack frame size of 1944 bytes in function 'Java_Main_unwindInProcess'
145 hostFrameSizeLimit = 6400
146 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700147 cflags = append(cflags,
148 fmt.Sprintf("-Wframe-larger-than=%d", hostFrameSizeLimit),
149 fmt.Sprintf("-DART_FRAME_SIZE_LIMIT=%d", hostFrameSizeLimit),
150 )
151
152 cflags = append(cflags, "-DART_BASE_ADDRESS="+ctx.AConfig().LibartImgHostBaseAddress())
153 minDelta := envDefault(ctx, "LIBART_IMG_HOST_MIN_BASE_ADDRESS_DELTA", "-0x1000000")
154 maxDelta := envDefault(ctx, "LIBART_IMG_HOST_MAX_BASE_ADDRESS_DELTA", "0x1000000")
155 cflags = append(cflags, "-DART_BASE_ADDRESS_MIN_DELTA="+minDelta)
156 cflags = append(cflags, "-DART_BASE_ADDRESS_MAX_DELTA="+maxDelta)
157
158 return cflags
159}
160
Colin Cross6e511782016-09-13 13:41:03 -0700161func globalDefaults(ctx android.LoadHookContext) {
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700162 type props struct {
163 Target struct {
164 Android struct {
165 Cflags []string
166 }
167 Host struct {
168 Cflags []string
169 }
170 }
171 Cflags []string
172 Asflags []string
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700173 Sanitize struct {
174 Recover []string
175 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700176 }
177
178 p := &props{}
179 p.Cflags, p.Asflags = globalFlags(ctx)
180 p.Target.Android.Cflags = deviceFlags(ctx)
181 p.Target.Host.Cflags = hostFlags(ctx)
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700182
183 if envTrue(ctx, "ART_DEX_FILE_ACCESS_TRACKING") {
184 p.Cflags = append(p.Cflags, "-DART_DEX_FILE_ACCESS_TRACKING")
185 p.Sanitize.Recover = []string {
186 "address",
187 }
188 }
189
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700190 ctx.AppendProperties(p)
Colin Crossfe6064a2016-08-30 13:49:26 -0700191}
Colin Cross6326d1b2016-09-06 10:24:28 -0700192
Colin Crossbe332ed2016-09-21 13:23:53 -0700193func debugDefaults(ctx android.LoadHookContext) {
194 type props struct {
195 Cflags []string
196 }
197
198 p := &props{}
199 p.Cflags = debugFlags(ctx)
200 ctx.AppendProperties(p)
201}
202
Colin Cross6e511782016-09-13 13:41:03 -0700203func customLinker(ctx android.LoadHookContext) {
Colin Crossfe6064a2016-08-30 13:49:26 -0700204 linker := envDefault(ctx, "CUSTOM_TARGET_LINKER", "")
205 if linker != "" {
206 type props struct {
207 DynamicLinker string
208 }
209
210 p := &props{}
211 p.DynamicLinker = linker
212 ctx.AppendProperties(p)
213 }
214}
215
Colin Cross6e511782016-09-13 13:41:03 -0700216func prefer32Bit(ctx android.LoadHookContext) {
Colin Cross6326d1b2016-09-06 10:24:28 -0700217 if envTrue(ctx, "HOST_PREFER_32_BIT") {
218 type props struct {
219 Target struct {
220 Host struct {
221 Compile_multilib string
222 }
223 }
224 }
225
226 p := &props{}
227 p.Target.Host.Compile_multilib = "prefer32"
228 ctx.AppendProperties(p)
229 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700230}
231
Colin Cross6e95dd52016-09-12 15:37:10 -0700232func testMap(config android.Config) map[string][]string {
233 return config.Once("artTests", func() interface{} {
234 return make(map[string][]string)
235 }).(map[string][]string)
236}
237
238func testInstall(ctx android.InstallHookContext) {
239 testMap := testMap(ctx.AConfig())
240
241 var name string
242 if ctx.Host() {
243 name = "host_"
244 } else {
245 name = "device_"
246 }
247 name += ctx.Arch().ArchType.String() + "_" + ctx.ModuleName()
248
249 artTestMutex.Lock()
250 defer artTestMutex.Unlock()
251
252 tests := testMap[name]
253 tests = append(tests, ctx.Path().RelPathString())
254 testMap[name] = tests
255}
256
257var artTestMutex sync.Mutex
258
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700259func init() {
Colin Cross96548c92016-10-12 14:26:55 -0700260 android.RegisterModuleType("art_cc_library", artLibrary)
261 android.RegisterModuleType("art_cc_binary", artBinary)
262 android.RegisterModuleType("art_cc_test", artTest)
263 android.RegisterModuleType("art_cc_test_library", artTestLibrary)
264 android.RegisterModuleType("art_cc_defaults", artDefaultsFactory)
265 android.RegisterModuleType("art_global_defaults", artGlobalDefaultsFactory)
266 android.RegisterModuleType("art_debug_defaults", artDebugDefaultsFactory)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700267}
268
269func artGlobalDefaultsFactory() (blueprint.Module, []interface{}) {
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700270 module, props := artDefaultsFactory()
Colin Cross6e511782016-09-13 13:41:03 -0700271 android.AddLoadHook(module, globalDefaults)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700272
273 return module, props
274}
275
Colin Crossbe332ed2016-09-21 13:23:53 -0700276func artDebugDefaultsFactory() (blueprint.Module, []interface{}) {
277 module, props := artDefaultsFactory()
278 android.AddLoadHook(module, debugDefaults)
279
280 return module, props
281}
282
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700283func artDefaultsFactory() (blueprint.Module, []interface{}) {
Colin Cross6e511782016-09-13 13:41:03 -0700284 c := &codegenProperties{}
285 module, props := cc.DefaultsFactory(c)
Colin Cross6e95dd52016-09-12 15:37:10 -0700286 android.AddLoadHook(module, func(ctx android.LoadHookContext) { codegen(ctx, c, true) })
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700287
288 return module, props
289}
290
291func artLibrary() (blueprint.Module, []interface{}) {
Colin Cross97916262016-12-09 14:47:29 -0800292 library, _ := cc.NewLibrary(android.HostAndDeviceSupported)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700293 module, props := library.Init()
294
Colin Cross6e95dd52016-09-12 15:37:10 -0700295 props = installCodegenCustomizer(module, props, true)
Colin Crossfe6064a2016-08-30 13:49:26 -0700296
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700297 return module, props
298}
299
Colin Cross123989f2016-09-07 14:12:50 -0700300func artBinary() (blueprint.Module, []interface{}) {
301 binary, _ := cc.NewBinary(android.HostAndDeviceSupported)
302 module, props := binary.Init()
303
Colin Cross6e511782016-09-13 13:41:03 -0700304 android.AddLoadHook(module, customLinker)
305 android.AddLoadHook(module, prefer32Bit)
Colin Cross123989f2016-09-07 14:12:50 -0700306 return module, props
307}
308
Colin Crossc7376e02016-09-08 12:52:18 -0700309func artTest() (blueprint.Module, []interface{}) {
310 test := cc.NewTest(android.HostAndDeviceSupported)
311 module, props := test.Init()
312
Colin Cross6e95dd52016-09-12 15:37:10 -0700313 props = installCodegenCustomizer(module, props, false)
314
Colin Cross6e511782016-09-13 13:41:03 -0700315 android.AddLoadHook(module, customLinker)
316 android.AddLoadHook(module, prefer32Bit)
Colin Cross6e95dd52016-09-12 15:37:10 -0700317 android.AddInstallHook(module, testInstall)
Colin Crossc7376e02016-09-08 12:52:18 -0700318 return module, props
319}
320
Colin Crossafd3c9e2016-09-16 13:47:21 -0700321func artTestLibrary() (blueprint.Module, []interface{}) {
322 test := cc.NewTestLibrary(android.HostAndDeviceSupported)
323 module, props := test.Init()
324
325 props = installCodegenCustomizer(module, props, false)
326
327 android.AddLoadHook(module, prefer32Bit)
328 android.AddInstallHook(module, testInstall)
329 return module, props
330}
331
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700332func envDefault(ctx android.BaseContext, key string, defaultValue string) string {
333 ret := ctx.AConfig().Getenv(key)
334 if ret == "" {
335 return defaultValue
336 }
337 return ret
338}
339
340func envTrue(ctx android.BaseContext, key string) bool {
341 return ctx.AConfig().Getenv(key) == "true"
342}
David Brazdil7b49e6c2016-09-01 11:06:18 +0100343
344func envFalse(ctx android.BaseContext, key string) bool {
345 return ctx.AConfig().Getenv(key) == "false"
346}