From 51825fc5f8bdc3da639aa77c98157710657ec37f Mon Sep 17 00:00:00 2001 From: AUA Date: Thu, 7 Nov 2024 10:28:49 +0800 Subject: [PATCH] commit-message: update the cmake #432 (#474) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### What problem were solved in this pull request? Issue Number: close #432 Problem:增加识别 CMAKE_BUILD_TYPE 选项,删除 DEBUG=ON Cmake选项 ### What is changed and how it works? 我更改了CMakeLists.txt文件,删除了DEBUG定义,新增CMAKE_BUILD_TYPE 选项的判断 更改了build.sh文件,修改了识别debug那部分命令 ### Other information 希望验证一下相关功能是否实现,因为不太确定是否只需要修改这两个文件以及这么修改是否正确 如果有问题,期待下一步沟通 --------- Co-authored-by: wangyunlai --- CMakeLists.txt | 29 ++++++++++++++++------------- build.sh | 27 ++++++++++++--------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 31b5917f6..4e7dfbb7e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,15 @@ SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake) +# 设置默认构建类型为 Debug +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Debug CACHE STRING "Build type:Default is debug" FORCE) +endif() +string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER) + +# 输出当前构建类型 +message(STATUS "Using build type: ${CMAKE_BUILD_TYPE}") + OPTION(ENABLE_ASAN "Enable build with address sanitizer" ON) OPTION(ENABLE_TSAN "Build with thread sanitizer" OFF) OPTION(ENABLE_UBSAN "Build with undefined behavior sanitizer" OFF) @@ -46,6 +55,12 @@ ENDIF(WIN32) # This is for clangd plugin for vscode # mute sign-compare error in lex/yacc SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -Wall -Werror -Wno-error=sign-compare") +IF ("${CMAKE_BUILD_TYPE_LOWER}" STREQUAL "debug") + SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -DDEBUG -g -O0") +ELSEIF ("${CMAKE_BUILD_TYPE_LOWER}" STREQUAL "release") + SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -g -O2") +ENDIF() + IF (ENABLE_NOPIE) SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -no-pie") ADD_LINK_OPTIONS(-no-pie) @@ -57,19 +72,6 @@ IF(USE_SIMD) ADD_DEFINITIONS(-DUSE_SIMD) ENDIF(USE_SIMD) -IF(DEBUG) - MESSAGE(STATUS "DEBUG has been set as TRUE ${DEBUG}") - SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -O0 -g -DDEBUG ") - ADD_DEFINITIONS(-DENABLE_DEBUG) -ELSEIF(NOT DEFINED ENV{DEBUG}) - MESSAGE(STATUS "Disable debug") - SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -O2 -g ") -ELSE() - MESSAGE(STATUS "Enable debug") - SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -O0 -g -DDEBUG") - ADD_DEFINITIONS(-DENABLE_DEBUG) -ENDIF(DEBUG) - IF (CONCURRENCY) MESSAGE(STATUS "CONCURRENCY is ON") SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -DCONCURRENCY") @@ -164,6 +166,7 @@ ENDIF(WITH_UNIT_TESTS) SET(CMAKE_CXX_FLAGS ${CMAKE_COMMON_FLAGS}) SET(CMAKE_C_FLAGS ${CMAKE_COMMON_FLAGS}) + MESSAGE(STATUS "CMAKE_CXX_FLAGS is " ${CMAKE_CXX_FLAGS}) # ADD_SUBDIRECTORY(src bin) bin 为目标目录, 可以省略 diff --git a/build.sh b/build.sh index 0a914d671..13b899418 100755 --- a/build.sh +++ b/build.sh @@ -147,23 +147,20 @@ function do_clean find . -maxdepth 1 -type d -name 'build_*' | xargs rm -rf } -function build -{ - set -- "${BUILD_ARGS[@]}" - case "x$1" in - xrelease) - do_build "$@" -DCMAKE_BUILD_TYPE=RelWithDebInfo -DDEBUG=OFF - ;; - xdebug) - do_build "$@" -DCMAKE_BUILD_TYPE=Debug -DDEBUG=ON - ;; - *) - BUILD_ARGS=(debug "${BUILD_ARGS[@]}") - build - ;; - esac +function build { + # 默认参数是 debug + if [ -z "${BUILD_ARGS[0]}" ]; then + set -- "debug" # 如果没有参数,则设置默认值 + else + set -- "${BUILD_ARGS[@]}" # 否则使用 BUILD_ARGS 的第一个参数 + fi + local build_type_lower=$(echo "$1" | tr '[:upper:]' '[:lower:]') # 转换为小写 + echo "Build type: $build_type_lower" # 输出构建类型 + + do_build "$build_type_lower" -DCMAKE_BUILD_TYPE="$build_type_lower" # 调用 do_build } + function main { case "$1" in