Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ${PROJECT_NAME} instead of writing projectname multiple times #134

Merged
merged 4 commits into from
Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ file(GLOB_RECURSE sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/
# ---- Create library ----

# Note: for header-only libraries change all PUBLIC flags to INTERFACE and create an interface
# target: add_library(Greeter INTERFACE)
add_library(Greeter ${headers} ${sources})
# target: add_library(${PROJECT_NAME} INTERFACE)
add_library(${PROJECT_NAME} ${headers} ${sources})

set_target_properties(Greeter PROPERTIES CXX_STANDARD 17)
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17)

# being a cross-platform target, we enforce standards conformance on MSVC
target_compile_options(Greeter PUBLIC "$<$<COMPILE_LANG_AND_ID:CXX,MSVC>:/permissive->")
target_compile_options(${PROJECT_NAME} PUBLIC "$<$<COMPILE_LANG_AND_ID:CXX,MSVC>:/permissive->")

# Link dependencies
target_link_libraries(Greeter PRIVATE fmt::fmt)
target_link_libraries(${PROJECT_NAME} PRIVATE fmt::fmt)

target_include_directories(
Greeter PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
)

Expand Down
4 changes: 2 additions & 2 deletions documentation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ project(GreeterDocs)
include(../cmake/CPM.cmake)

CPMAddPackage("gh:mosra/m.css#42d4a9a48f31f5df6e246c948403b54b50574a2a")
CPMAddPackage(NAME Greeter SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
CPMAddPackage(NAME ${CMAKE_PROJECT_NAME} SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..)

# ---- Doxygen variables ----

# set Doxyfile variables
set(DOXYGEN_PROJECT_NAME Greeter)
set(DOXYGEN_PROJECT_NAME ${CMAKE_PROJECT_NAME})
set(DOXYGEN_PROJECT_VERSION ${Greeter_VERSION})
set(DOXYGEN_PROJECT_ROOT "${CMAKE_CURRENT_LIST_DIR}/..")
set(DOXYGEN_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/doxygen")
Expand Down
8 changes: 4 additions & 4 deletions standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ CPMAddPackage(
OPTIONS "CXXOPTS_BUILD_EXAMPLES NO" "CXXOPTS_BUILD_TESTS NO" "CXXOPTS_ENABLE_INSTALL YES"
)

CPMAddPackage(NAME Greeter SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
CPMAddPackage(NAME ${CMAKE_PROJECT_NAME} SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..)

# ---- Create standalone executable ----

file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp)

add_executable(GreeterStandalone ${sources})
add_executable(${PROJECT_NAME} ${sources})

set_target_properties(GreeterStandalone PROPERTIES CXX_STANDARD 17 OUTPUT_NAME "Greeter")
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17 OUTPUT_NAME "Greeter")

target_link_libraries(GreeterStandalone Greeter::Greeter cxxopts)
target_link_libraries(${PROJECT_NAME} Greeter::Greeter cxxopts)
24 changes: 12 additions & 12 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ CPMAddPackage("gh:onqtam/doctest#2.4.5")
CPMAddPackage("gh:TheLartians/Format.cmake@1.7.0")

if(TEST_INSTALLED_VERSION)
find_package(Greeter REQUIRED)
find_package(${CMAKE_PROJECT_NAME} REQUIRED)
else()
CPMAddPackage(NAME Greeter SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
CPMAddPackage(NAME ${CMAKE_PROJECT_NAME} SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this would work, isn't ${CMAKE_PROJECT_NAME} the same as the project name "GreeterTests" in this context, as it's the top-level CMakeLists?

Copy link
Contributor

@ClausKlein ClausKlein Sep 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO you should only use ${PROJECT_NAME} in the top level project.

Only then it would work as standalone project and as subproject used with CPM.cmake in other CMake projects.

The test project is a standalone project and an subproject. Being explizit would be better, or not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ClausKlein If test project is designed to be a standalone project then yes it may make sense to remove ${CMAKE_PROJECT_NAME}.

@TheLartians
And regarding how it works this answer helped me to understand:
https://stackoverflow.com/questions/38938315/difference-between-cmake-project-name-and-project-name

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DominicD yeah as I understand it, ${CMAKE_PROJECT_NAME} should always refers to the most recent top-level project name. So it is context-dependent and we cannot know which value it holds. So if you run cmake -Stest -Bbuild, then ${CMAKE_PROJECT_NAME} should be GreeterTests, but if you run cmake -Sall -Bbuild, it should be BuildAll etc.

I think this is also why the "Install" CI test is failing.

endif()

# ---- Create binary ----

file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp)
add_executable(GreeterTests ${sources})
target_link_libraries(GreeterTests doctest::doctest Greeter::Greeter)
set_target_properties(GreeterTests PROPERTIES CXX_STANDARD 17)
add_executable(${PROJECT_NAME} ${sources})
target_link_libraries(${PROJECT_NAME} doctest::doctest Greeter::Greeter)
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17)

# enable compiler warnings
if(NOT TEST_INSTALLED_VERSION)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_compile_options(Greeter PUBLIC -Wall -Wpedantic -Wextra -Werror)
target_compile_options(${CMAKE_PROJECT_NAME} PUBLIC -Wall -Wpedantic -Wextra -Werror)
elseif(MSVC)
target_compile_options(Greeter PUBLIC /W4 /WX)
target_compile_definitions(GreeterTests PUBLIC DOCTEST_CONFIG_USE_STD_HEADERS)
target_compile_options(${CMAKE_PROJECT_NAME} PUBLIC /W4 /WX)
target_compile_definitions(${PROJECT_NAME} PUBLIC DOCTEST_CONFIG_USE_STD_HEADERS)
endif()
endif()

Expand All @@ -46,14 +46,14 @@ endif()
enable_testing()

# Note: doctest and similar testing frameworks can automatically configure CMake tests. For other
# testing frameworks add the tests target instead: add_test(NAME greeterTests COMMAND GreeterTests)
# testing frameworks add the tests target instead: add_test(NAME ${PROJECT_NAME} COMMAND GreeterTests)

include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake)
doctest_discover_tests(GreeterTests)
doctest_discover_tests(${PROJECT_NAME})

# ---- code coverage ----

if(ENABLE_TEST_COVERAGE)
target_compile_options(Greeter PUBLIC -O0 -g -fprofile-arcs -ftest-coverage)
target_link_options(Greeter PUBLIC -fprofile-arcs -ftest-coverage)
target_compile_options(${CMAKE_PROJECT_NAME} PUBLIC -O0 -g -fprofile-arcs -ftest-coverage)
target_link_options(${CMAKE_PROJECT_NAME} PUBLIC -fprofile-arcs -ftest-coverage)
endif()