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
15package xml
16
17import (
18	"android/soong/android"
19	"android/soong/etc"
20
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.
62	Schema *string `android:"path"`
63}
64
65type prebuiltEtcXml struct {
66	etc.PrebuiltEtc
67
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
75func (p *prebuiltEtcXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
76	p.PrebuiltEtc.GenerateAndroidBuildActions(ctx)
77
78	if p.properties.Schema != nil {
79		schema := android.PathForModuleSrc(ctx, proptools.String(p.properties.Schema))
80
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)
125	etc.InitPrebuiltEtcModule(&module.PrebuiltEtc, "etc")
126	// This module is device-only
127	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
128	return module
129}
130