build_conversion.go 23 KB

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