1 # --- T2-COPYRIGHT-BEGIN ---
2 # t2/scripts/functions.in
3 # Copyright (C) 2004 - 2025 The T2 SDE Project
4 # Copyright (C) 1998 - 2003 ROCK Linux Project
5 # SPDX-License-Identifier: GPL-2.0
6 # --- T2-COPYRIGHT-END ---
8 . scripts/core-functions.in
10 # This function returns a "uniqe id" as output
13 date "+%y%m%d.%H%M%S.$$"
16 # this functions expands an string replacing % with all possible values
18 case "$BASH_VERSION" in # damn workaround for different syntax in both versions
21 local string="$1"; shift
22 while [ $# -gt 0 ]; do
23 echo "${string//\\%/$1}"
30 local string="$1"; shift
31 while [ $# -gt 0 ]; do
32 echo "${string//\%/$1}"
39 # atstage <stagename> ...
40 # returns true if the build is on a stage related to the given name
47 [ $stagelevel -gt 0 ] || return 0 ;;
49 [ $stagelevel -lt 1 -o $stagelevel -gt 2 ] || return 0 ;;
51 [ $stagelevel -ne 9 ] || return 0 ;;
53 [ $stagelevel -lt 3 ] || return 0 ;;
55 echo "atstage: '$x' stagename is not supported." >&2
65 local desc_F=" $desc_F " # surounding spaces for match below
67 [ "$arch_sizeof_char_p" = 4 ] && arch2=arch32 || arch2=arch64
68 [ "$desc_F" != "${desc_F/ $1 }" -o \
69 "$desc_F" != "${desc_F/ $1.$arch }" -o "$desc_F" != "${desc_F/ $1.$arch2 }" -o \
70 "$desc_F" != "${desc_F/ $1.$SDECFG_LIBC }" -o \
71 "$desc_F" != "${desc_F/ $1.$SDECFG_DEFAULT_CC }" ]
76 unset hook_functions hook_fcounter
77 declare -a hook_functions='()'
80 # This function adds a code fragment to a named hook with the named priority
82 # hook_add hook_name priority code
85 hook_functions[hook_fcounter]="$3" # declare -a hookidx_$1
86 eval "hookidx_$1[\${#hookidx_$1[@]}]=\"$2 $hook_fcounter\""
87 eval "((hookdirty_$1++))" || true; ((hook_fcounter++)) || true
90 # This function executes all code fragments from the named hook
95 while read pri fnr; do
96 [ "$pri" ] && eval "${hook_functions[fnr]}"
97 done < <(IFS=$'\n'; eval "echo \"\${hookidx_$1[*]}\"" | sort)
98 eval "unset hookdirty_$1"
101 # This function prints all hooks and their current contents
106 for hook in ${!hookidx_*}; do
107 hook=${hook#hookidx_}
108 echo; echo "Contents of hook $hook:"
110 while read pri fnr; do
111 echo; echo " $pri ($fnr)"
112 echo "${hook_functions[fnr]}" | sed 's,^, ,'
114 eval "echo \"\${hookidx_$hook[*]}\"" | sort)
115 if eval "[ -n \"\$hookdirty_\$hook\" ]"; then
116 echo; echo -n " Hook is marked as dirty: "
117 eval "echo \"\${hookdirty_$hook}\""
123 # This function can be used to duplicate a shell-function. E.g. when
124 # overwriting a shell-function but the old one should stay available under
127 # copy_function set_confopt set_confopt_foobar_old
131 # set_confopt_foobar_old "$@"
136 eval "$(declare -f $1 | sed "1 s,$1,$2,")"
141 # convert tabs to spaces, transform multiple consecutive spaces to one,
142 # remove leading and trailing spaces
144 sed 's,\t, ,g; s, *, ,g; s,^[ ]*,,; s,[ ]*$,,'
147 # This function sets the 'confopt' and some other variables.
148 # Re-run it in the package .conf file if you modify $prefix
154 if atstage toolchain; then
158 confopt="--prefix=$z/$prefix"
159 mesonopt="setup objs/ --prefix=$z/$prefix"
160 ninjaopt="-j$SDECFG_PARALLEL"
162 for x in bindir sbindir libdir libexecdir datadir includedir \
163 docdir infodir mandir sysconfdir localstatedir
166 eval "$x=`pkggetdir $x`"
167 # --bindir=$root\$bindir
168 if [ "$x" != libexecdir -a "$x" != docdir ]; then
169 confopt="$confopt --$x=$z\$$x"
170 mesonopt="$mesonopt --$x=$z\$$x"
174 export LIBSUFF=${libdir##*/lib}
176 if [ "$SDECFG_CONFIGURE_OPTS" ]; then
177 var_append confopt " " "$SDECFG_CONFIGURE_OPTS"
180 if [ "$SDECFG_DEBUG" = 0 ]; then
181 var_append confopt " " "--disable-debug"
184 if [ "$SDECFG_DISABLE_NLS" = 1 ]; then
185 var_remove confopt ' ' '--enable-nls'
186 var_append confopt ' ' '--disable-nls'
189 confopt="$confopt \$extraconfopt"
191 atstage cross && var_append confopt ' ' '--with-sysroot=$root'
192 if atstage toolchain; then
193 confopt="$confopt --target=\$arch_target --build=\$arch_build --host=\$arch_build"
195 confopt="$confopt --build=\$arch_build --host=\$arch_target"
200 # eval_config_command $(eval echo $confopt)
202 function eval_config_command() {
205 for x in /usr/share/automake/*; do
206 [ -x "$x" -a -f "$x" ] || continue
208 if [ -L $x -a ! -e $x ]; then
209 echo "Fixing dead symlink $x."
210 ln -sf /usr/share/automake/$x .
214 if atstage cross; then
215 create_config_cache >> config.cache
216 grep -q '.--cache-file=' $configscript &&
217 set -- "$@" "--cache-file=./config.cache"
218 export cache_file=config.cache
221 config_command="$configprefix $configscript"
222 sub_scripts="$(find $(dirname $configscript) -name configure)"
224 if [ "$cleanconfopt" == "0" ]; then
225 config_command="$config_command $@"
227 # remove unsupported config script options
229 if grep -q "[[ ]${x%%=*}[]= ):]" $configscript $sub_scripts; then
230 config_command="$config_command $x"
231 elif [[ $x = --*able-* ]] && egrep -q "\--(en|dis)able-\*" $configscript ||
232 [[ $x = --with* ]] && egrep -q "\--with(|out)-\*" $configscript; then
233 echo "Autodetection for option impossible: " \
235 config_command="$config_command $x"
237 echo "Removing unsupported '$x' from configure option string."
242 echo Running "$config_command"
243 eval "$config_command"
245 # the missing script is generally not very helpful, ...
246 if [ -f build/missing ]; then echo '#!/bin/true' > build/missing; fi
247 if [ -f missing ]; then echo '#!/bin/true' > missing; fi
250 # run 'make check' if Makefile supports it.
252 function run_check() {
253 if grep -q -e "^check:" Makefile; then
254 echo "Running make check"
257 if grep -q -e "^test:" Makefile; then
258 echo "Running make test"
263 # move the static libs from lib to usr/lib and correct the libdir used
264 # inside the .la file
266 postflist_static_lib() {
267 echo "Processing static lib corrections ..."
268 egrep '^(lib|lib64)/.*\.(a|la)$' $builddir/flist.txt |
270 [ -e $root/$fn -o -L $root/$fn ] || continue
271 if [[ $fn = *.a ]]; then
272 mv -fv $root/$fn $root/usr/$fn
274 sed "s,\([ =']\)/lib\(.*\),\1/usr/lib\2,g" \
275 $root/$fn > $root/usr/$fn
277 ln -svf ../../$so $root/usr/$so
278 add_flist $root/usr/$so
281 add_flist $root/usr/$fn
284 # this check might be removed in the future when we decide this is not
285 # an issue anymore ...
286 echo "Verifing the .la files ..."
287 defect_la="`egrep '(lib|lib64)/.*\.la$' $builddir/flist.txt |
288 xargs egrep 'dependency_libs=.*-pthread.*' |
289 cut -d : -f1 | sort -u | tr '\n' ' '`"
290 if [ "$defect_la" ]; then
291 abort "-pthread in: $defect_la!"
294 defect_la="`egrep '(lib|lib64)/.*\.la$' $builddir/flist.txt |
295 xargs egrep "dependency_libs=.*(TOOLCHAIN|BACKUP|$SDECFG_ID).*" |
296 cut -d : -f1 | sort -u | tr '\n' ' '`"
297 if [ "$defect_la" ]; then
299 echo_warning "Detected problems in following libtool files:"
300 for la in $defect_la; do
303 echo_warning "In absence of a proper fix (or replacement) for libtool and friends,"
304 echo_warning "for now the .la files are automatically corrected."
306 # Cleanup dependency_libs, remove build system path
307 local dependency_libs
310 local libsub=${libdir##*/}
311 for la in $defect_la; do
312 eval `grep ^dependency_libs= $root/$la`
314 for deplib in $dependency_libs; do
315 if [ $libsub != lib ]; then
318 deplib="`echo $deplib | sed "s,/lib$,/$libsub,g; s,/lib/,/$libsub/,g"`"
324 *TOOLCHAIN*|*BACKUP*|*$SDECFG_ID*) ;;
326 dlibtmp=$dlibsnew; var_remove dlibtmp ' ' "$deplib"
327 [ "$dlibtmp" = "$dlibsnew" ] && var_append dlibsnew ' ' "$deplib"
333 sed -i "s,^dependency_libs=.*,dependency_libs='$dlibsnew'," $root/$la
339 postflist_fix_cross_scripts() {
340 grep 'bin/' $builddir/flist.txt |
342 if [ -f "$root/$f" ] && grep -qa '#!.*TOOLCHAIN/cross.*bin/' $root/$f; then
343 echo_warning "Sed'ing TOOLCHAIN.*bin in $f"
344 sed -i '1s,^#!.*TOOLCHAIN/cross.*bin/,#!/usr/bin/env -S ,' "$root/$f"
349 # Parse the *.desc file. Use the description from PKG-DESC-FORMAT and
350 # save the tag data in $desc_*.
355 tags="`sed -n '/^\[/ { s/][^]]*$//; s/. ./|/g; s/^\[//; p }' \
356 $base/misc/share/PKG-DESC-FORMAT`"
358 descs=$base/package/*/$1/$1.desc
359 [ -e $base/architecture/$arch/package/$1/$1.desc ] &&
360 var_append descs ' ' $base/architecture/$arch/package/$1/$1.desc
361 [ -e $base/target/$target/package/$pkg/$pkg.desc ] &&
362 var_append descs ' ' $base/target/$target/package/$1/$1.desc
364 for desc in $descs; do
365 #echo_status "Reading $desc ..."
367 tagdata="`egrep -a "^\[($tag)\]" $desc |
368 cut -f2- -d']' | sed 's,^ ,,'`"
369 tag="`echo $tag | cut -f1 -d'|' | tr - _`"
370 # only overwrite defined tags
371 [ "$tagdata" ] && eval "desc_$tag=\"\$tagdata\""
375 ver="`echo "$desc_V" | tail -n 1 | cut -f1 -d' '`"
376 extraver="`echo "$desc_V" | tail -n 1 | cut -s -f2- -d' '`"
377 [ -z "$extraver" ] && extraver="${sdever//DEV-*/DEV}"
380 # This function is used for forcing a file to be in the flist
384 echo "$addfile" | fl_wrparse -D -r "$xroot/" \
385 >> $builddir/flist.txt
389 # This function is used for forcing a package to be in the dependency list
393 echo "$addpackage: add_dependency()" \
394 >> $builddir/dependencies.debug
398 # This function is used to subsitute some important variables
399 # using a D_ prefix thru m4 ...
401 sed -e s,D_prefix,$prefix,g -e s,D_sysconfdir,$sysconfdir,g \
402 -e s,D_docdir,$docdir,g -e s,D_localstatedir,$localstatedir,g \
403 -e s,D_datadir,$datadir,g -e s,D_infodir,$infodir,g \
404 -e s,D_bindir,$bindir,g -e s,D_sbindir,$sbindir,g \
405 -e s,D_libdir,$libdir,g -e s,D_libexecdir,$libexecdir,g \
406 -e s,D_mandir,$mandir,g -e s,D_includedir,$includedir,g \
410 # This outputs a predefined config.cache file as it needed by some
411 # packages to cross-build correctly in stages 0 and 1.
413 create_config_cache() {
414 cat $base/scripts/config.cache
416 if [ $createarchcache -eq 1 ]; then
418 # Architecture specific stuff\n"
421 ac_cv_sizeof_short=$arch_sizeof_short
422 ac_cv_sizeof_int=$arch_sizeof_int
423 ac_cv_sizeof_long=$arch_sizeof_long
424 ac_cv_sizeof_long_long=$arch_sizeof_long_long
425 ac_cv_sizeof_char_p=$arch_sizeof_char_p
426 ac_cv_sizeof_void_p=$arch_sizeof_char_p
427 ac_cv_c_bigendian=$arch_bigendian
432 create_meson_cross() {
439 pkgconfig = 'pkg-config'
440 llvm-config = 'llvm-config'
442 rust = [ 'rustc', '--target', '$rust_target' ]
445 sizeof_int = $arch_sizeof_int
446 sizeof_wchar_t = $arch_sizeof_int
447 sizeof_void* = $arch_sizeof_char_p
449 alignment_void* = $arch_sizeof_int
451 has_function_printf = true
452 needs_exe_wrapper = true
455 system = '$SDECFG_KERNEL'
456 cpu_family = '$(uname -m)'
457 cpu = '$arch' # or uname -m?
459 [ $arch_bigendian == "yes" ] && echo "endian = 'big'" || echo "endian = 'little'"
462 # Generate a CMake toolchain file for cross builds
464 create_cmake_toolchain_file() {
466 SET(CMAKE_SYSTEM_NAME Linux)
467 SET(CMAKE_SYSTEM_VERSION 1)
468 SET(CMAKE_C_COMPILER $CC)
469 SET(CMAKE_CXX_COMPILER $CXX)
470 SET(CMAKE_FIND_ROOT_PATH $root)
471 SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
472 SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
473 SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
474 SET(PACKAGE_VENDOR "T2 SDE")
478 # Abort build before actual build starts
479 # (is overwritten in Build-Pkg)
482 echo -e "The package build aborted with the following config" \
483 "error:\n$*" > $root/var/adm/logs/$stagelevel-$xpkg.err
484 echo_errorquote "$(< $root/var/adm/logs/$stagelevel-$xpkg.err)"
485 echo_pkg_abort $stagelevel $repository $xpkg
493 # Dump $base - only set if there is not already an older value.
495 echo "base=\"\${base:-$base}\""
497 # Dump all variables including their flags, but skip read-only
498 # variables and $base.
500 # Substitute base directory with ${base}.
502 for name in ${!a*} ${!b*} ${!c*} ${!d*} ${!e*} ${!f*} ${!g*} ${!h*} \
503 ${!i*} ${!j*} ${!k*} ${!l*} ${!m*} ${!n*} ${!o*} ${!p*} \
504 ${!q*} ${!r*} ${!s*} ${!t*} ${!u*} ${!v*} ${!w*} ${!x*} \
506 ${!A*} ${!B*} ${!C*} ${!D*} ${!E*} ${!F*} ${!G*} ${!H*} \
507 ${!I*} ${!J*} ${!K*} ${!L*} ${!M*} ${!N*} ${!O*} ${!P*} \
508 ${!Q*} ${!R*} ${!S*} ${!T*} ${!U*} ${!V*} ${!W*} ${!X*} \
511 [ $name = base -o $name = PWD ] && continue
512 if declare -p $name | head -n 1 | grep -qv '^declare -[a-z]*r'
514 declare -p $name | sed "s,\\(^\\|[\"=:]\\)$base\\([\"=:/]\\|\$\\),\\1\${base}\\2,g"
520 declare -f | sed 's/^declare -f //; s/<<(/< <(/;'
527 # Estimate package build time based on binutils reference time and cached build time.
530 local pkg="$1" buildtime
532 [ -e $base/build/$SDECFG_ID/TOOLCHAIN/reftime ] &&
533 local reftime=$(< $base/build/$SDECFG_ID/TOOLCHAIN/reftime)
535 [ -e $base/package/*/$pkg/$pkg.cache ] &&
536 buildtime=$(sed -n "s/\[BUILDTIME\] \([^ ]*\).*/\1/p" $base/package/*/$pkg//$pkg.cache)
537 [ "$buildtime" = 0 ] && buildtime=1 # old quantizised to 0, or just fast: at least something
539 [ "$reftime" -a "$buildtime" ] &&
540 gawk "BEGIN { parallel=.95; buildtime=$reftime * $buildtime / 100 / 100; \
541 time = int(buildtime * (1-parallel) + buildtime * parallel / ${SDECFG_PARALLEL:-1}); \
542 printf(\"%d\n\", time > 0 ? time : 1); }"
545 # Check if a package is already installed
547 # It does check the build-list if not in the rebuild stage - and
548 # the really installed package data for rebuilds (and so manual builds).
551 # add optional dep, if not supressed with "-n"
552 [ "$1" == "-n" ] && shift || var_append desc_E $'\n' "opt $1"
554 if ! atstage rebuild; then
555 local pattern="${1//+/\\+}"
556 egrep -q "^X.* ($pattern) " $base/config/$config/packages && return
558 [ -f $root/var/adm/packages/$1 ] && return
561 [ -e $base/package/*/$1 ] ||
562 echo_warning "Installed package checked: $1 does not exist"
567 # pkgprefix [-t] [ <type> ] <package>
568 # Returns the prefix or the location of a 'type' of an already built package,
569 # or aborts the build if not yet installed.
570 # When run in the test mode [-t] it returns an error code accordingly.
574 local dotest= abortmsg=
577 # -t : only see if it's possible to get the values
578 if [ "$1" = "-t" ]; then
582 if [ $# -eq 2 ]; then
587 # test usual error causes
588 if [ -z "$pkg" ]; then
589 abort "pkgprefix: you must specify a package"
590 elif [ ! -f "$root/var/adm/packages/$pkg" ]; then
591 abortmsg="package $pkg is not present"
592 elif grep -q "^Prefix:" "$root/var/adm/packages/$pkg"; then
593 prefix=$(grep "^Prefix: " "$root/var/adm/packages/$pkg" | cut -d' ' -f2-)
595 abort "pkgprefix: $pkg record is to old (no Prefix: entry), please rebuild it."
598 if [ "$dotest" ]; then
599 # test mode: abort or continue
600 if [ "$abortmsg" ]; then
601 echo "pkgprefix: $abortmsg" 1>&2
606 elif [ "$abortmsg" ]; then
607 echo "pkgprefix: $abortmsg" 1>&2
608 elif [ -z "$type" -o "$type" = "prefix" ]; then
611 elif [ "$type" = "ver" ]; then
612 value=$(grep "^Package Name and Version:" "$root/var/adm/packages/$pkg" | cut -d' ' -f6)
614 value=$(grep "^Location $type: " "$root/var/adm/packages/$pkg" | cut -d' ' -f3-)
615 if [ -z "$value" ]; then
616 # try default location for that $prefix
617 value=$(xpkg="$pkg"; pkggetdir "$type")
620 echo "${value:-PKGPREFIX_ERROR}"
623 # pkggetdir <type> (needs $prefix and $xpkg)
624 # returns the location for the file of a 'type' considering it's prefix
627 local xprefix=${prefix:+/$prefix}
629 bindir) echo "$xprefix/bin" ;;
630 sbindir) echo "$xprefix/sbin" ;;
632 if atstage toolchain; then
634 elif [ "$SDECFG_MULTILIB" = 1 ]; then
635 case $arch_machine in
636 powerpc64*|sparc64|loongarch64|mips64|riscv*|x86_64)
637 if [ "$SDECFG_X8664_X32" = 1 ]; then
638 echo "$xprefix/libx32"
639 elif [ "$SDECFG_MIPS64_N32" = 1 ]; then
640 echo "$xprefix/lib32"
642 echo "$xprefix/lib64"
653 libexecdir) echo "${xprefix:-/usr}/libexec" ;;
654 datadir) echo "${xprefix:-/usr}/share" ;;
655 infodir) echo "${xprefix:-/usr}/info" ;;
656 mandir) echo "${xprefix:-/usr}/man" ;;
657 docdir) echo "${xprefix:-/usr}/doc/$xpkg" ;;
658 includedir) echo "${xprefix:-/usr}/include" ;;
659 sysconfdir) echo "/etc${xprefix##/usr*}" ;;
660 localstatedir) echo "/var${xprefix##/usr*}" ;;
665 # This function generates the T2 package checksum of $confdir.
666 # The checksum includes the filenames and content (except of the .cache),
667 # including subdirs but without whitespaces and comments and some tag lines
668 # that are not vital for rebuilds during update checks.
670 # pkgchksum package-name | full-patch
674 local md5sum="md5sum"
675 if ! type -p $md5sum > /dev/null; then
678 # expand to full path if only a package name was specified
679 [[ $1 == */* ]] || set $base/package/*/$1/
681 # find all files (without hidden (e.g. .svn) files)
682 find . ! -path '*/.*' ! -name '*.cache' -print -exec cat \{\} \; \
684 # strip some unimportant stuff (e.g. comments, whitespaces, ...)
689 -e '/^\[[T,I,U,A,M,L,S,C]\]/d' \
692 $md5sum | cut -d ' ' -f 1
696 # Create Package Database for gasgui install tool
698 create_package_db() {
700 for file in $(echo $1/descs/*); do
701 [ -f "$file" ] || continue
703 # only include the package if a binary file is available
705 if [ "$SDECFG_PKGFILE_VER" = 1 ]; then
706 v=-$(grep '^Package Name and Version' \
707 $1/packages/$pkg | cut -f6 -d' ')
711 bfile=${pkg}${v}.$SDECFG_PKGFILE_TYPE
713 if [ -e $2/$bfile ]; then
714 [ "$pkg" = TRANS.TBL ] && continue
719 cat $1/descs/$pkg | grep -v '\[COPY\]'
722 cat $1/dependencies/$pkg
731 echo_error "Binary file for $bfile not present." \
732 "Skipped in package database."
735 gzip -c $3.tmp > $3; rm -f $3.tmp
738 # Add files to the 'badfiles' list
740 register_badfiles() {
745 var_append badfiles $'\n' " $x\$"
746 badfiles_desc[$badfiles_nr]=" $x\$"$'\n'"$desc"
751 # Detect the available patchfiles
753 detect_patchfiles() {
755 patchfiles="`ls $confdir/*.patch{,.$arch,.$SDECFG_KERNEL} \
756 $confdir/*.patch_$xpkg{.$arch,.$SDECFG_KERNEL} \
757 2> /dev/null | tr '\n' ' '`"
760 var_append patchfiles ' ' "`ls $confdir/*.patch.cross0{,.$arch,.$SDECFG_KERNEL} \
761 2>/dev/null | tr '\n' ' '`"
763 var_append patchfiles ' ' "`ls $confdir/*.patch.cross{,.$arch,.$SDECFG_KERNEL} \
764 2>/dev/null | tr '\n' ' '`"
766 for x in $(get_reverted $targetchain); do
767 for y in pkg_$pkg.patch{,.$arch,.$SDECFG_KERNEL} xpkg_$xpkg.patch{,.$arch,.$SDECFG_KERNEL}; do
768 if [ -f $base/target/$x/$y ]; then
769 patchfiles="$patchfiles $base/target/$x/$y"
774 # get additional patches from $targetdir/package/$pkg and architecture/package/$pkg -
775 # if it's not the confdir ie. contains the .desc file which overrides the default
776 for x in $targetdir/package/$pkg $base/architecture/$arch/package/$pkg; do
777 if [ $confdir != $x ]; then
778 var_append patchfiles ' ' "`ls $x/*.patch{,.$arch} 2> /dev/null | tr '\n' ' '`"
786 *.bz2|*.tbz2) x="bzip2 -d" ;;
787 *.gz|*.tgz) x="gzip -d" ;;
789 *.lzma) x="lzma -d" ;;
790 *.xz|*.txz) x="xz -d" ;;
791 *.Z) x="compress -d" ;;
792 *.zst) x="zstd -d" ;;
793 *.br) x="brotli -d" ;;
795 [ "$x" ] && echo "$x"
798 # Apply the given $patchfiles
799 # [ hook called for each patch ] [ filter script ]
804 [ "$filter" ] || filter=cat
805 for x in $patchfiles; do
806 # correct the absolute path, e.g. for patchfiles supplied in
807 # the .desc file, we assume relative path patches are mirrorable
808 if [ ! -e "$x" -a -n "${x##*/*}" ]; then
809 x="$base/download/mirror/${x:0:1}/$x"
813 local compressor="$(get_compressor "$x")"
814 if [ "$compressor" ]; then
816 $compressor < $x > $patch_file
821 $filter $patch_file | patch $patchopt
823 [ "$compressor" ] && rm $patch_file
828 # -------------------------------------------------------------------
829 # The automatic extraction of archive (prior to building) supports
830 # multiple archive types. For every archive type we have a separate
831 # func that knows how to extract the archive. However, every func
832 # should deliver two file: untar.txt and xsrcdir.txt.
834 # untar.txt needs to contain all extracted files.
835 # xsrcdir.txt need to contain the top level extraction directories.
836 # -------------------------------------------------------------------
839 echo "Extracting $xsrctar ($taropt) ... "
840 tar -v $taropt $1 > untar.txt
844 echo "Extracting $xsrctar ($zipopt) ... "
845 unzip $zipopt $1 | sed -n '/: [a-z]/{s,.*: ,,p}' > untar.txt
849 echo "Extracting $xsrctar ($sevenzipopt) ... "
850 7z $sevenzipopt $1 | sed 's,/,,' | cut -f3 -d" " | grep -v "^$" > untar.txt
853 # Main program for building a package
855 build_this_package() {
856 if [ ".$desc_SRC" == "." ]; then
857 # Autodetect source tar and extract it
859 if [ "$srctar" = auto ]; then
860 xsourceballs=$(echo "$desc_D" | head -n 1 | sed 's, ,\t,g; s,\t\t*,\t,g' |
862 if [ -z "$xsourceballs" ]; then
863 echo "Can't auto-detect srctar for package '$xpkg'!"
867 xsourceballs="$srctar"
869 elif [ "$srctar" = auto ]; then
870 sourceballs=$(echo "$desc_D" | sed 's, ,\t,g; s,\t\t*,\t,g' |
872 xsrcpattern=$(echo "$desc_SRC" | sed 's, ,\t,g; s,\t\t*,\n,g')
873 xsourceballs=$(echo "$sourceballs" | grep -F "$xsrcpattern")
875 xsourceballs="$srctar"
877 for xsrctar in $xsourceballs; do
878 saved_patchfiles="$patchfiles"
879 var_append patchfiles " " \
880 "`ls $confdir/*.patch.${xsrctar/-[v0-9]*/} 2> /dev/null`"
881 if [ "$xsrctar" != none -a "$autoextract" = 1 ]; then
883 if [ -z "$custextract" ]; then
884 # No custom extraction, so determine what
885 # autoextraction to use.
887 *.zip) custextract="autoextract_zip" ;;
888 *.7z) custextract="autoextract_7z" ;;
889 *) custextract="autoextract_tar" ;; # *.tar.bz2|*.tbz2|*.tbz
892 if [ -n "$custextract" ]; then
893 # Do the actual extraction of the archive.
894 eval "$custextract $archdir/$xsrctar"
895 sed 's,^\./,,; /^$/d' untar.txt | cut -f1 -d/ | sort -u > xsrcdir.txt
898 if [ "$srcdir" = auto ]; then
899 xsrcdir=${xsrctar%.tar.*}
900 xsrcdir=${xsrcdir%.t?z*}
901 if [ ! -d $xsrcdir ]; then
902 for x in $pkg-$ver ${pkg}_$ver $pkg \
903 $xpkg-$ver ${xpkg}_$ver $xpkg \
906 [ -d "$x" ] && xsrcdir="$x"
913 if [ "$chownsrcdir" = 1 ]; then
914 echo "Fixing ownership and permissions ..."
915 chown -R 0:0 $builddir/$xsrcdir
918 if [ "$nocvsinsrcdir" = 1 ]; then
919 echo "Removing CVS, .svn, {arch} and .arch-ids directories ..."
920 egrep '(^|/)(CVS|\.svn|\{arch\}|\.arch-ids)(/|$)' untar.txt |
922 echo "Removing $x ..."
927 echo "Changing into $builddir/$xsrcdir ..."
928 cd $builddir/$xsrcdir
933 [ $autopatch = 1 ] && apply_patchfiles
940 if [ "$createprefix" = 1 ]; then
941 echo "Creating $root/$prefix/<..> if required ..."
942 for x in $foodirlist; do
943 eval "x=\"$root\$$x\""
946 rmemptydir="$rmemptydir $x"
951 if [ ".$custmain" = "." ]
955 # Maybe generate a configure first
957 if [ "$autogen" = 1 -o \
958 \( -f configure.in -a ! -f configure -a "$autogen" != 0 \) ]; then
959 if [ -f autogen.sh ]; then
960 echo "Running package autogen script"
961 sed -i 's,.*/configure ,: # &,' autogen.sh
962 sh autogen.sh $(eval echo $confopt)
964 echo "Running builtin autogen script"
965 libtoolize --force --automake
966 aclocal $ACLOCAL_FLAGS
967 if grep AM_INIT_AUTOMAKE \
969 then automake --add-missing; fi
973 # Run configure scripts etc.
975 if [ "$runconf" = 1 ]; then
976 if [ -n "$(type -p $configscript)" -o "$autogen" = 1 ]; then
977 eval_config_command $(eval echo $confopt)
983 if [ "$runcmake" = 1 -a ! -f Makefile ] &&
984 [ -e $cmakelists -o -e ../$cmakelists ]; then
985 if atstage cross; then
986 create_cmake_toolchain_file >> t2.cmake
987 var_insert cmakeopt " " "-DCMAKE_TOOLCHAIN_FILE=t2.cmake"
990 # top-level CMakeLists?
991 [ ! -e $cmakelists ] && cmakelists="../$cmakelists"
993 # already ending with a directory?
994 [[ "$cmakeopt" = *. ]] || var_append cmakeopt ' ' "${cmakelists%%/*}"
996 eval echo "Running cmake $cmakeopt"
997 cmake $(eval echo $cmakeopt)
1000 # Automated package build
1002 # Styles without make run first:
1003 if [ -f pyproject.toml -a "$rungpepinstall" = 1 ]; then
1006 echo "Running gpep517 build-wheel --wheel-dir .dist --output-fd 3 3>&1 >&2"
1007 gpep517 build-wheel --wheel-dir .dist --output-fd 3 3>&1 >&2
1009 echo "Running python -m installer .dist/*.whl"
1010 python -m installer .dist/*.whl
1011 elif [ -f setup.py -a "$runpysetup" = 1 ]; then
1012 pyconfopt="${pyconfopt:=--prefix $root/$prefix}"
1014 eval echo "Running ${pyscript:-python} setup.py build install $pyconfopt"
1015 eval ${pyscript:-python} setup.py build install $pyconfopt
1017 # for backwards compatibility, but users should REALLY use rungpepinstall instead
1018 elif [ -f pyproject.toml -a "$runpipinstall" = 1 ]; then
1020 eval echo "Running ${pyscript:-python} -m pip install ."
1021 eval ${pyscript:-python} -m pip install --break-system-packages .
1023 elif [ -f meson.build -a "$runmeson" = 1 ]; then
1024 if atstage cross; then
1025 create_meson_cross >> cross.ini
1026 CC=cc CXX=c++ # make meson happy :-/
1027 var_append mesonopt " " "--cross-file cross.ini"
1030 eval echo "Running meson $mesonopt"
1031 eval "meson $mesonopt"
1033 eval echo "Running ninja -C objs/ $ninjaopt install"
1034 atstage cross && export DESTDIR=$root
1035 ninja -C objs/ $ninjaopt install
1037 elif [ -f build.ninja -a "$runninja" = 1 ]; then
1039 eval echo "Running ninja $ninjaopt"
1042 eval echo "Running ninja $ninjaopt install"
1043 atstage cross && export DESTDIR=$root
1044 ninja $ninjaopt install
1046 elif [ -f justfile -a "$runjust" = 1 ]; then
1048 if [ "$justopt" ]; then
1049 eval echo "Running just $justopt"
1050 eval "just $justopt"
1053 if [ "$justinstopt" ]; then
1054 eval echo "Running just $justinstopt"
1055 eval "just $justinstopt"
1058 elif [ -f Cargo.toml -a "$runcargo" = 1 ]; then
1060 if [ "$cargoopt" -a ! "$cargoinstopt" ]; then
1061 eval echo "Running cargo $cargoopt"
1062 eval "cargo $cargoopt -j$SDECFG_PARALLEL"
1065 if [ "$cargoinstopt" ]; then
1066 eval echo "Running cargo $cargoinstopt"
1067 eval "cargo $cargoinstopt -j$SDECFG_PARALLEL"
1070 elif [ -f SConstruct -a "$runscons" = 1 ]; then
1072 if [ "$sconsopt" ]; then
1073 eval echo "Running scons $sconsopt"
1074 eval "scons $sconsopt"
1077 if [ "$sconsinstopt" ]; then
1078 eval echo "Running scons $sconsinstopt"
1079 eval "scons $sconsinstopt"
1082 elif [ -f build.zig -a "$runzig" = 1 ]; then # some projects still use makefile
1084 eval echo "Running zig build $zigconfopt"
1085 eval "zig build $zigconfopt"
1087 eval echo "Running zig build install $zigconfopt"
1088 eval "zig build install $zigconfopt"
1090 elif [ -f go.mod -a "$rungo" = 1 ]; then # some projects still use makefile
1092 eval echo "Running go build $goconfopt"
1093 eval "go build $goconfopt"
1095 #eval "go install" # No universal way to install?
1097 elif [ -f META6.json -a "$runzef" = 1 ]; then
1099 var_append zefopt " " "--force-install --/depends --/test-depends --/build-depends"
1100 var_append zefopt " " "--install-to=inst#$root/$prefix/share/perl6/site"
1102 eval echo "Running zef install . $zefopt"
1103 eval "zef install . $zefopt"
1104 elif [ -f pom.xml -a "$runmaven" = 1 ]; then
1106 eval echo "Running mvn build $mavenopt"
1107 eval "mvn build $mavenopt"
1109 eval echo "Running mvn install $maveninstopt"
1110 eval "mvn install $maveninstopt"
1113 else # styles that include a make run
1114 if [ ! -f Makefile -a ! -f makefile -a \
1115 -f Makefile.PL -a "$runmkpl" = 1 ]; then
1116 echo "Running perl Makefile.PL ${plconfopt:-INSTALLDIRS=perl}"
1117 perl Makefile.PL ${plconfopt:-INSTALLDIRS=perl}
1118 if atstage cross; then
1119 sed -i "s,/TOOLCHAIN/cross/usr/lib,$libdir,g" Makefile
1120 sed -i "s,/TOOLCHAIN/cross,,g" Makefile
1121 var_append makeopt ' ' 'PERL=perl FULLPERL=perl'
1122 var_append makeinstopt ' ' 'PERL=perl FULLPERL=perl DESTDIR='
1126 if [ ! -f Makefile -a ! -f makefile -a \
1127 -f Imakefile -a "$runxmkmf" = 1 ]; then
1128 echo "Running xmkmf -a"
1135 if [ "$runmake" = 1 -a "$makeopt" ]; then
1136 eval echo "Running $MAKE $makeopt"
1137 eval "$MAKE $makeopt"
1140 if [ "$runmake" = 1 -a "$makeinstopt" ]; then
1141 eval echo "Running $MAKE $makeinstopt"
1142 eval "$MAKE $makeinstopt"
1147 echo_warning "The use of custmain is deprecated, please use hooks instead."
1149 for x in preconf premake inmake postmake; do
1150 if eval "[ -n \"\$hookdirty_$x\" ]"; then
1151 echo "Hook $x is still marked as dirty running it, now ..."
1157 if [ "$createdocs" != 0 ]; then
1158 if [ ! -e $root$docdir ]; then
1160 rmemptydir="$rmemptydir $root$docdir"
1162 [ -z "$createdocs" ] && createdocs="$SDECFG_CREATE_DOCS"
1165 if [ "$createdocs" = 1 ]; then
1166 echo "Trying to copy the default documentation ..."
1167 for x in [A-Z][A-Z]* *.lsm ChangeLog*; do
1168 [ "${x#*.[cho0-9]}" ] || continue
1169 [ "${x#*.info*}" ] || continue
1170 [ "${x#*.TAR*}" ] || continue
1171 [ "${x#*akefile*}" ] || continue
1172 [ -f $x ] && cp -v $x $root$docdir/$x
1175 echo "Trying to copy even more documentation ..."
1176 [ -d $builddir/$xsrcdir ] && cd $builddir/$xsrcdir
1177 for x in `find -type d \( -name 'doc' -o -name 'docs' \
1178 -o -name '[Dd]ocumentation' \) ! -empty`
1180 if [ -d "$x" -a "`echo $x/*`" != "$x/*" ]
1181 then cp -rLv $x/* $root$docdir || true; fi
1183 for x in $confdir/*.doc; do
1185 then cp -v $x $root$docdir/${x%.doc}; fi
1187 find $root$docdir/ -name '*.[0-9]' -o -name '*.info*' \
1188 -o -name '[Mm]akefile*' |
1189 xargs -r rm -f 2> /dev/null || true
1190 find $root$docdir/* -type d -empty 2> /dev/null |
1191 xargs -r rmdir 2> /dev/null || true
1195 if atstage native && [ -f /sbin/ldconfig ]; then
1196 echo "Running ldconfig"
1199 patchfiles="$saved_patchfiles"
1202 if [ "$rmemptydir" ]; then
1203 rmdir $rmemptydir 2> /dev/null || true
1209 # source_file cksum file url
1211 # Create the file path from 'file' and 'url'.
1212 # cksum and url are ignored
1213 # ([D] tag compatible format)
1216 local pre="" file="$2" url="$3" mirror="mirror"
1218 # '-' as $url prefix means, nomirrorable
1219 [ "${url:0:1}" == "-" ] && mirror="local"
1221 # inside Build-Pkg $archdir is set
1222 if [ -n "$archdir" ]; then
1226 echo ${pre}download/${mirror}/${file:0:1}/$file
1229 # match_source_file [-p] pattern [[package] ...]
1231 # Returns path and name of a downloaded file from a list of packages, matching
1232 # a grep pattern. Without -p it only returns it's name, not the path.
1234 match_source_file() {
1235 local pattern= package= showpath=0
1236 local x= file= url= mirror=
1241 -p) showpath=1; shift ;;
1242 *) break ;; # no abort as the pattern might start with a '-'
1247 for package in ${*:-$pkg}; do
1248 while read x x file url x; do
1251 if [ $showpath -eq 0 ]; then
1254 [ "${url:0:1}" == "-" ] && mirror="local" || mirror="mirror"
1255 echo $base/download/${mirror}/${file:0:1}/$file
1257 done < <(for f in $base/target/$target/package/$package/$package.desc \
1258 $base/architecture/$arch/package/$package/$package.desc \
1259 $base/package/*/$package/$package.desc; do
1260 grep -a -e "^\[D\] [^ ]* .*$pattern" $f 2> /dev/null
1261 grep -q -a -e "^\[D\]" $f 2> /dev/null && break
1267 # create the virtual $archdir symlinks
1269 populate_archdir() {
1271 for x in `match_source_file -p .`; do
1272 if [ ! -f $x ]; then
1273 echo_warning "File not found: ${x#$base/}"
1275 elif [ ! -e "$builddir/archdir/${x##*/}" ]; then
1276 ln -vs $x $builddir/archdir/
1279 if [ $missing -eq 1 ]; then
1280 echo_warning "Did you run scripts/Download for this package?"
1285 # search for the package confdir
1289 for x in package/*/$pkg/$pkg.desc; do
1290 [ -f "$x" ] || continue
1291 if [ "$confdir" ]; then
1292 echo_pkg_deny $stagelevel $pkg "in multiple trees"
1293 echo "Package $pkg in multiple trees!" \
1294 > $root/var/adm/logs/$stagelevel-$xpkg.err
1297 x=${x#package/} x=${x%%/*}
1298 confdir="$base/package/$x/$pkg"
1302 if [ -e architecture/$arch/package/$pkg/$pkg.desc ]
1303 then confdir="$base/architecture/$arch/package/$pkg"; fi
1305 if [ -e $base/target/$target/package/$pkg/$pkg.desc ]
1306 then confdir="$base/target/$target/package/$pkg"; fi
1309 # initialize standard vars and hooks
1311 init_vars_and_hooks() {
1312 makeopt='CC="$CC" CPP="$CPP" CXX="$CXX"'
1313 sconsopt='CC=$CC PREFIX=/$prefix LIBDIR=$libdir'
1315 cargoinstopt='install --path . --root $root/$prefix --no-track --force' # -Z no-index-update
1316 goconfopt='-buildmode=pie -trimpath -mod=readonly'
1317 zigconfopt='--prefix $root/$prefix'
1318 justopt='prefix=/$prefix'
1320 # support both lowercase GNU and uppercase BSD conventions
1321 if atstage toolchain; then
1322 makeopt="$makeopt"' prefix="$root/$prefix" PREFIX="$root/$prefix" docdir="$root$docdir"'
1323 cmakeopt='-DCMAKE_INSTALL_PREFIX="$root/$prefix" -DCMAKE_INSTALL_LIBDIR="$root$libdir" -DCMAKE_LIBRARY_PATH="$root$libdir"'
1325 makeopt="$makeopt"' prefix="/$prefix" PREFIX="/$prefix" docdir="$docdir"'
1326 cmakeopt='-DCMAKE_INSTALL_PREFIX="/$prefix" -DCMAKE_INSTALL_LIBDIR="$libdir" -DCMAKE_LIBRARY_PATH="$libdir"'
1329 if [ "$SDECFG_DEBUG" = 1 ]; then
1330 var_append cargoinstopt ' ' '--debug'
1331 var_append cmakeopt ' ' '-DCMAKE_BUILD_TYPE=Debug'
1333 var_append cargoopt ' ' '--release'
1334 var_append cmakeopt ' ' '-DCMAKE_BUILD_TYPE=Release'
1337 if ! atstage native; then
1338 makeopt="$makeopt"' CC_FOR_BUILD="$BUILDCC"'
1339 makeopt="$makeopt"' BUILDCC="$BUILDCC" BUILD_CC="$BUILD_CC"'
1340 makeopt="$makeopt"' HOSTCC="$HOSTCC" HOST_CC="$HOST_CC"'
1341 makeopt="$makeopt"' STRIP="$STRIP" AR="$AR" LD="$LD" AS="$AS"'
1342 makeopt="$makeopt"' RANLIB="$RANLIB" NM="$NM"'
1345 if atstage native; then
1346 flistdel="$flistdel|`echo $base | sed s,^/,,`/.*"
1349 if atstage cross; then
1350 cmakeopt="$cmakeopt -DCMAKE_SYSTEM_PROCESSOR=$(echo $arch | arch2uname)"
1352 # everyting non-DESTDIR is passed in the specific packages
1353 makeinstopt="$makeopt"' DESTDIR="$root" install'
1354 sconsinstopt='PREFIX=$root/$prefix LIBDIR=$root$libdir install'
1356 cargoopt="$cargoopt"' --target ${arch_target}' # /-t2-/-unknown-}
1357 cargoinstopt="$cargoinstopt"' --target ${arch_target}' # /-t2-/-unknown-}
1359 justopt="$justopt"' root="$root"'
1360 justinstopt="$justopt"' root="$root" install'
1362 makeinstopt="$makeopt"' install'
1363 sconsinstopt="$sconsopt"' install'
1364 justinstopt="$justopt"' install'
1367 atstage cross && hook_add postflist 2 'postflist_fix_cross_scripts'
1368 [ "$SDECFG_DO_CHECK" = 1 ] && hook_add inmake 6 'run_check'
1369 [ "$SDECFG_STATIC_IN_USR" = 1 ] && hook_add postflist 3 'postflist_static_lib'
1374 configprefix="" autogen=
1375 configscript="./configure" extraconfopt=""
1376 cmakelists="CMakeLists.txt"
1378 srcdir=auto srctar=auto
1379 taropt="-xf" # --use-compress-program=zstd
1382 mainfunction="build_this_package"
1383 runconf=1 runmake=1 runxmkmf=1 runmkpl=1 runpysetup=1 runpipinstall=0 rungpepinstall=1
1384 runcmake=1 runmaven=1 runmeson=1 runninja=1 runcargo=1 runjust=1 rungo=1 runscons=1 runzig=1
1385 runzef=1 autopatch=1 autoextract=1 chownsrcdir=1 nocvsinsrcdir=1 cleanconfopt=1
1386 patchopt="-bfp1 -z .orig"
1387 createprefix=1 createdocs="" rmemptydir=""
1393 badfiles="" badfiles_nr=0
1394 declare -a badfiles_desc
1397 # this is a 2nd lightweight and modular "build this package" implementation
1398 # currently only used for the postlinux stuff - later maybe for more -ReneR
1407 pkg="$1" xpkg="$pkg"
1408 [ "$2" ] && conffile="$2" || conffile="$pkg.conf"
1411 declare -a hook_functions='()'
1419 # Erase positional parameters to prevent unintended current
1420 # arguments pssing to the loaded script.
1424 echo_status "Building $xpkg within $super (using $conffile)"
1426 for x in $(get_expanded $base/target/%/pkg_$pkg.conf $targetchain) \
1427 $targetdir/package/$pkg/$pkg.conf \
1428 architecture/$arch/package/$pkg/$pkg.conf \
1429 $confdir/$conffile; do
1431 if [[ $x == */$conffile ]]; then
1432 echo "Reading package configuration ($conffile) from package directory."
1434 echo "Reading package configuration from $x."
1441 # short path - to not abort on missing downloads of postlinux.conf
1442 # packages that are not built anyway -ReneR
1443 if [ "$custmain" = "true" ]; then
1444 echo "Nothing is going to be done ayway - returning quickly."
1451 # dump for debugging
1452 hook_dump > $builddir/debug.hooks.$pkg
1453 dump_env > $builddir/debug.buildenv.$pkg
1455 echo "Running main build function '$mainfunction'"
1458 eval "$mainfunction"
1461 [ -f $logstamp ] || return 1