# MIT License # Copyright (c) 2023 εΎζŒζ’ Xu Chiheng # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. check_build_type_toolchain_and_set_compiler_flags() { if [ -z "${BUILD_TYPE}" ];then BUILD_TYPE=Release fi if [ -z "${TOOLCHAIN}" ];then TOOLCHAIN=GCC fi case "${BUILD_TYPE}" in Release | Debug ) echo "build type : ${BUILD_TYPE}" ;; * ) echo "unknown build type : ${BUILD_TYPE}" echo "valid build type : Release Debug" exit ;; esac case "${TOOLCHAIN}" in GCC | Clang ) echo "toolchain : ${TOOLCHAIN}" ;; * ) echo "unknown toolchain : ${TOOLCHAIN}" echo "valid toolchain : GCC Clang" exit ;; esac case "${TOOLCHAIN}" in GCC ) CC=gcc CXX=g++ ;; Clang ) CC=clang CXX=clang++ # Disable color errors globally? # http://clang-developers.42468.n3.nabble.com/Disable-color-errors-globally-td4065317.html export TERM=dumb ;; esac # https://www.phoronix.com/scan.php?page=news_item&px=GCC-11-x86-64-Feature-Levels # x86-64: CMOV, CMPXCHG8B, FPU, FXSR, MMX, FXSR, SCE, SSE, SSE2 # x86-64-v2: (close to Nehalem) CMPXCHG16B, LAHF-SAHF, POPCNT, SSE3, SSE4.1, SSE4.2, SSSE3 # x86-64-v3: (close to Haswell) AVX, AVX2, BMI1, BMI2, F16C, FMA, LZCNT, MOVBE, XSAVE # x86-64-v4: AVX512F, AVX512BW, AVX512CD, AVX512DQ, AVX512VL # only gcc 11+ and clang 12+ support this # local CPU_ARCH_FLAGS="-march=x86-64-v3" local CPU_ARCH_FLAGS="-march=x86-64" case "${BUILD_TYPE}" in Release ) CFLAGS+=" -O2 ${CPU_ARCH_FLAGS}" CXXFLAGS+=" -O2 ${CPU_ARCH_FLAGS}" LDFLAGS+=" -Wl,--strip-all" ;; Debug ) CFLAGS+=" -O0 -g3 ${CPU_ARCH_FLAGS}" CXXFLAGS+=" -O0 -g3 ${CPU_ARCH_FLAGS}" LDFLAGS+= ;; esac # https://learn.microsoft.com/en-us/cpp/c-runtime-library/link-options # commode.obj pcommode.obj Sets the global commit flag to "commit". See fopen, _wfopen and fopen_s, _wfopen_s. case ${HOST_NAME} in x86_64-pc-mingw64 ) LDFLAGS+=" -Wl,$(gcc -print-file-name=binmode.o)" ;; esac # if true; then # # Do not link against shared libraries # LDFLAGS+=" -Wl,-Bstatic" # else # # Link against shared libraries # LDFLAGS+=" -Wl,-Bdynamic" # fi echo "CC : ${CC} $("${CC}" -dumpversion)" echo "CXX : ${CXX} $("${CXX}" -dumpversion)" echo "CFLAGS : ${CFLAGS}" echo "CXXFLAGS : ${CXXFLAGS}" echo "LDFLAGS : ${LDFLAGS}" export CC CXX CFLAGS CXXFLAGS LDFLAGS } llvm_check_static_or_shared() { if [ -z "${STATIC_OR_SHARED}" ];then STATIC_OR_SHARED=static fi case "${STATIC_OR_SHARED}" in static | shared ) echo "build ${STATIC_OR_SHARED}" ;; * ) echo "unknown arg : ${STATIC_OR_SHARED}" echo "valid arg : static shared" exit ;; esac } maybe_git_filemode_false() { case ${HOST_NAME} in x86_64-pc-msys | x86_64-pc-mingw64 ) git_filemode_false ;; esac } git_checkout_dir_revision() { local dir="$1" local git_revision="$2" ( cd "${dir}" && maybe_git_filemode_false ) \ && ( cd "${dir}" && git checkout "${git_revision}" && git reset --hard HEAD ) } git_clone_and_checkout_dir_revision() { local dir="$1" local git_revision="$2" local git_repo_url="$3" git clone "${git_repo_url}" "${dir}" \ && git_checkout_dir_revision "${dir}" "${git_revision}" } check_dir_maybe_checkout() { local dir="$1" local git_revision="$2" local git_repo_url="$3" if [ -d "${dir}" ]; then if [ -f "${dir}"/patching ]; then # do nothing true elif [ -d "${dir}"/.git ]; then git_checkout_dir_revision "${dir}" "${git_revision}" else echo "source directory ${dir} exists, but contains no patching file, or .git directory." rm -rf "${dir}" \ && git_clone_and_checkout_dir_revision "${dir}" "${git_revision}" "${git_repo_url}" fi else git_clone_and_checkout_dir_revision "${dir}" "${git_revision}" "${git_repo_url}" fi } llvm_check_dir_maybe_checkout() { local dir="$1" local git_revision="$2" local git_repo_url="$3" check_dir_maybe_checkout "${dir}" "${git_revision}" "${git_repo_url}" \ && if [ -f "${dir}"/patching ]; then # change CLANG_VERSION from git true fi } gcc_check_dir_maybe_checkout() { local dir="$1" local git_revision="$2" local git_repo_url="$3" check_dir_maybe_checkout "${dir}" "${git_revision}" "${git_repo_url}" \ && if [ -f "${dir}"/patching ]; then # change GCC_VERSION from git true fi } check_dir_maybe_remove() { local dir="$1" if [ -d "${dir}" ]; then if [ -d "${dir}"/.git ]; then # do nothing true else rm -rf "${dir}" fi else rm -rf "${dir}" fi } check_dir_maybe_checkout_branch() { local dir="$1" local branch="$2" if [ -d "${dir}" ]; then if [ -d "${dir}"/.git ]; then if [ -f "${dir}"/patching ]; then # do nothing true else time ( cd "${dir}" && git checkout "${branch}" && git reset --hard HEAD ) fi else # do nothing true fi else echo "dir ${dir} does not exist" false fi } maybe_make_tar_ball_and_move() { local build_type="$1" local tar_ball_name="$2" local prefix_dir="$3" local host_name="$4" if [ "${build_type}" != Debug ]; then rm -rf "${tar_ball_name}" \ && pushd "${prefix_dir}" \ && tar -cvf "../${tar_ball_name}" * \ && popd \ \ && mkdir -p "${host_name}" \ && mv -f "${tar_ball_name}" "${host_name}" fi } download_and_verify_source_tar_ball() { local url="$1" local base_name="$(basename "${url}")" if [ ! -f "${base_name}" ]; then wget --quiet "${url}" fi \ && if [ ! -f "${base_name}".sha512 ]; then sha512sum "${base_name}" >"${base_name}".sha512 else sha512sum --check --status "${base_name}".sha512 fi } push_to_build_dir_and_cmake() { local build_dir="$1" shift 1 # Removes $1 from the parameter list # https://stackoverflow.com/questions/12985178/bash-quoted-array-expansion # echo "cmake options: " "${@@Q}" echo "cmake options: " "$@" rm -rf "${build_dir}" \ && mkdir "${build_dir}" \ && pushd "${build_dir}" \ && cmake "$@" } push_to_build_dir_and_configure() { local build_dir="$1" local source_dir="$2" shift 2 # Removes $1 to $2 from the parameter list # echo "configure options: " "${@@Q}" echo "configure options: " "$@" rm -rf "${build_dir}" \ && mkdir "${build_dir}" \ && pushd "${build_dir}" \ && ../"${source_dir}"/configure "$@" } extract_tar_ball() { local tar_ball="$1" local extracted_dir="$2" local source_dir="$3" rm -rf "${extracted_dir}" "${source_dir}" \ && tar -xvf "${tar_ball}" \ && mv "${extracted_dir}" "${source_dir}" } extract_configure_build_and_install_package() { local package_name="$1" local tar_ball="$2" local extracted_dir="$3" local build_dir="$4" # https://stackoverflow.com/questions/6212219/passing-parameters-to-a-bash-function shift 4 # Removes $1 to $4 from the parameter list local source_dir="${package_name}" extract_tar_ball "${tar_ball}" "${extracted_dir}" "${source_dir}" \ && push_to_build_dir_and_configure "${build_dir}" "${source_dir}" "$@" \ && parallel_make \ && parallel_make install \ && popd } gcc_push_to_build_dir_and_configure() { local build_dir="$1" local source_dir="$2" local install_dir="$3" local languages="$4" local host_name="$5" shift 5 # Removes $1 to $5 from the parameter list # https://www.linuxfromscratch.org/lfs/view/7.1/chapter06/gcc.html # gcc/Makefile.in # # Control whether to run fixincludes. # STMP_FIXINC = @STMP_FIXINC@ # sed -i -e 's,@STMP_FIXINC@,,g' "${source_dir}/gcc/Makefile.in" #--disable-fixincludes # make[2]: *** No rule to make target '../build-x86_64-pc-cygwin/fixincludes/fixinc.sh', needed by 'stmp-fixinc'. Stop. local build_fixincludes_dir="build-${host_name}/fixincludes" push_to_build_dir_and_configure "${build_dir}" "${source_dir}" \ --prefix="$(pwd)/${install_dir}" \ --enable-languages="${languages}" \ --disable-bootstrap \ --disable-nls \ --disable-werror \ --disable-win32-registry \ --enable-checking=release \ --disable-fixincludes \ "$@" \ && mkdir -p "${build_fixincludes_dir}" && touch "${build_fixincludes_dir}/fixinc.sh" } gcc_test() { time_command parallel_make -k check 2>&1 | tee "../gcc_test_$(current_datetime).txt" sync }