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