[trunk] Add option to call dsymutil on built binaries (fixes issue 409)
[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 endif()
15
16 if(NOT OPENJPEG_NAMESPACE)
17   set(OPENJPEG_NAMESPACE "OPENJPEG")
18   set(OPENJPEG_STANDALONE 1)
19 endif()
20 # In all cases:
21 #string(TOLOWER ${OPENJPEG_NAMESPACE} OPENJPEG_LIBRARY_NAME)
22 set(OPENJPEG_LIBRARY_NAME openjp2)
23
24 project(${OPENJPEG_NAMESPACE} C)
25
26 # Do full dependency headers.
27 include_regular_expression("^.*$")
28
29 #-----------------------------------------------------------------------------
30 # OPENJPEG version number, useful for packaging and doxygen doc:
31 set(OPENJPEG_VERSION_MAJOR 2)
32 set(OPENJPEG_VERSION_MINOR 1)
33 set(OPENJPEG_VERSION_BUILD 0)
34 set(OPENJPEG_VERSION
35   "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}")
36 set(PACKAGE_VERSION
37   "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}")
38
39 # Because autotools does not support X.Y notation for SOVERSION, we have to use
40 # two numbering, one for the openjpeg version and one for openjpeg soversion
41 # version | soversion
42 #   1.0   |  0
43 #   1.1   |  1
44 #   1.2   |  2
45 #   1.3   |  3
46 #   1.4   |  4
47 #   1.5   |  5
48 #   1.5.1 |  5
49 #   2.0   |  6
50 #   2.0.1 |  6
51 #   2.1   |  7
52 # above is the recommendation by the OPJ team. If you really need to override this default,
53 # you can specify your own OPENJPEG_SOVERSION at cmake configuration time:
54 # cmake -DOPENJPEG_SOVERSION:STRING=42 /path/to/openjpeg
55 if(NOT OPENJPEG_SOVERSION)
56   SET(OPENJPEG_SOVERSION 7)
57 endif(NOT OPENJPEG_SOVERSION)
58 set(OPENJPEG_LIBRARY_PROPERTIES
59   VERSION   "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}"
60   SOVERSION "${OPENJPEG_SOVERSION}"
61 )
62
63 # --------------------------------------------------------------------------
64 # Path to additional CMake modules
65 set(CMAKE_MODULE_PATH
66     ${CMAKE_SOURCE_DIR}/cmake
67     ${CMAKE_MODULE_PATH})
68
69 # --------------------------------------------------------------------------
70 # On Visual Studio 8 MS deprecated C. This removes all 1.276E1265 security
71 # warnings
72 if(WIN32)
73   if(NOT BORLAND)
74     if(NOT CYGWIN)
75       if(NOT MINGW)
76         if(NOT ITK_ENABLE_VISUAL_STUDIO_DEPRECATED_C_WARNINGS)
77           add_definitions(
78             -D_CRT_FAR_MAPPINGS_NO_DEPRECATE
79             -D_CRT_IS_WCTYPE_NO_DEPRECATE
80             -D_CRT_MANAGED_FP_NO_DEPRECATE
81             -D_CRT_NONSTDC_NO_DEPRECATE
82             -D_CRT_SECURE_NO_DEPRECATE
83             -D_CRT_SECURE_NO_DEPRECATE_GLOBALS
84             -D_CRT_SETERRORMODE_BEEP_SLEEP_NO_DEPRECATE
85             -D_CRT_TIME_FUNCTIONS_NO_DEPRECATE
86             -D_CRT_VCCLRIT_NO_DEPRECATE
87             -D_SCL_SECURE_NO_DEPRECATE
88             )
89         endif()
90       endif()
91     endif()
92   endif()
93 endif()
94
95
96 # --------------------------------------------------------------------------
97 # Install directories
98 # Build DOCUMENTATION (not in ALL target and only if Doxygen is found)
99 option(BUILD_DOC "Build the HTML documentation (with doxygen if available)." OFF)
100
101 string(TOLOWER ${PROJECT_NAME} projectname)
102 set(OPENJPEG_INSTALL_SUBDIR "${projectname}-${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}")
103
104 if(NOT OPENJPEG_INSTALL_BIN_DIR)
105   set(OPENJPEG_INSTALL_BIN_DIR "bin")
106 endif()
107
108 if(NOT OPENJPEG_INSTALL_LIB_DIR)
109   set(OPENJPEG_INSTALL_LIB_DIR "lib")
110 endif()
111
112 if(NOT OPENJPEG_INSTALL_SHARE_DIR)
113   set(OPENJPEG_INSTALL_SHARE_DIR "share")
114 endif()
115
116 if(NOT OPENJPEG_INSTALL_DATA_DIR)
117   set(OPENJPEG_INSTALL_DATA_DIR "${OPENJPEG_INSTALL_SHARE_DIR}/${OPENJPEG_INSTALL_SUBDIR}")
118 endif()
119
120 if(NOT OPENJPEG_INSTALL_INCLUDE_DIR)
121   set(OPENJPEG_INSTALL_INCLUDE_DIR "include/${OPENJPEG_INSTALL_SUBDIR}")
122 endif()
123
124 if(BUILD_DOC)
125 if(NOT OPENJPEG_INSTALL_MAN_DIR)
126   set(OPENJPEG_INSTALL_MAN_DIR "share/man/")
127 endif()
128
129 if(NOT OPENJPEG_INSTALL_DOC_DIR)
130   set(OPENJPEG_INSTALL_DOC_DIR "share/doc/${OPENJPEG_INSTALL_SUBDIR}")
131 endif()
132 endif()
133
134 if(NOT OPENJPEG_INSTALL_JNI_DIR)
135   if(WIN32)
136     set(OPENJPEG_INSTALL_JNI_DIR ${OPENJPEG_INSTALL_BIN_DIR})
137   else()
138     set(OPENJPEG_INSTALL_JNI_DIR ${OPENJPEG_INSTALL_LIB_DIR})
139   endif()
140 endif()
141
142 if(NOT OPENJPEG_INSTALL_PACKAGE_DIR)
143   # We could install *.cmake files in share/ however those files contains
144   # hardcoded path to libraries on a multi-arch system (fedora/debian) those
145   # path will be different (lib/i386-linux-gnu vs lib/x86_64-linux-gnu)
146   set(OPENJPEG_INSTALL_PACKAGE_DIR "${OPENJPEG_INSTALL_LIB_DIR}/${OPENJPEG_INSTALL_SUBDIR}")
147 endif()
148
149 if (APPLE)
150         list(APPEND OPENJPEG_LIBRARY_PROPERTIES INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/${OPENJPEG_INSTALL_LIB_DIR}")
151         option(OPJ_USE_DSYMUTIL "Call dsymutil on binaries after build." OFF)
152 endif()
153
154 #-----------------------------------------------------------------------------
155 # Big endian test:
156 include (${CMAKE_ROOT}/Modules/TestBigEndian.cmake)
157 TEST_BIG_ENDIAN(OPJ_BIG_ENDIAN)
158
159 #-----------------------------------------------------------------------------
160 # Setup file for setting custom ctest vars
161 configure_file(
162   ${CMAKE_SOURCE_DIR}/cmake/CTestCustom.cmake.in
163   ${CMAKE_BINARY_DIR}/CTestCustom.cmake
164   @ONLY
165   )
166
167 #-----------------------------------------------------------------------------
168 # OpenJPEG build configuration options.
169 option(BUILD_SHARED_LIBS "Build OpenJPEG shared library and link executables against it." ON)
170 set (EXECUTABLE_OUTPUT_PATH ${OPENJPEG_BINARY_DIR}/bin CACHE PATH "Single output directory for building all executables.")
171 set (LIBRARY_OUTPUT_PATH ${OPENJPEG_BINARY_DIR}/bin CACHE PATH "Single output directory for building all libraries.")
172 mark_as_advanced(LIBRARY_OUTPUT_PATH EXECUTABLE_OUTPUT_PATH)
173
174 #-----------------------------------------------------------------------------
175 # configure name mangling to allow multiple libraries to coexist
176 # peacefully
177 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/openjpeg_mangle.h.in)
178 set(MANGLE_PREFIX ${OPENJPEG_LIBRARY_NAME})
179 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/openjpeg_mangle.h.in
180                ${CMAKE_CURRENT_BINARY_DIR}/openjpeg_mangle.h
181                @ONLY)
182 endif()
183
184 #-----------------------------------------------------------------------------
185 # Compiler specific flags:
186 if(CMAKE_COMPILER_IS_GNUCC)
187   # For all builds, make sure openjpeg is std99 compliant:
188   # set(CMAKE_C_FLAGS "-Wall -std=c99 ${CMAKE_C_FLAGS}") # FIXME: this setting prevented us from setting a coverage build.
189   # Do not use ffast-math for all build, it would produce incorrect results, only set for release:
190   set(CMAKE_C_FLAGS_RELEASE "-ffast-math ${CMAKE_C_FLAGS_RELEASE}")
191 endif()
192
193 #-----------------------------------------------------------------------------
194 # opj_config.h generation (1/2)
195
196 # Check if some include files are provided by the system
197 include(EnsureFileInclude)
198 # These files are mandatory
199 ensure_file_include("string.h"   HAVE_STRING_H YES)
200 ensure_file_include("memory.h"   HAVE_MEMORY_H YES)
201 ensure_file_include("stdlib.h"   HAVE_STDLIB_H YES)
202 ensure_file_include("stdio.h"    HAVE_STDIO_H  YES)
203 ensure_file_include("math.h"     HAVE_MATH_H   YES)
204 ensure_file_include("float.h"    HAVE_FLOAT_H  YES)
205 ensure_file_include("time.h"     HAVE_TIME_H   YES)
206 ensure_file_include("stdarg.h"   HAVE_STDARG_H YES)
207 ensure_file_include("ctype.h"    HAVE_CTYPE_H  YES)
208 ensure_file_include("assert.h"   HAVE_ASSERT_H YES)
209
210 # For the following files, we provide an alternative, they are not mandatory
211 ensure_file_include("stdint.h"   OPJ_HAVE_STDINT_H   NO)
212 ensure_file_include("inttypes.h" OPJ_HAVE_INTTYPES_H NO)
213
214 # why check this one ? for openjpip ?
215 include (${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake)
216 CHECK_INCLUDE_FILE("strings.h"      HAVE_STRINGS_H)
217 CHECK_INCLUDE_FILE("sys/stat.h"     HAVE_SYS_STAT_H)
218 CHECK_INCLUDE_FILE("sys/types.h"    HAVE_SYS_TYPES_H)
219 CHECK_INCLUDE_FILE("unistd.h"       HAVE_UNISTD_H)
220
221 # Enable Large file support
222 include(TestLargeFiles)
223 OPJ_TEST_LARGE_FILES(OPJ_HAVE_LARGEFILES)
224
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
237 #-----------------------------------------------------------------------------
238 # Build Applications
239 option(BUILD_CODEC "Build the CODEC executables" ON)
240 option(BUILD_MJ2 "Build the MJ2 executables." OFF)
241 option(BUILD_JPWL "Build the JPWL library and executables" OFF)
242 option(BUILD_JPIP "Build the JPIP library and executables." OFF)
243 if(BUILD_JPIP)
244   option(BUILD_JPIP_SERVER "Build the JPIP server." OFF)
245 endif()
246 option(BUILD_VIEWER "Build the OPJViewer executable (C++)" OFF)
247 option(BUILD_JAVA "Build the openjpeg jar (Java)" OFF)
248 option(BUILD_JP3D "Build the JP3D comp" OFF)
249 mark_as_advanced(BUILD_VIEWER)
250 mark_as_advanced(BUILD_JAVA)
251 mark_as_advanced(BUILD_JP3D)
252
253 if(BUILD_CODEC OR BUILD_MJ2)
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 # Buld 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 svn on the OpenJPEG google code project
292     # svn checkout http://openjpeg.googlecode.com/svn/data (about 70 Mo)
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
308 install(EXPORT OpenJPEGTargets DESTINATION ${OPENJPEG_INSTALL_PACKAGE_DIR})
309 configure_file( ${OPENJPEG_SOURCE_DIR}/cmake/OpenJPEGConfig.cmake.in
310   ${OPENJPEG_BINARY_DIR}/OpenJPEGConfig.cmake
311   @ONLY
312 )
313 install( FILES ${OPENJPEG_BINARY_DIR}/OpenJPEGConfig.cmake
314   DESTINATION ${OPENJPEG_INSTALL_PACKAGE_DIR}
315 )
316
317 #-----------------------------------------------------------------------------
318 # install CHANGES and LICENSE
319 if(BUILD_DOC)
320 if(EXISTS ${OPENJPEG_SOURCE_DIR}/CHANGES)
321   install(FILES CHANGES DESTINATION ${OPENJPEG_INSTALL_DOC_DIR})
322 endif()
323
324 install(FILES LICENSE DESTINATION ${OPENJPEG_INSTALL_DOC_DIR})
325 endif()
326
327 include (cmake/OpenJPEGCPack.cmake)
328
329 #-----------------------------------------------------------------------------
330 # pkgconfig support
331 # enabled by default on Unix, disabled by default on other platforms
332 if(UNIX)
333   option(BUILD_PKGCONFIG_FILES "Build and install pkg-config files" ON)
334 else()
335   option(BUILD_PKGCONFIG_FILES "Build and install pkg-config files" OFF)
336 endif()
337 if(BUILD_PKGCONFIG_FILES)
338   # install in lib and not share (see multi-arch note above)
339   configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjp2/libopenjp2.pc.cmake.in
340     ${CMAKE_CURRENT_BINARY_DIR}/libopenjp2.pc @ONLY)
341   install( FILES  ${CMAKE_CURRENT_BINARY_DIR}/libopenjp2.pc DESTINATION
342     ${OPENJPEG_INSTALL_LIB_DIR}/pkgconfig )
343 #
344   if(BUILD_JPWL)
345   # install in lib and not share (see multi-arch note above)
346   configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjpwl/libopenjpwl.pc.cmake.in
347     ${CMAKE_CURRENT_BINARY_DIR}/libopenjpwl.pc @ONLY)
348   install( FILES  ${CMAKE_CURRENT_BINARY_DIR}/libopenjpwl.pc DESTINATION
349     ${OPENJPEG_INSTALL_LIB_DIR}/pkgconfig )
350   endif()
351 #
352   if(BUILD_JPIP)
353   # install in lib and not share (see multi-arch note above)
354   configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjpip/libopenjpip.pc.cmake.in
355     ${CMAKE_CURRENT_BINARY_DIR}/libopenjpip.pc @ONLY)
356   install( FILES  ${CMAKE_CURRENT_BINARY_DIR}/libopenjpip.pc DESTINATION
357     ${OPENJPEG_INSTALL_LIB_DIR}/pkgconfig )
358   endif()
359 #
360   if(BUILD_JP3D)
361   # install in lib and not share (see multi-arch note above)
362   configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjp3d/libopenjp3d.pc.cmake.in
363     ${CMAKE_CURRENT_BINARY_DIR}/libopenjp3d.pc @ONLY)
364   install( FILES  ${CMAKE_CURRENT_BINARY_DIR}/libopenjp3d.pc DESTINATION
365     ${OPENJPEG_INSTALL_LIB_DIR}/pkgconfig )
366   endif()
367 endif()
368
369 #-----------------------------------------------------------------------------