aboutsummaryrefslogtreecommitdiff
path: root/xbps-src
blob: f8c21b3b92738dcb2256ef461b6f44f9f400305b (plain) (blame)
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
#!/bin/bash
# vim: set ts=4 sw=4 et:

print_cross_targets() {
    local f
    for f in "${XBPS_DISTDIR}"/common/cross-profiles/*.sh; do
        f=${f%.sh}; f=${f##*/}; printf "\t$f\n"
    done
}

usage() {
    # copied from main logic because this is run before XBPS_DISTDIR is set normally
    local _distdir="$(readlink -f "${0%/*}")"
    if [ "${_distdir}" = "." ]; then
        readonly XBPS_DISTDIR="$(pwd -P)"
    else
        readonly XBPS_DISTDIR="${_distdir}"
    fi
    cat << _EOF
$PROGNAME: [options] <target> [arguments]

Targets: (only one may be specified)

binary-bootstrap
    Install bootstrap packages from host repositories into <masterdir>.
    If the optional '-A <arch>' flag is set, it will install bootstrap packages
    from this architecture, and its required xbps utilities. The <masterdir> will
    be initialized for chroot operations.

bootstrap
    Build and install from source the bootstrap packages into <masterdir>.
    If the optional '-A <arch>' flag is set, it will build and install bootstrap
    packages from this architecture, and its required xbps utilities. The <masterdir>
    will be initialized for chroot operations.

bootstrap-update
    Updates bootstrap packages with latest versions available from registered
    repositories in the XBPS configuration file.

consistency-check
    Runs a consistency check on all packages

chroot
    Enter to the chroot in <masterdir>.

clean-repocache
    Removes obsolete packages from <hostdir>/repocache.

fetch <pkgname>
    Download package source distribution file(s).

extract <pkgname>
    Extract package source distribution file(s) into the build directory.
    By default set to <masterdir>/builddir.

patch <pkgname>
    Patch the package sources and perform other operations required to
    prepare a package for configuring and building

configure <pkgname>
    Configure a package (fetch + extract + patch + configure).

build <pkgname>
    Build package source (fetch + extract + patch + configure + build).

check <pkgname>
    Run the package check(s) after building the package source.

install <pkgname>
    Install target package into <destdir> but not building the binary package
    and not removing build directory for inspection purposes.

pkg <pkgname>
    Build binary package for <pkgname> and all required dependencies.

clean [pkgname]
    Removes auto dependencies, cleans up <masterdir>/builddir and <masterdir>/destdir.
    If <pkgname> argument is specified, package files from <masterdir>/destdir and its
    build directory in <masterdir>/buiddir are removed.

list
    Lists installed packages in <masterdir>.

remove <pkgname>
    Remove target package from <destdir>. If <pkgname>-<version> is not matched
    from build template nothing is removed.

remove-autodeps
    Removes all package dependencies that were installed automatically.

purge-distfiles
    Removes all obsolete distfiles in <hostdir>/sources.

show <pkgname>
    Show information for the specified package.

show-avail <pkgname>
    Returns 0 if package can be built for the given architecture,
    any other error otherwise.

show-build-deps <pkgname>
    Show required build dependencies for <pkgname>.

show-check-deps <pkgname>
    Show required check dependencies for <pkgname>.

show-deps <pkgname>
    Show required run-time dependencies for <pkgname>. Package must be
    installed into destdir.

show-files <pkgname>
    Show files installed by <pkgname>. Package must be installed into destdir.

show-hostmakedepends <pkgname>
    Show required host build dependencies for <pkgname>.

show-makedepends <pkgname>
    Show required target build dependencies for <pkgname>.

show-options <pkgname>
    Show available build options by <pkgname>.

show-shlib-provides <pkgname>
    Show list of provided shlibs for <pkgname>. Package must be installed into destdir.

show-shlib-requires <pkgname>
    Show list of required shlibs for <pkgname>. Package must be installed into destdir.

show-var <var>
    Prints the value of <var> if it's defined in xbps-src.

show-repo-updates
    Prints the list of outdated packages in XBPS repositories.

show-sys-updates
    Prints the list of outdated packages in your system.

show-local-updates
    Prints the list of outdated packages in your local repositories.

sort-dependencies <pkg> <pkgN+1> ...
    Given a list of packages specified as additional arguments, a sorted dependency
    list will be returned to stdout.

update-bulk
    Rebuilds all packages in the system repositories that are outdated.

update-sys
    Rebuilds all packages in your system that are outdated and updates them.

update-local
    Rebuilds all packages in your local repositories that are outdated.

update-check <pkgname>
    Check upstream site of <pkgname> for new releases.

update-hash-cache
    Update the hash cache with existing source distfiles.

zap
    Removes a masterdir but preserving ccache, distcc and host directories.

Options:

-1  If dependencies of target package are missing, fail instead of building them.

-A <host>
    Use this host machine. Automatically creates masterdir-<host> if it doesn't
    already exist. Some host machines may require qemu-user-static and
    binfmt-support if not natively supported by the processor. Supported hosts:

$(print_cross_targets)

-a  <target>
    Cross compile packages for this target machine. Supported targets:

$(print_cross_targets)

-b  Build packages even if marked as broken, nocross, or excluded with archs.

-c  <configuration>
    If specified, etc/conf.<configuration> will be used as the primary config
    file name; etc/conf will only be attempted if that does not exist.

-C  Do not remove build directory, automatic dependencies and
    package destdir after successful install.

-E  If a binary package exists in a repository for the target package,
    do not try to build it, exit immediately.

-f  Force running the specified stage (configure/build/install/pkg)
    even if it ran successfully.

-G  Enable XBPS_USE_GIT_REVS (see etc/defaults.conf for more information).

-g  Enable building -dbg packages with debugging symbols.

-H  <hostdir>
    Absolute path to a directory to be bind mounted at <masterdir>/host.
    The host directory stores binary packages, sources and package dependencies
    downloaded from remote repositories.
    If unset defaults to void-packages/hostdir.

-h  Usage output.

-I  Ignore required dependencies, useful for extracting/fetching sources.

-i  Make xbps-src internal errors non-fatal.

-j  Number of parallel build jobs to use when building packages.

-L  Disable ASCII colors.

-m  <masterdir>
    Absolute path to a directory to be used as masterdir.
    The masterdir is the main directory to build/store/compile packages.
    If unset defaults to void-packages/masterdir-<host>.

-N  Disable use of remote repositories to resolve dependencies.

-o  <opt,~opt2,...>
    Enable or disable (prefixed with ~) package build options. If 'etc/conf'
    already specifies some, it is merged. Keep in mind that these options
    apply to all packages within the build, as in if a dependency needs to
    be built, it will inherit these options.

    Supported options can be shown with the 'show-options' target.

-p  <variable,variable2,...>
    For show target, show specified variables in addition to default ones.
    Variable is split and each word is printed in separate line by default.
    In order to print the whole value in one line, append asterisk to variable name.

-Q  Enable running the check stage.

-K  Enable running the check stage with longer tests.

-q  Suppress informational output of xbps-src (build output is still printed).

-r  <repo>
    Use an alternative local repository to store generated binary packages.
    If unset defaults to <hostdir>/binpkgs. If set the binpkgs will
    be stored into <hostdir>/binpkgs/<repo>.
    This alternative repository will also be used to resolve dependencies
    with highest priority order than others.

-s  Make vsed warnings errors.

-t  Create a temporary masterdir to not pollute the current one. Note that
    the existing masterdir must be fully populated with binary-bootstrap first.
    Once the target has finished, this temporary masterdir will be removed.
    This flag requires xbps-uchroot(1), and won't work on filesystems that don't
    support overlayfs.

-v  Show verbose messages (only affects update-check).

-V  Print version of xbps, then exit.

_EOF
}

check_reqhost_utils() {
    local broken

    [ "$IN_CHROOT" ] && return 0

    for f in ${REQHOST_UTILS}; do
        if ! command -v ${f} &>/dev/null; then
            echo "${f} is missing in your system, can't continue!" 1>&2
            broken=1
        fi
    done
    [ "$broken" ] && exit 1
    [ -z "$1" ] && return 0

    for f in ${REQHOST_UTILS_BOOTSTRAP}; do
        if ! command -v ${f} &>/dev/null; then
            echo "${f} is missing in your system, can't continue!" 1>&2
            broken=1
        fi
    done
    [ "$broken" ] && exit 1
}

check_build_requirements() {
    local found

    case "$XBPS_TARGET" in
        *bootstrap*) found=1;;
        *) ;;
    esac
    if [ -z "$found" ]; then
        xbps-uhelper cmpver "$XBPS_VERSION" "$XBPS_VERSION_REQ"
        if [ $? -eq 255 ]; then
            echo "ERROR: requires xbps>=${XBPS_VERSION_REQ}" 1>&2
            echo "Bootstrap packages must be updated with 'xbps-src bootstrap-update'" 1>&2
            exit 1
        fi
    fi
}

chroot_check() {
    if [ -f $XBPS_MASTERDIR/.xbps_chroot_init -o "$XBPS_CHROOT_CMD" = "ethereal" ]; then
        export CHROOT_READY=1
    fi
}

check_native_arch() {
    if [ "$CHROOT_READY" ]; then
        if [ -s $XBPS_MASTERDIR/.xbps_chroot_init ]; then
            export XBPS_ARCH=$(<$XBPS_MASTERDIR/.xbps_chroot_init)
        else
            export XBPS_ARCH=$(xbps-uhelper arch)
        fi
    else
        export XBPS_ARCH="$XBPS_MACHINE"
    fi
}

masterdir_zap() {
    rm -rf "$XBPS_MASTERDIR"
    mkdir -p "$XBPS_MASTERDIR"
    msg_normal "xbps-src: $XBPS_MASTERDIR masterdir cleaned up.\n"
}

exit_func() {
    wait
    if [ "$sourcepkg" ]; then
        remove_pkg $XBPS_CROSS_BUILD
    fi
    if [ -z "$IN_CHROOT" ]; then
        msg_red "xbps-src: a failure has occurred! exiting...\n"
    fi
    exit 2
}

read_pkg() {
    if [ -z "${XBPS_TARGET_PKG}" ]; then
        [ ! -r ./template ] && msg_error "xbps-src: missing build template in $(pwd).\n"
        XBPS_TARGET_PKG=${PWD##*/}
    fi
    setup_pkg "$XBPS_TARGET_PKG" "$XBPS_CROSS_BUILD" "$1"
}

