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

Add cmake build files to cjose. #30

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
209 changes: 209 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
cmake_minimum_required (VERSION 3.0.2)
project (cjose)

include(CheckSymbolExists)
include(CheckIncludeFile)
include(CheckTypeSize)

option(ENABLE_Tests "Turn on Tests" ON)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

enable compiling tests by default.

option(CJOSE_BUILD_SHARED_LIBS "Build shared libraries." OFF)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMakeModules")

# Build Release by default
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Build Release by default.

else()
endif()

# Setup cjose version
set(CJOSE_MAJOR_VERSION 0)
set(CJOSE_MINOR_VERSION 2)
set(CJOSE_PATCH_VERSION 0)
set(PACKAGE_VERSION ${CJOSE_MAJOR_VERSION}.${CJOSE_MINOR_VERSION}.${CJOSE_PATCH_VERSION})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here we set a version of cjose.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Set cjose version.


# Set output directories
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)

# Create version.h file
configure_file (
"${PROJECT_SOURCE_DIR}/include/cjose/version.h.in"
"${PROJECT_BINARY_DIR}/include/cjose/version.h"
)

# Set include directories
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/src/include
${CMAKE_CURRENT_SOURCE_DIR}/src
)

# Find dependencies
# Look for Homebrew version of OpenSSL
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add priority to homebrew versions of openssl, jansson and check. We can always specify custom locations of those packages by passing OPENSSL_ROOT_DIR, JANSSON_ROOT_DIR and CHECK_ROOT_DIR to our cmake invocation.

