From e2bdc4aeb6596d2e79cf9e154b8c9b9fb5e3cd19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Sch=C3=BCtt?= Date: Sun, 6 Jun 2021 20:35:50 +0200 Subject: [PATCH] Toolchain: Towards static linking --- tools/toolchain/scripts/generate_arch_files.sh | 7 ++++++- tools/toolchain/scripts/parse_if.py | 1 + tools/toolchain/scripts/stage0/install_gcc.sh | 9 +++++++++ tools/toolchain/scripts/stage1/install_mpich.sh | 11 ++++++++++- tools/toolchain/scripts/stage2/install_openblas.sh | 2 ++ tools/toolchain/scripts/stage3/install_fftw.sh | 2 +- tools/toolchain/scripts/stage6/install_gsl.sh | 2 +- tools/toolchain/scripts/stage6/install_plumed.sh | 7 ++++--- tools/toolchain/scripts/stage7/install_hdf5.sh | 2 +- tools/toolchain/scripts/stage7/install_spglib.sh | 9 ++++----- tools/toolchain/scripts/stage8/install_spfft.sh | 6 ++++-- 11 files changed, 43 insertions(+), 15 deletions(-) diff --git a/tools/toolchain/scripts/generate_arch_files.sh b/tools/toolchain/scripts/generate_arch_files.sh index 91ca5c8cb7..74ca6a9b1f 100755 --- a/tools/toolchain/scripts/generate_arch_files.sh +++ b/tools/toolchain/scripts/generate_arch_files.sh @@ -98,7 +98,10 @@ FCFLAGS="$G_CFLAGS \$(FCDEBFLAGS) \$(WFLAGS) \$(DFLAGS)" CFLAGS="$G_CFLAGS -std=c11 -Wall -Wextra -Werror \$(DFLAGS)" # Linker flags -LDFLAGS="\$(FCFLAGS) ${CP_LDFLAGS}" +# About --whole-archive see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52590 +STATIC_FLAGS="-static -Wl,--whole-archive -lpthread -Wl,--no-whole-archive" +# Get unfortunately ignored: -static-libgcc -static-libstdc++ -static-libgfortran +LDFLAGS="IF_STATIC(${STATIC_FLAGS}|) \$(FCFLAGS) ${CP_LDFLAGS}" # Library flags # add standard libs @@ -194,12 +197,14 @@ EOF rm -f ${INSTALLDIR}/arch/local* # normal production arch files gen_arch_file "local.ssmp" +gen_arch_file "local_static.ssmp" STATIC gen_arch_file "local.sdbg" DEBUG arch_vers="ssmp sdbg" if [ "$MPI_MODE" != no ]; then gen_arch_file "local.psmp" MPI gen_arch_file "local.pdbg" MPI DEBUG + gen_arch_file "local_static.psmp" MPI STATIC gen_arch_file "local_warn.psmp" MPI WARNALL gen_arch_file "local_coverage.pdbg" MPI COVERAGE arch_vers="${arch_vers} psmp pdbg" diff --git a/tools/toolchain/scripts/parse_if.py b/tools/toolchain/scripts/parse_if.py index 03e74845a4..f7851a42ab 100755 --- a/tools/toolchain/scripts/parse_if.py +++ b/tools/toolchain/scripts/parse_if.py @@ -138,6 +138,7 @@ if __name__ == "__main__": "CUDA": False, "WARNALL": False, "DEBUG": False, + "STATIC": False, "VALGRIND": False, "COVERAGE": False, } diff --git a/tools/toolchain/scripts/stage0/install_gcc.sh b/tools/toolchain/scripts/stage0/install_gcc.sh index ec16add5bb..96b03aacec 100755 --- a/tools/toolchain/scripts/stage0/install_gcc.sh +++ b/tools/toolchain/scripts/stage0/install_gcc.sh @@ -62,6 +62,15 @@ case "$with_gcc" in GCCROOT=${PWD} mkdir obj cd obj + # TODO: Maybe use --disable-libquadmath-support to improve static linking: + # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46539 + # + # TODO: Maybe use --disable-gnu-unique-object to improve static linking: + # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60348#c13 + # https://stackoverflow.com/questions/11931420 + # + # TODO: Unfortunately, we can not simply use --disable-shared, because + # it would break OpenBLAS build and probably others too. ${GCCROOT}/configure --prefix="${pkg_install_dir}" \ --libdir="${pkg_install_dir}/lib" \ --enable-languages=c,c++,fortran \ diff --git a/tools/toolchain/scripts/stage1/install_mpich.sh b/tools/toolchain/scripts/stage1/install_mpich.sh index ed956d79f9..7a8b459858 100755 --- a/tools/toolchain/scripts/stage1/install_mpich.sh +++ b/tools/toolchain/scripts/stage1/install_mpich.sh @@ -52,7 +52,16 @@ case "$with_mpich" in # workaround for compilation with GCC-10, until properly fixed: # https://github.com/pmodels/mpich/issues/4300 ("${FC}" --version | grep -Eq 'GNU.+\s10\.') && compat_flag="-fallow-argument-mismatch" || compat_flag="" - ./configure --prefix="${pkg_install_dir}" --libdir="${pkg_install_dir}/lib" MPICC="" FFLAGS="${FCFLAGS} ${compat_flag}" FCFLAGS="${FCFLAGS} ${compat_flag}" --without-x --enable-gl=no > configure.log 2>&1 + ./configure \ + --prefix="${pkg_install_dir}" \ + --libdir="${pkg_install_dir}/lib" \ + MPICC="" \ + FFLAGS="${FCFLAGS} ${compat_flag}" \ + FCFLAGS="${FCFLAGS} ${compat_flag}" \ + --without-x \ + --enable-gl=no \ + --disable-shared \ + > configure.log 2>&1 make -j $(get_nprocs) > make.log 2>&1 make install > install.log 2>&1 cd .. diff --git a/tools/toolchain/scripts/stage2/install_openblas.sh b/tools/toolchain/scripts/stage2/install_openblas.sh index fdaf17f249..5e7a18c0f4 100755 --- a/tools/toolchain/scripts/stage2/install_openblas.sh +++ b/tools/toolchain/scripts/stage2/install_openblas.sh @@ -54,6 +54,8 @@ case "$with_openblas" in # # wrt NUM_THREADS=64: this is what the most common Linux distros seem to choose atm # for a good compromise between memory usage and scalability + # + # Unfortunately, NO_SHARED=1 breaks ScaLAPACK build. ( make -j $(get_nprocs) \ MAKE_NB_JOBS=0 \ diff --git a/tools/toolchain/scripts/stage3/install_fftw.sh b/tools/toolchain/scripts/stage3/install_fftw.sh index 3076c5b783..535abf4f5d 100755 --- a/tools/toolchain/scripts/stage3/install_fftw.sh +++ b/tools/toolchain/scripts/stage3/install_fftw.sh @@ -47,7 +47,7 @@ case "$with_fftw" in [ -d fftw-${fftw_ver} ] && rm -rf fftw-${fftw_ver} tar -xzf ${fftw_pkg} cd fftw-${fftw_ver} - FFTW_FLAGS="--enable-openmp --enable-shared --enable-static" + FFTW_FLAGS="--enable-openmp --disable-shared --enable-static" # fftw has mpi support but not compiled by default. so compile it if we build with mpi. # it will create a second library to link with if needed [ "$MPI_MODE" != "no" ] && FFTW_FLAGS="--enable-mpi ${FFTW_FLAGS}" diff --git a/tools/toolchain/scripts/stage6/install_gsl.sh b/tools/toolchain/scripts/stage6/install_gsl.sh index a8f3257acf..f4d06e7448 100755 --- a/tools/toolchain/scripts/stage6/install_gsl.sh +++ b/tools/toolchain/scripts/stage6/install_gsl.sh @@ -42,7 +42,7 @@ case "$with_gsl" in cd gsl-${gsl_ver} ./configure --prefix="${pkg_install_dir}" \ --libdir="${pkg_install_dir}/lib" \ - --enable-shared \ + --disable-shared \ --enable-static > configure.log 2>&1 make -j $(get_nprocs) > make.log 2>&1 make -j $(get_nprocs) install > install.log 2>&1 diff --git a/tools/toolchain/scripts/stage6/install_plumed.sh b/tools/toolchain/scripts/stage6/install_plumed.sh index 81d64bf5af..fcff04a741 100755 --- a/tools/toolchain/scripts/stage6/install_plumed.sh +++ b/tools/toolchain/scripts/stage6/install_plumed.sh @@ -59,6 +59,7 @@ case "$with_plumed" in CXXFLAGS="${CXXFLAGS//-g/-g0} ${GSL_CFLAGS}" \ LDFLAGS="${LDFLAGS} ${GSL_LDFLAGS}" \ LIBS="${libs}" \ + --disable-shared \ --prefix=${pkg_install_dir} \ --libdir="${pkg_install_dir}/lib" > configure.log 2>&1 make VERBOSE=1 -j $(get_nprocs) > make.log 2>&1 @@ -96,9 +97,9 @@ EOF cat << EOF >> "${BUILDDIR}/setup_plumed" export PLUMED_LDFLAGS="${PLUMED_LDFLAGS}" export PLUMED_LIBS="${PLUMED_LIBS}" -export CP_DFLAGS="\${CP_DFLAGS} -D__PLUMED2" -export CP_LDFLAGS="\${CP_LDFLAGS} ${PLUMED_LDFLAGS}" -export CP_LIBS="${PLUMED_LIBS} \${CP_LIBS}" +export CP_DFLAGS="\${CP_DFLAGS} IF_MPI(-D__PLUMED2|)" +export CP_LDFLAGS="\${CP_LDFLAGS} IF_MPI(${PLUMED_LDFLAGS}|)" +export CP_LIBS="IF_MPI(${PLUMED_LIBS}|) \${CP_LIBS}" EOF fi diff --git a/tools/toolchain/scripts/stage7/install_hdf5.sh b/tools/toolchain/scripts/stage7/install_hdf5.sh index 718bb08911..490a91ad60 100755 --- a/tools/toolchain/scripts/stage7/install_hdf5.sh +++ b/tools/toolchain/scripts/stage7/install_hdf5.sh @@ -44,7 +44,7 @@ case "$with_hdf5" in ./configure \ --prefix="${pkg_install_dir}" \ --libdir="${pkg_install_dir}/lib" \ - --enable-shared \ + --disable-shared \ > configure.log 2>&1 make -j $(get_nprocs) > make.log 2>&1 make -j $(get_nprocs) install > install.log 2>&1 diff --git a/tools/toolchain/scripts/stage7/install_spglib.sh b/tools/toolchain/scripts/stage7/install_spglib.sh index 40deaa67d9..b8310256ed 100755 --- a/tools/toolchain/scripts/stage7/install_spglib.sh +++ b/tools/toolchain/scripts/stage7/install_spglib.sh @@ -48,13 +48,12 @@ case "$with_spglib" in cmake \ -DCMAKE_INSTALL_PREFIX="${pkg_install_dir}" \ -DCMAKE_BUILD_TYPE=Release \ - -DBUILD_SHARED_LIBS=OFF \ + -DBUILD_SHARED_LIBS=NO \ .. > configure.log 2>&1 - make -j $(get_nprocs) > make.log 2>&1 + make -j $(get_nprocs) symspg > make.log 2>&1 make install >> make.log 2>&1 - mkdir ${pkg_install_dir}/include/spglib - cp ${pkg_install_dir}/include/{,spglib/}spglib.h - cd .. + # Despite -DBUILD_SHARED_LIBS=NO the shared library gets build and installed. + rm "${pkg_install_dir}"/lib/*.so* write_checksums "${install_lock_file}" "${SCRIPT_DIR}/stage7/$(basename ${SCRIPT_NAME})" fi diff --git a/tools/toolchain/scripts/stage8/install_spfft.sh b/tools/toolchain/scripts/stage8/install_spfft.sh index e9ae00a2df..18a4d9dd7b 100755 --- a/tools/toolchain/scripts/stage8/install_spfft.sh +++ b/tools/toolchain/scripts/stage8/install_spfft.sh @@ -47,9 +47,10 @@ case "$with_spfft" in cmake \ -DCMAKE_INSTALL_PREFIX="${pkg_install_dir}" \ -DCMAKE_INSTALL_LIBDIR=lib \ - -DBUILD_SHARED_LIBS=OFF \ + -DCMAKE_CXX_COMPILER="${MPICXX}" \ -DSPFFT_OMP=ON \ -DSPFFT_MPI=ON \ + -DSPFFT_STATIC=ON \ -DSPFFT_INSTALL=ON \ .. > cmake.log 2>&1 make -j $(get_nprocs) > make.log 2>&1 @@ -63,9 +64,10 @@ case "$with_spfft" in cmake \ -DCMAKE_INSTALL_PREFIX="${pkg_install_dir}" \ -DCMAKE_INSTALL_LIBDIR=lib \ - -DBUILD_SHARED_LIBS=OFF \ + -DCMAKE_CXX_COMPILER="${MPICXX}" \ -DSPFFT_OMP=ON \ -DSPFFT_MPI=ON \ + -DSPFFT_STATIC=ON \ -DSPFFT_INSTALL=ON \ -DSPFFT_GPU_BACKEND=CUDA \ .. > cmake.log 2>&1