summaryrefslogtreecommitdiff
path: root/pkg/rpm_pfg.bzl
blob: 0ccdbaf86cbc2e0c8be70ecadd7f7308de75eca9 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
# Copyright 2019 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.

"""Provides rules for creating RPM packages via pkg_filegroup and friends.

pkg_rpm() depends on the existence of an rpmbuild toolchain. Many users will
find to convenient to use the one provided with their system. To enable that
toolchain add the following stanza to WORKSPACE:

```
# Find rpmbuild if it exists.
load("@rules_pkg//toolchains/rpm:rpmbuild_configure.bzl", "find_system_rpmbuild")
find_system_rpmbuild(name="rules_pkg_rpmbuild")
```
"""

load(
    "//pkg:providers.bzl",
    "PackageDirsInfo",
    "PackageFilegroupInfo",
    "PackageFilesInfo",
    "PackageSymlinkInfo",
    "PackageVariablesInfo",
)
load("//pkg/private:util.bzl", "setup_output_files", "substitute_package_variables")

rpm_filetype = [".rpm"]

spec_filetype = [".spec", ".spec.in", ".spec.tpl"]

# TODO(nacl): __install, __cp
# {0} is the source, {1} is the dest
#
# TODO(nacl, #292): cp -r does not do the right thing with TreeArtifacts
_INSTALL_FILE_STANZA_FMT = """
install -d "%{{buildroot}}/$(dirname '{1}')"
cp '{0}' '%{{buildroot}}/{1}'
""".strip()

# TODO(nacl): __install
# {0} is the directory name
#
# This may not be strictly necessary, given that they'll be created in the
# CPIO when rpmbuild processes the `%files` list.
_INSTALL_DIR_STANZA_FMT = """
install -d '%{{buildroot}}/{0}'
""".strip()

# {0} is the name of the link, {1} is the target, {2} is the desired symlink "mode".
#
# In particular, {2} exists because umasks on symlinks apply on macOS, unlike
# Linux.  You can't even change symlink permissions in Linux; all permissions
# apply to the target instead.
#
# This is not the case in BSDs and macOS.  This comes up because rpmbuild(8)
# does not know about the BSD "lchmod" call, which would otherwise be used to
# set permissions.
#
# This is primarily to ensure that tests pass.  Actually attempting to build
# functional RPMs on macOS in rules_pkg has not yet been attempted at any scale.
#
# XXX: This may not apply all that well to users of cygwin and mingw.  We'll
# deal with that when the time comes.
_INSTALL_SYMLINK_STANZA_FMT = """
%{{__install}} -d "%{{buildroot}}/$(dirname '{0}')"
%{{__ln_s}} '{1}' '%{{buildroot}}/{0}'
%if "%_host_os" != "linux"
    %{{__chmod}} -h {2} '%{{buildroot}}/{0}'
%endif
""".strip()

# {0} is the file tag, {1} is the the path to file
_FILE_MODE_STANZA_FMT = """
{0} "{1}"
""".strip()

def _package_contents_metadata(origin_label, grouping_label):
    """Named construct for helping to identify conflicting packaged contents"""
    return struct(
        origin = origin_label if origin_label else "<UNKNOWN>",
        group = grouping_label,
    )

def _conflicting_contents_error(destination, from1, from2, attr_name = "srcs"):
    real_from1_origin = "<UNKNOWN>" if not from1.origin else from1.origin
    real_from1_group = "directly" if not from1.group else "from group {}".format(from1.group)
    real_from2_origin = "<UNKNOWN>" if not from2.origin else from2.origin
    real_from2_group = "directly" if not from2.group else "from group {}".format(from2.group)

    message = """Destination {destination} is provided by both (1) {from1_origin} and (2) {from2_origin}; please ensure that each destination is provided by exactly one input.

    (1) {from1_origin} is provided {from1_group}
    (2) {from2_origin} is provided {from2_group}
    """.format(
        destination = destination,
        from1_origin = real_from1_origin,
        from1_group = real_from1_group,
        from2_origin = real_from2_origin,
        from2_group = real_from2_group,
    )

    fail(message, attr_name)

def _make_filetags(attributes, default_filetag = None):
    """Helper function for rendering RPM spec file tags, like

    ```
    %attr(0755, root, root) %dir
    ```
    """
    template = "%attr({mode}, {user}, {group}) {supplied_filetag}"

    mode = attributes.get("mode", "-")
    user = attributes.get("user", "-")
    group = attributes.get("group", "-")

    supplied_filetag = attributes.get("rpm_filetag", default_filetag)

    return template.format(
        mode = mode,
        user = user,
        group = group,
        supplied_filetag = supplied_filetag or "",
    )

