Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / gil / CMakeLists.txt
1 #
2 # Copyright (c) 2017-2019 Mateusz Loskot <mateusz at loskot dot net>
3 #
4 # Distributed under the Boost Software License, Version 1.0.
5 # (See accompanying file LICENSE_1_0.txt or copy at
6 # http://www.boost.org/LICENSE_1_0.txt)
7 #
8 # **WARNING:**
9 # The CMake configuration is only provided for convenience
10 # of contributors. It does not export or install any targets,
11 # deploy config files or support subproject workflow.
12 #
13 cmake_minimum_required(VERSION 3.10)
14
15 #-----------------------------------------------------------------------------
16 # Options
17 #-----------------------------------------------------------------------------
18 option(GIL_BUILD_EXAMPLES "Build examples" ON)
19 option(GIL_BUILD_HEADER_TESTS "Enable self-contained header tests" ON)
20 option(GIL_ENABLE_EXT_DYNAMIC_IMAGE "Enable Dynamic Image extension, tests and examples" ON)
21 option(GIL_ENABLE_EXT_IO "Enable IO extension, tests and examples (require libjpeg, libpng, libtiff)" ON)
22 option(GIL_ENABLE_EXT_NUMERIC "Enable Numeric extension, tests and examples" ON)
23 option(GIL_ENABLE_EXT_TOOLBOX "Enable Toolbox extension, tests and examples" ON)
24 option(GIL_USE_CONAN "Use Conan to install dependencies" OFF)
25 option(GIL_USE_CLANG_TIDY "Set CMAKE_CXX_CLANG_TIDY property on targets to enable clang-tidy linting" OFF)
26 set(CMAKE_CXX_STANDARD 11 CACHE STRING "C++ standard version to use (default is 11)")
27
28 #-----------------------------------------------------------------------------
29 # Project
30 #-----------------------------------------------------------------------------
31 project(Boost.GIL
32   LANGUAGES CXX
33   DESCRIPTION "Boost.GIL - Generic Image Library | Requires C++11 since Boost 1.68")
34
35 list(INSERT CMAKE_MODULE_PATH 0 ${CMAKE_BINARY_DIR}/cmake)
36
37 #-----------------------------------------------------------------------------
38 # C++ language version and compilation flags
39 #-----------------------------------------------------------------------------
40 message(STATUS "Boost.GIL: Require C++${CMAKE_CXX_STANDARD}")
41 set(CMAKE_CXX_STANDARD_REQUIRED ON)
42 set(CMAKE_CXX_EXTENSIONS OFF)
43
44 add_library(gil_compile_options INTERFACE)
45
46 # See https://cmake.org/pipermail/cmake/2018-December/068716.html
47 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
48   string(REGEX REPLACE "/W3" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
49   string(REGEX REPLACE "-W3" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
50 endif()
51
52 # See https://svn.boost.org/trac10/wiki/Guidelines/WarningsGuidelines
53
54 target_compile_options(gil_compile_options
55   INTERFACE
56     $<$<CXX_COMPILER_ID:MSVC>:-W4>
57     $<$<CXX_COMPILER_ID:MSVC>:-bigobj>
58     $<$<CXX_COMPILER_ID:MSVC>:-FC> # Need absolute path for __FILE__ used in tests
59     $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-fstrict-aliasing -pedantic>)
60
61 # Do not mix warnings due to strict compilation level with linter warnings
62 if(NOT CMAKE_CXX_CLANG_TIDY)
63   target_compile_options(gil_compile_options
64     INTERFACE
65       $<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-Wall -Wconversion -Wextra -Wfloat-equal -Wshadow -Wsign-promo -Wstrict-aliasing -Wunused-parameter>)
66 endif()
67
68 target_compile_definitions(gil_compile_options
69   INTERFACE
70     $<$<CXX_COMPILER_ID:MSVC>:_CRT_NONSTDC_NO_DEPRECATE>
71     $<$<CXX_COMPILER_ID:MSVC>:_SCL_SECURE_NO_DEPRECATE>
72     $<$<CXX_COMPILER_ID:MSVC>:_CRT_SECURE_NO_WARNINGS>
73     $<$<CXX_COMPILER_ID:MSVC>:NOMINMAX>
74     $<$<CXX_COMPILER_ID:MSVC>:BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE>)
75
76 #-----------------------------------------------------------------------------
77 # Dependency target
78 #-----------------------------------------------------------------------------
79 add_library(gil_dependencies INTERFACE)
80
81 #-----------------------------------------------------------------------------
82 # Dependency: Boost
83 # - look for stage Build
84 # - look for default installation location
85 # - look for location specified with BOOST_ROOT
86 #-----------------------------------------------------------------------------
87 if(NOT DEFINED BOOST_ROOT AND NOT DEFINED ENV{BOOST_ROOT})
88   message(STATUS "Boost.GIL: Looking for Boost from current source tree and libraries from stage.")
89   message(STATUS "Boost.GIL: Disable stage look-up with passing -DBOOST_ROOT=/path/to/your/boost.")
90   get_filename_component(_boost_root ../../ ABSOLUTE)
91   if(EXISTS ${_boost_root}/boost-build.jam)
92     set(BOOST_ROOT ${_boost_root})
93     message(STATUS "Boost.GIL: Using Boost libraries from stage directory in BOOST_ROOT=${BOOST_ROOT}")
94   endif()
95 endif()
96
97 set(Boost_DETAILED_FAILURE_MSG ON)
98 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
99   set(Boost_USE_STATIC_LIBS ON)
100   set(Boost_USE_STATIC_RUNTIME OFF)
101 endif()
102
103 find_package(Boost 1.68.0 REQUIRED
104   COMPONENTS
105     filesystem
106     unit_test_framework)
107 message(STATUS "Boost.GIL: Using Boost_INCLUDE_DIRS=${Boost_INCLUDE_DIRS}")
108 message(STATUS "Boost.GIL: Using Boost_LIBRARY_DIRS=${Boost_LIBRARY_DIRS}")
109
110 target_link_libraries(gil_dependencies
111   INTERFACE
112     Boost::filesystem
113     Boost::unit_test_framework)
114
115 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
116   target_link_libraries(gil_dependencies INTERFACE Boost::disable_autolinking)
117 endif()
118
119 target_compile_definitions(gil_dependencies
120   INTERFACE
121     $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:BOOST_TEST_DYN_LINK>)
122
123 #-----------------------------------------------------------------------------
124 # Dependency: libpng, libjpeg, libtiff, libraw via Vcpkg or Conan
125 #-----------------------------------------------------------------------------
126 if(GIL_USE_CONAN)
127   # Download automatically, you can also just copy the conan.cmake file
128   if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
129     message(STATUS "Boost.GIL: Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
130     file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/v0.14/conan.cmake"
131       "${CMAKE_BINARY_DIR}/conan.cmake")
132   endif()
133
134   # NOTE: See RelWithDebInfo for Release builds, http://docs.conan.io/en/latest/howtos/vs2017_cmake.html
135   set(_build_type_saved ${CMAKE_BUILD_TYPE})
136   if(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
137     set(CMAKE_BUILD_TYPE "Release")
138   endif()
139
140   include(${CMAKE_BINARY_DIR}/conan.cmake)
141   conan_cmake_run(CONANFILE conanfile.txt BASIC_SETUP CMAKE_TARGETS BUILD missing)
142
143   set(CMAKE_BUILD_TYPE ${_build_type_saved})
144   unset(_build_type_saved)
145 endif()
146
147 if(GIL_ENABLE_EXT_IO)
148   if (GIL_USE_CONAN)
149     target_link_libraries(gil_dependencies
150       INTERFACE
151         CONAN_PKG::libjpeg
152         CONAN_PKG::libpng
153         CONAN_PKG::libtiff)
154   else()
155     find_package(JPEG REQUIRED)
156     find_package(PNG REQUIRED)
157     find_package(TIFF REQUIRED)
158     target_include_directories(gil_dependencies
159       INTERFACE
160         ${JPEG_INCLUDE_DIR})
161
162     target_link_libraries(gil_dependencies
163       INTERFACE
164         ${JPEG_LIBRARIES}
165         PNG::PNG
166         TIFF::TIFF)
167
168     if(UNIX)
169       # Typically, Linux packages provide C++ stream interface for TIFF
170       find_path(TIFFXX_INCLUDE_DIR NAMES tiffio.hxx)
171       find_library(TIFFXX_LIBRARY NAMES tiffxx)
172       target_include_directories(gil_dependencies INTERFACE ${TIFFXX_INCLUDE_DIR})
173       target_link_libraries(gil_dependencies INTERFACE ${TIFFXX_LIBRARY})
174     endif()
175
176     # LibRaw is optional, because it is not easy to install pre-built libraw on Windows and Mac OSX
177     if(NOT EXISTS "${CMAKE_BINARY_DIR}/cmake/FindLibRaw.cmake")
178       message(STATUS "Boost.GIL: Downloading FindLibRaw.cmake from https://github.com/LibRaw/LibRaw-cmake")
179       file(DOWNLOAD
180         "https://raw.githubusercontent.com/LibRaw/LibRaw-cmake/master/cmake/modules/FindLibRaw.cmake"
181         "${CMAKE_BINARY_DIR}/cmake/FindLibRaw.cmake")
182     endif()
183     find_package(LibRaw)
184     set(GIL_ENABLE_EXT_IO_RAW ${LibRaw_FOUND} CACHE BOOL "Enable IO RAW extension (requires libraw)" FORCE)
185     if(GIL_ENABLE_EXT_IO_RAW)
186       target_include_directories(gil_dependencies INTERFACE ${LibRaw_INCLUDE_DIR})
187       target_link_libraries(gil_dependencies INTERFACE ${LibRaw_LIBRARIES})
188       target_compile_definitions(gil_dependencies INTERFACE ${LibRaw_DEFINITIONS})
189     endif()
190   endif()
191 endif()
192
193 #-----------------------------------------------------------------------------
194 # clang-tidy
195 # - default checks specified in .clang-tidy configuration file
196 #-----------------------------------------------------------------------------
197 if(GIL_USE_CLANG_TIDY AND CMAKE_VERSION VERSION_GREATER_EQUAL 3.6)
198   find_program(_clang_tidy
199     NAMES clang-tidy-7 clang-tidy-6.0 clang-tidy-5.0 clang-tidy-4.0 clang-tidy
200     DOC "Path to clang-tidy executable")
201
202   if(_clang_tidy)
203     message(STATUS "Boost.GIL: Configuring ${_clang_tidy} to run linting analysis for targets")
204     set(CMAKE_CXX_CLANG_TIDY ${_clang_tidy})
205   endif()
206   unset(_clang_tidy)
207 endif()
208
209 #-----------------------------------------------------------------------------
210 # Common include directories
211 #
212 # The boostorg/gil repository includes must come first,
213 # before Boost includes from cloned Boost superproject or installed distribution.
214 # Otherwise IDEs may see the wrong file (ie. due to boost/ symlinks or
215 # GIL headers from installed Boost instead of this clone of boostog/gil).
216 #-----------------------------------------------------------------------------
217 add_library(gil_include_directories INTERFACE)
218 target_include_directories(gil_include_directories
219   BEFORE
220   INTERFACE
221     ${CMAKE_CURRENT_SOURCE_DIR}/include
222     ${CMAKE_CURRENT_SOURCE_DIR}/test)
223
224 #-----------------------------------------------------------------------------
225 # Tests
226 #-----------------------------------------------------------------------------
227 enable_testing()
228
229 # On CI services, test the self-contained headers on-demand only to avoid build timeouts.
230 # CI environment is common for Travis CI, AppVeyor, CircleCI, etc.
231 # On Boost regression builds, CMake does not run, but Boost.Build,
232 # so the header tests are not enabled there either.
233 if(DEFINED ENV{CI})
234   set(GIL_BUILD_HEADER_TESTS OFF)
235 endif()
236
237 add_subdirectory(test)
238
239 #-----------------------------------------------------------------------------
240 # Examples
241 #-----------------------------------------------------------------------------
242 if(GIL_BUILD_EXAMPLES AND GIL_ENABLE_EXT_IO)
243   add_subdirectory(example)
244 endif()