Make the use of -fdata-sections and --gc-sections more generic in Qt

Add qmake feature and configure option, which optimze the size of static
exectuable. Use for static build.
Enabled via configure --gc-binaries, or CONFIG += gc-binaries in 3rd party
projects.

Change-Id: I3c25b02caaef6a4afc6019afc9c67122dd11696d
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@qt.io>
This commit is contained in:
Mikhail Svetkin 2018-04-10 15:03:11 +02:00
parent 18d2619224
commit 194a404490
11 changed files with 30 additions and 9 deletions

View File

@ -98,6 +98,9 @@ Build options:
-gdb-index ........... Index the debug info to speed up GDB
[no; auto if -developer-build with debug info]
-strip ............... Strip release binaries of unneeded symbols [yes]
-gc-binaries ......... Place each function or data item into its own section
and enable linker garbage collection of unused
sections. [auto for static builds, otherwise no]
-force-asserts ....... Enable Q_ASSERT even in release builds [no]
-developer-build ..... Compile and link Qt for developing Qt itself
(exports for auto-tests, extra checks, etc.) [no]

View File

@ -80,6 +80,7 @@
"force-debug-info": { "type": "boolean", "name": "force_debug_info" },
"force-pkg-config": { "type": "void", "name": "pkg-config" },
"framework": "boolean",
"gc-binaries": { "type": "boolean", "name": "gc_binaries" },
"gdb-index": { "type": "boolean", "name": "gdb_index" },
"gcc-sysroot": "boolean",
"gcov": "boolean",
@ -397,6 +398,17 @@
]
}
},
"gc_binaries": {
"label": "support for split sections and linker garbage collection",
"type": "compile",
"test": {
"qmake": [
"isEmpty(QMAKE_CFLAGS_SPLIT_SECTIONS): error(\"Nope\")",
"isEmpty(QMAKE_CXXFLAGS_SPLIT_SECTIONS): error(\"Nope\")",
"isEmpty(QMAKE_LFLAGS_GCSECTIONS): error(\"Nope\")"
]
}
},
"sse2": {
"label": "SSE2 instructions",
"type": "x86Simd"
@ -592,6 +604,10 @@
"compiler-flags": {
"output": [ "compilerFlags" ]
},
"gc_binaries": {
"condition": "!features.shared && tests.gc_binaries",
"output": [ "privateFeature" ]
},
"gcc-sysroot": {
"output": [ "gccSysroot" ],
"condition": "input.sysroot != ''"

View File

@ -50,7 +50,7 @@ QMAKE_CFLAGS_ISYSTEM = -isystem
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_SPLIT_SECTIONS += -ffunction-sections -fdata-sections
QMAKE_CFLAGS_LTCG = -flto -fno-fat-lto-objects
QMAKE_CFLAGS_LTCG_FATOBJECTS = -ffat-lto-objects
QMAKE_CFLAGS_DISABLE_LTCG = -fno-lto

View File

@ -0,0 +1,3 @@
QMAKE_CFLAGS += $$QMAKE_CFLAGS_SPLIT_SECTIONS
QMAKE_CXXFLAGS += $$QMAKE_CXXFLAGS_SPLIT_SECTIONS
QMAKE_LFLAGS += $$QMAKE_LFLAGS_GCSECTIONS

View File

@ -20,9 +20,6 @@ isEmpty(QMAKE_TARGET_DESCRIPTION): \
isEmpty(QMAKE_INFO_PLIST): CONFIG -= app_bundle
# This decreases the binary size for tools if statically linked
QMAKE_LFLAGS += $$QMAKE_LFLAGS_GCSECTIONS
host_build: QT -= gui # no host tool will ever use gui
host_build:force_bootstrap {
!build_pass:qtConfig(release_tools): CONFIG += release

View File

@ -28,8 +28,6 @@ host_build|staticlib: CONFIG += static
host_build {
QT -= gui # no host module will ever use gui
QMAKE_CFLAGS += $$QMAKE_CFLAGS_SPLIT_SECTIONS
QMAKE_CXXFLAGS += $$QMAKE_CXXFLAGS_SPLIT_SECTIONS
force_bootstrap {
!build_pass:qtConfig(release_tools): CONFIG += release
contains(QT, core(-private)?|xml) {

View File

@ -0,0 +1,2 @@
# bitcode (release mode) is incompatible with splitting sections.
!bitcode|!release: load(gc_binaries)

View File

@ -26,7 +26,7 @@ QMAKE_CFLAGS_STATIC_LIB = $$QMAKE_CFLAGS_SHLIB
QMAKE_CFLAGS_YACC =
QMAKE_CFLAGS_ISYSTEM = -isystem
QMAKE_CFLAGS_THREAD = -D_REENTRANT
QMAKE_CFLAGS_SPLIT_SECTIONS = -ffunction-sections
QMAKE_CFLAGS_SPLIT_SECTIONS = -ffunction-sections -fdata-sections
QMAKE_CFLAGS_LTCG = -ipo -fno-fat-lto-objects
QMAKE_CFLAGS_LTCG_FATOBJECTS = -ffat-lto-objects
QMAKE_CFLAGS_DISABLE_LTCG = -no-ipo

View File

@ -25,7 +25,7 @@ QMAKE_CFLAGS_SHLIB = -fPIC
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_SPLIT_SECTIONS = -ffunction-sections -fdata-sections
QMAKE_CFLAGS_LTCG = -ipo
QMAKE_CFLAGS_DISABLE_LTCG = -no-ipo

View File

@ -4,6 +4,7 @@ CONFIG += exceptions
MODULE = core # not corelib, as per project file
MODULE_CONFIG = moc resources
qtConfig(gc_binaries): MODULE_CONFIG += gc_binaries
!isEmpty(QT_NAMESPACE): MODULE_DEFINES = QT_NAMESPACE=$$QT_NAMESPACE
TRACEPOINT_PROVIDER = $$PWD/qtcore.tracepoints

View File

@ -2,7 +2,7 @@ option(host_build)
TARGET = QtBootstrap
QT =
CONFIG += minimal_syncqt internal_module force_bootstrap
CONFIG += minimal_syncqt internal_module force_bootstrap gc_binaries
MODULE_INCNAME = QtCore QtXml
MODULE_DEFINES = \
@ -12,6 +12,7 @@ MODULE_DEFINES = \
QT_VERSION_PATCH=$$QT_PATCH_VERSION \
QT_BOOTSTRAPPED \
QT_NO_CAST_TO_ASCII
MODULE_CONFIG = gc_binaries
DEFINES += \
$$MODULE_DEFINES \