lin
2025-07-31 065ea569db06206874bbfa18eb25ff6121aec09b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
 
package art
 
import (
   "android/soong/android"
   "android/soong/apex"
   "android/soong/cc"
   "fmt"
   "log"
   "sync"
 
   "github.com/google/blueprint/proptools"
)
 
var supportedArches = []string{"arm", "arm64", "mips", "mips64", "x86", "x86_64"}
 
func globalFlags(ctx android.BaseContext) ([]string, []string) {
   var cflags []string
   var asflags []string
 
   opt := envDefault(ctx, "ART_NDEBUG_OPT_FLAG", "-O3")
   cflags = append(cflags, opt)
 
   tlab := false
 
   gcType := envDefault(ctx, "ART_DEFAULT_GC_TYPE", "CMS")
 
   if envTrue(ctx, "ART_TEST_DEBUG_GC") {
       gcType = "SS"
       tlab = true
   }
 
   cflags = append(cflags, "-DART_DEFAULT_GC_TYPE_IS_"+gcType)
   if tlab {
       cflags = append(cflags, "-DART_USE_TLAB=1")
   }
 
   imtSize := envDefault(ctx, "ART_IMT_SIZE", "43")
   cflags = append(cflags, "-DIMT_SIZE="+imtSize)
 
   if envTrue(ctx, "ART_HEAP_POISONING") {
       cflags = append(cflags, "-DART_HEAP_POISONING=1")
       asflags = append(asflags, "-DART_HEAP_POISONING=1")
   }
   if envTrue(ctx, "ART_USE_CXX_INTERPRETER") {
       cflags = append(cflags, "-DART_USE_CXX_INTERPRETER=1")
   }
 
   if !envFalse(ctx, "ART_USE_READ_BARRIER") && ctx.AConfig().ArtUseReadBarrier() {
       // Used to change the read barrier type. Valid values are BAKER, BROOKS,
       // TABLELOOKUP. The default is BAKER.
       barrierType := envDefault(ctx, "ART_READ_BARRIER_TYPE", "BAKER")
       cflags = append(cflags,
           "-DART_USE_READ_BARRIER=1",
           "-DART_READ_BARRIER_TYPE_IS_"+barrierType+"=1")
       asflags = append(asflags,
           "-DART_USE_READ_BARRIER=1",
           "-DART_READ_BARRIER_TYPE_IS_"+barrierType+"=1")
   }
 
   if !envFalse(ctx, "ART_USE_GENERATIONAL_CC") {
       cflags = append(cflags, "-DART_USE_GENERATIONAL_CC=1")
   }
 
   cdexLevel := envDefault(ctx, "ART_DEFAULT_COMPACT_DEX_LEVEL", "fast")
   cflags = append(cflags, "-DART_DEFAULT_COMPACT_DEX_LEVEL="+cdexLevel)
 
   // We need larger stack overflow guards for ASAN, as the compiled code will have
   // larger frame sizes. For simplicity, just use global not-target-specific cflags.
   // Note: We increase this for both debug and non-debug, as the overflow gap will
   //       be compiled into managed code. We always preopt (and build core images) with
   //       the debug version. So make the gap consistent (and adjust for the worst).
   if len(ctx.AConfig().SanitizeDevice()) > 0 || len(ctx.AConfig().SanitizeHost()) > 0 {
       cflags = append(cflags,
           "-DART_STACK_OVERFLOW_GAP_arm=8192",
           "-DART_STACK_OVERFLOW_GAP_arm64=8192",
           "-DART_STACK_OVERFLOW_GAP_mips=16384",
           "-DART_STACK_OVERFLOW_GAP_mips64=16384",
           "-DART_STACK_OVERFLOW_GAP_x86=16384",
           "-DART_STACK_OVERFLOW_GAP_x86_64=20480")
   } else {
       cflags = append(cflags,
           "-DART_STACK_OVERFLOW_GAP_arm=8192",
           "-DART_STACK_OVERFLOW_GAP_arm64=8192",
           "-DART_STACK_OVERFLOW_GAP_mips=16384",
           "-DART_STACK_OVERFLOW_GAP_mips64=16384",
           "-DART_STACK_OVERFLOW_GAP_x86=8192",
           "-DART_STACK_OVERFLOW_GAP_x86_64=8192")
   }
 
   if envTrue(ctx, "ART_ENABLE_ADDRESS_SANITIZER") {
       // Used to enable full sanitization, i.e., user poisoning, under ASAN.
       cflags = append(cflags, "-DART_ENABLE_ADDRESS_SANITIZER=1")
       asflags = append(asflags, "-DART_ENABLE_ADDRESS_SANITIZER=1")
   }
 
   if envTrue(ctx, "ART_MIPS32_CHECK_ALIGNMENT") {
       // Enable the use of MIPS32 CHECK_ALIGNMENT macro for debugging purposes
       asflags = append(asflags, "-DART_MIPS32_CHECK_ALIGNMENT")
   }
 
   if envTrueOrDefault(ctx, "USE_D8_DESUGAR") {
       cflags = append(cflags, "-DUSE_D8_DESUGAR=1")
   }
 
   return cflags, asflags
}
 
