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