Skip to content

Commit fe3d086

Browse files
jcfrsigmavirus24
authored andcommitted
Improve CMake build system
New build options ----------------- * Add option BUILD_TESTING by default ON See https://cmake.org/cmake/help/v2.8.12/cmake.html#module:CTest * Simplify library type selection using standard option BUILD_SHARED_LIBS See https://cmake.org/cmake/help/v3.0/variable/BUILD_SHARED_LIBS.html yamlConfig.cmake ---------------- * Generate and install yamlConfig.cmake, yamlConfigVersion.cmake and yamlTargets.cmake * Bump CMake version and explicitly associate include dirs with targets See https://cmake.org/cmake/help/v3.0/manual/cmake-buildsystem.7.html#include-directories-and-usage-requirements * Ensure building against libyaml using "find_package(yaml)" uses expected compile options: Set HAVE_CONFIG_H as private compile option, YAML_DECLARE_STATIC as public Testing ------- * Build all examples from "tests" directory CMake Best practices -------------------- * configure "config.h" based on version info found in CMakeLists.txt * Ensure buildsystem re-generation listing sources (best-practice) It is not recommended to use GLOB to collect a list of source files from the source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate. See https://cmake.org/cmake/help/v3.8/command/file.html Compilation warnings -------------------- * Set _CRT_SECURE_NO_WARNINGS if building using VisualStudio This will avoid warnings like this one: ``` C:\projects\libyaml\tests\run-emitter.c(268): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. ``` Continuous Integration ---------------------- * travis: Install CMake >= 3.x using scikit-ci-addons * Add comments to appveyor.yml and run-tests.sh
1 parent 660242d commit fe3d086

12 files changed

Lines changed: 224 additions & 31 deletions

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,3 @@ tests/run-test-suite/src/libyaml-parser
6060
/tests/test-version
6161
/tests/test-version.log
6262
/tests/test-version.trs
63-
/win32/Makefile

.travis.yml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
os:
2-
- linux
3-
- osx
1+
2+
matrix:
3+
include:
4+
- os: linux
5+
sudo: required
6+
compiler: gcc
7+
- os: linux
8+
sudo: required
9+
compiler: clang
10+
- os: osx
11+
compiler: gcc
12+
- os: osx
13+
compiler: clang
414

515
language: c
616

7-
compiler:
8-
- clang
9-
- gcc
17+
before_install:
18+
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then pip install --user scikit-ci-addons==0.15.0; ci_addons travis/install_cmake 3.2.0; fi
1019

1120
script: tests/run-tests.sh

CMakeLists.txt

Lines changed: 147 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,160 @@
1-
# Minimal CMake project for building a static library under Windows.
21

3-
cmake_minimum_required (VERSION 2.8)
2+
cmake_minimum_required(VERSION 3.0)
43
project (yaml C)
54

65
set (YAML_VERSION_MAJOR 0)
76
set (YAML_VERSION_MINOR 1)
87
set (YAML_VERSION_PATCH 7)
98
set (YAML_VERSION_STRING "${YAML_VERSION_MAJOR}.${YAML_VERSION_MINOR}.${YAML_VERSION_PATCH}")
109

