build_conversion.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. // Copyright 2020 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 bp2build
  15. /*
  16. For shareable/common functionality for conversion from soong-module to build files
  17. for queryview/bp2build
  18. */
  19. import (
  20. "fmt"
  21. "reflect"
  22. "sort"
  23. "strings"
  24. "android/soong/android"
  25. "android/soong/bazel"
  26. "android/soong/starlark_fmt"
  27. "android/soong/ui/metrics/bp2build_metrics_proto"
  28. "github.com/google/blueprint"
  29. "github.com/google/blueprint/proptools"
  30. )
  31. type BazelAttributes struct {
  32. Attrs map[string]string
  33. }
  34. type BazelTarget struct {
  35. name string
  36. packageName string
  37. content string
  38. ruleClass string
  39. bzlLoadLocation string
  40. }
  41. // IsLoadedFromStarlark determines if the BazelTarget's rule class is loaded from a .bzl file,
  42. // as opposed to a native rule built into Bazel.
  43. func (t BazelTarget) IsLoadedFromStarlark() bool {
  44. return t.bzlLoadLocation != ""
  45. }
  46. // Label is the fully qualified Bazel label constructed from the BazelTarget's
  47. // package name and target name.
  48. func (t BazelTarget) Label() string {
  49. if t.packageName == "." {
  50. return "//:" + t.name
  51. } else {
  52. return "//" + t.packageName + ":" + t.name
  53. }
  54. }
  55. // PackageName returns the package of the Bazel target.
  56. // Defaults to root of tree.
  57. func (t BazelTarget) PackageName() string {
  58. if t.packageName == "" {
  59. return "."
  60. }
  61. return t.packageName
  62. }
  63. // BazelTargets is a typedef for a slice of BazelTarget objects.
  64. type BazelTargets []BazelTarget
  65. func (targets BazelTargets) packageRule() *BazelTarget {
  66. for _, target := range targets {
  67. if target.ruleClass == "package" {
  68. return &target
  69. }
  70. }
  71. return nil
  72. }
  73. // sort a list of BazelTargets in-place, by name, and by generated/handcrafted types.
  74. func (targets BazelTargets) sort() {
  75. sort.Slice(targets, func(i, j int) bool {
  76. return targets[i].name < targets[j].name
  77. })
  78. }
  79. // String returns the string representation of BazelTargets, without load
  80. // statements (use LoadStatements for that), since the targets are usually not
  81. // adjacent to the load statements at the top of the BUILD file.
  82. func (targets BazelTargets) String() string {
  83. var res string
  84. for i, target := range targets {
  85. if target.ruleClass != "package" {
  86. res += target.content
  87. }
  88. if i != len(targets)-1 {
  89. res += "\n\n"
  90. }
  91. }
  92. return res
  93. }
  94. // LoadStatements return the string representation of the sorted and deduplicated
  95. // Starlark rule load statements needed by a group of BazelTargets.
  96. func (targets BazelTargets) LoadStatements() string {
  97. bzlToLoadedSymbols := map[string][]string{}
  98. for _, target := range targets {
  99. if target.IsLoadedFromStarlark() {
  100. bzlToLoadedSymbols[target.bzlLoadLocation] =
  101. append(bzlToLoadedSymbols[target.bzlLoadLocation], target.ruleClass)
  102. }
  103. }
  104. var loadStatements []string
  105. for bzl, ruleClasses := range bzlToLoadedSymbols {
  106. loadStatement := "load(\""
  107. loadStatement += bzl
  108. loadStatement += "\", "
  109. ruleClasses = android.SortedUniqueStrings(ruleClasses)
  110. for i, ruleClass := range ruleClasses {
  111. loadStatement += "\"" + ruleClass + "\""
  112. if i != len(ruleClasses)-1 {
  113. loadStatement += ", "
  114. }
  115. }
  116. loadStatement += ")"
  117. loadStatements = append(loadStatements, loadStatement)
  118. }
  119. return strings.Join(android.SortedUniqueStrings(loadStatements), "\n")
  120. }
  121. type bpToBuildContext interface {
  122. ModuleName(module blueprint.Module) string
  123. ModuleDir(module blueprint.Module) string
  124. ModuleSubDir(module blueprint.Module) string
  125. ModuleType(module blueprint.Module) string
  126. VisitAllModules(visit func(blueprint.Module))
  127. VisitDirectDeps(module blueprint.Module, visit func(blueprint.Module))
  128. }
  129. type CodegenContext struct {
  130. config android.Config
  131. context *android.Context
  132. mode CodegenMode
  133. additionalDeps []string
  134. unconvertedDepMode unconvertedDepsMode
  135. topDir string
  136. }
  137. func (ctx *CodegenContext) Mode() CodegenMode {
  138. return ctx.mode
  139. }
  140. // CodegenMode is an enum to differentiate code-generation modes.
  141. type CodegenMode int
  142. const (
  143. // Bp2Build - generate BUILD files with targets buildable by Bazel directly.
  144. //
  145. // This mode is used for the Soong->Bazel build definition conversion.
  146. Bp2Build CodegenMode = iota
  147. // QueryView - generate BUILD files with targets representing fully mutated
  148. // Soong modules, representing the fully configured Soong module graph with
  149. // variants and dependency edges.
  150. //
  151. // This mode is used for discovering and introspecting the existing Soong
  152. // module graph.
  153. QueryView
  154. // ApiBp2build - generate BUILD files for API contribution targets
  155. ApiBp2build
  156. )
  157. type unconvertedDepsMode int
  158. const (
  159. // Include a warning in conversion metrics about converted modules with unconverted direct deps
  160. warnUnconvertedDeps unconvertedDepsMode = iota
  161. // Error and fail conversion if encountering a module with unconverted direct deps
  162. // Enabled by setting environment variable `BP2BUILD_ERROR_UNCONVERTED`
  163. errorModulesUnconvertedDeps
  164. )
  165. func (mode CodegenMode) String() string {
  166. switch mode {
  167. case Bp2Build:
  168. return "Bp2Build"
  169. case QueryView:
  170. return "QueryView"
  171. case ApiBp2build:
  172. return "ApiBp2build"
  173. default:
  174. return fmt.Sprintf("%d", mode)
  175. }
  176. }
  177. // AddNinjaFileDeps adds dependencies on the specified files to be added to the ninja manifest. The
  178. // primary builder will be rerun whenever the specified files are modified. Allows us to fulfill the
  179. // PathContext interface in order to add dependencies on hand-crafted BUILD files. Note: must also
  180. // call AdditionalNinjaDeps and add them manually to the ninja file.
  181. func (ctx *CodegenContext) AddNinjaFileDeps(deps ...string) {
  182. ctx.additionalDeps = append(ctx.additionalDeps, deps...)
  183. }
  184. // AdditionalNinjaDeps returns additional ninja deps added by CodegenContext
  185. func (ctx *CodegenContext) AdditionalNinjaDeps() []string {
  186. return ctx.additionalDeps
  187. }
  188. func (ctx *CodegenContext) Config() android.Config { return ctx.config }
  189. func (ctx *CodegenContext) Context() *android.Context { return ctx.context }
  190. // NewCodegenContext creates a wrapper context that conforms to PathContext for
  191. // writing BUILD files in the output directory.
  192. func NewCodegenContext(config android.Config, context *android.Context, mode CodegenMode, topDir string) *CodegenContext {
  193. var unconvertedDeps unconvertedDepsMode
  194. if config.IsEnvTrue("BP2BUILD_ERROR_UNCONVERTED") {
  195. unconvertedDeps = errorModulesUnconvertedDeps
  196. }
  197. return &CodegenContext{
  198. context: context,
  199. config: config,
  200. mode: mode,
  201. unconvertedDepMode: unconvertedDeps,
  202. topDir: topDir,
  203. }
  204. }
  205. // props is an unsorted map. This function ensures that
  206. // the generated attributes are sorted to ensure determinism.
  207. func propsToAttributes(props map[string]string) string {
  208. var attributes string
  209. for _, propName := range android.SortedKeys(props) {
  210. attributes += fmt.Sprintf(" %s = %s,\n", propName, props[propName])
  211. }
  212. return attributes
  213. }
  214. type conversionResults struct {
  215. buildFileToTargets map[string]BazelTargets
  216. metrics CodegenMetrics
  217. }
  218. func (r conversionResults) BuildDirToTargets() map[string]BazelTargets {
  219. return r.buildFileToTargets
  220. }
  221. func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (conversionResults, []error) {
  222. buildFileToTargets := make(map[string]BazelTargets)
  223. // Simple metrics tracking for bp2build
  224. metrics := CreateCodegenMetrics()
  225. dirs := make(map[string]bool)
  226. var errs []error
  227. bpCtx := ctx.Context()
  228. bpCtx.VisitAllModules(func(m blueprint.Module) {
  229. dir := bpCtx.ModuleDir(m)
  230. moduleType := bpCtx.ModuleType(m)
  231. dirs[dir] = true
  232. var targets []BazelTarget
  233. switch ctx.Mode() {
  234. case Bp2Build:
  235. // There are two main ways of converting a Soong module to Bazel:
  236. // 1) Manually handcrafting a Bazel target and associating the module with its label
  237. // 2) Automatically generating with bp2build converters
  238. //
  239. // bp2build converters are used for the majority of modules.
  240. if b, ok := m.(android.Bazelable); ok && b.HasHandcraftedLabel() {
  241. // Handle modules converted to handcrafted targets.
  242. //
  243. // Since these modules are associated with some handcrafted
  244. // target in a BUILD file, we don't autoconvert them.
  245. // Log the module.
  246. metrics.AddUnconvertedModule(m, moduleType, dir,
  247. android.UnconvertedReason{
  248. ReasonType: int(bp2build_metrics_proto.UnconvertedReasonType_DEFINED_IN_BUILD_FILE),
  249. })
  250. } else if aModule, ok := m.(android.Module); ok && aModule.IsConvertedByBp2build() {
  251. // Handle modules converted to generated targets.
  252. // Log the module.
  253. metrics.AddConvertedModule(aModule, moduleType, dir)
  254. // Handle modules with unconverted deps. By default, emit a warning.
  255. if unconvertedDeps := aModule.GetUnconvertedBp2buildDeps(); len(unconvertedDeps) > 0 {
  256. msg := fmt.Sprintf("%s %s:%s depends on unconverted modules: %s",
  257. moduleType, bpCtx.ModuleDir(m), m.Name(), strings.Join(unconvertedDeps, ", "))
  258. switch ctx.unconvertedDepMode {
  259. case warnUnconvertedDeps:
  260. metrics.moduleWithUnconvertedDepsMsgs = append(metrics.moduleWithUnconvertedDepsMsgs, msg)
  261. case errorModulesUnconvertedDeps:
  262. errs = append(errs, fmt.Errorf(msg))
  263. return
  264. }
  265. }
  266. if unconvertedDeps := aModule.GetMissingBp2buildDeps(); len(unconvertedDeps) > 0 {
  267. msg := fmt.Sprintf("%s %s:%s depends on missing modules: %s",
  268. moduleType, bpCtx.ModuleDir(m), m.Name(), strings.Join(unconvertedDeps, ", "))
  269. switch ctx.unconvertedDepMode {
  270. case warnUnconvertedDeps:
  271. metrics.moduleWithMissingDepsMsgs = append(metrics.moduleWithMissingDepsMsgs, msg)
  272. case errorModulesUnconvertedDeps:
  273. errs = append(errs, fmt.Errorf(msg))
  274. return
  275. }
  276. }
  277. var targetErrs []error
  278. targets, targetErrs = generateBazelTargets(bpCtx, aModule)
  279. errs = append(errs, targetErrs...)
  280. for _, t := range targets {
  281. // A module can potentially generate more than 1 Bazel
  282. // target, each of a different rule class.
  283. metrics.IncrementRuleClassCount(t.ruleClass)
  284. }
  285. } else if _, ok := ctx.Config().BazelModulesForceEnabledByFlag()[m.Name()]; ok && m.Name() != "" {
  286. err := fmt.Errorf("Force Enabled Module %s not converted", m.Name())
  287. errs = append(errs, err)
  288. } else if aModule, ok := m.(android.Module); ok {
  289. reason := aModule.GetUnconvertedReason()
  290. if reason == nil {
  291. panic(fmt.Errorf("module '%s' was neither converted nor marked unconvertible with bp2build", aModule.Name()))
  292. } else {
  293. metrics.AddUnconvertedModule(m, moduleType, dir, *reason)
  294. }
  295. return
  296. } else {
  297. metrics.AddUnconvertedModule(m, moduleType, dir, android.UnconvertedReason{
  298. ReasonType: int(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED),
  299. })
  300. return
  301. }
  302. case QueryView:
  303. // Blocklist certain module types from being generated.
  304. if canonicalizeModuleType(bpCtx.ModuleType(m)) == "package" {
  305. // package module name contain slashes, and thus cannot
  306. // be mapped cleanly to a bazel label.
  307. return
  308. }
  309. t, err := generateSoongModuleTarget(bpCtx, m)
  310. if err != nil {
  311. errs = append(errs, err)
  312. }
  313. targets = append(targets, t)
  314. case ApiBp2build:
  315. if aModule, ok := m.(android.Module); ok && aModule.IsConvertedByBp2build() {
  316. targets, errs = generateBazelTargets(bpCtx, aModule)
  317. }
  318. default:
  319. errs = append(errs, fmt.Errorf("Unknown code-generation mode: %s", ctx.Mode()))
  320. return
  321. }
  322. for _, target := range targets {
  323. targetDir := target.PackageName()
  324. buildFileToTargets[targetDir] = append(buildFileToTargets[targetDir], target)
  325. }
  326. })
  327. if len(errs) > 0 {
  328. return conversionResults{}, errs
  329. }
  330. if generateFilegroups {
  331. // Add a filegroup target that exposes all sources in the subtree of this package
  332. // NOTE: This also means we generate a BUILD file for every Android.bp file (as long as it has at least one module)
  333. //
  334. // This works because: https://bazel.build/reference/be/functions#exports_files
  335. // "As a legacy behaviour, also files mentioned as input to a rule are exported with the
  336. // default visibility until the flag --incompatible_no_implicit_file_export is flipped. However, this behavior
  337. // should not be relied upon and actively migrated away from."
  338. //
  339. // TODO(b/198619163): We should change this to export_files(glob(["**/*"])) instead, but doing that causes these errors:
  340. // "Error in exports_files: generated label '//external/avb:avbtool' conflicts with existing py_binary rule"
  341. // So we need to solve all the "target ... is both a rule and a file" warnings first.
  342. for dir := range dirs {
  343. buildFileToTargets[dir] = append(buildFileToTargets[dir], BazelTarget{
  344. name: "bp2build_all_srcs",
  345. content: `filegroup(name = "bp2build_all_srcs", srcs = glob(["**/*"]))`,
  346. ruleClass: "filegroup",
  347. })
  348. }
  349. }
  350. return conversionResults{
  351. buildFileToTargets: buildFileToTargets,
  352. metrics: metrics,
  353. }, errs
  354. }
  355. func generateBazelTargets(ctx bpToBuildContext, m android.Module) ([]BazelTarget, []error) {
  356. var targets []BazelTarget
  357. var errs []error
  358. for _, m := range m.Bp2buildTargets() {
  359. target, err := generateBazelTarget(ctx, m)
  360. if err != nil {
  361. errs = append(errs, err)
  362. return targets, errs
  363. }
  364. targets = append(targets, target)
  365. }
  366. return targets, errs
  367. }
  368. type bp2buildModule interface {
  369. TargetName() string
  370. TargetPackage() string
  371. BazelRuleClass() string
  372. BazelRuleLoadLocation() string
  373. BazelAttributes() []interface{}
  374. }
  375. func generateBazelTarget(ctx bpToBuildContext, m bp2buildModule) (BazelTarget, error) {
  376. ruleClass := m.BazelRuleClass()
  377. bzlLoadLocation := m.BazelRuleLoadLocation()
  378. // extract the bazel attributes from the module.
  379. attrs := m.BazelAttributes()
  380. props, err := extractModuleProperties(attrs, true)
  381. if err != nil {
  382. return BazelTarget{}, err
  383. }
  384. // name is handled in a special manner
  385. delete(props.Attrs, "name")
  386. // Return the Bazel target with rule class and attributes, ready to be
  387. // code-generated.
  388. attributes := propsToAttributes(props.Attrs)
  389. var content string
  390. targetName := m.TargetName()
  391. if targetName != "" {
  392. content = fmt.Sprintf(ruleTargetTemplate, ruleClass, targetName, attributes)
  393. } else {
  394. content = fmt.Sprintf(unnamedRuleTargetTemplate, ruleClass, attributes)
  395. }
  396. return BazelTarget{
  397. name: targetName,
  398. packageName: m.TargetPackage(),
  399. ruleClass: ruleClass,
  400. bzlLoadLocation: bzlLoadLocation,
  401. content: content,
  402. }, nil
  403. }
  404. // Convert a module and its deps and props into a Bazel macro/rule
  405. // representation in the BUILD file.
  406. func generateSoongModuleTarget(ctx bpToBuildContext, m blueprint.Module) (BazelTarget, error) {
  407. props, err := getBuildProperties(ctx, m)
  408. // TODO(b/163018919): DirectDeps can have duplicate (module, variant)
  409. // items, if the modules are added using different DependencyTag. Figure
  410. // out the implications of that.
  411. depLabels := map[string]bool{}
  412. if aModule, ok := m.(android.Module); ok {
  413. ctx.VisitDirectDeps(aModule, func(depModule blueprint.Module) {
  414. depLabels[qualifiedTargetLabel(ctx, depModule)] = true
  415. })
  416. }
  417. for p := range ignoredPropNames {
  418. delete(props.Attrs, p)
  419. }
  420. attributes := propsToAttributes(props.Attrs)
  421. depLabelList := "[\n"
  422. for depLabel := range depLabels {
  423. depLabelList += fmt.Sprintf(" %q,\n", depLabel)
  424. }
  425. depLabelList += " ]"
  426. targetName := targetNameWithVariant(ctx, m)
  427. return BazelTarget{
  428. name: targetName,
  429. packageName: ctx.ModuleDir(m),
  430. content: fmt.Sprintf(
  431. soongModuleTargetTemplate,
  432. targetName,
  433. ctx.ModuleName(m),
  434. canonicalizeModuleType(ctx.ModuleType(m)),
  435. ctx.ModuleSubDir(m),
  436. depLabelList,
  437. attributes),
  438. }, err
  439. }
  440. func getBuildProperties(ctx bpToBuildContext, m blueprint.Module) (BazelAttributes, error) {
  441. // TODO: this omits properties for blueprint modules (blueprint_go_binary,
  442. // bootstrap_go_binary, bootstrap_go_package), which will have to be handled separately.
  443. if aModule, ok := m.(android.Module); ok {
  444. return extractModuleProperties(aModule.GetProperties(), false)
  445. }
  446. return BazelAttributes{}, nil
  447. }
  448. // Generically extract module properties and types into a map, keyed by the module property name.
  449. func extractModuleProperties(props []interface{}, checkForDuplicateProperties bool) (BazelAttributes, error) {
  450. ret := map[string]string{}
  451. // Iterate over this android.Module's property structs.
  452. for _, properties := range props {
  453. propertiesValue := reflect.ValueOf(properties)
  454. // Check that propertiesValue is a pointer to the Properties struct, like
  455. // *cc.BaseLinkerProperties or *java.CompilerProperties.
  456. //
  457. // propertiesValue can also be type-asserted to the structs to
  458. // manipulate internal props, if needed.
  459. if isStructPtr(propertiesValue.Type()) {
  460. structValue := propertiesValue.Elem()
  461. ok, err := extractStructProperties(structValue, 0)
  462. if err != nil {
  463. return BazelAttributes{}, err
  464. }
  465. for k, v := range ok {
  466. if existing, exists := ret[k]; checkForDuplicateProperties && exists {
  467. return BazelAttributes{}, fmt.Errorf(
  468. "%s (%v) is present in properties whereas it should be consolidated into a commonAttributes",
  469. k, existing)
  470. }
  471. ret[k] = v
  472. }
  473. } else {
  474. return BazelAttributes{},
  475. fmt.Errorf(
  476. "properties must be a pointer to a struct, got %T",
  477. propertiesValue.Interface())
  478. }
  479. }
  480. return BazelAttributes{
  481. Attrs: ret,
  482. }, nil
  483. }
  484. func isStructPtr(t reflect.Type) bool {
  485. return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
  486. }
  487. // prettyPrint a property value into the equivalent Starlark representation
  488. // recursively.
  489. func prettyPrint(propertyValue reflect.Value, indent int, emitZeroValues bool) (string, error) {
  490. if !emitZeroValues && isZero(propertyValue) {
  491. // A property value being set or unset actually matters -- Soong does set default
  492. // values for unset properties, like system_shared_libs = ["libc", "libm", "libdl"] at
  493. // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=281-287;drc=f70926eef0b9b57faf04c17a1062ce50d209e480
  494. //
  495. // In Bazel-parlance, we would use "attr.<type>(default = <default
  496. // value>)" to set the default value of unset attributes. In the cases
  497. // where the bp2build converter didn't set the default value within the
  498. // mutator when creating the BazelTargetModule, this would be a zero
  499. // value. For those cases, we return an empty string so we don't
  500. // unnecessarily generate empty values.
  501. return "", nil
  502. }
  503. switch propertyValue.Kind() {
  504. case reflect.String:
  505. return fmt.Sprintf("\"%v\"", escapeString(propertyValue.String())), nil
  506. case reflect.Bool:
  507. return starlark_fmt.PrintBool(propertyValue.Bool()), nil
  508. case reflect.Int, reflect.Uint, reflect.Int64:
  509. return fmt.Sprintf("%v", propertyValue.Interface()), nil
  510. case reflect.Ptr:
  511. return prettyPrint(propertyValue.Elem(), indent, emitZeroValues)
  512. case reflect.Slice:
  513. elements := make([]string, 0, propertyValue.Len())
  514. for i := 0; i < propertyValue.Len(); i++ {
  515. val, err := prettyPrint(propertyValue.Index(i), indent, emitZeroValues)
  516. if err != nil {
  517. return "", err
  518. }
  519. if val != "" {
  520. elements = append(elements, val)
  521. }
  522. }
  523. return starlark_fmt.PrintList(elements, indent, func(s string) string {
  524. return "%s"
  525. }), nil
  526. case reflect.Struct:
  527. // Special cases where the bp2build sends additional information to the codegenerator
  528. // by wrapping the attributes in a custom struct type.
  529. if attr, ok := propertyValue.Interface().(bazel.Attribute); ok {
  530. return prettyPrintAttribute(attr, indent)
  531. } else if label, ok := propertyValue.Interface().(bazel.Label); ok {
  532. return fmt.Sprintf("%q", label.Label), nil
  533. }
  534. // Sort and print the struct props by the key.
  535. structProps, err := extractStructProperties(propertyValue, indent)
  536. if err != nil {
  537. return "", err
  538. }
  539. if len(structProps) == 0 {
  540. return "", nil
  541. }
  542. return starlark_fmt.PrintDict(structProps, indent), nil
  543. case reflect.Interface:
  544. // TODO(b/164227191): implement pretty print for interfaces.
  545. // Interfaces are used for for arch, multilib and target properties.
  546. return "", nil
  547. case reflect.Map:
  548. if v, ok := propertyValue.Interface().(bazel.StringMapAttribute); ok {
  549. return starlark_fmt.PrintStringStringDict(v, indent), nil
  550. }
  551. return "", fmt.Errorf("bp2build expects map of type map[string]string for field: %s", propertyValue)
  552. default:
  553. return "", fmt.Errorf(
  554. "unexpected kind for property struct field: %s", propertyValue.Kind())
  555. }
  556. }
  557. // Converts a reflected property struct value into a map of property names and property values,
  558. // which each property value correctly pretty-printed and indented at the right nest level,
  559. // since property structs can be nested. In Starlark, nested structs are represented as nested
  560. // dicts: https://docs.bazel.build/skylark/lib/dict.html
  561. func extractStructProperties(structValue reflect.Value, indent int) (map[string]string, error) {
  562. if structValue.Kind() != reflect.Struct {
  563. return map[string]string{}, fmt.Errorf("Expected a reflect.Struct type, but got %s", structValue.Kind())
  564. }
  565. var err error
  566. ret := map[string]string{}
  567. structType := structValue.Type()
  568. for i := 0; i < structValue.NumField(); i++ {
  569. field := structType.Field(i)
  570. if shouldSkipStructField(field) {
  571. continue
  572. }
  573. fieldValue := structValue.Field(i)
  574. if isZero(fieldValue) {
  575. // Ignore zero-valued fields
  576. continue
  577. }
  578. // if the struct is embedded (anonymous), flatten the properties into the containing struct
  579. if field.Anonymous {
  580. if field.Type.Kind() == reflect.Ptr {
  581. fieldValue = fieldValue.Elem()
  582. }
  583. if fieldValue.Type().Kind() == reflect.Struct {
  584. propsToMerge, err := extractStructProperties(fieldValue, indent)
  585. if err != nil {
  586. return map[string]string{}, err
  587. }
  588. for prop, value := range propsToMerge {
  589. ret[prop] = value
  590. }
  591. continue
  592. }
  593. }
  594. propertyName := proptools.PropertyNameForField(field.Name)
  595. var prettyPrintedValue string
  596. prettyPrintedValue, err = prettyPrint(fieldValue, indent+1, false)
  597. if err != nil {
  598. return map[string]string{}, fmt.Errorf(
  599. "Error while parsing property: %q. %s",
  600. propertyName,
  601. err)
  602. }
  603. if prettyPrintedValue != "" {
  604. ret[propertyName] = prettyPrintedValue
  605. }
  606. }
  607. return ret, nil
  608. }
  609. func isZero(value reflect.Value) bool {
  610. switch value.Kind() {
  611. case reflect.Func, reflect.Map, reflect.Slice:
  612. return value.IsNil()
  613. case reflect.Array:
  614. valueIsZero := true
  615. for i := 0; i < value.Len(); i++ {
  616. valueIsZero = valueIsZero && isZero(value.Index(i))
  617. }
  618. return valueIsZero
  619. case reflect.Struct:
  620. valueIsZero := true
  621. for i := 0; i < value.NumField(); i++ {
  622. valueIsZero = valueIsZero && isZero(value.Field(i))
  623. }
  624. return valueIsZero
  625. case reflect.Ptr:
  626. if !value.IsNil() {
  627. return isZero(reflect.Indirect(value))
  628. } else {
  629. return true
  630. }
  631. // Always print bool/strings, if you want a bool/string attribute to be able to take the default value, use a
  632. // pointer instead
  633. case reflect.Bool, reflect.String:
  634. return false
  635. default:
  636. if !value.IsValid() {
  637. return true
  638. }
  639. zeroValue := reflect.Zero(value.Type())
  640. result := value.Interface() == zeroValue.Interface()
  641. return result
  642. }
  643. }
  644. func escapeString(s string) string {
  645. s = strings.ReplaceAll(s, "\\", "\\\\")
  646. // b/184026959: Reverse the application of some common control sequences.
  647. // These must be generated literally in the BUILD file.
  648. s = strings.ReplaceAll(s, "\t", "\\t")
  649. s = strings.ReplaceAll(s, "\n", "\\n")
  650. s = strings.ReplaceAll(s, "\r", "\\r")
  651. return strings.ReplaceAll(s, "\"", "\\\"")
  652. }
  653. func targetNameWithVariant(c bpToBuildContext, logicModule blueprint.Module) string {
  654. name := ""
  655. if c.ModuleSubDir(logicModule) != "" {
  656. // TODO(b/162720883): Figure out a way to drop the "--" variant suffixes.
  657. name = c.ModuleName(logicModule) + "--" + c.ModuleSubDir(logicModule)
  658. } else {
  659. name = c.ModuleName(logicModule)
  660. }
  661. return strings.Replace(name, "//", "", 1)
  662. }
  663. func qualifiedTargetLabel(c bpToBuildContext, logicModule blueprint.Module) string {
  664. return fmt.Sprintf("//%s:%s", c.ModuleDir(logicModule), targetNameWithVariant(c, logicModule))
  665. }