mrrl

Minimal Reliable Reproducible Linux
git clone https://ccx.te2000.cz/git/mrrl
Log | Files | Refs | Submodules | README

gcc (9888B)


      1 {%- set pkg_version=target_gcc_version|default("13.4.0") -%}
      2 {%- set pkg_source="tar.xz" -%}
      3 {%- set arch=triplet.split("-")[0] -%}
      4 {% extends "genlinks_mini" %}
      5 {%- do import_functions.update(["build_env_static", "check_static"]) %}
      6 {%- block template_deps %}{% endblock %}
      7 {% block package_deps -%}
      8 #@pragma:nofail_digest
      9 #+{{pkg_install_name("musl-cross-make:bootstrap-1")}}
     10 #+{{pkg_install_name("gnu-make:bootstrap")}}
     11 #+{{pkg_install_name("busybox:bootstrap")}}
     12 #+{{pkg_install_name("nawk:bootstrap")}}
     13 #+{{pkg_install_name("musl")}}
     14 #+{{pkg_install_name("gmp")}}
     15 #+{{pkg_install_name("mpc")}}
     16 #+{{pkg_install_name("mpfr")}}
     17 #@sha256:{{files["mcm-"~shortname~"-"~pkg_version~"-merged.diff"]}}:merged.patch
     18 #@sha256:{{files["gcc14-esysroot.patch"]}}:esysroot.patch
     19 {% endblock package_deps %}
     20 {% block prepare %}
     21 {{- super() }}
     22 patch -p1 <../merged.patch
     23 patch -p1 <../esysroot.patch
     24 {% endblock prepare %}
     25 {% block configure %}
     26 export TZ=UTC
     27 export SOURCE_DATE_EPOCH=1748000928
     28 
     29 mcm={{versions}}/musl-cross-make.{{pkg_sha256("musl-cross-make:bootstrap-1")}}
     30 mcm_bin=$mcm/bin/{{triplet}}
     31 binutils_cmd={{versions}}/binutils.{{pkg_sha256("binutils")}}
     32 musl={{versions}}/musl.{{pkg_sha256("musl")}}
     33 build_env_static
     34 
     35 sysroot="${pthbs_workdir}/sysroot"
     36 objdir="${pthbs_workdir}/objdir"
     37 installdir="${pthbs_workdir}/install"
     38 src="$(realpath .)"
     39 
     40 with_make_vars_inner() {
     41 	"$@" \
     42 	MULTILIB_OSDIRNAMES= \
     43 	INFO_DEPS= \
     44 	infodir= \
     45 	ac_cv_prog_lex_root=lex.yy \
     46 	MAKEINFO=false
     47 }
     48 
     49 with_make_vars() {
     50 	with_make_vars_inner "$@" \
     51 	MAKE="make $(with_make_vars_inner printf '\t%s"' | sed -Ee 's/\t([^"=]+=)/ \1"/g') "
     52 }
     53 
     54 sed -i.orig -e "s|/lib/ld-musl-|$musl/"{{dynlibdir|shesc}}"/ld-musl-|" ./gcc/config/*/linux*.h
     55 for orig in ./gcc/config/*/linux*.h.orig; do
     56 	diff -u "$orig" "${orig%.orig}" || true
     57 	rm "$orig"
     58 done
     59 
     60 {# now place libc and other library files where gcc might look for them
     61  # but skip the bootstrap toolchain's files -#}
     62 env_lib=${pthbs_build_environment}/{{libdir|shesc}}
     63 env_dynlib=${pthbs_build_environment}/{{dynlibdir|shesc}}
     64 mkdir -p "$sysroot/lib"
     65 find "$pthbs_build_environment/{{libdir}}" "$pthbs_build_environment/{{dynlibdir}}" -type l -print | while IFS= read -r lnk; do
     66 	case $(realpath $lnk) in
     67 		($mcm/*) ;;
     68 		(*)
     69 			ldir=${lnk%/*}
     70 			case $ldir in
     71 				(${env_lib})
     72 					ldir=$sysroot/lib ;;
     73 				(${env_lib}/*)
     74 					ldir=$sysroot/lib/${ldir#${env_lib}/}
     75 					mkdir -p "$ldir"
     76 					;;
     77 				(${env_dynlib})
     78 					ldir=$sysroot/lib ;;
     79 				(${env_dynlib}/*)
     80 					ldir=$sysroot/lib/${ldir#${env_dynlib}/}
     81 					mkdir -p "$ldir"
     82 					;;
     83 				(*) printf 'WARNING: unexpected file: %s\n' "$lnk"
     84 					continue ;;
     85 			esac
     86 			cp -a "$lnk" "$ldir/"
     87 			;;
     88 	esac
     89 done
     90 
     91 mkdir -p "$sysroot/${musl%/*}"
     92 ln -s "$musl" "$sysroot/${musl%/*}"  {#- TODO: check if this is useful #}
     93 
     94 {# make separate build dir, because in-tree build is broken/unsupported -#}
     95 mkdir "$objdir"
     96 cd "$objdir"
     97 
     98 st='-static --static'
     99 fl="-g -O0"
    100 repro="-ffile-prefix-map=${installdir}="
    101 repro="$repro -ffile-prefix-map=${sysroot}="
    102 repro="$repro -ffile-prefix-map=${src}=builddir"
    103 {# While there's an option for distinct dynamic library (--with-slibdir)
    104  # it doesn't actually work. The installer places files there but the compiler
    105  # isn't able to find them afterwards as it searches the directory
    106  # for static libraries instead.
    107  # So we put everything in $prefix/lib and just place a symlink in proper directories
    108  # to avoid gcc or it's install scripts getting confused by unusual layout. #}
    109 "../${src##*/}/configure" \  {#- use relative path here as it's stored in the binary #}
    110 	--enable-checking \  {#- for debugging #}
    111 	--enable-languages=c,c++  \
    112 	--prefix="/" \
    113 	--bindir="/{{bindir}}" \
    114 	--sbindir="/{{sbindir}}" \
    115 	--infodir="/{{infodir}}" \
    116 	--localedir="/{{localedir}}" \
    117 	--mandir="/{{mandir}}" \
    118 	--libdir="/lib" \
    119 	--libexecdir="/libexec" \
    120 	--docdir="/{{docdir}}/{{shortname}}" \
    121 	--datadir="/{{datadir}}" \
    122 	--datarootdir="/{{datarootdir}}" \
    123 	--localstatedir='{{var_dir}}' \
    124 	--disable-bootstrap \
    125 	--disable-assembly \
    126 	--disable-werror \
    127 	--target={{triplet}} \
    128 	--build={{arch}}-pc-linux-muslxx \
    129 	--host={{arch}}-pc-linux-muslxx \
    130 	--disable-multilib \
    131 	--enable-tls \
    132 	--disable-libmudflap \
    133 	--disable-libsanitizer \
    134 	--disable-gnu-indirect-function \
    135 	--disable-libmpx \
    136 	--enable-initfini-array \
    137 	--enable-libstdcxx-time=rt \
    138 	--with-native-system-header-dir="$musl/{{includedir}}" \
    139 	--with-build-time-tools="$pthbs_build_environment/{{bindir}}" \
    140 	--with-sysroot=/ \
    141 	FFLAGS="$fl" \
    142 	LDFLAGS="--static $LDFLAGS" \
    143 	AR_FOR_TARGET="$mcm_bin"-ar \
    144 	AS_FOR_TARGET="$mcm_bin"-as \
    145 	LD_FOR_TARGET="$mcm_bin"-ld \
    146 	NM_FOR_TARGET="$mcm_bin"-nm \
    147 	OBJCOPY_FOR_TARGET="$mcm_bin"-objcopy \
    148 	OBJDUMP_FOR_TARGET="$mcm_bin"-objdump \
    149 	RANLIB_FOR_TARGET="$mcm_bin"-ranlib \
    150 	READELF_FOR_TARGET="$mcm_bin"-readelf \
    151 	STRIP_FOR_TARGET="$mcm_bin"-strip \
    152 	REDACT=FROM-HERE \  {#- following arguments are not stable / reproducible #}
    153 	--with-build-sysroot="$sysroot" \
    154 	CFLAGS="-std=gnu17 $fl $st $repro" \
    155 	CXXFLAGS="-std=gnu++11 $fl $st $repro" \
    156 
    157 
    158 {%- endblock configure %}
    159 {%- block build %}
    160 with_make_vars {{make}} configure-host
    161 sed -iEe 's/REDACT=FROM-HERE.*";$/# build paths redacted";/' gcc/configargs.h
    162 for tgt in configure-target all-gcc all-target-libgcc; do
    163 	with_make_vars {{make}} "$tgt"
    164 done
    165 
    166 with_make_vars {{make}}
    167 
    168 {%- endblock build %}
    169 {%- block install %}
    170 with_make_vars make "DESTDIR=$dest" install
    171 
    172 cd "$pthbs_workdir"
    173 
    174 {# if the doubling of $prefix was resolved this is unnecessary
    175 mkdir -p "${dest%/*}"
    176 mv "${installdir}${prefix}" "${dest%/*}/" -#}
    177 
    178 {# sort out shared object from static linking archives -#}
    179 {# this is for subdirs of (dyn)libdir, not sure that's useful
    180 printf >link_to  '%s\n' 'tgtdir="$to/${1#./lib/}"'
    181 printf >>link_to '%s\n' 'tgtdir="${tgtdir%/*"}'
    182 printf >>link_to '%s\n' 'topdir="$(printf "%s\n" "${1%/*}" | sed -Ee "s!^\./!!; s![^/]+!..!g")"'
    183 printf >>link_to '%s\n' 'mkdir -p "$tgtdir"'
    184 printf >>link_to '%s\n' 'ln -s "$topdir${1#.}" "$tgtdir/"'
    185 
    186 ( cd "$dest" && env to={{dynlibdir|shesc}} find "./lib" \
    187 	-type f \( -name '*.so' -o -name '*.so.*' \) \
    188 	-exec sh -xe "$pthbs_workdir/link_to" '{}' ';'
    189 )
    190 
    191 ( cd "$dest" && env to={{libdir|shesc}} find "./lib" \
    192 	-type f \( -name '*.o' -o -name '*.a' \) \
    193 	-exec sh -xe "$pthbs_workdir/link_to" '{}' ';'
    194 ) -#}
    195 
    196 mkdir -p "$dest"/{{dynlibdir|shesc}} "$dest"/{{libdir|shesc}}
    197 {# TODO: handle this in python, it's just as many ../ as there are dirs #}
    198 dynlib_to_top="$(printf "%s\n" {{dynlibdir|shesc}} | sed -Ee "s!^\./!!; s![^/]+!..!g")"
    199 lib_to_top="$(printf "%s\n" {{libdir|shesc}} | sed -Ee "s!^\./!!; s![^/]+!..!g")"
    200 
    201 {# go through relevant directories and make symlinks depending on type of library -#}
    202 printf >>link_to_dynlib '%s\n' 'ln -s "'$dynlib_to_top'${1#.}" '{{dynlibdir|shesc}}/
    203 printf >>link_to_lib '%s\n' 'ln -s "'$lib_to_top'${1#.}" '{{libdir|shesc}}/
    204 
    205 ( cd "$dest" && find "./lib" "./{{triplet}}" \
    206 	\( -type f -o -type l \) \( -name '*.so' -o -name '*.so.*' \) \
    207 	-exec sh -xe "$pthbs_workdir/link_to_dynlib" '{}' ';'
    208 )
    209 
    210 ( cd "$dest" && find "./lib" ./{{triplet}} \
    211 	\( -type f -o -type l \) \( -name '*.o' -o -name '*.a' \) \
    212 	-exec sh -xe "$pthbs_workdir/link_to_lib" '{}' ';'
    213 )
    214 
    215 {# move over non-gcc library files from sysroot to somewhere it finds them -#}
    216 find "$sysroot/lib" -mindepth 1 -maxdepth 1 -exec mv -v '{}' "$dest/lib/gcc/" \;
    217 
    218 {# Symlink binutils so gcc can use them without extra paths and naming.
    219  # Binutils aren't explicit dependency as we use previous toolchain's to build,
    220  # but this package is expected to use them further on -#}
    221 #find "$binutils_cmd" -mindepth 1 -maxdepth 1 -exec ln -s -v '{}' "$dest/lib/gcc/{{triplet}}/" \;
    222 for util in addr2line ar as c++filt elfedit gprof ld ld.bfd nm objcopy objdump ranlib readelf size strings strip; do
    223 	ln -s -v "$binutils_cmd/$util" "$dest/lib/gcc/{{triplet}}/" 
    224 done
    225 
    226 
    227 {%- endblock install %}
    228 {%- block check %}
    229 export ESYSROOT=$pthbs_build_environment
    230 # Validate the C compiler
    231 "$dest"/{{bindir|shesc}}/{{triplet}}-gcc --version
    232 printf '%s\n' 'int main(void) { return 0; }' >true.c
    233 "$dest"/{{bindir|shesc}}/{{triplet}}-gcc -o true-dl true.c
    234 "$mcm_bin"-readelf --string-dump=.interp true-dl >true.linker
    235 xxd true.linker
    236 grep -F "  $musl/" true.linker
    237 ./true-dl
    238 
    239 "$dest"/{{bindir|shesc}}/{{triplet}}-gcc -static -o true-sl true.c
    240 ./true-sl
    241 
    242 # Validate the C++ compiler
    243 "$dest"/{{bindir|shesc}}/{{triplet}}-g++ --version
    244 printf '%s\n' >sort.cpp '
    245 #include <iostream>
    246 #include <vector>
    247 #include <algorithm>
    248 #include <iterator>
    249 int main() {
    250        using namespace std;
    251        istream_iterator<int> first(cin);
    252        istream_iterator<int> last;
    253        vector<int> vec(first, last);
    254        sort(vec.begin(), vec.end());
    255        cout << "Sorted:" << endl;
    256        copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, "\n"));
    257        return 0;
    258 }
    259 '
    260 printf '%s\n' >sort.input 5 3 1 2 4
    261 printf '%s\n' >sort.expected Sorted: 1 2 3 4 5
    262 "$dest"/{{bindir|shesc}}/{{triplet}}-g++ -o sort-dl sort.cpp
    263 "$mcm_bin"-readelf --string-dump=.interp sort-dl >sort.linker
    264 xxd sort.linker
    265 grep -F "  $musl/" sort.linker
    266 env LD_LIBRARY_PATH="$dest"/{{dynlibdir|shesc}} ./sort-dl <sort.input >sort-dl.out
    267 cmp sort-dl.out sort.expected
    268 
    269 "$dest"/{{bindir|shesc}}/{{triplet}}-g++ -static -o sort-sl sort.cpp
    270 ./sort-sl <sort.input >sort-sl.out
    271 cmp sort-sl.out sort.expected
    272 
    273 if which dejagnu 2>/dev/null; then
    274 	{{make}} -C ../obj_sysroot -k check-gcc
    275 	mkdir "$dest/test-results"
    276 	find ../obj_sysroot -name '*.sum' \
    277 		-exec mv -v '{}' "$dest/test-results/" \;
    278 fi
    279 
    280 {%- endblock check %}
    281 {% block genlinks_begin %}
    282 	{{- super() }}
    283 	x["./{{bindir}}/{{triplet}}-g++"]=1
    284 	x["./{{bindir}}/{{triplet}}-gcc"]=1
    285 {% endblock %}
    286 {% block genlinks_rules %}
    287 {# make "x86_64-linux-musl-gcc" available as just "gcc" too -#}
    288 $2 == "{{bindir}}" && $3 ~ /^{{triplet}}-/ { target=$3; sub("{{triplet}}-", "", target); link("./{{bindir}}/" target) }
    289 {% endblock -%}