setup_distfiles_mirror() {
    local mirror scheme path

    # Scheme file:// mirror locations only work with uchroot
    for mirror in $XBPS_DISTFILES_MIRROR; do
        scheme="file"
        if [[ "$mirror" == *://* ]]; then
            scheme="${mirror%%://*}"
            path="${mirror#${scheme}://}"
        else
            path="$mirror"
        fi
        [ "$scheme" != "file" ] && continue
        if [ "$XBPS_CHROOT_CMD" == "uchroot" ]; then
            if [ ! -d "$path" ]; then
                msg_warn "xbps-src: Invalid path in XBPS_DISTFILES_MIRROR ($mirror)\n"
                continue
            fi
            mkdir -p "$XBPS_MASTERDIR/$path"
            XBPS_CHROOT_CMD_ARGS+=" -b $path:$path"
        else
            case "$XBPS_TARGET" in
                fetch|extract|patch|configure|build|check|install|pkg|bootstrap|bootstrap-update|update-sys)
                    msg_warn "xbps-src: File URLs ($mirror) don't work with '$XBPS_CHROOT_CMD'\n"
            esac
        fi
    done
}

#
# main()
#
readonly PROGNAME="${0##*/}"
readonly XBPS_VERSION_REQ="0.55"
XBPS_VERSION=$(xbps-uhelper -V)
XBPS_VERSION=${XBPS_VERSION%%API*}
XBPS_VERSION=${XBPS_VERSION##*:}
readonly XBPS_SRC_VERSION="113"
export XBPS_MACHINE=$(xbps-uhelper -C /dev/null arch)

XBPS_OPTIONS=
XBPS_OPTSTRING="1A:a:bc:CEfgGhH:iIj:Lm:No:p:qsQKr:tvV"

# Preprocess arguments in order to allow options before and after XBPS_TARGET.
eval set -- $(getopt "$XBPS_OPTSTRING" "$@");

# Options are saved as XBPS_ARG_FOO instead of XBPS_FOO for now; this is
# because configuration files may override those and we want arguments to
# take precedence over configuration files
while getopts "$XBPS_OPTSTRING" opt; do
    case $opt in
        1) XBPS_ARG_BUILD_ONLY_ONE_PKG=yes; XBPS_OPTIONS+=" -1";;
        A) XBPS_ARG_MACHINE="$OPTARG"; XBPS_OPTIONS+=" -A $OPTARG";;
        a) XBPS_ARG_CROSS_BUILD="$OPTARG"; XBPS_OPTIONS+=" -a $OPTARG";;
        b) XBPS_ARG_IGNORE_BROKENNESS=yes; XBPS_OPTIONS+=" -b";;
        c) XBPS_ARG_CONFIG="$OPTARG"; XBPS_OPTIONS+=" -c $OPTARG";;
        C) XBPS_ARG_KEEP_ALL=1; XBPS_OPTIONS+=" -C";;
        E) XBPS_ARG_BINPKG_EXISTS=1; XBPS_OPTIONS+=" -E";;
        f) XBPS_ARG_BUILD_FORCEMODE=1; XBPS_OPTIONS+=" -f";;
        G) XBPS_ARG_USE_GIT_REVS=1; XBPS_OPTIONS+=" -G";;
        g) XBPS_ARG_DEBUG_PKGS=1; XBPS_OPTIONS+=" -g";;
        H) XBPS_ARG_HOSTDIR="$OPTARG"; XBPS_OPTIONS+=" -H $OPTARG";;
        h) usage && exit 0;;
        i) XBPS_ARG_INFORMATIVE_RUN=1; XBPS_OPTIONS+=" -i";;
        I) XBPS_ARG_SKIP_DEPS=1; XBPS_SKIP_REMOTEREPOS=1; XBPS_OPTIONS+=" -I -N";;
        j) XBPS_ARG_MAKEJOBS="$OPTARG"; XBPS_OPTIONS+=" -j $OPTARG";;
        L) export NOCOLORS=1; XBPS_OPTIONS+=" -L";;
        m) XBPS_ARG_MASTERDIR="$OPTARG"; XBPS_OPTIONS+=" -m $OPTARG";;
        N) XBPS_ARG_SKIP_REMOTEREPOS=1; XBPS_OPTIONS+=" -N";;
        o) XBPS_ARG_PKG_OPTIONS="$OPTARG"; XBPS_OPTIONS+=" -o $OPTARG";;
        p) XBPS_ARG_PRINT_VARIABLES="$OPTARG"; XBPS_OPTIONS+=" -p $OPTARG";;
        q) XBPS_ARG_QUIET=1; XBPS_OPTIONS+=" -q";;
        Q) XBPS_ARG_CHECK_PKGS=yes; XBPS_OPTIONS+=" -Q";;
        K) XBPS_ARG_CHECK_PKGS=full; XBPS_OPTIONS+=" -K";;
        r) XBPS_ARG_ALT_REPOSITORY="$OPTARG"; XBPS_OPTIONS+=" -r $OPTARG";;
        s) XBPS_ARG_STRICT=yes; XBPS_OPTIONS+=" -s";;
        t) XBPS_ARG_TEMP_MASTERDIR=1; XBPS_OPTIONS+=" -t -C";;
        v) XBPS_ARG_VERBOSE=1; XBPS_OPTIONS+=" -v";;
        V) echo "xbps-src-$XBPS_SRC_VERSION $(xbps-uhelper -V)" && exit 0;;
        --) shift; break;;
    esac
