From 591052005ba3a92bcc944e45eadf86f0faec1626 Mon Sep 17 00:00:00 2001 From: ooxi Date: Wed, 27 Dec 2017 20:58:56 +0100 Subject: [PATCH] Rewrote build system to make use of modern CMake features Now using modern CMake features as discussed by Daniel Pfeifer on C++Now 2017. Moreover a full build environment is defined by the provided [mini-cross](https://github.com/ooxi/mini-cross) description. --- .travis.yml | 3 +- CMakeLists.txt | 72 ++++++++++++++++----------------- mc.yaml | 8 ++++ run-tests.sh | 5 --- src/xml.c | 4 ++ test/CMakeLists.txt | 98 +++++++++++++++++++++++++++++++++++++++++++++ test/test-xml-c.c | 1 + 7 files changed, 147 insertions(+), 44 deletions(-) create mode 100644 mc.yaml delete mode 100755 run-tests.sh create mode 100644 test/CMakeLists.txt diff --git a/.travis.yml b/.travis.yml index 591432a..9a63393 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,5 +9,6 @@ script: - mkdir build && cd build - cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=gcc -DXML_PARSER_VERBOSE=On .. - make -after_script: ../run-tests.sh +after_script: + - make test diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c195f0..1923726 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,55 +1,51 @@ # Project setup -PROJECT(xml) -SET(VERSION_MAJOR "0") -SET(VERSION_MINOR "1") -SET(VERSION_PATCH "4") -CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0 FATAL_ERROR) +project(xml C CXX) +set(VERSION_MAJOR "0") +set(VERSION_MINOR "2") +set(VERSION_PATCH "0") +cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) + + +# Define main library target +add_library(xml STATIC "") # Compiler setup -SET(CMAKE_C_FLAGS_DEBUG "-g -DDEBUG") -SET(CMAKE_C_FLAGS_RELEASE "-O2") +target_compile_options( + xml + PRIVATE + -std=c11 +) + # Options -OPTION(XML_PARSER_VERBOSE "Enable to be told everything the xml parser does" OFF) +option(XML_PARSER_VERBOSE "Enable to be told everything the xml parser does" OFF) -IF(XML_PARSER_VERBOSE) - ADD_DEFINITIONS(-DXML_PARSER_VERBOSE) -ENDIF(XML_PARSER_VERBOSE) +if(XML_PARSER_VERBOSE) + target_compile_definitions( + xml + PRIVATE + XML_PARSER_VERBOSE + ) +endif(XML_PARSER_VERBOSE) # Sources -SET(SOURCE_DIRECTORY src) -SET(TEST_SOURCE_DIRECTORY test) - - -# Build library -ADD_LIBRARY(xml STATIC - ${SOURCE_DIRECTORY}/xml.c +target_sources( + xml + PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/src/xml.c" ) -# Build unit cases -INCLUDE_DIRECTORIES(${SOURCE_DIRECTORY}) - -ADD_EXECUTABLE(test-xml-c - ${TEST_SOURCE_DIRECTORY}/test-xml-c -) -TARGET_LINK_LIBRARIES(test-xml-c xml) - -ADD_EXECUTABLE(test-xml-cpp - ${TEST_SOURCE_DIRECTORY}/test-xml-cpp -) -TARGET_LINK_LIBRARIES(test-xml-cpp xml) - -FILE( COPY ${TEST_SOURCE_DIRECTORY}/test.xml - DESTINATION ${PROJECT_BINARY_DIR} +target_include_directories( + xml + PUBLIC + "${CMAKE_CURRENT_LIST_DIR}/src/" ) -# Building example -ADD_EXECUTABLE(example - ${TEST_SOURCE_DIRECTORY}/example -) -TARGET_LINK_LIBRARIES(example xml) +# Build unit cases +enable_testing() +add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/test") diff --git a/mc.yaml b/mc.yaml new file mode 100644 index 0000000..5bd5ef7 --- /dev/null +++ b/mc.yaml @@ -0,0 +1,8 @@ +--- +base: ubuntu:16.04 +install: + - cmake + - g++ + - gcc + - valgrind +--- diff --git a/run-tests.sh b/run-tests.sh deleted file mode 100755 index 828b426..0000000 --- a/run-tests.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -./test-xml-c -./test-xml-cpp -valgrind --tool=memcheck --leak-check=full --track-origins=yes -v ./test-xml-c -valgrind --tool=memcheck --leak-check=full --track-origins=yes -v ./test-xml-cpp diff --git a/src/xml.c b/src/xml.c index 1e5c310..d26143b 100644 --- a/src/xml.c +++ b/src/xml.c @@ -20,6 +20,10 @@ * * 3. This notice may not be removed or altered from any source distribution. */ +#ifdef XML_PARSER_VERBOSE +#include +#endif + #include #include #include diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..071518b --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,98 @@ +# xml.c / test +cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) + + + +# Example +add_executable( + "${PROJECT_NAME}-example" + "${CMAKE_CURRENT_LIST_DIR}/example.c" +) + +target_compile_options( + "${PROJECT_NAME}-example" + PRIVATE + -std=c11 +) + +target_link_libraries( + "${PROJECT_NAME}-example" + PRIVATE + xml +) + +add_test( + NAME "${PROJECT_NAME}-example" + COMMAND "${PROJECT_NAME}-example" +) + + + +# Test case +FILE( COPY "${CMAKE_CURRENT_LIST_DIR}/test.xml" + DESTINATION "${CMAKE_CURRENT_BINARY_DIR}" +) + + + +# Test (C) +add_executable( + "${PROJECT_NAME}-test-c" + "${CMAKE_CURRENT_LIST_DIR}/test-xml-c.c" +) + +target_compile_options( + "${PROJECT_NAME}-test-c" + PRIVATE + -std=c11 +) + +target_link_libraries( + "${PROJECT_NAME}-test-c" + PRIVATE + xml +) + + +add_test( + NAME "${PROJECT_NAME}-test-c" + COMMAND "${PROJECT_NAME}-test-c" +) + +add_test( + NAME "${PROJECT_NAME}-test-c-valgrind" + COMMAND valgrind --tool=memcheck --leak-check=full --track-origins=yes -v "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-test-c" +) + + + +# Test (C++) +add_executable( + "${PROJECT_NAME}-test-cpp" + "${CMAKE_CURRENT_LIST_DIR}/test-xml-cpp.cpp" +) + +target_compile_options( + "${PROJECT_NAME}-test-cpp" + PRIVATE + -std=c++11 +) + +target_link_libraries( + "${PROJECT_NAME}-test-cpp" + PRIVATE + xml +) + + +add_test( + NAME "${PROJECT_NAME}-test-cpp" + COMMAND "${PROJECT_NAME}-test-cpp" +) + + +add_test( + NAME "${PROJECT_NAME}-test-cpp-valgrind" + COMMAND valgrind --tool=memcheck --leak-check=full --track-origins=yes -v "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-test-cpp" +) + diff --git a/test/test-xml-c.c b/test/test-xml-c.c index fc55573..7b0b8b8 100644 --- a/test/test-xml-c.c +++ b/test/test-xml-c.c @@ -20,6 +20,7 @@ * * 3. This notice may not be removed or altered from any source distribution. */ +#include #include #include #include