blob: 3dabce39759c69b7a54272f93be7668b7eaf7512 [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
49 imtSize := envDefault(ctx, "ART_IMT_SIZE", "43")
50 cflags = append(cflags, "-DIMT_SIZE="+imtSize)
51
52 if envTrue(ctx, "ART_HEAP_POISONING") {
53 cflags = append(cflags, "-DART_HEAP_POISONING=1")
54 asflags = append(asflags, "-DART_HEAP_POISONING=1")
55 }
56
Hiroshi Yamauchida75ad72017-01-24 11:06:47 -080057 if !envFalse(ctx, "ART_USE_READ_BARRIER") && ctx.AConfig().ArtUseReadBarrier() {
Roland Levillainb81e9e92017-04-20 17:35:32 +010058 // Used to change the read barrier type. Valid values are BAKER, BROOKS,
59 // TABLELOOKUP. The default is BAKER.
Colin Cross1f7f3bd2016-07-27 10:12:38 -070060 barrierType := envDefault(ctx, "ART_READ_BARRIER_TYPE", "BAKER")
61 cflags = append(cflags,
62 "-DART_USE_READ_BARRIER=1",
63 "-DART_READ_BARRIER_TYPE_IS_"+barrierType+"=1")
64 asflags = append(asflags,
65 "-DART_USE_READ_BARRIER=1",
66 "-DART_READ_BARRIER_TYPE_IS_"+barrierType+"=1")
Colin Cross1f7f3bd2016-07-27 10:12:38 -070067 }
68
Mathieu Chartierecb86622018-01-04 15:30:45 -080069 cdexLevel := envDefault(ctx, "ART_DEFAULT_COMPACT_DEX_LEVEL", "fast")
Mathieu Chartiera7f6b812017-12-11 13:34:29 -080070 cflags = append(cflags, "-DART_DEFAULT_COMPACT_DEX_LEVEL="+cdexLevel)
71
Andreas Gampe655c6fd2017-05-24 21:42:10 -070072 // We need larger stack overflow guards for ASAN, as the compiled code will have
73 // larger frame sizes. For simplicity, just use global not-target-specific cflags.
74 // Note: We increase this for both debug and non-debug, as the overflow gap will
75 // be compiled into managed code. We always preopt (and build core images) with
76 // the debug version. So make the gap consistent (and adjust for the worst).
77 if len(ctx.AConfig().SanitizeDevice()) > 0 || len(ctx.AConfig().SanitizeHost()) > 0 {
78 cflags = append(cflags,
Colin Cross2db58b62017-06-27 10:38:31 -070079 "-DART_STACK_OVERFLOW_GAP_arm=8192",
80 "-DART_STACK_OVERFLOW_GAP_arm64=8192",
81 "-DART_STACK_OVERFLOW_GAP_mips=16384",
82 "-DART_STACK_OVERFLOW_GAP_mips64=16384",
83 "-DART_STACK_OVERFLOW_GAP_x86=16384",
84 "-DART_STACK_OVERFLOW_GAP_x86_64=20480")
Andreas Gampe655c6fd2017-05-24 21:42:10 -070085 } else {
86 cflags = append(cflags,
Colin Cross2db58b62017-06-27 10:38:31 -070087 "-DART_STACK_OVERFLOW_GAP_arm=8192",
88 "-DART_STACK_OVERFLOW_GAP_arm64=8192",
89 "-DART_STACK_OVERFLOW_GAP_mips=16384",
90 "-DART_STACK_OVERFLOW_GAP_mips64=16384",
91 "-DART_STACK_OVERFLOW_GAP_x86=8192",
92 "-DART_STACK_OVERFLOW_GAP_x86_64=8192")
Andreas Gampe655c6fd2017-05-24 21:42:10 -070093 }
Andreas Gampebc9f10c2017-05-19 08:28:06 -070094
Andreas Gampedbf0e0f2017-07-11 08:34:48 -070095 if envTrue(ctx, "ART_ENABLE_ADDRESS_SANITIZER") {
96 // Used to enable full sanitization, i.e., user poisoning, under ASAN.
97 cflags = append(cflags, "-DART_ENABLE_ADDRESS_SANITIZER=1")
98 asflags = append(asflags, "-DART_ENABLE_ADDRESS_SANITIZER=1")
99 }
100
Chris Larsen715f43e2017-10-23 11:00:32 -0700101 if envTrue(ctx, "ART_MIPS32_CHECK_ALIGNMENT") {
102 // Enable the use of MIPS32 CHECK_ALIGNMENT macro for debugging purposes
103 asflags = append(asflags, "-DART_MIPS32_CHECK_ALIGNMENT")
104 }
105
Alan Leung50173eb2017-12-20 13:33:21 -0800106 if envTrueOrDefault(ctx, "USE_D8_DESUGAR") {
Alan Leung8f514ee2017-12-08 14:08:25 -0800107 cflags = append(cflags, "-DUSE_D8_DESUGAR=1")
108 }
109
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700110 return cflags, asflags
111}
112
Colin Crossbe332ed2016-09-21 13:23:53 -0700113func debugFlags(ctx android.BaseContext) []string {
114 var cflags []string
115
116 opt := envDefault(ctx, "ART_DEBUG_OPT_FLAG", "-O2")
117 cflags = append(cflags, opt)
118
119 return cflags
120}
121
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700122func deviceFlags(ctx android.BaseContext) []string {
123 var cflags []string
124 deviceFrameSizeLimit := 1736
125 if len(ctx.AConfig().SanitizeDevice()) > 0 {
Vishwath Mohan1ecc4fe2016-09-26 09:22:42 -0700126 deviceFrameSizeLimit = 7400
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700127 }
128 cflags = append(cflags,
129 fmt.Sprintf("-Wframe-larger-than=%d", deviceFrameSizeLimit),
130 fmt.Sprintf("-DART_FRAME_SIZE_LIMIT=%d", deviceFrameSizeLimit),
131 )
132
133 cflags = append(cflags, "-DART_BASE_ADDRESS="+ctx.AConfig().LibartImgDeviceBaseAddress())
134 if envTrue(ctx, "ART_TARGET_LINUX") {
135 cflags = append(cflags, "-DART_TARGET_LINUX")
136 } else {
137 cflags = append(cflags, "-DART_TARGET_ANDROID")
138 }
139 minDelta := envDefault(ctx, "LIBART_IMG_TARGET_MIN_BASE_ADDRESS_DELTA", "-0x1000000")
140 maxDelta := envDefault(ctx, "LIBART_IMG_TARGET_MAX_BASE_ADDRESS_DELTA", "0x1000000")
141 cflags = append(cflags, "-DART_BASE_ADDRESS_MIN_DELTA="+minDelta)
142 cflags = append(cflags, "-DART_BASE_ADDRESS_MAX_DELTA="+maxDelta)
143
144 return cflags
145}
146
147func hostFlags(ctx android.BaseContext) []string {
148 var cflags []string
149 hostFrameSizeLimit := 1736
Colin Cross3174b682016-09-19 12:25:31 -0700150 if len(ctx.AConfig().SanitizeHost()) > 0 {
151 // art/test/137-cfi/cfi.cc
152 // error: stack frame size of 1944 bytes in function 'Java_Main_unwindInProcess'
153 hostFrameSizeLimit = 6400
154 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700155 cflags = append(cflags,
156 fmt.Sprintf("-Wframe-larger-than=%d", hostFrameSizeLimit),
157 fmt.Sprintf("-DART_FRAME_SIZE_LIMIT=%d", hostFrameSizeLimit),
158 )
159
160 cflags = append(cflags, "-DART_BASE_ADDRESS="+ctx.AConfig().LibartImgHostBaseAddress())
161 minDelta := envDefault(ctx, "LIBART_IMG_HOST_MIN_BASE_ADDRESS_DELTA", "-0x1000000")
162 maxDelta := envDefault(ctx, "LIBART_IMG_HOST_MAX_BASE_ADDRESS_DELTA", "0x1000000")
163 cflags = append(cflags, "-DART_BASE_ADDRESS_MIN_DELTA="+minDelta)
164 cflags = append(cflags, "-DART_BASE_ADDRESS_MAX_DELTA="+maxDelta)
165
Andreas Gampefcc7d672017-07-19 10:30:22 -0700166 if len(ctx.AConfig().SanitizeHost()) > 0 && !envFalse(ctx, "ART_ENABLE_ADDRESS_SANITIZER") {
167 // We enable full sanitization on the host by default.
168 cflags = append(cflags, "-DART_ENABLE_ADDRESS_SANITIZER=1")
169 }
170
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700171 return cflags
172}
173
Colin Cross6e511782016-09-13 13:41:03 -0700174func globalDefaults(ctx android.LoadHookContext) {
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700175 type props struct {
176 Target struct {
177 Android struct {
178 Cflags []string
179 }
180 Host struct {
181 Cflags []string
182 }
183 }
Colin Cross2db58b62017-06-27 10:38:31 -0700184 Cflags []string
185 Asflags []string
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700186 Sanitize struct {
Colin Cross2db58b62017-06-27 10:38:31 -0700187 Recover []string
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700188 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700189 }
190
191 p := &props{}
192 p.Cflags, p.Asflags = globalFlags(ctx)
193 p.Target.Android.Cflags = deviceFlags(ctx)
194 p.Target.Host.Cflags = hostFlags(ctx)
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700195
196 if envTrue(ctx, "ART_DEX_FILE_ACCESS_TRACKING") {
197 p.Cflags = append(p.Cflags, "-DART_DEX_FILE_ACCESS_TRACKING")
Colin Cross2db58b62017-06-27 10:38:31 -0700198 p.Sanitize.Recover = []string{
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700199 "address",
200 }
201 }
202
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700203 ctx.AppendProperties(p)
Colin Crossfe6064a2016-08-30 13:49:26 -0700204}
Colin Cross6326d1b2016-09-06 10:24:28 -0700205
Colin Crossbe332ed2016-09-21 13:23:53 -0700206func debugDefaults(ctx android.LoadHookContext) {
207 type props struct {
208 Cflags []string
209 }
210
211 p := &props{}
212 p.Cflags = debugFlags(ctx)
213 ctx.AppendProperties(p)
214}
215
Colin Cross6e511782016-09-13 13:41:03 -0700216func customLinker(ctx android.LoadHookContext) {
Colin Crossfe6064a2016-08-30 13:49:26 -0700217 linker := envDefault(ctx, "CUSTOM_TARGET_LINKER", "")
Colin Cross84b69332017-11-01 14:23:17 -0700218 type props struct {
219 DynamicLinker string
Colin Crossfe6064a2016-08-30 13:49:26 -0700220 }
Colin Cross84b69332017-11-01 14:23:17 -0700221
222 p := &props{}
223 if linker != "" {
224 p.DynamicLinker = linker
225 }
226
227 ctx.AppendProperties(p)
Colin Crossfe6064a2016-08-30 13:49:26 -0700228}
229
Colin Cross6e511782016-09-13 13:41:03 -0700230func prefer32Bit(ctx android.LoadHookContext) {
Colin Cross84b69332017-11-01 14:23:17 -0700231 type props struct {
232 Target struct {
233 Host struct {
234 Compile_multilib *string
Colin Cross6326d1b2016-09-06 10:24:28 -0700235 }
236 }
Colin Cross6326d1b2016-09-06 10:24:28 -0700237 }
Colin Cross84b69332017-11-01 14:23:17 -0700238
239 p := &props{}
240 if envTrue(ctx, "HOST_PREFER_32_BIT") {
241 p.Target.Host.Compile_multilib = proptools.StringPtr("prefer32")
242 }
243
244 ctx.AppendProperties(p)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700245}
246
Colin Cross6e95dd52016-09-12 15:37:10 -0700247func testMap(config android.Config) map[string][]string {
248 return config.Once("artTests", func() interface{} {
249 return make(map[string][]string)
250 }).(map[string][]string)
251}
252
253func testInstall(ctx android.InstallHookContext) {
254 testMap := testMap(ctx.AConfig())
255
256 var name string
257 if ctx.Host() {
258 name = "host_"
259 } else {
260 name = "device_"
261 }
262 name += ctx.Arch().ArchType.String() + "_" + ctx.ModuleName()
263
264 artTestMutex.Lock()
265 defer artTestMutex.Unlock()
266
267 tests := testMap[name]
268 tests = append(tests, ctx.Path().RelPathString())
269 testMap[name] = tests
270}
271
272var artTestMutex sync.Mutex
273
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700274func init() {
Colin Cross96548c92016-10-12 14:26:55 -0700275 android.RegisterModuleType("art_cc_library", artLibrary)
Vladimir Marko74527972016-11-29 15:57:32 +0000276 android.RegisterModuleType("art_cc_static_library", artStaticLibrary)
Colin Cross96548c92016-10-12 14:26:55 -0700277 android.RegisterModuleType("art_cc_binary", artBinary)
278 android.RegisterModuleType("art_cc_test", artTest)
279 android.RegisterModuleType("art_cc_test_library", artTestLibrary)
280 android.RegisterModuleType("art_cc_defaults", artDefaultsFactory)
Nicolas Geoffray8a229072018-05-10 16:34:14 +0100281 android.RegisterModuleType("libart_cc_defaults", libartDefaultsFactory)
Colin Cross96548c92016-10-12 14:26:55 -0700282 android.RegisterModuleType("art_global_defaults", artGlobalDefaultsFactory)
283 android.RegisterModuleType("art_debug_defaults", artDebugDefaultsFactory)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700284}
285
Colin Crossca06ea32017-06-27 10:38:55 -0700286func artGlobalDefaultsFactory() android.Module {
287 module := artDefaultsFactory()
Colin Cross6e511782016-09-13 13:41:03 -0700288 android.AddLoadHook(module, globalDefaults)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700289
Colin Crossca06ea32017-06-27 10:38:55 -0700290 return module
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700291}
292
Colin Crossca06ea32017-06-27 10:38:55 -0700293func artDebugDefaultsFactory() android.Module {
294 module := artDefaultsFactory()
Colin Crossbe332ed2016-09-21 13:23:53 -0700295 android.AddLoadHook(module, debugDefaults)
296
Colin Crossca06ea32017-06-27 10:38:55 -0700297 return module
Colin Crossbe332ed2016-09-21 13:23:53 -0700298}
299
Colin Crossca06ea32017-06-27 10:38:55 -0700300func artDefaultsFactory() android.Module {
Colin Cross6e511782016-09-13 13:41:03 -0700301 c := &codegenProperties{}
Colin Crossca06ea32017-06-27 10:38:55 -0700302 module := cc.DefaultsFactory(c)
Colin Cross6e95dd52016-09-12 15:37:10 -0700303 android.AddLoadHook(module, func(ctx android.LoadHookContext) { codegen(ctx, c, true) })
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700304
Colin Crossca06ea32017-06-27 10:38:55 -0700305 return module
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700306}
307
Nicolas Geoffray8a229072018-05-10 16:34:14 +0100308func libartDefaultsFactory() android.Module {
309 c := &codegenProperties{}
310 module := cc.DefaultsFactory(c)
311 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
312 codegen(ctx, c, true)
313
314 type props struct {
315 Target struct {
316 Android struct {
317 Shared_libs []string
318 }
319 }
320 }
321
322 p := &props{}
323 // TODO: express this in .bp instead b/79671158
324 if !envTrue(ctx, "ART_TARGET_LINUX") {
325 p.Target.Android.Shared_libs = []string {
326 "libmetricslogger",
327 }
328 }
329 ctx.AppendProperties(p)
330 })
331
332 return module
333}
334
Colin Crossca06ea32017-06-27 10:38:55 -0700335func artLibrary() android.Module {
Vladimir Marko74527972016-11-29 15:57:32 +0000336 m, _ := cc.NewLibrary(android.HostAndDeviceSupported)
337 module := m.Init()
338
339 installCodegenCustomizer(module, true)
340
341 return module
342}
343
344func artStaticLibrary() android.Module {
345 m, library := cc.NewLibrary(android.HostAndDeviceSupported)
346 library.BuildOnlyStatic()
347 module := m.Init()
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700348
Colin Crossca06ea32017-06-27 10:38:55 -0700349 installCodegenCustomizer(module, true)
Colin Crossfe6064a2016-08-30 13:49:26 -0700350
Colin Crossca06ea32017-06-27 10:38:55 -0700351 return module
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700352}
353
Colin Crossca06ea32017-06-27 10:38:55 -0700354func artBinary() android.Module {
Colin Cross123989f2016-09-07 14:12:50 -0700355 binary, _ := cc.NewBinary(android.HostAndDeviceSupported)
Colin Crossca06ea32017-06-27 10:38:55 -0700356 module := binary.Init()
Colin Cross123989f2016-09-07 14:12:50 -0700357
Colin Cross6e511782016-09-13 13:41:03 -0700358 android.AddLoadHook(module, customLinker)
359 android.AddLoadHook(module, prefer32Bit)
Colin Crossca06ea32017-06-27 10:38:55 -0700360 return module
Colin Cross123989f2016-09-07 14:12:50 -0700361}
362
Colin Crossca06ea32017-06-27 10:38:55 -0700363func artTest() android.Module {
Colin Crossc7376e02016-09-08 12:52:18 -0700364 test := cc.NewTest(android.HostAndDeviceSupported)
Colin Crossca06ea32017-06-27 10:38:55 -0700365 module := test.Init()
Colin Crossc7376e02016-09-08 12:52:18 -0700366
Colin Crossca06ea32017-06-27 10:38:55 -0700367 installCodegenCustomizer(module, false)
Colin Cross6e95dd52016-09-12 15:37:10 -0700368
Colin Cross6e511782016-09-13 13:41:03 -0700369 android.AddLoadHook(module, customLinker)
370 android.AddLoadHook(module, prefer32Bit)
Colin Cross6e95dd52016-09-12 15:37:10 -0700371 android.AddInstallHook(module, testInstall)
Colin Crossca06ea32017-06-27 10:38:55 -0700372 return module
Colin Crossc7376e02016-09-08 12:52:18 -0700373}
374
Colin Crossca06ea32017-06-27 10:38:55 -0700375func artTestLibrary() android.Module {
Colin Crossafd3c9e2016-09-16 13:47:21 -0700376 test := cc.NewTestLibrary(android.HostAndDeviceSupported)
Colin Crossca06ea32017-06-27 10:38:55 -0700377 module := test.Init()
Colin Crossafd3c9e2016-09-16 13:47:21 -0700378
Colin Crossca06ea32017-06-27 10:38:55 -0700379 installCodegenCustomizer(module, false)
Colin Crossafd3c9e2016-09-16 13:47:21 -0700380
381 android.AddLoadHook(module, prefer32Bit)
382 android.AddInstallHook(module, testInstall)
Colin Crossca06ea32017-06-27 10:38:55 -0700383 return module
Colin Crossafd3c9e2016-09-16 13:47:21 -0700384}
385
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700386func envDefault(ctx android.BaseContext, key string, defaultValue string) string {
387 ret := ctx.AConfig().Getenv(key)
388 if ret == "" {
389 return defaultValue
390 }
391 return ret
392}
393
394func envTrue(ctx android.BaseContext, key string) bool {
395 return ctx.AConfig().Getenv(key) == "true"
396}
David Brazdil7b49e6c2016-09-01 11:06:18 +0100397
398func envFalse(ctx android.BaseContext, key string) bool {
399 return ctx.AConfig().Getenv(key) == "false"
400}
Alan Leung50173eb2017-12-20 13:33:21 -0800401
402func envTrueOrDefault(ctx android.BaseContext, key string) bool {
403 return ctx.AConfig().Getenv(key) != "false"
404}