object.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. "strings"
  18. "android/soong/android"
  19. "android/soong/bazel"
  20. "android/soong/bazel/cquery"
  21. )
  22. //
  23. // Objects (for crt*.o)
  24. //
  25. func init() {
  26. android.RegisterModuleType("cc_object", ObjectFactory)
  27. android.RegisterSdkMemberType(ccObjectSdkMemberType)
  28. }
  29. var ccObjectSdkMemberType = &librarySdkMemberType{
  30. SdkMemberTypeBase: android.SdkMemberTypeBase{
  31. PropertyName: "native_objects",
  32. SupportsSdk: true,
  33. },
  34. prebuiltModuleType: "cc_prebuilt_object",
  35. }
  36. type objectLinker struct {
  37. *baseLinker
  38. Properties ObjectLinkerProperties
  39. // Location of the object in the sysroot. Empty if the object is not
  40. // included in the NDK.
  41. ndkSysrootPath android.Path
  42. }
  43. type objectBazelHandler struct {
  44. module *Module
  45. }
  46. var _ BazelHandler = (*objectBazelHandler)(nil)
  47. func (handler *objectBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
  48. bazelCtx := ctx.Config().BazelContext
  49. bazelCtx.QueueBazelRequest(label, cquery.GetOutputFiles, android.GetConfigKeyApexVariant(ctx, GetApexConfigKey(ctx)))
  50. }
  51. func (handler *objectBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
  52. bazelCtx := ctx.Config().BazelContext
  53. objPaths, err := bazelCtx.GetOutputFiles(label, android.GetConfigKeyApexVariant(ctx, GetApexConfigKey(ctx)))
  54. if err != nil {
  55. ctx.ModuleErrorf(err.Error())
  56. return
  57. }
  58. if len(objPaths) != 1 {
  59. ctx.ModuleErrorf("expected exactly one object file for '%s', but got %s", label, objPaths)
  60. return
  61. }
  62. handler.module.outputFile = android.OptionalPathForPath(android.PathForBazelOut(ctx, objPaths[0]))
  63. }
  64. type ObjectLinkerProperties struct {
  65. // list of static library modules that should only provide headers for this module.
  66. Static_libs []string `android:"arch_variant,variant_prepend"`
  67. // list of shared library modules should only provide headers for this module.
  68. Shared_libs []string `android:"arch_variant,variant_prepend"`
  69. // list of modules that should only provide headers for this module.
  70. Header_libs []string `android:"arch_variant,variant_prepend"`
  71. // list of default libraries that will provide headers for this module. If unset, generally
  72. // defaults to libc, libm, and libdl. Set to [] to prevent using headers from the defaults.
  73. System_shared_libs []string `android:"arch_variant"`
  74. // names of other cc_object modules to link into this module using partial linking
  75. Objs []string `android:"arch_variant"`
  76. // if set, add an extra objcopy --prefix-symbols= step
  77. Prefix_symbols *string
  78. // if set, the path to a linker script to pass to ld -r when combining multiple object files.
  79. Linker_script *string `android:"path,arch_variant"`
  80. // Indicates that this module is a CRT object. CRT objects will be split
  81. // into a variant per-API level between min_sdk_version and current.
  82. Crt *bool
  83. // Indicates that this module should not be included in the NDK sysroot.
  84. // Only applies to CRT objects. Defaults to false.
  85. Exclude_from_ndk_sysroot *bool
  86. }
  87. func newObject(hod android.HostOrDeviceSupported) *Module {
  88. module := newBaseModule(hod, android.MultilibBoth)
  89. module.sanitize = &sanitize{}
  90. module.stl = &stl{}
  91. return module
  92. }
  93. // cc_object runs the compiler without running the linker. It is rarely
  94. // necessary, but sometimes used to generate .s files from .c files to use as
  95. // input to a cc_genrule module.
  96. func ObjectFactory() android.Module {
  97. module := newObject(android.HostAndDeviceSupported)
  98. module.linker = &objectLinker{
  99. baseLinker: NewBaseLinker(module.sanitize),
  100. }
  101. module.compiler = NewBaseCompiler()
  102. module.bazelHandler = &objectBazelHandler{module: module}
  103. // Clang's address-significance tables are incompatible with ld -r.
  104. module.compiler.appendCflags([]string{"-fno-addrsig"})
  105. module.sdkMemberTypes = []android.SdkMemberType{ccObjectSdkMemberType}
  106. module.bazelable = true
  107. return module.Init()
  108. }
  109. // For bp2build conversion.
  110. type bazelObjectAttributes struct {
  111. Srcs bazel.LabelListAttribute
  112. Srcs_as bazel.LabelListAttribute
  113. Hdrs bazel.LabelListAttribute
  114. Objs bazel.LabelListAttribute
  115. Deps bazel.LabelListAttribute
  116. System_dynamic_deps bazel.LabelListAttribute
  117. Copts bazel.StringListAttribute
  118. Asflags bazel.StringListAttribute
  119. Local_includes bazel.StringListAttribute
  120. Absolute_includes bazel.StringListAttribute
  121. Stl *string
  122. Linker_script bazel.LabelAttribute
  123. Crt *bool
  124. sdkAttributes
  125. }
  126. // objectBp2Build is the bp2build converter from cc_object modules to the
  127. // Bazel equivalent target, plus any necessary include deps for the cc_object.
  128. func objectBp2Build(ctx android.TopDownMutatorContext, m *Module) {
  129. if m.compiler == nil {
  130. // a cc_object must have access to the compiler decorator for its props.
  131. ctx.ModuleErrorf("compiler must not be nil for a cc_object module")
  132. }
  133. // Set arch-specific configurable attributes
  134. baseAttributes := bp2BuildParseBaseProps(ctx, m)
  135. compilerAttrs := baseAttributes.compilerAttributes
  136. var objs bazel.LabelListAttribute
  137. var deps bazel.LabelListAttribute
  138. systemDynamicDeps := bazel.LabelListAttribute{ForceSpecifyEmptyList: true}
  139. var linkerScript bazel.LabelAttribute
  140. for axis, configToProps := range m.GetArchVariantProperties(ctx, &ObjectLinkerProperties{}) {
  141. for config, props := range configToProps {
  142. if objectLinkerProps, ok := props.(*ObjectLinkerProperties); ok {
  143. if objectLinkerProps.Linker_script != nil {
  144. label := android.BazelLabelForModuleSrcSingle(ctx, *objectLinkerProps.Linker_script)
  145. linkerScript.SetSelectValue(axis, config, label)
  146. }
  147. objs.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, objectLinkerProps.Objs))
  148. systemSharedLibs := objectLinkerProps.System_shared_libs
  149. if len(systemSharedLibs) > 0 {
  150. systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs)
  151. }
  152. systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs))
  153. deps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, objectLinkerProps.Static_libs))
  154. deps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, objectLinkerProps.Shared_libs))
  155. deps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, objectLinkerProps.Header_libs))
  156. // static_libs, shared_libs, and header_libs have variant_prepend tag
  157. deps.Prepend = true
  158. }
  159. }
  160. }
  161. objs.ResolveExcludes()
  162. // Don't split cc_object srcs across languages. Doing so would add complexity,
  163. // and this isn't typically done for cc_object.
  164. srcs := compilerAttrs.srcs
  165. srcs.Append(compilerAttrs.cSrcs)
  166. asFlags := compilerAttrs.asFlags
  167. if compilerAttrs.asSrcs.IsEmpty() {
  168. // Skip asflags for BUILD file simplicity if there are no assembly sources.
  169. asFlags = bazel.MakeStringListAttribute(nil)
  170. }
  171. attrs := &bazelObjectAttributes{
  172. Srcs: srcs,
  173. Srcs_as: compilerAttrs.asSrcs,
  174. Objs: objs,
  175. Deps: deps,
  176. System_dynamic_deps: systemDynamicDeps,
  177. Copts: compilerAttrs.copts,
  178. Asflags: asFlags,
  179. Local_includes: compilerAttrs.localIncludes,
  180. Absolute_includes: compilerAttrs.absoluteIncludes,
  181. Stl: compilerAttrs.stl,
  182. Linker_script: linkerScript,
  183. Crt: m.linker.(*objectLinker).Properties.Crt,
  184. sdkAttributes: bp2BuildParseSdkAttributes(m),
  185. }
  186. props := bazel.BazelTargetModuleProperties{
  187. Rule_class: "cc_object",
  188. Bzl_load_location: "//build/bazel/rules/cc:cc_object.bzl",
  189. }
  190. tags := android.ApexAvailableTagsWithoutTestApexes(ctx, m)
  191. ctx.CreateBazelTargetModule(props, android.CommonAttributes{
  192. Name: m.Name(),
  193. Tags: tags,
  194. }, attrs)
  195. }
  196. func (object *objectLinker) appendLdflags(flags []string) {
  197. panic(fmt.Errorf("appendLdflags on objectLinker not supported"))
  198. }
  199. func (object *objectLinker) linkerProps() []interface{} {
  200. return []interface{}{&object.Properties}
  201. }
  202. func (*objectLinker) linkerInit(ctx BaseModuleContext) {}
  203. func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
  204. deps.HeaderLibs = append(deps.HeaderLibs, object.Properties.Header_libs...)
  205. deps.SharedLibs = append(deps.SharedLibs, object.Properties.Shared_libs...)
  206. deps.StaticLibs = append(deps.StaticLibs, object.Properties.Static_libs...)
  207. deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
  208. deps.SystemSharedLibs = object.Properties.System_shared_libs
  209. if deps.SystemSharedLibs == nil {
  210. // Provide a default set of shared libraries if system_shared_libs is unspecified.
  211. // Note: If an empty list [] is specified, it implies that the module declines the
  212. // default shared libraries.
  213. deps.SystemSharedLibs = append(deps.SystemSharedLibs, ctx.toolchain().DefaultSharedLibraries()...)
  214. }
  215. deps.LateSharedLibs = append(deps.LateSharedLibs, deps.SystemSharedLibs...)
  216. return deps
  217. }
  218. func (object *objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
  219. flags.Global.LdFlags = append(flags.Global.LdFlags, ctx.toolchain().ToolchainLdflags())
  220. if lds := android.OptionalPathForModuleSrc(ctx, object.Properties.Linker_script); lds.Valid() {
  221. flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-T,"+lds.String())
  222. flags.LdFlagsDeps = append(flags.LdFlagsDeps, lds.Path())
  223. }
  224. return flags
  225. }
  226. func (object *objectLinker) link(ctx ModuleContext,
  227. flags Flags, deps PathDeps, objs Objects) android.Path {
  228. objs = objs.Append(deps.Objs)
  229. var output android.WritablePath
  230. builderFlags := flagsToBuilderFlags(flags)
  231. outputName := ctx.ModuleName()
  232. if !strings.HasSuffix(outputName, objectExtension) {
  233. outputName += objectExtension
  234. }
  235. // isForPlatform is terribly named and actually means isNotApex.
  236. if Bool(object.Properties.Crt) &&
  237. !Bool(object.Properties.Exclude_from_ndk_sysroot) && ctx.useSdk() &&
  238. ctx.isSdkVariant() && ctx.isForPlatform() {
  239. output = getVersionedLibraryInstallPath(ctx,
  240. nativeApiLevelOrPanic(ctx, ctx.sdkVersion())).Join(ctx, outputName)
  241. object.ndkSysrootPath = output
  242. } else {
  243. output = android.PathForModuleOut(ctx, outputName)
  244. }
  245. outputFile := output
  246. if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" {
  247. if String(object.Properties.Prefix_symbols) != "" {
  248. transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), objs.objFiles[0],
  249. builderFlags, output)
  250. } else {
  251. ctx.Build(pctx, android.BuildParams{
  252. Rule: android.Cp,
  253. Input: objs.objFiles[0],
  254. Output: output,
  255. })
  256. }
  257. } else {
  258. outputAddrSig := android.PathForModuleOut(ctx, "addrsig", outputName)
  259. if String(object.Properties.Prefix_symbols) != "" {
  260. input := android.PathForModuleOut(ctx, "unprefixed", outputName)
  261. transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input,
  262. builderFlags, output)
  263. output = input
  264. }
  265. transformObjsToObj(ctx, objs.objFiles, builderFlags, outputAddrSig, flags.LdFlagsDeps)
  266. // ld -r reorders symbols and invalidates the .llvm_addrsig section, which then causes warnings
  267. // if the resulting object is used with ld --icf=safe. Strip the .llvm_addrsig section to
  268. // prevent the warnings.
  269. transformObjectNoAddrSig(ctx, outputAddrSig, output)
  270. }
  271. ctx.CheckbuildFile(outputFile)
  272. return outputFile
  273. }
  274. func (object *objectLinker) linkerSpecifiedDeps(specifiedDeps specifiedDeps) specifiedDeps {
  275. specifiedDeps.sharedLibs = append(specifiedDeps.sharedLibs, object.Properties.Shared_libs...)
  276. // Must distinguish nil and [] in system_shared_libs - ensure that [] in
  277. // either input list doesn't come out as nil.
  278. if specifiedDeps.systemSharedLibs == nil {
  279. specifiedDeps.systemSharedLibs = object.Properties.System_shared_libs
  280. } else {
  281. specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, object.Properties.System_shared_libs...)
  282. }
  283. return specifiedDeps
  284. }
  285. func (object *objectLinker) unstrippedOutputFilePath() android.Path {
  286. return nil
  287. }
  288. func (object *objectLinker) nativeCoverage() bool {
  289. return true
  290. }
  291. func (object *objectLinker) coverageOutputFilePath() android.OptionalPath {
  292. return android.OptionalPath{}
  293. }
  294. func (object *objectLinker) object() bool {
  295. return true
  296. }
  297. func (object *objectLinker) isCrt() bool {
  298. return Bool(object.Properties.Crt)
  299. }