def _make_absolute_if_not_already_or_is_macro(path):
    # Make a destination path absolute if it isn't already or if it starts with
    # a macro (assumed to be a value that starts with "%").
    #
    # If the user has provided a macro as the installation destination, assume
    # they know what they're doing.  Specifically, the macro needs to resolve to
    # an absolute path.

    # This may not be the fastest way to do this, but if it becomes a problem
    # this can be inlined easily.
    return path if path.startswith(("/", "%")) else "/" + path

#### Input processing helper functions.

# TODO(nacl, #459): These are redundant with functions and structures in
# pkg/private/pkg_files.bzl.  We should really use the infrastructure provided
# there, but as of writing, it's not quite ready.
def _process_files(pfi, origin_label, grouping_label, file_base, dest_check_map, packaged_directories, rpm_files_list, install_script_pieces):
    for dest, src in pfi.dest_src_map.items():
        metadata = _package_contents_metadata(origin_label, grouping_label)
        if dest in dest_check_map:
            _conflicting_contents_error(dest, metadata, dest_check_map[dest])
        else:
            dest_check_map[dest] = metadata

        abs_dest = _make_absolute_if_not_already_or_is_macro(dest)
        if src.is_directory:
            # Set aside TreeArtifact information for external processing
            #
            # @unsorted-dict-items
            packaged_directories.append({
                "src": src,
                "dest": abs_dest,
                # This doesn't exactly make it extensible, but it saves
                # us from having to having to maintain tag processing
                # code in multiple places.
                "tags": file_base,
            })
        else:
            # Files are well-known.  Take care of them right here.
            rpm_files_list.append(_FILE_MODE_STANZA_FMT.format(file_base, abs_dest))
            install_script_pieces.append(_INSTALL_FILE_STANZA_FMT.format(
                src.path,
                abs_dest,
            ))

def _process_dirs(pdi, origin_label, grouping_label, file_base, dest_check_map, _, rpm_files_list, install_script_pieces):
    for dest in pdi.dirs:
        metadata = _package_contents_metadata(origin_label, grouping_label)
        if dest in dest_check_map:
            _conflicting_contents_error(dest, metadata, dest_check_map[dest])
        else:
            dest_check_map[dest] = metadata

        abs_dirname = _make_absolute_if_not_already_or_is_macro(dest)
        rpm_files_list.append(_FILE_MODE_STANZA_FMT.format(file_base, abs_dirname))

        install_script_pieces.append(_INSTALL_DIR_STANZA_FMT.format(
            abs_dirname,
        ))

def _process_symlink(psi, origin_label, grouping_label, file_base, dest_check_map, _, rpm_files_list, install_script_pieces):
    metadata = _package_contents_metadata(origin_label, grouping_label)
    if psi.destination in dest_check_map:
        _conflicting_contents_error(psi.destination, metadata, dest_check_map[psi.destination])
    else:
        dest_check_map[psi.destination] = metadata

    abs_dest = _make_absolute_if_not_already_or_is_macro(psi.destination)
    rpm_files_list.append(_FILE_MODE_STANZA_FMT.format(file_base, abs_dest))
    install_script_pieces.append(_INSTALL_SYMLINK_STANZA_FMT.format(
        abs_dest,
        psi.target,
        psi.attributes["mode"],
    ))

