From b50e9fb31b3f72a5d2a89bbddb79bdcf42811c6f Mon Sep 17 00:00:00 2001 From: Andy Green Date: Thu, 28 Feb 2019 07:05:12 +0800 Subject: [PATCH] cmake: override build system release optimization policy The cmake config on the build system actually decides the release build optimization policy. On Fedora, it's -O2. On Ubuntu, it's -O3. Anything given in CMakeLists.txt is overridden by the build system policy since it goes at the end of the compiler commandline. When you are building cross, the build system's opinion of your cross binary optimization level is irrelevant, and at worst destructive. Some versions of gcc contain broken optimizations that are applied only at -O3. This patch removes any doomed attempt to set -O in CMakeLists.txt, which has no effect since the build system policy is still added at the end, but removes confusion; and adds code to all the cross build files to forcibly override release optimization level to -O2, removing the build system's opinion of how your cross build should look. --- CMakeLists.txt | 7 +------ contrib/cross-aarch64.cmake | 16 ++++++++++++++++ contrib/cross-arm-android-gnueabi.cmake | 16 ++++++++++++++++ contrib/cross-arm-linux-gnueabihf.cmake | 16 ++++++++++++++++ contrib/cross-esp32.cmake | 16 ++++++++++++++++ contrib/cross-ming.cmake | 16 ++++++++++++++++ contrib/cross-w32.cmake | 16 ++++++++++++++++ contrib/cross-w64.cmake | 16 ++++++++++++++++ 8 files changed, 113 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d1b50722..73002a842 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1330,18 +1330,13 @@ if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR (CMAKE_C_COMPILER_ID set(VISIBILITY_FLAG -fvisibility=hidden) endif() if (LWS_WITH_GCOV) - set (GCOV_FLAGS "-fprofile-arcs -ftest-coverage -O0") + set (GCOV_FLAGS "-fprofile-arcs -ftest-coverage ") endif() if (LWS_WITH_ASAN) set (ASAN_FLAGS "-fsanitize=address -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=leak -fsanitize=undefined -fsanitize-address-use-after-scope -fsanitize-undefined-trap-on-error") message("Enabling ASAN") endif() - if (CMAKE_BUILD_TYPE MATCHES "DEBUG") - set(CMAKE_C_FLAGS "-O0 ${CMAKE_C_FLAGS}") - else() - set(CMAKE_C_FLAGS "-O3 ${CMAKE_C_FLAGS}") - endif() if (UNIX AND NOT LWS_WITH_ESP32) set(CMAKE_C_FLAGS "-Wall -Wsign-compare -Wignored-qualifiers -Wtype-limits -Wuninitialized -Werror ${VISIBILITY_FLAG} -Wundef ${GCOV_FLAGS} ${CMAKE_C_FLAGS} ${ASAN_FLAGS}" ) diff --git a/contrib/cross-aarch64.cmake b/contrib/cross-aarch64.cmake index 518150191..8f36fc389 100644 --- a/contrib/cross-aarch64.cmake +++ b/contrib/cross-aarch64.cmake @@ -14,6 +14,22 @@ set(CMAKE_SYSTEM_PROCESSOR aarch64) set(CMAKE_C_COMPILER "aarch64-linux-gnu-gcc") set(CMAKE_CXX_COMPILER "aarch64-linux-gnu-g++") +# +# Different build system distros set release optimization level to different +# things according to their local policy, eg, Fedora is -O2 and Ubuntu is -O3 +# here. Actually the build system's local policy is completely unrelated to +# our desire for cross-build release optimization policy for code built to run +# on a completely different target than the build system itself. +# +# Since this goes last on the compiler commandline we have to override it to a +# sane value for cross-build here. Notice some gcc versions enable broken +# optimizations with -O3. +# +if (CMAKE_BUILD_TYPE MATCHES RELEASE OR CMAKE_BUILD_TYPE MATCHES Release OR CMAKE_BUILD_TYPE MATCHES release) + set(CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE}" -O2") + set(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}" -O2") +endif() + #-nostdlib SET(CMAKE_C_FLAGS "-DGCC_VER=\"\\\"$(GCC_VER)\\\"\" -DARM64=1 -D__LP64__=1 -Os -g3 -fpie -mstrict-align -DOPTEE_DEV_KIT=../../../../out/arm-plat-hikey/export-ta_arm64/include -I../../../../lib/libutee/include -fPIC -ffunction-sections -fdata-sections -I../../../../core/include" CACHE STRING "" FORCE) diff --git a/contrib/cross-arm-android-gnueabi.cmake b/contrib/cross-arm-android-gnueabi.cmake index e99e6f65d..f0a18a54a 100644 --- a/contrib/cross-arm-android-gnueabi.cmake +++ b/contrib/cross-arm-android-gnueabi.cmake @@ -18,6 +18,22 @@ set(BUILD_SHARED_LIBS ON) set(CMAKE_C_COMPILER "${CROSS_PATH}/bin/arm-linux-androideabi-gcc") set(CMAKE_CXX_COMPILER "${CROSS_PATH}/bin/arm-linux-androideabi-g++") +# +# Different build system distros set release optimization level to different +# things according to their local policy, eg, Fedora is -O2 and Ubuntu is -O3 +# here. Actually the build system's local policy is completely unrelated to +# our desire for cross-build release optimization policy for code built to run +# on a completely different target than the build system itself. +# +# Since this goes last on the compiler commandline we have to override it to a +# sane value for cross-build here. Notice some gcc versions enable broken +# optimizations with -O3. +# +if (CMAKE_BUILD_TYPE MATCHES RELEASE OR CMAKE_BUILD_TYPE MATCHES Release OR CMAKE_BUILD_TYPE MATCHES release) + set(CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE}" -O2") + set(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}" -O2") +endif() + # Where to look for the target environment. (More paths can be added here) set(CMAKE_FIND_ROOT_PATH "${CROSS_PATH}") diff --git a/contrib/cross-arm-linux-gnueabihf.cmake b/contrib/cross-arm-linux-gnueabihf.cmake index 12cf3e98d..c7ceae9d5 100644 --- a/contrib/cross-arm-linux-gnueabihf.cmake +++ b/contrib/cross-arm-linux-gnueabihf.cmake @@ -15,6 +15,22 @@ set(CMAKE_SYSTEM_NAME Linux) set(CMAKE_C_COMPILER "${CROSS_PATH}/bin/arm-linux-gnueabihf-gcc") set(CMAKE_CXX_COMPILER "${CROSS_PATH}/bin/arm-linux-gnueabihf-g++") +# +# Different build system distros set release optimization level to different +# things according to their local policy, eg, Fedora is -O2 and Ubuntu is -O3 +# here. Actually the build system's local policy is completely unrelated to +# our desire for cross-build release optimization policy for code built to run +# on a completely different target than the build system itself. +# +# Since this goes last on the compiler commandline we have to override it to a +# sane value for cross-build here. Notice some gcc versions enable broken +# optimizations with -O3. +# +if (CMAKE_BUILD_TYPE MATCHES RELEASE OR CMAKE_BUILD_TYPE MATCHES Release OR CMAKE_BUILD_TYPE MATCHES release) + set(CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE}" -O2") + set(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}" -O2") +endif() + # Where to look for the target environment. (More paths can be added here) set(CMAKE_FIND_ROOT_PATH "${CROSS_PATH}") diff --git a/contrib/cross-esp32.cmake b/contrib/cross-esp32.cmake index 406763c61..b99c37d65 100644 --- a/contrib/cross-esp32.cmake +++ b/contrib/cross-esp32.cmake @@ -15,6 +15,22 @@ set(CMAKE_AR "${CROSS_PATH}/bin/xtensa-esp32-elf-ar${EXECUTABLE_EXT}") set(CMAKE_RANLIB "${CROSS_PATH}/bin/xtensa-esp32-elf-ranlib${EXECUTABLE_EXT}") set(CMAKE_LINKER "${CROSS_PATH}/bin/xtensa-esp32-elf-ld${EXECUTABLE_EXT}") +# +# Different build system distros set release optimization level to different +# things according to their local policy, eg, Fedora is -O2 and Ubuntu is -O3 +# here. Actually the build system's local policy is completely unrelated to +# our desire for cross-build release optimization policy for code built to run +# on a completely different target than the build system itself. +# +# Since this goes last on the compiler commandline we have to override it to a +# sane value for cross-build here. Notice some gcc versions enable broken +# optimizations with -O3. +# +if (CMAKE_BUILD_TYPE MATCHES RELEASE OR CMAKE_BUILD_TYPE MATCHES Release OR CMAKE_BUILD_TYPE MATCHES release) + set(CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE}" -O2") + set(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}" -O2") +endif() + SET(CMAKE_C_FLAGS "-nostdlib -Wall -Werror \ -I${BUILD_DIR_BASE}/include \ -I${IDF_PATH}/components/newlib/platform_include \ diff --git a/contrib/cross-ming.cmake b/contrib/cross-ming.cmake index 94989f230..5d737dcfb 100644 --- a/contrib/cross-ming.cmake +++ b/contrib/cross-ming.cmake @@ -18,6 +18,22 @@ set(CMAKE_C_COMPILER "${CROSS_PATH}/x86_64-w64-mingw32-gcc") set(CMAKE_RC_COMPILER "${CROSS_PATH}/x86_64-w64-mingw32-windres") set(CMAKE_C_FLAGS "-Wno-error") +# +# Different build system distros set release optimization level to different +# things according to their local policy, eg, Fedora is -O2 and Ubuntu is -O3 +# here. Actually the build system's local policy is completely unrelated to +# our desire for cross-build release optimization policy for code built to run +# on a completely different target than the build system itself. +# +# Since this goes last on the compiler commandline we have to override it to a +# sane value for cross-build here. Notice some gcc versions enable broken +# optimizations with -O3. +# +if (CMAKE_BUILD_TYPE MATCHES RELEASE OR CMAKE_BUILD_TYPE MATCHES Release OR CMAKE_BUILD_TYPE MATCHES release) + set(CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE}" -O2") + set(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}" -O2") +endif() + # Where to look for the target environment. (More paths can be added here) set(CMAKE_FIND_ROOT_PATH "${CROSS_PATH}") diff --git a/contrib/cross-w32.cmake b/contrib/cross-w32.cmake index 14cc234c3..0cb96cb3f 100644 --- a/contrib/cross-w32.cmake +++ b/contrib/cross-w32.cmake @@ -17,6 +17,22 @@ set(CMAKE_CXX_COMPILER "${CROSS_PATH}/bin/i686-w64-mingw32-g++") set(CMAKE_RC_COMPILER "${CROSS_PATH}/bin/i686-w64-mingw32-windres") set(CMAKE_C_FLAGS "-Wno-error") +# +# Different build system distros set release optimization level to different +# things according to their local policy, eg, Fedora is -O2 and Ubuntu is -O3 +# here. Actually the build system's local policy is completely unrelated to +# our desire for cross-build release optimization policy for code built to run +# on a completely different target than the build system itself. +# +# Since this goes last on the compiler commandline we have to override it to a +# sane value for cross-build here. Notice some gcc versions enable broken +# optimizations with -O3. +# +if (CMAKE_BUILD_TYPE MATCHES RELEASE OR CMAKE_BUILD_TYPE MATCHES Release OR CMAKE_BUILD_TYPE MATCHES release) + set(CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE}" -O2") + set(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}" -O2") +endif() + # Where to look for the target environment. (More paths can be added here) set(CMAKE_FIND_ROOT_PATH "${CROSS_PATH}") diff --git a/contrib/cross-w64.cmake b/contrib/cross-w64.cmake index 2fc5de30d..9d4a45ab6 100644 --- a/contrib/cross-w64.cmake +++ b/contrib/cross-w64.cmake @@ -17,6 +17,22 @@ set(CMAKE_CXX_COMPILER "${CROSS_PATH}/bin/x86_64-w64-mingw32-g++") set(CMAKE_RC_COMPILER "${CROSS_PATH}/bin/x86_64-w64-mingw32-windres") set(CMAKE_C_FLAGS "-Wno-error") +# +# Different build system distros set release optimization level to different +# things according to their local policy, eg, Fedora is -O2 and Ubuntu is -O3 +# here. Actually the build system's local policy is completely unrelated to +# our desire for cross-build release optimization policy for code built to run +# on a completely different target than the build system itself. +# +# Since this goes last on the compiler commandline we have to override it to a +# sane value for cross-build here. Notice some gcc versions enable broken +# optimizations with -O3. +# +if (CMAKE_BUILD_TYPE MATCHES RELEASE OR CMAKE_BUILD_TYPE MATCHES Release OR CMAKE_BUILD_TYPE MATCHES release) + set(CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE}" -O2") + set(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}" -O2") +endif() + # Where to look for the target environment. (More paths can be added here) set(CMAKE_FIND_ROOT_PATH "${CROSS_PATH}")