func debugFlags(ctx android.BaseContext) []string {
   var cflags []string
 
   opt := envDefault(ctx, "ART_DEBUG_OPT_FLAG", "-O2")
   cflags = append(cflags, opt)
 
   return cflags
}
 
func deviceFlags(ctx android.BaseContext) []string {
   var cflags []string
   deviceFrameSizeLimit := 1736
   if len(ctx.AConfig().SanitizeDevice()) > 0 {
       deviceFrameSizeLimit = 7400
   }
   cflags = append(cflags,
       fmt.Sprintf("-Wframe-larger-than=%d", deviceFrameSizeLimit),
       fmt.Sprintf("-DART_FRAME_SIZE_LIMIT=%d", deviceFrameSizeLimit),
   )
 
   cflags = append(cflags, "-DART_BASE_ADDRESS="+ctx.AConfig().LibartImgDeviceBaseAddress())
   if envTrue(ctx, "ART_TARGET_LINUX") {
       cflags = append(cflags, "-DART_TARGET_LINUX")
   } else {
       cflags = append(cflags, "-DART_TARGET_ANDROID")
   }
   minDelta := envDefault(ctx, "LIBART_IMG_TARGET_MIN_BASE_ADDRESS_DELTA", "-0x1000000")
   maxDelta := envDefault(ctx, "LIBART_IMG_TARGET_MAX_BASE_ADDRESS_DELTA", "0x1000000")
   cflags = append(cflags, "-DART_BASE_ADDRESS_MIN_DELTA="+minDelta)
   cflags = append(cflags, "-DART_BASE_ADDRESS_MAX_DELTA="+maxDelta)
 
   return cflags
}
 
func hostFlags(ctx android.BaseContext) []string {
   var cflags []string
   hostFrameSizeLimit := 1736
   if len(ctx.AConfig().SanitizeHost()) > 0 {
       // art/test/137-cfi/cfi.cc
       // error: stack frame size of 1944 bytes in function 'Java_Main_unwindInProcess'
       hostFrameSizeLimit = 6400
   }
   cflags = append(cflags,
       fmt.Sprintf("-Wframe-larger-than=%d", hostFrameSizeLimit),
       fmt.Sprintf("-DART_FRAME_SIZE_LIMIT=%d", hostFrameSizeLimit),
   )
 
   cflags = append(cflags, "-DART_BASE_ADDRESS="+ctx.AConfig().LibartImgHostBaseAddress())
   minDelta := envDefault(ctx, "LIBART_IMG_HOST_MIN_BASE_ADDRESS_DELTA", "-0x1000000")
   maxDelta := envDefault(ctx, "LIBART_IMG_HOST_MAX_BASE_ADDRESS_DELTA", "0x1000000")
   cflags = append(cflags, "-DART_BASE_ADDRESS_MIN_DELTA="+minDelta)
   cflags = append(cflags, "-DART_BASE_ADDRESS_MAX_DELTA="+maxDelta)
 
   if len(ctx.AConfig().SanitizeHost()) > 0 && !envFalse(ctx, "ART_ENABLE_ADDRESS_SANITIZER") {
       // We enable full sanitization on the host by default.
       cflags = append(cflags, "-DART_ENABLE_ADDRESS_SANITIZER=1")
   }
 
   return cflags
}
 
