blob: 5704b438342d7ec1e22e15696bca25ce5304bd67 [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
Andreas Gampe655c6fd2017-05-24 21:42:10 -070069 // We need larger stack overflow guards for ASAN, as the compiled code will have
70 // larger frame sizes. For simplicity, just use global not-target-specific cflags.
71 // Note: We increase this for both debug and non-debug, as the overflow gap will
72 // be compiled into managed code. We always preopt (and build core images) with
73 // the debug version. So make the gap consistent (and adjust for the worst).
74 if len(ctx.AConfig().SanitizeDevice()) > 0 || len(ctx.AConfig().SanitizeHost()) > 0 {
75 cflags = append(cflags,
Colin Cross2db58b62017-06-27 10:38:31 -070076 "-DART_STACK_OVERFLOW_GAP_arm=8192",
77 "-DART_STACK_OVERFLOW_GAP_arm64=8192",
78 "-DART_STACK_OVERFLOW_GAP_mips=16384",
79 "-DART_STACK_OVERFLOW_GAP_mips64=16384",
80 "-DART_STACK_OVERFLOW_GAP_x86=16384",
81 "-DART_STACK_OVERFLOW_GAP_x86_64=20480")
Andreas Gampe655c6fd2017-05-24 21:42:10 -070082 } else {
83 cflags = append(cflags,
Colin Cross2db58b62017-06-27 10:38:31 -070084 "-DART_STACK_OVERFLOW_GAP_arm=8192",
85 "-DART_STACK_OVERFLOW_GAP_arm64=8192",
86 "-DART_STACK_OVERFLOW_GAP_mips=16384",
87 "-DART_STACK_OVERFLOW_GAP_mips64=16384",
88 "-DART_STACK_OVERFLOW_GAP_x86=8192",
89 "-DART_STACK_OVERFLOW_GAP_x86_64=8192")
Andreas Gampe655c6fd2017-05-24 21:42:10 -070090 }
Andreas Gampebc9f10c2017-05-19 08:28:06 -070091
Andreas Gampedbf0e0f2017-07-11 08:34:48 -070092 if envTrue(ctx, "ART_ENABLE_ADDRESS_SANITIZER") {
93 // Used to enable full sanitization, i.e., user poisoning, under ASAN.
94 cflags = append(cflags, "-DART_ENABLE_ADDRESS_SANITIZER=1")
95 asflags = append(asflags, "-DART_ENABLE_ADDRESS_SANITIZER=1")
96 }
97
Chris Larsen715f43e2017-10-23 11:00:32 -070098 if envTrue(ctx, "ART_MIPS32_CHECK_ALIGNMENT") {
99 // Enable the use of MIPS32 CHECK_ALIGNMENT macro for debugging purposes
100 asflags = append(asflags, "-DART_MIPS32_CHECK_ALIGNMENT")
101 }
102
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700103 return cflags, asflags
104}
105
Colin Crossbe332ed2016-09-21 13:23:53 -0700106func debugFlags(ctx android.BaseContext) []string {
107 var cflags []string
108
109 opt := envDefault(ctx, "ART_DEBUG_OPT_FLAG", "-O2")
110 cflags = append(cflags, opt)
111
112 return cflags
113}
114
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700115func deviceFlags(ctx android.BaseContext) []string {
116 var cflags []string
117 deviceFrameSizeLimit := 1736
118 if len(ctx.AConfig().SanitizeDevice()) > 0 {
Vishwath Mohan1ecc4fe2016-09-26 09:22:42 -0700119 deviceFrameSizeLimit = 7400
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700120 }
121 cflags = append(cflags,
122 fmt.Sprintf("-Wframe-larger-than=%d", deviceFrameSizeLimit),
123 fmt.Sprintf("-DART_FRAME_SIZE_LIMIT=%d", deviceFrameSizeLimit),
124 )
125
126 cflags = append(cflags, "-DART_BASE_ADDRESS="+ctx.AConfig().LibartImgDeviceBaseAddress())
127 if envTrue(ctx, "ART_TARGET_LINUX") {
128 cflags = append(cflags, "-DART_TARGET_LINUX")
129 } else {
130 cflags = append(cflags, "-DART_TARGET_ANDROID")
131 }
132 minDelta := envDefault(ctx, "LIBART_IMG_TARGET_MIN_BASE_ADDRESS_DELTA", "-0x1000000")
133 maxDelta := envDefault(ctx, "LIBART_IMG_TARGET_MAX_BASE_ADDRESS_DELTA", "0x1000000")
134 cflags = append(cflags, "-DART_BASE_ADDRESS_MIN_DELTA="+minDelta)
135 cflags = append(cflags, "-DART_BASE_ADDRESS_MAX_DELTA="+maxDelta)
136
137 return cflags
138}
139
140func hostFlags(ctx android.BaseContext) []string {
141 var cflags []string
142 hostFrameSizeLimit := 1736
Colin Cross3174b682016-09-19 12:25:31 -0700143 if len(ctx.AConfig().SanitizeHost()) > 0 {
144 // art/test/137-cfi/cfi.cc
145 // error: stack frame size of 1944 bytes in function 'Java_Main_unwindInProcess'
146 hostFrameSizeLimit = 6400
147 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700148 cflags = append(cflags,
149 fmt.Sprintf("-Wframe-larger-than=%d", hostFrameSizeLimit),
150 fmt.Sprintf("-DART_FRAME_SIZE_LIMIT=%d", hostFrameSizeLimit),
151 )
152
153 cflags = append(cflags, "-DART_BASE_ADDRESS="+ctx.AConfig().LibartImgHostBaseAddress())
154 minDelta := envDefault(ctx, "LIBART_IMG_HOST_MIN_BASE_ADDRESS_DELTA", "-0x1000000")
155 maxDelta := envDefault(ctx, "LIBART_IMG_HOST_MAX_BASE_ADDRESS_DELTA", "0x1000000")
156 cflags = append(cflags, "-DART_BASE_ADDRESS_MIN_DELTA="+minDelta)
157 cflags = append(cflags, "-DART_BASE_ADDRESS_MAX_DELTA="+maxDelta)
158
Andreas Gampefcc7d672017-07-19 10:30:22 -0700159 if len(ctx.AConfig().SanitizeHost()) > 0 && !envFalse(ctx, "ART_ENABLE_ADDRESS_SANITIZER") {
160 // We enable full sanitization on the host by default.
161 cflags = append(cflags, "-DART_ENABLE_ADDRESS_SANITIZER=1")
162 }
163
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700164 return cflags
165}
166
Colin Cross6e511782016-09-13 13:41:03 -0700167func globalDefaults(ctx android.LoadHookContext) {
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700168 type props struct {
169 Target struct {
170 Android struct {
171 Cflags []string
172 }
173 Host struct {
174 Cflags []string
175 }
176 }
Colin Cross2db58b62017-06-27 10:38:31 -0700177 Cflags []string
178 Asflags []string
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700179 Sanitize struct {
Colin Cross2db58b62017-06-27 10:38:31 -0700180 Recover []string
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700181 }
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700182 }
183
184 p := &props{}
185 p.Cflags, p.Asflags = globalFlags(ctx)
186 p.Target.Android.Cflags = deviceFlags(ctx)
187 p.Target.Host.Cflags = hostFlags(ctx)
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700188
189 if envTrue(ctx, "ART_DEX_FILE_ACCESS_TRACKING") {
190 p.Cflags = append(p.Cflags, "-DART_DEX_FILE_ACCESS_TRACKING")
Colin Cross2db58b62017-06-27 10:38:31 -0700191 p.Sanitize.Recover = []string{
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700192 "address",
193 }
194 }
195
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700196 ctx.AppendProperties(p)
Colin Crossfe6064a2016-08-30 13:49:26 -0700197}
Colin Cross6326d1b2016-09-06 10:24:28 -0700198
Colin Crossbe332ed2016-09-21 13:23:53 -0700199func debugDefaults(ctx android.LoadHookContext) {
200 type props struct {
201 Cflags []string
202 }
203
204 p := &props{}
205 p.Cflags = debugFlags(ctx)
206 ctx.AppendProperties(p)
207}
208
Colin Cross6e511782016-09-13 13:41:03 -0700209func customLinker(ctx android.LoadHookContext) {
Colin Crossfe6064a2016-08-30 13:49:26 -0700210 linker := envDefault(ctx, "CUSTOM_TARGET_LINKER", "")
Colin Cross84b69332017-11-01 14:23:17 -0700211 type props struct {
212 DynamicLinker string
Colin Crossfe6064a2016-08-30 13:49:26 -0700213 }
Colin Cross84b69332017-11-01 14:23:17 -0700214
215 p := &props{}
216 if linker != "" {
217 p.DynamicLinker = linker
218 }
219
220 ctx.AppendProperties(p)
Colin Crossfe6064a2016-08-30 13:49:26 -0700221}
222
Colin Cross6e511782016-09-13 13:41:03 -0700223func prefer32Bit(ctx android.LoadHookContext) {
Colin Cross84b69332017-11-01 14:23:17 -0700224 type props struct {
225 Target struct {
226 Host struct {
227 Compile_multilib *string
Colin Cross6326d1b2016-09-06 10:24:28 -0700228 }
229 }
Colin Cross6326d1b2016-09-06 10:24:28 -0700230 }
Colin Cross84b69332017-11-01 14:23:17 -0700231
232 p := &props{}
233 if envTrue(ctx, "HOST_PREFER_32_BIT") {
234 p.Target.Host.Compile_multilib = proptools.StringPtr("prefer32")
235 }
236
237 ctx.AppendProperties(p)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700238}
239
Colin Cross6e95dd52016-09-12 15:37:10 -0700240func testMap(config android.Config) map[string][]string {
241 return config.Once("artTests", func() interface{} {
242 return make(map[string][]string)
243 }).(map[string][]string)
244}
245
246func testInstall(ctx android.InstallHookContext) {
247 testMap := testMap(ctx.AConfig())
248
249 var name string
250 if ctx.Host() {
251 name = "host_"
252 } else {
253 name = "device_"
254 }
255 name += ctx.Arch().ArchType.String() + "_" + ctx.ModuleName()
256
257 artTestMutex.Lock()
258 defer artTestMutex.Unlock()
259
260 tests := testMap[name]
261 tests = append(tests, ctx.Path().RelPathString())
262 testMap[name] = tests
263}
264
265var artTestMutex sync.Mutex
266
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700267func init() {
Colin Cross96548c92016-10-12 14:26:55 -0700268 android.RegisterModuleType("art_cc_library", artLibrary)
Vladimir Marko74527972016-11-29 15:57:32 +0000269 android.RegisterModuleType("art_cc_static_library", artStaticLibrary)
Colin Cross96548c92016-10-12 14:26:55 -0700270 android.RegisterModuleType("art_cc_binary", artBinary)
271 android.RegisterModuleType("art_cc_test", artTest)
272 android.RegisterModuleType("art_cc_test_library", artTestLibrary)
273 android.RegisterModuleType("art_cc_defaults", artDefaultsFactory)
274 android.RegisterModuleType("art_global_defaults", artGlobalDefaultsFactory)
275 android.RegisterModuleType("art_debug_defaults", artDebugDefaultsFactory)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700276}
277
Colin Crossca06ea32017-06-27 10:38:55 -0700278func artGlobalDefaultsFactory() android.Module {
279 module := artDefaultsFactory()
Colin Cross6e511782016-09-13 13:41:03 -0700280 android.AddLoadHook(module, globalDefaults)
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700281
Colin Crossca06ea32017-06-27 10:38:55 -0700282 return module
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700283}
284
Colin Crossca06ea32017-06-27 10:38:55 -0700285func artDebugDefaultsFactory() android.Module {
286 module := artDefaultsFactory()
Colin Crossbe332ed2016-09-21 13:23:53 -0700287 android.AddLoadHook(module, debugDefaults)
288
Colin Crossca06ea32017-06-27 10:38:55 -0700289 return module
Colin Crossbe332ed2016-09-21 13:23:53 -0700290}
291
Colin Crossca06ea32017-06-27 10:38:55 -0700292func artDefaultsFactory() android.Module {
Colin Cross6e511782016-09-13 13:41:03 -0700293 c := &codegenProperties{}
Colin Crossca06ea32017-06-27 10:38:55 -0700294 module := cc.DefaultsFactory(c)
Colin Cross6e95dd52016-09-12 15:37:10 -0700295 android.AddLoadHook(module, func(ctx android.LoadHookContext) { codegen(ctx, c, true) })
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700296
Colin Crossca06ea32017-06-27 10:38:55 -0700297 return module
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700298}
299
Colin Crossca06ea32017-06-27 10:38:55 -0700300func artLibrary() android.Module {
Vladimir Marko74527972016-11-29 15:57:32 +0000301 m, _ := cc.NewLibrary(android.HostAndDeviceSupported)
302 module := m.Init()
303
304 installCodegenCustomizer(module, true)
305
306 return module
307}
308
309func artStaticLibrary() android.Module {
310 m, library := cc.NewLibrary(android.HostAndDeviceSupported)
311 library.BuildOnlyStatic()
312 module := m.Init()
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700313
Colin Crossca06ea32017-06-27 10:38:55 -0700314 installCodegenCustomizer(module, true)
Colin Crossfe6064a2016-08-30 13:49:26 -0700315
Colin Crossca06ea32017-06-27 10:38:55 -0700316 return module
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700317}
318
Colin Crossca06ea32017-06-27 10:38:55 -0700319func artBinary() android.Module {
Colin Cross123989f2016-09-07 14:12:50 -0700320 binary, _ := cc.NewBinary(android.HostAndDeviceSupported)
Colin Crossca06ea32017-06-27 10:38:55 -0700321 module := binary.Init()
Colin Cross123989f2016-09-07 14:12:50 -0700322
Colin Cross6e511782016-09-13 13:41:03 -0700323 android.AddLoadHook(module, customLinker)
324 android.AddLoadHook(module, prefer32Bit)
Colin Crossca06ea32017-06-27 10:38:55 -0700325 return module
Colin Cross123989f2016-09-07 14:12:50 -0700326}
327
Colin Crossca06ea32017-06-27 10:38:55 -0700328func artTest() android.Module {
Colin Crossc7376e02016-09-08 12:52:18 -0700329 test := cc.NewTest(android.HostAndDeviceSupported)
Colin Crossca06ea32017-06-27 10:38:55 -0700330 module := test.Init()
Colin Crossc7376e02016-09-08 12:52:18 -0700331
Colin Crossca06ea32017-06-27 10:38:55 -0700332 installCodegenCustomizer(module, false)
Colin Cross6e95dd52016-09-12 15:37:10 -0700333
Colin Cross6e511782016-09-13 13:41:03 -0700334 android.AddLoadHook(module, customLinker)
335 android.AddLoadHook(module, prefer32Bit)
Colin Cross6e95dd52016-09-12 15:37:10 -0700336 android.AddInstallHook(module, testInstall)
Colin Crossca06ea32017-06-27 10:38:55 -0700337 return module
Colin Crossc7376e02016-09-08 12:52:18 -0700338}
339
Colin Crossca06ea32017-06-27 10:38:55 -0700340func artTestLibrary() android.Module {
Colin Crossafd3c9e2016-09-16 13:47:21 -0700341 test := cc.NewTestLibrary(android.HostAndDeviceSupported)
Colin Crossca06ea32017-06-27 10:38:55 -0700342 module := test.Init()
Colin Crossafd3c9e2016-09-16 13:47:21 -0700343
Colin Crossca06ea32017-06-27 10:38:55 -0700344 installCodegenCustomizer(module, false)
Colin Crossafd3c9e2016-09-16 13:47:21 -0700345
346 android.AddLoadHook(module, prefer32Bit)
347 android.AddInstallHook(module, testInstall)
Colin Crossca06ea32017-06-27 10:38:55 -0700348 return module
Colin Crossafd3c9e2016-09-16 13:47:21 -0700349}
350
Colin Cross1f7f3bd2016-07-27 10:12:38 -0700351func envDefault(ctx android.BaseContext, key string, defaultValue string) string {
352 ret := ctx.AConfig().Getenv(key)
353 if ret == "" {
354 return defaultValue
355 }
356 return ret
357}
358
359func envTrue(ctx android.BaseContext, key string) bool {
360 return ctx.AConfig().Getenv(key) == "true"
361}
David Brazdil7b49e6c2016-09-01 11:06:18 +0100362
363func envFalse(ctx android.BaseContext, key string) bool {
364 return ctx.AConfig().Getenv(key) == "false"
365}