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