1// 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 llvm 16 17import ( 18 "android/soong/android" 19 "android/soong/cc" 20 21 "github.com/google/blueprint/proptools" 22) 23 24func globalFlags(ctx android.LoadHookContext) []string { 25 var cflags []string 26 27 if ctx.Config().IsEnvTrue("FORCE_BUILD_LLVM_DISABLE_NDEBUG") { 28 cflags = append(cflags, "-D_DEBUG", "-UNDEBUG") 29 } 30 31 return cflags 32} 33 34func deviceFlags(ctx android.LoadHookContext) []string { 35 var cflags []string 36 37 return cflags 38} 39 40func hostFlags(ctx android.LoadHookContext) []string { 41 var cflags []string 42 43 if ctx.Config().IsEnvTrue("FORCE_BUILD_LLVM_DEBUG") { 44 cflags = append(cflags, "-O0", "-g") 45 } 46 47 return cflags 48} 49 50func llvmDefaults(ctx android.LoadHookContext) { 51 type props struct { 52 Target struct { 53 Android struct { 54 Cflags []string 55 Enabled *bool 56 } 57 Host struct { 58 Enabled *bool 59 } 60 Not_windows struct { 61 Cflags []string 62 } 63 } 64 Cflags []string 65 } 66 67 p := &props{} 68 p.Cflags = globalFlags(ctx) 69 p.Target.Android.Cflags = deviceFlags(ctx) 70 // Mingw fails to link binaries with lots of debug information 71 p.Target.Not_windows.Cflags = hostFlags(ctx) 72 73 if ctx.Config().IsEnvTrue("DISABLE_LLVM_DEVICE_BUILDS") { 74 p.Target.Android.Enabled = proptools.BoolPtr(false) 75 } 76 77 ctx.AppendProperties(p) 78} 79 80func forceBuildLlvmComponents(ctx android.LoadHookContext) { 81 forceBuild := false 82 if ctx.Config().IsEnvTrue("FORCE_BUILD_LLVM_COMPONENTS") { 83 forceBuild = true 84 } 85 if len(ctx.Config().SanitizeHost()) > 0 { 86 forceBuild = true 87 } 88 89 if !forceBuild { 90 type props struct { 91 Target struct { 92 Darwin_arm64 struct { 93 Enabled *bool 94 } 95 Host struct { 96 Enabled *bool 97 } 98 Linux_bionic_arm64 struct { 99 Enabled *bool 100 } 101 } 102 } 103 p := &props{} 104 p.Target.Darwin_arm64.Enabled = proptools.BoolPtr(true) 105 p.Target.Host.Enabled = proptools.BoolPtr(false) 106 p.Target.Linux_bionic_arm64.Enabled = proptools.BoolPtr(true) 107 ctx.AppendProperties(p) 108 } 109} 110 111func init() { 112 android.RegisterModuleType("llvm_defaults", llvmDefaultsFactory) 113 android.RegisterModuleType("force_build_llvm_components_defaults", forceBuildLlvmComponentsDefaultsFactory) 114} 115 116func llvmDefaultsFactory() android.Module { 117 module := cc.DefaultsFactory() 118 android.AddLoadHook(module, llvmDefaults) 119 120 return module 121} 122 123func forceBuildLlvmComponentsDefaultsFactory() android.Module { 124 module := cc.DefaultsFactory() 125 android.AddLoadHook(module, forceBuildLlvmComponents) 126 return module 127} 128