done
shift $(($OPTIND - 1))

[ $# -eq 0 ] && usage && exit 1

# Check if stdout is a tty; if false disable colors.
test -t 1 || export NOCOLORS=1
# http://no-color.org
if [ "${NO_COLOR+x}" ]; then
    export NOCOLORS=1
fi

# sane umask
umask 022

#
# Check for required utilities in host system.
#
# Required utilities in host system for the bootstrap target.
readonly REQHOST_UTILS_BOOTSTRAP="file objdump find make gawk bash sed gcc g++ gnat \
    perl bsdtar gzip patch flock pkg-config"

# Required utilities in host.
readonly REQHOST_UTILS="xbps-install xbps-query xbps-rindex xbps-uhelper \
    xbps-reconfigure xbps-remove xbps-create xbps-uchroot xbps-uunshare"

check_reqhost_utils

#
# Set XBPS_CONFIG_FILE, XBPS_DISTDIR, XBPS_MASTERDIR
# and XBPS_HOSTDIR.
#
if [ "$IN_CHROOT" ]; then
    readonly XBPS_CONFIG_FILE=/etc/xbps/xbps-src.conf
    readonly XBPS_DISTDIR=/void-packages
    readonly XBPS_MASTERDIR=/
    readonly XBPS_HOSTDIR=/host
else
    _distdir="$(readlink -f ${0%/*})"
    if [ "${_distdir}" = "." ]; then
        readonly XBPS_DISTDIR="$(pwd -P)"
    else
        readonly XBPS_DISTDIR="${_distdir}"
    fi
    # Read defaults and then the local configuration file
    if [ -f $XBPS_DISTDIR/etc/defaults.conf ]; then
        . $XBPS_DISTDIR/etc/defaults.conf
    fi
    if [ -n "$XBPS_ARG_CONFIG" -a -s $XBPS_DISTDIR/etc/conf.$XBPS_ARG_CONFIG ]; then
        # If specified, read custom user configuration...
        readonly XBPS_CONFIG_FILE=$XBPS_DISTDIR/etc/conf.$XBPS_ARG_CONFIG
    elif [ -s $XBPS_DISTDIR/etc/conf ]; then
        # ... otherwise read generic user configuration...
        readonly XBPS_CONFIG_FILE=$XBPS_DISTDIR/etc/conf
    elif [ -s ${XDG_CONFIG_HOME:-$HOME/.config}/xbps-src.conf ]; then
        readonly XBPS_CONFIG_FILE=${XDG_CONFIG_HOME:-$HOME/.config}/xbps-src.conf
    elif [ -s $HOME/.xbps-src.conf ]; then
        # ... fallback to ~/.xbps-src.conf otherwise.
        readonly XBPS_CONFIG_FILE=$HOME/.xbps-src.conf
    fi
fi
# Read settings from config file
[ -s "$XBPS_CONFIG_FILE" ] && . $XBPS_CONFIG_FILE &>/dev/null

# show colors unconditionally in CI
[ "$XBPS_BUILD_ENVIRONMENT" = void-packages-ci ] && export NOCOLORS=

# Set options passed on command line, after configuration files have been read
[ -n "$XBPS_ARG_BUILD_ONLY_ONE_PKG" ] && XBPS_BUILD_ONLY_ONE_PKG=yes
[ -n "$XBPS_ARG_IGNORE_BROKENNESS" ] && XBPS_IGNORE_BROKENNESS=1
[ -n "$XBPS_ARG_SKIP_REMOTEREPOS" ] && XBPS_SKIP_REMOTEREPOS=1
[ -n "$XBPS_ARG_BUILD_FORCEMODE" ] && XBPS_BUILD_FORCEMODE=1
[ -n "$XBPS_ARG_INFORMATIVE_RUN" ] && XBPS_INFORMATIVE_RUN=1
[ -n "$XBPS_ARG_TEMP_MASTERDIR" ] && XBPS_TEMP_MASTERDIR=1
[ -n "$XBPS_ARG_BINPKG_EXISTS" ] && XBPS_BINPKG_EXISTS=1
[ -n "$XBPS_ARG_USE_GIT_REVS" ] && XBPS_USE_GIT_REVS=1
[ -n "$XBPS_ARG_DEBUG_PKGS" ] && XBPS_DEBUG_PKGS=1
[ -n "$XBPS_ARG_SKIP_DEPS" ] && XBPS_SKIP_DEPS=1
[ -n "$XBPS_ARG_KEEP_ALL" ] && XBPS_KEEP_ALL=1
[ -n "$XBPS_ARG_QUIET" ] && XBPS_QUIET=1
[ -n "$XBPS_ARG_PRINT_VARIABLES" ] && XBPS_PRINT_VARIABLES="$XBPS_ARG_PRINT_VARIABLES"
[ -n "$XBPS_ARG_ALT_REPOSITORY" ] && XBPS_ALT_REPOSITORY="$XBPS_ARG_ALT_REPOSITORY"
[ -n "$XBPS_ARG_STRICT" ] && XBPS_STRICT="$XBPS_ARG_STRICT"
[ -n "$XBPS_ARG_MACHINE" ] && XBPS_MACHINE="$XBPS_ARG_MACHINE"
[ -n "$XBPS_ARG_CROSS_BUILD" ] && XBPS_CROSS_BUILD="$XBPS_ARG_CROSS_BUILD"
[ -n "$XBPS_ARG_CHECK_PKGS" ] && XBPS_CHECK_PKGS="$XBPS_ARG_CHECK_PKGS"
[ -n "$XBPS_ARG_MAKEJOBS" ] && XBPS_MAKEJOBS="$XBPS_ARG_MAKEJOBS"
[ -n "$XBPS_ARG_VERBOSE" ] && XBPS_VERBOSE="$XBPS_ARG_VERBOSE"

# Default to nproc for XBPS_MAKEJOBS if not set and nproc is available
if command -v nproc >/dev/null ; then
    : ${XBPS_MAKEJOBS:=$(nproc)}
else
    : ${XBPS_MAKEJOBS:=1}
fi

export XBPS_BUILD_ONLY_ONE_PKG XBPS_SKIP_REMOTEREPOS XBPS_BUILD_FORCEMODE \
       XBPS_INFORMATIVE_RUN XBPS_TEMP_MASTERDIR XBPS_BINPKG_EXISTS \
       XBPS_USE_GIT_REVS XBPS_CHECK_PKGS XBPS_DEBUG_PKGS XBPS_SKIP_DEPS \
       XBPS_KEEP_ALL XBPS_QUIET XBPS_ALT_REPOSITORY XBPS_STRICT XBPS_CROSS_BUILD \
       XBPS_MAKEJOBS XBPS_PRINT_VARIABLES XBPS_IGNORE_BROKENNESS

# The masterdir/hostdir variables are forced and readonly in chroot
if [ -z "$IN_CHROOT" ]; then
    [ -n "$XBPS_ARG_MASTERDIR" ] && XBPS_MASTERDIR="$XBPS_ARG_MASTERDIR"
    [ -n "$XBPS_ARG_HOSTDIR" ] && XBPS_HOSTDIR="$XBPS_ARG_HOSTDIR"

    # Sanitize masterdir/hostdir once set for real (resolve links)
    export XBPS_MASTERDIR="$(readlink -f $XBPS_MASTERDIR 2>/dev/null)"
    export XBPS_HOSTDIR="$(readlink -f $XBPS_HOSTDIR 2>/dev/null)"
fi

# Forbid root unless XBPS_ALLOW_CHROOT_BREAKOUT is set
# (for CI).
if [ -z "$IN_CHROOT" -a "$UID" -eq 0 -a -z "$XBPS_ALLOW_CHROOT_BREAKOUT" ]; then
    echo "ERROR: xbps-src cannot be used as root." 1>&2
    exit 1
fi

# if XBPS_MASTERDIR unset, defaults to $XBPS_DISTDIR/masterdir-<host machine>
# unless in a container for simplicity of container/CI usage
if [ "$XBPS_CHROOT_CMD" = ethereal ]; then
    : "${XBPS_MASTERDIR:=$XBPS_DISTDIR/masterdir}"
elif [ -n "$XBPS_ARG_MASTERDIR" ]; then
    : # Don't mess with masterdir in argv
else
    : "${XBPS_MASTERDIR:=$XBPS_DISTDIR/masterdir-$XBPS_MACHINE}"
    # but use $XBPS_DISTDIR/masterdir if it exists and the new style doesn't
    if [ ! -d "${XBPS_MASTERDIR}" ] && [ -r "${XBPS_DISTDIR}/masterdir/.xbps_chroot_init" ] \
        && [ "$(cat "${XBPS_DISTDIR}/masterdir/.xbps_chroot_init")" = "$XBPS_MACHINE" ]; then
        XBPS_MASTERDIR="${XBPS_DISTDIR}/masterdir"
    fi
fi
[ ! -d "$XBPS_MASTERDIR" ] && mkdir -p "$XBPS_MASTERDIR"

# if XBPS_HOSTDIR unset, defaults to $XBPS_DISTDIR/hostdir.
: ${XBPS_HOSTDIR:=$XBPS_DISTDIR/hostdir}
[ ! -d $XBPS_HOSTDIR ] && mkdir -p $XBPS_HOSTDIR

if [ -d "$XBPS_MASTERDIR" -a ! -w "$XBPS_MASTERDIR" ]; then
    echo "ERROR: can't write to masterdir $XBPS_MASTERDIR." 1>&2
    exit 1
fi

# Try using chroot-git then git from the host system
XBPS_GIT_CMD="$(PATH="/usr/libexec/chroot-git:$PATH:$XBPS_MASTERDIR/usr/libexec/chroot-git" command -v git 2>/dev/null)"
if [ -n "$XBPS_GIT_CMD" ]; then
    export XBPS_GIT_CMD
elif [ -z "$XBPS_USE_BUILD_MTIME" ] || [ "$XBPS_USE_GIT_REVS" ]; then
    echo "neither chroot-git or git are available in your system!" 1>&2
    exit 1
else
    unset XBPS_GIT_CMD
fi

if [ -n "$XBPS_HOSTDIR" ]; then
    export XBPS_REPOSITORY=$XBPS_HOSTDIR/binpkgs
    readonly XBPS_SRCDISTDIR=$XBPS_HOSTDIR/sources
else
    export XBPS_REPOSITORY=$XBPS_MASTERDIR/host/binpkgs
    readonly XBPS_SRCDISTDIR=$XBPS_MASTERDIR/host/sources
fi

# Set XBPS_REPOSITORY to our current branch.
if [ -z "$XBPS_ALT_REPOSITORY" ]; then
    pushd "$PWD" &>/dev/null
    cd $XBPS_DISTDIR
    _gitbranch="$($XBPS_GIT_CMD symbolic-ref --short HEAD 2>/dev/null)"
    if [ "${_gitbranch}" -a "${_gitbranch}" != "master" ]; then
        export XBPS_ALT_REPOSITORY="${_gitbranch}"
        export XBPS_REPOSITORY="${XBPS_REPOSITORY}/${_gitbranch}"
     fi
     popd &>/dev/null
else
    export XBPS_REPOSITORY="${XBPS_REPOSITORY}/${XBPS_ALT_REPOSITORY}"
fi

readonly XBPS_SRCPKGDIR=$XBPS_DISTDIR/srcpkgs
readonly XBPS_COMMONDIR=$XBPS_DISTDIR/common
readonly XBPS_SHUTILSDIR=$XBPS_COMMONDIR/xbps-src/shutils
readonly XBPS_TRIGGERSDIR=$XBPS_SRCPKGDIR/xbps-triggers/files
readonly XBPS_CROSSPFDIR=$XBPS_COMMONDIR/cross-profiles
readonly XBPS_BUILDSTYLEDIR=$XBPS_COMMONDIR/build-style
readonly XBPS_LIBEXECDIR=$XBPS_COMMONDIR/xbps-src/libexec
readonly XBPS_BUILDHELPERDIR=$XBPS_COMMONDIR/build-helper

readonly XBPS_TARGET="$1"
if [ "$2" ]; then
    XBPS_TARGET_PKG="${2##*/}"
fi

# Check for CHROOT_READY and set up XBPS_ARCH environment var for xbps.
chroot_check
check_native_arch

# test if to use linux32 for 32-bit masterdirs in 64-bit environments
# x86_64, ppc64 (BE) and aarch64 are capable of this, others are not
linux32_check() {
    local hostarch="$1"
    local tgtarch="$2"
    case "$hostarch" in
        x86_64*) if [[ "$tgtarch" == i686* ]]; then return 0; fi ;;
        ppc64le*) if [[ "$tgtarch" == ppcle* ]]; then return 0; fi ;;
        ppc64*)
            case "$tgtarch" in
                ppc64*) return 1 ;;
                ppc*) return 0 ;;
            esac
            ;;
        aarch64*) if [[ "$tgtarch" == armv* ]]; then return 0; fi ;;
    esac
    return 1
}

