CMakeLists.txt 5.26 KB
Newer Older
1
2
3
# 3.5 is actually available almost everywhere, but this a good minimum
cmake_minimum_required(VERSION 3.4)
project(YAML_CPP VERSION 0.6.3 LANGUAGES CXX)
4

5
6
include(CMakePackageConfigHelpers)
include(CMakeDependentOption)
7
include(CheckCXXCompilerFlag)
8
9
include(GNUInstallDirs)
include(CTest)
10

11
find_program(YAML_CPP_CLANG_FORMAT_EXE NAMES clang-format)
12

13
option(YAML_CPP_BUILD_CONTRIB "Enable yaml-cpp contrib in library" ON)
Matthew Woehlke's avatar
Matthew Woehlke committed
14
option(YAML_CPP_BUILD_TOOLS "Enable parse tools" ON)
15
option(YAML_BUILD_SHARED_LIBS "Build yaml-cpp shared library" ${BUILD_SHARED_LIBS})
16

17
18
19
20
21
22
23
24
25
cmake_dependent_option(YAML_CPP_BUILD_TESTS
  "Enable yaml-cpp tests" ON
  "BUILD_TESTING;CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR" OFF)
cmake_dependent_option(YAML_CPP_INSTALL
  "Enable generation of yaml-cpp install targets" ON
  "CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR" OFF)
cmake_dependent_option(YAML_MSVC_SHARED_RT
  "MSVC: Build yaml-cpp with shared runtime libs (/MD)" ON
  "MSVC" OFF)
26

27
set(yaml-cpp-type STATIC)
28
set(yaml-cpp-label-postfix "static")
29
30
if (YAML_BUILD_SHARED_LIBS)
  set(yaml-cpp-type SHARED)
31
  set(yaml-cpp-label-postfix "shared")
32
endif()
33

34
35
36
set(build-shared $<BOOL:${YAML_BUILD_SHARED_LIBS}>)
set(build-windows-dll $<AND:$<BOOL:${CMAKE_HOST_WIN32}>,${build-shared}>)
set(not-msvc $<NOT:$<CXX_COMPILER_ID:MSVC>>)
37

