aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/build_defs/pkg/rpm.bzl
blob: 948889bb820f306d2cfc4684d608be86f4ce7d92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Rules to create RPM archives."""

rpm_filetype = [".rpm"]

spec_filetype = [".spec"]

def _pkg_rpm_impl(ctx):
  """Implements to pkg_rpm rule."""

  files = []
  args = ["--name=" + ctx.label.name]

  # Version can be specified by a file or inlined
  if ctx.attr.version_file:
    if ctx.attr.version:
      fail("Both version and version_file attributes were specified")
    args += ["--version=@" + ctx.file.version_file.path]
    files += [ctx.file.version_file]
  elif ctx.attr.version:
    args += ["--version=" + ctx.attr.version]
  else:
    fail("Neither version_file nor version attribute was specified")

  if ctx.attr.architecture:
    args += ["--arch=" + ctx.attr.architecture]

  if not ctx.attr.spec_file:
    fail("spec_file was not specified")

  # Expand the spec file template.
  spec_file = ctx.actions.declare_file("%s.spec" % ctx.label.name)
  # Create the default substitutions based on the data files.
  substitutions = {}
  for data_file in ctx.files.data:
    key = "{%s}" % data_file.basename
    substitutions[key] = data_file.path
  ctx.actions.expand_template(
    template = ctx.file.spec_file,
    output = spec_file,
    substitutions = substitutions)
  args += ["--spec_file=" + spec_file.path]
  files += [spec_file]

  args += ["--out_file=" + ctx.outputs.rpm.path]

  # Add data files.
  if ctx.file.changelog:
    files += [ctx.file.changelog]
    args += [ctx.file.changelog.path]
  files += ctx.files.data

  for f in ctx.files.data:
    args += [f.path]

  # Call the generator script.
  # TODO(katre): Generate a source RPM.
  ctx.actions.run(
      executable = ctx.executable._make_rpm,
      use_default_shell_env = True,
      arguments = args,
      inputs = files,
      outputs = [ctx.outputs.rpm],
      mnemonic = "MakeRpm")

  # Link the RPM to the expected output name.
  ctx.actions.run(
      executable = "ln",
      arguments = [
        "-s",
        ctx.outputs.rpm.basename,
        ctx.outputs.out.path,
      ],
      inputs = [ctx.outputs.rpm],
      outputs = [ctx.outputs.out])

# Define the rule.
pkg_rpm = rule(
    attrs = {
        "spec_file": attr.label(
            mandatory = True,
            allow_files = spec_filetype,
            single_file = True,
        ),
        "architecture": attr.string(default = "all"),
        "version_file": attr.label(
            allow_files = True,
            single_file = True,
        ),
        "version": attr.string(),
        "changelog": attr.label(
            allow_files = True,
            single_file = True,
        ),
        "data": attr.label_list(
            mandatory = True,
            allow_files = True,
        ),

        # Implicit dependencies.
        "_make_rpm": attr.label(
            default = Label("//tools/build_defs/pkg:make_rpm"),
            cfg = "host",
            executable = True,
            allow_files = True,
        ),
    },
    executable = False,
    outputs = {
        "out": "%{name}.rpm",
        "rpm": "%{name}-%{architecture}.rpm",
    },
    implementation = _pkg_rpm_impl,
)

"""Creates an RPM format package from the data files.

This runs rpmbuild (and requires it to be installed beforehand) to generate
an RPM package based on the spec_file and data attributes.

Args:
  spec_file: The RPM spec file to use. If the version or version_file
    attributes are provided, the Version in the spec will be overwritten.
    Any Sources listed in the spec file must be provided as data dependencies.
    The base names of data dependencies can be replaced with the actual location
    using "{basename}" syntax.
  version: The version of the package to generate. This will overwrite any
    Version provided in the spec file. Only specify one of version and
    version_file.
  version_file: A file containing the version of the package to generate. This
    will overwrite any Version provided in the spec file. Only specify one of
    version and version_file.
  changelog: A changelog file to include. This will not be written to the spec
    file, which should only list changes to the packaging, not the software itself.
  data: List all files to be included in the package here.
"""