# Reconfigure pkgs for 32bit on 64-bit systems and reexec itself.
if [ -z "$XBPS_REINIT" -a -s $XBPS_MASTERDIR/.xbps_chroot_init ]; then
    export XBPS_ARCH=${XBPS_ARCH:-$(<$XBPS_MASTERDIR/.xbps_chroot_init)}
    if linux32_check "$XBPS_MACHINE" "$XBPS_ARCH"; then
        # reconfigure pkgs via linux32
        linux32 xbps-reconfigure -r ${XBPS_MASTERDIR} -a &>/dev/null
        # reexec itself via linux32
        export XBPS_REINIT=1
        exec linux32 $0 ${XBPS_OPTIONS} "$@"
    fi
fi
if [ -n "$XBPS_ARCH" ]; then
    export XBPS_MACHINE=$XBPS_ARCH
fi
# At this point if XBPS_TARGET_MACHINE isn't defined we assume
# it's a native build.
if [ -z "$XBPS_TARGET_MACHINE" ]; then
        export XBPS_TARGET_MACHINE=$XBPS_MACHINE
fi

if [ "$IN_CHROOT" ]; then
    readonly XBPS_UHELPER_CMD="xbps-uhelper"
    readonly XBPS_INSTALL_CMD="xbps-install -c /host/repocache-$XBPS_MACHINE"
    readonly XBPS_QUERY_CMD="xbps-query -c /host/repocache-$XBPS_MACHINE"
    readonly XBPS_RECONFIGURE_CMD="xbps-reconfigure"
    readonly XBPS_REMOVE_CMD="xbps-remove"
    readonly XBPS_CHECKVERS_CMD="xbps-checkvers"
    readonly XBPS_DESTDIR=/destdir
    readonly XBPS_BUILDDIR=/builddir
