Add link-time optimization support for Clang, GCC and ICC

GCC currently requires fat object files for static libraries, since the
linker would otherwise not load the .o file from the archive at all and
the linking would fail with a lot of undefined references. Clang on
Linux also needs this, but it has no equivalent flag, so enabling LTCG
for Clang on static libraries will result in linker error.

This commit does not add support for enabling it in configure. It can be
enabled on a per-project basis by doing CONFIG += ltcg or by passing
-config ltcg to qmake's command-line.

Change-Id: I52cf99f1ed9f1701e23a3b457ba3502fd28126ce
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com>
This commit is contained in:
Thiago Macieira 2014-06-26 14:56:06 -07:00 committed by hjk
parent 849d1fece1
commit 1bd27f24fb
7 changed files with 71 additions and 6 deletions

View File

@ -16,8 +16,12 @@ QMAKE_PCH_OUTPUT_EXT = .pch
QMAKE_CFLAGS_ISYSTEM = -isystem
QMAKE_CFLAGS_PRECOMPILE = -x c-header -c ${QMAKE_PCH_INPUT} -o ${QMAKE_PCH_OUTPUT}
QMAKE_CFLAGS_USE_PRECOMPILE = -Xclang -include-pch -Xclang ${QMAKE_PCH_OUTPUT}
QMAKE_CFLAGS_LTCG = -flto
QMAKE_CXXFLAGS_PRECOMPILE = -x c++-header -c ${QMAKE_PCH_INPUT} -o ${QMAKE_PCH_OUTPUT}
QMAKE_CXXFLAGS_USE_PRECOMPILE = $$QMAKE_CFLAGS_USE_PRECOMPILE
QMAKE_CXXFLAGS_LTCG = $$QMAKE_CFLAGS_LTCG
QMAKE_CXXFLAGS_CXX11 = -std=c++11
QMAKE_CXXFLAGS_CXX11 = -std=c++11
QMAKE_LFLAGS_CXX11 =
QMAKE_LFLAGS_LTCG = $$QMAKE_CFLAGS_LTCG

View File

@ -48,6 +48,7 @@ QMAKE_CFLAGS_YACC += -Wno-unused -Wno-parentheses
QMAKE_CFLAGS_HIDESYMS += -fvisibility=hidden
QMAKE_CFLAGS_EXCEPTIONS_OFF += -fno-exceptions
QMAKE_CFLAGS_SPLIT_SECTIONS += -ffunction-sections
QMAKE_CFLAGS_LTCG = -flto
QMAKE_CXXFLAGS += $$QMAKE_CFLAGS
QMAKE_CXXFLAGS_DEPS += $$QMAKE_CFLAGS_DEPS
@ -62,12 +63,14 @@ QMAKE_CXXFLAGS_YACC += $$QMAKE_CFLAGS_YACC
QMAKE_CXXFLAGS_HIDESYMS += $$QMAKE_CFLAGS_HIDESYMS -fvisibility-inlines-hidden
QMAKE_CXXFLAGS_EXCEPTIONS_OFF += $$QMAKE_CFLAGS_EXCEPTIONS_OFF
QMAKE_CXXFLAGS_SPLIT_SECTIONS += $$QMAKE_CFLAGS_SPLIT_SECTIONS
QMAKE_CXXFLAGS_LTCG = $$QMAKE_CFLAGS_LTCG
QMAKE_LFLAGS +=
QMAKE_LFLAGS_DEBUG +=
QMAKE_LFLAGS_APP +=
QMAKE_LFLAGS_RELEASE +=
QMAKE_LFLAGS_EXCEPTIONS_OFF +=
QMAKE_LFLAGS_LTCG = $$QMAKE_CFLAGS_LTCG -fuse-linker-plugin
QMAKE_CFLAGS_SSE2 += -msse2
QMAKE_CFLAGS_SSE3 += -msse3

54
mkspecs/features/ltcg.prf Normal file
View File

