Skip to content

Commit 8a066c9

Browse files
authored
Merge pull request #59 from gocarlos/master
build: improve cmake scripts
2 parents ce85f79 + 4dd70e5 commit 8a066c9

File tree

9 files changed

+270
-55
lines changed

9 files changed

+270
-55
lines changed

.github/workflows/main.yml

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: CMake
2+
3+
on: [push]
4+
5+
env:
6+
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
7+
BUILD_TYPE: RelWithDebInfo
8+
9+
jobs:
10+
build:
11+
runs-on: ${{ matrix.os }}
12+
strategy:
13+
matrix:
14+
os: [windows-latest, ubuntu-latest]
15+
16+
steps:
17+
- uses: actions/checkout@v1
18+
19+
- uses: actions/setup-python@v1
20+
with:
21+
python-version: "3.x"
22+
23+
- name: Install tools (Linux)
24+
if: startsWith(runner.os, 'Linux')
25+
run: |
26+
sudo apt-get install python3-setuptools python3-wheel python3-pip
27+
shell: bash
28+
29+
- name: Install conan (Linux)
30+
if: startsWith(runner.os, 'Linux')
31+
run: |
32+
sudo pip3 install conan --upgrade
33+
shell: bash
34+
35+
- name: Install conan (Windows)
36+
if: startsWith(runner.os, 'Windows')
37+
run: |
38+
pip3 install conan --upgrade
39+
shell: bash
40+
41+
- name: Create Build Environment
42+
run: cmake -E make_directory ${{runner.workspace}}/build
43+
44+
- name: Install conan profile
45+
working-directory: ${{runner.workspace}}/build
46+
run: conan profile new default --detect
47+
48+
- name: Use cpp 11 (Linux)
49+
if: startsWith(runner.os, 'Linux')
50+
run: |
51+
conan profile update settings.compiler.libcxx=libstdc++11 default
52+
shell: bash
53+
54+
- name: Install conan dependencies
55+
working-directory: ${{runner.workspace}}/build
56+
run: conan install $GITHUB_WORKSPACE --build missing
57+
shell: bash
58+
59+
- name: Configure CMake
60+
shell: bash
61+
working-directory: ${{runner.workspace}}/build
62+
run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE
63+
64+
- name: Build
65+
working-directory: ${{runner.workspace}}/build
66+
shell: bash
67+
run: cmake --build . --config $BUILD_TYPE
68+
69+
- name: Test
70+
working-directory: ${{runner.workspace}}/build
71+
shell: bash
72+
run: ctest --verbose

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build/

CMakeLists.txt

+86-17
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,95 @@
1-
# CMakeLists files in this project can
2-
# refer to the root source directory of the project as ${HELLO_SOURCE_DIR} and
3-
# to the root binary directory of the project as ${HELLO_BINARY_DIR}.
1+
cmake_minimum_required(VERSION 3.5.0)
2+
project(cpp-jwt)
43

5-
cmake_minimum_required (VERSION 2.8.11)
6-
project (cpp-jwt)
4+
option(CPP_JWT_BUILD_EXAMPLES "build examples" ON)
5+
option(CPP_JWT_BUILD_TESTS "build tests" ON)
6+
option(CPP_JWT_USE_VENDORED_NLOHMANN_JSON "use vendored json header" ON)
77

8-
#SET (CMAKE_CXX_COMPILER /usr/local/bin/g++)
9-
SET( CMAKE_CXX_FLAGS "-std=c++14 -Wall -Wextra" )
8+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
9+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_BINARY_DIR})
1010

11-
include_directories (include)
11+
# only set compiler flags if we are the main project, otherwise let the main
12+
# project decide on the flags
13+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
14+
set(CMAKE_CXX_STANDARD 14)
15+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
16+
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}"
17+
MATCHES "Clang")
18+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
19+
endif()
20+
21+
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
22+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
23+
endif()
24+
25+
endif()
1226

1327
find_package(OpenSSL REQUIRED)
14-
include_directories(${OPENSSL_INCLUDE_DIR})
1528