11-
file (GLOB SRC src/*.c)
10+
option(BUILD_SHARED_LIBS "Build libyaml as a shared library" OFF)
1211

13-
include_directories (include win32)
14-
add_library (yaml SHARED ${SRC})
15-
set_target_properties(yaml PROPERTIES COMPILE_FLAGS "-DYAML_DECLARE_EXPORT -DHAVE_CONFIG_H")
12+
#
13+
# Output directories for a build tree
14+
#
15+
if(NOT DEFINED CMAKE_RUNTIME_OUTPUT_DIRECTORY)
16+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
17+
endif()
1618

17-
add_library (yaml_static STATIC ${SRC})
18-
set_target_properties(yaml_static PROPERTIES COMPILE_FLAGS "-DYAML_DECLARE_STATIC -DHAVE_CONFIG_H")
19+
#
20+
# Install relative directories
21+
#
22+
if(NOT DEFINED INSTALL_LIB_DIR)
23+
set(INSTALL_LIB_DIR lib)
24+
endif()
25+
if(NOT DEFINED INSTALL_BIN_DIR)
26+
set(INSTALL_BIN_DIR bin)
27+
endif()
28+
if(NOT DEFINED INSTALL_INCLUDE_DIR)
29+
set(INSTALL_INCLUDE_DIR include)
30+
endif()
31+
if(NOT DEFINED INSTALL_CMAKE_DIR)
32+
set(INSTALL_CMAKE_DIR cmake)
33+
endif()
1934

20-
add_executable (test-version tests/test-version.c)
21-
target_link_libraries(test-version yaml)
22-
add_test(NAME version COMMAND test-version)
35+
#
36+
# Build library
37+
#
38+
set(SRCS
39+
src/api.c
40+
src/dumper.c
41+
src/emitter.c
42+
src/loader.c
43+
src/parser.c
44+
src/reader.c
45+
src/scanner.c
46+
src/writer.c
47+
)
2348

24-
add_executable (test-reader tests/test-reader.c)
25-
target_link_libraries(test-reader yaml)
26-
add_test(NAME reader COMMAND test-reader)
49+
set(config_h ${CMAKE_CURRENT_BINARY_DIR}/include/config.h)
50+
configure_file(
51+
cmake/config.h.in
52+
${config_h}
53+
)
2754

28-
enable_testing()
55+
add_library(yaml ${SRCS})
56+
57+
if(NOT BUILD_SHARED_LIBS)
58+
set_target_properties(yaml
59+
PROPERTIES OUTPUT_NAME yaml_static
60+
)
61+
endif()
62+
63+
set_target_properties(yaml
64+
PROPERTIES DEFINE_SYMBOL YAML_DECLARE_EXPORT
65+
)
66+
67+
target_compile_definitions(yaml
68+
PRIVATE HAVE_CONFIG_H
69+
PUBLIC
70+
$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:YAML_DECLARE_STATIC>
71+
$<$<BOOL:${MSVC}>:_CRT_SECURE_NO_WARNINGS>
72+
)
73+
74+
target_include_directories(yaml PUBLIC
75+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
76+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
77+
$<INSTALL_INTERFACE:${INSTALL_INCLUDE_DIR}>
78+
)
79+
80+
#
81+
# Install rules
82+
#
83+
install(
84+
FILES
85+
include/yaml.h
86+
${config_h}
87+
DESTINATION include COMPONENT Development
88+
)
89+
90+
install(
91+
TARGETS yaml
92+
EXPORT yamlTargets
93+
RUNTIME DESTINATION "${INSTALL_BIN_DIR}" COMPONENT Runtime
94+
LIBRARY DESTINATION "${INSTALL_LIB_DIR}" COMPONENT Development
95+
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}" COMPONENT Development
96+
)
97+
98+
#
99+
# Add tests
100+
#
101+
include(CTest) # This module defines BUILD_TESTING option
102+
if(BUILD_TESTING)
103+
add_subdirectory(tests)
104+
endif()
105+
106+
#
107+
# Generate 'yamlConfig.cmake', 'yamlConfigVersion.cmake' and 'yamlTargets.cmake'
108+
#
109+
include(CMakePackageConfigHelpers)
110+
111+
# Configure 'yamlConfig.cmake' for a build tree
112+
set(CONFIG_DIR_CONFIG ${PROJECT_BINARY_DIR})
113+
set(config_file ${PROJECT_BINARY_DIR}/yamlConfig.cmake)
114+
configure_package_config_file(
115+
yamlConfig.cmake.in
116+
${config_file}
117+
INSTALL_DESTINATION ${PROJECT_BINARY_DIR}
118+
PATH_VARS CONFIG_DIR_CONFIG
119+
NO_CHECK_REQUIRED_COMPONENTS_MACRO
120+
)
121+
122+
# Configure 'yamlTargets.cmake' for a build tree
123+
export(TARGETS yaml
124+
FILE ${PROJECT_BINARY_DIR}/yamlTargets.cmake
125+
)
126+
127+
# Configure and install 'yamlConfig.cmake' for an install tree
128+
set(CONFIG_DIR_CONFIG ${INSTALL_CMAKE_DIR})
129+
set(install_config_file ${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/yamlConfig.cmake )
130+
configure_package_config_file(
131+
yamlConfig.cmake.in
132+
${install_config_file}
133+
INSTALL_DESTINATION ${CMAKE_INSTALL_PREFIX}/${INSTALL_CMAKE_DIR}
134+
PATH_VARS CONFIG_DIR_CONFIG
135+
NO_CHECK_REQUIRED_COMPONENTS_MACRO
136+
)
137+
install(
138+
FILES ${install_config_file}
139+
DESTINATION ${INSTALL_CMAKE_DIR} COMPONENT Development
140+
)
141+
142+
# Configure and install 'yamlTargets.cmake' for an install tree
143+
install(EXPORT yamlTargets
144+
FILE yamlTargets.cmake
145+
DESTINATION ${INSTALL_CMAKE_DIR}
146+
COMPONENT Development
147+
)
148+
149+
# Configure 'yamlConfigVersion.cmake' for a build tree
150+
set(config_version_file ${PROJECT_BINARY_DIR}/yamlConfigVersion.cmake)
151+
write_basic_package_version_file(
152+
${config_version_file}
153+
VERSION ${YAML_VERSION_STRING}
154+
COMPATIBILITY AnyNewerVersion
155+
)
156+
# ... and install for an install tree
157+
install(
158+
FILES ${config_version_file}
159+
DESTINATION ${INSTALL_CMAKE_DIR} COMPONENT Development
160+
)

Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## Run `./bootstrap` to generate the "Makefile.in" files in this directory and
22
## the "$SUBDIRS" subdirectories.
33

4-
SUBDIRS = include src . tests win32
4+
SUBDIRS = include src . tests
55

66
EXTRA_DIST = README LICENSE CMakeLists.txt doc/doxygen.cfg
77

appveyor.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,21 @@ image:
55
- Visual Studio 2013
66

77
build_script:
8+
9+
#
10+
# CMake based in-source build and tests using Visual Studio
11+
#
12+
13+
# Use 32-bit default generator ("Visual Studio 12 2013" or "Visual Studio 14 2015")
814
- cmake .
915
- cmake --build . --config release --clean-first
1016
- ctest -C release
17+
18+
#
19+
# Autoconf based in-source build and tests under Cygwin using gcc
20+
#
21+
22+
# 32-bit
1123
- C:\cygwin\bin\sh -c "export PATH=/usr/bin:/usr/local/bin:$PATH && ./bootstrap && ./configure && make && make test && make distclean"
24+
# 64-bit
1225
- C:\cygwin64\bin\sh -c "export PATH=/usr/bin:/usr/local/bin:$PATH && ./bootstrap && ./configure && make && make test && make distclean"

cmake/config.h.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#define YAML_VERSION_MAJOR @YAML_VERSION_MAJOR@
2+
#define YAML_VERSION_MINOR @YAML_VERSION_MINOR@
3+
#define YAML_VERSION_PATCH @YAML_VERSION_PATCH@
4+
#define YAML_VERSION_STRING "@YAML_VERSION_STRING@"

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ AC_C_CONST
6767
AC_TYPE_SIZE_T
6868

6969
# Define Makefiles.
70-
AC_CONFIG_FILES([yaml-0.1.pc include/Makefile src/Makefile Makefile tests/Makefile win32/Makefile])
70+
AC_CONFIG_FILES([yaml-0.1.pc include/Makefile src/Makefile Makefile tests/Makefile])
7171

7272
# Generate the "configure" script.
7373
AC_OUTPUT

tests/CMakeLists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
function(add_yaml_executable name)
3+
add_executable(${name} ${name}.c)
4+
target_link_libraries(${name} yaml)
5+
endfunction()
6+
7+
foreach(name IN ITEMS
8+
example-deconstructor
9+
example-deconstructor-alt
10+
example-reformatter
11+
example-reformatter-alt
12+
run-dumper
13+
run-emitter
14+
run-loader
15+
run-parser
16+
run-scanner
17+
test-reader
18+
test-version
19+
)
20+
add_yaml_executable(${name})
21+
endforeach()
22+
23+
add_test(NAME version COMMAND test-version)
24+
add_test(NAME reader COMMAND test-reader)
25+

tests/run-tests.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
set -e
44

55
main() {
6+
# Autoconf based in-source build and tests
67
clean
78

89
./bootstrap
910
./configure
1011
make test-all
1112

13+
# CMake based in-source build and tests
1214
clean
1315

1416
cmake .

win32/Makefile.am

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)