From 1a69a73fb6eb6c54dc0df89cf4d90b9948cff2f9 Mon Sep 17 00:00:00 2001 From: Thorsten Otto Date: Fri, 22 Mar 2024 09:55:59 +0100 Subject: [PATCH] Add check whether compiler supports -fdata-sections/-ffunction-sections --- CMakeLists.txt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ce8dad3..be15b5f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.10) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/") include(FeatureSummary) +include(CheckCXXCompilerFlag) if(NOT DEFINED CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo" FORCE) @@ -82,6 +83,24 @@ if(NOT MSVC) set(CMAKE_FIND_FRAMEWORK "LAST") endif() + message(STATUS "Checking whether compiler supports -fdata-sections") + check_cxx_compiler_flag("-Werror -fdata-sections" HAVE_DATA_SECTIONS) + if(HAVE_DATA_SECTIONS) + message(STATUS "yes") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdata-sections") + else() + message(STATUS "no") + endif() + + message(STATUS "Checking whether compiler supports -ffunction-sections") + check_cxx_compiler_flag("-Werror -ffunction-sections" HAVE_FUNCTION_SECTIONS) + if(HAVE_FUNCTION_SECTIONS) + message(STATUS "yes") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffunction-sections") + else() + message(STATUS "no") + endif() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w -Wwrite-strings -Werror=write-strings -fcheck-new -fsigned-char -fdata-sections -ffunction-sections -DNOMINMAX") else() set(CMAKE_CXX_FLAGS "/Zc:strictStrings")