16-
find_package(GTest REQUIRED)
17-
include_directories(${GTEST_INCLUDE_DIRS})
29+
if(NOT CPP_JWT_USE_VENDORED_NLOHMANN_JSON)
30+
find_package(nlohmann_json REQUIRED)
31+
endif()
32+
33+
# ##############################################################################
34+
# LIBRARY
35+
# ##############################################################################
36+
37+
add_library(${PROJECT_NAME} INTERFACE)
38+
target_include_directories(
39+
${PROJECT_NAME}
40+
INTERFACE $<BUILD_INTERFACE:${${PROJECT_NAME}_SOURCE_DIR}/include>
41+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
42+
${OpenSSL_INCLUDE_DIR})
43+
target_link_libraries(${PROJECT_NAME} INTERFACE ${OpenSSL_LIBRARIES})
44+
if(NOT CPP_JWT_USE_VENDORED_NLOHMANN_JSON)
45+
target_link_libraries(${PROJECT_NAME} INTERFACE nlohmann_json::nlohmann_json)
46+
else()
47+
add_definitions(-DCPP_JWT_USE_VENDORED_NLOHMANN_JSON)
48+
endif()
49+
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_14)
50+
51+
# ##############################################################################
52+
# TESTS
53+
# ##############################################################################
54+
55+
if(CPP_JWT_BUILD_TESTS)
56+
find_package(GTest REQUIRED)
57+
include_directories(${GTEST_INCLUDE_DIRS})
58+
enable_testing()
59+
# Recurse into the "Hello" and "Demo" subdirectories. This does not actually
60+
# cause another cmake executable to run. The same process will walk through
61+
# the project's entire directory structure.
62+
add_subdirectory(tests)
63+
endif()
64+
65+
# ##############################################################################
66+
# EXAMPLES
67+
# ##############################################################################
68+
69+
if(CPP_JWT_BUILD_EXAMPLES)
70+
add_subdirectory(examples)
71+
endif()
1872

19-
enable_testing()
73+
# ##############################################################################
74+
# INSTALL
75+
# ##############################################################################
2076

21-
# Recurse into the "Hello" and "Demo" subdirectories. This does not actually
22-
# cause another cmake executable to run. The same process will walk through
23-
# the project's entire directory structure.
24-
add_subdirectory (tests)
77+
include(GNUInstallDirs)
78+
install(
79+
TARGETS ${PROJECT_NAME}
80+
EXPORT ${PROJECT_NAME}_Targets
81+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
82+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
83+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2584

26-
add_subdirectory (examples)
85+
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/jwt/detail
86+
DESTINATION include/jwt)
87+
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/jwt/impl
88+
DESTINATION include/jwt)
89+
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/jwt/json
90+
DESTINATION include/jwt)
91+
install(
92+
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/jwt/
93+
DESTINATION include/jwt
94+
FILES_MATCHING
95+
PATTERN "*.hpp")

README.md

+19-4
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,27 @@ Tested with <strong>clang-5.0</strong> and <strong>g++-6.4</strong>.
209209
With issue#12, <strong>VS2017</strong> is also supported.
210210

211211
## Installation
212-
Use the C++ package manager..... just kidding :)
213-
This is a header only library, so you can just add it to your include path and start using it. The only somewhat tricky part is to link it with openssl library. Check out the cmake file for building it properly.
214212

215-
For example one can run cmake like:
213+
### using conan
214+
215+
```shell
216+
mkdir build
217+
cd build
218+
conan install .. --build missing
219+
cmake ..
220+
cmake --build . -j
216221
```
217-
cmake -DOPENSSL_ROOT_DIR=/usr/local/Cellar/openssl/1.0.2j -DGTEST_ROOT=$HOME/googletest
222+
223+
### using debian
224+
225+
```shell
226+
sudo apt install nlohmann-json3-dev
227+
sudo apt install libgtest-dev
228+
sudo apt install libssl-dev
229+
mkdir build
230+
cd build
231+
cmake ..
232+
cmake --build . -j
218233
```
219234

220235
## Parameters

conanfile.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[requires]
2+
gtest/1.10.0
3+
nlohmann_json/3.7.0
4+
openssl/1.1.1d
5+
6+
[generators]
7+
cmake_find_package
8+
cmake_paths
9+
10+
[options]