@ -0,0 +1,54 @@
CONFIG(release, debug|release) {
# We need fat object files when creating static libraries on some platforms
# so the linker will know to load a particular object from the library
# in the first place. With GCC, we have -ffat-lto-objects. MSVC
# seems to work just fine. For other compilers, we disable LTCG
# for static libraries.
msvc {
# Nothing to do
} else: gcc {
!clang:!intel_icc {
static|fat-lto {
QMAKE_CFLAGS_LTCG += -ffat-lto-objects
QMAKE_CXXFLAGS_LTCG += -ffat-lto-objects
} else {
QMAKE_CFLAGS_LTCG += -fno-fat-lto-objects
QMAKE_CXXFLAGS_LTCG += -fno-fat-lto-objects
}
linux {
# Get the number of online processors, like _SC_NPROCESSORS_ONLN
isEmpty(QMAKE_NPROCESSORS_ONLN) {
QMAKE_NPROCESSORS_ONLN = $$system("grep -c '^processor' /proc/cpuinfo 2>/dev/null || echo 1")
cache(QMAKE_NPROCESSORS_ONLN, set stash)
}
# Override LTO number of jobs
QMAKE_LFLAGS_LTCG -= -flto
QMAKE_LFLAGS_LTCG += -flto=$$QMAKE_NPROCESSORS_ONLN
}
} else: static {
QMAKE_CFLAGS_LTCG =
QMAKE_CXXFLAGS_LTCG =
QMAKE_LFLAGS_LTCG =
}
# When doing link-time code generation, we need to pass the compiler
# flags during linking stage too. This file is processed after
# default_post.prf, so the QMAKE_CXXFLAGS already contains
# QMAKE_CXXFLAGS_DEBUG or _RELEASE.
use_c_linker {
# use_c_linker.prf is in effect, use the C flags
QMAKE_LFLAGS_LTCG += $$QMAKE_CFLAGS $$QMAKE_CFLAGS_LTCG
QMAKE_LFLAGS_APP += $$QMAKE_CFLAGS_APP
QMAKE_LFLAGS_SHLIB += $$QMAKE_CFLAGS_SHLIB
} else {
QMAKE_LFLAGS_LTCG = $$QMAKE_CXXFLAGS $$QMAKE_LFLAGS_LTCG
QMAKE_LFLAGS_APP += $$QMAKE_CXXFLAGS_APP
QMAKE_LFLAGS_SHLIB += $$QMAKE_CXXFLAGS_SHLIB
}
}
QMAKE_CFLAGS *= $$QMAKE_CFLAGS_LTCG
QMAKE_CXXFLAGS *= $$QMAKE_CXXFLAGS_LTCG
QMAKE_LFLAGS *= $$QMAKE_LFLAGS_LTCG
}

View File

@ -1,5 +0,0 @@
CONFIG(release, debug|release) {
QMAKE_CFLAGS *= $$QMAKE_CFLAGS_LTCG
QMAKE_CXXFLAGS *= $$QMAKE_CXXFLAGS_LTCG
QMAKE_LFLAGS *= $$QMAKE_LFLAGS_LTCG
}

View File

@ -24,6 +24,7 @@ QMAKE_CFLAGS_YACC =
QMAKE_CFLAGS_ISYSTEM = -isystem
QMAKE_CFLAGS_THREAD = -D_REENTRANT
QMAKE_CFLAGS_SPLIT_SECTIONS = -ffunction-sections
QMAKE_CFLAGS_LTCG = -ipo
QMAKE_CFLAGS_SSE2 += -xSSE2
QMAKE_CFLAGS_SSE3 += -xSSE3
@ -49,6 +50,7 @@ QMAKE_CXXFLAGS_SPLIT_SECTIONS = $$QMAKE_CFLAGS_SPLIT_SECTIONS
# Disabling exceptions disabled - workaround for QTBUG-36577
#QMAKE_CXXFLAGS_EXCEPTIONS_OFF = -fno-exceptions
QMAKE_CXXFLAGS_CXX11 = -std=c++0x
QMAKE_CXXFLAGS_LTCG = $$QMAKE_CFLAGS_LTCG
QMAKE_INCDIR =
QMAKE_LIBDIR =
@ -70,6 +72,7 @@ QMAKE_LFLAGS_NOUNDEF = -Wl,-z,defs
QMAKE_LFLAGS_RPATH = -Wl,-rpath,
QMAKE_LFLAGS_RPATHLINK = -Wl,-rpath-link,
QMAKE_LFLAGS_CXX11 =
QMAKE_LFLAGS_LTCG = $$QMAKE_CFLAGS_LTCG
QMAKE_LIBS =
QMAKE_LIBS_DYNLOAD = -ldl

