Prepare for OpenJPEG v2.5.1 release
[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 3.5)
11
12 if(NOT OPENJPEG_NAMESPACE)
13   set(OPENJPEG_NAMESPACE "OPENJPEG")
14   set(OPENJPEG_STANDALONE 1)
15 endif()
16 # In all cases:
17 #string(TOLOWER ${OPENJPEG_NAMESPACE} OPENJPEG_LIBRARY_NAME)
18 set(OPENJPEG_LIBRARY_NAME openjp2)
19
20 project(${OPENJPEG_NAMESPACE} C)
21
22 # Do full dependency headers.
23 include_regular_expression("^.*$")
24
25 #-----------------------------------------------------------------------------
26 # OPENJPEG version number, useful for packaging and doxygen doc:
27 set(OPENJPEG_VERSION_MAJOR 2)
28 set(OPENJPEG_VERSION_MINOR 5)
29 set(OPENJPEG_VERSION_BUILD 1)
30 set(OPENJPEG_VERSION
31   "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}")
32 set(PACKAGE_VERSION
33   "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}")
34
35 # Because autotools does not support X.Y notation for SOVERSION, we have to use
36 # two numbering, one for the openjpeg version and one for openjpeg soversion
37 # version | soversion
38 #   1.0   |  0
39 #   1.1   |  1
40 #   1.2   |  2
41 #   1.3   |  3
42 #   1.4   |  4
43 #   1.5   |  5
44 #   1.5.1 |  5
45 #   2.0   |  6
46 #   2.0.1 |  6
47 #   2.1   |  7
48 #   2.1.1 |  7
49 #   2.1.2 |  7
50 #   2.2.0 |  7
51 #   2.3.0 |  7
52 #   2.3.1 |  7
53 #   2.4.0 |  7
54 #   2.5.0 |  7
55 #   2.5.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     ${${OPENJPEG_NAMESPACE}_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 string(TOLOWER ${PROJECT_NAME} PROJECT_NAME)
103 include(GNUInstallDirs)
104
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 set(OPENJPEG_INSTALL_SUBDIR "${PROJECT_NAME}-${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}")
109
110 if(NOT OPENJPEG_INSTALL_JNI_DIR)
111   if(WIN32)
112     set(OPENJPEG_INSTALL_JNI_DIR ${CMAKE_INSTALL_BINDIR})
113   else()
114     set(OPENJPEG_INSTALL_JNI_DIR ${CMAKE_INSTALL_LIBDIR})
115   endif()
116 endif()
117
118 if(NOT OPENJPEG_INSTALL_PACKAGE_DIR)
119   set(OPENJPEG_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${OPENJPEG_INSTALL_SUBDIR}")
120 endif()
121
122 if (APPLE)
123         option(OPJ_USE_DSYMUTIL "Call dsymutil on binaries after build." OFF)
124 endif()
125
126 #-----------------------------------------------------------------------------
127 # Big endian test:
128 if (NOT EMSCRIPTEN)
129 include (${CMAKE_ROOT}/Modules/TestBigEndian.cmake)
130 TEST_BIG_ENDIAN(OPJ_BIG_ENDIAN)
131 endif()
132
133 #-----------------------------------------------------------------------------
134 # Setup file for setting custom ctest vars
135 configure_file(
136   ${${OPENJPEG_NAMESPACE}_SOURCE_DIR}/cmake/CTestCustom.cmake.in
137   ${${OPENJPEG_NAMESPACE}_BINARY_DIR}/CTestCustom.cmake
138   @ONLY
139   )
140
141 #-----------------------------------------------------------------------------
142 # OpenJPEG build configuration options.
143 option(BUILD_SHARED_LIBS "Build OpenJPEG shared library and link executables against it." ON)
144 option(BUILD_STATIC_LIBS "Build OpenJPEG static library." ON)
145 set (EXECUTABLE_OUTPUT_PATH ${OPENJPEG_BINARY_DIR}/bin CACHE PATH "Single output directory for building all executables.")
146 set (LIBRARY_OUTPUT_PATH ${OPENJPEG_BINARY_DIR}/bin CACHE PATH "Single output directory for building all libraries.")
147 mark_as_advanced(LIBRARY_OUTPUT_PATH EXECUTABLE_OUTPUT_PATH)
148
149 #-----------------------------------------------------------------------------
150 # configure name mangling to allow multiple libraries to coexist
151 # peacefully
152 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/openjpeg_mangle.h.in)
153 set(MANGLE_PREFIX ${OPENJPEG_LIBRARY_NAME})
154 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/openjpeg_mangle.h.in
155                ${CMAKE_CURRENT_BINARY_DIR}/openjpeg_mangle.h
156                @ONLY)
157 endif()
158
159 #-----------------------------------------------------------------------------
160 # Compiler specific flags:
161 if(CMAKE_COMPILER_IS_GNUCC)
162   # For all builds, make sure openjpeg is std99 compliant:
163   # set(CMAKE_C_FLAGS "-Wall -std=c99 ${CMAKE_C_FLAGS}") # FIXME: this setting prevented us from setting a coverage build.
164   # Do not use ffast-math for all build, it would produce incorrect results, only set for release:
165   set(OPENJPEG_LIBRARY_COMPILE_OPTIONS ${OPENJPEG_LIBRARY_COMPILE_OPTIONS} "$<$<CONFIG:Release>:-ffast-math>")
166   set(OPENJP2_COMPILE_OPTIONS ${OPENJP2_COMPILE_OPTIONS} "$<$<CONFIG:Release>:-ffast-math>" -Wall -Wextra -Wconversion -Wunused-parameter -Wdeclaration-after-statement -Werror=declaration-after-statement -Wstrict-prototypes -Werror=strict-prototypes -Wmissing-prototypes -Werror=missing-prototypes)
167 endif()
168
169 #-----------------------------------------------------------------------------
170 # opj_config.h generation (1/2)
171
172 # Check if some include files are provided by the system
173 include(EnsureFileInclude)
174 # These files are mandatory
175 ensure_file_include("string.h"   HAVE_STRING_H   YES)
176 ensure_file_include("memory.h"   HAVE_MEMORY_H   YES)
177 ensure_file_include("stdlib.h"   HAVE_STDLIB_H   YES)
178 ensure_file_include("stdio.h"    HAVE_STDIO_H    YES)
179 ensure_file_include("math.h"     HAVE_MATH_H     YES)
180 ensure_file_include("float.h"    HAVE_FLOAT_H    YES)
181 ensure_file_include("time.h"     HAVE_TIME_H     YES)
182 ensure_file_include("stdarg.h"   HAVE_STDARG_H   YES)
183 ensure_file_include("ctype.h"    HAVE_CTYPE_H    YES)
184 ensure_file_include("assert.h"   HAVE_ASSERT_H   YES)
185 ensure_file_include("stdint.h"   HAVE_STDINT_H   YES)
186 ensure_file_include("inttypes.h" HAVE_INTTYPES_H YES)
187
188 # why check this one ? for openjpip ?
189 include (${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake)
190 CHECK_INCLUDE_FILE("strings.h"      HAVE_STRINGS_H)
191 CHECK_INCLUDE_FILE("sys/stat.h"     HAVE_SYS_STAT_H)
192 CHECK_INCLUDE_FILE("sys/types.h"    HAVE_SYS_TYPES_H)
193 CHECK_INCLUDE_FILE("unistd.h"       HAVE_UNISTD_H)
194
195 # Enable Large file support
196 include(TestLargeFiles)
197 OPJ_TEST_LARGE_FILES(OPJ_HAVE_LARGEFILES)
198
199 # Allocating Aligned Memory Blocks
200 include(CheckIncludeFiles)
201 check_include_files(malloc.h OPJ_HAVE_MALLOC_H)
202 include(CheckSymbolExists)
203 # _aligned_alloc https://msdn.microsoft.com/en-us/library/8z34s9c6.aspx
204 check_symbol_exists(_aligned_malloc malloc.h OPJ_HAVE__ALIGNED_MALLOC)
205 # posix_memalign (needs _POSIX_C_SOURCE >= 200112L on Linux)
206 set(CMAKE_REQUIRED_DEFINITIONS -D_POSIX_C_SOURCE=200112L)
207 check_symbol_exists(posix_memalign stdlib.h OPJ_HAVE_POSIX_MEMALIGN)
208 unset(CMAKE_REQUIRED_DEFINITIONS)
209 # memalign (obsolete)
210 check_symbol_exists(memalign malloc.h OPJ_HAVE_MEMALIGN)
211 #-----------------------------------------------------------------------------
212 # Build Library
213 if(BUILD_JPIP_SERVER)
214   find_package(CURL REQUIRED)
215   find_package(FCGI REQUIRED)
216   find_package(Threads REQUIRED)
217   if(NOT CMAKE_USE_PTHREADS_INIT)
218     message(FATAL_ERROR "Only pthread are supported")
219   endif()
220 endif()
221 add_subdirectory(src/lib)
222 option(BUILD_LUTS_GENERATOR "Build utility to generate t1_luts.h" OFF)
223 if(UNIX)
224 option(BUILD_UNIT_TESTS "Build unit tests (bench_dwt, test_sparse_array, etc..)" OFF)
225 endif()
226
227 #-----------------------------------------------------------------------------
228 # Build Applications
229 option(BUILD_CODEC "Build the CODEC executables" ON)
230 option(BUILD_JPIP "Build the JPIP library and executables." OFF)
231 if(BUILD_JPIP)
232   option(BUILD_JPIP_SERVER "Build the JPIP server." OFF)
233 endif()
234 option(BUILD_VIEWER "Build the OPJViewer executable (C++)" OFF)
235 option(BUILD_JAVA "Build the openjpeg jar (Java)" OFF)
236 mark_as_advanced(BUILD_VIEWER)
237 mark_as_advanced(BUILD_JAVA)
238
239 if(BUILD_CODEC)
240   # OFF: It will only build 3rd party libs if they are not found on the system
241   # ON: 3rd party libs will ALWAYS be build, and used
242   option(BUILD_THIRDPARTY "Build the thirdparty executables if it is needed" OFF)
243   add_subdirectory(thirdparty)
244   add_subdirectory(src/bin)
245 endif ()
246 add_subdirectory(wrapping)
247
248 #-----------------------------------------------------------------------------
249 # opj_config.h generation (2/2)
250 configure_file(
251  ${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjp2/opj_config.h.cmake.in
252  ${CMAKE_CURRENT_BINARY_DIR}/src/lib/openjp2/opj_config.h
253  @ONLY
254  )
255
256  configure_file(
257  ${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjp2/opj_config_private.h.cmake.in
258  ${CMAKE_CURRENT_BINARY_DIR}/src/lib/openjp2/opj_config_private.h
259  @ONLY
260  )
261
262 #-----------------------------------------------------------------------------
263 # build documentation in doc subdir:
264 if(BUILD_DOC)
265   add_subdirectory(doc)
266 endif()
267
268 #-----------------------------------------------------------------------------
269 # Build Testing
270 option(BUILD_TESTING "Build the tests." OFF)
271 if(BUILD_TESTING)
272   if(BUILD_CODEC)
273     enable_testing()
274     include(CTest)
275
276     # Search openjpeg data needed for the tests
277     # They could be found via git on the OpenJPEG GitHub code project
278     # git clone https://github.com/uclouvain/openjpeg-data.git
279     find_path(OPJ_DATA_ROOT README-OPJ-Data
280       PATHS $ENV{OPJ_DATA_ROOT} ${CMAKE_SOURCE_DIR}/../data
281       NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH
282       )
283
284     # Add repository where to find tests
285     add_subdirectory(tests)
286
287   else()
288     message(FATAL_ERROR "You need build codec to run the tests")
289   endif()
290 endif()
291
292 #-----------------------------------------------------------------------------
293 # install all targets referenced as OPENJPEGTargets (relocatable with CMake 3.0+)
294 install(EXPORT OpenJPEGTargets DESTINATION ${OPENJPEG_INSTALL_PACKAGE_DIR})
295 include(CMakePackageConfigHelpers)
296 configure_package_config_file(${CMAKE_CURRENT_LIST_DIR}/cmake/OpenJPEGConfig.cmake.in
297   ${OPENJPEG_BINARY_DIR}/OpenJPEGConfig.cmake
298   INSTALL_DESTINATION ${OPENJPEG_INSTALL_PACKAGE_DIR}
299   PATH_VARS CMAKE_INSTALL_INCLUDEDIR)
300 write_basic_package_version_file(
301   ${OPENJPEG_BINARY_DIR}/OpenJPEGConfigVersion.cmake
302   COMPATIBILITY SameMajorVersion
303   VERSION ${OPENJPEG_VERSION})
304 install(
305   FILES
306     ${OPENJPEG_BINARY_DIR}/OpenJPEGConfig.cmake
307     ${OPENJPEG_BINARY_DIR}/OpenJPEGConfigVersion.cmake
308   DESTINATION ${OPENJPEG_INSTALL_PACKAGE_DIR})
309
310 #-----------------------------------------------------------------------------
311
312 include (cmake/OpenJPEGCPack.cmake)
313
314 #-----------------------------------------------------------------------------
315 # pkgconfig support
316 macro(set_variable_from_rel_or_absolute_path var root rel_or_abs_path)
317   if(IS_ABSOLUTE "${rel_or_abs_path}")
318     set(${var} "${rel_or_abs_path}")
319   else()
320     set(${var} "${root}/${rel_or_abs_path}")
321   endif()
322 endmacro()
323 set_variable_from_rel_or_absolute_path("bindir" "\\\${prefix}" "${CMAKE_INSTALL_BINDIR}")
324 set_variable_from_rel_or_absolute_path("mandir" "\\\${prefix}" "${CMAKE_INSTALL_MANDIR}")
325 set_variable_from_rel_or_absolute_path("docdir" "\\\${prefix}" "${CMAKE_INSTALL_DOCDIR}")
326 set_variable_from_rel_or_absolute_path("libdir" "\\\${prefix}" "${CMAKE_INSTALL_LIBDIR}")
327 set_variable_from_rel_or_absolute_path("includedir" "\\\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}/${OPENJPEG_INSTALL_SUBDIR}")
328
329 # install in lib and not share (CMAKE_INSTALL_LIBDIR takes care of it for multi-arch)
330 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjp2/libopenjp2.pc.cmake.in
331   ${CMAKE_CURRENT_BINARY_DIR}/libopenjp2.pc @ONLY)
332 install( FILES  ${CMAKE_CURRENT_BINARY_DIR}/libopenjp2.pc DESTINATION
333   ${CMAKE_INSTALL_LIBDIR}/pkgconfig )
334 #
335 if(BUILD_JPIP)
336   configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/lib/openjpip/libopenjpip.pc.cmake.in
337     ${CMAKE_CURRENT_BINARY_DIR}/libopenjpip.pc @ONLY)
338   install( FILES  ${CMAKE_CURRENT_BINARY_DIR}/libopenjpip.pc DESTINATION
339     ${CMAKE_INSTALL_LIBDIR}/pkgconfig )
340 endif()
341
342 #-----------------------------------------------------------------------------
343
344 # build our version of astyle
345 SET (WITH_ASTYLE FALSE CACHE BOOL "If you plan to contribute you should reindent with scripts/prepare-commit.sh (using 'our' astyle)")