summaryrefslogtreecommitdiff
path: root/fullpkg
blob: 7e75afc87ef88f967528a18f13cfdadb62b91d3d (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
#!/bin/bash
# set -x # uncomment for debug
# Builds packages from ABS recursively. It tries to find dependencies that
# aren't built or need update and then makepkg them in order.

# TODO move __build to chroot

source /etc/makepkg.conf
source /etc/abs.conf
source /etc/libretools.conf

if [ -z $XDG_CONFIG_HOME ]; then # Avoid /libretools dir doesn't exist errors

    error "There's no XDG_CONFIG_HOME var set"; exit 1

elif [ -e $XDG_CONFIG_HOME/libretools/libretools.conf ]; then

    source $XDG_CONFIG_HOME/libretools/libretools.conf

fi

usage() {

    echo "cd to a dir containing a PKGBUILD and run:"
    echo "$0 [options]"
    printf "This script will check dependencies, build them if possible "
    printf "and stage the packages on it's repo."
    echo
    echo "OPTIONS:"
    echo " -h           : this message."
    echo " -a absdir    : set absdir as ABSROOT."
    echo " -b build_dir : use a fullpkg build_dir and only build."
    echo " -c           : check deps only, do not build."
    echo " -d build_dir : use this dir to build. Defaults to mktemp."
    echo " -n           : don't update pacman db."
    echo " -m max_level : check deps until this level"
    echo " -r \"command\" : use this instead of \"$FULLBUILDCMD\""
    echo
    exit 1

}

# Finds a PKGBUILD on toru's path cache
# Look in all caches but pick the first one
# TODO move to a toru flag (-p?)
where_is() {
  grep -m1 "^${1}:" "${TORUPATH}/paths" 2>/dev/null| \
    cut -d: -f2 2>/dev/null
}

# Removes a package from the buildorder
# $1 package name
# $2 buildorder file
remove_buildorder() {
  grep -Evw "${1}" ${2} > ${2}2
  mv -f ${2}2 ${2}

  return $?
}

# Get repo name. Asumes ${ABSROOT}/repo/package/PKGBUILD
guess_repo() {
    basename $(dirname $(pwd))
}

# return : full version spec, including epoch (if necessary), pkgver, pkgrel
#  usage : get_fullver( ${epoch:-0}, $pkgver, $pkgrel )
get_fullver() {
    if [[ $1 -eq 0 ]]; then
# zero epoch case, don't include it in version
        echo $2-$3
    else
        echo $1:$2-$3
    fi

}

# Cleans the build_dir.
cleanup() {
# Do nothing or already cleaned.
    [[ "${do_cleanup}" = false || ! -d ${build_dir} ]] && return 0

# Only do cleanup on level 0.
    msg "Cleaning up..."
    [ $level -eq 0 ] && rm -rf $build_dir/

}

# Checks ABSROOT and look for target pkg deps. Adds them if not built or outdated.
find_deps() {
# Check this level
    source PKGBUILD

# unset PKGBUILD variables
    unset pkgdesc url license groups optdepends provides conflicts replaces \
          backup options install changelog source noextract md5sums build \
          check package
    for _pkg in ${pkgname[@]}; do
      unset package_${_pkg} >/dev/null 2>&1
    done

    local repo=${repo:-$(guess_repo)}
    local pkgbase=${pkgbase:-${pkgname[0]}}
# Provide a default 0 to epoch
    local epoch=${epoch:-0}
    local fullver=$(get_fullver ${epoch} ${pkgver} ${pkgrel})

# Check if the package is already built
    if is_built "${pkgbase}>=${fullver}"; then
# pkg is built and updated
        exit 0
    fi

# greater levels are built first
    echo "${level}:${pkgbase}" >>"${build_dir}/BUILDORDER"
# PKGBUILD is already there
    if [ -d "${build_dir}/${pkgbase}" ]; then
        exit 0
# Copy dir to build_dir
    else
        cp -r ../${pkgbase}/ ${build_dir}/

# to identify repo later
        echo "repo=$repo" > "${build_dir}/${pkgbase}/.INFO"
    fi

# current package plus a space for every level
    msg2 "%${level}s${pkgbase}-${fullver}"

## Check next levels
    declare -i next_level=$level+1

# All deps in separate line, only once, without version.
    deps=($(echo "${depends[@]} ${makedepends[@]}" | \
           sed "s/[=<>]\+[^ ]\+//g" | \
           tr ' ' "\n" | \
           sort -u))

    for _dep in ${deps[@]}; do

        msg2 "Checking ${_dep}"

        local found=false
        local pkgdir=$(where_is ${_dep})

        if [ -d "${pkgdir}" ]; then
          found=true

          pushd "${pkgdir}" > /dev/null
# runs itself on dep's PKGBUILD dir
          $0 -c -d ${build_dir} -l ${next_level}

# probable circular deps
          [ $? -eq 20 ] && return 20
          popd > /dev/null
        else
          error "Not found"
          echo "dep_not_found:$_dep" >>$build_dir/log
        fi

    done

## End variable block

    unset next_level dir
}

__build() {
    pushd ${build_dir} >/dev/null

# greater levels must be built first
    build_packages=($(sort -gr $buildorder | cut -d: -f2))

    while [ ${#build_packages[@]} -ge 1 ]; do
        pushd $build_dir/${build_packages[0]} >/dev/null
        source PKGBUILD

        msg2 "${pkgbase:-${pkgname[0]}} $pkgver-$pkgrel"

        msg2 "Checking for non free deps"
        pkgbuild-check-nonfree || {
# this error means nonfree others means fail.
            if [ $? -eq 15 ]; then

              echo "nonfree:$(basename $PWD)" >>$build_dir/log

# take out package from $buildorder
              remove_buildorder "$(basename $PWD)" $buildorder

# build next package
              continue
            fi
        }

        msg2 "Building $(basename $PWD)"

# this buildcmd is on libretools.conf
        $FULLBUILDCMD; r=$?

        case $r in

## Succesfull build
            0)

                plain "The build was succesful."
                if source .INFO && [ -n $repo ]; then

# Calls a local release script if it's used
                    if [ ! -z $HOOKLOCALRELEASE ]; then
                        find -name "*.pkg.tar.?z" -print0 | xargs -0 $HOOKLOCALRELEASE $repo
                    fi

                    librestage $repo || echo "unstaged:$(basename $PWD)" >>$build_dir/log

                    msg "Updating pacman db and packages"
                    sudo pacman -Sy || true

                fi

                echo "built:$(basename $PWD)" >>$build_dir/log
            ;;

# # Build failed
            *)
                error "There were errors while trying to build the package."
                echo "failed:$(basename $PWD)" >>$build_dir/log
            ;;
        esac

        remove_buildorder "${build_packages[0]}" $buildorder || true

# which is next package?
        build_packages=($(sort -gr $buildorder | cut -d: -f2))
        popd > /dev/null
    done

    pkgs=($(grep "nonfree:" $build_dir/log)) && {
        error "Those packages contain nonfree deps:"
        echo ${pkgs[@]} | tr " " "\n" | cut -d: -f2
    }

    pkgs=($(grep "built:" $build_dir/log)) && {
        msg "Those packages were built and staged:"
        echo ${pkgs[@]} | tr " " "\n" | cut -d: -f2
    }

    pkgs=($(grep "failed:" $build_dir/log)) && {
        error "Those packages failed to build:"
        echo ${pkgs[@]} | tr " " "\n" | cut -d: -f2
    }

    pkgs=($(grep "unstaged:" $build_dir/log)) && {
        error "Those packages couldn't be staged because of missing reponame:"
        echo ${pkgs[@]} | tr " " "\n" | cut -d: -f2
    }

    popd >/dev/null
}

# End inmediately but print a useful message
trap_exit() {
  error "$@"
  warning "Leftover files left on $build_dir"

  exit 1
}

# Trap signals from makepkg
set -E
trap 'cleanup' 0
trap 'trap_exit "(prfullpkg:${level}) TERM signal caught. Exiting..."' TERM HUP QUIT
trap 'trap_exit "(prfullpkg:${level}) Aborted by user! Exiting..."' INT
trap 'trap_exit "(prfullpkg:${level}) An unknown error has occurred. Exiting..."' ERR

force_build=""
level=0
noupdate=false
build_only=false
check_deps_only=false
do_cleanup=false
max_level=21

while getopts 'ha:b:cCd:l:nm:r:' arg; do
    case $arg in
        h) usage ;;
        a) ABSROOT="$OPTARG" ;;
        b) build_only=true
           build_dir="$OPTARG"

           if [ -z "${build_dir}" ]; then
               usage
           fi

           if [ ! -r "${build_dir}/BUILDORDER" ] ; then
               error "${build_dir}/BUILDORDER doesn't exist."
               exit 1
           fi

           ;;
        c) check_deps_only=true ;;
        C) do_cleanup=true;;
        d) build_dir="$OPTARG" ;;