examples/CMakeLists.txt

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
2-
include_directories(${OPENSSL_INCLUDE_DIR})
3-
4-
SET(CERT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/rsa_256")
5-
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCERT_ROOT_DIR=\"\\\"${CERT_ROOT_DIR}\\\"\"")
1+
set(CERT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/rsa_256")
2+
set(CMAKE_CXX_FLAGS
3+
"${CMAKE_CXX_FLAGS} -DCERT_ROOT_DIR=\"\\\"${CERT_ROOT_DIR}\\\"\"")
64

75
add_executable(simple_ex1 simple_ex1.cc)
8-
target_link_libraries(simple_ex1 ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES})
9-
add_test(NAME simple_ex1 COMMAND ./simple_ex1 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
6+
target_link_libraries(simple_ex1 ${PROJECT_NAME})
7+
add_test(
8+
NAME simple_ex1
9+
COMMAND ./simple_ex1
10+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
1011

1112
add_executable(simple_ex2 simple_ex2.cc)
12-
target_link_libraries(simple_ex2 ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES})
13-
add_test(NAME simple_ex2 COMMAND ./simple_ex2 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
13+
target_link_libraries(simple_ex2 ${PROJECT_NAME})
14+
add_test(
15+
NAME simple_ex2
16+
COMMAND ./simple_ex2
17+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
1418

1519
add_executable(simple_ex3_rsa simple_ex3_rsa.cc)
16-
target_link_libraries(simple_ex3_rsa ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES})
17-
add_test(NAME simple_ex3_rsa COMMAND ./simple_ex3_rsa WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
18-
20+
target_link_libraries(simple_ex3_rsa ${PROJECT_NAME})
21+
add_test(
22+
NAME simple_ex3_rsa
23+
COMMAND ./simple_ex3_rsa
24+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

include/jwt/json/test_json.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#include <iostream>
22
#include <string>
3+
#if defined( CPP_JWT_USE_VENDORED_NLOHMANN_JSON)
34
#include "./json.hpp"
4-
5+
#else
6+
#include "nlohmann/json.hpp"
7+
#endif
58
using json = nlohmann::json;
69

710
void basic_json_test()

include/jwt/jwt.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ SOFTWARE.
3838
#include "jwt/string_view.hpp"
3939
#include "jwt/parameters.hpp"
4040
#include "jwt/exceptions.hpp"
41+
#if defined(CPP_JWT_USE_VENDORED_NLOHMANN_JSON)
4142
#include "jwt/json/json.hpp"
42-
43+
#else
44+
#include "nlohmann/json.hpp"
45+
#endif
4346
// For convenience
4447
using json_t = nlohmann::json;
4548
using system_time_t = std::chrono::time_point<std::chrono::system_clock>;

tests/CMakeLists.txt

+56-20
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,69 @@
1-
2-
include_directories(${OPENSSL_INCLUDE_DIR})
3-
4-
SET(CERT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/certs")
5-
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCERT_ROOT_DIR=\"\\\"${CERT_ROOT_DIR}\\\"\"")
1+
set(CERT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/certs")
2+
set(CMAKE_CXX_FLAGS
3+
"${CMAKE_CXX_FLAGS} -DCERT_ROOT_DIR=\"\\\"${CERT_ROOT_DIR}\\\"\"")
64

75
add_executable(test_jwt_object test_jwt_object.cc)
8-
target_link_libraries(test_jwt_object ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES})
9-
add_test(NAME test_jwt_object COMMAND ./test_jwt_object WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
6+
target_link_libraries(test_jwt_object ${GTest_LIBRARIES} ${PROJECT_NAME})
7+
target_include_directories(test_jwt_object PRIVATE ${GTEST_INCLUDE_DIRS}
8+
${GTest_INCLUDE_DIRS})
9+
add_test(
10+
NAME test_jwt_object
11+
COMMAND ./test_jwt_object
12+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
1013

1114
add_executable(test_jwt_encode test_jwt_encode.cc)
12-
target_link_libraries(test_jwt_encode ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES})
13-
add_test(NAME test_jwt_encode COMMAND ./test_jwt_encode WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
15+
target_link_libraries(test_jwt_encode ${GTest_LIBRARIES} ${PROJECT_NAME})
16+
target_include_directories(test_jwt_encode PRIVATE ${GTEST_INCLUDE_DIRS}
17+
${GTest_INCLUDE_DIRS})
18+
add_test(
19+
NAME test_jwt_encode
20+
COMMAND ./test_jwt_encode
21+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
1422

1523
add_executable(test_jwt_decode test_jwt_decode.cc)
16-
target_link_libraries(test_jwt_decode ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES})
17-
add_test(NAME test_jwt_decode COMMAND ./test_jwt_decode WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
24+
target_link_libraries(test_jwt_decode ${GTest_LIBRARIES} ${PROJECT_NAME})
25+
target_include_directories(test_jwt_decode PRIVATE ${GTEST_INCLUDE_DIRS}
26+
${GTest_INCLUDE_DIRS})
27+
add_test(
28+
NAME test_jwt_decode
29+
COMMAND ./test_jwt_decode
30+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
1831

1932
add_executable(test_jwt_decode_verifiy test_jwt_decode_verifiy.cc)
20-
target_link_libraries(test_jwt_decode_verifiy ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES})
21-
add_test(NAME test_jwt_decode_verifiy COMMAND ./test_jwt_decode_verifiy WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
33+
target_link_libraries(test_jwt_decode_verifiy ${GTest_LIBRARIES}
34+
${PROJECT_NAME})
35+
target_include_directories(test_jwt_decode_verifiy
36+
PRIVATE ${GTEST_INCLUDE_DIRS} ${GTest_INCLUDE_DIRS})
37+
add_test(
38+
NAME test_jwt_decode_verifiy
39+
COMMAND ./test_jwt_decode_verifiy
40+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
2241

23-
add_executable(test_jwt_decode_verifiy_with_exception test_jwt_decode_verifiy_with_exception.cc)
24-
target_link_libraries(test_jwt_decode_verifiy_with_exception ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES})
25-
add_test(NAME test_jwt_decode_verifiy_with_exception COMMAND ./test_jwt_decode_verifiy_with_exception WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
42+
add_executable(test_jwt_decode_verifiy_with_exception
43+
test_jwt_decode_verifiy_with_exception.cc)
44+
target_link_libraries(test_jwt_decode_verifiy_with_exception ${GTest_LIBRARIES}
45+
${PROJECT_NAME})
46+
target_include_directories(test_jwt_decode_verifiy_with_exception
47+
PRIVATE ${GTEST_INCLUDE_DIRS} ${GTest_INCLUDE_DIRS})
48+
add_test(
49+
NAME test_jwt_decode_verifiy_with_exception
50+
COMMAND ./test_jwt_decode_verifiy_with_exception
51+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
2652

2753
add_executable(test_jwt_rsa test_jwt_rsa.cc)
28-
target_link_libraries(test_jwt_rsa ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES} )
29-
add_test(NAME test_jwt_rsa COMMAND ./test_jwt_rsa WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
54+
target_link_libraries(test_jwt_rsa ${GTest_LIBRARIES} ${PROJECT_NAME})
55+
target_include_directories(test_jwt_rsa PRIVATE ${GTEST_INCLUDE_DIRS}
56+
${GTest_INCLUDE_DIRS})
57+
add_test(
58+
NAME test_jwt_rsa
59+
COMMAND ./test_jwt_rsa
60+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
3061

3162
add_executable(test_jwt_es test_jwt_es.cc)
32-
target_link_libraries(test_jwt_es ${OPENSSL_LIBRARIES} ${GTEST_LIBRARIES})
33-
add_test(NAME test_jwt_es COMMAND ./test_jwt_es WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
63+
target_link_libraries(test_jwt_es ${GTest_LIBRARIES} ${PROJECT_NAME})
64+
target_include_directories(test_jwt_es PRIVATE ${GTEST_INCLUDE_DIRS}
65+
${GTest_INCLUDE_DIRS})
66+
add_test(
67+
NAME test_jwt_es
68+
COMMAND ./test_jwt_es
69+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

0 commit comments

Comments
 (0)