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