blob: 8dc6d2436a95285dbb905debfbfe9af775448a90 [file] [log] [blame]
Lorenz Brunbcae6582021-03-04 17:09:50 +01001# Copyright 2020 The Monogon Project Authors.
2#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Copyright 2018 Josh Pieper, jjp@pobox.com.
18#
19# Licensed under the Apache License, Version 2.0 (the "License");
20# you may not use this file except in compliance with the License.
21# You may obtain a copy of the License at
22#
23# http://www.apache.org/licenses/LICENSE-2.0
24#
25# Unless required by applicable law or agreed to in writing, software
26# distributed under the License is distributed on an "AS IS" BASIS,
27# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28# See the License for the specific language governing permissions and
29# limitations under the License.
30
31def _dictify(data):
32 result = dict([x.split("=", 1) for x in data])
33 return result
34
35def _template_file_impl(ctx):
36 out = ctx.actions.declare_file(ctx.label.name)
37
38 substitutions = dict(ctx.attr.substitutions)
39 substitutions.update(_dictify(ctx.attr.substitution_list))
40
41 ctx.actions.expand_template(
42 template = ctx.files.src[0],
43 output = out,
44 substitutions = substitutions,
45 is_executable = ctx.attr.is_executable,
46 )
47 return [DefaultInfo(
48 files = depset([out]),
49 data_runfiles = ctx.runfiles(files = [out]),
50 )]
51
52template_file = rule(
53 attrs = {
54 "src": attr.label(allow_files = True),
55 "is_executable": attr.bool(default = False),
56 "substitutions": attr.string_dict(),
57 "substitution_list": attr.string_list(),
58 },
59 output_to_genfiles = True,
60 implementation = _template_file_impl,
61)