Skip to content

Commit

Permalink
refactor(cxx): improve project structure and simplify cmake scripts
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
pplmx committed Nov 25, 2024
1 parent 2cbcded commit deb98b3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 55 deletions.
58 changes: 21 additions & 37 deletions template/cxx/{{cookiecutter.project_slug}}/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,72 +1,56 @@
# 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}}"
HOMEPAGE_URL "https://github.com/{{cookiecutter.__gh_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
$<BUILD_INTERFACE:${INCLUDE_DIR}>
)

# ===================== 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)
23 changes: 5 additions & 18 deletions template/cxx/{{cookiecutter.project_slug}}/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# ===================== Google Test Setup =====================

# Enable testing
enable_testing()

Expand All @@ -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)

0 comments on commit deb98b3

Please sign in to comment.