compiler.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. "path/filepath"
  18. "regexp"
  19. "strconv"
  20. "strings"
  21. "github.com/google/blueprint/proptools"
  22. "android/soong/android"
  23. "android/soong/cc/config"
  24. )
  25. var (
  26. allowedManualInterfacePaths = []string{"vendor/", "hardware/"}
  27. )
  28. // This file contains the basic C/C++/assembly to .o compliation steps
  29. type BaseCompilerProperties struct {
  30. // list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files.
  31. // srcs may reference the outputs of other modules that produce source files like genrule
  32. // or filegroup using the syntax ":module".
  33. Srcs []string `android:"path,arch_variant"`
  34. // list of source files that should not be used to build the C/C++ module.
  35. // This is most useful in the arch/multilib variants to remove non-common files
  36. Exclude_srcs []string `android:"path,arch_variant"`
  37. // list of module-specific flags that will be used for C and C++ compiles.
  38. Cflags []string `android:"arch_variant"`
  39. // list of module-specific flags that will be used for C++ compiles
  40. Cppflags []string `android:"arch_variant"`
  41. // list of module-specific flags that will be used for C compiles
  42. Conlyflags []string `android:"arch_variant"`
  43. // list of module-specific flags that will be used for .S compiles
  44. Asflags []string `android:"arch_variant"`
  45. // list of module-specific flags that will be used for C and C++ compiles when
  46. // compiling with clang
  47. Clang_cflags []string `android:"arch_variant"`
  48. // list of module-specific flags that will be used for .S compiles when
  49. // compiling with clang
  50. Clang_asflags []string `android:"arch_variant"`
  51. // the instruction set architecture to use to compile the C/C++
  52. // module.
  53. Instruction_set *string `android:"arch_variant"`
  54. // list of directories relative to the root of the source tree that will
  55. // be added to the include path using -I.
  56. // If possible, don't use this. If adding paths from the current directory use
  57. // local_include_dirs, if adding paths from other modules use export_include_dirs in
  58. // that module.
  59. Include_dirs []string `android:"arch_variant,variant_prepend"`
  60. // list of directories relative to the Blueprints file that will
  61. // be added to the include path using -I
  62. Local_include_dirs []string `android:"arch_variant,variant_prepend"`
  63. // Add the directory containing the Android.bp file to the list of include
  64. // directories. Defaults to true.
  65. Include_build_directory *bool
  66. // list of generated sources to compile. These are the names of gensrcs or
  67. // genrule modules.
  68. Generated_sources []string `android:"arch_variant"`
  69. // list of generated sources that should not be used to build the C/C++ module.
  70. // This is most useful in the arch/multilib variants to remove non-common files
  71. Exclude_generated_sources []string `android:"arch_variant"`
  72. // list of generated headers to add to the include path. These are the names
  73. // of genrule modules.
  74. Generated_headers []string `android:"arch_variant"`
  75. // pass -frtti instead of -fno-rtti
  76. Rtti *bool
  77. // C standard version to use. Can be a specific version (such as "gnu11"),
  78. // "experimental" (which will use draft versions like C1x when available),
  79. // or the empty string (which will use the default).
  80. C_std *string
  81. // C++ standard version to use. Can be a specific version (such as
  82. // "gnu++11"), "experimental" (which will use draft versions like C++1z when
  83. // available), or the empty string (which will use the default).
  84. Cpp_std *string
  85. // if set to false, use -std=c++* instead of -std=gnu++*
  86. Gnu_extensions *bool
  87. Yacc *YaccProperties
  88. Lex *LexProperties
  89. Aidl struct {
  90. // list of directories that will be added to the aidl include paths.
  91. Include_dirs []string
  92. // list of directories relative to the Blueprints file that will
  93. // be added to the aidl include paths.
  94. Local_include_dirs []string
  95. // whether to generate traces (for systrace) for this interface
  96. Generate_traces *bool
  97. }
  98. Renderscript struct {
  99. // list of directories that will be added to the llvm-rs-cc include paths
  100. Include_dirs []string
  101. // list of flags that will be passed to llvm-rs-cc
  102. Flags []string
  103. // Renderscript API level to target
  104. Target_api *string
  105. }
  106. Debug, Release struct {
  107. // list of module-specific flags that will be used for C and C++ compiles in debug or
  108. // release builds
  109. Cflags []string `android:"arch_variant"`
  110. } `android:"arch_variant"`
  111. Target struct {
  112. Vendor, Product struct {
  113. // list of source files that should only be used in vendor or
  114. // product variant of the C/C++ module.
  115. Srcs []string `android:"path"`
  116. // list of source files that should not be used to build vendor
  117. // or product variant of the C/C++ module.
  118. Exclude_srcs []string `android:"path"`
  119. // List of additional cflags that should be used to build vendor
  120. // or product variant of the C/C++ module.
  121. Cflags []string
  122. // list of generated sources that should not be used to build
  123. // vendor or product variant of the C/C++ module.
  124. Exclude_generated_sources []string
  125. }
  126. Recovery struct {
  127. // list of source files that should only be used in the
  128. // recovery variant of the C/C++ module.
  129. Srcs []string `android:"path"`
  130. // list of source files that should not be used to
  131. // build the recovery variant of the C/C++ module.
  132. Exclude_srcs []string `android:"path"`
  133. // List of additional cflags that should be used to build the recovery
  134. // variant of the C/C++ module.
  135. Cflags []string
  136. // list of generated sources that should not be used to
  137. // build the recovery variant of the C/C++ module.
  138. Exclude_generated_sources []string
  139. }
  140. Vendor_ramdisk struct {
  141. // list of source files that should not be used to
  142. // build the vendor ramdisk variant of the C/C++ module.
  143. Exclude_srcs []string `android:"path"`
  144. // List of additional cflags that should be used to build the vendor ramdisk
  145. // variant of the C/C++ module.
  146. Cflags []string
  147. }
  148. }
  149. Proto struct {
  150. // Link statically against the protobuf runtime
  151. Static *bool `android:"arch_variant"`
  152. } `android:"arch_variant"`
  153. // Stores the original list of source files before being cleared by library reuse
  154. OriginalSrcs []string `blueprint:"mutated"`
  155. // Build and link with OpenMP
  156. Openmp *bool `android:"arch_variant"`
  157. // Deprecated.
  158. // Adds __ANDROID_APEX_<APEX_MODULE_NAME>__ macro defined for apex variants in addition to __ANDROID_APEX__
  159. Use_apex_name_macro *bool
  160. // Adds two macros for apex variants in addition to __ANDROID_APEX__
  161. // * __ANDROID_APEX_COM_ANDROID_FOO__
  162. // * __ANDROID_APEX_NAME__="com.android.foo"
  163. UseApexNameMacro bool `blueprint:"mutated"`
  164. }
  165. func NewBaseCompiler() *baseCompiler {
  166. return &baseCompiler{}
  167. }
  168. type baseCompiler struct {
  169. Properties BaseCompilerProperties
  170. Proto android.ProtoProperties
  171. cFlagsDeps android.Paths
  172. pathDeps android.Paths
  173. flags builderFlags
  174. // Sources that were passed to the C/C++ compiler
  175. srcs android.Paths
  176. // Sources that were passed in the Android.bp file, including generated sources generated by
  177. // other modules and filegroups. May include source files that have not yet been translated to
  178. // C/C++ (.aidl, .proto, etc.)
  179. srcsBeforeGen android.Paths
  180. }
  181. var _ compiler = (*baseCompiler)(nil)
  182. type CompiledInterface interface {
  183. Srcs() android.Paths
  184. }
  185. func (compiler *baseCompiler) Srcs() android.Paths {
  186. return append(android.Paths{}, compiler.srcs...)
  187. }
  188. func (compiler *baseCompiler) appendCflags(flags []string) {
  189. compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
  190. }
  191. func (compiler *baseCompiler) appendAsflags(flags []string) {
  192. compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
  193. }
  194. func (compiler *baseCompiler) compilerProps() []interface{} {
  195. return []interface{}{&compiler.Properties, &compiler.Proto}
  196. }
  197. func (compiler *baseCompiler) compilerInit(ctx BaseModuleContext) {}
  198. func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
  199. deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
  200. deps.GeneratedSources = removeListFromList(deps.GeneratedSources, compiler.Properties.Exclude_generated_sources)
  201. deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
  202. android.ProtoDeps(ctx, &compiler.Proto)
  203. if compiler.hasSrcExt(".proto") {
  204. deps = protoDeps(ctx, deps, &compiler.Proto, Bool(compiler.Properties.Proto.Static))
  205. }
  206. if Bool(compiler.Properties.Openmp) {
  207. deps.StaticLibs = append(deps.StaticLibs, "libomp")
  208. }
  209. return deps
  210. }
  211. func (compiler *baseCompiler) useApexNameMacro() bool {
  212. return Bool(compiler.Properties.Use_apex_name_macro) || compiler.Properties.UseApexNameMacro
  213. }
  214. // Return true if the module is in the WarningAllowedProjects.
  215. func warningsAreAllowed(subdir string) bool {
  216. subdir += "/"
  217. return android.HasAnyPrefix(subdir, config.WarningAllowedProjects)
  218. }
  219. func addToModuleList(ctx ModuleContext, key android.OnceKey, module string) {
  220. getNamedMapForConfig(ctx.Config(), key).Store(module, true)
  221. }
  222. // Create a Flags struct that collects the compile flags from global values,
  223. // per-target values, module type values, and per-module Blueprints properties
  224. func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
  225. tc := ctx.toolchain()
  226. modulePath := android.PathForModuleSrc(ctx).String()
  227. compiler.srcsBeforeGen = android.PathsForModuleSrcExcludes(ctx, compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
  228. compiler.srcsBeforeGen = append(compiler.srcsBeforeGen, deps.GeneratedSources...)
  229. CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
  230. CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
  231. CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
  232. CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
  233. CheckBadCompilerFlags(ctx, "vendor.cflags", compiler.Properties.Target.Vendor.Cflags)
  234. CheckBadCompilerFlags(ctx, "product.cflags", compiler.Properties.Target.Product.Cflags)
  235. CheckBadCompilerFlags(ctx, "recovery.cflags", compiler.Properties.Target.Recovery.Cflags)
  236. CheckBadCompilerFlags(ctx, "vendor_ramdisk.cflags", compiler.Properties.Target.Vendor_ramdisk.Cflags)
  237. esc := proptools.NinjaAndShellEscapeList
  238. flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Cflags)...)
  239. flags.Local.CppFlags = append(flags.Local.CppFlags, esc(compiler.Properties.Cppflags)...)
  240. flags.Local.ConlyFlags = append(flags.Local.ConlyFlags, esc(compiler.Properties.Conlyflags)...)
  241. flags.Local.AsFlags = append(flags.Local.AsFlags, esc(compiler.Properties.Asflags)...)
  242. flags.Local.YasmFlags = append(flags.Local.YasmFlags, esc(compiler.Properties.Asflags)...)
  243. flags.Yacc = compiler.Properties.Yacc
  244. flags.Lex = compiler.Properties.Lex
  245. // Include dir cflags
  246. localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
  247. if len(localIncludeDirs) > 0 {
  248. f := includeDirsToFlags(localIncludeDirs)
  249. flags.Local.CommonFlags = append(flags.Local.CommonFlags, f)
  250. flags.Local.YasmFlags = append(flags.Local.YasmFlags, f)
  251. }
  252. rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
  253. if len(rootIncludeDirs) > 0 {
  254. f := includeDirsToFlags(rootIncludeDirs)
  255. flags.Local.CommonFlags = append(flags.Local.CommonFlags, f)
  256. flags.Local.YasmFlags = append(flags.Local.YasmFlags, f)
  257. }
  258. if compiler.Properties.Include_build_directory == nil ||
  259. *compiler.Properties.Include_build_directory {
  260. flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-I"+modulePath)
  261. flags.Local.YasmFlags = append(flags.Local.YasmFlags, "-I"+modulePath)
  262. }
  263. if !(ctx.useSdk() || ctx.useVndk()) || ctx.Host() {
  264. flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
  265. "${config.CommonGlobalIncludes}",
  266. tc.IncludeFlags())
  267. }
  268. if ctx.useSdk() {
  269. // TODO: Switch to --sysroot.
  270. // The NDK headers are installed to a common sysroot. While a more
  271. // typical Soong approach would be to only make the headers for the
  272. // library you're using available, we're trying to emulate the NDK
  273. // behavior here, and the NDK always has all the NDK headers available.
  274. flags.SystemIncludeFlags = append(flags.SystemIncludeFlags,
  275. "-isystem "+getCurrentIncludePath(ctx).String(),
  276. "-isystem "+getCurrentIncludePath(ctx).Join(ctx, config.NDKTriple(tc)).String())
  277. }
  278. if ctx.useVndk() {
  279. flags.Global.CommonFlags = append(flags.Global.CommonFlags, "-D__ANDROID_VNDK__")
  280. }
  281. if ctx.inRecovery() {
  282. flags.Global.CommonFlags = append(flags.Global.CommonFlags, "-D__ANDROID_RECOVERY__")
  283. }
  284. if ctx.apexVariationName() != "" {
  285. flags.Global.CommonFlags = append(flags.Global.CommonFlags, "-D__ANDROID_APEX__")
  286. if compiler.useApexNameMacro() {
  287. flags.Global.CommonFlags = append(flags.Global.CommonFlags, "-D__ANDROID_APEX_"+makeDefineString(ctx.apexVariationName())+"__")
  288. flags.Global.CommonFlags = append(flags.Global.CommonFlags, "-D__ANDROID_APEX_NAME__='\""+ctx.apexVariationName()+"\"'")
  289. }
  290. if ctx.Device() {
  291. flags.Global.CommonFlags = append(flags.Global.CommonFlags,
  292. fmt.Sprintf("-D__ANDROID_SDK_VERSION__=%d",
  293. ctx.apexSdkVersion().FinalOrFutureInt()))
  294. }
  295. }
  296. if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
  297. flags.Global.CommonFlags = append(flags.Global.CommonFlags, "-D__ANDROID_NATIVE_BRIDGE__")
  298. }
  299. instructionSet := String(compiler.Properties.Instruction_set)
  300. if flags.RequiredInstructionSet != "" {
  301. instructionSet = flags.RequiredInstructionSet
  302. }
  303. instructionSetFlags, err := tc.ClangInstructionSetFlags(instructionSet)
  304. if err != nil {
  305. ctx.ModuleErrorf("%s", err)
  306. }
  307. CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
  308. // TODO: debug
  309. flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Release.Cflags)...)
  310. CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
  311. CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
  312. flags.Local.CFlags = config.ClangFilterUnknownCflags(flags.Local.CFlags)
  313. flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Clang_cflags)...)
  314. flags.Local.AsFlags = append(flags.Local.AsFlags, esc(compiler.Properties.Clang_asflags)...)
  315. flags.Local.CppFlags = config.ClangFilterUnknownCflags(flags.Local.CppFlags)
  316. flags.Local.ConlyFlags = config.ClangFilterUnknownCflags(flags.Local.ConlyFlags)
  317. flags.Local.LdFlags = config.ClangFilterUnknownCflags(flags.Local.LdFlags)
  318. target := "-target " + tc.ClangTriple()
  319. if ctx.Os().Class == android.Device {
  320. version := ctx.sdkVersion()
  321. if version == "" || version == "current" {
  322. target += strconv.Itoa(android.FutureApiLevelInt)
  323. } else {
  324. target += version
  325. }
  326. }
  327. gccPrefix := "-B" + config.ToolPath(tc)
  328. flags.Global.CFlags = append(flags.Global.CFlags, target, gccPrefix)
  329. flags.Global.AsFlags = append(flags.Global.AsFlags, target, gccPrefix)
  330. flags.Global.LdFlags = append(flags.Global.LdFlags, target, gccPrefix)
  331. hod := "Host"
  332. if ctx.Os().Class == android.Device {
  333. hod = "Device"
  334. }
  335. flags.Global.CommonFlags = append(flags.Global.CommonFlags, instructionSetFlags)
  336. flags.Global.ConlyFlags = append([]string{"${config.CommonGlobalConlyflags}"}, flags.Global.ConlyFlags...)
  337. flags.Global.CppFlags = append([]string{fmt.Sprintf("${config.%sGlobalCppflags}", hod)}, flags.Global.CppFlags...)
  338. flags.Global.AsFlags = append(flags.Global.AsFlags, tc.ClangAsflags())
  339. flags.Global.CppFlags = append([]string{"${config.CommonClangGlobalCppflags}"}, flags.Global.CppFlags...)
  340. flags.Global.CommonFlags = append(flags.Global.CommonFlags,
  341. tc.ClangCflags(),
  342. "${config.CommonClangGlobalCflags}",
  343. fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
  344. if isThirdParty(modulePath) {
  345. flags.Global.CommonFlags = append([]string{"${config.ClangExternalCflags}"}, flags.Global.CommonFlags...)
  346. }
  347. if tc.Bionic() {
  348. if Bool(compiler.Properties.Rtti) {
  349. flags.Local.CppFlags = append(flags.Local.CppFlags, "-frtti")
  350. } else {
  351. flags.Local.CppFlags = append(flags.Local.CppFlags, "-fno-rtti")
  352. }
  353. }
  354. flags.Global.AsFlags = append(flags.Global.AsFlags, "-D__ASSEMBLY__")
  355. flags.Global.CppFlags = append(flags.Global.CppFlags, tc.ClangCppflags())
  356. flags.Global.YasmFlags = append(flags.Global.YasmFlags, tc.YasmFlags())
  357. flags.Global.CommonFlags = append(flags.Global.CommonFlags, tc.ToolchainClangCflags())
  358. cStd := config.CStdVersion
  359. if String(compiler.Properties.C_std) == "experimental" {
  360. cStd = config.ExperimentalCStdVersion
  361. } else if String(compiler.Properties.C_std) != "" {
  362. cStd = String(compiler.Properties.C_std)
  363. }
  364. cppStd := String(compiler.Properties.Cpp_std)
  365. switch String(compiler.Properties.Cpp_std) {
  366. case "":
  367. cppStd = config.CppStdVersion
  368. case "experimental":
  369. cppStd = config.ExperimentalCppStdVersion
  370. }
  371. if compiler.Properties.Gnu_extensions != nil && *compiler.Properties.Gnu_extensions == false {
  372. cStd = gnuToCReplacer.Replace(cStd)
  373. cppStd = gnuToCReplacer.Replace(cppStd)
  374. }
  375. flags.Local.ConlyFlags = append([]string{"-std=" + cStd}, flags.Local.ConlyFlags...)
  376. flags.Local.CppFlags = append([]string{"-std=" + cppStd}, flags.Local.CppFlags...)
  377. if ctx.inVendor() {
  378. flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Target.Vendor.Cflags)...)
  379. }
  380. if ctx.inProduct() {
  381. // TODO(b/150902910): must use 'compiler.Properties.Target.Product.Cflags'
  382. flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Target.Vendor.Cflags)...)
  383. }
  384. if ctx.inRecovery() {
  385. flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Target.Recovery.Cflags)...)
  386. }
  387. if ctx.inVendorRamdisk() {
  388. flags.Local.CFlags = append(flags.Local.CFlags, esc(compiler.Properties.Target.Vendor_ramdisk.Cflags)...)
  389. }
  390. // We can enforce some rules more strictly in the code we own. strict
  391. // indicates if this is code that we can be stricter with. If we have
  392. // rules that we want to apply to *our* code (but maybe can't for
  393. // vendor/device specific things), we could extend this to be a ternary
  394. // value.
  395. strict := true
  396. if strings.HasPrefix(modulePath, "external/") {
  397. strict = false
  398. }
  399. // Can be used to make some annotations stricter for code we can fix
  400. // (such as when we mark functions as deprecated).
  401. if strict {
  402. flags.Global.CFlags = append(flags.Global.CFlags, "-DANDROID_STRICT")
  403. }
  404. if compiler.hasSrcExt(".proto") {
  405. flags = protoFlags(ctx, flags, &compiler.Proto)
  406. }
  407. if compiler.hasSrcExt(".y") || compiler.hasSrcExt(".yy") {
  408. flags.Local.CommonFlags = append(flags.Local.CommonFlags,
  409. "-I"+android.PathForModuleGen(ctx, "yacc", ctx.ModuleDir()).String())
  410. }
  411. if compiler.hasSrcExt(".mc") {
  412. flags.Local.CommonFlags = append(flags.Local.CommonFlags,
  413. "-I"+android.PathForModuleGen(ctx, "windmc", ctx.ModuleDir()).String())
  414. }
  415. if compiler.hasSrcExt(".aidl") {
  416. if len(compiler.Properties.Aidl.Local_include_dirs) > 0 {
  417. localAidlIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Aidl.Local_include_dirs)
  418. flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(localAidlIncludeDirs))
  419. }
  420. if len(compiler.Properties.Aidl.Include_dirs) > 0 {
  421. rootAidlIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Aidl.Include_dirs)
  422. flags.aidlFlags = append(flags.aidlFlags, includeDirsToFlags(rootAidlIncludeDirs))
  423. }
  424. if Bool(compiler.Properties.Aidl.Generate_traces) {
  425. flags.aidlFlags = append(flags.aidlFlags, "-t")
  426. }
  427. flags.Local.CommonFlags = append(flags.Local.CommonFlags,
  428. "-I"+android.PathForModuleGen(ctx, "aidl").String())
  429. }
  430. if compiler.hasSrcExt(".rscript") || compiler.hasSrcExt(".fs") {
  431. flags = rsFlags(ctx, flags, &compiler.Properties)
  432. }
  433. if compiler.hasSrcExt(".sysprop") {
  434. flags.Local.CommonFlags = append(flags.Local.CommonFlags,
  435. "-I"+android.PathForModuleGen(ctx, "sysprop", "include").String())
  436. }
  437. if len(compiler.Properties.Srcs) > 0 {
  438. module := ctx.ModuleDir() + "/Android.bp:" + ctx.ModuleName()
  439. if inList("-Wno-error", flags.Local.CFlags) || inList("-Wno-error", flags.Local.CppFlags) {
  440. addToModuleList(ctx, modulesUsingWnoErrorKey, module)
  441. } else if !inList("-Werror", flags.Local.CFlags) && !inList("-Werror", flags.Local.CppFlags) {
  442. if warningsAreAllowed(ctx.ModuleDir()) {
  443. addToModuleList(ctx, modulesAddedWallKey, module)
  444. flags.Local.CFlags = append([]string{"-Wall"}, flags.Local.CFlags...)
  445. } else {
  446. flags.Local.CFlags = append([]string{"-Wall", "-Werror"}, flags.Local.CFlags...)
  447. }
  448. }
  449. }
  450. if Bool(compiler.Properties.Openmp) {
  451. flags.Local.CFlags = append(flags.Local.CFlags, "-fopenmp")
  452. }
  453. // Exclude directories from manual binder interface allowed list.
  454. //TODO(b/145621474): Move this check into IInterface.h when clang-tidy no longer uses absolute paths.
  455. if android.HasAnyPrefix(ctx.ModuleDir(), allowedManualInterfacePaths) {
  456. flags.Local.CFlags = append(flags.Local.CFlags, "-DDO_NOT_CHECK_MANUAL_BINDER_INTERFACES")
  457. }
  458. return flags
  459. }
  460. func (compiler *baseCompiler) hasSrcExt(ext string) bool {
  461. for _, src := range compiler.srcsBeforeGen {
  462. if src.Ext() == ext {
  463. return true
  464. }
  465. }
  466. for _, src := range compiler.Properties.Srcs {
  467. if filepath.Ext(src) == ext {
  468. return true
  469. }
  470. }
  471. for _, src := range compiler.Properties.OriginalSrcs {
  472. if filepath.Ext(src) == ext {
  473. return true
  474. }
  475. }
  476. return false
  477. }
  478. func (compiler *baseCompiler) uniqueApexVariations() bool {
  479. return compiler.useApexNameMacro()
  480. }
  481. var invalidDefineCharRegex = regexp.MustCompile("[^a-zA-Z0-9_]")
  482. // makeDefineString transforms a name of an APEX module into a value to be used as value for C define
  483. // For example, com.android.foo => COM_ANDROID_FOO
  484. func makeDefineString(name string) string {
  485. return invalidDefineCharRegex.ReplaceAllString(strings.ToUpper(name), "_")
  486. }
  487. var gnuToCReplacer = strings.NewReplacer("gnu", "c")
  488. func ndkPathDeps(ctx ModuleContext) android.Paths {
  489. if ctx.Module().(*Module).IsSdkVariant() {
  490. // The NDK sysroot timestamp file depends on all the NDK sysroot files
  491. // (headers and libraries).
  492. return android.Paths{getNdkBaseTimestampFile(ctx)}
  493. }
  494. return nil
  495. }
  496. func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
  497. pathDeps := deps.GeneratedDeps
  498. pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
  499. buildFlags := flagsToBuilderFlags(flags)
  500. srcs := append(android.Paths(nil), compiler.srcsBeforeGen...)
  501. srcs, genDeps := genSources(ctx, srcs, buildFlags)
  502. pathDeps = append(pathDeps, genDeps...)
  503. compiler.pathDeps = pathDeps
  504. compiler.cFlagsDeps = flags.CFlagsDeps
  505. // Save src, buildFlags and context
  506. compiler.srcs = srcs
  507. // Compile files listed in c.Properties.Srcs into objects
  508. objs := compileObjs(ctx, buildFlags, "", srcs, pathDeps, compiler.cFlagsDeps)
  509. if ctx.Failed() {
  510. return Objects{}
  511. }
  512. return objs
  513. }
  514. // Compile a list of source files into objects a specified subdirectory
  515. func compileObjs(ctx android.ModuleContext, flags builderFlags,
  516. subdir string, srcFiles, pathDeps android.Paths, cFlagsDeps android.Paths) Objects {
  517. return TransformSourceToObj(ctx, subdir, srcFiles, flags, pathDeps, cFlagsDeps)
  518. }
  519. var thirdPartyDirPrefixExceptions = []*regexp.Regexp{
  520. regexp.MustCompile("^vendor/[^/]*google[^/]*/"),
  521. regexp.MustCompile("^hardware/google/"),
  522. regexp.MustCompile("^hardware/interfaces/"),
  523. regexp.MustCompile("^hardware/libhardware[^/]*/"),
  524. regexp.MustCompile("^hardware/ril/"),
  525. }
  526. func isThirdParty(path string) bool {
  527. thirdPartyDirPrefixes := []string{"external/", "vendor/", "hardware/"}
  528. if android.HasAnyPrefix(path, thirdPartyDirPrefixes) {
  529. for _, prefix := range thirdPartyDirPrefixExceptions {
  530. if prefix.MatchString(path) {
  531. return false
  532. }
  533. }
  534. }
  535. return true
  536. }
  537. // Properties for rust_bindgen related to generating rust bindings.
  538. // This exists here so these properties can be included in a cc_default
  539. // which can be used in both cc and rust modules.
  540. type RustBindgenClangProperties struct {
  541. // list of directories relative to the Blueprints file that will
  542. // be added to the include path using -I
  543. Local_include_dirs []string `android:"arch_variant,variant_prepend"`
  544. // list of static libraries that provide headers for this binding.
  545. Static_libs []string `android:"arch_variant,variant_prepend"`
  546. // list of shared libraries that provide headers for this binding.
  547. Shared_libs []string `android:"arch_variant"`
  548. // List of libraries which export include paths required for this module
  549. Header_libs []string `android:"arch_variant,variant_prepend"`
  550. // list of clang flags required to correctly interpret the headers.
  551. Cflags []string `android:"arch_variant"`
  552. // list of c++ specific clang flags required to correctly interpret the headers.
  553. // This is provided primarily to make sure cppflags defined in cc_defaults are pulled in.
  554. Cppflags []string `android:"arch_variant"`
  555. // C standard version to use. Can be a specific version (such as "gnu11"),
  556. // "experimental" (which will use draft versions like C1x when available),
  557. // or the empty string (which will use the default).
  558. //
  559. // If this is set, the file extension will be ignored and this will be used as the std version value. Setting this
  560. // to "default" will use the build system default version. This cannot be set at the same time as cpp_std.
  561. C_std *string
  562. // C++ standard version to use. Can be a specific version (such as
  563. // "gnu++11"), "experimental" (which will use draft versions like C++1z when
  564. // available), or the empty string (which will use the default).
  565. //
  566. // If this is set, the file extension will be ignored and this will be used as the std version value. Setting this
  567. // to "default" will use the build system default version. This cannot be set at the same time as c_std.
  568. Cpp_std *string
  569. //TODO(b/161141999) Add support for headers from cc_library_header modules.
  570. }