func globalDefaults(ctx android.LoadHookContext) {
   type props struct {
       Target struct {
           Android struct {
               Cflags []string
           }
           Host struct {
               Cflags []string
           }
       }
       Cflags   []string
       Asflags  []string
       Sanitize struct {
           Recover []string
       }
   }
 
   p := &props{}
   p.Cflags, p.Asflags = globalFlags(ctx)
   p.Target.Android.Cflags = deviceFlags(ctx)
   p.Target.Host.Cflags = hostFlags(ctx)
 
   if envTrue(ctx, "ART_DEX_FILE_ACCESS_TRACKING") {
       p.Cflags = append(p.Cflags, "-DART_DEX_FILE_ACCESS_TRACKING")
       p.Sanitize.Recover = []string{
           "address",
       }
   }
 
   ctx.AppendProperties(p)
}
 
func debugDefaults(ctx android.LoadHookContext) {
   type props struct {
       Cflags []string
   }
 
   p := &props{}
   p.Cflags = debugFlags(ctx)
   ctx.AppendProperties(p)
}
 
func customLinker(ctx android.LoadHookContext) {
   linker := envDefault(ctx, "CUSTOM_TARGET_LINKER", "")
   type props struct {
       DynamicLinker string
   }
 
   p := &props{}
   if linker != "" {
       p.DynamicLinker = linker
   }
 
   ctx.AppendProperties(p)
}
 
func prefer32Bit(ctx android.LoadHookContext) {
   type props struct {
       Target struct {
           Host struct {
               Compile_multilib *string
           }
       }
   }
 
   p := &props{}
   if envTrue(ctx, "HOST_PREFER_32_BIT") {
       p.Target.Host.Compile_multilib = proptools.StringPtr("prefer32")
   }
 
   ctx.AppendProperties(p)
}
 
var testMapKey = android.NewOnceKey("artTests")
 
func testMap(config android.Config) map[string][]string {
   return config.Once(testMapKey, func() interface{} {
       return make(map[string][]string)
   }).(map[string][]string)
}
 
func testInstall(ctx android.InstallHookContext) {
   testMap := testMap(ctx.AConfig())
 
   var name string
   if ctx.Host() {
       name = "host_"
   } else {
       name = "device_"
   }
   name += ctx.Arch().ArchType.String() + "_" + ctx.ModuleName()
 
   artTestMutex.Lock()
   defer artTestMutex.Unlock()
 
   tests := testMap[name]
   tests = append(tests, ctx.Path().RelPathString())
   testMap[name] = tests
}
 
var artTestMutex sync.Mutex
 
func init() {
   android.RegisterModuleType("art_cc_library", artLibrary)
   android.RegisterModuleType("art_cc_library_static", artStaticLibrary)
   android.RegisterModuleType("art_cc_binary", artBinary)
   android.RegisterModuleType("art_cc_test", artTest)
   android.RegisterModuleType("art_cc_test_library", artTestLibrary)
   android.RegisterModuleType("art_cc_defaults", artDefaultsFactory)
   android.RegisterModuleType("libart_cc_defaults", libartDefaultsFactory)
   android.RegisterModuleType("libart_static_cc_defaults", libartStaticDefaultsFactory)
   android.RegisterModuleType("art_global_defaults", artGlobalDefaultsFactory)
   android.RegisterModuleType("art_debug_defaults", artDebugDefaultsFactory)
 
   // TODO: This makes the module disable itself for host if HOST_PREFER_32_BIT is
   // set. We need this because the multilib types of binaries listed in the apex
   // rule must match the declared type. This is normally not difficult but HOST_PREFER_32_BIT
   // changes this to 'prefer32' on all host binaries. Since HOST_PREFER_32_BIT is
   // only used for testing we can just disable the module.
   // See b/120617876 for more information.
   android.RegisterModuleType("art_apex_test", artTestApexBundleFactory)
}
 
