Skip to content

Commit 3ec69ff

Browse files
committed
Convert CMakeLists.txt to "modern" cmake style
This entails changing most global variables to target attributes and being careful about PUBLIC vs PRIVATE target attributes. For those global variables we must have, like the options, give them a SCITOKENS prefix as an ersatz namespace. Also, it means using built-in cmake mechanisms to set things like the minimum C++ standard required, debugging and optimization flags in a portable manner. Note that with these changes, scitokens-cpp is now cmake fetchable. That is, a client can chose to use this library in their own cmake by just doing this: include(FetchContent) FetchContent_Declare(SciTokens GIT_REPOSITORY https://github.com/scitokens/scitokens-cpp GIT_TAG master) FetchContent_MakeAvailable(SciTokens) add_executable(test test.cpp) target_link_libraries(test SciTokens) And all the -l -D and -I flags flow through the SciTokens target to the users of that target, but don't impact any targets that don't link with SciTokens.
1 parent 2f22c4f commit 3ec69ff

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

CMakeLists.txt

+30-25
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11

2-
cmake_minimum_required( VERSION 2.6 )
3-
project( scitokens-cpp )
2+
cmake_minimum_required( VERSION 3.10)
43

5-
option( BUILD_UNITTESTS "Build the scitokens-cpp unit tests" OFF )
6-
option( EXTERNAL_GTEST "Use an external/pre-installed copy of GTest" OFF )
4+
project( scitokens-cpp
5+
DESCRIPTION "A C++ Library to interface to scitokens"
6+
VERSION 0.7.0
7+
LANGUAGES CXX)
78

8-
set( CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake )
9+
option( SCITOKENS_BUILD_UNITTESTS "Build the scitokens-cpp unit tests" OFF )
10+
option( SCITOKENS_EXTERNAL_GTEST "Use an external/pre-installed copy of GTest" OFF )
11+
12+
set( CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}" )
13+
14+
set( CMAKE_BUILD_TYPE RelWithDebInfo) # -g -O2
915

1016
include(GNUInstallDirs)
1117

1218
find_package( jwt-cpp REQUIRED )
1319
find_package( CURL REQUIRED )
1420
find_package( UUID REQUIRED )
1521

16-
if( CMAKE_COMPILER_IS_GNUCXX )
17-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror" )
18-
endif()
19-
20-
if( CMAKE_COMPILER_IS_GNUCC )
21-
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror" )
22-
endif()
23-
24-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
25-
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
26-
27-
2822
if( APPLE )
2923

3024
find_package( OpenSSL REQUIRED )
@@ -40,34 +34,45 @@ pkg_check_modules(LIBCRYPTO REQUIRED libcrypto)
4034
pkg_check_modules(OPENSSL REQUIRED openssl)
4135
pkg_check_modules(SQLITE REQUIRED sqlite3)
4236

43-
# pkg_check_modules fails to return an absolute path on RHEL7. Set the
44-
# link directories accordingly.
45-
link_directories(${OPENSSL_LIBRARY_DIRS} ${LIBCRYPTO_LIBRARY_DIRS})
4637
endif()
4738

48-
include_directories( "${PROJECT_SOURCE_DIR}" ${JWT_CPP_INCLUDES} ${CURL_INCLUDES} ${OPENSSL_INCLUDE_DIRS} ${LIBCRYPTO_INCLUDE_DIRS} ${SQLITE_INCLUDE_DIRS} ${UUID_INCLUDE_DIRS} )
49-
5039
add_library(SciTokens SHARED src/scitokens.cpp src/scitokens_internal.cpp src/scitokens_cache.cpp)
51-
target_link_libraries(SciTokens ${OPENSSL_LIBRARIES} ${LIBCRYPTO_LIBRARIES} ${CURL_LIBRARIES} ${SQLITE_LIBRARIES} ${UUID_LIBRARIES})
40+
target_compile_features(SciTokens PUBLIC cxx_std_11) # Use at least C++11 for building and when linking to scitokens
41+
target_compile_options(SciTokens PRIVATE $<$<CXX_COMPILER_ID:GNU>:-Wall -Werror>)
42+
target_include_directories(SciTokens PUBLIC ${JWT_CPP_INCLUDES} "${PROJECT_SOURCE_DIR}/src" PRIVATE ${CURL_INCLUDES} ${OPENSSL_INCLUDE_DIRS} ${LIBCRYPTO_INCLUDE_DIRS} ${SQLITE_INCLUDE_DIRS} ${UUID_INCLUDE_DIRS})
43+
44+
target_link_libraries(SciTokens PUBLIC ${OPENSSL_LIBRARIES} ${LIBCRYPTO_LIBRARIES} ${CURL_LIBRARIES} ${SQLITE_LIBRARIES} ${UUID_LIBRARIES})
45+
if (UNIX)
46+
# pkg_check_modules fails to return an absolute path on RHEL7. Set the
47+
# link directories accordingly.
48+
target_link_directories(SciTokens PUBLIC ${OPENSSL_LIBRARY_DIRS} ${LIBCRYPTO_LIBRARY_DIRS})
49+
endif()
5250

5351
if ( NOT APPLE AND UNIX )
5452
set_target_properties(SciTokens PROPERTIES LINK_FLAGS "-Wl,--version-script=${PROJECT_SOURCE_DIR}/configs/export-symbols")
5553
endif()
5654

5755
add_executable(scitokens-test src/test.cpp)
56+
#target_include_directories(scitokens-test PRIVATE "${PROJECT_SOURCE_DIR}" ${JWT_CPP_INCLUDES} ${CURL_INCLUDES} ${OPENSSL_INCLUDE_DIRS} ${LIBCRYPTO_INCLUDE_DIRS} ${SQLITE_INCLUDE_DIRS} ${UUID_INCLUDE_DIRS})
57+
target_include_directories(scitokens-test PRIVATE "${PROJECT_SOURCE_DIR}" ${JWT_CPP_INCLUDES})
5858
target_link_libraries(scitokens-test SciTokens)
59+
target_compile_options(scitokens-test PRIVATE $<$<CXX_COMPILER_ID:GNU>:-Wall -Werror>)
5960

6061
add_executable(scitokens-verify src/verify.cpp)
6162
target_link_libraries(scitokens-verify SciTokens)
63+
target_compile_options(scitokens-verify PRIVATE $<$<CXX_COMPILER_ID:GNU>:-Wall -Werror>)
6264

6365
add_executable(scitokens-test-access src/test_access.cpp)
6466
target_link_libraries(scitokens-test-access SciTokens)
67+
target_compile_options(scitokens-test-access PRIVATE $<$<CXX_COMPILER_ID:GNU>:-Wall -Werror>)
6568

6669
add_executable(scitokens-list-access src/list_access.cpp)
6770
target_link_libraries(scitokens-list-access SciTokens)
71+
target_compile_options(scitokens-list-access PRIVATE $<$<CXX_COMPILER_ID:GNU>:-Wall -Werror>)
6872

6973
add_executable(scitokens-create src/create.cpp)
7074
target_link_libraries(scitokens-create SciTokens)
75+
target_compile_options(scitokens-create PRIVATE $<$<CXX_COMPILER_ID:GNU>:-Wall -Werror>)
7176

7277
get_directory_property(TARGETS BUILDSYSTEM_TARGETS)
7378
install(
@@ -86,8 +91,8 @@ set_target_properties(
8691
SOVERSION "0"
8792
)
8893

89-
if( BUILD_UNITTESTS )
90-
if( NOT EXTERNAL_GTEST )
94+
if( SCITOKENS_BUILD_UNITTESTS )
95+
if( NOT SCITOKENS_EXTERNAL_GTEST )
9196
include(ExternalProject)
9297
ExternalProject_Add(gtest
9398
PREFIX external/gtest

test/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ add_executable(scitokens-gtest main.cpp)
33
add_dependencies(scitokens-gtest gtest)
44
include_directories("${PROJECT_SOURCE_DIR}/vendor/gtest/googletest/include")
55

6-
if(EXTERNAL_GTEST)
6+
if(SCITOKENS_EXTERNAL_GTEST)
77
set(LIBGTEST "gtest")
88
else()
99
set(LIBGTEST "${CMAKE_BINARY_DIR}/external/gtest/src/gtest-build/lib/libgtest.a")

0 commit comments

Comments
 (0)