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")
10CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
11
12IF(COMMAND CMAKE_POLICY)
13  CMAKE_POLICY(SET CMP0003 NEW)
14ENDIF(COMMAND CMAKE_POLICY)
15
16IF(NOT OPENJPEG_NAMESPACE)
17  SET(OPENJPEG_NAMESPACE "OPENJPEG")
18  SET(OPENJPEG_STANDALONE 1)
19ENDIF(NOT OPENJPEG_NAMESPACE)
20# In all cases:
21STRING(TOLOWER ${OPENJPEG_NAMESPACE} OPENJPEG_LIBRARY_NAME)
22
23PROJECT(${OPENJPEG_NAMESPACE} C)
24
25# Do full dependency headers.
26INCLUDE_REGULAR_EXPRESSION("^.*$")
27
28#-----------------------------------------------------------------------------
29# OPENJPEG version number, useful for packaging and doxygen doc:
30SET(OPENJPEG_VERSION_MAJOR 1)
31SET(OPENJPEG_VERSION_MINOR 5)
32SET(OPENJPEG_VERSION_BUILD 2)
33SET(OPENJPEG_VERSION
34  "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}")
35SET(PACKAGE_VERSION
36  "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}")
37
38# Because autotools does not support X.Y notation for SOVERSION, we have to use
39# two numerorations, one for the openjpeg version and one for openjpeg soversion
40# version | soversion
41#   1.0   |  0
42#   1.1   |  1
43#   1.2   |  2
44#   1.3   |  3
45#   1.4   |  4
46#   1.5   |  5
47#   1.5.1 |  5
48#   2.0   |  6
49# above is the recommendation by the OPJ team. If you really need to override this default,
50# you can specify your own OPENJPEG_SOVERSION at cmake configuration time:
51# cmake -DOPENJPEG_SOVERSION:STRING=42 /path/to/openjpeg
52if(NOT OPENJPEG_SOVERSION)
53  SET(OPENJPEG_SOVERSION 5)
54endif(NOT OPENJPEG_SOVERSION)
55SET(OPENJPEG_LIBRARY_PROPERTIES
56  VERSION   "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}"
57  SOVERSION "${OPENJPEG_SOVERSION}"
58)
59
60# --------------------------------------------------------------------------
61# Path to additional CMake modules
62SET(CMAKE_MODULE_PATH
63    ${CMAKE_SOURCE_DIR}/CMake
64    ${CMAKE_MODULE_PATH})
65
66# --------------------------------------------------------------------------
67# On Visual Studio 8 MS deprecated C. This removes all 1.276E1265 security
68# warnings
69IF(WIN32)
70  IF(NOT BORLAND)
71    IF(NOT CYGWIN)
72      IF(NOT MINGW)
73        IF(NOT ITK_ENABLE_VISUAL_STUDIO_DEPRECATED_C_WARNINGS)
74          ADD_DEFINITIONS(
75            -D_CRT_FAR_MAPPINGS_NO_DEPRECATE
76            -D_CRT_IS_WCTYPE_NO_DEPRECATE
77            -D_CRT_MANAGED_FP_NO_DEPRECATE
78            -D_CRT_NONSTDC_NO_DEPRECATE
79            -D_CRT_SECURE_NO_DEPRECATE
80            -D_CRT_SECURE_NO_DEPRECATE_GLOBALS
81            -D_CRT_SETERRORMODE_BEEP_SLEEP_NO_DEPRECATE
82            -D_CRT_TIME_FUNCTIONS_NO_DEPRECATE
83            -D_CRT_VCCLRIT_NO_DEPRECATE
84            -D_SCL_SECURE_NO_DEPRECATE
85            )
86        ENDIF(NOT ITK_ENABLE_VISUAL_STUDIO_DEPRECATED_C_WARNINGS)
87      ENDIF(NOT MINGW)
88    ENDIF(NOT CYGWIN)
89  ENDIF(NOT BORLAND)
90ENDIF(WIN32)
91
92
93# --------------------------------------------------------------------------
94# Install directories
95
96STRING(TOLOWER ${PROJECT_NAME} projectname)
97SET(OPENJPEG_INSTALL_SUBDIR "${projectname}-${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}")
98
99IF(NOT OPENJPEG_INSTALL_BIN_DIR)
100  SET(OPENJPEG_INSTALL_BIN_DIR "bin")
101ENDIF(NOT OPENJPEG_INSTALL_BIN_DIR)
102
103IF(NOT OPENJPEG_INSTALL_LIB_DIR)
104  SET(OPENJPEG_INSTALL_LIB_DIR "lib")
105ENDIF(NOT OPENJPEG_INSTALL_LIB_DIR)
106
107# The following will compute the amount of parent dir to go
108# from include to lib. it works nicely with
109# OPENJPEG_INSTALL_LIB_DIR=lib
110# OPENJPEG_INSTALL_LIB_DIR=lib/
111# OPENJPEG_INSTALL_LIB_DIR=/lib
112# OPENJPEG_INSTALL_LIB_DIR=lib/gnu-linux-x64
113STRING(REPLACE "/" ";" relative_to_lib ${OPENJPEG_INSTALL_LIB_DIR})
114set(relative_parent "..")
115foreach( elem ${relative_to_lib})
116  set( relative_parent "${relative_parent}/.." )
117endforeach()
118
119IF(NOT OPENJPEG_INSTALL_SHARE_DIR)
120  SET(OPENJPEG_INSTALL_SHARE_DIR "share")
121ENDIF(NOT OPENJPEG_INSTALL_SHARE_DIR)
122
123IF(NOT OPENJPEG_INSTALL_DATA_DIR)
124  SET(OPENJPEG_INSTALL_DATA_DIR "${OPENJPEG_INSTALL_SHARE_DIR}/${OPENJPEG_INSTALL_SUBDIR}")
125ENDIF(NOT OPENJPEG_INSTALL_DATA_DIR)
126
127IF(NOT OPENJPEG_INSTALL_INCLUDE_DIR)
128  SET(OPENJPEG_INSTALL_INCLUDE_DIR "include/${OPENJPEG_INSTALL_SUBDIR}")
129ENDIF(NOT OPENJPEG_INSTALL_INCLUDE_DIR)
130
131IF(NOT OPENJPEG_INSTALL_MAN_DIR)
132  SET(OPENJPEG_INSTALL_MAN_DIR "share/man/")
133ENDIF(NOT OPENJPEG_INSTALL_MAN_DIR)
134
135IF(NOT OPENJPEG_INSTALL_DOC_DIR)
136  SET(OPENJPEG_INSTALL_DOC_DIR "share/doc/${OPENJPEG_INSTALL_SUBDIR}")
137ENDIF(NOT OPENJPEG_INSTALL_DOC_DIR)
138
139if(NOT OPENJPEG_INSTALL_JNI_DIR)
140  if(WIN32)
141    set(OPENJPEG_INSTALL_JNI_DIR ${OPENJPEG_INSTALL_BIN_DIR})
142  else()
143    set(OPENJPEG_INSTALL_JNI_DIR ${OPENJPEG_INSTALL_LIB_DIR})
144  endif()
145endif()
146
147IF(NOT OPENJPEG_INSTALL_PACKAGE_DIR)
148  # We could install *.cmake files in share/ however those files contains
149  # hardcoded path to libraries on a multi-arch system (fedora/debian) those
150  # path will be different (lib/i386-linux-gnu vs lib/x86_64-linux-gnu)
151  SET(OPENJPEG_INSTALL_PACKAGE_DIR "${OPENJPEG_INSTALL_LIB_DIR}/${OPENJPEG_INSTALL_SUBDIR}")
152ENDIF(NOT OPENJPEG_INSTALL_PACKAGE_DIR)
153
154#-----------------------------------------------------------------------------
155# Big endian test:
156INCLUDE (${CMAKE_ROOT}/Modules/TestBigEndian.cmake)
157TEST_BIG_ENDIAN(OPJ_BIG_ENDIAN)
158
159#-----------------------------------------------------------------------------
160# Setup file for setting custom ctest vars
161CONFIGURE_FILE(
162  ${CMAKE_SOURCE_DIR}/CMake/CTestCustom.cmake.in
163  ${CMAKE_BINARY_DIR}/CTestCustom.cmake
164  @ONLY
165  )
166
167#-----------------------------------------------------------------------------
168# OpenJPEG build configuration options.
169OPTION(BUILD_SHARED_LIBS "Build OpenJPEG shared library and link executables against it." ON)
170SET (EXECUTABLE_OUTPUT_PATH ${OPENJPEG_BINARY_DIR}/bin CACHE PATH "Single output directory for building all executables.")
171SET (LIBRARY_OUTPUT_PATH ${OPENJPEG_BINARY_DIR}/bin CACHE PATH "Single output directory for building all libraries.")
172MARK_AS_ADVANCED(LIBRARY_OUTPUT_PATH EXECUTABLE_OUTPUT_PATH)
173
174#-----------------------------------------------------------------------------
175# configure name mangling to allow multiple libraries to coexist
176# peacefully
177IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/openjpeg_mangle.h.in)
178SET(MANGLE_PREFIX ${OPENJPEG_LIBRARY_NAME})
179CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/openjpeg_mangle.h.in
180               ${CMAKE_CURRENT_BINARY_DIR}/openjpeg_mangle.h
181               @ONLY)
182ENDIF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/openjpeg_mangle.h.in)
183
184#-----------------------------------------------------------------------------
185# pkgconfig support
186IF(UNIX)
187  # install in lib and not share (see multi-arch note above)
188  CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libopenjpeg1.pc.cmake
189    ${CMAKE_CURRENT_BINARY_DIR}/libopenjpeg1.pc @ONLY)
190  INSTALL( FILES  ${CMAKE_CURRENT_BINARY_DIR}/libopenjpeg1.pc DESTINATION
191    ${OPENJPEG_INSTALL_LIB_DIR}/pkgconfig )
192  INSTALL( CODE "EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E create_symlink
193  \"libopenjpeg1.pc\"
194  \"\$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/${OPENJPEG_INSTALL_SHARE_DIR}/pkgconfig/libopenjpeg.pc\")")
195ENDIF(UNIX)
196
197#-----------------------------------------------------------------------------
198# Compiler specific flags:
199IF(CMAKE_COMPILER_IS_GNUCC)
200  # For all builds, make sure openjpeg is std99 compliant:
201  # SET(CMAKE_C_FLAGS "-Wall -std=c99 ${CMAKE_C_FLAGS}") # FIXME: this setting prevented us from setting a coverage build.
202  # Do not use ffast-math for all build, it would produce incorrect results, only set for release:
203  SET(CMAKE_C_FLAGS_RELEASE "-ffast-math ${CMAKE_C_FLAGS_RELEASE}")
204ENDIF(CMAKE_COMPILER_IS_GNUCC)
205
206#-----------------------------------------------------------------------------
207# opj_config.h generation (1/2)
208INCLUDE (${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake)
209CHECK_INCLUDE_FILE("strings.h"      HAVE_STRINGS_H)
210CHECK_INCLUDE_FILE("inttypes.h"     HAVE_INTTYPES_H)
211CHECK_INCLUDE_FILE("memory.h"       HAVE_MEMORY_H)
212CHECK_INCLUDE_FILE("stdint.h"       HAVE_STDINT_H)
213CHECK_INCLUDE_FILE("stdlib.h"       HAVE_STDLIB_H)
214CHECK_INCLUDE_FILE("string.h"       HAVE_STRING_H)
215CHECK_INCLUDE_FILE("sys/stat.h"     HAVE_SYS_STAT_H)
216CHECK_INCLUDE_FILE("sys/types.h"    HAVE_SYS_TYPES_H)
217CHECK_INCLUDE_FILE("unistd.h"       HAVE_UNISTD_H)
218
219
220#-----------------------------------------------------------------------------
221# Build Library
222INCLUDE_DIRECTORIES(BEFORE ${OPENJPEG_BINARY_DIR})
223ADD_SUBDIRECTORY(libopenjpeg)
224
225#-----------------------------------------------------------------------------
226# Build Applications
227OPTION(BUILD_CODEC "Build the CODEC executables" ON)
228OPTION(BUILD_MJ2 "Build the MJ2 executables." OFF)
229OPTION(BUILD_JPWL "Build the JPWL library and executables" OFF)
230OPTION(BUILD_JPIP "Build the JPIP library and executables." OFF)
231IF(BUILD_JPIP)
232  OPTION(BUILD_JPIP_SERVER "Build the JPIP server." OFF)
233ENDIF(BUILD_JPIP)
234OPTION(BUILD_VIEWER "Build the OPJViewer executable (C++)" OFF)
235OPTION(BUILD_JAVA "Build the openjpeg jar (Java)" OFF)
236OPTION(USE_SYSTEM_GETOPT "Prefer system installed getopt()" OFF)
237MARK_AS_ADVANCED(USE_SYSTEM_GETOPT)
238MARK_AS_ADVANCED(BUILD_VIEWER)
239MARK_AS_ADVANCED(BUILD_JAVA)
240
241IF(BUILD_CODEC OR BUILD_MJ2)
242  # OFF: It will only build 3rd party libs if they are not found on the system
243  # ON: 3rd party libs will ALWAYS be build, and used
244  OPTION(BUILD_THIRDPARTY "Build the thirdparty executables if it is needed" OFF)
245  ADD_SUBDIRECTORY(thirdparty)
246  ADD_SUBDIRECTORY(applications)
247ENDIF (BUILD_CODEC OR BUILD_MJ2)
248
249#-----------------------------------------------------------------------------
250# opj_config.h generation (2/2)
251CONFIGURE_FILE("${OPENJPEG_SOURCE_DIR}/opj_config.h.cmake.in"
252 "${OPENJPEG_BINARY_DIR}/opj_config.h"
253 @ONLY
254 )
255
256#-----------------------------------------------------------------------------
257# Build DOCUMENTATION (not in ALL target and only if Doxygen is found)
258OPTION(BUILD_DOC "Build the HTML documentation (with doxygen if available)." OFF)
259IF(BUILD_DOC)
260    ADD_SUBDIRECTORY(doc)
261ENDIF(BUILD_DOC)
262
263#-----------------------------------------------------------------------------
264# Buld Testing
265OPTION(BUILD_TESTING "Build the tests." OFF)
266IF(BUILD_TESTING)
267  IF(BUILD_CODEC)
268    ENABLE_TESTING()
269    INCLUDE(CTest)
270
271    # Search openjpeg data needed for the tests
272    # They could be found via svn on the OpenJPEG google code project
273    # svn checkout http://openjpeg.googlecode.com/svn/data (about 70 Mo)
274    FIND_PATH(OPJ_DATA_ROOT README-OPJ-Data
275      PATHS $ENV{OPJ_DATA_ROOT} ${CMAKE_SOURCE_DIR}/../data
276        ${CMAKE_SOURCE_DIR}/../../data
277      NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH
278      )
279
280    # Add repository where to find tests
281    ADD_SUBDIRECTORY(tests)
282
283  ELSE(BUILD_CODEC)
284    message(FATAL_ERROR "You need build codec to run the tests")
285  ENDIF(BUILD_CODEC)
286ENDIF(BUILD_TESTING)
287
288#-----------------------------------------------------------------------------
289# install all targets referenced as OPENJPEGTargets
290INSTALL(EXPORT OpenJPEGTargets DESTINATION ${OPENJPEG_INSTALL_PACKAGE_DIR})
291CONFIGURE_FILE( ${OPENJPEG_SOURCE_DIR}/CMake/OpenJPEGConfig.cmake.in
292  ${OPENJPEG_BINARY_DIR}/OpenJPEGConfig.cmake
293  @ONLY
294)
295INSTALL( FILES ${OPENJPEG_BINARY_DIR}/OpenJPEGConfig.cmake
296  DESTINATION ${OPENJPEG_INSTALL_PACKAGE_DIR}
297)
298
299#-----------------------------------------------------------------------------
300# install CHANGES and LICENSE
301IF(EXISTS ${OPENJPEG_SOURCE_DIR}/CHANGES)
302  INSTALL(FILES CHANGES DESTINATION ${OPENJPEG_INSTALL_DOC_DIR})
303ENDIF(EXISTS ${OPENJPEG_SOURCE_DIR}/CHANGES)
304INSTALL(FILES LICENSE DESTINATION ${OPENJPEG_INSTALL_DOC_DIR})
305
306INCLUDE (CMake/OpenJPEGCPack.cmake)
307