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" |
| 19 | |
| 20 | "github.com/google/blueprint" |
| 21 | "github.com/google/blueprint/proptools" |
| 22 | ) |
| 23 | |
| 24 | // prebuilt_etc_xml installs an xml file under <partition>/etc/<subdir>. |
| 25 | // It also optionally validates the xml file against the schema. |
| 26 | |
| 27 | var ( |
| 28 | pctx = android.NewPackageContext("android/soong/xml") |
| 29 | |
| 30 | xmllintDtd = pctx.AndroidStaticRule("xmllint-dtd", |
| 31 | blueprint.RuleParams{ |
| 32 | Command: `$XmlLintCmd --dtdvalid $dtd $in > /dev/null && touch -a $out`, |
| 33 | CommandDeps: []string{"$XmlLintCmd"}, |
| 34 | Restat: true, |
| 35 | }, |
| 36 | "dtd") |
| 37 | |
| 38 | xmllintXsd = pctx.AndroidStaticRule("xmllint-xsd", |
| 39 | blueprint.RuleParams{ |
| 40 | Command: `$XmlLintCmd --schema $xsd $in > /dev/null && touch -a $out`, |
| 41 | CommandDeps: []string{"$XmlLintCmd"}, |
| 42 | Restat: true, |
| 43 | }, |
| 44 | "xsd") |
| 45 | |
| 46 | xmllintMinimal = pctx.AndroidStaticRule("xmllint-minimal", |
| 47 | blueprint.RuleParams{ |
| 48 | Command: `$XmlLintCmd $in > /dev/null && touch -a $out`, |
| 49 | CommandDeps: []string{"$XmlLintCmd"}, |
| 50 | Restat: true, |
| 51 | }) |
| 52 | ) |
| 53 | |
| 54 | func init() { |
| 55 | android.RegisterModuleType("prebuilt_etc_xml", PrebuiltEtcXmlFactory) |
| 56 | pctx.HostBinToolVariable("XmlLintCmd", "xmllint") |
| 57 | } |
| 58 | |
| 59 | type prebuiltEtcXmlProperties struct { |
| 60 | // Optional DTD that will be used to validate the xml file. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 61 | Schema *string `android:"path"` |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | type prebuiltEtcXml struct { |
| 65 | android.PrebuiltEtc |
| 66 | |
| 67 | properties prebuiltEtcXmlProperties |
| 68 | } |
| 69 | |
| 70 | func (p *prebuiltEtcXml) timestampFilePath(ctx android.ModuleContext) android.WritablePath { |
| 71 | return android.PathForModuleOut(ctx, p.PrebuiltEtc.SourceFilePath(ctx).Base()+"-timestamp") |
| 72 | } |
| 73 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 74 | func (p *prebuiltEtcXml) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 75 | p.PrebuiltEtc.GenerateAndroidBuildActions(ctx) |
| 76 | |
| 77 | if p.properties.Schema != nil { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 78 | schema := android.PathForModuleSrc(ctx, proptools.String(p.properties.Schema)) |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 79 | |
| 80 | switch schema.Ext() { |
| 81 | case ".dtd": |
| 82 | ctx.Build(pctx, android.BuildParams{ |
| 83 | Rule: xmllintDtd, |
| 84 | Description: "xmllint-dtd", |
| 85 | Input: p.PrebuiltEtc.SourceFilePath(ctx), |
| 86 | Output: p.timestampFilePath(ctx), |
| 87 | Implicit: schema, |
| 88 | Args: map[string]string{ |
| 89 | "dtd": schema.String(), |
| 90 | }, |
| 91 | }) |
| 92 | break |
| 93 | case ".xsd": |
| 94 | ctx.Build(pctx, android.BuildParams{ |
| 95 | Rule: xmllintXsd, |
| 96 | Description: "xmllint-xsd", |
| 97 | Input: p.PrebuiltEtc.SourceFilePath(ctx), |
| 98 | Output: p.timestampFilePath(ctx), |
| 99 | Implicit: schema, |
| 100 | Args: map[string]string{ |
| 101 | "xsd": schema.String(), |
| 102 | }, |
| 103 | }) |
| 104 | break |
| 105 | default: |
| 106 | ctx.PropertyErrorf("schema", "not supported extension: %q", schema.Ext()) |
| 107 | } |
| 108 | } else { |
| 109 | // when schema is not specified, just check if the xml is well-formed |
| 110 | ctx.Build(pctx, android.BuildParams{ |
| 111 | Rule: xmllintMinimal, |
| 112 | Description: "xmllint-minimal", |
| 113 | Input: p.PrebuiltEtc.SourceFilePath(ctx), |
| 114 | Output: p.timestampFilePath(ctx), |
| 115 | }) |
| 116 | } |
| 117 | |
| 118 | p.SetAdditionalDependencies([]android.Path{p.timestampFilePath(ctx)}) |
| 119 | } |
| 120 | |
| 121 | func PrebuiltEtcXmlFactory() android.Module { |
| 122 | module := &prebuiltEtcXml{} |
| 123 | module.AddProperties(&module.properties) |
Jooyung Han | a017182 | 2019-07-22 15:48:36 +0900 | [diff] [blame] | 124 | android.InitPrebuiltEtcModule(&module.PrebuiltEtc, "etc") |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 125 | // This module is device-only |
Jooyung Han | a017182 | 2019-07-22 15:48:36 +0900 | [diff] [blame] | 126 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst) |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 127 | return module |
| 128 | } |