xml.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2018 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 xml
  15. import (
  16. "android/soong/android"
  17. "android/soong/bazel"
  18. "android/soong/etc"
  19. "github.com/google/blueprint"
  20. "github.com/google/blueprint/proptools"
  21. )
  22. // prebuilt_etc_xml installs an xml file under <partition>/etc/<subdir>.
  23. // It also optionally validates the xml file against the schema.
  24. var (
  25. pctx = android.NewPackageContext("android/soong/xml")
  26. xmllintDtd = pctx.AndroidStaticRule("xmllint-dtd",
  27. blueprint.RuleParams{
  28. Command: `$XmlLintCmd --dtdvalid $dtd $in > /dev/null && touch -a $out`,
  29. CommandDeps: []string{"$XmlLintCmd"},
  30. Restat: true,
  31. },
  32. "dtd")
  33. xmllintXsd = pctx.AndroidStaticRule("xmllint-xsd",
  34. blueprint.RuleParams{
  35. Command: `$XmlLintCmd --schema $xsd $in > /dev/null && touch -a $out`,
  36. CommandDeps: []string{"$XmlLintCmd"},
  37. Restat: true,
  38. },
  39. "xsd")
  40. xmllintMinimal = pctx.AndroidStaticRule("xmllint-minimal",
  41. blueprint.RuleParams{
  42. Command: `$XmlLintCmd $in > /dev/null && touch -a $out`,
  43. CommandDeps: []string{"$XmlLintCmd"},
  44. Restat: true,
  45. })
  46. )
  47. func init() {
  48. registerXmlBuildComponents(android.InitRegistrationContext)
  49. pctx.HostBinToolVariable("XmlLintCmd", "xmllint")
  50. }
  51. func registerXmlBuildComponents(ctx android.RegistrationContext) {
  52. ctx.RegisterModuleType("prebuilt_etc_xml", PrebuiltEtcXmlFactory)
  53. }
  54. type prebuiltEtcXmlProperties struct {
  55. // Optional DTD that will be used to validate the xml file.
  56. Schema *string `android:"path"`
  57. }
  58. type prebuiltEtcXml struct {
  59. android.BazelModuleBase
  60. etc.PrebuiltEtc
  61. properties prebuiltEtcXmlProperties
  62. }
  63. func (p *prebuiltEtcXml) timestampFilePath(ctx android.ModuleContext) android.WritablePath {
  64. return android.PathForModuleOut(ctx, p.PrebuiltEtc.SourceFilePath(ctx).Base()+"-timestamp")
  65. }
  66. func (p *prebuiltEtcXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  67. p.PrebuiltEtc.GenerateAndroidBuildActions(ctx)
  68. if p.properties.Schema != nil {
  69. schema := android.PathForModuleSrc(ctx, proptools.String(p.properties.Schema))
  70. switch schema.Ext() {
  71. case ".dtd":
  72. ctx.Build(pctx, android.BuildParams{
  73. Rule: xmllintDtd,
  74. Description: "xmllint-dtd",
  75. Input: p.PrebuiltEtc.SourceFilePath(ctx),
  76. Output: p.timestampFilePath(ctx),
  77. Implicit: schema,
  78. Args: map[string]string{
  79. "dtd": schema.String(),
  80. },
  81. })
  82. break
  83. case ".xsd":
  84. ctx.Build(pctx, android.BuildParams{
  85. Rule: xmllintXsd,
  86. Description: "xmllint-xsd",
  87. Input: p.PrebuiltEtc.SourceFilePath(ctx),
  88. Output: p.timestampFilePath(ctx),
  89. Implicit: schema,
  90. Args: map[string]string{
  91. "xsd": schema.String(),
  92. },
  93. })
  94. break
  95. default:
  96. ctx.PropertyErrorf("schema", "not supported extension: %q", schema.Ext())
  97. }
  98. } else {
  99. // when schema is not specified, just check if the xml is well-formed
  100. ctx.Build(pctx, android.BuildParams{
  101. Rule: xmllintMinimal,
  102. Description: "xmllint-minimal",
  103. Input: p.PrebuiltEtc.SourceFilePath(ctx),
  104. Output: p.timestampFilePath(ctx),
  105. })
  106. }
  107. p.SetAdditionalDependencies([]android.Path{p.timestampFilePath(ctx)})
  108. }
  109. func PrebuiltEtcXmlFactory() android.Module {
  110. module := &prebuiltEtcXml{}
  111. module.AddProperties(&module.properties)
  112. etc.InitPrebuiltEtcModule(&module.PrebuiltEtc, "etc")
  113. // This module is device-only
  114. android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
  115. android.InitBazelModule(module)
  116. return module
  117. }
  118. type bazelPrebuiltEtcXmlAttributes struct {
  119. Src bazel.LabelAttribute
  120. Filename bazel.LabelAttribute
  121. Dir string
  122. Installable bazel.BoolAttribute
  123. Filename_from_src bazel.BoolAttribute
  124. Schema *string
  125. }
  126. func (p *prebuiltEtcXml) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
  127. baseAttrs := p.PrebuiltEtc.Bp2buildHelper(ctx)
  128. var schema *string
  129. if p.properties.Schema != nil {
  130. schema = p.properties.Schema
  131. }
  132. attrs := &bazelPrebuiltEtcXmlAttributes{
  133. Src: baseAttrs.Src,
  134. Filename: baseAttrs.Filename,
  135. Dir: baseAttrs.Dir,
  136. Installable: baseAttrs.Installable,
  137. Filename_from_src: baseAttrs.Filename_from_src,
  138. Schema: schema,
  139. }
  140. props := bazel.BazelTargetModuleProperties{
  141. Rule_class: "prebuilt_xml",
  142. Bzl_load_location: "//build/bazel/rules/prebuilt_xml.bzl",
  143. }
  144. ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: p.Name()}, attrs)
  145. }