Skip to content

Commit

Permalink
cmake script lint
Browse files Browse the repository at this point in the history
  • Loading branch information
XUranus committed Nov 6, 2023
1 parent 9b125ed commit b9b7442
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 60 deletions.
70 changes: 35 additions & 35 deletions CMakeLists.txt
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()
2 changes: 0 additions & 2 deletions Json.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* Author: XUranus
* Date: 2022-11-21
* Description: a tiny C++ Json library
* https://github.com/XUranus/minicpp
*
================================================================*/

Expand Down Expand Up @@ -135,7 +134,6 @@ class MINIJSON_API JsonElement: public Serializable {
explicit JsonElement(char const *str);
JsonElement(const JsonObject& object);
JsonElement(const JsonArray& array);

JsonElement(const JsonElement& ele);
explicit JsonElement(JsonElement&& ele);
JsonElement& operator = (const JsonElement& ele);
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# MiniJson
> a mini Json library C++ implement
<div align="center">
<img src="https://github.com/XUranus/minijson/actions/workflows/cmake-multi-platform.yml/badge.svg" alt="minijson" title="minijson">&thinsp;
<img src="https://img.shields.io/badge/-C++11-3F63B3.svg?style=flat&logo=C%2B%2B&logoColor=white" alt="C++14" title="C++ Standards Used: C++11">&thinsp;
<img src="https://img.shields.io/badge/-Windows-6E46A2.svg?style=flat&logo=windows-11&logoColor=white" alt="Windows" title="Supported Platform: Windows">&thinsp;
<img src="https://img.shields.io/badge/-Linux-9C2A91.svg?style=flat&logo=linux&logoColor=white" alt="Linux" title="Supported Platform: Linux">&thinsp;
<img src="https://img.shields.io/badge/MSVC%202015+-flag.svg?color=555555&style=flat&logo=visual%20studio&logoColor=white" alt="MSVC 2015+" title="Supported Windows Compiler: MSVC 2015 or later">&thinsp;
<img src="https://img.shields.io/badge/GCC%204.9+-flag.svg?color=555555&style=flat&logo=gnu&logoColor=white" alt="GCC 4.9+" title="Supported Unix Compiler: GCC 4.9 or later">&thinsp;
</div>

## build & test
build static/dynamic library:
```
Expand Down
46 changes: 23 additions & 23 deletions test/CMakeLists.txt
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()

0 comments on commit b9b7442

Please sign in to comment.