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