Merge pull request #800 from rouault/tier1_optimizations_multithreading_pterm_check
[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.2)
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})
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 2)
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 |  8
58 # above is the recommendation by the OPJ team. If you really need to override this default,
59 # you can specify your own OPENJPEG_SOVERSION at cmake configuration time:
60 # cmake -DOPENJPEG_SOVERSION:STRING=42 /path/to/openjpeg
61 if(NOT OPENJPEG_SOVERSION)
62   set(OPENJPEG_SOVERSION 7)
63 endif(NOT OPENJPEG_SOVERSION)
64 set(OPENJPEG_LIBRARY_PROPERTIES
65   VERSION   "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}"
66   SOVERSION "${OPENJPEG_SOVERSION}"
67 )
68
69 # --------------------------------------------------------------------------
70 # Path to additional CMake modules
71 set(CMAKE_MODULE_PATH
72     ${${OPENJPEG_NAMESPACE}_SOURCE_DIR}/cmake
73     ${CMAKE_MODULE_PATH})
74
75 # --------------------------------------------------------------------------
76 # On Visual Studio 8 MS deprecated C. This removes all 1.276E1265 security
77 # warnings
78 if(WIN32)
79   if(NOT BORLAND)
80     if(NOT CYGWIN)
81       if(NOT MINGW)
82         if(NOT ITK_ENABLE_VISUAL_STUDIO_DEPRECATED_C_WARNINGS)
83           add_definitions(
84             -D_CRT_FAR_MAPPINGS_NO_DEPRECATE
85             -D_CRT_IS_WCTYPE_NO_DEPRECATE
86             -D_CRT_MANAGED_FP_NO_DEPRECATE
87             -D_CRT_NONSTDC_NO_DEPRECATE
88             -D_CRT_SECURE_NO_DEPRECATE
89             -D_CRT_SECURE_NO_DEPRECATE_GLOBALS
90             -D_CRT_SETERRORMODE_BEEP_SLEEP_NO_DEPRECATE
91             -D_CRT_TIME_FUNCTIONS_NO_DEPRECATE
92             -D_CRT_VCCLRIT_NO_DEPRECATE
93             -D_SCL_SECURE_NO_DEPRECATE
94             )
95         endif()
96       endif()
97     endif()
98   endif()
99 endif()
100
101
102 # --------------------------------------------------------------------------
103 # Install directories
104 # Build DOCUMENTATION (not in ALL target and only if Doxygen is found)
105 option(BUILD_DOC "Build the HTML documentation (with doxygen if available)." OFF)
106
107 string(TOLOWER ${PROJECT_NAME} projectname)
108 set(OPENJPEG_INSTALL_SUBDIR "${projectname}-${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}")
109
110 if(NOT OPENJPEG_INSTALL_BIN_DIR)
111   set(OPENJPEG_INSTALL_BIN_DIR "bin")
112 endif()
113
114 if(NOT OPENJPEG_INSTALL_LIB_DIR)
115   set(OPENJPEG_INSTALL_LIB_DIR "lib")
116 endif()
117
118 if(NOT OPENJPEG_INSTALL_SHARE_DIR)
119   set(OPENJPEG_INSTALL_SHARE_DIR "share")
120 endif()
121
122 if(NOT OPENJPEG_INSTALL_DATA_DIR)
123   set(OPENJPEG_INSTALL_DATA_DIR "${OPENJPEG_INSTALL_SHARE_DIR}/${OPENJPEG_INSTALL_SUBDIR}")
124 endif()
125
126 if(NOT OPENJPEG_INSTALL_INCLUDE_DIR)
127   set(OPENJPEG_INSTALL_INCLUDE_DIR "include/${OPENJPEG_INSTALL_SUBDIR}")
128 endif()
129
130 if(BUILD_DOC)
131 if(NOT OPENJPEG_INSTALL_MAN_DIR)
132   set(OPENJPEG_INSTALL_MAN_DIR "share/man/")
133 endif()
134
135 if(NOT OPENJPEG_INSTALL_DOC_DIR)
136   set(OPENJPEG_INSTALL_DOC_DIR "share/doc/${OPENJPEG_INSTALL_SUBDIR}")
137 endif()
138 endif()
139
140 if(NOT OPENJPEG_INSTALL_JNI_DIR)
141   if(WIN32)
142     set(OPENJPEG_INSTALL_JNI_DIR ${OPENJPEG_INSTALL_BIN_DIR})
143   else()
144     set(OPENJPEG_INSTALL_JNI_DIR ${OPENJPEG_INSTALL_LIB_DIR})
145   endif()
146 endif()
147
148 if(NOT OPENJPEG_INSTALL_PACKAGE_DIR)
149   # We could install *.cmake files in share/ however those files contains
150   # hardcoded path to libraries on a multi-arch system (fedora/debian) those
151   # path will be different (lib/i386-linux-gnu vs lib/x86_64-linux-gnu)
152   set(OPENJPEG_INSTALL_PACKAGE_DIR "${OPENJPEG_INSTALL_LIB_DIR}/${OPENJPEG_INSTALL_SUBDIR}")
153 endif()
154
155 if (APPLE)
156         list(APPEND OPENJPEG_LIBRARY_PROPERTIES INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/${OPENJPEG_INSTALL_LIB_DIR}")
157         option(OPJ_USE_DSYMUTIL "Call dsymutil on binaries after build." OFF)
158 endif()
159
160 #-----------------------------------------------------------------------------
161 # Big endian test:
162 include (${CMAKE_ROOT}/Modules/TestBigEndian.cmake)
163 TEST_BIG_ENDIAN(OPJ_BIG_ENDIAN)
164
165 #-----------------------------------------------------------------------------
166 # Setup file for setting custom ctest vars
167 configure_file(
168   ${${OPENJPEG_NAMESPACE}_SOURCE_DIR}/cmake/CTestCustom.cmake.in
169   ${${OPENJPEG_NAMESPACE}_BINARY_DIR}/CTestCustom.cmake
170   @ONLY
171   )
172
173 #-----------------------------------------------------------------------------
174 # OpenJPEG build configuration options.
175 option(BUILD_SHARED_LIBS "Build OpenJPEG shared library and link executables against it." ON)
176 set (EXECUTABLE_OUTPUT_PATH ${OPENJPEG_BINARY_DIR}/bin CACHE PATH "Single output directory for building all executables.")
177 set (LIBRARY_OUTPUT_PATH ${OPENJPEG_BINARY_DIR}/bin CACHE PATH "Single output directory for building all libraries.")
178 mark_as_advanced(LIBRARY_OUTPUT_PATH EXECUTABLE_OUTPUT_PATH)
179
180 #-----------------------------------------------------------------------------
181 # configure name mangling to allow multiple libraries to coexist
182 # peacefully
183 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/openjpeg_mangle.h.in)
184 set(MANGLE_PREFIX ${OPENJPEG_LIBRARY_NAME})
185 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/openjpeg_mangle.h.in
186                ${CMAKE_CURRENT_BINARY_DIR}/openjpeg_mangle.h
187                @ONLY)
188 endif()
189
190 #-----------------------------------------------------------------------------
191 # Compiler specific flags:
192 if(CMAKE_COMPILER_IS_GNUCC)
193   # For all builds, make sure openjpeg is std99 compliant:
194   # set(CMAKE_C_FLAGS "-Wall -std=c99 ${CMAKE_C_FLAGS}") # FIXME: this setting prevented us from setting a coverage build.
195   # Do not use ffast-math for all build, it would produce incorrect results, only set for release:
196   set(OPENJPEG_LIBRARY_COMPILE_OPTIONS ${OPENJPEG_LIBRARY_COMPILE_OPTIONS} "$<$<CONFIG:Release>:-ffast-math>")
197   set(OPENJP2_COMPILE_OPTIONS ${OPENJP2_COMPILE_OPTIONS} "$<$<CONFIG:Release>:-ffast-math>" -Wall -Wextra -Wconversion -Wunused-parameter -Wdeclaration-after-statement -Werror=declaration-after-statement)
198 endif()
199
200 #-----------------------------------------------------------------------------
201 # opj_config.h generation (1/2)
202
203 # Check if some include files are provided by the system
204 include(EnsureFileInclude)
205 # These files are mandatory
206 ensure_file_include("string.h"   HAVE_STRING_H YES)
207 ensure_file_include("memory.h"   HAVE_MEMORY_H YES)
208 ensure_file_include("stdlib.h"   HAVE_STDLIB_H YES)
209 ensure_file_include("stdio.h"    HAVE_STDIO_H  YES)
210 ensure_file_include("math.h"     HAVE_MATH_H   YES)
211 ensure_file_include("float.h"    HAVE_FLOAT_H  YES)
212 ensure_file_include("time.h"     HAVE_TIME_H   YES)
213 ensure_file_include("stdarg.h"   HAVE_STDARG_H YES)
214 ensure_file_include("ctype.h"    HAVE_CTYPE_H  YES)
215 ensure_file_include("assert.h"   HAVE_ASSERT_H YES)
216
217 # For the following files, we provide an alternative, they are not mandatory
218 ensure_file_include("stdint.h"   OPJ_HAVE_STDINT_H   NO)
219 ensure_file_include("inttypes.h" OPJ_HAVE_INTTYPES_H NO)
220
221 # why check this one ? for openjpip ?
222 include (${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake)
223 CHECK_INCLUDE_FILE("strings.h"      HAVE_STRINGS_H)
224 CHECK_INCLUDE_FILE("sys/stat.h"     HAVE_SYS_STAT_H)
225 CHECK_INCLUDE_FILE("sys/types.h"    HAVE_SYS_TYPES_H)
226 CHECK_INCLUDE_FILE("unistd.h"       HAVE_UNISTD_H)
227
228 # Enable Large file support
229 include(TestLargeFiles)
230 OPJ_TEST_LARGE_FILES(OPJ_HAVE_LARGEFILES)
231
232 # Allocating Aligned Memory Blocks
233 include(CheckIncludeFiles)
234 check_include_files(malloc.h OPJ_HAVE_MALLOC_H)
235 include(CheckSymbolExists)
236 # _aligned_alloc https://msdn.microsoft.com/en-us/library/8z34s9c6.aspx
237 check_symbol_exists(_aligned_malloc malloc.h OPJ_HAVE__ALIGNED_MALLOC)
238 # posix_memalign (needs _POSIX_C_SOURCE >= 200112L on Linux)
239 set(CMAKE_REQUIRED_DEFINITIONS -D_POSIX_C_SOURCE=200112L)
240 check_symbol_exists(posix_memalign stdlib.h OPJ_HAVE_POSIX_MEMALIGN)
241 unset(CMAKE_REQUIRED_DEFINITIONS)
242 # memalign (obsolete)
243 check_symbol_exists(memalign malloc.h OPJ_HAVE_MEMALIGN)
244 #-----------------------------------------------------------------------------
245 # Build Library
246 if(BUILD_JPIP_SERVER)
247   find_package(CURL REQUIRED)
248   find_package(FCGI REQUIRED)
249   find_package(Threads REQUIRED)
250   if(NOT CMAKE_USE_PTHREADS_INIT)
251     message(FATAL_ERROR "Only pthread are supported")
252   endif()
253 endif()
254 add_subdirectory(src/lib)
255 option(BUILD_LUTS_GENERATOR "Build utility to generate t1_luts.h" OFF)
256 option(BUILD_BENCH_DWT "Build bench_dwt utility (development benchmark)" OFF)
257
258 #-----------------------------------------------------------------------------
259 # Build Applications
260 option(BUILD_CODEC "Build the CODEC executables" ON)
261 option(BUILD_MJ2 "Build the MJ2 executables." OFF)
262 option(BUILD_JPWL "Build the JPWL library and executables" OFF)
263 option(BUILD_JPIP "Build the JPIP library and executables." OFF)
264 if(BUILD_JPIP)
265   option(BUILD_JPIP_SERVER "Build the JPIP server." OFF)
266 endif()
267 option(BUILD_VIEWER "Build the OPJViewer executable (C++)" OFF)
268 option(BUILD_JAVA "Build the openjpeg jar (Java)" OFF)
269 option(BUILD_JP3D "Build the JP3D comp" OFF)
270 mark_as_advanced(BUILD_VIEWER)
271 mark_as_advanced(BUILD_JAVA)
272 mark_as_advanced(BUILD_JP3D)
273
274 if(BUILD_CODEC OR BUILD_MJ2)
275   # OFF: It will only build 3rd party libs if they are not found on the system
276   # ON: 3rd party libs will ALWAYS be build, and used
277   option(BUILD_THIRDPARTY "Build the thirdparty executables if it is needed" OFF)
278   add_subdirectory(thirdparty)
279   add_subdirectory(src/bin)
280 endif ()
281 add_subdirectory(wrapping)
282
283 #-----------------------------------------------------------------------------
284 # opj_config.h generation (2/2)
285 configure_file(
286  ${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjp2/opj_config.h.cmake.in
287  ${CMAKE_CURRENT_BINARY_DIR}/src/lib/openjp2/opj_config.h
288  @ONLY
289  )
290
291  configure_file(
292  ${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjp2/opj_config_private.h.cmake.in
293  ${CMAKE_CURRENT_BINARY_DIR}/src/lib/openjp2/opj_config_private.h
294  @ONLY
295  )
296
297 #-----------------------------------------------------------------------------
298 # build documentation in doc subdir:
299 if(BUILD_DOC)
300   add_subdirectory(doc)
301 endif()
302
303 #-----------------------------------------------------------------------------
304 # Buld Testing
305 option(BUILD_TESTING "Build the tests." OFF)
306 if(BUILD_TESTING)
307   if(BUILD_CODEC)
308     enable_testing()
309     include(CTest)
310
311     # Search openjpeg data needed for the tests
312     # They could be found via git on the OpenJPEG GitHub code project
313     # git clone https://github.com/uclouvain/openjpeg-data.git
314     find_path(OPJ_DATA_ROOT README-OPJ-Data
315       PATHS $ENV{OPJ_DATA_ROOT} ${CMAKE_SOURCE_DIR}/../data
316       NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH
317       )
318
319     # Add repository where to find tests
320     add_subdirectory(tests)
321
322   else()
323     message(FATAL_ERROR "You need build codec to run the tests")
324   endif()
325 endif()
326
327 #-----------------------------------------------------------------------------
328 # install all targets referenced as OPENJPEGTargets
329 install(EXPORT OpenJPEGTargets DESTINATION ${OPENJPEG_INSTALL_PACKAGE_DIR})
330 configure_file( ${${OPENJPEG_NAMESPACE}_SOURCE_DIR}/cmake/OpenJPEGConfig.cmake.in
331   ${${OPENJPEG_NAMESPACE}_BINARY_DIR}/OpenJPEGConfig.cmake
332   @ONLY
333 )
334 install( FILES ${OPENJPEG_BINARY_DIR}/OpenJPEGConfig.cmake
335   DESTINATION ${OPENJPEG_INSTALL_PACKAGE_DIR}
336 )
337
338 #-----------------------------------------------------------------------------
339 # install CHANGES and LICENSE
340 if(BUILD_DOC)
341 if(EXISTS ${OPENJPEG_SOURCE_DIR}/CHANGES)
342   install(FILES CHANGES DESTINATION ${OPENJPEG_INSTALL_DOC_DIR})
343 endif()
344
345 install(FILES LICENSE DESTINATION ${OPENJPEG_INSTALL_DOC_DIR})
346 endif()
347
348 include (cmake/OpenJPEGCPack.cmake)
349
350 #-----------------------------------------------------------------------------
351 # pkgconfig support
352 # enabled by default on Unix, disabled by default on other platforms
353 if(UNIX)
354   option(BUILD_PKGCONFIG_FILES "Build and install pkg-config files" ON)
355 else()
356   option(BUILD_PKGCONFIG_FILES "Build and install pkg-config files" OFF)
357 endif()
358 if(BUILD_PKGCONFIG_FILES)
359   # install in lib and not share (see multi-arch note above)
360   configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjp2/libopenjp2.pc.cmake.in
361     ${CMAKE_CURRENT_BINARY_DIR}/libopenjp2.pc @ONLY)
362   install( FILES  ${CMAKE_CURRENT_BINARY_DIR}/libopenjp2.pc DESTINATION
363     ${OPENJPEG_INSTALL_LIB_DIR}/pkgconfig )
364 #
365   if(BUILD_JPWL)
366   # install in lib and not share (see multi-arch note above)
367   configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjpwl/libopenjpwl.pc.cmake.in
368     ${CMAKE_CURRENT_BINARY_DIR}/libopenjpwl.pc @ONLY)
369   install( FILES  ${CMAKE_CURRENT_BINARY_DIR}/libopenjpwl.pc DESTINATION
370     ${OPENJPEG_INSTALL_LIB_DIR}/pkgconfig )
371   endif()
372 #
373   if(BUILD_JPIP)
374   # install in lib and not share (see multi-arch note above)
375   configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjpip/libopenjpip.pc.cmake.in
376     ${CMAKE_CURRENT_BINARY_DIR}/libopenjpip.pc @ONLY)
377   install( FILES  ${CMAKE_CURRENT_BINARY_DIR}/libopenjpip.pc DESTINATION
378     ${OPENJPEG_INSTALL_LIB_DIR}/pkgconfig )
379   endif()
380 #
381   if(BUILD_JP3D)
382   # install in lib and not share (see multi-arch note above)
383   configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjp3d/libopenjp3d.pc.cmake.in
384     ${CMAKE_CURRENT_BINARY_DIR}/libopenjp3d.pc @ONLY)
385   install( FILES  ${CMAKE_CURRENT_BINARY_DIR}/libopenjp3d.pc DESTINATION
386     ${OPENJPEG_INSTALL_LIB_DIR}/pkgconfig )
387   endif()
388 endif()
389
390 #-----------------------------------------------------------------------------
391
392 # build our version of astyle
393 SET (WITH_ASTYLE FALSE CACHE BOOL "If you plan to contribute you should reindent with scripts/prepare-commit.sh (using 'our' astyle)")