else
    readonly XBPS_UHELPER_CMD="xbps-uhelper -r $XBPS_MASTERDIR"
    readonly XBPS_INSTALL_CMD="xbps-install -c $XBPS_HOSTDIR/repocache-$XBPS_MACHINE -r $XBPS_MASTERDIR -C etc/xbps.d"
    readonly XBPS_QUERY_CMD="xbps-query -c $XBPS_HOSTDIR/repocache-$XBPS_MACHINE -r $XBPS_MASTERDIR -C etc/xbps.d"
    readonly XBPS_RECONFIGURE_CMD="xbps-reconfigure -r $XBPS_MASTERDIR"
    readonly XBPS_REMOVE_CMD="xbps-remove -r $XBPS_MASTERDIR"
    readonly XBPS_CHECKVERS_CMD="xbps-checkvers -r $XBPS_MASTERDIR"
    readonly XBPS_DESTDIR=$XBPS_MASTERDIR/destdir
    readonly XBPS_BUILDDIR=$XBPS_MASTERDIR/builddir
fi
readonly XBPS_RINDEX_CMD="xbps-rindex"
readonly XBPS_FETCH_CMD="xbps-fetch"
readonly XBPS_DIGEST_CMD="xbps-digest"
readonly XBPS_CMPVER_CMD="xbps-uhelper cmpver"

