blob: 4e48d2d932d7b5b9500b84cd34800bf7dda826d1 [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 Cross84b69332017-11-01 14:23:17 -070022
23 "github.com/google/blueprint/proptools"
Colin Cross1f7f3bd2016-07-27 10:12:38 -070024)
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
Andreas Gampe655c6fd2017-05-24 21:42:10 -070073 // We need larger stack overflow guards for ASAN, as the compiled code will have
74 // larger frame sizes. For simplicity, just use global not-target-specific cflags.
75 // Note: We increase this for both debug and non-debug, as the overflow gap will
76 // be compiled into managed code. We always preopt (and build core images) with
77 // the debug version. So make the gap consistent (and adjust for the worst).
78 if len(ctx.AConfig().SanitizeDevice()) > 0 || len(ctx.AConfig().SanitizeHost()) > 0 {
79 cflags = append(cflags,
Colin Cross2db58b62017-06-27 10:38:31 -070080 "-DART_STACK_OVERFLOW_GAP_arm=8192",
81 "-DART_STACK_OVERFLOW_GAP_arm64=8192",
82 "-DART_STACK_OVERFLOW_GAP_mips=16384",
83 "-DART_STACK_OVERFLOW_GAP_mips64=16384",
84 "-DART_STACK_OVERFLOW_GAP_x86=16384",
85 "-DART_STACK_OVERFLOW_GAP_x86_64=20480")
Andreas Gampe655c6fd2017-05-24 21:42:10 -070086 } else {
87 cflags = append(cflags,
Colin Cross2db58b62017-06-27 10:38:31 -070088 "-DART_STACK_OVERFLOW_GAP_arm=8192",
89 "-DART_STACK_OVERFLOW_GAP_arm64=8192",
90 "-DART_STACK_OVERFLOW_GAP_mips=16384",
91 "-DART_STACK_OVERFLOW_GAP_mips64=16384",
92 "-DART_STACK_OVERFLOW_GAP_x86=8192",
93 "-DART_STACK_OVERFLOW_GAP_x86_64=8192")
Andreas Gampe655c6fd2017-05-24 21:42:10 -070094 }
Andreas Gampebc9f10c2017-05-19 08:28:06 -070095
Andreas Gampedbf0e0f2017-07-11 08:34:48 -070096 if envTrue(ctx, "ART_ENABLE_ADDRESS_SANITIZER") {
97 // Used to enable full sanitization, i.e., user poisoning, under ASAN.
98 cflags = append(cflags, "-DART_ENABLE_ADDRESS_SANITIZER=1")
99 asflags = append(asflags, "-DART_ENABLE_ADDRESS_SANITIZER=1")
100 }
101
Chris Larsen715f43e2017-10-23 11:00:32 -0700102 if envTrue(ctx, "ART_MIPS32_CHECK_ALIGNMENT") {
103 // Enable the use of MIPS32 CHECK_ALIGNMENT macro for debugging purposes
104 asflags = append(asflags, "-DART_MIPS32_CHECK_ALIGNMENT")
105 }
106
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700107 return cflags, asflags
108}
109
Colin Crossbe332ed2016-09-21 13:23:53 -0700110func debugFlags(ctx android.BaseContext) []string {
111 var cflags []string
112
113 opt := envDefault(ctx, "ART_DEBUG_OPT_FLAG", "-O2")
114 cflags = append(cflags, opt)
115
116 return cflags
117}
118
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700119func deviceFlags(ctx android.BaseContext) []string {
120 var cflags []string
121 deviceFrameSizeLimit := 1736
122 if len(ctx.AConfig().SanitizeDevice()) > 0 {
Vishwath Mohan1ecc4fe2016-09-26 09:22:42 -0700123 deviceFrameSizeLimit = 7400
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700124 }
125 cflags = append(cflags,
126 fmt.Sprintf("-Wframe-larger-than=%d", deviceFrameSizeLimit),
127 fmt.Sprintf("-DART_FRAME_SIZE_LIMIT=%d", deviceFrameSizeLimit),
128 )
129
130 cflags = append(cflags, "-DART_BASE_ADDRESS="+ctx.AConfig().LibartImgDeviceBaseAddress())
131 if envTrue(ctx, "ART_TARGET_LINUX") {
132 cflags = append(cflags, "-DART_TARGET_LINUX")
133 } else {
134 cflags = append(cflags, "-DART_TARGET_ANDROID")
135 }
136 minDelta := envDefault(ctx, "LIBART_IMG_TARGET_MIN_BASE_ADDRESS_DELTA", "-0x1000000")
137 maxDelta := envDefault(ctx, "LIBART_IMG_TARGET_MAX_BASE_ADDRESS_DELTA", "0x1000000")
138 cflags = append(cflags, "-DART_BASE_ADDRESS_MIN_DELTA="+minDelta)
139 cflags = append(cflags, "-DART_BASE_ADDRESS_MAX_DELTA="+maxDelta)
140
141 return cflags
142}
143
144func hostFlags(ctx android.BaseContext) []string {
145 var cflags []string
146 hostFrameSizeLimit := 1736
Colin Cross3174b682016-09-19 12:25:31 -0700147 if len(ctx.AConfig().SanitizeHost()) > 0 {
148 // art/test/137-cfi/cfi.cc
149 // error: stack frame size of 1944 bytes in function 'Java_Main_unwindInProcess'
150 hostFrameSizeLimit = 6400
151 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700152 cflags = append(cflags,
153 fmt.Sprintf("-Wframe-larger-than=%d", hostFrameSizeLimit),
154 fmt.Sprintf("-DART_FRAME_SIZE_LIMIT=%d", hostFrameSizeLimit),
155 )
156
157 cflags = append(cflags, "-DART_BASE_ADDRESS="+ctx.AConfig().LibartImgHostBaseAddress())
158 minDelta := envDefault(ctx, "LIBART_IMG_HOST_MIN_BASE_ADDRESS_DELTA", "-0x1000000")
159 maxDelta := envDefault(ctx, "LIBART_IMG_HOST_MAX_BASE_ADDRESS_DELTA", "0x1000000")
160 cflags = append(cflags, "-DART_BASE_ADDRESS_MIN_DELTA="+minDelta)
161 cflags = append(cflags, "-DART_BASE_ADDRESS_MAX_DELTA="+maxDelta)
162
Andreas Gampefcc7d672017-07-19 10:30:22 -0700163 if len(ctx.AConfig().SanitizeHost()) > 0 && !envFalse(ctx, "ART_ENABLE_ADDRESS_SANITIZER") {
164 // We enable full sanitization on the host by default.
165 cflags = append(cflags, "-DART_ENABLE_ADDRESS_SANITIZER=1")
166 }
167
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700168 return cflags
169}
170
Colin Cross6e511782016-09-13 13:41:03 -0700171func globalDefaults(ctx android.LoadHookContext) {
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700172 type props struct {
173 Target struct {
174 Android struct {
175 Cflags []string
176 }
177 Host struct {
178 Cflags []string
179 }
180 }
Colin Cross2db58b62017-06-27 10:38:31 -0700181 Cflags []string
182 Asflags []string
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700183 Sanitize struct {
Colin Cross2db58b62017-06-27 10:38:31 -0700184 Recover []string
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700185 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700186 }
187
188 p := &props{}
189 p.Cflags, p.Asflags = globalFlags(ctx)
190 p.Target.Android.Cflags = deviceFlags(ctx)
191 p.Target.Host.Cflags = hostFlags(ctx)
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700192
193 if envTrue(ctx, "ART_DEX_FILE_ACCESS_TRACKING") {
194 p.Cflags = append(p.Cflags, "-DART_DEX_FILE_ACCESS_TRACKING")
Colin Cross2db58b62017-06-27 10:38:31 -0700195 p.Sanitize.Recover = []string{
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700196 "address",
197 }
198 }
199
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700200 ctx.AppendProperties(p)
Colin Crossfe6064a2016-08-30 13:49:26 -0700201}
Colin Cross6326d1b2016-09-06 10:24:28 -0700202
Colin Crossbe332ed2016-09-21 13:23:53 -0700203func debugDefaults(ctx android.LoadHookContext) {
204 type props struct {
205 Cflags []string
206 }
207
208 p := &props{}
209 p.Cflags = debugFlags(ctx)
210 ctx.AppendProperties(p)
211}
212
Colin Cross6e511782016-09-13 13:41:03 -0700213func customLinker(ctx android.LoadHookContext) {
Colin Crossfe6064a2016-08-30 13:49:26 -0700214 linker := envDefault(ctx, "CUSTOM_TARGET_LINKER", "")
Colin Cross84b69332017-11-01 14:23:17 -0700215 type props struct {
216 DynamicLinker string
Colin Crossfe6064a2016-08-30 13:49:26 -0700217 }
Colin Cross84b69332017-11-01 14:23:17 -0700218
219 p := &props{}
220 if linker != "" {
221 p.DynamicLinker = linker
222 }
223
224 ctx.AppendProperties(p)
Colin Crossfe6064a2016-08-30 13:49:26 -0700225}
226
Colin Cross6e511782016-09-13 13:41:03 -0700227func prefer32Bit(ctx android.LoadHookContext) {
Colin Cross84b69332017-11-01 14:23:17 -0700228 type props struct {
229 Target struct {
230 Host struct {
231 Compile_multilib *string
Colin Cross6326d1b2016-09-06 10:24:28 -0700232 }
233 }
Colin Cross6326d1b2016-09-06 10:24:28 -0700234 }
Colin Cross84b69332017-11-01 14:23:17 -0700235
236 p := &props{}
237 if envTrue(ctx, "HOST_PREFER_32_BIT") {
238 p.Target.Host.Compile_multilib = proptools.StringPtr("prefer32")
239 }
240
241 ctx.AppendProperties(p)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700242}
243
Colin Cross6e95dd52016-09-12 15:37:10 -0700244func testMap(config android.Config) map[string][]string {
245 return config.Once("artTests", func() interface{} {
246 return make(map[string][]string)
247 }).(map[string][]string)
248}
249
250func testInstall(ctx android.InstallHookContext) {
251 testMap := testMap(ctx.AConfig())
252
253 var name string
254 if ctx.Host() {
255 name = "host_"
256 } else {
257 name = "device_"
258 }
259 name += ctx.Arch().ArchType.String() + "_" + ctx.ModuleName()
260
261 artTestMutex.Lock()
262 defer artTestMutex.Unlock()
263
264 tests := testMap[name]
265 tests = append(tests, ctx.Path().RelPathString())
266 testMap[name] = tests
267}
268
269var artTestMutex sync.Mutex
270
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700271func init() {
Colin Cross96548c92016-10-12 14:26:55 -0700272 android.RegisterModuleType("art_cc_library", artLibrary)
Vladimir Marko74527972016-11-29 15:57:32 +0000273 android.RegisterModuleType("art_cc_static_library", artStaticLibrary)
Colin Cross96548c92016-10-12 14:26:55 -0700274 android.RegisterModuleType("art_cc_binary", artBinary)
275 android.RegisterModuleType("art_cc_test", artTest)
276 android.RegisterModuleType("art_cc_test_library", artTestLibrary)
277 android.RegisterModuleType("art_cc_defaults", artDefaultsFactory)
278 android.RegisterModuleType("art_global_defaults", artGlobalDefaultsFactory)
279 android.RegisterModuleType("art_debug_defaults", artDebugDefaultsFactory)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700280}
281
Colin Crossca06ea32017-06-27 10:38:55 -0700282func artGlobalDefaultsFactory() android.Module {
283 module := artDefaultsFactory()
Colin Cross6e511782016-09-13 13:41:03 -0700284 android.AddLoadHook(module, globalDefaults)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700285
Colin Crossca06ea32017-06-27 10:38:55 -0700286 return module
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700287}
288
Colin Crossca06ea32017-06-27 10:38:55 -0700289func artDebugDefaultsFactory() android.Module {
290 module := artDefaultsFactory()
Colin Crossbe332ed2016-09-21 13:23:53 -0700291 android.AddLoadHook(module, debugDefaults)
292
Colin Crossca06ea32017-06-27 10:38:55 -0700293 return module
Colin Crossbe332ed2016-09-21 13:23:53 -0700294}
295
Colin Crossca06ea32017-06-27 10:38:55 -0700296func artDefaultsFactory() android.Module {
Colin Cross6e511782016-09-13 13:41:03 -0700297 c := &codegenProperties{}
Colin Crossca06ea32017-06-27 10:38:55 -0700298 module := cc.DefaultsFactory(c)
Colin Cross6e95dd52016-09-12 15:37:10 -0700299 android.AddLoadHook(module, func(ctx android.LoadHookContext) { codegen(ctx, c, true) })
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700300
Colin Crossca06ea32017-06-27 10:38:55 -0700301 return module
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700302}
303
Colin Crossca06ea32017-06-27 10:38:55 -0700304func artLibrary() android.Module {
Vladimir Marko74527972016-11-29 15:57:32 +0000305 m, _ := cc.NewLibrary(android.HostAndDeviceSupported)
306 module := m.Init()
307
308 installCodegenCustomizer(module, true)
309
310 return module
311}
312
313func artStaticLibrary() android.Module {
314 m, library := cc.NewLibrary(android.HostAndDeviceSupported)
315 library.BuildOnlyStatic()
316 module := m.Init()
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700317
Colin Crossca06ea32017-06-27 10:38:55 -0700318 installCodegenCustomizer(module, true)
Colin Crossfe6064a2016-08-30 13:49:26 -0700319
Colin Crossca06ea32017-06-27 10:38:55 -0700320 return module
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700321}
322
Colin Crossca06ea32017-06-27 10:38:55 -0700323func artBinary() android.Module {
Colin Cross123989f2016-09-07 14:12:50 -0700324 binary, _ := cc.NewBinary(android.HostAndDeviceSupported)
Colin Crossca06ea32017-06-27 10:38:55 -0700325 module := binary.Init()
Colin Cross123989f2016-09-07 14:12:50 -0700326
Colin Cross6e511782016-09-13 13:41:03 -0700327 android.AddLoadHook(module, customLinker)
328 android.AddLoadHook(module, prefer32Bit)
Colin Crossca06ea32017-06-27 10:38:55 -0700329 return module
Colin Cross123989f2016-09-07 14:12:50 -0700330}
331
Colin Crossca06ea32017-06-27 10:38:55 -0700332func artTest() android.Module {
Colin Crossc7376e02016-09-08 12:52:18 -0700333 test := cc.NewTest(android.HostAndDeviceSupported)
Colin Crossca06ea32017-06-27 10:38:55 -0700334 module := test.Init()
Colin Crossc7376e02016-09-08 12:52:18 -0700335
Colin Crossca06ea32017-06-27 10:38:55 -0700336 installCodegenCustomizer(module, false)
Colin Cross6e95dd52016-09-12 15:37:10 -0700337
Colin Cross6e511782016-09-13 13:41:03 -0700338 android.AddLoadHook(module, customLinker)
339 android.AddLoadHook(module, prefer32Bit)
Colin Cross6e95dd52016-09-12 15:37:10 -0700340 android.AddInstallHook(module, testInstall)
Colin Crossca06ea32017-06-27 10:38:55 -0700341 return module
Colin Crossc7376e02016-09-08 12:52:18 -0700342}
343
Colin Crossca06ea32017-06-27 10:38:55 -0700344func artTestLibrary() android.Module {
Colin Crossafd3c9e2016-09-16 13:47:21 -0700345 test := cc.NewTestLibrary(android.HostAndDeviceSupported)
Colin Crossca06ea32017-06-27 10:38:55 -0700346 module := test.Init()
Colin Crossafd3c9e2016-09-16 13:47:21 -0700347
Colin Crossca06ea32017-06-27 10:38:55 -0700348 installCodegenCustomizer(module, false)
Colin Crossafd3c9e2016-09-16 13:47:21 -0700349
350 android.AddLoadHook(module, prefer32Bit)
351 android.AddInstallHook(module, testInstall)
Colin Crossca06ea32017-06-27 10:38:55 -0700352 return module
Colin Crossafd3c9e2016-09-16 13:47:21 -0700353}
354
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700355func envDefault(ctx android.BaseContext, key string, defaultValue string) string {
356 ret := ctx.AConfig().Getenv(key)
357 if ret == "" {
358 return defaultValue
359 }
360 return ret
361}
362
363func envTrue(ctx android.BaseContext, key string) bool {
364 return ctx.AConfig().Getenv(key) == "true"
365}
David Brazdil7b49e6c2016-09-01 11:06:18 +0100366
367func envFalse(ctx android.BaseContext, key string) bool {
368 return ctx.AConfig().Getenv(key) == "false"
369}