Skip to content

Commit

Permalink
perf: enable exe target more applicable (#323)
Browse files Browse the repository at this point in the history
* perf: enable exe target more applicable
 - Rename <target_name>_private to <target_name>_internal.
 - Make the internal target as a compiled object target to conform to the real situation.
* make the internal target warned, hardened, sanitized and covered

---------

Signed-off-by: msclock <msclock@qq.com>
  • Loading branch information
msclock authored Jul 27, 2024
1 parent f1f8ebb commit 7ce3c7d
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 36 deletions.
31 changes: 21 additions & 10 deletions src/exe/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
find_package(spdlog CONFIG REQUIRED)

# The private target will not be installed and only act as requirements for main
# and tests internally
set(target_name_private exe_private)
# The internal target will not be installed and only act as requirements for
# main and tests internally
set(target_name_internal exe_internal)

add_library(${target_name_private} INTERFACE)
file(GLOB_RECURSE internal_srcs "src/*.cpp")
file(GLOB_RECURSE internal_hdrs "include/*.hpp")

list(FILTER internal_srcs EXCLUDE REGEX "main.cpp")

add_library(${target_name_internal} OBJECT)
warn_target(${target_name_internal})
harden_target(${target_name_internal})
sanitize_target(${target_name_internal})
target_code_coverage(${target_name_internal} ALL)

generate_git_header(VERSION_NAMESPACE_PREFIX exe)

target_sources(${target_name_internal} PRIVATE ${internal_srcs}
${internal_hdrs})

target_include_interface_directories(
${target_name_private} ${CMAKE_CURRENT_SOURCE_DIR}/include
${target_name_internal} ${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_BINARY_DIR}/git_version)

target_link_libraries(${target_name_private} INTERFACE spdlog::spdlog)
target_link_libraries(${target_name_internal} PUBLIC spdlog::spdlog)

# The main target only contains main logits. Here it is recommended to put the
# parts that need to be verified and tested in the target target_name_private
# parts that need to be verified and tested in the target target_name_internal
set(target_name exe)

file(GLOB_RECURSE _srcs "src/*.cpp")
file(GLOB_RECURSE _srcs "src/main.cpp")

add_executable(${target_name} ${_srcs})
warn_target(${target_name})
harden_target(${target_name})
sanitize_target(${target_name})
target_code_coverage(${target_name} ALL)

target_link_libraries(${target_name} PRIVATE spdlog::spdlog
${target_name_private})
target_link_libraries(${target_name} PRIVATE ${target_name_internal})

install(TARGETS ${target_name}
RUNTIME DESTINATION $<$<CONFIG:Debug>:debug/>${CMAKE_INSTALL_BINDIR})
Expand Down
11 changes: 4 additions & 7 deletions src/exe/include/distribution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

namespace exe {
namespace distribution {
inline bool is_debug() noexcept {
#ifdef _DEBUG
return true;
#else
return false;
#endif
}
/**
* \return A bool represents if it is debug distribution.
*/
bool is_debug() noexcept;
} // namespace distribution
} // namespace exe
13 changes: 13 additions & 0 deletions src/exe/src/distribution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "distribution.hpp"

namespace exe {
namespace distribution {
bool is_debug() noexcept {
#ifdef _DEBUG
return true;
#else
return false;
#endif
}
} // namespace distribution
} // namespace exe
2 changes: 1 addition & 1 deletion src/exe/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ file(GLOB files "*.cpp")
foreach(_file ${files})
get_filename_component(file_basename ${_file} NAME_WE)
add_executable(${file_basename} ${_file})
target_link_libraries(${file_basename} PRIVATE GTest::gtest_main exe_private)
target_link_libraries(${file_basename} PRIVATE GTest::gtest_main exe_internal)
warn_target(${file_basename})
harden_target(${file_basename})
sanitize_target(${file_basename})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
find_package(spdlog CONFIG REQUIRED)

# The private target will not be installed and only act as requirements for main
# and tests internally
set(target_name_private {{ exe_target }}_private)
# The internal target will not be installed and only act as requirements for
# main and tests internally
set(target_name_internal {{ exe_target }}_internal)

add_library(${target_name_private} INTERFACE)
file(GLOB_RECURSE internal_srcs "src/*.cpp")
file(GLOB_RECURSE internal_hdrs "include/*.hpp")

list(FILTER internal_srcs EXCLUDE REGEX "main.cpp")

add_library(${target_name_internal} OBJECT)
warn_target(${target_name_internal})
harden_target(${target_name_internal})
sanitize_target(${target_name_internal})
target_code_coverage(${target_name_internal} ALL)

generate_git_header(VERSION_NAMESPACE_PREFIX {{ exe_target }})

target_sources(${target_name_internal} PRIVATE ${internal_srcs}
${internal_hdrs})

target_include_interface_directories(
${target_name_private} ${CMAKE_CURRENT_SOURCE_DIR}/include
${target_name_internal} ${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_BINARY_DIR}/git_version)

target_link_libraries(${target_name_private} INTERFACE spdlog::spdlog)
target_link_libraries(${target_name_internal} PUBLIC spdlog::spdlog)

# The main target only contains main logits. Here it is recommended to put the
# parts that need to be verified and tested in the target target_name_private
# parts that need to be verified and tested in the target target_name_internal
set(target_name {{ exe_target }})

file(GLOB_RECURSE _srcs "src/*.cpp")
file(GLOB_RECURSE _srcs "src/main.cpp")

add_executable(${target_name} ${_srcs})
warn_target(${target_name})
harden_target(${target_name})
sanitize_target(${target_name})
target_code_coverage(${target_name} ALL)

target_link_libraries(${target_name} PRIVATE spdlog::spdlog
${target_name_private})
target_link_libraries(${target_name} PRIVATE ${target_name_internal})

install(TARGETS ${target_name}
RUNTIME DESTINATION $<$<CONFIG:Debug>:debug/>${CMAKE_INSTALL_BINDIR})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

namespace {{ exe_target }} {
namespace distribution {
inline bool is_debug() noexcept {
#ifdef _DEBUG
return true;
#else
return false;
#endif
}
/**
* \return A bool represents if it is debug distribution.
*/
bool is_debug() noexcept;
} // namespace distribution
} // namespace {{ exe_target }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "distribution.hpp"

namespace {{ exe_target }} {
namespace distribution {
bool is_debug() noexcept {
#ifdef _DEBUG
return true;
#else
return false;
#endif
}
} // namespace distribution
} // namespace {{ exe_target }}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ file(GLOB files "*.cpp")
foreach(_file ${files})
get_filename_component(file_basename ${_file} NAME_WE)
add_executable(${file_basename} ${_file})
target_link_libraries(${file_basename} PRIVATE GTest::gtest_main {{ exe_target }}_private)
target_link_libraries(${file_basename} PRIVATE GTest::gtest_main {{ exe_target }}_internal)
warn_target(${file_basename})
harden_target(${file_basename})
sanitize_target(${file_basename})
Expand Down

0 comments on commit 7ce3c7d

Please sign in to comment.