export XBPS_SHUTILSDIR XBPS_CROSSPFDIR XBPS_TRIGGERSDIR \
    XBPS_SRCPKGDIR XBPS_COMMONDIR XBPS_BUILDDIR \
    XBPS_REPOSITORY XBPS_ALT_REPOSITORY XBPS_STRICT XBPS_SRCDISTDIR XBPS_DIGEST_CMD \
    XBPS_UHELPER_CMD XBPS_INSTALL_CMD XBPS_QUERY_CMD XBPS_BUILD_ONLY_ONE_PKG \
    XBPS_RINDEX_CMD XBPS_RECONFIGURE_CMD XBPS_REMOVE_CMD XBPS_CHECKVERS_CMD \
    XBPS_CMPVER_CMD XBPS_FETCH_CMD XBPS_VERSION XBPS_BUILDSTYLEDIR \
    XBPS_CPPFLAGS XBPS_CFLAGS XBPS_CXXFLAGS XBPS_FFLAGS XBPS_LDFLAGS \
    XBPS_MAKEJOBS XBPS_BUILD_FORCEMODE XBPS_USE_GIT_REVS XBPS_DEBUG_PKGS \
    XBPS_CHECK_PKGS XBPS_CCACHE XBPS_DISTCC XBPS_DISTCC_HOSTS XBPS_SKIP_DEPS \
    XBPS_SKIP_REMOTEREPOS XBPS_CROSS_BUILD XBPS_ARG_PKG_OPTIONS XBPS_CONFIG_FILE \
    XBPS_KEEP_ALL XBPS_HOSTDIR XBPS_MASTERDIR XBPS_SRC_VERSION \
    XBPS_DESTDIR XBPS_MACHINE XBPS_TEMP_MASTERDIR XBPS_BINPKG_EXISTS \
    XBPS_LIBEXECDIR XBPS_DISTDIR XBPS_DISTFILES_MIRROR XBPS_ALLOW_RESTRICTED \
    XBPS_USE_GIT_COMMIT_DATE XBPS_PKG_COMPTYPE XBPS_REPO_COMPTYPE \
    XBPS_BUILDHELPERDIR XBPS_USE_BUILD_MTIME XBPS_BUILD_ENVIRONMENT \
    XBPS_PRESERVE_PKGS XBPS_IGNORE_BROKENNESS

for i in REPOSITORY DESTDIR BUILDDIR SRCDISTDIR; do
    eval val="\$XBPS_$i"
    if [ ! -d "$val" ]; then
        mkdir -p $val
    fi
    unset val
done

# A temporary masterdir requires xbps-uchroot(1) and -O to use overlayfs
# on tmpfs (available with xbps-0.45).
if [ -z "$IN_CHROOT" -a -n "$XBPS_TEMP_MASTERDIR" ]; then
    export XBPS_CHROOT_CMD="uchroot"
    export XBPS_CHROOT_CMD_ARGS+=" -O"
fi
#
# Sanitize PATH.
#
if [ -z "$IN_CHROOT" ]; then
    # In non chroot case always prefer host tools.
    export PATH="$PATH:$XBPS_MASTERDIR/usr/bin"
fi

