From f0e05bde25952f6d4f9ab3aa8e002273bf0fea8b Mon Sep 17 00:00:00 2001 From: Vasily Date: Thu, 9 Apr 2020 12:10:10 +0200 Subject: [PATCH] Add link time optimization to clang/gcc builds Using `-flto` on clang without using the gold linker produces the error: ```main.o: file not recognized: File format not recognized``` https://github.com/android/ndk/issues/242 `gold` seems to be faster than `ld`, and decently tested even in 2010: https://stackoverflow.com/questions/3476093/replacing-ld-with-gold-any-experience Qmake will still append `-Wl,-O1` even though higher optimization levels are used. Removing this in order to keep the compile command as short as possible, and to reduce weird issues down the line. Ref topic: https://stackoverflow.com/questions/53384988/why-qmake-passes-wl-o1-to-gcc-when-linking-and-will-it-harm-lto --- build_scripts/qt/compilers/clang-gcc-common-switches.pri | 5 +++++ build_scripts/qt/compilers/gcc.pri | 3 +++ 2 files changed, 8 insertions(+) diff --git a/build_scripts/qt/compilers/clang-gcc-common-switches.pri b/build_scripts/qt/compilers/clang-gcc-common-switches.pri index fca9988..c7c2a83 100644 --- a/build_scripts/qt/compilers/clang-gcc-common-switches.pri +++ b/build_scripts/qt/compilers/clang-gcc-common-switches.pri @@ -9,3 +9,8 @@ QMAKE_CXXFLAGS += -Wnon-virtual-dtor QMAKE_CXXFLAGS += -Wcast-align -Wunused -Woverloaded-virtual -Wformat=2 QMAKE_CXXFLAGS += -Wdouble-promotion + +# Enable Link Time Optimization +QMAKE_CXXFLAGS += -flto +# Clang LTO requires the gold linker instead of ld +QMAKE_LFLAGS += -flto -fuse-ld=gold diff --git a/build_scripts/qt/compilers/gcc.pri b/build_scripts/qt/compilers/gcc.pri index 2c49bd5..f3e2f7a 100644 --- a/build_scripts/qt/compilers/gcc.pri +++ b/build_scripts/qt/compilers/gcc.pri @@ -31,3 +31,6 @@ QMAKE_CXXFLAGS += -Wduplicated-branches -Wduplicated-cond -Wlogical-op -Wrestric QMAKE_CXXFLAGS += -Wconversion -Wno-sign-conversion QMAKE_CXXFLAGS += -Wzero-as-null-pointer-constant + +# Qmake will append this even though higher optimization levels are used. +QMAKE_LFLAGS_RELEASE -= -Wl,-O1