FortranCInterface broken with GCC 12.0 if LTO is disabled
I did build a docker image based on Ubuntu 22.04 for cross compilation to Windows.
FROM ubuntu:22.04
WORKDIR /mnt
ENV SYSROOT=/sysroot/usr
ARG PKG_CONFIG_VERSION=0.29.2
ARG CMAKE_VERSION=3.23.2
ARG BINUTILS_VERSION=2.38
ARG MINGW_VERSION=10.0.0
ARG GCC_VERSION=12.1.0
ARG NASM_VERSION=2.15.05
SHELL [ "/bin/bash", "-c" ]
RUN set -ex \
\
&& apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get upgrade --no-install-recommends -y \
&& DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
ca-certificates \
gcc \
g++ \
zlib1g-dev \
libssl-dev \
libgmp-dev \
libmpfr-dev \
libmpc-dev \
libisl-dev \
xz-utils \
python3 \
python3-lxml \
python3-mako \
ninja-build \
meson \
gnupg \
bzip2 \
patch \
curl \
smbclient \
gnuplot \
doxygen \
graphviz \
graphicsmagick \
imagemagick \
inkscape \
nano \
gperf \
bison \
file \
flex \
make \
yasm \
wget \
zip \
git \
texinfo \
wine \
\
&& wget -q https://pkg-config.freedesktop.org/releases/pkg-config-${PKG_CONFIG_VERSION}.tar.gz -O - | tar -xz \
&& wget -q https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/cmake-${CMAKE_VERSION}.tar.gz -O - | tar -xz \
&& wget -q https://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VERSION}.tar.xz -O - | tar -xJ \
&& wget -q https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/mingw-w64-v${MINGW_VERSION}.tar.bz2 -O - | tar -xj \
&& wget -q https://ftp.gnu.org/gnu/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.xz -O - | tar -xJ \
&& wget -q https://www.nasm.us/pub/nasm/releasebuilds/${NASM_VERSION}/nasm-${NASM_VERSION}.tar.xz -O - | tar -xJ \
\
&& mkdir -p ${SYSROOT}/include ${SYSROOT}/lib/pkgconfig \
&& chmod 0777 -R /mnt ${SYSROOT} \
\
&& cd pkg-config-${PKG_CONFIG_VERSION} \
&& ./configure \
--prefix=/usr/local \
--with-pc-path=${SYSROOT}/lib/pkgconfig \
--with-internal-glib \
--disable-shared \
--disable-nls \
&& make -j $(nproc) \
&& make install \
&& cd .. \
\
&& cd cmake-${CMAKE_VERSION} \
&& ./configure \
--prefix=/usr/local \
--parallel=$(nproc) \
&& make -j $(nproc) \
&& make install \
&& cd .. \
\
&& cd binutils-${BINUTILS_VERSION} \
&& ./configure \
--prefix=/usr/local \
--target=x86_64-w64-mingw32 \
--disable-shared \
--enable-static \
--disable-lto \
--disable-plugins \
--disable-multilib \
--disable-nls \
--disable-werror \
--with-system-zlib \
&& make -j $(nproc) \
&& make install \
&& cd .. \
\
&& mkdir mingw-w64 \
&& cd mingw-w64 \
&& ../mingw-w64-v${MINGW_VERSION}/mingw-w64-headers/configure \
--prefix=/usr/local/x86_64-w64-mingw32 \
--host=x86_64-w64-mingw32 \
--enable-sdk=all \
--enable-secure-api \
&& make install \
&& cd .. \
&& ln -s /usr/local/x86_64-w64-mingw32/include/windows.h /usr/local/x86_64-w64-mingw32/include/Windows.h \
&& ln -s /usr/local/x86_64-w64-mingw32/include/setupapi.h /usr/local/x86_64-w64-mingw32/include/SetupAPI.h \
\
&& mkdir gcc \
&& cd gcc \
&& ../gcc-${GCC_VERSION}/configure \
--prefix=/usr/local \
--target=x86_64-w64-mingw32 \
--enable-languages=c,c++,fortran \
--disable-shared \
--enable-static \
--enable-threads=posix \
--with-system-zlib \
--enable-libgomp \
--enable-libatomic \
--enable-graphite \
--disable-libstdcxx-pch \
--disable-libstdcxx-debug \
--disable-multilib \
--disable-lto \
--disable-nls \
--disable-werror \
&& make -j $(nproc) all-gcc \
&& make install-gcc \
&& cd .. \
\
&& cd mingw-w64 \
&& ../mingw-w64-v${MINGW_VERSION}/mingw-w64-crt/configure \
--prefix=/usr/local/x86_64-w64-mingw32 \
--host=x86_64-w64-mingw32 \
--enable-wildcard \
--disable-lib32 \
--enable-lib64 \
&& make -j $(nproc) \
&& make install \
&& cd .. \
\
&& cd mingw-w64 \
&& ../mingw-w64-v${MINGW_VERSION}/mingw-w64-libraries/winpthreads/configure \
--prefix=/usr/local/x86_64-w64-mingw32 \
--host=x86_64-w64-mingw32 \
--enable-static \
--disable-shared \
&& make -j $(nproc) \
&& make install \
&& cd .. \
\
&& cd gcc \
&& make -j $(nproc) \
&& make install \
&& cd .. \
\
&& cd nasm-${NASM_VERSION} \
&& ./configure --prefix=/usr/local \
&& make -j $(nproc) \
&& make install \
&& cd .. \
\
&& rm -r pkg-config-${PKG_CONFIG_VERSION} \
&& rm -r cmake-${CMAKE_VERSION} \
&& rm -r binutils-${BINUTILS_VERSION} \
&& rm -r mingw-w64 mingw-w64-v${MINGW_VERSION} \
&& rm -r gcc gcc-${GCC_VERSION} \
&& rm -r nasm-${NASM_VERSION} \
\
&& apt-get remove --purge -y file zlib1g-dev libssl-dev libgmp-dev libmpfr-dev libmpc-dev libisl-dev python3-lxml python3-mako \
\
&& apt-get remove --purge -y gnupg \
&& apt-get autoremove --purge -y \
&& apt-get clean
ENV CMAKE_VARS=" -Wno-dev -DCMAKE_BUILD_TYPE=Release -DCMAKE_SYSTEM_PROCESSOR=x86_64 -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_VERSION=10.0 -DCMAKE_INSTALL_PREFIX=$SYSROOT -DCMAKE_FIND_ROOT_PATH=$SYSROOT -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++ -DCMAKE_RC_COMPILER=x86_64-w64-mingw32-windres -DCMAKE_Fortran_COMPILER=x86_64-w64-mingw32-gfortran -DCMAKE_FIND_ROOT_PATH=/sysroot/usr;/usr/local/x86_64-w64-mingw32" \
CONFIGURE_VARS="--host=x86_64-w64-mingw32 --with-sysroot=$SYSROOT --prefix=$SYSROOT"
Based on this I try to build lapack via:
LAPACK_VERSION=3.10.1
# download
cd /mnt
curl -L https://github.com/Reference-LAPACK/lapack/archive/v${LAPACK_VERSION}.tar.gz | tar xz
# build, install
mkdir -p /mnt/build && cd $_
cmake $CMAKE_VARS /mnt/lapack-${LAPACK_VERSION}
Cmake fails with:
-- The Fortran compiler identification is GNU 12.1.0
-- The C compiler identification is GNU 12.1.0
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Check for working Fortran compiler: /usr/local/bin/x86_64-w64-mingw32-gfortran - skipped
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/local/bin/x86_64-w64-mingw32-gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Checking if build type is 'Coverage'
-- Checking if build type is 'Coverage': 0
-- Performing Test _frecursiveFlag
-- Performing Test _frecursiveFlag - Success
-- Build tests: OFF
-- Reducing RELEASE optimization level to O2
-- Looking for Fortran NONE - found
-- Looking for Fortran INT_CPU_TIME - found
-- Looking for Fortran EXT_ETIME - not found
-- Looking for Fortran EXT_ETIME_ - not found
-- Looking for Fortran INT_ETIME - found
-- --> Will use second_INT_ETIME.f and dsecnd_INT_ETIME.f as timing function.
-- Build deprecated routines: OFF
-- Build single precision real: ON
-- Build double precision real: ON
-- Build single precision complex: ON
-- Build double precision complex: ON
-- Using supplied NETLIB BLAS implementation
-- Using supplied NETLIB LAPACK implementation
-- Detecting Fortran/C Interface
Failed to compile
-- Verifying Fortran/C Compiler Compatibility
Failed to compile
-- Verifying Fortran/C Compiler Compatibility - Failed
CMake Error at /usr/local/share/cmake-3.23/Modules/FortranCInterface.cmake:400 (message):
The Fortran compiler:
/usr/local/bin/x86_64-w64-mingw32-gfortran
and the C compiler:
/usr/local/bin/x86_64-w64-mingw32-gcc
failed to compile a simple test project using both languages. The output
was:
Change Dir: /mnt/build/CMakeFiles/FortranCInterface/VerifyC
Run Build Command(s):/usr/bin/gmake -f Makefile VerifyFortranC && /usr/local/bin/cmake -S/usr/local/share/cmake-3.23/Modules/FortranCInterface/Verify -B/mnt/build/CMakeFiles/FortranCInterface/VerifyC --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/gmake -f CMakeFiles/Makefile2 VerifyFortranC
gmake[1]: Entering directory '/mnt/build/CMakeFiles/FortranCInterface/VerifyC'
/usr/local/bin/cmake -S/usr/local/share/cmake-3.23/Modules/FortranCInterface/Verify -B/mnt/build/CMakeFiles/FortranCInterface/VerifyC --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/bin/cmake -E cmake_progress_start /mnt/build/CMakeFiles/FortranCInterface/VerifyC/CMakeFiles 5
/usr/bin/gmake -f CMakeFiles/Makefile2 CMakeFiles/VerifyFortranC.dir/all
gmake[2]: Entering directory '/mnt/build/CMakeFiles/FortranCInterface/VerifyC'
/usr/bin/gmake -f CMakeFiles/VerifyFortran.dir/build.make CMakeFiles/VerifyFortran.dir/depend
gmake[3]: Entering directory '/mnt/build/CMakeFiles/FortranCInterface/VerifyC'
cd /mnt/build/CMakeFiles/FortranCInterface/VerifyC && /usr/local/bin/cmake -E cmake_depends "Unix Makefiles" /usr/local/share/cmake-3.23/Modules/FortranCInterface/Verify /usr/local/share/cmake-3.23/Modules/FortranCInterface/Verify /mnt/build/CMakeFiles/FortranCInterface/VerifyC /mnt/build/CMakeFiles/FortranCInterface/VerifyC /mnt/build/CMakeFiles/FortranCInterface/VerifyC/CMakeFiles/VerifyFortran.dir/DependInfo.cmake
Scanning dependencies of target VerifyFortran
gmake[3]: Leaving directory '/mnt/build/CMakeFiles/FortranCInterface/VerifyC'
/usr/bin/gmake -f CMakeFiles/VerifyFortran.dir/build.make CMakeFiles/VerifyFortran.dir/build
gmake[3]: Entering directory '/mnt/build/CMakeFiles/FortranCInterface/VerifyC'
[ 20%] Building Fortran object CMakeFiles/VerifyFortran.dir/VerifyFortran.f.o
/usr/local/bin/x86_64-w64-mingw32-gfortran @CMakeFiles/VerifyFortran.dir/includes_Fortran.rsp -frecursive -O2 -DNDEBUG -O2 -c /usr/local/share/cmake-3.23/Modules/FortranCInterface/Verify/VerifyFortran.f -o CMakeFiles/VerifyFortran.dir/VerifyFortran.f.o
[ 40%] Linking Fortran static library libVerifyFortran.a
/usr/local/bin/cmake -P CMakeFiles/VerifyFortran.dir/cmake_clean_target.cmake
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/VerifyFortran.dir/link.txt --verbose=1
/usr/local/bin/x86_64-w64-mingw32-ar qc libVerifyFortran.a CMakeFiles/VerifyFortran.dir/VerifyFortran.f.o
/usr/local/bin/x86_64-w64-mingw32-ranlib libVerifyFortran.a
gmake[3]: Leaving directory '/mnt/build/CMakeFiles/FortranCInterface/VerifyC'
[ 40%] Built target VerifyFortran
/usr/bin/gmake -f CMakeFiles/VerifyFortranC.dir/build.make CMakeFiles/VerifyFortranC.dir/depend
gmake[3]: Entering directory '/mnt/build/CMakeFiles/FortranCInterface/VerifyC'
cd /mnt/build/CMakeFiles/FortranCInterface/VerifyC && /usr/local/bin/cmake -E cmake_depends "Unix Makefiles" /usr/local/share/cmake-3.23/Modules/FortranCInterface/Verify /usr/local/share/cmake-3.23/Modules/FortranCInterface/Verify /mnt/build/CMakeFiles/FortranCInterface/VerifyC /mnt/build/CMakeFiles/FortranCInterface/VerifyC /mnt/build/CMakeFiles/FortranCInterface/VerifyC/CMakeFiles/VerifyFortranC.dir/DependInfo.cmake
Scanning dependencies of target VerifyFortranC
gmake[3]: Leaving directory '/mnt/build/CMakeFiles/FortranCInterface/VerifyC'
/usr/bin/gmake -f CMakeFiles/VerifyFortranC.dir/build.make CMakeFiles/VerifyFortranC.dir/build
gmake[3]: Entering directory '/mnt/build/CMakeFiles/FortranCInterface/VerifyC'
[ 60%] Building C object CMakeFiles/VerifyFortranC.dir/main.c.obj
/usr/local/bin/x86_64-w64-mingw32-gcc @CMakeFiles/VerifyFortranC.dir/includes_C.rsp -O3 -DNDEBUG -o CMakeFiles/VerifyFortranC.dir/main.c.obj -c /usr/local/share/cmake-3.23/Modules/FortranCInterface/Verify/main.c
[ 80%] Building C object CMakeFiles/VerifyFortranC.dir/VerifyC.c.obj
/usr/local/bin/x86_64-w64-mingw32-gcc @CMakeFiles/VerifyFortranC.dir/includes_C.rsp -O3 -DNDEBUG -o CMakeFiles/VerifyFortranC.dir/VerifyC.c.obj -c /usr/local/share/cmake-3.23/Modules/FortranCInterface/Verify/VerifyC.c
[100%] Linking C executable VerifyFortranC.exe
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/VerifyFortranC.dir/link.txt --verbose=1
/usr/local/bin/cmake -E rm -f CMakeFiles/VerifyFortranC.dir/objects.a
/usr/local/bin/x86_64-w64-mingw32-ar qc CMakeFiles/VerifyFortranC.dir/objects.a @CMakeFiles/VerifyFortranC.dir/objects1.rsp
/usr/local/bin/x86_64-w64-mingw32-gcc -O3 -DNDEBUG -Wl,--whole-archive CMakeFiles/VerifyFortranC.dir/objects.a -Wl,--no-whole-archive -o VerifyFortranC.exe -Wl,--out-implib,libVerifyFortranC.dll.a -Wl,--major-image-version,0,--minor-image-version,0 @CMakeFiles/VerifyFortranC.dir/linklibs.rsp
/usr/local/lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/VerifyFortranC.dir/objects.a(main.c.obj):main.c:(.text.startup+0xf): undefined reference to `VerifyFortran'
collect2: error: ld returned 1 exit status
gmake[3]: *** [CMakeFiles/VerifyFortranC.dir/build.make:119: VerifyFortranC.exe] Error 1
gmake[3]: Leaving directory '/mnt/build/CMakeFiles/FortranCInterface/VerifyC'
gmake[2]: *** [CMakeFiles/Makefile2:114: CMakeFiles/VerifyFortranC.dir/all] Error 2
gmake[2]: Leaving directory '/mnt/build/CMakeFiles/FortranCInterface/VerifyC'
gmake[1]: *** [CMakeFiles/Makefile2:121: CMakeFiles/VerifyFortranC.dir/rule] Error 2
gmake[1]: Leaving directory '/mnt/build/CMakeFiles/FortranCInterface/VerifyC'
gmake: *** [Makefile:140: VerifyFortranC] Error 2
Call Stack (most recent call first):
LAPACKE/CMakeLists.txt:4 (FortranCInterface_VERIFY)
-- Configuring incomplete, errors occurred!
See also "/mnt/build/CMakeFiles/CMakeOutput.log".
See also "/mnt/build/CMakeFiles/CMakeError.log".
If your replace GCC_VERSION=12.1.0
with GCC_VERSION=11.2.0
in the Dockerfile
, everything works fine. If your enable LTO during GCC configuration also everything works as expected.
Edited by Benjamin Buch