Skip to content

Commit

Permalink
Restructure project layout to support use via CMake add_subdirectory (#…
Browse files Browse the repository at this point in the history
…23)

* Restructure project layout
* build PRs on CI
  • Loading branch information
Johannes Wolf authored Jan 18, 2021
1 parent 09e3ade commit 6a4db60
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 46 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Build and test

on: [push]
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:

Expand Down Expand Up @@ -51,7 +55,7 @@ jobs:
- name: Generate gcovr report
if: matrix.config == 'Coverage'
run: gcovr -r . -f "keychain.*" -x -o coverage.xml
run: gcovr -r . -f "src/*" -f "include/*" -x -o coverage.xml
- name: Upload coverage to Codecov
if: matrix.config == 'Coverage'
uses: codecov/codecov-action@v1
Expand Down
106 changes: 63 additions & 43 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,71 +1,91 @@
cmake_minimum_required(VERSION 3.12.4)

set(PROJECT_NAME "keychain")
project(${PROJECT_NAME})
project(keychain)

option(BUILD_TESTS "Build tests for ${PROJECT_NAME}" OFF)

if (MSVC)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++11")
else (MSVC)
set (CMAKE_CXX_STANDARD 11)
endif (MSVC)

# enable compiler warnings
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -pedantic")
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W2 /WX")
endif ()
add_library(${PROJECT_NAME})
target_include_directories(${PROJECT_NAME}
PUBLIC
"include"
PRIVATE
"src"
"include/keychain")

if (WIN32)
add_definitions(-DKEYCHAIN_WINDOWS=1)
set_target_properties(${PROJECT_NAME}
PROPERTIES PUBLIC_HEADER "include/keychain/keychain.h")

target_compile_features(${PROJECT_NAME}
PUBLIC
cxx_std_11)

list(APPEND SOURCES "keychain_win.cpp")
target_compile_options(${PROJECT_NAME}
PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/W2 /WX>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Wextra -pedantic -Werror>)

if (WIN32)
target_compile_definitions(${PROJECT_NAME}
PUBLIC
-DKEYCHAIN_WINDOWS=1)

list(APPEND KEYCHAIN_LIBRARIES crypt32)
target_sources(${PROJECT_NAME}
PRIVATE
"src/keychain_win.cpp")

target_link_libraries(${PROJECT_NAME}
PRIVATE
crypt32)
elseif (APPLE)
add_definitions(-DKEYCHAIN_MACOS=1)
target_compile_definitions(${PROJECT_NAME}
PUBLIC
-DKEYCHAIN_MACOS=1)

list(APPEND SOURCES "keychain_mac.cpp")
target_sources(${PROJECT_NAME}
PRIVATE
"src/keychain_mac.cpp")

find_library(COREFOUNDATION_LIBRARY CoreFoundation REQUIRED)
list(APPEND KEYCHAIN_LIBRARIES ${COREFOUNDATION_LIBRARY})

find_library(SECURITY_LIBRARY Security REQUIRED)
list(APPEND KEYCHAIN_LIBRARIES ${SECURITY_LIBRARY})

else (WIN32) # assuming Linux
add_definitions(-DKEYCHAIN_LINUX=1)
target_link_libraries(${PROJECT_NAME}
PRIVATE
${COREFOUNDATION_LIBRARY}
${SECURITY_LIBRARY})
else () # assuming Linux
target_compile_definitions(${PROJECT_NAME}
PUBLIC
-DKEYCHAIN_LINUX=1)

list(APPEND SOURCES "keychain_linux.cpp")
target_sources(${PROJECT_NAME}
PRIVATE
"src/keychain_linux.cpp")

find_package(PkgConfig REQUIRED)
pkg_check_modules(KEYCHAIN REQUIRED glib-2.0 libsecret-1)

endif (WIN32)
pkg_check_modules(GLIB2 IMPORTED_TARGET glib-2.0)
pkg_check_modules(LIBSECRET IMPORTED_TARGET libsecret-1)

add_library(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PRIVATE ${KEYCHAIN_INCLUDE_DIRS})
set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER "keychain.h")
target_link_libraries(${PROJECT_NAME} PUBLIC ${KEYCHAIN_LIBRARIES})
target_link_libraries(${PROJECT_NAME}
PRIVATE
PkgConfig::GLIB2
PkgConfig::LIBSECRET)
endif ()

# Code Coverage Configuration
option(CODE_COVERAGE "Enable coverage reporting" OFF)
if(CODE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
if (CODE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
set(COVERAGE_COMPILE_FLAGS "--coverage -g -O0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILE_FLAGS}")
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} \"--coverage\"")
endif()
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILE_FLAGS}")
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
endif ()
endif ()

install(TARGETS ${PROJECT_NAME}
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
PUBLIC_HEADER DESTINATION include/keychain)
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
PUBLIC_HEADER DESTINATION include/keychain)

if (BUILD_TESTS)
add_subdirectory("test")
endif (BUILD_TESTS)
endif ()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ set(TEST_BINARY_NAME "${PROJECT_NAME}-test")

# see comment in test-main.cpp
add_library("catchmain" OBJECT "main.cpp")
target_compile_features(catchmain PUBLIC cxx_std_11)

add_executable(${TEST_BINARY_NAME} $<TARGET_OBJECTS:catchmain> "tests.cpp")
target_include_directories(${TEST_BINARY_NAME} PRIVATE ${PROJECT_SOURCE_DIR})
Expand Down
2 changes: 1 addition & 1 deletion test/tests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "catch.hpp"
#include "keychain.h"
#include "keychain/keychain.h"

using namespace keychain;

Expand Down

0 comments on commit 6a4db60

Please sign in to comment.