From 10804428577fa5d3a35a5b57b2c4233ae4484543 Mon Sep 17 00:00:00 2001 From: Shyamnath Premnadh Date: Wed, 23 Mar 2022 09:33:22 +0100 Subject: PySide6: Optimize for Size build: use the following flag with setup.py to turn off size optimization --no-size-optimization Added the following compiler optimization flags and their corresponding flags on other platforms GCC - -ffunction-sections -fdata-section which segretates data and function section and linker flag --gc-section which removes unused code. - -fno-exceptions to disable exception handling - -Os - Optimize for size. Basically same as -O2 but removes some flags that cause increase in size. (Ran a couple of example and did not see difference in execution time) MSVC - /Gy /Gw /OPT:REF - same as -ffunction-sections, -fdata-section, -Wl, --gc-section - /EHsc same as -fno-exceptions - /O1 instead of /Os because for MSVC /O1 gave the best results. Clang - Same as GCC except for using -Oz instead of -Os. Experiments: Built a wheel with QtCore and noticed a 300kb reduction in size on both Windows and Linux. Built a complete wheel(except QTest) and it gives me a 4 mb size reduction with unaffected performance. Task-number: PYSIDE-1860 Change-Id: Ia5dfa2c4bfde92994c939b5fac0d0831fa3a73ab Reviewed-by: Friedemann Kleint Reviewed-by: Christian Tismer (cherry picked from commit a88f7b21c61e4aa9efcfb81d7939e44b06df3a0d) --- build_scripts/main.py | 3 +++ build_scripts/options.py | 3 +++ sources/pyside6/cmake/Macros/PySideModules.cmake | 16 ++++++++++++++++ sources/pyside6/cmake/PySideHelpers.cmake | 13 +++++++++++++ sources/pyside6/cmake/PySideSetup.cmake | 3 +++ sources/pyside6/libpyside/CMakeLists.txt | 3 +++ 6 files changed, 41 insertions(+) diff --git a/build_scripts/main.py b/build_scripts/main.py index d44fa2f76..3f8ebd4d7 100644 --- a/build_scripts/main.py +++ b/build_scripts/main.py @@ -667,6 +667,9 @@ class PysideBuild(_build, DistUtilsCommandMixin, BuildInfoCollectorMixin): else: if OPTION['NO_STRIP']: cmake_cmd.append("-DQFP_NO_STRIP=1") + if OPTION['NO_OVERRIDE_OPTIMIZATION_FLAGS']: + cmake_cmd.append("-DQFP_NO_OVERRIDE_OPTIMIZATION_FLAGS=1") + if OPTION["LIMITED_API"] == "yes": cmake_cmd.append("-DFORCE_LIMITED_API=yes") elif OPTION["LIMITED_API"] == "no": diff --git a/build_scripts/options.py b/build_scripts/options.py index 0cb80e6ce..456f342a3 100644 --- a/build_scripts/options.py +++ b/build_scripts/options.py @@ -249,6 +249,7 @@ class DistUtilsCommandMixin(object): ('qt-conf-prefix=', None, 'Qt configuration prefix'), ('qt-src-dir=', None, 'Qt source directory'), ('no-qt-tools', None, 'Do not copy the Qt tools'), + ('no-size-optimization', None, 'Turn off size optimization for PySide6 binaries'), ('pyside-numpy-support', None, 'libpyside: Add (experimental) numpy support'), ('internal-cmake-install-dir-query-file-path=', None, 'Path to file where the CMake install path of the project will be saved'), @@ -307,6 +308,7 @@ class DistUtilsCommandMixin(object): self.qt_conf_prefix = None self.qt_src_dir = None self.no_qt_tools = False + self.no_size_optimization = False self.pyside_numpy_support = False self.plat_name = None self.internal_cmake_install_dir_query_file_path = None @@ -468,6 +470,7 @@ class DistUtilsCommandMixin(object): OPTION['QT_CONF_PREFIX'] = self.qt_conf_prefix OPTION['QT_SRC'] = self.qt_src_dir OPTION['NO_QT_TOOLS'] = self.no_qt_tools + OPTION['NO_OVERRIDE_OPTIMIZATION_FLAGS'] = self.no_size_optimization OPTION['PYSIDE_NUMPY_SUPPORT'] = self.pyside_numpy_support if not self._extra_checks(): diff --git a/sources/pyside6/cmake/Macros/PySideModules.cmake b/sources/pyside6/cmake/Macros/PySideModules.cmake index c5bee643c..d722b4650 100644 --- a/sources/pyside6/cmake/Macros/PySideModules.cmake +++ b/sources/pyside6/cmake/Macros/PySideModules.cmake @@ -18,6 +18,19 @@ macro(unmake_path varname) string(REPLACE "${PATH_SEP}" ";" ${varname} "${ARGN}") endmacro() +# set size optimization flags for pyside6 +macro(append_size_optimization_flags _module_name) + if(NOT QFP_NO_OVERRIDE_OPTIMIZATION_FLAGS) + if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + target_compile_options(${_module_name} PRIVATE /Gy /Gw /EHsc) + target_link_options(${_module_name} PRIVATE LINKER:/OPT:REF) + elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU|CLANG") + target_compile_options(${_module_name} PRIVATE -ffunction-sections -fdata-sections -fno-exceptions) + target_link_options(${_module_name} PRIVATE LINKER:--gc-sections) + endif() + endif() +endmacro() + # Sample usage # create_pyside_module(NAME QtGui # INCLUDE_DIRS QtGui_include_dirs @@ -146,6 +159,9 @@ macro(create_pyside_module) include_directories(${module_NAME} ${${module_INCLUDE_DIRS}} ${pyside6_SOURCE_DIR}) add_library(${module_NAME} MODULE ${${module_SOURCES}} ${${module_STATIC_SOURCES}}) + + append_size_optimization_flags(${module_NAME}) + set_target_properties(${module_NAME} PROPERTIES PREFIX "" OUTPUT_NAME "${module_NAME}${SHIBOKEN_PYTHON_EXTENSION_SUFFIX}" diff --git a/sources/pyside6/cmake/PySideHelpers.cmake b/sources/pyside6/cmake/PySideHelpers.cmake index 86cda83f8..0de8e1bdc 100644 --- a/sources/pyside6/cmake/PySideHelpers.cmake +++ b/sources/pyside6/cmake/PySideHelpers.cmake @@ -252,3 +252,16 @@ macro(collect_module_if_found shortname) endif() endif() endmacro() + +# resets the RELEASE CXX flags for size based optimization +macro(override_release_flags_for_size_optimization) + if(NOT QFP_NO_OVERRIDE_OPTIMIZATION_FLAGS) + if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + set(CMAKE_CXX_FLAGS_RELEASE "/O1 /DNDEBUG") + elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + set(CMAKE_CXX_FLAGS_RELEASE "-Os -DNDEBUG") + elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + set(CMAKE_CXX_FLAGS_RELEASE "-Oz -DNDEBUG") + endif() + endif() +endmacro() diff --git a/sources/pyside6/cmake/PySideSetup.cmake b/sources/pyside6/cmake/PySideSetup.cmake index 08072ac02..a167f4ddd 100644 --- a/sources/pyside6/cmake/PySideSetup.cmake +++ b/sources/pyside6/cmake/PySideSetup.cmake @@ -12,6 +12,9 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/Macros") include(ShibokenHelpers) include(PySideHelpers) +#does nothing if QFP_NO_OVERRIDE_OPTIMIZATION_FLAGS (no-size-optimization) flag is not set +override_release_flags_for_size_optimization() + # Don't display "up-to-date / install" messages when installing, to reduce visual clutter. if(QUIET_BUILD) set(CMAKE_INSTALL_MESSAGE NEVER) diff --git a/sources/pyside6/libpyside/CMakeLists.txt b/sources/pyside6/libpyside/CMakeLists.txt index cd39483a3..0b2f7a097 100644 --- a/sources/pyside6/libpyside/CMakeLists.txt +++ b/sources/pyside6/libpyside/CMakeLists.txt @@ -50,6 +50,9 @@ add_other_files(${other_files}) add_library(pyside6 SHARED ${libpyside_SRC} ${other_files}) add_library(PySide6::pyside6 ALIAS pyside6) +#does nothing if QFP_NO_OVERRIDE_OPTIMIZATION_FLAGS (no-size-optimization) flag is not set +append_size_optimization_flags(pyside6) + target_include_directories(pyside6 PUBLIC $ $ -- cgit v1.2.3