makevars.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // Copyright 2016 Google Inc. All rights reserved.
  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. package cc
  15. import (
  16. "fmt"
  17. "sort"
  18. "strings"
  19. "sync"
  20. "android/soong/android"
  21. "android/soong/cc/config"
  22. )
  23. var (
  24. modulesAddedWallKey = android.NewOnceKey("ModulesAddedWall")
  25. modulesUsingWnoErrorKey = android.NewOnceKey("ModulesUsingWnoError")
  26. modulesMissingProfileFileKey = android.NewOnceKey("ModulesMissingProfileFile")
  27. )
  28. func init() {
  29. android.RegisterMakeVarsProvider(pctx, makeVarsProvider)
  30. }
  31. func getNamedMapForConfig(config android.Config, key android.OnceKey) *sync.Map {
  32. return config.Once(key, func() interface{} {
  33. return &sync.Map{}
  34. }).(*sync.Map)
  35. }
  36. func makeStringOfKeys(ctx android.MakeVarsContext, key android.OnceKey) string {
  37. set := getNamedMapForConfig(ctx.Config(), key)
  38. keys := []string{}
  39. set.Range(func(key interface{}, value interface{}) bool {
  40. keys = append(keys, key.(string))
  41. return true
  42. })
  43. sort.Strings(keys)
  44. return strings.Join(keys, " ")
  45. }
  46. func makeStringOfWarningAllowedProjects() string {
  47. allProjects := append([]string{}, config.WarningAllowedProjects...)
  48. allProjects = append(allProjects, config.WarningAllowedOldProjects...)
  49. sort.Strings(allProjects)
  50. // Makefile rules use pattern "path/%" to match module paths.
  51. if len(allProjects) > 0 {
  52. return strings.Join(allProjects, "% ") + "%"
  53. } else {
  54. return ""
  55. }
  56. }
  57. type notOnHostContext struct {
  58. }
  59. func (c *notOnHostContext) Host() bool {
  60. return false
  61. }
  62. func makeVarsProvider(ctx android.MakeVarsContext) {
  63. ctx.Strict("LLVM_RELEASE_VERSION", "${config.ClangShortVersion}")
  64. ctx.Strict("LLVM_PREBUILTS_VERSION", "${config.ClangVersion}")
  65. ctx.Strict("LLVM_PREBUILTS_BASE", "${config.ClangBase}")
  66. ctx.Strict("LLVM_PREBUILTS_PATH", "${config.ClangBin}")
  67. ctx.Strict("CLANG", "${config.ClangBin}/clang")
  68. ctx.Strict("CLANG_CXX", "${config.ClangBin}/clang++")
  69. ctx.Strict("LLVM_AS", "${config.ClangBin}/llvm-as")
  70. ctx.Strict("LLVM_LINK", "${config.ClangBin}/llvm-link")
  71. ctx.Strict("LLVM_OBJCOPY", "${config.ClangBin}/llvm-objcopy")
  72. ctx.Strict("LLVM_STRIP", "${config.ClangBin}/llvm-strip")
  73. ctx.Strict("PATH_TO_CLANG_TIDY", "${config.ClangBin}/clang-tidy")
  74. ctx.StrictSorted("CLANG_CONFIG_UNKNOWN_CFLAGS", strings.Join(config.ClangUnknownCflags, " "))
  75. ctx.Strict("RS_LLVM_PREBUILTS_VERSION", "${config.RSClangVersion}")
  76. ctx.Strict("RS_LLVM_PREBUILTS_BASE", "${config.RSClangBase}")
  77. ctx.Strict("RS_LLVM_PREBUILTS_PATH", "${config.RSLLVMPrebuiltsPath}")
  78. ctx.Strict("RS_LLVM_INCLUDES", "${config.RSIncludePath}")
  79. ctx.Strict("RS_CLANG", "${config.RSLLVMPrebuiltsPath}/clang")
  80. ctx.Strict("RS_LLVM_AS", "${config.RSLLVMPrebuiltsPath}/llvm-as")
  81. ctx.Strict("RS_LLVM_LINK", "${config.RSLLVMPrebuiltsPath}/llvm-link")
  82. ctx.Strict("CLANG_EXTERNAL_CFLAGS", "${config.ExternalCflags}")
  83. ctx.Strict("GLOBAL_CLANG_CFLAGS_NO_OVERRIDE", "${config.NoOverrideGlobalCflags}")
  84. ctx.Strict("GLOBAL_CLANG_CPPFLAGS_NO_OVERRIDE", "")
  85. ctx.Strict("BOARD_VNDK_VERSION", ctx.DeviceConfig().VndkVersion())
  86. ctx.Strict("RECOVERY_SNAPSHOT_VERSION", ctx.DeviceConfig().RecoverySnapshotVersion())
  87. // Filter vendor_public_library that are exported to make
  88. exportedVendorPublicLibraries := []string{}
  89. ctx.VisitAllModules(func(module android.Module) {
  90. if ccModule, ok := module.(*Module); ok {
  91. baseName := ccModule.BaseModuleName()
  92. if ccModule.IsVendorPublicLibrary() && module.ExportedToMake() {
  93. if !inList(baseName, exportedVendorPublicLibraries) {
  94. exportedVendorPublicLibraries = append(exportedVendorPublicLibraries, baseName)
  95. }
  96. }
  97. }
  98. })
  99. sort.Strings(exportedVendorPublicLibraries)
  100. ctx.Strict("VENDOR_PUBLIC_LIBRARIES", strings.Join(exportedVendorPublicLibraries, " "))
  101. sort.Strings(lsdumpPaths)
  102. ctx.Strict("LSDUMP_PATHS", strings.Join(lsdumpPaths, " "))
  103. ctx.Strict("ANDROID_WARNING_ALLOWED_PROJECTS", makeStringOfWarningAllowedProjects())
  104. ctx.Strict("SOONG_MODULES_ADDED_WALL", makeStringOfKeys(ctx, modulesAddedWallKey))
  105. ctx.Strict("SOONG_MODULES_USING_WNO_ERROR", makeStringOfKeys(ctx, modulesUsingWnoErrorKey))
  106. ctx.Strict("SOONG_MODULES_MISSING_PGO_PROFILE_FILE", makeStringOfKeys(ctx, modulesMissingProfileFileKey))
  107. ctx.Strict("ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS", strings.Join(asanCflags, " "))
  108. ctx.Strict("ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS", strings.Join(asanLdflags, " "))
  109. ctx.Strict("HWADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS", strings.Join(hwasanCflags, " "))
  110. ctx.Strict("HWADDRESS_SANITIZER_GLOBAL_OPTIONS", strings.Join(hwasanGlobalOptions, ","))
  111. ctx.Strict("CFI_EXTRA_CFLAGS", strings.Join(cfiCflags, " "))
  112. ctx.Strict("CFI_EXTRA_ASFLAGS", strings.Join(cfiAsflags, " "))
  113. ctx.Strict("CFI_EXTRA_LDFLAGS", strings.Join(cfiLdflags, " "))
  114. ctx.Strict("INTEGER_OVERFLOW_EXTRA_CFLAGS", strings.Join(intOverflowCflags, " "))
  115. ctx.Strict("DEFAULT_C_STD_VERSION", config.CStdVersion)
  116. ctx.Strict("DEFAULT_CPP_STD_VERSION", config.CppStdVersion)
  117. ctx.Strict("EXPERIMENTAL_C_STD_VERSION", config.ExperimentalCStdVersion)
  118. ctx.Strict("EXPERIMENTAL_CPP_STD_VERSION", config.ExperimentalCppStdVersion)
  119. ctx.Strict("DEFAULT_GLOBAL_TIDY_CHECKS", "${config.TidyDefaultGlobalChecks}")
  120. ctx.Strict("DEFAULT_LOCAL_TIDY_CHECKS", joinLocalTidyChecks(config.DefaultLocalTidyChecks))
  121. ctx.Strict("DEFAULT_TIDY_HEADER_DIRS", "${config.TidyDefaultHeaderDirs}")
  122. ctx.Strict("WITH_TIDY_FLAGS", "${config.TidyWithTidyFlags}")
  123. ctx.Strict("AIDL_CPP", "${aidlCmd}")
  124. ctx.Strict("ALLOWED_MANUAL_INTERFACE_PATHS", strings.Join(allowedManualInterfacePaths, " "))
  125. ctx.Strict("RS_GLOBAL_INCLUDES", "${config.RsGlobalIncludes}")
  126. ctx.Strict("SOONG_STRIP_PATH", "${stripPath}")
  127. ctx.Strict("XZ", "${xzCmd}")
  128. ctx.Strict("CREATE_MINIDEBUGINFO", "${createMiniDebugInfo}")
  129. includeFlags, err := ctx.Eval("${config.CommonGlobalIncludes}")
  130. if err != nil {
  131. panic(err)
  132. }
  133. includes, systemIncludes := splitSystemIncludes(ctx, includeFlags)
  134. ctx.StrictRaw("SRC_HEADERS", strings.Join(includes, " "))
  135. ctx.StrictRaw("SRC_SYSTEM_HEADERS", strings.Join(systemIncludes, " "))
  136. ndkKnownLibs := *getNDKKnownLibs(ctx.Config())
  137. sort.Strings(ndkKnownLibs)
  138. ctx.Strict("NDK_KNOWN_LIBS", strings.Join(ndkKnownLibs, " "))
  139. hostTargets := ctx.Config().Targets[ctx.Config().BuildOS]
  140. makeVarsToolchain(ctx, "", hostTargets[0])
  141. if len(hostTargets) > 1 {
  142. makeVarsToolchain(ctx, "2ND_", hostTargets[1])
  143. }
  144. deviceTargets := ctx.Config().Targets[android.Android]
  145. makeVarsToolchain(ctx, "", deviceTargets[0])
  146. if len(deviceTargets) > 1 {
  147. makeVarsToolchain(ctx, "2ND_", deviceTargets[1])
  148. }
  149. }
  150. func makeVarsToolchain(ctx android.MakeVarsContext, secondPrefix string,
  151. target android.Target) {
  152. var typePrefix string
  153. switch target.Os.Class {
  154. case android.Host:
  155. typePrefix = "HOST_"
  156. case android.Device:
  157. typePrefix = "TARGET_"
  158. }
  159. makePrefix := secondPrefix + typePrefix
  160. toolchain := config.FindToolchain(target.Os, target.Arch)
  161. var productExtraCflags string
  162. var productExtraLdflags string
  163. hod := "Host"
  164. if target.Os.Class == android.Device {
  165. hod = "Device"
  166. }
  167. if target.Os.Class == android.Host && ctx.Config().HostStaticBinaries() {
  168. productExtraLdflags += "-static"
  169. }
  170. includeFlags, err := ctx.Eval(toolchain.IncludeFlags())
  171. if err != nil {
  172. panic(err)
  173. }
  174. includes, systemIncludes := splitSystemIncludes(ctx, includeFlags)
  175. ctx.StrictRaw(makePrefix+"C_INCLUDES", strings.Join(includes, " "))
  176. ctx.StrictRaw(makePrefix+"C_SYSTEM_INCLUDES", strings.Join(systemIncludes, " "))
  177. if target.Arch.ArchType == android.Arm {
  178. flags, err := toolchain.InstructionSetFlags("arm")
  179. if err != nil {
  180. panic(err)
  181. }
  182. ctx.Strict(makePrefix+"arm_CFLAGS", flags)
  183. flags, err = toolchain.InstructionSetFlags("thumb")
  184. if err != nil {
  185. panic(err)
  186. }
  187. ctx.Strict(makePrefix+"thumb_CFLAGS", flags)
  188. }
  189. clangPrefix := secondPrefix + "CLANG_" + typePrefix
  190. clangExtras := "-B" + config.ToolPath(toolchain)
  191. ctx.Strict(clangPrefix+"TRIPLE", toolchain.ClangTriple())
  192. ctx.Strict(clangPrefix+"GLOBAL_CFLAGS", strings.Join([]string{
  193. toolchain.Cflags(),
  194. "${config.CommonGlobalCflags}",
  195. fmt.Sprintf("${config.%sGlobalCflags}", hod),
  196. toolchain.ToolchainCflags(),
  197. clangExtras,
  198. productExtraCflags,
  199. }, " "))
  200. ctx.Strict(clangPrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
  201. "${config.CommonGlobalCppflags}",
  202. fmt.Sprintf("${config.%sGlobalCppflags}", hod),
  203. toolchain.Cppflags(),
  204. }, " "))
  205. ctx.Strict(clangPrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
  206. fmt.Sprintf("${config.%sGlobalLdflags}", hod),
  207. toolchain.Ldflags(),
  208. toolchain.ToolchainLdflags(),
  209. productExtraLdflags,
  210. clangExtras,
  211. }, " "))
  212. ctx.Strict(clangPrefix+"GLOBAL_LLDFLAGS", strings.Join([]string{
  213. fmt.Sprintf("${config.%sGlobalLldflags}", hod),
  214. toolchain.Lldflags(),
  215. toolchain.ToolchainLdflags(),
  216. productExtraLdflags,
  217. clangExtras,
  218. }, " "))
  219. if target.Os.Class == android.Device {
  220. ctx.Strict(secondPrefix+"ADDRESS_SANITIZER_RUNTIME_LIBRARY", strings.TrimSuffix(config.AddressSanitizerRuntimeLibrary(toolchain), ".so"))
  221. ctx.Strict(secondPrefix+"HWADDRESS_SANITIZER_RUNTIME_LIBRARY", strings.TrimSuffix(config.HWAddressSanitizerRuntimeLibrary(toolchain), ".so"))
  222. ctx.Strict(secondPrefix+"HWADDRESS_SANITIZER_STATIC_LIBRARY", strings.TrimSuffix(config.HWAddressSanitizerStaticLibrary(toolchain), ".a"))
  223. ctx.Strict(secondPrefix+"UBSAN_RUNTIME_LIBRARY", strings.TrimSuffix(config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain), ".so"))
  224. ctx.Strict(secondPrefix+"UBSAN_MINIMAL_RUNTIME_LIBRARY", strings.TrimSuffix(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(toolchain), ".a"))
  225. ctx.Strict(secondPrefix+"TSAN_RUNTIME_LIBRARY", strings.TrimSuffix(config.ThreadSanitizerRuntimeLibrary(toolchain), ".so"))
  226. ctx.Strict(secondPrefix+"SCUDO_RUNTIME_LIBRARY", strings.TrimSuffix(config.ScudoRuntimeLibrary(toolchain), ".so"))
  227. ctx.Strict(secondPrefix+"SCUDO_MINIMAL_RUNTIME_LIBRARY", strings.TrimSuffix(config.ScudoMinimalRuntimeLibrary(toolchain), ".so"))
  228. }
  229. // This is used by external/gentoo/...
  230. ctx.Strict("CLANG_CONFIG_"+target.Arch.ArchType.Name+"_"+typePrefix+"TRIPLE",
  231. toolchain.ClangTriple())
  232. if target.Os == android.Darwin {
  233. ctx.Strict(makePrefix+"AR", "${config.MacArPath}")
  234. ctx.Strict(makePrefix+"NM", "${config.MacToolPath}/nm")
  235. ctx.Strict(makePrefix+"OTOOL", "${config.MacToolPath}/otool")
  236. ctx.Strict(makePrefix+"STRIP", "${config.MacStripPath}")
  237. } else {
  238. ctx.Strict(makePrefix+"AR", "${config.ClangBin}/llvm-ar")
  239. ctx.Strict(makePrefix+"READELF", "${config.ClangBin}/llvm-readelf")
  240. ctx.Strict(makePrefix+"NM", "${config.ClangBin}/llvm-nm")
  241. ctx.Strict(makePrefix+"STRIP", "${config.ClangBin}/llvm-strip")
  242. }
  243. if target.Os.Class == android.Device {
  244. ctx.Strict(makePrefix+"OBJCOPY", "${config.ClangBin}/llvm-objcopy")
  245. ctx.Strict(makePrefix+"LD", "${config.ClangBin}/lld")
  246. ctx.Strict(makePrefix+"NDK_TRIPLE", config.NDKTriple(toolchain))
  247. ctx.Strict(makePrefix+"TOOLS_PREFIX", "${config.ClangBin}/llvm-")
  248. // TODO: GCC version is obsolete now that GCC has been removed.
  249. ctx.Strict(makePrefix+"GCC_VERSION", toolchain.GccVersion())
  250. }
  251. if target.Os.Class == android.Host {
  252. ctx.Strict(makePrefix+"AVAILABLE_LIBRARIES", strings.Join(toolchain.AvailableLibraries(), " "))
  253. }
  254. ctx.Strict(makePrefix+"SHLIB_SUFFIX", toolchain.ShlibSuffix())
  255. ctx.Strict(makePrefix+"EXECUTABLE_SUFFIX", toolchain.ExecutableSuffix())
  256. }
  257. func splitSystemIncludes(ctx android.MakeVarsContext, val string) (includes, systemIncludes []string) {
  258. flags, err := ctx.Eval(val)
  259. if err != nil {
  260. panic(err)
  261. }
  262. extract := func(flags string, dirs []string, prefix string) (string, []string, bool) {
  263. if strings.HasPrefix(flags, prefix) {
  264. flags = strings.TrimPrefix(flags, prefix)
  265. flags = strings.TrimLeft(flags, " ")
  266. s := strings.SplitN(flags, " ", 2)
  267. dirs = append(dirs, s[0])
  268. if len(s) > 1 {
  269. return strings.TrimLeft(s[1], " "), dirs, true
  270. }
  271. return "", dirs, true
  272. } else {
  273. return flags, dirs, false
  274. }
  275. }
  276. flags = strings.TrimLeft(flags, " ")
  277. for flags != "" {
  278. found := false
  279. flags, includes, found = extract(flags, includes, "-I")
  280. if !found {
  281. flags, systemIncludes, found = extract(flags, systemIncludes, "-isystem ")
  282. }
  283. if !found {
  284. panic(fmt.Errorf("Unexpected flag in %q", flags))
  285. }
  286. }
  287. return includes, systemIncludes
  288. }
  289. func joinLocalTidyChecks(checks []config.PathBasedTidyCheck) string {
  290. rets := make([]string, len(checks))
  291. for i, check := range config.DefaultLocalTidyChecks {
  292. rets[i] = check.PathPrefix + ":" + check.Checks
  293. }
  294. return strings.Join(rets, " ")
  295. }