Merge pull request #1463 from rouault/fix_570
[openjpeg.git] / CMakeLists.txt
1 # Main CMakeLists.txt to build the OpenJPEG project using CMake (www.cmake.org)
2 # Written by Mathieu Malaterre
3
4 # This CMake project will by default create a library called openjpeg
5 # But if you want to use this project within your own (CMake) project
6 # you will eventually like to prefix the library to avoid linking confusion
7 # For this purpose you can define a CMake var: OPENJPEG_NAMESPACE to whatever you like
8 # e.g.:
9 # set(OPENJPEG_NAMESPACE "GDCMOPENJPEG")
10 cmake_minimum_required(VERSION 2.8.5)
11
12 if(COMMAND CMAKE_POLICY)
13   cmake_policy(SET CMP0003 NEW)
14   if (NOT (${CMAKE_VERSION} VERSION_LESS 3.0))
15     cmake_policy(SET CMP0042 NEW)
16   endif()
17 endif()
18
19 if(NOT OPENJPEG_NAMESPACE)
20   set(OPENJPEG_NAMESPACE "OPENJPEG")
21   set(OPENJPEG_STANDALONE 1)
22 endif()
23 # In all cases:
24 #string(TOLOWER ${OPENJPEG_NAMESPACE} OPENJPEG_LIBRARY_NAME)
25 set(OPENJPEG_LIBRARY_NAME openjp2)
26
27 project(${OPENJPEG_NAMESPACE} C)
28
29 # Do full dependency headers.
30 include_regular_expression("^.*$")
31
32 #-----------------------------------------------------------------------------
33 # OPENJPEG version number, useful for packaging and doxygen doc:
34 set(OPENJPEG_VERSION_MAJOR 2)
35 set(OPENJPEG_VERSION_MINOR 5)
36 set(OPENJPEG_VERSION_BUILD 0)
37 set(OPENJPEG_VERSION
38   "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}")
39 set(PACKAGE_VERSION
40   "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}")
41
42 # Because autotools does not support X.Y notation for SOVERSION, we have to use
43 # two numbering, one for the openjpeg version and one for openjpeg soversion
44 # version | soversion
45 #   1.0   |  0
46 #   1.1   |  1
47 #   1.2   |  2
48 #   1.3   |  3
49 #   1.4   |  4
50 #   1.5   |  5
51 #   1.5.1 |  5
52 #   2.0   |  6
53 #   2.0.1 |  6
54 #   2.1   |  7
55 #   2.1.1 |  7
56 #   2.1.2 |  7
57 #   2.2.0 |  7
58 #   2.3.0 |  7
59 #   2.3.1 |  7
60 #   2.4.0 |  7
61 #   2.5.0 |  7
62 # above is the recommendation by the OPJ team. If you really need to override this default,
63 # you can specify your own OPENJPEG_SOVERSION at cmake configuration time:
64 # cmake -DOPENJPEG_SOVERSION:STRING=42 /path/to/openjpeg
65 if(NOT OPENJPEG_SOVERSION)
66   set(OPENJPEG_SOVERSION 7)
67 endif(NOT OPENJPEG_SOVERSION)
68 set(OPENJPEG_LIBRARY_PROPERTIES
69   VERSION   "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}"
70   SOVERSION "${OPENJPEG_SOVERSION}"
71 )
72
73 # --------------------------------------------------------------------------
74 # Path to additional CMake modules
75 set(CMAKE_MODULE_PATH
76     ${${OPENJPEG_NAMESPACE}_SOURCE_DIR}/cmake
77     ${CMAKE_MODULE_PATH})
78
79 # --------------------------------------------------------------------------
80 # On Visual Studio 8 MS deprecated C. This removes all 1.276E1265 security
81 # warnings
82 if(WIN32)
83   if(NOT BORLAND)
84     if(NOT CYGWIN)
85       if(NOT MINGW)
86         if(NOT ITK_ENABLE_VISUAL_STUDIO_DEPRECATED_C_WARNINGS)
87           add_definitions(
88             -D_CRT_FAR_MAPPINGS_NO_DEPRECATE
89             -D_CRT_IS_WCTYPE_NO_DEPRECATE
90             -D_CRT_MANAGED_FP_NO_DEPRECATE
91             -D_CRT_NONSTDC_NO_DEPRECATE
92             -D_CRT_SECURE_NO_DEPRECATE
93             -D_CRT_SECURE_NO_DEPRECATE_GLOBALS
94             -D_CRT_SETERRORMODE_BEEP_SLEEP_NO_DEPRECATE
95             -D_CRT_TIME_FUNCTIONS_NO_DEPRECATE
96             -D_CRT_VCCLRIT_NO_DEPRECATE
97             -D_SCL_SECURE_NO_DEPRECATE
98             )
99         endif()
100       endif()
101     endif()
102   endif()
103 endif()
104
105
106 # --------------------------------------------------------------------------
107 # Install directories
108 string(TOLOWER ${PROJECT_NAME} PROJECT_NAME)
109 include(GNUInstallDirs)
110
111 # Build DOCUMENTATION (not in ALL target and only if Doxygen is found)
112 option(BUILD_DOC "Build the HTML documentation (with doxygen if available)." OFF)
113
114 set(OPENJPEG_INSTALL_SUBDIR "${PROJECT_NAME}-${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}")
115
116 if(NOT OPENJPEG_INSTALL_JNI_DIR)
117   if(WIN32)
118     set(OPENJPEG_INSTALL_JNI_DIR ${CMAKE_INSTALL_BINDIR})
119   else()
120     set(OPENJPEG_INSTALL_JNI_DIR ${CMAKE_INSTALL_LIBDIR})
121   endif()
122 endif()
123
124 if(NOT OPENJPEG_INSTALL_PACKAGE_DIR)
125   set(OPENJPEG_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${OPENJPEG_INSTALL_SUBDIR}")
126 endif()
127
128 if (APPLE)
129     if (${CMAKE_VERSION} VERSION_LESS 3.0)
130         # For cmake >= 3.0, we turn on CMP0042 and
131         # https://cmake.org/cmake/help/v3.0/policy/CMP0042.html mentions
132         # "Projects wanting @rpath in a target’s install name may remove any setting of the INSTALL_NAME_DIR and CMAKE_INSTALL_NAME_DIR variables"
133         list(APPEND OPENJPEG_LIBRARY_PROPERTIES INSTALL_NAME_DIR "${CMAKE_INSTALL_FULL_LIBDIR}")
134     endif()
135         option(OPJ_USE_DSYMUTIL "Call dsymutil on binaries after build." OFF)
136 endif()
137
138 #-----------------------------------------------------------------------------
139 # Big endian test:
140 if (NOT EMSCRIPTEN)
141 include (${CMAKE_ROOT}/Modules/TestBigEndian.cmake)
142 TEST_BIG_ENDIAN(OPJ_BIG_ENDIAN)
143 endif()
144
145 #-----------------------------------------------------------------------------
146 # Setup file for setting custom ctest vars
147 configure_file(
148   ${${OPENJPEG_NAMESPACE}_SOURCE_DIR}/cmake/CTestCustom.cmake.in
149   ${${OPENJPEG_NAMESPACE}_BINARY_DIR}/CTestCustom.cmake
150   @ONLY
151   )
152
153 #-----------------------------------------------------------------------------
154 # OpenJPEG build configuration options.
155 option(BUILD_SHARED_LIBS "Build OpenJPEG shared library and link executables against it." ON)
156 option(BUILD_STATIC_LIBS "Build OpenJPEG static library." ON)
157 set (EXECUTABLE_OUTPUT_PATH ${OPENJPEG_BINARY_DIR}/bin CACHE PATH "Single output directory for building all executables.")
158 set (LIBRARY_OUTPUT_PATH ${OPENJPEG_BINARY_DIR}/bin CACHE PATH "Single output directory for building all libraries.")
159 mark_as_advanced(LIBRARY_OUTPUT_PATH EXECUTABLE_OUTPUT_PATH)
160
161 #-----------------------------------------------------------------------------
162 # configure name mangling to allow multiple libraries to coexist
163 # peacefully
164 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/openjpeg_mangle.h.in)
165 set(MANGLE_PREFIX ${OPENJPEG_LIBRARY_NAME})
166 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/openjpeg_mangle.h.in
167                ${CMAKE_CURRENT_BINARY_DIR}/openjpeg_mangle.h
168                @ONLY)
169 endif()
170
171 #-----------------------------------------------------------------------------
172 # Compiler specific flags:
173 if(CMAKE_COMPILER_IS_GNUCC)
174   # For all builds, make sure openjpeg is std99 compliant:
175   # set(CMAKE_C_FLAGS "-Wall -std=c99 ${CMAKE_C_FLAGS}") # FIXME: this setting prevented us from setting a coverage build.
176   # Do not use ffast-math for all build, it would produce incorrect results, only set for release:
177   set(OPENJPEG_LIBRARY_COMPILE_OPTIONS ${OPENJPEG_LIBRARY_COMPILE_OPTIONS} "$<$<CONFIG:Release>:-ffast-math>")
178   set(OPENJP2_COMPILE_OPTIONS ${OPENJP2_COMPILE_OPTIONS} "$<$<CONFIG:Release>:-ffast-math>" -Wall -Wextra -Wconversion -Wunused-parameter -Wdeclaration-after-statement -Werror=declaration-after-statement -Wstrict-prototypes -Werror=strict-prototypes -Wmissing-prototypes -Werror=missing-prototypes)
179 endif()
180
181 #-----------------------------------------------------------------------------
182 # opj_config.h generation (1/2)
183
184 # Check if some include files are provided by the system
185 include(EnsureFileInclude)
186 # These files are mandatory
187 ensure_file_include("string.h"   HAVE_STRING_H YES)
188 ensure_file_include("memory.h"   HAVE_MEMORY_H YES)
189 ensure_file_include("stdlib.h"   HAVE_STDLIB_H YES)
190 ensure_file_include("stdio.h"    HAVE_STDIO_H  YES)
191 ensure_file_include("math.h"     HAVE_MATH_H   YES)
192 ensure_file_include("float.h"    HAVE_FLOAT_H  YES)
193 ensure_file_include("time.h"     HAVE_TIME_H   YES)
194 ensure_file_include("stdarg.h"   HAVE_STDARG_H YES)
195 ensure_file_include("ctype.h"    HAVE_CTYPE_H  YES)
196 ensure_file_include("assert.h"   HAVE_ASSERT_H YES)
197
198 # For the following files, we provide an alternative, they are not mandatory
199 ensure_file_include("stdint.h"   OPJ_HAVE_STDINT_H   NO)
200 ensure_file_include("inttypes.h" OPJ_HAVE_INTTYPES_H NO)
201
202 # why check this one ? for openjpip ?
203 include (${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake)
204 CHECK_INCLUDE_FILE("strings.h"      HAVE_STRINGS_H)
205 CHECK_INCLUDE_FILE("sys/stat.h"     HAVE_SYS_STAT_H)
206 CHECK_INCLUDE_FILE("sys/types.h"    HAVE_SYS_TYPES_H)
207 CHECK_INCLUDE_FILE("unistd.h"       HAVE_UNISTD_H)
208
209 # Enable Large file support
210 include(TestLargeFiles)
211 OPJ_TEST_LARGE_FILES(OPJ_HAVE_LARGEFILES)
212
213 # Allocating Aligned Memory Blocks
214 include(CheckIncludeFiles)
215 check_include_files(malloc.h OPJ_HAVE_MALLOC_H)
216 include(CheckSymbolExists)
217 # _aligned_alloc https://msdn.microsoft.com/en-us/library/8z34s9c6.aspx
218 check_symbol_exists(_aligned_malloc malloc.h OPJ_HAVE__ALIGNED_MALLOC)
219 # posix_memalign (needs _POSIX_C_SOURCE >= 200112L on Linux)
220 set(CMAKE_REQUIRED_DEFINITIONS -D_POSIX_C_SOURCE=200112L)
221 check_symbol_exists(posix_memalign stdlib.h OPJ_HAVE_POSIX_MEMALIGN)
222 unset(CMAKE_REQUIRED_DEFINITIONS)
223 # memalign (obsolete)
224 check_symbol_exists(memalign malloc.h OPJ_HAVE_MEMALIGN)
225 #-----------------------------------------------------------------------------
226 # Build Library
227 if(BUILD_JPIP_SERVER)
228   find_package(CURL REQUIRED)
229   find_package(FCGI REQUIRED)
230   find_package(Threads REQUIRED)
231   if(NOT CMAKE_USE_PTHREADS_INIT)
232     message(FATAL_ERROR "Only pthread are supported")
233   endif()
234 endif()
235 add_subdirectory(src/lib)
236 option(BUILD_LUTS_GENERATOR "Build utility to generate t1_luts.h" OFF)
237 if(UNIX)
238 option(BUILD_UNIT_TESTS "Build unit tests (bench_dwt, test_sparse_array, etc..)" OFF)
239 endif()
240
241 #-----------------------------------------------------------------------------
242 # Build Applications
243 option(BUILD_CODEC "Build the CODEC executables" ON)
244 option(BUILD_JPIP "Build the JPIP library and executables." OFF)
245 if(BUILD_JPIP)
246   option(BUILD_JPIP_SERVER "Build the JPIP server." OFF)
247 endif()
248 option(BUILD_VIEWER "Build the OPJViewer executable (C++)" OFF)
249 option(BUILD_JAVA "Build the openjpeg jar (Java)" OFF)
250 mark_as_advanced(BUILD_VIEWER)
251 mark_as_advanced(BUILD_JAVA)
252
253 if(BUILD_CODEC)
254   # OFF: It will only build 3rd party libs if they are not found on the system
255   # ON: 3rd party libs will ALWAYS be build, and used
256   option(BUILD_THIRDPARTY "Build the thirdparty executables if it is needed" OFF)
257   add_subdirectory(thirdparty)
258   add_subdirectory(src/bin)
259 endif ()
260 add_subdirectory(wrapping)
261
262 #-----------------------------------------------------------------------------
263 # opj_config.h generation (2/2)
264 configure_file(
265  ${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjp2/opj_config.h.cmake.in
266  ${CMAKE_CURRENT_BINARY_DIR}/src/lib/openjp2/opj_config.h
267  @ONLY
268  )
269
270  configure_file(
271  ${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjp2/opj_config_private.h.cmake.in
272  ${CMAKE_CURRENT_BINARY_DIR}/src/lib/openjp2/opj_config_private.h
273  @ONLY
274  )
275
276 #-----------------------------------------------------------------------------
277 # build documentation in doc subdir:
278 if(BUILD_DOC)
279   add_subdirectory(doc)
280 endif()
281
282 #-----------------------------------------------------------------------------
283 # Build Testing
284 option(BUILD_TESTING "Build the tests." OFF)
285 if(BUILD_TESTING)
286   if(BUILD_CODEC)
287     enable_testing()
288     include(CTest)
289
290     # Search openjpeg data needed for the tests
291     # They could be found via git on the OpenJPEG GitHub code project
292     # git clone https://github.com/uclouvain/openjpeg-data.git
293     find_path(OPJ_DATA_ROOT README-OPJ-Data
294       PATHS $ENV{OPJ_DATA_ROOT} ${CMAKE_SOURCE_DIR}/../data
295       NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH
296       )
297
298     # Add repository where to find tests
299     add_subdirectory(tests)
300
301   else()
302     message(FATAL_ERROR "You need build codec to run the tests")
303   endif()
304 endif()
305
306 #-----------------------------------------------------------------------------
307 # install all targets referenced as OPENJPEGTargets (relocatable with CMake 3.0+)
308 install(EXPORT OpenJPEGTargets DESTINATION ${OPENJPEG_INSTALL_PACKAGE_DIR})
309 if (${CMAKE_VERSION} VERSION_LESS 3.0)
310   set(PACKAGE_INIT)
311   set(PACKAGE_CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_FULL_INCLUDEDIR})
312   configure_file( ${${OPENJPEG_NAMESPACE}_SOURCE_DIR}/cmake/OpenJPEGConfig.cmake.in
313     ${${OPENJPEG_NAMESPACE}_BINARY_DIR}/OpenJPEGConfig.cmake
314     @ONLY
315   )
316 else()
317   include(CMakePackageConfigHelpers)
318   configure_package_config_file(${CMAKE_CURRENT_LIST_DIR}/cmake/OpenJPEGConfig.cmake.in
319     ${OPENJPEG_BINARY_DIR}/OpenJPEGConfig.cmake
320     INSTALL_DESTINATION ${OPENJPEG_INSTALL_PACKAGE_DIR}
321     PATH_VARS CMAKE_INSTALL_INCLUDEDIR)
322 endif()
323
324 install( FILES ${OPENJPEG_BINARY_DIR}/OpenJPEGConfig.cmake
325   DESTINATION ${OPENJPEG_INSTALL_PACKAGE_DIR}
326 )
327
328 #-----------------------------------------------------------------------------
329
330 include (cmake/OpenJPEGCPack.cmake)
331
332 #-----------------------------------------------------------------------------
333 # pkgconfig support
334 # enabled by default on Unix or if using GCC, disabled by default on other platforms
335 if(UNIX OR CMAKE_COMPILER_IS_GNUCC)
336   option(BUILD_PKGCONFIG_FILES "Build and install pkg-config files" ON)
337 else()
338   option(BUILD_PKGCONFIG_FILES "Build and install pkg-config files" OFF)
339 endif()
340 if(BUILD_PKGCONFIG_FILES)
341   macro(set_variable_from_rel_or_absolute_path var root rel_or_abs_path)
342     if(IS_ABSOLUTE "${rel_or_abs_path}")
343       set(${var} "${rel_or_abs_path}")
344     else()
345       set(${var} "${root}/${rel_or_abs_path}")
346     endif()
347   endmacro()
348   set_variable_from_rel_or_absolute_path("bindir" "\\\${prefix}" "${CMAKE_INSTALL_BINDIR}")
349   set_variable_from_rel_or_absolute_path("mandir" "\\\${prefix}" "${CMAKE_INSTALL_MANDIR}")
350   set_variable_from_rel_or_absolute_path("docdir" "\\\${prefix}" "${CMAKE_INSTALL_DOCDIR}")
351   set_variable_from_rel_or_absolute_path("libdir" "\\\${prefix}" "${CMAKE_INSTALL_LIBDIR}")
352   set_variable_from_rel_or_absolute_path("includedir" "\\\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}/${OPENJPEG_INSTALL_SUBDIR}")
353
354   # install in lib and not share (CMAKE_INSTALL_LIBDIR takes care of it for multi-arch)
355   configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjp2/libopenjp2.pc.cmake.in
356     ${CMAKE_CURRENT_BINARY_DIR}/libopenjp2.pc @ONLY)
357   install( FILES  ${CMAKE_CURRENT_BINARY_DIR}/libopenjp2.pc DESTINATION
358     ${CMAKE_INSTALL_LIBDIR}/pkgconfig )
359 #
360   if(BUILD_JPIP)
361   configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjpip/libopenjpip.pc.cmake.in
362     ${CMAKE_CURRENT_BINARY_DIR}/libopenjpip.pc @ONLY)
363   install( FILES  ${CMAKE_CURRENT_BINARY_DIR}/libopenjpip.pc DESTINATION
364     ${CMAKE_INSTALL_LIBDIR}/pkgconfig )
365   endif()
366 endif()
367
368 #-----------------------------------------------------------------------------
369
370 # build our version of astyle
371 SET (WITH_ASTYLE FALSE CACHE BOOL "If you plan to contribute you should reindent with scripts/prepare-commit.sh (using 'our' astyle)")