#### Rule implementation

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

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

    if ctx.attr.debug:
        args.append("--debug")

    if ctx.attr.rpmbuild_path:
        args.append("--rpmbuild=" + ctx.attr.rpmbuild_path)

        # buildifier: disable=print
        print("rpmbuild_path is deprecated. See the README for instructions on how" +
              " to migrate to toolchains")
    else:
        toolchain = ctx.toolchains["@rules_pkg//toolchains/rpm:rpmbuild_toolchain_type"].rpmbuild
        if not toolchain.valid:
            fail("The rpmbuild_toolchain is not properly configured: " +
                 toolchain.name)
        if toolchain.path:
            args.append("--rpmbuild=" + toolchain.path)
        else:
            executable_files = toolchain.label[DefaultInfo].files_to_run
            tools.append(executable_files)
            args.append("--rpmbuild=%s" % executable_files.executable.path)

    #### Calculate output file name
    # rpm_name takes precedence over name if provided
    if ctx.attr.package_name:
        rpm_name = ctx.attr.package_name
    else:
        rpm_name = ctx.attr.name

    default_file = ctx.actions.declare_file("{}.rpm".format(rpm_name))

    package_file_name = ctx.attr.package_file_name
    if not package_file_name:
        package_file_name = "%s-%s-%s.%s.rpm" % (
            rpm_name,
            ctx.attr.version,
            ctx.attr.release,
            ctx.attr.architecture,
        )

    _, output_file, _ = setup_output_files(
        ctx,
        package_file_name = package_file_name,
        default_output_file = default_file,
    )

    #### rpm spec "preamble"
    preamble_pieces = []

    preamble_pieces.append("Name: " + rpm_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")

        preamble_pieces.append("Version: ${{VERSION_FROM_FILE}}")
        args.append("--version=@" + ctx.file.version_file.path)
        files.append(ctx.file.version_file)
    elif ctx.attr.version:
        preamble_pieces.append("Version: " + ctx.attr.version)
    else:
        fail("None of the version or version_file attributes were specified")

    # Release can be specified by a file or inlined.
    if ctx.attr.release_file:
        if ctx.attr.release:
            fail("Both release and release_file attributes were specified")

        preamble_pieces.append("Release: ${{RELEASE_FROM_FILE}}")
        args.append("--release=@" + ctx.file.release_file.path)
        files.append(ctx.file.release_file)
    elif ctx.attr.release:
        preamble_pieces.append("Release: " + ctx.attr.release)
    else:
        fail("None of the release or release_file attributes were specified")

    # source_date_epoch is an integer, and Bazel (as of 4.2.2) does not allow
    # you to put "None" as the default for an "int" attribute.  See also
    # https://github.com/bazelbuild/bazel/issues/14434.
    #
    # Since source_date_epoch cannot reasonably be negative, being zero or
    # positive treated the same as existing below.
    if ctx.attr.source_date_epoch_file:
        if ctx.attr.source_date_epoch >= 0:
            fail("Both source_date_epoch and source_date_epoch_file attributes were specified")
        args.append("--source_date_epoch=@" + ctx.file.source_date_epoch_file.path)
        files.append(ctx.file.source_date_epoch_file)
    elif ctx.attr.source_date_epoch >= 0:
        args.append("--source_date_epoch=" + str(ctx.attr.source_date_epoch))

    if ctx.attr.summary:
        preamble_pieces.append("Summary: " + ctx.attr.summary)
    if ctx.attr.url:
        preamble_pieces.append("URL: " + ctx.attr.url)
    if ctx.attr.license:
        preamble_pieces.append("License: " + ctx.attr.license)
    if ctx.attr.group:
        preamble_pieces.append("Group: " + ctx.attr.group)
    if ctx.attr.provides:
        preamble_pieces.extend(["Provides: " + p for p in ctx.attr.provides])
    if ctx.attr.conflicts:
        preamble_pieces.extend(["Conflicts: " + c for c in ctx.attr.conflicts])
    if ctx.attr.obsoletes:
        preamble_pieces.extend(["Obsoletes: " + o for o in ctx.attr.obsoletes])
    if ctx.attr.requires:
        preamble_pieces.extend(["Requires: " + r for r in ctx.attr.requires])
    if ctx.attr.requires_contextual:
        preamble_pieces.extend(
            [
                "Requires({}): {}".format(scriptlet, capability)
                for scriptlet in ctx.attr.requires_contextual.keys()
                for capability in ctx.attr.requires_contextual[scriptlet]
            ],
        )

    # TODO: BuildArch is usually not hardcoded in spec files, unless the package
    # is indeed restricted to a particular CPU architecture, or is actually
    # "noarch".  This will become more of a concern when we start providing
    # source RPMs.
    #
    # In the meantime, this will allow the "architecture" attribute to take
    # effect.
    if ctx.attr.architecture:
        preamble_pieces.append("BuildArch: " + ctx.attr.architecture)

    preamble_file = ctx.actions.declare_file(
        "{}.spec.preamble".format(rpm_name),
    )
    ctx.actions.write(
        output = preamble_file,
        content = substitute_package_variables(ctx, "\n".join(preamble_pieces)),
    )
    files.append(preamble_file)
    args.append("--preamble=" + preamble_file.path)

    #### %description

    if ctx.attr.description_file:
        if ctx.attr.description:
            fail("Both description and description_file attributes were specified")
        description_file = ctx.file.description_file
    elif ctx.attr.description:
        description_file = ctx.actions.declare_file(
            "{}.spec.description".format(rpm_name),
        )
        ctx.actions.write(
            output = description_file,
            content = ctx.attr.description,
        )
    else:
        fail("None of the description or description_file attributes were specified")

    files.append(description_file)
    args.append("--description=" + description_file.path)

    if ctx.attr.changelog:
        files.append(ctx.file.changelog)
        args.append("--changelog=" + ctx.file.changelog.path)

    #### Non-procedurally-generated scriptlets

    substitutions = {}
    if ctx.attr.pre_scriptlet_file:
        if ctx.attr.pre_scriptlet:
            fail("Both pre_scriptlet and pre_scriptlet_file attributes were specified")
        pre_scriptlet_file = ctx.file.pre_scriptlet_file
        files.append(pre_scriptlet_file)
        args.append("--pre_scriptlet=" + pre_scriptlet_file.path)
    elif ctx.attr.pre_scriptlet:
        scriptlet_file = ctx.actions.declare_file(ctx.label.name + ".pre_scriptlet")
        files.append(scriptlet_file)
        ctx.actions.write(scriptlet_file, ctx.attr.pre_scriptlet)
        args.append("--pre_scriptlet=" + scriptlet_file.path)

    if ctx.attr.post_scriptlet_file:
        if ctx.attr.post_scriptlet:
            fail("Both post_scriptlet and post_scriptlet_file attributes were specified")
        post_scriptlet_file = ctx.file.post_scriptlet_file
        files.append(post_scriptlet_file)
        args.append("--post_scriptlet=" + post_scriptlet_file.path)
    elif ctx.attr.post_scriptlet:
        scriptlet_file = ctx.actions.declare_file(ctx.label.name + ".post_scriptlet")
        files.append(scriptlet_file)
        ctx.actions.write(scriptlet_file, ctx.attr.post_scriptlet)
        args.append("--post_scriptlet=" + scriptlet_file.path)

    if ctx.attr.preun_scriptlet_file:
        if ctx.attr.preun_scriptlet:
            fail("Both preun_scriptlet and preun_scriptlet_file attributes were specified")
        preun_scriptlet_file = ctx.file.preun_scriptlet_file
        files.append(preun_scriptlet_file)
        args.append("--preun_scriptlet=" + preun_scriptlet_file.path)
    elif ctx.attr.preun_scriptlet:
        scriptlet_file = ctx.actions.declare_file(ctx.label.name + ".preun_scriptlet")
        files.append(scriptlet_file)
        ctx.actions.write(scriptlet_file, ctx.attr.preun_scriptlet)
        args.append("--preun_scriptlet=" + scriptlet_file.path)

    if ctx.attr.postun_scriptlet_file:
        if ctx.attr.postun_scriptlet:
            fail("Both postun_scriptlet and postun_scriptlet_file attributes were specified")
        postun_scriptlet_file = ctx.file.postun_scriptlet_file
        files.append(postun_scriptlet_file)
        args.append("--postun_scriptlet=" + postun_scriptlet_file.path)
    elif ctx.attr.postun_scriptlet:
        scriptlet_file = ctx.actions.declare_file(ctx.label.name + ".postun_scriptlet")
        files.append(scriptlet_file)
        ctx.actions.write(scriptlet_file, ctx.attr.postun_scriptlet)
        args.append("--postun_scriptlet=" + scriptlet_file.path)

    if ctx.attr.posttrans_scriptlet_file:
        if ctx.attr.posttrans_scriptlet:
            fail("Both posttrans_scriptlet and posttrans_scriptlet_file attributes were specified")
        posttrans_scriptlet_file = ctx.file.posttrans_scriptlet_file
        files.append(posttrans_scriptlet_file)
        args.append("--posttrans_scriptlet=" + posttrans_scriptlet_file.path)
    elif ctx.attr.posttrans_scriptlet:
        scriptlet_file = ctx.actions.declare_file(ctx.label.name + ".posttrans_scriptlet")
        files.append(scriptlet_file)
        ctx.actions.write(scriptlet_file, ctx.attr.posttrans_scriptlet)
        args.append("--posttrans_scriptlet=" + scriptlet_file.path)

    #### Expand the spec file template; prepare data files

    spec_file = ctx.actions.declare_file("%s.spec" % rpm_name)
    ctx.actions.expand_template(
        template = ctx.file.spec_template,
        output = spec_file,
        substitutions = substitutions,
    )
    args.append("--spec_file=" + spec_file.path)
    files.append(spec_file)

    args.append("--out_file=" + output_file.path)

    # Add data files
    files += ctx.files.srcs

    #### Consistency checking; input processing

    # Ensure that no destinations collide.  RPMs that fail this check may be
    # correct, but the output may also create hard-to-debug issues.  Better to
    # err on the side of correctness here.
    dest_check_map = {}

    # The contents of the "%install" scriptlet
    install_script_pieces = []
    if ctx.attr.debug:
        install_script_pieces.append("set -x")

    # The list of entries in the "%files" list
    rpm_files_list = []

    # Directories (TreeArtifacts) are to be treated differently.  Specifically,
    # since Bazel does not know their contents at analysis time, processing them
    # needs to be delegated to a helper script.  This is done via the
    # _treeartifact_helper script used later on.
    packaged_directories = []

    # Iterate over all incoming data, checking for conflicts and creating
    # datasets as we go from the actual contents of the RPM.
    #
    # This is a naive approach to script creation is almost guaranteed to
    # produce an installation script that is longer than necessary.  A better
    # implementation would track directories that are created and ensure that
    # they aren't unnecessarily recreated.
    for dep in ctx.attr.srcs:
        # NOTE: This does not detect cases where directories are not named
        # consistently.  For example, all of these may collide in reality, but
        # won't be detected by the below:
        #
        # 1) usr/lib/libfoo.a
        # 2) /usr/lib/libfoo.a
        # 3) %{_libdir}/libfoo.a
        #
        # The most important thing, regardless of how these checks below are
        # done, is to be consistent with path naming conventions.
        #
        # There is also an unsolved question of determining how to handle
        # subdirectories of "PackageFilesInfo" targets that are actually
        # directories.

        # dep is a Target
        if PackageFilesInfo in dep:
            _process_files(
                dep[PackageFilesInfo],
                dep.label,  # origin label
                None,  # group label
                _make_filetags(dep[PackageFilesInfo].attributes),  # file_base
                dest_check_map,
                packaged_directories,
                rpm_files_list,
                install_script_pieces,
            )

        if PackageDirsInfo in dep:
            _process_dirs(
                dep[PackageDirsInfo],
                dep.label,  # origin label
                None,  # group label
                _make_filetags(dep[PackageDirsInfo].attributes, "%dir"),  # file_base
                dest_check_map,
                packaged_directories,
                rpm_files_list,
                install_script_pieces,
            )

        if PackageSymlinkInfo in dep:
            _process_symlink(
                dep[PackageSymlinkInfo],
                dep.label,  # origin label
                None,  # group label
                _make_filetags(dep[PackageSymlinkInfo].attributes),  # file_base
                dest_check_map,
                packaged_directories,
                rpm_files_list,
                install_script_pieces,
            )

        if PackageFilegroupInfo in dep:
            pfg_info = dep[PackageFilegroupInfo]
            for entry, origin in pfg_info.pkg_files:
                file_base = _make_filetags(entry.attributes)
                _process_files(
                    entry,
                    origin,
                    dep.label,
                    file_base,
                    dest_check_map,
                    packaged_directories,
                    rpm_files_list,
                    install_script_pieces,
                )
            for entry, origin in pfg_info.pkg_dirs:
                file_base = _make_filetags(entry.attributes, "%dir")
                _process_dirs(
                    entry,
                    origin,
                    dep.label,
                    file_base,
                    dest_check_map,
                    packaged_directories,
                    rpm_files_list,
                    install_script_pieces,
                )

            for entry, origin in pfg_info.pkg_symlinks:
                file_base = _make_filetags(entry.attributes)
                _process_symlink(
                    entry,
                    origin,
                    dep.label,
                    file_base,
                    dest_check_map,
                    packaged_directories,
                    rpm_files_list,
                    install_script_pieces,
                )

    #### Procedurally-generated scripts/lists (%install, %files)

    # We need to write these out regardless of whether we are using
    # TreeArtifacts.  That stage will use these files as inputs.
    install_script = ctx.actions.declare_file("{}.spec.install".format(rpm_name))
    ctx.actions.write(
        install_script,
        "\n".join(install_script_pieces),
    )

    rpm_files_file = ctx.actions.declare_file(
        "{}.spec.files".format(rpm_name),
    )
    ctx.actions.write(
        rpm_files_file,
        "\n".join(rpm_files_list),
    )

    # TreeArtifact processing work
    if packaged_directories:
        packaged_directories_file = ctx.actions.declare_file("{}.spec.packaged_directories.json".format(rpm_name))

        packaged_directories_inputs = [d["src"] for d in packaged_directories]

        # This isn't the prettiest thing in the world, but it works.  Bazel
        # needs the "File" data to pass to the command, but "File"s cannot be
        # JSONified.
        #
        # This data isn't used outside of this block, so it's probably fine.
        # Cleaner code would separate the JSONable values from the File type (in
        # a struct, probably).
        for d in packaged_directories:
            d["src"] = d["src"].path

        ctx.actions.write(packaged_directories_file, json.encode(packaged_directories))

        # Overwrite all following uses of the install script and files lists to
        # use the ones generated below.
        install_script_old = install_script
        install_script = ctx.actions.declare_file("{}.spec.install.with_dirs".format(rpm_name))
        rpm_files_file_old = rpm_files_file
        rpm_files_file = ctx.actions.declare_file("{}.spec.files.with_dirs".format(rpm_name))

        input_files = [packaged_directories_file, install_script_old, rpm_files_file_old]
        output_files = [install_script, rpm_files_file]

        helper_args = ctx.actions.args()
        helper_args.add_all(input_files)
        helper_args.add_all(output_files)

        ctx.actions.run(
            executable = ctx.executable._treeartifact_helper,
            use_default_shell_env = True,
            arguments = [helper_args],
            inputs = input_files + packaged_directories_inputs,
            outputs = output_files,
            progress_message = "Generating RPM TreeArtifact Data " + str(ctx.label),
        )

    # And then we're done.  Yay!

    files.append(install_script)
    args.append("--install_script=" + install_script.path)

    files.append(rpm_files_file)
    args.append("--file_list=" + rpm_files_file.path)

    #### Remaining setup

    additional_rpmbuild_args = []
    if ctx.attr.binary_payload_compression:
        additional_rpmbuild_args.extend([
            "--define",
            "_binary_payload {}".format(ctx.attr.binary_payload_compression),
        ])

    for key, value in ctx.attr.defines.items():
        additional_rpmbuild_args.extend([
            "--define",
            "{} {}".format(key, value),
        ])

    args.extend(["--rpmbuild_arg=" + a for a in additional_rpmbuild_args])

    for f in ctx.files.srcs:
        args.append(f.path)

    #### Call the generator script.

    ctx.actions.run(
        mnemonic = "MakeRpm",
        executable = ctx.executable._make_rpm,
        use_default_shell_env = True,
        arguments = args,
        inputs = files,
        outputs = [output_file],
        env = {
            "LANG": "en_US.UTF-8",
            "LC_CTYPE": "UTF-8",
            "PYTHONIOENCODING": "UTF-8",
            "PYTHONUTF8": "1",
        },
        tools = tools,
    )

    changes = []
    if ctx.file.changelog:
        changes = [ctx.file.changelog]

    output_groups = {
        "out": [default_file],
        "rpm": [output_file],
        "changes": changes,
    }
    return [
        OutputGroupInfo(**output_groups),
        DefaultInfo(
            files = depset([output_file]),
        ),
    ]

# Define the rule.
pkg_rpm = rule(
    doc = """Creates an RPM format package via `pkg_filegroup` and friends.

    The uses the outputs of the rules in `mappings.bzl` to construct arbitrary
    RPM packages.  Attributes of this rule provide preamble information and
    scriptlets, which are then used to compose a valid RPM spec file.

    This rule will fail at analysis time if:

    - Any `srcs` input creates the same destination, regardless of other
      attributes.

    This rule only functions on UNIXy platforms. The following tools must be
    available on your system for this to function properly:

    - `rpmbuild` (as specified in `rpmbuild_path`, or available in `$PATH`)

    - GNU coreutils.  BSD coreutils may work, but are not tested.

    To set RPM file attributes (like `%config` and friends), set the
    `rpm_filetag` in corresponding packaging rule (`pkg_files`, etc).  The value
    is prepended with "%" and added to the `%files` list, for example:

    ```
    attrs = {"rpm_filetag": ("config(missingok, noreplace)",)},
    ```

    Is the equivalent to `%config(missingok, noreplace)` in the `%files` list.

    This rule produces 2 artifacts: an .rpm and a .changes file. The DefaultInfo will
    include both. If you need downstream rule to specifically depend on only the .rpm or
    .changes file then you can use `filegroup` to select distinct output groups.

    **OutputGroupInfo**
    - `out` the RPM or a symlink to the actual package.
    - `rpm` the package with any precise file name created with `package_file_name`.
    - `changes` the .changes file.
    """,
    # @unsorted-dict-items
    attrs = {
        "package_name": attr.string(
            doc = """Optional; RPM name override.

            If not provided, the `name` attribute of this rule will be used
            instead.

            This influences values like the spec file name.
            """,
        ),
        "package_file_name": attr.string(
            doc = """See 'Common Attributes' in the rules_pkg reference.

            If this is not provided, the package file given a NVRA-style
            (name-version-release.arch) output, which is preferred by most RPM
            repositories.
            """,
        ),
        "package_variables": attr.label(
            doc = "See 'Common Attributes' in the rules_pkg reference",
            providers = [PackageVariablesInfo],
        ),
        "version": attr.string(
            doc = """RPM "Version" tag.

            Exactly one of `version` or `version_file` must be provided.
            """,
        ),
        "version_file": attr.label(
            doc = """File containing RPM "Version" tag.""",
            allow_single_file = True,
        ),
        "release": attr.string(
            doc = """RPM "Release" tag

            Exactly one of `release` or `release_file` must be provided.
            """,
        ),
        "release_file": attr.label(
            doc = """File containing RPM "Release" tag.""",
            allow_single_file = True,
        ),
        "group": attr.string(
            doc = """Optional; RPM "Group" tag.

            NOTE: some distributions (as of writing, Fedora > 17 and CentOS/RHEL
            > 5) have deprecated this tag.  Other distributions may require it,
            but it is harmless in any case.

            """,
        ),
        "source_date_epoch": attr.int(
            doc = """Value to export as SOURCE_DATE_EPOCH to facilitate reproducible builds

            Implicitly sets the `%clamp_mtime_to_source_date_epoch` in the
            subordinate call to `rpmbuild` to facilitate more consistent in-RPM
            file timestamps.

            Negative values (like the default) disable this feature.
            """,
            default = -1,
        ),
        "source_date_epoch_file": attr.label(
            doc = """File containing the SOURCE_DATE_EPOCH value.

            Implicitly sets the `%clamp_mtime_to_source_date_epoch` in the
            subordinate call to `rpmbuild` to facilitate more consistent in-RPM
            file timestamps.
            """,
            allow_single_file = True,
        ),
        # TODO(nacl): this should be augmented to use bazel platforms, and
        # should not really set BuildArch.
        #
        # TODO(nacl): This, uh, is more required than it looks.  It influences
        # the "A" part of the "NVRA" RPM file name, and RPMs file names look
        # funny if it's not provided.  The contents of the RPM are believed to
        # be set as expected, though.
        "architecture": attr.string(
            doc = """Package architecture.

            This currently sets the `BuildArch` tag, which influences the output
            architecture of the package.

            Typically, `BuildArch` only needs to be set when the package is
            known to be cross-platform (e.g. written in an interpreted
            language), or, less common, when it is known that the application is
            only valid for specific architectures.

            When no attribute is provided, this will default to your host's
            architecture.  This is usually what you want.

            """,
        ),
        "license": attr.string(
            doc = """RPM "License" tag.

            The software license for the code distributed in this package.

            The underlying RPM builder requires you to put something here; if
            your package is not going to be distributed, feel free to set this
            to something like "Internal".

            """,
            mandatory = True,
        ),
        "summary": attr.string(
            doc = """RPM "Summary" tag.

            One-line summary of this package.  Must not contain newlines.

            """,
            mandatory = True,
        ),
        "url": attr.string(
            doc = """RPM "URL" tag; this project/vendor's home on the Internet.""",
        ),
        "description": attr.string(
            doc = """Multi-line description of this package, corresponds to RPM %description.

            Exactly one of `description` or `description_file` must be provided.
            """,
        ),
        "description_file": attr.label(
            doc = """File containing a multi-line description of this package, corresponds to RPM
            %description.""",
            allow_single_file = True,
        ),
        # TODO: this isn't consumed yet
        "changelog": attr.label(
            allow_single_file = True,
        ),
        "srcs": attr.label_list(
            doc = """Mapping groups to include in this RPM.

            These are typically brought into life as `pkg_filegroup`s.
            """,
            mandatory = True,
            providers = [
                [PackageDirsInfo],
                [PackageFilesInfo],
                [PackageFilegroupInfo],
                [PackageSymlinkInfo],
            ],
        ),
        "debug": attr.bool(
            doc = """Debug the RPM helper script and RPM generation""",
            default = False,
        ),
        "pre_scriptlet": attr.string(
            doc = """RPM `%pre` scriptlet.  Currently only allowed to be a shell script.

            `pre_scriptlet` and `pre_scriptlet_file` are mutually exclusive.
            """,
        ),
        "pre_scriptlet_file": attr.label(
            doc = """File containing the RPM `%pre` scriptlet""",
            allow_single_file = True,
        ),
        "post_scriptlet": attr.string(
            doc = """RPM `%post` scriptlet.  Currently only allowed to be a shell script.

            `post_scriptlet` and `post_scriptlet_file` are mutually exclusive.
            """,
        ),
        "post_scriptlet_file": attr.label(
            doc = """File containing the RPM `%post` scriptlet""",
            allow_single_file = True,
        ),
        "preun_scriptlet": attr.string(
            doc = """RPM `%preun` scriptlet.  Currently only allowed to be a shell script.

            `preun_scriptlet` and `preun_scriptlet_file` are mutually exclusive.
            """,
        ),
        "preun_scriptlet_file": attr.label(
            doc = """File containing the RPM `%preun` scriptlet""",
            allow_single_file = True,
        ),
        "postun_scriptlet": attr.string(
            doc = """RPM `%postun` scriptlet.  Currently only allowed to be a shell script.

            `postun_scriptlet` and `postun_scriptlet_file` are mutually exclusive.
            """,
        ),
        "postun_scriptlet_file": attr.label(
            doc = """File containing the RPM `%postun` scriptlet""",
            allow_single_file = True,
        ),
        "posttrans_scriptlet": attr.string(
            doc = """RPM `%posttrans` scriptlet.  Currently only allowed to be a shell script.

            `posttrans_scriptlet` and `posttrans_scriptlet_file` are mutually exclusive.
            """,
        ),
        "posttrans_scriptlet_file": attr.label(
            doc = """File containing the RPM `%posttrans` scriptlet""",
            allow_single_file = True,
        ),
        "conflicts": attr.string_list(
            doc = """List of capabilities that conflict with this package when it is installed.

            Corresponds to the "Conflicts" preamble tag.

            See also: https://rpm-software-management.github.io/rpm/manual/dependencies.html
            """,
        ),
        "provides": attr.string_list(
            doc = """List of rpm capabilities that this package provides.

            Corresponds to the "Provides" preamble tag.

            See also: https://rpm-software-management.github.io/rpm/manual/dependencies.html
            """,
        ),
        "obsoletes": attr.string_list(
            doc = """List of rpm capability expressions that this package obsoletes.

            Corresponds to the "Obsoletes" preamble tag.

            See also: https://rpm-software-management.github.io/rpm/manual/dependencies.html
            """,
	),
        "requires": attr.string_list(
            doc = """List of rpm capability expressions that this package requires.

            Corresponds to the "Requires" preamble tag.

            See also: https://rpm-software-management.github.io/rpm/manual/dependencies.html
            """,
        ),
        "requires_contextual": attr.string_list_dict(
            doc = """Contextualized requirement specifications

            This is a map of various properties (often scriptlet types) to
            capability name specifications, e.g.:

            ```python
            {"pre": ["GConf2"],"post": ["GConf2"], "postun": ["GConf2"]}
            ```

            Which causes the below to be added to the spec file's preamble:

            ```
            Requires(pre): GConf2
            Requires(post): GConf2
            Requires(postun): GConf2
            ```

            This is most useful for ensuring that required tools exist when
            scriptlets are run, although there may be other valid use cases.
            Valid keys for this attribute may include, but are not limited to:

            - `pre`
            - `post`
            - `preun`
            - `postun`
            - `pretrans`
            - `posttrans`

            For capabilities that are always required by packages at runtime,
            use the `requires` attribute instead.

            See also: https://rpm-software-management.github.io/rpm/manual/more_dependencies.html

            NOTE: `pkg_rpm` does not check if the keys of this dictionary are
            acceptable to `rpm(8)`.
            """,
        ),
        "spec_template": attr.label(
            doc = """Spec file template.

            Use this if you need to add additional logic to your spec files that
            is not available by default.

            In most cases, you should not need to override this attribute.
            """,
            allow_single_file = spec_filetype,
            default = "//pkg/rpm:template.spec.tpl",
        ),
        "binary_payload_compression": attr.string(
            doc = """Compression mode used for this RPM

            Must be a form that `rpmbuild(8)` knows how to process, which will
            depend on the version of `rpmbuild` in use.  The value corresponds
            to the `%_binary_payload` macro and is set on the `rpmbuild(8)`
            command line if provided.

            Some examples of valid values (which may not be supported on your
            system) can be found [here](https://git.io/JU9Wg).  On CentOS
            systems (also likely Red Hat and Fedora), you can find some
            supported values by looking for `%_binary_payload` in
            `/usr/lib/rpm/macros`.  Other systems have similar files and
            configurations.

            If not provided, the compression mode will be computed by `rpmbuild`
            itself.  Defaults may vary per distribution or build of `rpm`;
            consult the relevant documentation for more details.

            WARNING: Bazel is currently not aware of action threading requirements
            for non-test actions.  Using threaded compression may result in
            overcommitting your system.
            """,
        ),
        "defines": attr.string_dict(
            doc = """Additional definitions to pass to rpmbuild""",
        ),
        "rpmbuild_path": attr.string(
            doc = """Path to a `rpmbuild` binary.  Deprecated in favor of the rpmbuild toolchain""",
        ),
        # Implicit dependencies.
        "_make_rpm": attr.label(
            default = Label("//pkg:make_rpm"),
            cfg = "exec",
            executable = True,
            allow_files = True,
        ),
        "_treeartifact_helper": attr.label(
            default = Label("//pkg/rpm:augment_rpm_files_install"),
            cfg = "exec",
            executable = True,
            allow_files = True,
        ),
    },
    executable = False,
    implementation = _pkg_rpm_impl,
    toolchains = ["@rules_pkg//toolchains/rpm:rpmbuild_toolchain_type"],
)