build_conversion.go 24 KB

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