# hidden option to know dep level.
        l) level=$OPTARG ;;
        n) noupdate=true;;
        m) max_level=$OPTARG ;;
        r) FULLBUILDCMD="$OPTARG" ;;
    esac
done

if ! $build_only; then

# Check if we are actually on a build directory. Do this early.
    if [ ! -r PKGBUILD ]; then
        error "This isn't a build directory"
        usage
    fi

# Run the pre build hook
    if [ ! -z "${HOOKPKGBUILDMOD}" ]; then
        ${HOOKPKGBUILDMOD}
    fi

fi

if [ ${level} -eq 0 ]; then

# use -d option or else mktemp
    build_dir="${build_dir:-$(mktemp -d /tmp/fullpkg.XXXXXX)}"

# in case of custom -d option
    if [ ! -d "${build_dir}" ]; then
        mkdir -p "${build_dir}"
    else
# files already there can screw find_deps
        cleanup
    fi

# make files for log and buildorder
    touch "${build_dir}"/{log,BUILDORDER}
    buildorder="${build_dir}/BUILDORDER"

    if ! noupdate; then

        msg "Updating pacman db"
        sudo pacman -Sy --noconfirm || true

    fi

    if $build_only; then

        msg "Building Packages"

        __build

        exit 0

    fi

    msg "Checking dependencies"
fi

# Probable circular deps
[ $level -ge $max_level ] && exit 20

# Find the dependencies on the ABS itself
find_deps || {

# Probable circular deps
    if [ $? -eq 20 ]; then

# Show error only on level 0
       if [ $level -eq 0 ]; then
           error "Check for circular deps on $build_dir/BUILDORDER";
       fi

    fi
# Pass message 20
    exit 20
}

# only build on level 0
if (( check_deps_only )) || [ $level -gt 0 ]; then
   exit 0
fi

# Build the packages
msg "Building packages:"
__build

echo
msg2 "Check if your system works fine and librerelease if it does."

exit 0