Skip to content

cmake does not uses AR, RANLIB and NM environment variables which breaks use LTO with static libraries

I'm trying to use cmake with LTO optimisation and seems like current version of the cmake breaks LTO. To use it it is necessary to use gcc wrappers. Usual procedure witch autotools is pass to the env:

AR=gcc-ar RANLIB=gcc-ranlib NM=gcc-nm

than start cmake witch all configuration parameters to generate Makefile files and then start build. Despite that above variables are passed seems like all Makefiles are not using those gcc wrappers which is cause breaking static libraries generation

Fragments from my build log:

+ export 'CFLAGS=-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches
 -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -
fcf-protection -flto'
+ CFLAGS='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs
=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-pro
tection -flto'
+ export 'CXXFLAGS=-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switch
es -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection
 -fcf-protection -flto'
+ CXXFLAGS='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -spe
cs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-p
rotection -flto'
+ export 'FFLAGS=-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches
 -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -
fcf-protection -I/usr/lib64/gfortran/modules -flto'
+ FFLAGS='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs
=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-pro
tection -I/usr/lib64/gfortran/modules -flto'
+ export 'FCFLAGS=-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switche
s -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection
-fcf-protection -I/usr/lib64/gfortran/modules -flto'
+ FCFLAGS='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -spec
s=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-pr
otection -I/usr/lib64/gfortran/modules -flto'
+ export 'LDFLAGS=-Wl,-z,relro -Wl,--as-needed  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -flto -fuse-linker-plugin'
+ LDFLAGS='-Wl,-z,relro -Wl,--as-needed  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -flto -fuse-linker-plugin'
+ export AR=gcc-ar RANLIB=gcc-ranlib NM=gcc-nm
+ AR=gcc-ar
+ RANLIB=gcc-ranlib
+ NM=gcc-nm
+ export CC=gcc CXX=g++
+ CC=gcc
+ CXX=g++
+ /usr/bin/cmake -DBUILD_SHARED_LIBS:BOOL=ON -DCMAKE_C_FLAGS_RELEASE:STRING=-DNDEBUG -DCMAKE_CXX_FLAGS_RELEASE:STRING=-DNDEBUG -DCMAKE_Fortran_FLAGS_RELEASE:STRING=-DNDE
BUG -DCMAKE_INSTALL_PREFIX:PATH=/usr -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DINCLUDE_INSTALL_DIR:PATH=/usr/include -DLIB_INSTALL_DIR:PATH=/us
r/lib64 -DSHARE_INSTALL_PREFIX:PATH=/usr/share -DSYSCONF_INSTALL_DIR:PATH=/etc -DLIB_SUFFIX=64 -G 'Unix Makefiles' -DENABLE_DOT_LOCKING=OFF -DENABLE_FILE_LOCKING=fcntl -
DENABLE_GTK_DOC=ON -DENABLE_INSTALLED_TESTS=ON -DENABLE_INTROSPECTION=ON -DENABLE_LARGEFILE=ON -DENABLE_MAINTAINER_MODE=OFF -DENABLE_SMIME=ON -DENABLE_UOA=OFF -DENABLE_V
ALA_BINDINGS=ON -DWITH_KRB5=ON -DWITH_LIBDB=/usr -DWITH_OPENLDAP=ON

[..]

cd /home/tkloczko/rpmbuild/BUILD/evolution-data-server-3.29.91/src/camel/tests/lib && /usr/bin/cmake -P CMakeFiles/cameltest-provider.dir/cmake_clean_target.cmake
cd /home/tkloczko/rpmbuild/BUILD/evolution-data-server-3.29.91/src/camel/tests/lib && /usr/bin/cmake -E cmake_link_script CMakeFiles/cameltest-provider.dir/link.txt --ve
/usr/bin/ar qc libcameltest-provider.a  CMakeFiles/cameltest-provider.dir/camel-test-provider.c.o
/usr/bin/ar: CMakeFiles/cameltest-provider.dir/camel-test-provider.c.o: plugin needed to handle lto object
/usr/bin/ranlib libcameltest-provider.a
/usr/bin/ranlib: libcameltest-provider.a(camel-test-provider.c.o): plugin needed to handle lto object

Other env variables like CFLAGS, LDFLAGS and other seems they are used correctly but this is not the case on use AR, RANLIB and NM.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information