Skip to content

Commit 5910520

Browse files
committed
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.
1 parent 16fc76f commit 5910520

File tree

7 files changed

+147
-44
lines changed

7 files changed

+147
-44
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ script:
99
- mkdir build && cd build
1010
- cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=gcc -DXML_PARSER_VERBOSE=On ..
1111
- make
12-
after_script: ../run-tests.sh
12+
after_script:
13+
- make test
1314

CMakeLists.txt

+34-38
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,51 @@
11
# Project setup
2-
PROJECT(xml)
3-
SET(VERSION_MAJOR "0")
4-
SET(VERSION_MINOR "1")
5-
SET(VERSION_PATCH "4")
6-
CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0 FATAL_ERROR)
2+
project(xml C CXX)
3+
set(VERSION_MAJOR "0")
4+
set(VERSION_MINOR "2")
5+
set(VERSION_PATCH "0")
6+
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
7+
8+
9+
# Define main library target
10+
add_library(xml STATIC "")
711

812

913
# Compiler setup
10-
SET(CMAKE_C_FLAGS_DEBUG "-g -DDEBUG")
11-
SET(CMAKE_C_FLAGS_RELEASE "-O2")
14+
target_compile_options(
15+
xml
16+
PRIVATE
17+
-std=c11
18+
)
19+
1220

1321
# Options
14-
OPTION(XML_PARSER_VERBOSE "Enable to be told everything the xml parser does" OFF)
22+
option(XML_PARSER_VERBOSE "Enable to be told everything the xml parser does" OFF)
1523

16-
IF(XML_PARSER_VERBOSE)
17-
ADD_DEFINITIONS(-DXML_PARSER_VERBOSE)
18-
ENDIF(XML_PARSER_VERBOSE)
24+
if(XML_PARSER_VERBOSE)
25+
target_compile_definitions(
26+
xml
27+
PRIVATE
28+
XML_PARSER_VERBOSE
29+
)
30+
endif(XML_PARSER_VERBOSE)
1931

2032

2133
# Sources
22-
SET(SOURCE_DIRECTORY src)
23-
SET(TEST_SOURCE_DIRECTORY test)
24-
25-
26-
# Build library
27-
ADD_LIBRARY(xml STATIC
28-
${SOURCE_DIRECTORY}/xml.c
34+
target_sources(
35+
xml
36+
PRIVATE
37+
"${CMAKE_CURRENT_LIST_DIR}/src/xml.c"
2938
)
3039

3140

32-
# Build unit cases
33-
INCLUDE_DIRECTORIES(${SOURCE_DIRECTORY})
34-
35-
ADD_EXECUTABLE(test-xml-c
36-
${TEST_SOURCE_DIRECTORY}/test-xml-c
37-
)
38-
TARGET_LINK_LIBRARIES(test-xml-c xml)
39-
40-
ADD_EXECUTABLE(test-xml-cpp
41-
${TEST_SOURCE_DIRECTORY}/test-xml-cpp
42-
)
43-
TARGET_LINK_LIBRARIES(test-xml-cpp xml)
44-
45-
FILE( COPY ${TEST_SOURCE_DIRECTORY}/test.xml
46-
DESTINATION ${PROJECT_BINARY_DIR}
41+
target_include_directories(
42+
xml
43+
PUBLIC
44+
"${CMAKE_CURRENT_LIST_DIR}/src/"
4745
)
4846

4947

50-
# Building example
51-
ADD_EXECUTABLE(example
52-
${TEST_SOURCE_DIRECTORY}/example
53-
)
54-
TARGET_LINK_LIBRARIES(example xml)
48+
# Build unit cases
49+
enable_testing()
50+
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/test")
5551

mc.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
base: ubuntu:16.04
3+
install:
4+
- cmake
5+
- g++
6+
- gcc
7+
- valgrind
8+
---

run-tests.sh

-5
This file was deleted.

src/xml.c

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
*
2121
* 3. This notice may not be removed or altered from any source distribution.
2222
*/
23+
#ifdef XML_PARSER_VERBOSE
24+
#include <alloca.h>
25+
#endif
26+
2327
#include <ctype.h>
2428
#include <malloc.h>
2529
#include <stdarg.h>

test/CMakeLists.txt

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# xml.c / test
2+
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
3+
4+
5+
6+
# Example
7+
add_executable(
8+
"${PROJECT_NAME}-example"
9+
"${CMAKE_CURRENT_LIST_DIR}/example.c"
10+
)
11+
12+
target_compile_options(
13+
"${PROJECT_NAME}-example"
14+
PRIVATE
15+
-std=c11
16+
)
17+
18+
target_link_libraries(
19+
"${PROJECT_NAME}-example"
20+
PRIVATE
21+
xml
22+
)
23+
24+
add_test(
25+
NAME "${PROJECT_NAME}-example"
26+
COMMAND "${PROJECT_NAME}-example"
27+
)
28+
29+
30+
31+
# Test case
32+
FILE( COPY "${CMAKE_CURRENT_LIST_DIR}/test.xml"
33+
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}"
34+
)
35+
36+
37+
38+
# Test (C)
39+
add_executable(
40+
"${PROJECT_NAME}-test-c"
41+
"${CMAKE_CURRENT_LIST_DIR}/test-xml-c.c"
42+
)
43+
44+
target_compile_options(
45+
"${PROJECT_NAME}-test-c"
46+
PRIVATE
47+
-std=c11
48+
)
49+
50+
target_link_libraries(
51+
"${PROJECT_NAME}-test-c"
52+
PRIVATE
53+
xml
54+
)
55+
56+
57+
add_test(
58+
NAME "${PROJECT_NAME}-test-c"
59+
COMMAND "${PROJECT_NAME}-test-c"
60+
)
61+
62+
add_test(
63+
NAME "${PROJECT_NAME}-test-c-valgrind"
64+
COMMAND valgrind --tool=memcheck --leak-check=full --track-origins=yes -v "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-test-c"
65+
)
66+
67+
68+
69+
# Test (C++)
70+
add_executable(
71+
"${PROJECT_NAME}-test-cpp"
72+
"${CMAKE_CURRENT_LIST_DIR}/test-xml-cpp.cpp"
73+
)
74+
75+
target_compile_options(
76+
"${PROJECT_NAME}-test-cpp"
77+
PRIVATE
78+
-std=c++11
79+
)
80+
81+
target_link_libraries(
82+
"${PROJECT_NAME}-test-cpp"
83+
PRIVATE
84+
xml
85+
)
86+
87+
88+
add_test(
89+
NAME "${PROJECT_NAME}-test-cpp"
90+
COMMAND "${PROJECT_NAME}-test-cpp"
91+
)
92+
93+
94+
add_test(
95+
NAME "${PROJECT_NAME}-test-cpp-valgrind"
96+
COMMAND valgrind --tool=memcheck --leak-check=full --track-origins=yes -v "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-test-cpp"
97+
)
98+

test/test-xml-c.c

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*
2121
* 3. This notice may not be removed or altered from any source distribution.
2222
*/
23+
#include <alloca.h>
2324
#include <stdbool.h>
2425
#include <stdio.h>
2526
#include <stdlib.h>

0 commit comments

Comments
 (0)