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. |
| 61 | Schema *string |
| 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 | |
| 74 | func (p *prebuiltEtcXml) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 75 | p.PrebuiltEtc.DepsMutator(ctx) |
| 76 | |
| 77 | // To support ":modulename" in schema |
| 78 | android.ExtractSourceDeps(ctx, p.properties.Schema) |
| 79 | } |
| 80 | |
| 81 | func (p *prebuiltEtcXml) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 82 | p.PrebuiltEtc.GenerateAndroidBuildActions(ctx) |
| 83 | |
| 84 | if p.properties.Schema != nil { |
| 85 | schema := ctx.ExpandSource(proptools.String(p.properties.Schema), "schema") |
| 86 | |
| 87 | switch schema.Ext() { |
| 88 | case ".dtd": |
| 89 | ctx.Build(pctx, android.BuildParams{ |
| 90 | Rule: xmllintDtd, |
| 91 | Description: "xmllint-dtd", |
| 92 | Input: p.PrebuiltEtc.SourceFilePath(ctx), |
| 93 | Output: p.timestampFilePath(ctx), |
| 94 | Implicit: schema, |
| 95 | Args: map[string]string{ |
| 96 | "dtd": schema.String(), |
| 97 | }, |
| 98 | }) |
| 99 | break |
| 100 | case ".xsd": |
| 101 | ctx.Build(pctx, android.BuildParams{ |
| 102 | Rule: xmllintXsd, |
| 103 | Description: "xmllint-xsd", |
| 104 | Input: p.PrebuiltEtc.SourceFilePath(ctx), |
| 105 | Output: p.timestampFilePath(ctx), |
| 106 | Implicit: schema, |
| 107 | Args: map[string]string{ |
| 108 | "xsd": schema.String(), |
| 109 | }, |
| 110 | }) |
| 111 | break |
| 112 | default: |
| 113 | ctx.PropertyErrorf("schema", "not supported extension: %q", schema.Ext()) |
| 114 | } |
| 115 | } else { |
| 116 | // when schema is not specified, just check if the xml is well-formed |
| 117 | ctx.Build(pctx, android.BuildParams{ |
| 118 | Rule: xmllintMinimal, |
| 119 | Description: "xmllint-minimal", |
| 120 | Input: p.PrebuiltEtc.SourceFilePath(ctx), |
| 121 | Output: p.timestampFilePath(ctx), |
| 122 | }) |
| 123 | } |
| 124 | |
| 125 | p.SetAdditionalDependencies([]android.Path{p.timestampFilePath(ctx)}) |
| 126 | } |
| 127 | |
| 128 | func PrebuiltEtcXmlFactory() android.Module { |
| 129 | module := &prebuiltEtcXml{} |
| 130 | module.AddProperties(&module.properties) |
| 131 | |
| 132 | android.InitPrebuiltEtcModule(&module.PrebuiltEtc) |
| 133 | // This module is device-only |
| 134 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 135 | return module |
| 136 | } |