38
39
40
if (NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
  set(CMAKE_MSVC_RUNTIME_LIBRARY
    MultiThreaded$<$<CONFIG:Debug>:Debug>$<${build-shared}:DLL>)
41
42
endif()

43
44
45
46
47
set(contrib-pattern "src/contrib/*.cpp")
set(src-pattern "src/*.cpp")
if (CMAKE_VERSION VERSION_GREATER 3.12)
  list(INSERT contrib-pattern 0 CONFIGURE_DEPENDS)
  list(INSERT src-pattern 0 CONFIGURE_DEPENDS)
48
49
endif()

50
51
file(GLOB yaml-cpp-contrib-sources ${contrib-pattern})
file(GLOB yaml-cpp-sources ${src-pattern})
52

53
set(msvc-rt $<TARGET_PROPERTY:MSVC_RUNTIME_LIBRARY>)
54

55
56
set(msvc-rt-mtd-static $<STREQUAL:${msvc-rt},MultiThreadedDebug>)
set(msvc-rt-mt-static $<STREQUAL:${msvc-rt},MultiThreaded>)
57

58
59
set(msvc-rt-mtd-dll $<STREQUAL:${msvc-rt},MultiThreadedDebugDLL>)
set(msvc-rt-mt-dll $<STREQUAL:${msvc-rt},MultiThreadedDLL>)
60

61
set(backport-msvc-runtime $<VERSION_LESS:${CMAKE_VERSION},3.15>)
62

63
add_library(yaml-cpp ${yaml-cpp-type} "")
64
add_library(yaml-cpp::yaml-cpp ALIAS yaml-cpp)
65

66
67
68
69
70
71
set_property(TARGET yaml-cpp
  PROPERTY
    MSVC_RUNTIME_LIBRARY ${CMAKE_MSVC_RUNTIME_LIBRARY})
set_property(TARGET yaml-cpp
  PROPERTY
    CXX_STANDARD_REQUIRED ON)
72

73
74
75
76
77
78
target_include_directories(yaml-cpp
  PUBLIC
    $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  PRIVATE
    $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>)
79

80
81
82
83
if (NOT DEFINED CMAKE_CXX_STANDARD)
  set_target_properties(yaml-cpp
    PROPERTIES
      CXX_STANDARD 11)
84
85
endif()

86
87
88
89
target_compile_options(yaml-cpp
  PRIVATE
    $<${not-msvc}:-Wall -Wextra -Wshadow -Weffc++ -Wno-long-long>
    $<${not-msvc}:-pedantic -pedantic-errors>
90

91
92
93
94
    $<$<AND:${backport-msvc-runtime},${msvc-rt-mtd-static}>:-MTd>
    $<$<AND:${backport-msvc-runtime},${msvc-rt-mt-static}>:-MT>
    $<$<AND:${backport-msvc-runtime},${msvc-rt-mtd-dll}>:-MDd>
    $<$<AND:${backport-msvc-runtime},${msvc-rt-mt-dll}>:-MD>
95

96
97
98
99
100
    # /wd4127 = disable warning C4127 "conditional expression is constant"
    # http://msdn.microsoft.com/en-us/library/6t66728h.aspx
    # /wd4355 = disable warning C4355 "'this' : used in base member initializer list
    # http://msdn.microsoft.com/en-us/library/3c594ae3.aspx
    $<$<CXX_COMPILER_ID:MSVC>:/W3 /wd4127 /wd4355>)
101

102
103
104
105
target_compile_definitions(yaml-cpp
  PRIVATE
    $<${build-windows-dll}:${PROJECT_NAME}_DLL>
    $<$<NOT:$<BOOL:${YAML_CPP_BUILD_CONTRIB}>>:YAML_CPP_NO_CONTRIB>)
106

107
108
109
110
target_sources(yaml-cpp
  PRIVATE
    $<$<BOOL:${YAML_CPP_BUILD_CONTRIB}>:${yaml-cpp-contrib-sources}>
    ${yaml-cpp-sources})
111

112
set_target_properties(yaml-cpp PROPERTIES
113
114
  VERSION "${PROJECT_VERSION}"
  SOVERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}"
115
  PROJECT_LABEL "yaml-cpp ${yaml-cpp-label-postfix}"
116
  DEBUG_POSTFIX d)
117

118
119
120
121
configure_package_config_file(
  "${PROJECT_SOURCE_DIR}/yaml-cpp-config.cmake.in"
  "${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake"
  INSTALL_DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/yaml-cpp")
122

123
124
125
write_basic_package_version_file(
  "${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake"
  COMPATIBILITY AnyNewerVersion)
126

127
configure_file(yaml-cpp.pc.in yaml-cpp.pc @ONLY)
128

129
if (YAML_CPP_INSTALL)
130
131
132
133
134
135
136
137
138
139
	install(TARGETS yaml-cpp
    EXPORT yaml-cpp-targets
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
	install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
		FILES_MATCHING PATTERN "*.h")
  install(EXPORT yaml-cpp-targets
    DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/yaml-cpp")
140
	install(FILES
141
		"${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake"
142
		"${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake"
143
144
145
    DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/yaml-cpp")
  install(FILES "${PROJECT_BINARY_DIR}/yaml-cpp.pc"
    DESTINATION ${CMAKE_INSTALL_DATADIR}/pkgconfig)
146
147
endif()

Matthew Woehlke's avatar
Matthew Woehlke committed
148
if(YAML_CPP_BUILD_TESTS)
149
	add_subdirectory(test)
Matthew Woehlke's avatar
Matthew Woehlke committed
150
endif()
151

Matthew Woehlke's avatar
Matthew Woehlke committed
152
if(YAML_CPP_BUILD_TOOLS)
153
154
	add_subdirectory(util)
endif()
155

156
157
158
159
160
161
if (YAML_CPP_CLANG_FORMAT_EXE)
  add_custom_target(format
    COMMAND clang-format --style=file -i $<TARGET_PROPERTY:yaml-cpp,SOURCES>
    COMMENT "Running clang-format"
    VERBATIM)
endif()