#
# Set up ccache
#
if [ "$XBPS_CCACHE" ]; then
    export CCACHEPATH="/usr/lib/ccache/bin"
    export CCACHE_DIR="$XBPS_HOSTDIR/ccache"
    # Avoid not using cached files just due to compiler mtime
    # changes when e.g. bootstrapping
    export CCACHE_COMPILERCHECK=content CCACHE_COMPRESS=1
    export PATH="$CCACHEPATH:$PATH"
    mkdir -p $CCACHE_DIR
fi

#
# Set up distcc
#
if [ "$XBPS_DISTCC" ]; then
    if [ "$XBPS_CCACHE" ]; then
        export CCACHE_PREFIX="/usr/bin/distcc"
    else
        DISTCCPATH="/usr/lib/distcc/bin"
        export PATH="$DISTCCPATH:$PATH"
    fi
    export DISTCC_DIR="$XBPS_HOSTDIR/distcc-${XBPS_CROSS_BUILD:-${XBPS_MACHINE}}"
    export DISTCC_HOSTS="$XBPS_DISTCC_HOSTS"
    mkdir -p $DISTCC_DIR
fi

check_build_requirements

#
# Read funcs from helpers
#
for f in ${XBPS_SHUTILSDIR}/*.sh; do
    [ -r "$f" ] && . $f
done

if [ -z "$IN_CHROOT" ]; then
    trap 'exit_func' INT TERM
    if [ -n "$XBPS_DISTFILES_MIRROR" ]; then
        setup_distfiles_mirror
    fi
fi

reconfigure_base_chroot

#
# Main switch.
#
case "$XBPS_TARGET" in
    binary-bootstrap)
        if [ -n "$XBPS_TARGET_PKG" ]; then
            msg_error "xbps-src: binary-bootstrap <arch> is no longer supported, use '-A <arch>' instead\n"
            exit 1
        fi
        install_base_chroot "$XBPS_MACHINE"
        ;;
    bootstrap)
        if [ -n "$XBPS_CHECK_PKGS" ]; then
            msg_error "xbps-src: disable tests for bootstrap\n"
            exit 1
        fi
        if [ -n "$XBPS_TARGET_PKG" ]; then
            msg_error "xbps-src: bootstrap <arch> is no longer supported, use '-A <arch>' instead\n"
            exit 1
        fi
        # base-chroot building on host
        # check for required host utils
        check_reqhost_utils bootstrap
        (
            export XBPS_ARCH=$XBPS_MACHINE
            export XBPS_SKIP_REMOTEREPOS=1
            chroot_sync_repodata
            $XBPS_LIBEXECDIR/build.sh \
                base-chroot base-chroot $XBPS_TARGET || exit 1
        ) || exit 1
        [ -d $XBPS_MASTERDIR ] && rm -rf $XBPS_MASTERDIR
        install_base_chroot "$XBPS_MACHINE"
        ;;
    bootstrap-update)
        if [ -n "$CHROOT_READY" -a -z "$IN_CHROOT" ]; then
            chroot_handler bootstrap-update
        else
            chroot_sync_repodata
            update_base_chroot
        fi
        ;;
    chroot)
        chroot_sync_repodata
        chroot_handler chroot dummy
        ;;
    clean)
        if [ -z "$XBPS_TARGET_PKG" ]; then
            if [ -n "$CHROOT_READY" -a -z "$IN_CHROOT" ]; then
                chroot_handler remove-autodeps
            else
                remove_pkg_autodeps
            fi
            msg_normal "xbps-src: cleaning up masterdir...\n"
            # Needed to remove Go Modules
            [ -d "$XBPS_BUILDDIR" ] && chmod -R +wX $XBPS_BUILDDIR
            rm -rf \
                $XBPS_BUILDDIR \
                $XBPS_DESTDIR
            rm -rf $XBPS_MASTERDIR/tmp
            mkdir -p $XBPS_MASTERDIR/tmp
        else
            read_pkg
            if [ -n "$CHROOT_READY" -a -z "$IN_CHROOT" ]; then
                chroot_handler $XBPS_TARGET $XBPS_TARGET_PKG || exit $?
            else
                if declare -f do_clean >/dev/null; then
                    run_func do_clean
                fi
                remove_pkg_wrksrc
                remove_pkg_statedir
            fi
            remove_pkg $XBPS_CROSS_BUILD
        fi
        ;;
    clean-repocache)
        export XBPS_TARGET_ARCH="${XBPS_CROSS_BUILD:-$XBPS_TARGET_MACHINE}"
        $XBPS_REMOVE_CMD -C /dev/null -c $XBPS_HOSTDIR/repocache-${XBPS_TARGET_ARCH} -O
        ;;
    consistency-check)
        consistency_check
        ;;
    remove-autodeps)
        if [ -n "$CHROOT_READY" -a -z "$IN_CHROOT" ]; then
            chroot_handler remove-autodeps
        else
            remove_pkg_autodeps
        fi
        ;;
    fetch|extract|patch|configure|build|check|install|pkg)
        if [ "$XBPS_TARGET" = "check" ] && [ -z "$XBPS_CHECK_PKGS" ]; then
            export XBPS_CHECK_PKGS=full
        fi
        read_pkg
        if [ -n "$CHROOT_READY" -a -z "$IN_CHROOT" ]; then
            chroot_handler $XBPS_TARGET $XBPS_TARGET_PKG
        else
            check_existing_pkg
            chroot_sync_repodata
            # prevent update_base_chroot from removing the builddir/destdir
            update_base_chroot keep-all-force
            $XBPS_LIBEXECDIR/build.sh $XBPS_TARGET_PKG $XBPS_TARGET_PKG \
                $XBPS_TARGET $XBPS_CROSS_BUILD || exit $?
        fi
        ;;
    remove|remove-destdir)
        read_pkg
        remove_pkg $XBPS_CROSS_BUILD
        ;;
    list)
        $XBPS_QUERY_CMD -l
        ;;
    purge-distfiles)
        purge_distfiles
        ;;
    show)
        read_pkg ignore-problems
        show_pkg $XBPS_PRINT_VARIABLES
        ;;
    show-avail)
        read_pkg &>/dev/null
        show_avail
        ;;
    show-files)
        read_pkg ignore-problems
        show_pkg_files
        ;;
    show-deps)
        read_pkg ignore-problems
        show_pkg_deps
        ;;
    show-build-deps)
        read_pkg ignore-problems
        show_pkg_build_deps
        ;;
    show-hostmakedepends)
        read_pkg ignore-problems
        show_pkg_hostmakedepends
        ;;
    show-makedepends)
        read_pkg ignore-problems
        show_pkg_makedepends
        ;;
    show-checkdepends)
        read_pkg ignore-problems
        show_pkg_checkdepends
        ;;
    show-pkg-var-dump)
        read_pkg ignore-problems
        for sub_name in $subpackages; do
            if [ $sub_name = $XBPS_TARGET_PKG ]; then
                . ${XBPS_COMMONDIR}/environment/setup-subpkg/subpkg.sh
                ${sub_name}_package
            fi
        done
        printf "archs: %s\n\n" "$archs"
        printf "bootstrap: %s\n\n" "$bootstrap"
        printf "broken: %s\n\n" "$broken"
        printf "depends: %s\n\n" "$depends"
        printf "hostmakedepends: %s\n\n" "$hostmakedepends"
        printf "makedepends: %s\n\n" "$makedepends"
        printf "nocross: %s\n\n" "$nocross"
        printf "pkgname: %s\n\n" "$pkgname"
        printf "restricted: %s\n\n" "$restricted"
        printf "revision: %s\n\n" "$revision"
        printf "subpackages: %s\n\n" "$subpackages"
        printf "version: %s\n\n" "$version"
        printf "provides: %s\n\n" "$provides"
        printf "lib32disabled: %s\n\n" "$lib32disabled"
        printf "nodebug: %s\n\n" "$nodebug"
        ;;
    show-pkg-var)
        read_pkg ignore-problems
        for sub_name in $subpackages; do
            if [ $sub_name = $XBPS_TARGET_PKG ]; then
                . ${XBPS_COMMONDIR}/environment/setup-subpkg/subpkg.sh
                ${sub_name}_package
            fi
        done
        while IFS= read -r pkg_var; do
                print_var=
                case "${pkg_var}" in
                    archs) print_var="$archs ";;
                    bootstrap) print_var="$bootstrap ";;
                    broken) print_var="$broken ";;
                    depends) print_var="$depends ";;
                    hostmakedepends) print_var="$hostmakedepends ";;
                    makedepends) print_var="$makedepends ";;
                    nocross) print_var="$nocross ";;
                    pkgname) print_var="$pkgname ";;
                    restricted) print_var="$restricted ";;
                    revision) print_var="$revision ";;
                    subpackages) print_var="$subpackages ";;
                    version) print_var="$version ";;
                    provides) print_var="$provides ";;
                    lib32disabled) print_var="$lib32disabled ";;
                    nodebug) print_var="$nodebug ";;
                esac # the space at the end of each is essential for unset vars
                if [ -n "$print_var" ]; then
                        printf "%s\n" "$(printf "${print_var% }" | tr -t '\n\r' ' ')"
                fi # The trailing space gets stripped before printing anyway
        done
        ;;
    dbulk-dump)
        read_pkg
        check_pkg_arch "$XBPS_CROSS_BUILD"
        for x in pkgname version revision; do
            printf '%s: %s\n' "$x" "${!x}"
        done
        for x in bootstrap; do
            [[ ${!x} ]] && printf '%s: %s\n' "$x" "${!x}"
        done
        for x in hostmakedepends makedepends; do
            arr=(${!x})
            if [[ ${#arr} -gt 0 ]]; then
                printf '%s:\n' "$x"
                printf ' %s\n' "${arr[@]}"
            fi
        done
        _cleandeps=$(setup_pkg_depends "" 1 1 | { grep -vF "$(printf "%s\n" $pkgname $subpackages)" || :; } | sort -u) || exit 1
        if [[ $_cleandeps ]]; then
                printf 'depends:\n'
                printf ' %s\n' $_cleandeps
        fi
        if [[ $subpackages ]]; then
            printf 'subpackages:\n'
            printf ' %s\n' $subpackages
        fi
        ;;
    show-options)
        read_pkg ignore-problems
        show_pkg_build_options
        ;;
    show-shlib-provides)
        read_pkg ignore-problems
        show_pkg_shlib_provides
        ;;
    show-shlib-requires)
        read_pkg ignore-problems
        show_pkg_shlib_requires
        ;;
    show-var)
        for f in ${XBPS_COMMONDIR}/environment/setup/*.sh; do
            source $f
        done
        if [ "$XBPS_CROSS_BUILD" ]; then
            source ${XBPS_COMMONDIR}/cross-profiles/${XBPS_CROSS_BUILD}.sh
        else
            source ${XBPS_COMMONDIR}/build-profiles/${XBPS_MACHINE}.sh
        fi
        eval value="\${$XBPS_TARGET_PKG}"
        echo $value
        ;;
    show-repo-updates)
        bulk_build
        ;;
    show-sys-updates)
        bulk_build installed
        ;;
    show-local-updates)
        bulk_build local
        ;;
    sort-dependencies)
        bulk_sortdeps ${@/$XBPS_TARGET/}
        ;;
    update-bulk)
        bulk_update
        ;;
    update-sys)
        bulk_update installed
        ;;
    update-local)
        bulk_update local
        ;;
    update-check)
        read_pkg ignore-problems
        update_check
        ;;
    update-hash-cache)
        update_hash_cache
        ;;
    zap)
        masterdir_zap
        ;;
    *)
        msg_red "xbps-src: invalid target $XBPS_TARGET.\n"
        usage && exit 1
        ;;
esac

exit_and_cleanup $?