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