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