if ((NOT OPENSSL_INCLUDE_DIR) OR (NOT OPENSSL_LIBRARIES))
file(GLOB HB_OPENSSL_ROOT_DIRECTORIES /usr/local/Cellar/openssl/*)
list(GET HB_OPENSSL_ROOT_DIRECTORIES 0 HB_OPENSSL_ROOT_DIR)
if(HB_OPENSSL_ROOT_DIRECTORIES AND NOT OPENSSL_ROOT_DIR)
SET(OPENSSL_ROOT_DIR ${HB_OPENSSL_ROOT_DIR})
endif(HB_OPENSSL_ROOT_DIRECTORIES AND NOT OPENSSL_ROOT_DIR)
endif((NOT OPENSSL_INCLUDE_DIR) OR (NOT OPENSSL_LIBRARIES))
endif(CMAKE_C_COMPILER_ID MATCHES "AppleClang")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We prefer homebrew version of openssl.


find_package(OpenSSL 1.0.1 REQUIRED)
find_package(Jansson 2.3 REQUIRED)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Look for jansson

find_package(Check 0.9.4)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

look for check


message(STATUS "Looking for rt")
find_library(RT NAMES rt)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Look for librt

if (${RT} STREQUAL RT-NOTFOUND)
message(STATUS "Looking for rt - not found")
unset(${RT})
endif(${RT} STREQUAL RT-NOTFOUND)

message(STATUS "Looking for m")
find_library(M NAMES m)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

look for libm

if (${M} STREQUAL M-NOTFOUND)
message(STATUS "Looking for m - not found")
unset(${M})
endif(${M} STREQUAL M-NOTFOUND)

check_type_size(ssize_t SSIZE_T)

# Enable test if possible
if (NOT CHECK_FOUND OR ENABLE_Tests==OFF)
SET(ENABLE_Tests OFF)
endif(NOT CHECK_FOUND OR ENABLE_Tests==OFF)
message(STATUS "Tests Enabled............${ENABLE_Tests}")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Disable tests if check library not found.


CHECK_SYMBOL_EXISTS("random" stdlib.h HAVE_RANDOM)

# Set dependency include directories
include_directories(
${OPENSSL_INCLUDE_DIR}
${JANSSON_INCLUDE_DIRS}
)

#Generate config file
configure_file (
"${PROJECT_SOURCE_DIR}/include/cjose/config.h.in"
"${PROJECT_BINARY_DIR}/include/cjose/cjose_config.h"
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

generate configuration files.


# Setup compilation flags for different compilers
if (MSVC)
add_compile_options(/W2 /nologo /WX-)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

compiler options for visual studio.

add_definitions( "/DVERSION=\"${PACKAGE_VERSION}\"")
add_definitions( "/D_CRT_SECURE_NO_WARNINGS")
set(DEFAULT_COMPILER_OPTIONS /Zi /DEBUG $<$<CONFIG:Debug>:/Od> $<$<CONFIG:Release>:/O2> /MP )
set(LIB_COMPILER_OPTIONS ${DEFAULT_COMPILER_OPTIONS} /WX)
if (STATIC_CRT)
SET(LIB_COMPILER_OPTIONS ${LIB_COMPILER_OPTIONS} $<$<CONFIG:Debug>:/MTd> $<$<CONFIG:Release>:/MT>)
else()
SET(LIB_COMPILER_OPTIONS ${LIB_COMPILER_OPTIONS} $<$<CONFIG:Debug>:/MDd> $<$<CONFIG:Release>:/MD>)
endif()
elseif(CMAKE_COMPILER_IS_GNUCC)
add_definitions( "-DVERSION=\"${PACKAGE_VERSION}\"")
set(DEFAULT_COMPILER_OPTIONS -std=gnu99 -g $<$<CONFIG:Release>:-O2>)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

compiler options for gcc compiler

set(LIB_COMPILER_OPTIONS ${DEFAULT_COMPILER_OPTIONS} --pedantic -Wall -Werror)
else(CMAKE_C_COMPILER_ID MATCHES "AppleClang")
add_definitions( "-DVERSION=\"${PACKAGE_VERSION}\"")
set(DEFAULT_COMPILER_OPTIONS -std=gnu99 -g $<$<CONFIG:Release>:-O2>)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

compiler options for mac

set(LIB_COMPILER_OPTIONS ${DEFAULT_COMPILER_OPTIONS} --pedantic -Wall -Werror)
endif(MSVC)


file(GLOB CJOSE_SRC ${PROJECT_SOURCE_DIR}/src/*.c)
file(GLOB CJOSE_HDR_PUBLIC ${PROJECT_SOURCE_DIR}/include/cjose/*.h)
file(GLOB CJOSE_HDR_PRIVATE ${PROJECT_SOURCE_DIR}/src/include/*.h)

source_group("Library Sources" FILES ${CJOSE_SRC})
source_group("Library Private Headers" FILES ${CJOSE_HDR_PRIVATE})
source_group("Library Public Headers" FILES ${CJOSE_HDR_PUBLIC})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is for solution build systems like VisualStudio or xcode. This will create named filters instead of default ones.


if (CJOSE_BUILD_SHARED_LIBS)
add_library(cjose SHARED
${CJOSE_SRC}
${CJOSE_HDR_PRIVATE}
${CJOSE_HDR_PUBLIC}
)
else()
add_library(cjose
${CJOSE_SRC}
${CJOSE_HDR_PRIVATE}
${CJOSE_HDR_PUBLIC}
)
endif()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Create dynamic or static lib. Depending on CJOSE_BUILD_SHARED_LIBS.


target_link_libraries(cjose ${JANSSON_LIBRARIES} ${OPENSSL_LIBRARIES} ${MLIB} ${RTLIB})
target_compile_options(cjose PRIVATE ${LIB_COMPILER_OPTIONS})

if (ENABLE_Tests)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Compile tests if enabled.

file(GLOB CJOSE_TESTS_SRC ${PROJECT_SOURCE_DIR}/test/*.c)
file(GLOB CJOSE_TESTS_HDR ${PROJECT_SOURCE_DIR}/test/*.h)
include_directories(${CHECK_INCLUDE_DIRS})
source_group("Test Files " FILES ${CJOSE_TESTS_SRC} ${CJOSE_TESTS_HDR})

add_executable(tests ${CJOSE_TESTS_SRC} ${CJOSE_TESTS_HDR})
target_compile_options(tests PRIVATE ${DEFAULT_COMPILER_OPTIONS})
target_link_libraries(tests cjose ${JANSSON_LIBRARIES} ${OPENSSL_LIBRARIES} ${CHECK_LIBRARIES})
endif(ENABLE_Tests)

#install

# Allow the user to override installation directories.
set(CJOSE_INSTALL_LIB_DIR lib CACHE PATH "Installation directory for libraries")
set(CJOSE_INSTALL_BIN_DIR bin CACHE PATH "Installation directory for executables")
set(CJOSE_INSTALL_INCLUDE_DIR include CACHE PATH "Installation directory for header files")


if(WIN32 AND NOT CYGWIN)
set(DEF_INSTALL_CMAKE_DIR cmake)
else()
set(DEF_INSTALL_CMAKE_DIR lib/cmake/cjose)
endif()

set(CJOSE_INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH "Installation directory for CMake files")

# Create pkg-conf file.
# (We use the same files as ./configure does, so we
# have to defined the same variables used there).
set(prefix ${CMAKE_INSTALL_PREFIX})
set(exec_prefix ${CMAKE_INSTALL_PREFIX})
set(libdir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
set(includedir ${CMAKE_INSTALL_PREFIX}/${CJOSE_INSTALL_INCLUDE_DIR})
set(VERSION ${PACKAGE_VERSION})
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cjose.pc.in
${CMAKE_CURRENT_BINARY_DIR}/cjose.pc @ONLY)

# Make sure the paths are absolute.
foreach(p LIB BIN INCLUDE CMAKE)
set(var CJOSE_INSTALL_${p}_DIR)
if(NOT IS_ABSOLUTE "${${var}}")
set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
endif()
endforeach()

set_target_properties(cjose PROPERTIES PUBLIC_HEADER "${CJOSE_HDR_PUBLIC}")


# Install the pkg-config.
install (FILES
${CMAKE_CURRENT_BINARY_DIR}/cjose.pc
DESTINATION ${CJOSE_INSTALL_LIB_DIR}/pkgconfig COMPONENT dev)

install(TARGETS cjose
EXPORT CjoseTargets
LIBRARY DESTINATION "${CJOSE_INSTALL_LIB_DIR}" COMPONENT lib
ARCHIVE DESTINATION "${CJOSE_INSTALL_LIB_DIR}" COMPONENT lib
RUNTIME DESTINATION "${CJOSE_INSTALL_BIN_DIR}" COMPONENT lib # Windows DLLs
PUBLIC_HEADER DESTINATION "${CJOSE_INSTALL_INCLUDE_DIR}/cjose" COMPONENT dev)

# Install exports for the install-tree.
install(EXPORT CjoseTargets
DESTINATION "${CJOSE_INSTALL_CMAKE_DIR}" COMPONENT dev)

# For use when simply using add_library from a parent project to build cjose.
set(CJOSE_LIBRARIES cjose CACHE STRING "Cjose libraries")
74 changes: 74 additions & 0 deletions CMakeModules/FindCheck.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
if (CHECK_LIBRARIES AND CHECK_INCLUDE_DIRS)
set(CHECK_FOUND TRUE)
else (CHECK_LIBRARIES AND CHECK_INCLUDE_DIRS)
find_path(CHECK_INCLUDE_DIR
NAMES
check.h
PATHS
${CHECK_ROOT_DIR}/include
NO_DEFAULT_PATH
)

find_path(CHECK_INCLUDE_DIR
NAMES
check.h
)

find_library(CHECK_LIBRARY_CHECK
NAMES
check
compat
PATHS
${CHECK_ROOT_DIR}/lib
NO_DEFAULT_PATH
)

find_library(CHECK_LIBRARY_CHECK
NAMES
check
compat
)

find_library(CHECK_LIBRARY_COMPAT
NAMES
compat
PATHS
${CHECK_ROOT_DIR}/lib
NO_DEFAULT_PATH
)

find_library(CHECK_LIBRARY_COMPAT
NAMES
compat
)

if (CHECK_INCLUDE_DIR)
set(CHECK_INCLUDE_DIRS
${CHECK_INCLUDE_DIRS}
${CHECK_INCLUDE_DIR}
)
endif(CHECK_INCLUDE_DIR)

if (CHECK_LIBRARY_CHECK)
set(CHECK_LIBRARIES
${CHECK_LIBRARIES}
${CHECK_LIBRARY_CHECK}
)
endif (CHECK_LIBRARY_CHECK)

if (CHECK_LIBRARY_COMPAT)
set(CHECK_LIBRARIES
${CHECK_LIBRARIES}
${CHECK_LIBRARY_COMPAT}
)
endif (CHECK_LIBRARY_COMPAT)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Check DEFAULT_MSG
CHECK_LIBRARIES CHECK_INCLUDE_DIRS)

mark_as_advanced(CHECK_INCLUDE_DIRS CHECK_LIBRARIES)

endif (CHECK_LIBRARIES AND CHECK_INCLUDE_DIRS)


70 changes: 70 additions & 0 deletions CMakeModules/FindJansson.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
if (JANSSON_LIBRARIES AND JANSSON_INCLUDE_DIRS)
set(JANSSON_FOUND TRUE)
else (JANSSON_LIBRARIES AND JANSSON_INCLUDE_DIRS)
find_path(JANSSON_INCLUDE_DIR
NAMES
jansson.h
PATHS
${JANSSON_ROOT_DIR}/include
NO_DEFAULT_PATH
)

find_path(JANSSON_INCLUDE_DIR
NAMES
jansson.h
)

find_library(JANSSON_LIBRARY_RELEASE
NAMES
jansson
PATHS
${JANSSON_ROOT_DIR}/lib
NO_DEFAULT_PATH
)

find_library(JANSSON_LIBRARY_RELEASE
NAMES
jansson
)

find_library(JANSSON_LIBRARY_DEBUG
NAMES
jansson_d
PATHS
${JANSSON_ROOT_DIR}/lib
NO_DEFAULT_PATH
)

find_library(JANSSON_LIBRARY_DEBUG
NAMES
jansson_d
)

set(JANSSON_INCLUDE_DIRS
${JANSSON_INCLUDE_DIR}
)

if (JANSSON_LIBRARY_DEBUG)
set(JANSSON_LIBRARIES
${JANSSON_LIBRARIES}
debug ${JANSSON_LIBRARY_DEBUG}
)
endif (JANSSON_LIBRARY_DEBUG)

if (JANSSON_LIBRARY_RELEASE)
if (WIN32)
set(JANSSON_LIBRARIES ${JANSSON_LIBRARIES} optimized ${JANSSON_LIBRARY_RELEASE})
else()
set(JANSSON_LIBRARIES ${JANSSON_LIBRARIES} general ${JANSSON_LIBRARY_RELEASE})
endif(WIN32)
endif (JANSSON_LIBRARY_RELEASE)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Jansson DEFAULT_MSG
JANSSON_LIBRARIES JANSSON_INCLUDE_DIRS)

mark_as_advanced(JANSSON_INCLUDE_DIRS JANSSON_LIBRARIES)

endif (JANSSON_LIBRARIES AND JANSSON_INCLUDE_DIRS)


8 changes: 4 additions & 4 deletions include/cjose/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extern "C"
* \param err [out] An optional error object which can be used to get additional
* information in the event of an error.
*/
bool cjose_base64_encode(const uint8_t *input, const size_t inlen, char **output, size_t *outlen, cjose_err *err);
bool cjose_base64_encode(const uint8_t *input, size_t inlen, char **output, size_t *outlen, cjose_err *err);
/**
* Encodes the given octet string to URL-safe Base64.
*
Expand All @@ -49,7 +49,7 @@ bool cjose_base64_encode(const uint8_t *input, const size_t inlen, char **output
* \param err [out] An optional error object which can be used to get additional
* information in the event of an error.
*/
bool cjose_base64url_encode(const uint8_t *input, const size_t inlen, char **output, size_t *outlen, cjose_err *err);
bool cjose_base64url_encode(const uint8_t *input, size_t inlen, char **output, size_t *outlen, cjose_err *err);

/**
* Decodes the given string from Base64.
Expand All @@ -63,7 +63,7 @@ bool cjose_base64url_encode(const uint8_t *input, const size_t inlen, char **out
* \param err [out] An optional error object which can be used to get additional
* information in the event of an error.
*/
bool cjose_base64_decode(const char *input, const size_t inlen, uint8_t **output, size_t *outlen, cjose_err *err);
bool cjose_base64_decode(const char *input, size_t inlen, uint8_t **output, size_t *outlen, cjose_err *err);
/**
* Decodes the given string from URL-Safe Base64.
*
Expand All @@ -76,7 +76,7 @@ bool cjose_base64_decode(const char *input, const size_t inlen, uint8_t **output
* \param err [out] An optional error object which can be used to get additional
* information in the event of an error.
*/
bool cjose_base64url_decode(const char *input, const size_t inlen, uint8_t **output, size_t *outlen, cjose_err *err);
bool cjose_base64url_decode(const char *input, size_t inlen, uint8_t **output, size_t *outlen, cjose_err *err);


#ifdef __cplusplus
Expand Down
Loading