From deb98b32ba010d8c30780a8e9a4583b7e96a828d Mon Sep 17 00:00:00 2001 From: Mystic <215104920@qq.com> Date: Mon, 25 Nov 2024 18:22:08 +0800 Subject: [PATCH] refactor(cxx): improve project structure and simplify cmake scripts - Remove unnecessary comments and sections - Use variables for project name and directories - Simplify test setup and discovery - Update build settings for better portability - Improve code organization and readability --- .../CMakeLists.txt | 58 +++++++------------ .../tests/CMakeLists.txt | 23 ++------ 2 files changed, 26 insertions(+), 55 deletions(-) diff --git a/template/cxx/{{cookiecutter.project_slug}}/CMakeLists.txt b/template/cxx/{{cookiecutter.project_slug}}/CMakeLists.txt index 83c87bc..a8bab65 100644 --- a/template/cxx/{{cookiecutter.project_slug}}/CMakeLists.txt +++ b/template/cxx/{{cookiecutter.project_slug}}/CMakeLists.txt @@ -1,7 +1,5 @@ -# Specify the minimum version of CMake required cmake_minimum_required(VERSION 3.25) -# Define the project project({{cookiecutter.project_slug}} VERSION {{cookiecutter.project_version}} DESCRIPTION "{{cookiecutter.project_desc}}" @@ -9,64 +7,50 @@ project({{cookiecutter.project_slug}} LANGUAGES CXX ) -# ===================== Build Settings ===================== - -# Add custom cmake modules path +# Custom modules list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) -# Set C++ standards +# Build settings set(CMAKE_CXX_STANDARD {{cookiecutter.cxx_standard_version}}) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) - -# Set compile options -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Enable compile commands for clangd +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Set architecture for macOS (if applicable) if(APPLE) set(CMAKE_OSX_ARCHITECTURES "arm64") endif() -# ===================== Output Directories ===================== +# Build type configuration +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release) +endif() -# Use generator expressions for output directories +# Set output directories set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "$<1:${CMAKE_BINARY_DIR}/bin>") set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "$<1:${CMAKE_BINARY_DIR}/lib>") set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "$<1:${CMAKE_BINARY_DIR}/lib>") -# ===================== Source Files ===================== - -# Collect source files +# Source files configuration +set(SRC_DIR ${CMAKE_SOURCE_DIR}/src) +set(INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include) file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS - ${CMAKE_SOURCE_DIR}/src/*.cpp + ${SRC_DIR}/*.cpp ) file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS - ${CMAKE_SOURCE_DIR}/include/*.h + ${INCLUDE_DIR}/*.h ) -# ===================== Library Target ===================== - -# Create library target -add_library({{cookiecutter.package_name}}_lib ${SOURCES} ${HEADERS}) - -# Set include directories for the library -target_include_directories({{cookiecutter.package_name}}_lib +# Library target +add_library(${PROJECT_NAME}_lib ${SOURCES} ${HEADERS}) +target_include_directories(${PROJECT_NAME}_lib PUBLIC - ${CMAKE_SOURCE_DIR}/include -) - -# ===================== Executable Target ===================== - -# Create executable target -add_executable({{cookiecutter.project_slug}} ${CMAKE_SOURCE_DIR}/src/main.cpp) - -# Link library and CUDA libraries to the executable -target_link_libraries({{cookiecutter.project_slug}} - PRIVATE - {{cookiecutter.package_name}}_lib + $ ) -# ===================== Tests ===================== +# Executable target +add_executable(${PROJECT_NAME} ${SRC_DIR}/main.cpp) +target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_NAME}_lib) -# Add tests subdirectory +# Tests add_subdirectory(tests) diff --git a/template/cxx/{{cookiecutter.project_slug}}/tests/CMakeLists.txt b/template/cxx/{{cookiecutter.project_slug}}/tests/CMakeLists.txt index ebb0f1f..c3b778f 100644 --- a/template/cxx/{{cookiecutter.project_slug}}/tests/CMakeLists.txt +++ b/template/cxx/{{cookiecutter.project_slug}}/tests/CMakeLists.txt @@ -1,5 +1,3 @@ -# ===================== Google Test Setup ===================== - # Enable testing enable_testing() @@ -9,30 +7,19 @@ FetchContent_Declare( googletest URL https://github.com/google/googletest/archive/refs/tags/v1.15.2.tar.gz ) - -# For Windows: Prevent overriding the parent project's compiler/linker settings set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) FetchContent_MakeAvailable(googletest) -# ===================== Test Sources ===================== - # Collect test source files file(GLOB_RECURSE TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) -# ===================== Test Executable ===================== - # Create test executable -add_executable({{cookiecutter.project_slug}}-tests ${TEST_SOURCES}) - -# Link the test executable with Google Test -target_link_libraries({{cookiecutter.project_slug}}-tests - PRIVATE - {{cookiecutter.package_name}}_lib +add_executable(${PROJECT_NAME}-tests ${TEST_SOURCES}) +target_link_libraries(${PROJECT_NAME}-tests PRIVATE GTest::gtest_main + ${PROJECT_NAME}_lib ) -# ===================== Test Discovery ===================== - -# Discover tests +# Enable test discovery include(GoogleTest) -gtest_discover_tests({{cookiecutter.project_slug}}-tests) +gtest_discover_tests(${PROJECT_NAME}-tests)