func artTestApexBundleFactory() android.Module {
   module := apex.ApexBundleFactory( /*testApex*/ true)
   android.AddLoadHook(module, func(ctx android.LoadHookContext) {
       if envTrue(ctx, "HOST_PREFER_32_BIT") {
           type props struct {
               Target struct {
                   Host struct {
                       Enabled *bool
                   }
               }
           }
 
           p := &props{}
           p.Target.Host.Enabled = proptools.BoolPtr(false)
           ctx.AppendProperties(p)
           log.Print("Disabling host build of " + ctx.ModuleName() + " for HOST_PREFER_32_BIT=true")
       }
   })
 
   return module
}
 
func artGlobalDefaultsFactory() android.Module {
   module := artDefaultsFactory()
   android.AddLoadHook(module, globalDefaults)
 
   return module
}
 
func artDebugDefaultsFactory() android.Module {
   module := artDefaultsFactory()
   android.AddLoadHook(module, debugDefaults)
 
   return module
}
 
func artDefaultsFactory() android.Module {
   c := &codegenProperties{}
   module := cc.DefaultsFactory(c)
   android.AddLoadHook(module, func(ctx android.LoadHookContext) { codegen(ctx, c, true) })
 
   return module
}
 
func libartDefaultsFactory() android.Module {
   c := &codegenProperties{}
   module := cc.DefaultsFactory(c)
   android.AddLoadHook(module, func(ctx android.LoadHookContext) { codegen(ctx, c, true) })
 
   return module
}
 
func libartStaticDefaultsFactory() android.Module {
   c := &codegenProperties{}
   module := cc.DefaultsFactory(c)
   android.AddLoadHook(module, func(ctx android.LoadHookContext) { codegen(ctx, c, true) })
 
   return module
}
 
func artLibrary() android.Module {
   m, _ := cc.NewLibrary(android.HostAndDeviceSupported)
   module := m.Init()
 
   installCodegenCustomizer(module, true)
 
   return module
}
 
func artStaticLibrary() android.Module {
   m, library := cc.NewLibrary(android.HostAndDeviceSupported)
   library.BuildOnlyStatic()
   module := m.Init()
 
   installCodegenCustomizer(module, true)
 
   return module
}
 
func artBinary() android.Module {
   binary, _ := cc.NewBinary(android.HostAndDeviceSupported)
   module := binary.Init()
 
   android.AddLoadHook(module, customLinker)
   android.AddLoadHook(module, prefer32Bit)
   return module
}
 
func artTest() android.Module {
   test := cc.NewTest(android.HostAndDeviceSupported)
   module := test.Init()
 
   installCodegenCustomizer(module, false)
 
   android.AddLoadHook(module, customLinker)
   android.AddLoadHook(module, prefer32Bit)
   android.AddInstallHook(module, testInstall)
   return module
}
 
func artTestLibrary() android.Module {
   test := cc.NewTestLibrary(android.HostAndDeviceSupported)
   module := test.Init()
 
   installCodegenCustomizer(module, false)
 
   android.AddLoadHook(module, prefer32Bit)
   android.AddInstallHook(module, testInstall)
   return module
}
 
func envDefault(ctx android.BaseContext, key string, defaultValue string) string {
   ret := ctx.AConfig().Getenv(key)
   if ret == "" {
       return defaultValue
   }
   return ret
}
 
func envTrue(ctx android.BaseContext, key string) bool {
   return ctx.AConfig().Getenv(key) == "true"
}
 
func envFalse(ctx android.BaseContext, key string) bool {
   return ctx.AConfig().Getenv(key) == "false"
}
 
func envTrueOrDefault(ctx android.BaseContext, key string) bool {
   return ctx.AConfig().Getenv(key) != "false"
}