From 0c8d62de286938ec61579983d77ff0483d98d5a2 Mon Sep 17 00:00:00 2001 From: Mystic <215104920@qq.com> Date: Tue, 13 Aug 2024 18:19:18 +0800 Subject: [PATCH] refactor: optimize the cmakelist --- .../CMakeLists.txt | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/template/cxx/{{cookiecutter.project_slug}}/CMakeLists.txt b/template/cxx/{{cookiecutter.project_slug}}/CMakeLists.txt index 585a2b6..67a60a9 100644 --- a/template/cxx/{{cookiecutter.project_slug}}/CMakeLists.txt +++ b/template/cxx/{{cookiecutter.project_slug}}/CMakeLists.txt @@ -1,11 +1,15 @@ cmake_minimum_required(VERSION {{cookiecutter.cmake_min_version}}) project({{cookiecutter.project_slug}} LANGUAGES CXX) +# C++ Standard settings set(CMAKE_CXX_STANDARD {{cookiecutter.cxx_standard_version}}) set(CMAKE_CXX_STANDARD_REQUIRED {{cookiecutter.cxx_standard_required}}) -set(CMAKE_CXX_EXTENSIONS {{cookiecutter.cxx_extensions_required}}) # enable compiler-specific features or not -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # clangd completion +set(CMAKE_CXX_EXTENSIONS {{cookiecutter.cxx_extensions_required}}) # enable compiler-specific features or not +# Enable compile commands for clangd +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +# Source files file(GLOB SOURCES CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/src/*.h ${CMAKE_SOURCE_DIR}/src/*.cpp) # Speed up the compile by ccache @@ -18,22 +22,17 @@ if(NOT MSVC) endif() endif() -{% if cookiecutter.project_type == "binary" -%} +# Determine target type +set(IS_BINARY {{ cookiecutter.project_type == "binary" }}) +set(IS_SHARED {{ cookiecutter.is_shared }}) +if(IS_BINARY) add_executable(${PROJECT_NAME} ${SOURCES}) +else() + add_library({{cookiecutter.project_slug}} ${IS_SHARED} src/library.cpp) +endif() -{% else -%} - - {% if not cookiecutter.is_shared -%} - # static library - add_library({{cookiecutter.project_slug}} STATIC src/library.cpp) - {% else -%} - # shared library - add_library({{cookiecutter.project_slug}} SHARED src/library.cpp) - {% endif %} - -{% endif %} - -{% if cookiecutter.project_type == "binary" and not cookiecutter.is_shared -%} +# Link static libraries for binary projects if not shared +if(IS_BINARY AND NOT IS_SHARED) target_link_libraries(${PROJECT_NAME} PUBLIC "-static") -{% endif %} +endif()