-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
67 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,50 @@ | ||
CMAKE_MINIMUM_REQUIRED(VERSION 3.14) | ||
SET(Project minijson) | ||
PROJECT(${Project} C CXX) | ||
|
||
SET(SOURCE_DIR ${${Project}_SOURCE_DIR}) | ||
SET(CMAKE_C_STANDARD 99) | ||
SET(CMAKE_CXX_STANDARD 11) | ||
SET(CMAKE_POSITION_INDEPENDENT_CODE on) | ||
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/") | ||
|
||
MESSAGE("Compiling minijson") | ||
MESSAGE(STATUS "Source directory is " ${SOURCE_DIR}) | ||
MESSAGE("Project = ${Project}") | ||
MESSAGE("CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}") | ||
MESSAGE("COVERAGE = ${COVERAGE}") | ||
cmake_minimum_required(VERSION 3.14) | ||
set(Project minijson) | ||
project(${Project} C CXX) | ||
|
||
set(SOURCE_DIR ${${Project}_SOURCE_DIR}) | ||
set(CMAKE_C_STANDARD 99) | ||
set(CMAKE_CXX_STANDARD 11) | ||
set(CMAKE_POSITION_INDEPENDENT_CODE on) | ||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/") | ||
|
||
message("Compiling minijson") | ||
message(STATUS "Source directory is " ${SOURCE_DIR}) | ||
message("Project = ${Project}") | ||
message("CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}") | ||
message("COVERAGE = ${COVERAGE}") | ||
|
||
# supress MSVC/GCC warnings | ||
IF(${CMAKE_HOST_WIN32}) | ||
SET(CMAKE_CXX_FLAGS_DEBUG "/MTd /Zi /Ob0 /Od /RTC1") | ||
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4251") | ||
ENDIF() | ||
if(${CMAKE_HOST_WIN32}) | ||
set(CMAKE_CXX_FLAGS_DEBUG "/MTd /Zi /Ob0 /Od /RTC1") | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4251") | ||
endif() | ||
|
||
# define headers and sources | ||
SET(Headers Json.h) | ||
SET(Sources Json.cpp) | ||
set(Headers Json.h) | ||
set(Sources Json.cpp) | ||
|
||
# build a dynamic library | ||
MESSAGE("Build dynamic library ${MINIJSON_DYNAMIC_LIBRARY_TARGET}") | ||
SET(MINIJSON_DYNAMIC_LIBRARY_TARGET ${Project}) | ||
ADD_LIBRARY(${MINIJSON_DYNAMIC_LIBRARY_TARGET} SHARED ${Sources} ${Headers}) | ||
message("Build dynamic library ${MINIJSON_DYNAMIC_LIBRARY_TARGET}") | ||
set(MINIJSON_DYNAMIC_LIBRARY_TARGET ${Project}) | ||
add_library(${MINIJSON_DYNAMIC_LIBRARY_TARGET} SHARED ${Sources} ${Headers}) | ||
# to generate export library when build dynamic library | ||
TARGET_COMPILE_DEFINITIONS(${MINIJSON_DYNAMIC_LIBRARY_TARGET} PRIVATE -DLIBRARY_EXPORT) | ||
target_compile_definitions(${MINIJSON_DYNAMIC_LIBRARY_TARGET} PRIVATE -DLIBRARY_EXPORT) | ||
|
||
# build a static library | ||
MESSAGE("Build static library ${MINIJSON_STATIC_LIBRARY_TARGET}") | ||
SET(MINIJSON_STATIC_LIBRARY_TARGET ${Project}_static) | ||
ADD_LIBRARY(${MINIJSON_STATIC_LIBRARY_TARGET} STATIC ${Sources} ${Headers}) | ||
message("Build static library ${MINIJSON_STATIC_LIBRARY_TARGET}") | ||
set(MINIJSON_STATIC_LIBRARY_TARGET ${Project}_static) | ||
add_library(${MINIJSON_STATIC_LIBRARY_TARGET} STATIC ${Sources} ${Headers}) | ||
|
||
# set -DCMAKE_BUILD_TYPE=Debug to enable LLT, set -DCOVERAGE=ON to enable code coverage | ||
IF (CMAKE_BUILD_TYPE STREQUAL "Debug") | ||
if (CMAKE_BUILD_TYPE STREQUAL "Debug") | ||
# these config must be put at the level of source code in order to append compile flags | ||
IF (${COVERAGE} STREQUAL "lcov" OR ${COVERAGE} STREQUAL "gcovr") | ||
MESSAGE("code coverage is enabled, COVERAGE = ${COVERAGE}, append coverage compiler flags") | ||
if (${COVERAGE} STREQUAL "lcov" OR ${COVERAGE} STREQUAL "gcovr") | ||
message("code coverage is enabled, COVERAGE = ${COVERAGE}, append coverage compiler flags") | ||
INCLUDE(${SOURCE_DIR}/test/CodeCoverage.cmake) | ||
APPEND_COVERAGE_COMPILER_FLAGS() | ||
ENDIF() | ||
endif() | ||
|
||
ENABLE_TESTING() | ||
ADD_SUBDIRECTORY("test") | ||
ENDIF() | ||
enable_testing() | ||
add_subdirectory("test") | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,60 @@ | ||
CMAKE_MINIMUM_REQUIRED(VERSION 3.14) | ||
SET(Project "minijson_test") | ||
SET(${Project} C CXX) | ||
cmake_minimum_required(VERSION 3.14) | ||
set(Project "minijson_test") | ||
set(${Project} C CXX) | ||
|
||
SET(Headers StructSample.h) | ||
SET(Sources MiniJsonTest.cpp) | ||
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) | ||
set(Headers StructSample.h) | ||
set(Sources MiniJsonTest.cpp) | ||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) | ||
|
||
# build test and run code coverage | ||
ENABLE_TESTING() | ||
enable_testing() | ||
|
||
# auto fetch googletest | ||
INCLUDE(FetchContent) | ||
include(FetchContent) | ||
FetchContent_Declare( | ||
googletest | ||
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip | ||
) | ||
|
||
# For Windows: Prevent overriding the parent project's compiler/linker settings | ||
SET(gtest_force_shared_crt ON CACHE BOOL "" FORCE) | ||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) | ||
FetchContent_MakeAvailable(googletest) | ||
|
||
ADD_EXECUTABLE(${Project} ${Sources} ${Headers}) | ||
add_executable(${Project} ${Sources} ${Headers}) | ||
|
||
TARGET_LINK_LIBRARIES(${Project} PUBLIC | ||
target_link_libraries(${Project} PUBLIC | ||
minijson_static | ||
GTest::gtest_main | ||
) | ||
|
||
ADD_TEST( | ||
add_test( | ||
NAME ${Project} | ||
COMMAND ${Project} | ||
) | ||
|
||
INCLUDE(GoogleTest) | ||
include(GoogleTest) | ||
gtest_discover_tests(${Project}) | ||
|
||
# configure code coverage | ||
IF (${COVERAGE} STREQUAL "lcov") | ||
MESSAGE("code coverage is enabled, using lcov") | ||
INCLUDE(CodeCoverage.cmake) | ||
if (${COVERAGE} STREQUAL "lcov") | ||
message("code coverage is enabled, using lcov") | ||
include(CodeCoverage.cmake) | ||
APPEND_COVERAGE_COMPILER_FLAGS() | ||
SETUP_TARGET_FOR_COVERAGE_LCOV( | ||
setUP_TARGET_FOR_COVERAGE_LCOV( | ||
NAME "minijson_coverage_test" | ||
EXECUTABLE make test | ||
EXCLUDE "${PROJECT_SOURCE_DIR}/test/*" "/usr/include/*" "*googlemock*" "*googletest*" | ||
) | ||
ENDIF() | ||
endif() | ||
|
||
IF (${COVERAGE} STREQUAL "gcovr") | ||
MESSAGE("code coverage is enabled, using gcovr") | ||
INCLUDE(CodeCoverage.cmake) | ||
if (${COVERAGE} STREQUAL "gcovr") | ||
message("code coverage is enabled, using gcovr") | ||
include(CodeCoverage.cmake) | ||
APPEND_COVERAGE_COMPILER_FLAGS() | ||
SETUP_TARGET_FOR_COVERAGE_GCOVR_HTML( | ||
setUP_TARGET_FOR_COVERAGE_GCOVR_HTML( | ||
NAME "minijson_coverage_test" | ||
EXECUTABLE make test | ||
BASE_DIRECTORY "${PROJECT_SOURCE_DIR}" | ||
EXCLUDE "${PROJECT_SOURCE_DIR}/test/*" "/usr/include/*" "*googlemock*" "*googletest*" "*build*" | ||
) | ||
ENDIF() | ||
endif() |