Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 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 | |
| 15 | package xml |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
Alix | 5918d64 | 2022-06-27 20:57:44 +0000 | [diff] [blame] | 19 | "android/soong/bazel" |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 20 | "android/soong/etc" |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 21 | |
| 22 | "github.com/google/blueprint" |
| 23 | "github.com/google/blueprint/proptools" |
| 24 | ) |
| 25 | |
| 26 | // prebuilt_etc_xml installs an xml file under <partition>/etc/<subdir>. |
| 27 | // It also optionally validates the xml file against the schema. |
| 28 | |
| 29 | var ( |
| 30 | pctx = android.NewPackageContext("android/soong/xml") |
| 31 | |
| 32 | xmllintDtd = pctx.AndroidStaticRule("xmllint-dtd", |
| 33 | blueprint.RuleParams{ |
| 34 | Command: `$XmlLintCmd --dtdvalid $dtd $in > /dev/null && touch -a $out`, |
| 35 | CommandDeps: []string{"$XmlLintCmd"}, |
| 36 | Restat: true, |
| 37 | }, |
| 38 | "dtd") |
| 39 | |
| 40 | xmllintXsd = pctx.AndroidStaticRule("xmllint-xsd", |
| 41 | blueprint.RuleParams{ |
| 42 | Command: `$XmlLintCmd --schema $xsd $in > /dev/null && touch -a $out`, |
| 43 | CommandDeps: []string{"$XmlLintCmd"}, |
| 44 | Restat: true, |
| 45 | }, |
| 46 | "xsd") |
| 47 | |
| 48 | xmllintMinimal = pctx.AndroidStaticRule("xmllint-minimal", |
| 49 | blueprint.RuleParams{ |
| 50 | Command: `$XmlLintCmd $in > /dev/null && touch -a $out`, |
| 51 | CommandDeps: []string{"$XmlLintCmd"}, |
| 52 | Restat: true, |
| 53 | }) |
| 54 | ) |
| 55 | |
| 56 | func init() { |
Paul Duffin | 94b6960 | 2021-03-18 01:32:07 +0000 | [diff] [blame] | 57 | registerXmlBuildComponents(android.InitRegistrationContext) |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 58 | pctx.HostBinToolVariable("XmlLintCmd", "xmllint") |
| 59 | } |
| 60 | |
Paul Duffin | 94b6960 | 2021-03-18 01:32:07 +0000 | [diff] [blame] | 61 | func registerXmlBuildComponents(ctx android.RegistrationContext) { |
| 62 | ctx.RegisterModuleType("prebuilt_etc_xml", PrebuiltEtcXmlFactory) |
| 63 | } |
| 64 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 65 | type prebuiltEtcXmlProperties struct { |
| 66 | // Optional DTD that will be used to validate the xml file. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 67 | Schema *string `android:"path"` |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | type prebuiltEtcXml struct { |
Alix | 5918d64 | 2022-06-27 20:57:44 +0000 | [diff] [blame] | 71 | android.BazelModuleBase |
| 72 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 73 | etc.PrebuiltEtc |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 74 | |
| 75 | properties prebuiltEtcXmlProperties |
| 76 | } |
| 77 | |
| 78 | func (p *prebuiltEtcXml) timestampFilePath(ctx android.ModuleContext) android.WritablePath { |
| 79 | return android.PathForModuleOut(ctx, p.PrebuiltEtc.SourceFilePath(ctx).Base()+"-timestamp") |
| 80 | } |
| 81 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 82 | func (p *prebuiltEtcXml) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 83 | p.PrebuiltEtc.GenerateAndroidBuildActions(ctx) |
| 84 | |
| 85 | if p.properties.Schema != nil { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 86 | schema := android.PathForModuleSrc(ctx, proptools.String(p.properties.Schema)) |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 87 | |
| 88 | switch schema.Ext() { |
| 89 | case ".dtd": |
| 90 | ctx.Build(pctx, android.BuildParams{ |
| 91 | Rule: xmllintDtd, |
| 92 | Description: "xmllint-dtd", |
| 93 | Input: p.PrebuiltEtc.SourceFilePath(ctx), |
| 94 | Output: p.timestampFilePath(ctx), |
| 95 | Implicit: schema, |
| 96 | Args: map[string]string{ |
| 97 | "dtd": schema.String(), |
| 98 | }, |
| 99 | }) |
| 100 | break |
| 101 | case ".xsd": |
| 102 | ctx.Build(pctx, android.BuildParams{ |
| 103 | Rule: xmllintXsd, |
| 104 | Description: "xmllint-xsd", |
| 105 | Input: p.PrebuiltEtc.SourceFilePath(ctx), |
| 106 | Output: p.timestampFilePath(ctx), |
| 107 | Implicit: schema, |
| 108 | Args: map[string]string{ |
| 109 | "xsd": schema.String(), |
| 110 | }, |
| 111 | }) |
| 112 | break |
| 113 | default: |
| 114 | ctx.PropertyErrorf("schema", "not supported extension: %q", schema.Ext()) |
| 115 | } |
| 116 | } else { |
| 117 | // when schema is not specified, just check if the xml is well-formed |
| 118 | ctx.Build(pctx, android.BuildParams{ |
| 119 | Rule: xmllintMinimal, |
| 120 | Description: "xmllint-minimal", |
| 121 | Input: p.PrebuiltEtc.SourceFilePath(ctx), |
| 122 | Output: p.timestampFilePath(ctx), |
| 123 | }) |
| 124 | } |
| 125 | |
| 126 | p.SetAdditionalDependencies([]android.Path{p.timestampFilePath(ctx)}) |
| 127 | } |
| 128 | |
| 129 | func PrebuiltEtcXmlFactory() android.Module { |
| 130 | module := &prebuiltEtcXml{} |
| 131 | module.AddProperties(&module.properties) |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 132 | etc.InitPrebuiltEtcModule(&module.PrebuiltEtc, "etc") |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 133 | // This module is device-only |
Jooyung Han | a017182 | 2019-07-22 15:48:36 +0900 | [diff] [blame] | 134 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst) |
Alix | 5918d64 | 2022-06-27 20:57:44 +0000 | [diff] [blame] | 135 | android.InitBazelModule(module) |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 136 | return module |
| 137 | } |
Alix | 5918d64 | 2022-06-27 20:57:44 +0000 | [diff] [blame] | 138 | |
| 139 | type bazelPrebuiltEtcXmlAttributes struct { |
| 140 | Src bazel.LabelAttribute |
| 141 | Filename bazel.LabelAttribute |
| 142 | Dir string |
| 143 | Installable bazel.BoolAttribute |
| 144 | Filename_from_src bazel.BoolAttribute |
| 145 | Schema *string |
| 146 | } |
| 147 | |
Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame^] | 148 | func (p *prebuiltEtcXml) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { |
Liz Kammer | d5d12d0 | 2023-09-05 09:18:50 -0400 | [diff] [blame] | 149 | baseAttrs, convertible := p.PrebuiltEtc.Bp2buildHelper(ctx) |
| 150 | |
| 151 | if !convertible { |
| 152 | return |
| 153 | } |
Alix | 5918d64 | 2022-06-27 20:57:44 +0000 | [diff] [blame] | 154 | |
| 155 | var schema *string |
| 156 | if p.properties.Schema != nil { |
| 157 | schema = p.properties.Schema |
| 158 | } |
| 159 | |
| 160 | attrs := &bazelPrebuiltEtcXmlAttributes{ |
| 161 | Src: baseAttrs.Src, |
| 162 | Filename: baseAttrs.Filename, |
| 163 | Dir: baseAttrs.Dir, |
| 164 | Installable: baseAttrs.Installable, |
| 165 | Filename_from_src: baseAttrs.Filename_from_src, |
| 166 | Schema: schema, |
| 167 | } |
| 168 | |
| 169 | props := bazel.BazelTargetModuleProperties{ |
| 170 | Rule_class: "prebuilt_xml", |
| 171 | Bzl_load_location: "//build/bazel/rules/prebuilt_xml.bzl", |
| 172 | } |
| 173 | |
| 174 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: p.Name()}, attrs) |
| 175 | } |