cc.go 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  1. // Copyright 2015 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. // This file contains the module types for compiling C/C++ for Android, and converts the properties
  16. // into the flags and filenames necessary to pass to the compiler. The final creation of the rules
  17. // is handled in builder.go
  18. import (
  19. "fmt"
  20. "path/filepath"
  21. "runtime"
  22. "strings"
  23. "github.com/google/blueprint"
  24. "github.com/google/blueprint/pathtools"
  25. "android/soong"
  26. "android/soong/common"
  27. "android/soong/genrule"
  28. )
  29. func init() {
  30. soong.RegisterModuleType("cc_library_static", CCLibraryStaticFactory)
  31. soong.RegisterModuleType("cc_library_shared", CCLibrarySharedFactory)
  32. soong.RegisterModuleType("cc_library", CCLibraryFactory)
  33. soong.RegisterModuleType("cc_object", CCObjectFactory)
  34. soong.RegisterModuleType("cc_binary", CCBinaryFactory)
  35. soong.RegisterModuleType("cc_test", CCTestFactory)
  36. soong.RegisterModuleType("cc_benchmark", CCBenchmarkFactory)
  37. soong.RegisterModuleType("toolchain_library", ToolchainLibraryFactory)
  38. soong.RegisterModuleType("ndk_prebuilt_library", NdkPrebuiltLibraryFactory)
  39. soong.RegisterModuleType("ndk_prebuilt_object", NdkPrebuiltObjectFactory)
  40. soong.RegisterModuleType("ndk_prebuilt_static_stl", NdkPrebuiltStaticStlFactory)
  41. soong.RegisterModuleType("ndk_prebuilt_shared_stl", NdkPrebuiltSharedStlFactory)
  42. soong.RegisterModuleType("cc_library_host_static", CCLibraryHostStaticFactory)
  43. soong.RegisterModuleType("cc_library_host_shared", CCLibraryHostSharedFactory)
  44. soong.RegisterModuleType("cc_binary_host", CCBinaryHostFactory)
  45. soong.RegisterModuleType("cc_test_host", CCTestHostFactory)
  46. soong.RegisterModuleType("cc_benchmark_host", CCBenchmarkHostFactory)
  47. // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
  48. // the Go initialization order because this package depends on common, so common's init
  49. // functions will run first.
  50. soong.RegisterEarlyMutator("link", LinkageMutator)
  51. soong.RegisterEarlyMutator("test_per_src", TestPerSrcMutator)
  52. }
  53. var (
  54. HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", common.Config.PrebuiltOS)
  55. SrcDir = pctx.VariableConfigMethod("SrcDir", common.Config.SrcDir)
  56. LibcRoot = pctx.StaticVariable("LibcRoot", "bionic/libc")
  57. LibmRoot = pctx.StaticVariable("LibmRoot", "bionic/libm")
  58. )
  59. // Flags used by lots of devices. Putting them in package static variables will save bytes in
  60. // build.ninja so they aren't repeated for every file
  61. var (
  62. commonGlobalCflags = []string{
  63. "-DANDROID",
  64. "-fmessage-length=0",
  65. "-W",
  66. "-Wall",
  67. "-Wno-unused",
  68. "-Winit-self",
  69. "-Wpointer-arith",
  70. // COMMON_RELEASE_CFLAGS
  71. "-DNDEBUG",
  72. "-UDEBUG",
  73. }
  74. deviceGlobalCflags = []string{
  75. // TARGET_ERROR_FLAGS
  76. "-Werror=return-type",
  77. "-Werror=non-virtual-dtor",
  78. "-Werror=address",
  79. "-Werror=sequence-point",
  80. }
  81. hostGlobalCflags = []string{}
  82. commonGlobalCppflags = []string{
  83. "-Wsign-promo",
  84. }
  85. illegalFlags = []string{
  86. "-w",
  87. }
  88. )
  89. func init() {
  90. pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
  91. pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
  92. pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
  93. pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
  94. pctx.StaticVariable("commonClangGlobalCflags",
  95. strings.Join(clangFilterUnknownCflags(commonGlobalCflags), " "))
  96. pctx.StaticVariable("deviceClangGlobalCflags",
  97. strings.Join(clangFilterUnknownCflags(deviceGlobalCflags), " "))
  98. pctx.StaticVariable("hostClangGlobalCflags",
  99. strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
  100. pctx.StaticVariable("commonClangGlobalCppflags",
  101. strings.Join(clangFilterUnknownCflags(commonGlobalCppflags), " "))
  102. // Everything in this list is a crime against abstraction and dependency tracking.
  103. // Do not add anything to this list.
  104. pctx.StaticVariable("commonGlobalIncludes", strings.Join([]string{
  105. "-isystem ${SrcDir}/system/core/include",
  106. "-isystem ${SrcDir}/hardware/libhardware/include",
  107. "-isystem ${SrcDir}/hardware/libhardware_legacy/include",
  108. "-isystem ${SrcDir}/hardware/ril/include",
  109. "-isystem ${SrcDir}/libnativehelper/include",
  110. "-isystem ${SrcDir}/frameworks/native/include",
  111. "-isystem ${SrcDir}/frameworks/native/opengl/include",
  112. "-isystem ${SrcDir}/frameworks/av/include",
  113. "-isystem ${SrcDir}/frameworks/base/include",
  114. }, " "))
  115. pctx.StaticVariable("clangPath", "${SrcDir}/prebuilts/clang/${HostPrebuiltTag}/host/3.6/bin/")
  116. }
  117. // Building C/C++ code is handled by objects that satisfy this interface via composition
  118. type CCModuleType interface {
  119. common.AndroidModule
  120. // Modify property values after parsing Blueprints file but before starting dependency
  121. // resolution or build rule generation
  122. ModifyProperties(common.AndroidBaseContext)
  123. // Modify the ccFlags
  124. flags(common.AndroidModuleContext, CCFlags) CCFlags
  125. // Return list of dependency names for use in AndroidDynamicDependencies and in depsToPaths
  126. depNames(common.AndroidBaseContext, CCDeps) CCDeps
  127. // Compile objects into final module
  128. compileModule(common.AndroidModuleContext, CCFlags, CCDeps, []string)
  129. // Install the built module.
  130. installModule(common.AndroidModuleContext, CCFlags)
  131. // Return the output file (.o, .a or .so) for use by other modules
  132. outputFile() string
  133. }
  134. type CCDeps struct {
  135. StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs, ObjFiles, Cflags []string
  136. WholeStaticLibObjFiles []string
  137. CrtBegin, CrtEnd string
  138. }
  139. type CCFlags struct {
  140. GlobalFlags []string // Flags that apply to C, C++, and assembly source files
  141. AsFlags []string // Flags that apply to assembly source files
  142. CFlags []string // Flags that apply to C and C++ source files
  143. ConlyFlags []string // Flags that apply to C source files
  144. CppFlags []string // Flags that apply to C++ source files
  145. YaccFlags []string // Flags that apply to Yacc source files
  146. LdFlags []string // Flags that apply to linker command lines
  147. Nocrt bool
  148. Toolchain Toolchain
  149. Clang bool
  150. }
  151. // Properties used to compile all C or C++ modules
  152. type CCBaseProperties struct {
  153. // list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files.
  154. Srcs []string `android:"arch_variant"`
  155. // list of source files that should not be used to build the C/C++ module.
  156. // This is most useful in the arch/multilib variants to remove non-common files
  157. Exclude_srcs []string `android:"arch_variant"`
  158. // list of module-specific flags that will be used for C and C++ compiles.
  159. Cflags []string `android:"arch_variant"`
  160. // list of module-specific flags that will be used for C++ compiles
  161. Cppflags []string `android:"arch_variant"`
  162. // list of module-specific flags that will be used for C compiles
  163. Conlyflags []string `android:"arch_variant"`
  164. // list of module-specific flags that will be used for .S compiles
  165. Asflags []string `android:"arch_variant"`
  166. // list of module-specific flags that will be used for .y and .yy compiles
  167. Yaccflags []string
  168. // list of module-specific flags that will be used for all link steps
  169. Ldflags []string `android:"arch_variant"`
  170. // the instruction set architecture to use to compile the C/C++
  171. // module.
  172. Instruction_set string `android:"arch_variant"`
  173. // list of directories relative to the root of the source tree that will
  174. // be added to the include path using -I.
  175. // If possible, don't use this. If adding paths from the current directory use
  176. // local_include_dirs, if adding paths from other modules use export_include_dirs in
  177. // that module.
  178. Include_dirs []string `android:"arch_variant"`
  179. // list of files relative to the root of the source tree that will be included
  180. // using -include.
  181. // If possible, don't use this.
  182. Include_files []string `android:"arch_variant"`
  183. // list of directories relative to the Blueprints file that will
  184. // be added to the include path using -I
  185. Local_include_dirs []string `android:"arch_variant"`
  186. // list of files relative to the Blueprints file that will be included
  187. // using -include.
  188. // If possible, don't use this.
  189. Local_include_files []string `android:"arch_variant"`
  190. // list of directories relative to the Blueprints file that will
  191. // be added to the include path using -I for any module that links against this module
  192. Export_include_dirs []string `android:"arch_variant"`
  193. // list of module-specific flags that will be used for C and C++ compiles when
  194. // compiling with clang
  195. Clang_cflags []string `android:"arch_variant"`
  196. // list of module-specific flags that will be used for .S compiles when
  197. // compiling with clang
  198. Clang_asflags []string `android:"arch_variant"`
  199. // list of system libraries that will be dynamically linked to
  200. // shared library and executable modules. If unset, generally defaults to libc
  201. // and libm. Set to [] to prevent linking against libc and libm.
  202. System_shared_libs []string
  203. // list of modules whose object files should be linked into this module
  204. // in their entirety. For static library modules, all of the .o files from the intermediate
  205. // directory of the dependency will be linked into this modules .a file. For a shared library,
  206. // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
  207. Whole_static_libs []string `android:"arch_variant"`
  208. // list of modules that should be statically linked into this module.
  209. Static_libs []string `android:"arch_variant"`
  210. // list of modules that should be dynamically linked into this module.
  211. Shared_libs []string `android:"arch_variant"`
  212. // allow the module to contain undefined symbols. By default,
  213. // modules cannot contain undefined symbols that are not satisified by their immediate
  214. // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
  215. // This flag should only be necessary for compiling low-level libraries like libc.
  216. Allow_undefined_symbols bool
  217. // don't link in crt_begin and crt_end. This flag should only be necessary for
  218. // compiling crt or libc.
  219. Nocrt bool `android:"arch_variant"`
  220. // don't link in libgcc.a
  221. No_libgcc bool
  222. // don't insert default compiler flags into asflags, cflags,
  223. // cppflags, conlyflags, ldflags, or include_dirs
  224. No_default_compiler_flags bool
  225. // compile module with clang instead of gcc
  226. Clang bool `android:"arch_variant"`
  227. // pass -frtti instead of -fno-rtti
  228. Rtti bool
  229. // -l arguments to pass to linker for host-provided shared libraries
  230. Host_ldlibs []string `android:"arch_variant"`
  231. // select the STL library to use. Possible values are "libc++", "libc++_static",
  232. // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
  233. // default
  234. Stl string
  235. // Set for combined shared/static libraries to prevent compiling object files a second time
  236. SkipCompileObjs bool `blueprint:"mutated"`
  237. Debug, Release struct {
  238. // list of module-specific flags that will be used for C and C++ compiles in debug or
  239. // release builds
  240. Cflags []string `android:"arch_variant"`
  241. } `android:"arch_variant"`
  242. // Minimum sdk version supported when compiling against the ndk
  243. Sdk_version string
  244. // install to a subdirectory of the default install path for the module
  245. Relative_install_path string
  246. }
  247. // CCBase contains the properties and members used by all C/C++ module types, and implements
  248. // the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
  249. // and uses a ccModuleType interface to that struct to create the build steps.
  250. type CCBase struct {
  251. common.AndroidModuleBase
  252. module CCModuleType
  253. Properties CCBaseProperties
  254. unused struct {
  255. Native_coverage bool
  256. Required []string
  257. Sanitize []string `android:"arch_variant"`
  258. Sanitize_recover []string
  259. Strip string
  260. Tags []string
  261. }
  262. installPath string
  263. savedDepNames CCDeps
  264. }
  265. func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported,
  266. multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
  267. base.module = module
  268. props = append(props, &base.Properties, &base.unused)
  269. return common.InitAndroidArchModule(module, hod, multilib, props...)
  270. }
  271. func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
  272. toolchain := c.findToolchain(ctx)
  273. if ctx.Failed() {
  274. return
  275. }
  276. flags := c.collectFlags(ctx, toolchain)
  277. if ctx.Failed() {
  278. return
  279. }
  280. deps := c.depsToPaths(ctx, c.savedDepNames)
  281. if ctx.Failed() {
  282. return
  283. }
  284. flags.CFlags = append(flags.CFlags, deps.Cflags...)
  285. objFiles := c.compileObjs(ctx, flags)
  286. if ctx.Failed() {
  287. return
  288. }
  289. generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
  290. if ctx.Failed() {
  291. return
  292. }
  293. objFiles = append(objFiles, generatedObjFiles...)
  294. c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
  295. if ctx.Failed() {
  296. return
  297. }
  298. c.ccModuleType().installModule(ctx, flags)
  299. if ctx.Failed() {
  300. return
  301. }
  302. }
  303. func (c *CCBase) ccModuleType() CCModuleType {
  304. return c.module
  305. }
  306. var _ common.AndroidDynamicDepender = (*CCBase)(nil)
  307. func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain {
  308. arch := ctx.Arch()
  309. hod := ctx.HostOrDevice()
  310. factory := toolchainFactories[hod][arch.ArchType]
  311. if factory == nil {
  312. panic(fmt.Sprintf("Toolchain not found for %s arch %q",
  313. hod.String(), arch.String()))
  314. }
  315. return factory(arch.ArchVariant, arch.CpuVariant)
  316. }
  317. func (c *CCBase) ModifyProperties(ctx common.AndroidBaseContext) {
  318. }
  319. func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
  320. depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
  321. depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
  322. depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
  323. return depNames
  324. }
  325. func (c *CCBase) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
  326. c.module.ModifyProperties(ctx)
  327. c.savedDepNames = c.module.depNames(ctx, CCDeps{})
  328. c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs)
  329. c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs)
  330. c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs)
  331. staticLibs := c.savedDepNames.WholeStaticLibs
  332. staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...)
  333. staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...)
  334. ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
  335. ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...)
  336. ret := append([]string(nil), c.savedDepNames.ObjFiles...)
  337. if c.savedDepNames.CrtBegin != "" {
  338. ret = append(ret, c.savedDepNames.CrtBegin)
  339. }
  340. if c.savedDepNames.CrtEnd != "" {
  341. ret = append(ret, c.savedDepNames.CrtEnd)
  342. }
  343. return ret
  344. }
  345. // Create a ccFlags struct that collects the compile flags from global values,
  346. // per-target values, module type values, and per-module Blueprints properties
  347. func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
  348. flags := CCFlags{
  349. CFlags: c.Properties.Cflags,
  350. CppFlags: c.Properties.Cppflags,
  351. ConlyFlags: c.Properties.Conlyflags,
  352. LdFlags: c.Properties.Ldflags,
  353. AsFlags: c.Properties.Asflags,
  354. YaccFlags: c.Properties.Yaccflags,
  355. Nocrt: c.Properties.Nocrt,
  356. Toolchain: toolchain,
  357. Clang: c.Properties.Clang,
  358. }
  359. // Include dir cflags
  360. common.CheckSrcDirsExist(ctx, c.Properties.Include_dirs, "include_dirs")
  361. common.CheckModuleSrcDirsExist(ctx, c.Properties.Local_include_dirs, "local_include_dirs")
  362. rootIncludeDirs := pathtools.PrefixPaths(c.Properties.Include_dirs, ctx.AConfig().SrcDir())
  363. localIncludeDirs := pathtools.PrefixPaths(c.Properties.Local_include_dirs, common.ModuleSrcDir(ctx))
  364. flags.GlobalFlags = append(flags.GlobalFlags,
  365. includeDirsToFlags(rootIncludeDirs),
  366. includeDirsToFlags(localIncludeDirs))
  367. rootIncludeFiles := pathtools.PrefixPaths(c.Properties.Include_files, ctx.AConfig().SrcDir())
  368. localIncludeFiles := pathtools.PrefixPaths(c.Properties.Local_include_files, common.ModuleSrcDir(ctx))
  369. flags.GlobalFlags = append(flags.GlobalFlags,
  370. includeFilesToFlags(rootIncludeFiles),
  371. includeFilesToFlags(localIncludeFiles))
  372. if !c.Properties.No_default_compiler_flags {
  373. if c.Properties.Sdk_version == "" || ctx.Host() {
  374. flags.GlobalFlags = append(flags.GlobalFlags,
  375. "${commonGlobalIncludes}",
  376. toolchain.IncludeFlags(),
  377. "-I${SrcDir}/libnativehelper/include/nativehelper")
  378. }
  379. flags.GlobalFlags = append(flags.GlobalFlags, []string{
  380. "-I" + common.ModuleSrcDir(ctx),
  381. "-I" + common.ModuleOutDir(ctx),
  382. "-I" + common.ModuleGenDir(ctx),
  383. }...)
  384. }
  385. instructionSet := c.Properties.Instruction_set
  386. instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
  387. if err != nil {
  388. ctx.ModuleErrorf("%s", err)
  389. }
  390. // TODO: debug
  391. flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
  392. if ctx.Host() && !ctx.ContainsProperty("clang") {
  393. flags.Clang = true
  394. }
  395. if flags.Clang {
  396. flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
  397. flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
  398. flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
  399. flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
  400. flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
  401. flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
  402. flags.CFlags = append(flags.CFlags, "${clangExtraCflags}")
  403. flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
  404. if ctx.Device() {
  405. flags.CFlags = append(flags.CFlags, "${clangExtraTargetCflags}")
  406. }
  407. target := "-target " + toolchain.ClangTriple()
  408. gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
  409. flags.CFlags = append(flags.CFlags, target, gccPrefix)
  410. flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
  411. flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
  412. }
  413. if !c.Properties.No_default_compiler_flags {
  414. if ctx.Device() && !c.Properties.Allow_undefined_symbols {
  415. flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
  416. }
  417. flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
  418. if flags.Clang {
  419. flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
  420. flags.GlobalFlags = append(flags.GlobalFlags,
  421. toolchain.ClangCflags(),
  422. "${commonClangGlobalCflags}",
  423. fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
  424. } else {
  425. flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
  426. flags.GlobalFlags = append(flags.GlobalFlags,
  427. toolchain.Cflags(),
  428. "${commonGlobalCflags}",
  429. fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
  430. }
  431. if ctx.Device() {
  432. if c.Properties.Rtti {
  433. flags.CppFlags = append(flags.CppFlags, "-frtti")
  434. } else {
  435. flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
  436. }
  437. }
  438. flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
  439. if flags.Clang {
  440. flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
  441. flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
  442. } else {
  443. flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
  444. flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
  445. }
  446. if ctx.Host() {
  447. flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
  448. }
  449. }
  450. flags = c.ccModuleType().flags(ctx, flags)
  451. if c.Properties.Sdk_version == "" {
  452. if ctx.Host() && !flags.Clang {
  453. // The host GCC doesn't support C++14 (and is deprecated, so likely
  454. // never will). Build these modules with C++11.
  455. flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
  456. } else {
  457. flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
  458. }
  459. }
  460. flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
  461. flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
  462. flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
  463. // Optimization to reduce size of build.ninja
  464. // Replace the long list of flags for each file with a module-local variable
  465. ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
  466. ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
  467. ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
  468. flags.CFlags = []string{"$cflags"}
  469. flags.CppFlags = []string{"$cppflags"}
  470. flags.AsFlags = []string{"$asflags"}
  471. return flags
  472. }
  473. func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
  474. return flags
  475. }
  476. // Compile a list of source files into objects a specified subdirectory
  477. func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
  478. subdir string, srcFiles, excludes []string) []string {
  479. buildFlags := ccFlagsToBuilderFlags(flags)
  480. srcFiles = ctx.ExpandSources(srcFiles, excludes)
  481. srcFiles, deps := genSources(ctx, srcFiles, buildFlags)
  482. return TransformSourceToObj(ctx, subdir, srcFiles, buildFlags, deps)
  483. }
  484. // Compile files listed in c.Properties.Srcs into objects
  485. func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
  486. if c.Properties.SkipCompileObjs {
  487. return nil
  488. }
  489. return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs)
  490. }
  491. // Compile generated source files from dependencies
  492. func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
  493. var srcs []string
  494. if c.Properties.SkipCompileObjs {
  495. return nil
  496. }
  497. ctx.VisitDirectDeps(func(module blueprint.Module) {
  498. if gen, ok := module.(genrule.SourceFileGenerator); ok {
  499. srcs = append(srcs, gen.GeneratedSourceFiles()...)
  500. }
  501. })
  502. if len(srcs) == 0 {
  503. return nil
  504. }
  505. return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
  506. }
  507. func (c *CCBase) outputFile() string {
  508. return ""
  509. }
  510. func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
  511. names []string) (modules []common.AndroidModule,
  512. outputFiles []string, exportedFlags []string) {
  513. for _, n := range names {
  514. found := false
  515. ctx.VisitDirectDeps(func(m blueprint.Module) {
  516. otherName := ctx.OtherModuleName(m)
  517. if otherName != n {
  518. return
  519. }
  520. if a, ok := m.(CCModuleType); ok {
  521. if a.Disabled() {
  522. // If a cc_library host+device module depends on a library that exists as both
  523. // cc_library_shared and cc_library_host_shared, it will end up with two
  524. // dependencies with the same name, one of which is marked disabled for each
  525. // of host and device. Ignore the disabled one.
  526. return
  527. }
  528. if a.HostOrDevice() != ctx.HostOrDevice() {
  529. ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
  530. otherName)
  531. return
  532. }
  533. if outputFile := a.outputFile(); outputFile != "" {
  534. if found {
  535. ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
  536. return
  537. }
  538. outputFiles = append(outputFiles, outputFile)
  539. modules = append(modules, a)
  540. if i, ok := a.(ccExportedFlagsProducer); ok {
  541. exportedFlags = append(exportedFlags, i.exportedFlags()...)
  542. }
  543. found = true
  544. } else {
  545. ctx.ModuleErrorf("module %q missing output file", otherName)
  546. return
  547. }
  548. } else {
  549. ctx.ModuleErrorf("module %q not an android module", otherName)
  550. return
  551. }
  552. })
  553. if !found {
  554. ctx.ModuleErrorf("unsatisified dependency on %q", n)
  555. }
  556. }
  557. return modules, outputFiles, exportedFlags
  558. }
  559. // Convert depenedency names to paths. Takes a CCDeps containing names and returns a CCDeps
  560. // containing paths
  561. func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCDeps {
  562. var depPaths CCDeps
  563. var newCflags []string
  564. var wholeStaticLibModules []common.AndroidModule
  565. wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
  566. c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
  567. depPaths.Cflags = append(depPaths.Cflags, newCflags...)
  568. for _, m := range wholeStaticLibModules {
  569. if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
  570. depPaths.WholeStaticLibObjFiles =
  571. append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
  572. } else {
  573. ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
  574. }
  575. }
  576. _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
  577. depPaths.Cflags = append(depPaths.Cflags, newCflags...)
  578. _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
  579. depPaths.Cflags = append(depPaths.Cflags, newCflags...)
  580. _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
  581. depPaths.Cflags = append(depPaths.Cflags, newCflags...)
  582. ctx.VisitDirectDeps(func(m blueprint.Module) {
  583. if obj, ok := m.(ccObjectProvider); ok {
  584. otherName := ctx.OtherModuleName(m)
  585. if otherName == depNames.CrtBegin {
  586. if !c.Properties.Nocrt {
  587. depPaths.CrtBegin = obj.object().outputFile()
  588. }
  589. } else if otherName == depNames.CrtEnd {
  590. if !c.Properties.Nocrt {
  591. depPaths.CrtEnd = obj.object().outputFile()
  592. }
  593. } else {
  594. depPaths.ObjFiles = append(depPaths.ObjFiles, obj.object().outputFile())
  595. }
  596. }
  597. })
  598. return depPaths
  599. }
  600. type ccLinkedProperties struct {
  601. VariantIsShared bool `blueprint:"mutated"`
  602. VariantIsStatic bool `blueprint:"mutated"`
  603. VariantIsStaticBinary bool `blueprint:"mutated"`
  604. }
  605. // CCLinked contains the properties and members used by libraries and executables
  606. type CCLinked struct {
  607. CCBase
  608. dynamicProperties ccLinkedProperties
  609. }
  610. func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
  611. multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
  612. props = append(props, &dynamic.dynamicProperties)
  613. return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
  614. }
  615. func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
  616. if ctx.ContainsProperty("system_shared_libs") {
  617. return c.Properties.System_shared_libs
  618. } else if ctx.Device() && c.Properties.Sdk_version == "" {
  619. return []string{"libc", "libm"}
  620. } else {
  621. return nil
  622. }
  623. }
  624. func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
  625. if c.Properties.Sdk_version != "" && ctx.Device() {
  626. switch c.Properties.Stl {
  627. case "":
  628. return "ndk_system"
  629. case "c++_shared", "c++_static",
  630. "stlport_shared", "stlport_static",
  631. "gnustl_static":
  632. return "ndk_lib" + c.Properties.Stl
  633. default:
  634. ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
  635. return ""
  636. }
  637. }
  638. switch c.Properties.Stl {
  639. case "libc++", "libc++_static",
  640. "libstdc++":
  641. return c.Properties.Stl
  642. case "none":
  643. return ""
  644. case "":
  645. if c.static() {
  646. return "libc++_static"
  647. } else {
  648. return "libc++" // TODO: mingw needs libstdc++
  649. }
  650. default:
  651. ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
  652. return ""
  653. }
  654. }
  655. var hostDynamicGccLibs, hostStaticGccLibs []string
  656. func init() {
  657. if runtime.GOOS == "darwin" {
  658. hostDynamicGccLibs = []string{"-lc", "-lSystem"}
  659. hostStaticGccLibs = []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"}
  660. } else {
  661. hostDynamicGccLibs = []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"}
  662. hostStaticGccLibs = []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"}
  663. }
  664. }
  665. func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
  666. stl := c.stl(ctx)
  667. if ctx.Failed() {
  668. return flags
  669. }
  670. switch stl {
  671. case "libc++", "libc++_static":
  672. flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
  673. flags.CFlags = append(flags.CFlags, "-I${SrcDir}/external/libcxx/include")
  674. if ctx.Host() {
  675. flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
  676. flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
  677. flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
  678. if c.staticBinary() {
  679. flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
  680. } else {
  681. flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
  682. }
  683. } else {
  684. if ctx.Arch().ArchType == common.Arm {
  685. flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
  686. }
  687. }
  688. case "libstdc++":
  689. // Using bionic's basic libstdc++. Not actually an STL. Only around until the
  690. // tree is in good enough shape to not need it.
  691. // Host builds will use GNU libstdc++.
  692. if ctx.Device() {
  693. flags.CFlags = append(flags.CFlags, "-I${SrcDir}/bionic/libstdc++/include")
  694. }
  695. case "ndk_system":
  696. ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources/"
  697. flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot+"cxx-stl/system/include")
  698. case "ndk_libc++_shared", "ndk_libc++_static":
  699. // TODO(danalbert): This really shouldn't be here...
  700. flags.CppFlags = append(flags.CppFlags, "-std=c++11")
  701. case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
  702. // Nothing
  703. case "":
  704. // None or error.
  705. if ctx.Host() {
  706. flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
  707. flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
  708. if c.staticBinary() {
  709. flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
  710. } else {
  711. flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
  712. }
  713. }
  714. default:
  715. panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
  716. }
  717. return flags
  718. }
  719. func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
  720. depNames = c.CCBase.depNames(ctx, depNames)
  721. stl := c.stl(ctx)
  722. if ctx.Failed() {
  723. return depNames
  724. }
  725. switch stl {
  726. case "libstdc++":
  727. if ctx.Device() {
  728. depNames.SharedLibs = append(depNames.SharedLibs, stl)
  729. }
  730. case "libc++", "libc++_static":
  731. if stl == "libc++" {
  732. depNames.SharedLibs = append(depNames.SharedLibs, stl)
  733. } else {
  734. depNames.StaticLibs = append(depNames.StaticLibs, stl)
  735. }
  736. if ctx.Device() {
  737. if ctx.Arch().ArchType == common.Arm {
  738. depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
  739. }
  740. if c.staticBinary() {
  741. depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
  742. } else {
  743. depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
  744. }
  745. }
  746. case "":
  747. // None or error.
  748. case "ndk_system":
  749. // TODO: Make a system STL prebuilt for the NDK.
  750. // The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have
  751. // its own includes. The includes are handled in CCBase.Flags().
  752. depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
  753. case "ndk_libc++_shared", "ndk_libstlport_shared":
  754. depNames.SharedLibs = append(depNames.SharedLibs, stl)
  755. case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
  756. depNames.StaticLibs = append(depNames.StaticLibs, stl)
  757. default:
  758. panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
  759. }
  760. if ctx.ModuleName() != "libcompiler_rt-extras" {
  761. depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
  762. }
  763. if ctx.Device() {
  764. // libgcc and libatomic have to be last on the command line
  765. depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcov", "libatomic")
  766. if !c.Properties.No_libgcc {
  767. depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcc")
  768. }
  769. if !c.static() {
  770. depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
  771. }
  772. if c.Properties.Sdk_version != "" {
  773. version := c.Properties.Sdk_version
  774. depNames.SharedLibs = append(depNames.SharedLibs,
  775. "ndk_libc."+version,
  776. "ndk_libm."+version,
  777. )
  778. }
  779. }
  780. return depNames
  781. }
  782. // ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
  783. type ccLinkedInterface interface {
  784. // Returns true if the build options for the module have selected a static or shared build
  785. buildStatic() bool
  786. buildShared() bool
  787. // Sets whether a specific variant is static or shared
  788. setStatic(bool)
  789. // Returns whether a specific variant is a static library or binary
  790. static() bool
  791. // Returns whether a module is a static binary
  792. staticBinary() bool
  793. }
  794. var _ ccLinkedInterface = (*CCLibrary)(nil)
  795. var _ ccLinkedInterface = (*CCBinary)(nil)
  796. func (c *CCLinked) static() bool {
  797. return c.dynamicProperties.VariantIsStatic
  798. }
  799. func (c *CCLinked) staticBinary() bool {
  800. return c.dynamicProperties.VariantIsStaticBinary
  801. }
  802. func (c *CCLinked) setStatic(static bool) {
  803. c.dynamicProperties.VariantIsStatic = static
  804. }
  805. type ccExportedFlagsProducer interface {
  806. exportedFlags() []string
  807. }
  808. //
  809. // Combined static+shared libraries
  810. //
  811. type CCLibraryProperties struct {
  812. BuildStatic bool `blueprint:"mutated"`
  813. BuildShared bool `blueprint:"mutated"`
  814. Static struct {
  815. Srcs []string `android:"arch_variant"`
  816. Exclude_srcs []string `android:"arch_variant"`
  817. Cflags []string `android:"arch_variant"`
  818. Whole_static_libs []string `android:"arch_variant"`
  819. Static_libs []string `android:"arch_variant"`
  820. Shared_libs []string `android:"arch_variant"`
  821. } `android:"arch_variant"`
  822. Shared struct {
  823. Srcs []string `android:"arch_variant"`
  824. Exclude_srcs []string `android:"arch_variant"`
  825. Cflags []string `android:"arch_variant"`
  826. Whole_static_libs []string `android:"arch_variant"`
  827. Static_libs []string `android:"arch_variant"`
  828. Shared_libs []string `android:"arch_variant"`
  829. } `android:"arch_variant"`
  830. // local file name to pass to the linker as --version_script
  831. Version_script string `android:"arch_variant"`
  832. }
  833. type CCLibrary struct {
  834. CCLinked
  835. reuseFrom ccLibraryInterface
  836. reuseObjFiles []string
  837. objFiles []string
  838. exportFlags []string
  839. out string
  840. LibraryProperties CCLibraryProperties
  841. }
  842. func (c *CCLibrary) buildStatic() bool {
  843. return c.LibraryProperties.BuildStatic
  844. }
  845. func (c *CCLibrary) buildShared() bool {
  846. return c.LibraryProperties.BuildShared
  847. }
  848. type ccLibraryInterface interface {
  849. ccLinkedInterface
  850. ccLibrary() *CCLibrary
  851. setReuseFrom(ccLibraryInterface)
  852. getReuseFrom() ccLibraryInterface
  853. getReuseObjFiles() []string
  854. allObjFiles() []string
  855. }
  856. var _ ccLibraryInterface = (*CCLibrary)(nil)
  857. func (c *CCLibrary) ccLibrary() *CCLibrary {
  858. return c
  859. }
  860. func NewCCLibrary(library *CCLibrary, module CCModuleType,
  861. hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
  862. return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
  863. &library.LibraryProperties)
  864. }
  865. func CCLibraryFactory() (blueprint.Module, []interface{}) {
  866. module := &CCLibrary{}
  867. module.LibraryProperties.BuildShared = true
  868. module.LibraryProperties.BuildStatic = true
  869. return NewCCLibrary(module, module, common.HostAndDeviceSupported)
  870. }
  871. func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
  872. depNames = c.CCLinked.depNames(ctx, depNames)
  873. if c.static() {
  874. depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
  875. depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
  876. depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
  877. } else {
  878. if ctx.Device() {
  879. if c.Properties.Sdk_version == "" {
  880. depNames.CrtBegin = "crtbegin_so"
  881. depNames.CrtEnd = "crtend_so"
  882. } else {
  883. depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
  884. depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
  885. }
  886. }
  887. depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
  888. depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
  889. depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
  890. }
  891. return depNames
  892. }
  893. func (c *CCLibrary) outputFile() string {
  894. return c.out
  895. }
  896. func (c *CCLibrary) getReuseObjFiles() []string {
  897. return c.reuseObjFiles
  898. }
  899. func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
  900. c.reuseFrom = reuseFrom
  901. }
  902. func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
  903. return c.reuseFrom
  904. }
  905. func (c *CCLibrary) allObjFiles() []string {
  906. return c.objFiles
  907. }
  908. func (c *CCLibrary) exportedFlags() []string {
  909. return c.exportFlags
  910. }
  911. func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
  912. flags = c.CCLinked.flags(ctx, flags)
  913. flags.CFlags = append(flags.CFlags, "-fPIC")
  914. if c.static() {
  915. flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
  916. } else {
  917. flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
  918. }
  919. if !c.static() {
  920. libName := ctx.ModuleName()
  921. // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
  922. sharedFlag := "-Wl,-shared"
  923. if c.Properties.Clang || ctx.Host() {
  924. sharedFlag = "-shared"
  925. }
  926. if ctx.Device() {
  927. flags.LdFlags = append(flags.LdFlags, "-nostdlib")
  928. }
  929. if ctx.Darwin() {
  930. flags.LdFlags = append(flags.LdFlags,
  931. "-dynamiclib",
  932. "-single_module",
  933. //"-read_only_relocs suppress",
  934. "-install_name @rpath/"+libName+sharedLibraryExtension,
  935. )
  936. } else {
  937. flags.LdFlags = append(flags.LdFlags,
  938. "-Wl,--gc-sections",
  939. sharedFlag,
  940. "-Wl,-soname,"+libName+sharedLibraryExtension,
  941. )
  942. }
  943. }
  944. return flags
  945. }
  946. func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
  947. flags CCFlags, deps CCDeps, objFiles []string) {
  948. staticFlags := flags
  949. objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
  950. c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
  951. objFiles = append(objFiles, objFilesStatic...)
  952. objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
  953. outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+staticLibraryExtension)
  954. if ctx.Darwin() {
  955. TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
  956. } else {
  957. TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
  958. }
  959. c.objFiles = objFiles
  960. c.out = outputFile
  961. common.CheckModuleSrcDirsExist(ctx, c.Properties.Export_include_dirs, "export_include_dirs")
  962. includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
  963. c.exportFlags = []string{includeDirsToFlags(includeDirs)}
  964. ctx.CheckbuildFile(outputFile)
  965. }
  966. func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
  967. flags CCFlags, deps CCDeps, objFiles []string) {
  968. sharedFlags := flags
  969. objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
  970. c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
  971. objFiles = append(objFiles, objFilesShared...)
  972. outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+sharedLibraryExtension)
  973. var linkerDeps []string
  974. if c.LibraryProperties.Version_script != "" {
  975. versionScript := filepath.Join(common.ModuleSrcDir(ctx), c.LibraryProperties.Version_script)
  976. sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript)
  977. linkerDeps = append(linkerDeps, versionScript)
  978. }
  979. TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
  980. deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
  981. ccFlagsToBuilderFlags(flags), outputFile)
  982. c.out = outputFile
  983. includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
  984. c.exportFlags = []string{includeDirsToFlags(includeDirs)}
  985. }
  986. func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
  987. flags CCFlags, deps CCDeps, objFiles []string) {
  988. // Reuse the object files from the matching static library if it exists
  989. if c.getReuseFrom().ccLibrary() == c {
  990. c.reuseObjFiles = objFiles
  991. } else {
  992. if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
  993. c.LibraryProperties.Shared.Cflags == nil {
  994. objFiles = append([]string(nil), c.getReuseFrom().getReuseObjFiles()...)
  995. }
  996. }
  997. if c.static() {
  998. c.compileStaticLibrary(ctx, flags, deps, objFiles)
  999. } else {
  1000. c.compileSharedLibrary(ctx, flags, deps, objFiles)
  1001. }
  1002. }
  1003. func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
  1004. // Static libraries do not get installed.
  1005. }
  1006. func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
  1007. installDir := "lib"
  1008. if flags.Toolchain.Is64Bit() {
  1009. installDir = "lib64"
  1010. }
  1011. ctx.InstallFile(filepath.Join(installDir, c.Properties.Relative_install_path), c.out)
  1012. }
  1013. func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
  1014. if c.static() {
  1015. c.installStaticLibrary(ctx, flags)
  1016. } else {
  1017. c.installSharedLibrary(ctx, flags)
  1018. }
  1019. }
  1020. //
  1021. // Objects (for crt*.o)
  1022. //
  1023. type ccObjectProvider interface {
  1024. object() *ccObject
  1025. }
  1026. type ccObject struct {
  1027. CCBase
  1028. out string
  1029. }
  1030. func (c *ccObject) object() *ccObject {
  1031. return c
  1032. }
  1033. func CCObjectFactory() (blueprint.Module, []interface{}) {
  1034. module := &ccObject{}
  1035. return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
  1036. }
  1037. func (*ccObject) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
  1038. // object files can't have any dynamic dependencies
  1039. return nil
  1040. }
  1041. func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
  1042. // object files can't have any dynamic dependencies
  1043. return CCDeps{}
  1044. }
  1045. func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
  1046. flags CCFlags, deps CCDeps, objFiles []string) {
  1047. objFiles = append(objFiles, deps.ObjFiles...)
  1048. var outputFile string
  1049. if len(objFiles) == 1 {
  1050. outputFile = objFiles[0]
  1051. } else {
  1052. outputFile = filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+objectExtension)
  1053. TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
  1054. }
  1055. c.out = outputFile
  1056. ctx.CheckbuildFile(outputFile)
  1057. }
  1058. func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
  1059. // Object files do not get installed.
  1060. }
  1061. func (c *ccObject) outputFile() string {
  1062. return c.out
  1063. }
  1064. var _ ccObjectProvider = (*ccObject)(nil)
  1065. //
  1066. // Executables
  1067. //
  1068. type CCBinaryProperties struct {
  1069. // compile executable with -static
  1070. Static_executable bool
  1071. // set the name of the output
  1072. Stem string `android:"arch_variant"`
  1073. // append to the name of the output
  1074. Suffix string `android:"arch_variant"`
  1075. // if set, add an extra objcopy --prefix-symbols= step
  1076. Prefix_symbols string
  1077. // Create a separate binary for each source file. Useful when there is
  1078. // global state that can not be torn down and reset between each test suite.
  1079. Test_per_src bool
  1080. }
  1081. type CCBinary struct {
  1082. CCLinked
  1083. out string
  1084. installFile string
  1085. BinaryProperties CCBinaryProperties
  1086. }
  1087. func (c *CCBinary) buildStatic() bool {
  1088. return c.BinaryProperties.Static_executable
  1089. }
  1090. func (c *CCBinary) buildShared() bool {
  1091. return !c.BinaryProperties.Static_executable
  1092. }
  1093. func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
  1094. stem := ctx.ModuleName()
  1095. if c.BinaryProperties.Stem != "" {
  1096. stem = c.BinaryProperties.Stem
  1097. }
  1098. return stem + c.BinaryProperties.Suffix
  1099. }
  1100. func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
  1101. depNames = c.CCLinked.depNames(ctx, depNames)
  1102. if ctx.Device() {
  1103. if c.Properties.Sdk_version == "" {
  1104. if c.BinaryProperties.Static_executable {
  1105. depNames.CrtBegin = "crtbegin_static"
  1106. } else {
  1107. depNames.CrtBegin = "crtbegin_dynamic"
  1108. }
  1109. depNames.CrtEnd = "crtend_android"
  1110. } else {
  1111. if c.BinaryProperties.Static_executable {
  1112. depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
  1113. } else {
  1114. depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
  1115. }
  1116. depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
  1117. }
  1118. if c.BinaryProperties.Static_executable {
  1119. if c.stl(ctx) == "libc++_static" {
  1120. depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
  1121. }
  1122. // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
  1123. // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
  1124. // move them to the beginning of deps.LateStaticLibs
  1125. var groupLibs []string
  1126. depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
  1127. []string{"libc", "libc_nomalloc", "libcompiler_rt"})
  1128. depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
  1129. }
  1130. }
  1131. return depNames
  1132. }
  1133. func NewCCBinary(binary *CCBinary, module CCModuleType,
  1134. hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
  1135. props = append(props, &binary.BinaryProperties)
  1136. return newCCDynamic(&binary.CCLinked, module, hod, common.MultilibFirst, props...)
  1137. }
  1138. func CCBinaryFactory() (blueprint.Module, []interface{}) {
  1139. module := &CCBinary{}
  1140. return NewCCBinary(module, module, common.HostAndDeviceSupported)
  1141. }
  1142. func (c *CCBinary) ModifyProperties(ctx common.AndroidBaseContext) {
  1143. if ctx.Darwin() {
  1144. c.BinaryProperties.Static_executable = false
  1145. }
  1146. if c.BinaryProperties.Static_executable {
  1147. c.dynamicProperties.VariantIsStaticBinary = true
  1148. }
  1149. }
  1150. func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
  1151. flags = c.CCLinked.flags(ctx, flags)
  1152. flags.CFlags = append(flags.CFlags, "-fpie")
  1153. if ctx.Device() {
  1154. if c.BinaryProperties.Static_executable {
  1155. // Clang driver needs -static to create static executable.
  1156. // However, bionic/linker uses -shared to overwrite.
  1157. // Linker for x86 targets does not allow coexistance of -static and -shared,
  1158. // so we add -static only if -shared is not used.
  1159. if !inList("-shared", flags.LdFlags) {
  1160. flags.LdFlags = append(flags.LdFlags, "-static")
  1161. }
  1162. flags.LdFlags = append(flags.LdFlags,
  1163. "-nostdlib",
  1164. "-Bstatic",
  1165. "-Wl,--gc-sections",
  1166. )
  1167. } else {
  1168. linker := "/system/bin/linker"
  1169. if flags.Toolchain.Is64Bit() {
  1170. linker = "/system/bin/linker64"
  1171. }
  1172. flags.LdFlags = append(flags.LdFlags,
  1173. "-nostdlib",
  1174. "-Bdynamic",
  1175. fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
  1176. "-Wl,--gc-sections",
  1177. "-Wl,-z,nocopyreloc",
  1178. )
  1179. }
  1180. } else if ctx.Darwin() {
  1181. flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
  1182. }
  1183. return flags
  1184. }
  1185. func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
  1186. flags CCFlags, deps CCDeps, objFiles []string) {
  1187. if !c.BinaryProperties.Static_executable && inList("libc", c.Properties.Static_libs) {
  1188. ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
  1189. "from static libs or set static_executable: true")
  1190. }
  1191. outputFile := filepath.Join(common.ModuleOutDir(ctx), c.getStem(ctx))
  1192. c.out = outputFile
  1193. if c.BinaryProperties.Prefix_symbols != "" {
  1194. afterPrefixSymbols := outputFile
  1195. outputFile = outputFile + ".intermediate"
  1196. TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
  1197. ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
  1198. }
  1199. var linkerDeps []string
  1200. TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
  1201. deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
  1202. ccFlagsToBuilderFlags(flags), outputFile)
  1203. }
  1204. func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
  1205. c.installFile = ctx.InstallFile(filepath.Join("bin", c.Properties.Relative_install_path), c.out)
  1206. }
  1207. func (c *CCBinary) HostToolPath() string {
  1208. if c.HostOrDevice().Host() {
  1209. return c.installFile
  1210. }
  1211. return ""
  1212. }
  1213. func (c *CCBinary) testPerSrc() bool {
  1214. return c.BinaryProperties.Test_per_src
  1215. }
  1216. func (c *CCBinary) binary() *CCBinary {
  1217. return c
  1218. }
  1219. type testPerSrc interface {
  1220. binary() *CCBinary
  1221. testPerSrc() bool
  1222. }
  1223. var _ testPerSrc = (*CCBinary)(nil)
  1224. func TestPerSrcMutator(mctx blueprint.EarlyMutatorContext) {
  1225. if test, ok := mctx.Module().(testPerSrc); ok {
  1226. if test.testPerSrc() {
  1227. testNames := make([]string, len(test.binary().Properties.Srcs))
  1228. for i, src := range test.binary().Properties.Srcs {
  1229. testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
  1230. }
  1231. tests := mctx.CreateLocalVariations(testNames...)
  1232. for i, src := range test.binary().Properties.Srcs {
  1233. tests[i].(testPerSrc).binary().Properties.Srcs = []string{src}
  1234. tests[i].(testPerSrc).binary().BinaryProperties.Stem = mctx.ModuleName() + "_" + testNames[i]
  1235. }
  1236. }
  1237. }
  1238. }
  1239. type CCTest struct {
  1240. CCBinary
  1241. }
  1242. func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
  1243. flags = c.CCBinary.flags(ctx, flags)
  1244. flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
  1245. if ctx.Host() {
  1246. flags.CFlags = append(flags.CFlags, "-O0", "-g")
  1247. flags.LdFlags = append(flags.LdFlags, "-lpthread")
  1248. }
  1249. // TODO(danalbert): Make gtest export its dependencies.
  1250. flags.CFlags = append(flags.CFlags,
  1251. "-I"+filepath.Join(ctx.AConfig().SrcDir(), "external/gtest/include"))
  1252. return flags
  1253. }
  1254. func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
  1255. depNames.StaticLibs = append(depNames.StaticLibs, "libgtest", "libgtest_main")
  1256. depNames = c.CCBinary.depNames(ctx, depNames)
  1257. return depNames
  1258. }
  1259. func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
  1260. if ctx.Device() {
  1261. ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
  1262. } else {
  1263. c.CCBinary.installModule(ctx, flags)
  1264. }
  1265. }
  1266. func NewCCTest(test *CCTest, module CCModuleType,
  1267. hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
  1268. return NewCCBinary(&test.CCBinary, module, hod, props...)
  1269. }
  1270. func CCTestFactory() (blueprint.Module, []interface{}) {
  1271. module := &CCTest{}
  1272. return NewCCTest(module, module, common.HostAndDeviceSupported)
  1273. }
  1274. type CCBenchmark struct {
  1275. CCBinary
  1276. }
  1277. func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
  1278. depNames = c.CCBinary.depNames(ctx, depNames)
  1279. depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
  1280. return depNames
  1281. }
  1282. func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
  1283. if ctx.Device() {
  1284. ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
  1285. } else {
  1286. c.CCBinary.installModule(ctx, flags)
  1287. }
  1288. }
  1289. func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
  1290. hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
  1291. return NewCCBinary(&test.CCBinary, module, hod, props...)
  1292. }
  1293. func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
  1294. module := &CCBenchmark{}
  1295. return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
  1296. }
  1297. //
  1298. // Static library
  1299. //
  1300. func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
  1301. module := &CCLibrary{}
  1302. module.LibraryProperties.BuildStatic = true
  1303. return NewCCLibrary(module, module, common.HostAndDeviceSupported)
  1304. }
  1305. //
  1306. // Shared libraries
  1307. //
  1308. func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
  1309. module := &CCLibrary{}
  1310. module.LibraryProperties.BuildShared = true
  1311. return NewCCLibrary(module, module, common.HostAndDeviceSupported)
  1312. }
  1313. //
  1314. // Host static library
  1315. //
  1316. func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
  1317. module := &CCLibrary{}
  1318. module.LibraryProperties.BuildStatic = true
  1319. return NewCCLibrary(module, module, common.HostSupported)
  1320. }
  1321. //
  1322. // Host Shared libraries
  1323. //
  1324. func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
  1325. module := &CCLibrary{}
  1326. module.LibraryProperties.BuildShared = true
  1327. return NewCCLibrary(module, module, common.HostSupported)
  1328. }
  1329. //
  1330. // Host Binaries
  1331. //
  1332. func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
  1333. module := &CCBinary{}
  1334. return NewCCBinary(module, module, common.HostSupported)
  1335. }
  1336. //
  1337. // Host Tests
  1338. //
  1339. func CCTestHostFactory() (blueprint.Module, []interface{}) {
  1340. module := &CCTest{}
  1341. return NewCCBinary(&module.CCBinary, module, common.HostSupported)
  1342. }
  1343. //
  1344. // Host Benchmarks
  1345. //
  1346. func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
  1347. module := &CCBenchmark{}
  1348. return NewCCBinary(&module.CCBinary, module, common.HostSupported)
  1349. }
  1350. //
  1351. // Device libraries shipped with gcc
  1352. //
  1353. type toolchainLibrary struct {
  1354. CCLibrary
  1355. }
  1356. func (*toolchainLibrary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
  1357. // toolchain libraries can't have any dependencies
  1358. return nil
  1359. }
  1360. func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
  1361. // toolchain libraries can't have any dependencies
  1362. return CCDeps{}
  1363. }
  1364. func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
  1365. module := &toolchainLibrary{}
  1366. module.LibraryProperties.BuildStatic = true
  1367. return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
  1368. &module.LibraryProperties)
  1369. }
  1370. func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
  1371. flags CCFlags, deps CCDeps, objFiles []string) {
  1372. libName := ctx.ModuleName() + staticLibraryExtension
  1373. outputFile := filepath.Join(common.ModuleOutDir(ctx), libName)
  1374. CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
  1375. c.out = outputFile
  1376. ctx.CheckbuildFile(outputFile)
  1377. }
  1378. func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
  1379. // Toolchain libraries do not get installed.
  1380. }
  1381. // NDK prebuilt libraries.
  1382. //
  1383. // These differ from regular prebuilts in that they aren't stripped and usually aren't installed
  1384. // either (with the exception of the shared STLs, which are installed to the app's directory rather
  1385. // than to the system image).
  1386. func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) string {
  1387. return fmt.Sprintf("%s/prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
  1388. ctx.AConfig().SrcDir(), version, toolchain.Name())
  1389. }
  1390. func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
  1391. ext string, version string) string {
  1392. // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
  1393. // We want to translate to just NAME.EXT
  1394. name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
  1395. dir := getNdkLibDir(ctx, toolchain, version)
  1396. return filepath.Join(dir, name+ext)
  1397. }
  1398. type ndkPrebuiltObject struct {
  1399. ccObject
  1400. }
  1401. func (*ndkPrebuiltObject) AndroidDynamicDependencies(
  1402. ctx common.AndroidDynamicDependerModuleContext) []string {
  1403. // NDK objects can't have any dependencies
  1404. return nil
  1405. }
  1406. func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
  1407. // NDK objects can't have any dependencies
  1408. return CCDeps{}
  1409. }
  1410. func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
  1411. module := &ndkPrebuiltObject{}
  1412. return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
  1413. }
  1414. func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
  1415. deps CCDeps, objFiles []string) {
  1416. // A null build step, but it sets up the output path.
  1417. if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
  1418. ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
  1419. }
  1420. c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version)
  1421. }
  1422. func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
  1423. // Objects do not get installed.
  1424. }
  1425. var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
  1426. type ndkPrebuiltLibrary struct {
  1427. CCLibrary
  1428. }
  1429. func (*ndkPrebuiltLibrary) AndroidDynamicDependencies(
  1430. ctx common.AndroidDynamicDependerModuleContext) []string {
  1431. // NDK libraries can't have any dependencies
  1432. return nil
  1433. }
  1434. func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
  1435. // NDK libraries can't have any dependencies
  1436. return CCDeps{}
  1437. }
  1438. func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
  1439. module := &ndkPrebuiltLibrary{}
  1440. module.LibraryProperties.BuildShared = true
  1441. return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
  1442. }
  1443. func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
  1444. deps CCDeps, objFiles []string) {
  1445. // A null build step, but it sets up the output path.
  1446. if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
  1447. ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
  1448. }
  1449. includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
  1450. c.exportFlags = []string{common.JoinWithPrefix(includeDirs, "-isystem ")}
  1451. c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, sharedLibraryExtension,
  1452. c.Properties.Sdk_version)
  1453. }
  1454. func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
  1455. // NDK prebuilt libraries do not get installed.
  1456. }
  1457. // The NDK STLs are slightly different from the prebuilt system libraries:
  1458. // * Are not specific to each platform version.
  1459. // * The libraries are not in a predictable location for each STL.
  1460. type ndkPrebuiltStl struct {
  1461. ndkPrebuiltLibrary
  1462. }
  1463. type ndkPrebuiltStaticStl struct {
  1464. ndkPrebuiltStl
  1465. }
  1466. type ndkPrebuiltSharedStl struct {
  1467. ndkPrebuiltStl
  1468. }
  1469. func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
  1470. module := &ndkPrebuiltSharedStl{}
  1471. module.LibraryProperties.BuildShared = true
  1472. return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
  1473. }
  1474. func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
  1475. module := &ndkPrebuiltStaticStl{}
  1476. module.LibraryProperties.BuildStatic = true
  1477. return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
  1478. }
  1479. func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) string {
  1480. gccVersion := toolchain.GccVersion()
  1481. var libDir string
  1482. switch stl {
  1483. case "libstlport":
  1484. libDir = "cxx-stl/stlport/libs"
  1485. case "libc++":
  1486. libDir = "cxx-stl/llvm-libc++/libs"
  1487. case "libgnustl":
  1488. libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
  1489. }
  1490. if libDir != "" {
  1491. ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources"
  1492. return fmt.Sprintf("%s/%s/%s", ndkSrcRoot, libDir, ctx.Arch().Abi)
  1493. }
  1494. ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
  1495. return ""
  1496. }
  1497. func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
  1498. deps CCDeps, objFiles []string) {
  1499. // A null build step, but it sets up the output path.
  1500. if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
  1501. ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
  1502. }
  1503. includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
  1504. c.exportFlags = []string{includeDirsToFlags(includeDirs)}
  1505. libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
  1506. libExt := sharedLibraryExtension
  1507. if c.LibraryProperties.BuildStatic {
  1508. libExt = staticLibraryExtension
  1509. }
  1510. stlName := strings.TrimSuffix(libName, "_shared")
  1511. stlName = strings.TrimSuffix(stlName, "_static")
  1512. libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
  1513. c.out = libDir + "/" + libName + libExt
  1514. }
  1515. func LinkageMutator(mctx blueprint.EarlyMutatorContext) {
  1516. if c, ok := mctx.Module().(ccLinkedInterface); ok {
  1517. var modules []blueprint.Module
  1518. if c.buildStatic() && c.buildShared() {
  1519. modules = mctx.CreateLocalVariations("static", "shared")
  1520. modules[0].(ccLinkedInterface).setStatic(true)
  1521. modules[1].(ccLinkedInterface).setStatic(false)
  1522. } else if c.buildStatic() {
  1523. modules = mctx.CreateLocalVariations("static")
  1524. modules[0].(ccLinkedInterface).setStatic(true)
  1525. } else if c.buildShared() {
  1526. modules = mctx.CreateLocalVariations("shared")
  1527. modules[0].(ccLinkedInterface).setStatic(false)
  1528. } else {
  1529. panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
  1530. }
  1531. if _, ok := c.(ccLibraryInterface); ok {
  1532. reuseFrom := modules[0].(ccLibraryInterface)
  1533. for _, m := range modules {
  1534. m.(ccLibraryInterface).setReuseFrom(reuseFrom)
  1535. }
  1536. }
  1537. }
  1538. }
  1539. // lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
  1540. // modifies the slice contents in place, and returns a subslice of the original slice
  1541. func lastUniqueElements(list []string) []string {
  1542. totalSkip := 0
  1543. for i := len(list) - 1; i >= totalSkip; i-- {
  1544. skip := 0
  1545. for j := i - 1; j >= totalSkip; j-- {
  1546. if list[i] == list[j] {
  1547. skip++
  1548. } else {
  1549. list[j+skip] = list[j]
  1550. }
  1551. }
  1552. totalSkip += skip
  1553. }
  1554. return list[totalSkip:]
  1555. }