object.go 12 KB

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