|
1 | | -# Minimal CMake project for building a static library under Windows. |
2 | 1 |
|
3 | | -cmake_minimum_required (VERSION 2.8) |
| 2 | +cmake_minimum_required(VERSION 3.0) |
4 | 3 | project (yaml C) |
5 | 4 |
|
6 | 5 | set (YAML_VERSION_MAJOR 0) |
7 | 6 | set (YAML_VERSION_MINOR 1) |
8 | 7 | set (YAML_VERSION_PATCH 7) |
9 | 8 | set (YAML_VERSION_STRING "${YAML_VERSION_MAJOR}.${YAML_VERSION_MINOR}.${YAML_VERSION_PATCH}") |
10 | 9 |
|
11 | | -file (GLOB SRC src/*.c) |
| 10 | +option(BUILD_SHARED_LIBS "Build libyaml as a shared library" OFF) |
12 | 11 |
|
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() |
16 | 18 |
|
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() |
19 | 34 |
|
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 | + ) |
23 | 48 |
|
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 | + ) |
27 | 54 |
|
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 | + ) |
0 commit comments