View File

@ -23,6 +23,7 @@ QMAKE_CFLAGS_STATIC_LIB = $$QMAKE_CFLAGS_SHLIB
QMAKE_CFLAGS_YACC = -Wno-unused -Wno-parentheses
QMAKE_CFLAGS_THREAD =
QMAKE_CFLAGS_SPLIT_SECTIONS = -ffunction-sections
QMAKE_CFLAGS_LTCG = -ipo
QMAKE_CFLAGS_SSE2 += -msse2
QMAKE_CFLAGS_SSE3 += -msse3
@ -54,6 +55,7 @@ QMAKE_CXXFLAGS_THREAD = $$QMAKE_CFLAGS_THREAD
# Disabled, due to invalid C++11 code on Apple headers
#QMAKE_CXXFLAGS_CXX11 = -std=c++11
QMAKE_CXXFLAGS_SPLIT_SECTIONS = $$QMAKE_CFLAGS_SPLIT_SECTIONS
QMAKE_CXXFLAGS_LTCG = $$QMAKE_CFLAGS_LTCG
QMAKE_LINK = icpc
QMAKE_LINK_SHLIB = icpc
@ -66,6 +68,7 @@ QMAKE_LFLAGS_PLUGIN = $$QMAKE_LFLAGS_SHLIB
QMAKE_LFLAGS_SONAME = -install_name$${LITERAL_WHITESPACE}
QMAKE_LFLAGS_THREAD =
QMAKE_LFLAGS_RPATH =
QMAKE_LFLAGS_LTCG = $$QMAKE_CFLAGS_LTCG
QMAKE_LFLAGS_VERSION = -current_version$${LITERAL_WHITESPACE}
QMAKE_LFLAGS_COMPAT_VERSION = -compatibility_version$${LITERAL_WHITESPACE}

View File

@ -23,6 +23,7 @@ QMAKE_CFLAGS_WARN_OFF = -W0 /Qwd673
QMAKE_CFLAGS_RELEASE = -O2 -MD
QMAKE_CFLAGS_DEBUG = -Zi -MDd -Od
QMAKE_CFLAGS_YACC =
QMAKE_CFLAGS_LTCG = -Qipo
QMAKE_CFLAGS_SSE2 = -QxSSE2
QMAKE_CFLAGS_SSE3 = -QxSSE3
QMAKE_CFLAGS_SSSE3 = -QxSSSE3
@ -45,6 +46,7 @@ QMAKE_CXXFLAGS_RTTI_OFF =
QMAKE_CXXFLAGS_EXCEPTIONS_ON = -EHsc
QMAKE_CXXFLAGS_EXCEPTIONS_OFF =
QMAKE_CXXFLAGS_CXX11 = -Qstd=c++11
QMAKE_CXXFLAGS_LTCG = $$QMAKE_CFLAGS_LTCG
QMAKE_INCDIR =
@ -62,6 +64,7 @@ QMAKE_LFLAGS_DEBUG = /DEBUG
QMAKE_LFLAGS_CONSOLE = /SUBSYSTEM:console
QMAKE_LFLAGS_WINDOWS = /SUBSYSTEM:windows
QMAKE_LFLAGS_DLL = /DLL
QMAKE_LFLAGS_LTCG = $$QMAKE_CFLAGS_LTCG
QMAKE_EXTENSION_STATICLIB = lib
QMAKE_LIBS =