]> git.cworth.org Git - apitrace/blob - CMakeLists.txt
Always use bundled QJson.
[apitrace] / CMakeLists.txt
1 cmake_minimum_required (VERSION 2.8)
2
3 project (apitrace)
4
5
6 ##############################################################################
7 # Options
8
9 # On Mac OS X build fat binaries with i386 and x86_64 architectures by default.
10 if (APPLE AND NOT CMAKE_OSX_ARCHITECTURES)
11     set (CMAKE_OSX_ARCHITECTURES "i386;x86_64" CACHE STRING "Build architectures for OSX" FORCE)
12 endif ()
13
14 # We use a cached string variable instead of the standard (boolean) OPTION
15 # command so that we can default to auto-detecting optional depencies, while
16 # still providing a mechanism to force/disable these optional dependencies, as
17 # prescribed in http://www.gentoo.org/proj/en/qa/automagic.xml
18 set (ENABLE_GUI "AUTO" CACHE STRING "Enable Qt GUI.")
19
20 set (ENABLE_CLI true CACHE BOOL "Enable command Line interface.")
21
22 set (ENABLE_EGL true CACHE BOOL "Enable EGL support.")
23
24
25 ##############################################################################
26 # Find dependencies
27
28 set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
29
30 set (CMAKE_USE_PYTHON_VERSION 2.7 2.6)
31
32 if (ANDROID)
33     set (ENABLE_GUI false)
34 else ()
35     macro (find_host_package)
36         find_package (${ARGN})
37     endmacro()
38 endif ()
39
40 find_host_package (PythonInterp REQUIRED)
41 find_package (Threads)
42
43 if (ENABLE_GUI)
44     if (NOT (ENABLE_GUI STREQUAL "AUTO"))
45         set (REQUIRE_GUI REQUIRED)
46     endif ()
47     find_package (Qt4 4.7 COMPONENTS QtCore QtGui QtWebKit ${REQUIRE_GUI})
48 endif ()
49
50 if (WIN32)
51     find_package (DirectX)
52     set (ENABLE_EGL false)
53 elseif (APPLE)
54     set (ENABLE_EGL false)
55 else ()
56     find_package (X11)
57
58     if (X11_FOUND)
59         include_directories (${X11_INCLUDE_DIR})
60         add_definitions (-DHAVE_X11)
61     endif ()
62 endif ()
63
64
65 ##############################################################################
66 # Set global build options
67
68 include (CheckCXXCompilerFlag)
69
70 if (WIN32)
71     # http://msdn.microsoft.com/en-us/library/aa383745.aspx
72     add_definitions (-D_WIN32_WINNT=0x0601 -DWINVER=0x0601)
73 else (WIN32)
74     CHECK_CXX_COMPILER_FLAG("-fvisibility=hidden" CXX_COMPILER_FLAG_VISIBILITY)
75     if (CXX_COMPILER_FLAG_VISIBILITY)
76         add_definitions ("-fvisibility=hidden")
77     endif ()
78 endif ()
79
80 if (MSVC)
81     # C99 includes for msvc
82     include_directories (${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/msvc)
83
84     # Enable math constants defines
85     add_definitions (-D_USE_MATH_DEFINES)
86
87     # No min/max macros
88     add_definitions (-DNOMINMAX)
89
90     # Adjust warnings
91     add_definitions (-D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS)
92     add_definitions (-D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS)
93     add_definitions (-W4)
94     # XXX: it's safer to use ssize_t everywhere instead of disabling warning
95     add_definitions (-wd4018) # signed/unsigned mismatch
96     add_definitions (-wd4063) # not a valid value for switch of enum
97     add_definitions (-wd4100) # unreferenced formal parameter
98     add_definitions (-wd4127) # conditional expression is constant
99     add_definitions (-wd4244) # conversion from 'type1' to 'type2', possible loss of data
100     add_definitions (-wd4505) # unreferenced local function has been removed
101     add_definitions (-wd4512) # assignment operator could not be generated
102     add_definitions (-wd4800) # forcing value to bool 'true' or 'false' (performance warning)
103     
104     # Use static runtime
105     # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
106     foreach (flag_var
107         CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
108         CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
109     )
110         if (${flag_var} MATCHES "/MD")
111             string (REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
112         endif ()
113     endforeach (flag_var)
114 else ()
115     # Adjust warnings
116     add_definitions (-Wall)
117     # XXX: it's safer to use ssize_t everywhere instead of disabling warning
118     add_definitions (-Wno-sign-compare) # comparison between signed and unsigned integer expressions
119
120     # Use GDB extensions if available
121     if (CMAKE_COMPILER_IS_GNUC)
122         set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ggdb -O0")
123         set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} -ggdb")
124     endif ()
125     if (CMAKE_COMPILER_IS_GNUCXX)
126         set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ggdb -O0")
127         set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -ggdb")
128     endif ()
129
130     # Be nice to Eclipse
131     add_definitions (-fmessage-length=0)
132 endif ()
133
134 if (MINGW)
135     # Avoid depending on MinGW runtime DLLs
136     check_cxx_compiler_flag (-static-libgcc HAVE_STATIC_LIBGCC_FLAG)
137     if (HAVE_STATIC_LIBGCC_FLAG)
138         set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc")
139         set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libgcc")
140         set (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -static-libgcc")
141     endif ()
142     check_cxx_compiler_flag (-static-libstdc++ HAVE_STATIC_LIBSTDCXX_FLAG)
143     if (HAVE_STATIC_LIBSTDCXX_FLAG)
144         set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++")
145         set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libstdc++")
146         set (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -static-libstdc++")
147     endif ()
148 endif ()
149
150
151 # Put all executables into the same top level build directory, regardless of
152 # which subdirectory they are declared
153 set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
154
155
156 ##############################################################################
157 # Bundled dependencies
158 #
159 # We always use the bundled zlib, libpng, and snappy sources:
160 # - on Windows to make it easy to deploy the wrappers DLLs
161 # - on unices to prevent symbol collisions when tracing applications that link
162 # against other versions of these libraries
163
164 set (ZLIB_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/zlib)
165 set (ZLIB_LIBRARIES z_bundled)
166 add_subdirectory (thirdparty/zlib EXCLUDE_FROM_ALL)
167
168 include_directories (${ZLIB_INCLUDE_DIRS})
169
170 set (SNAPPY_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/snappy)
171 set (SNAPPY_LIBRARIES snappy_bundled)
172 add_subdirectory (thirdparty/snappy EXCLUDE_FROM_ALL)
173
174 include_directories (${SNAPPY_INCLUDE_DIRS})
175
176 set (PNG_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libpng)
177 set (PNG_DEFINITIONS "")
178 set (PNG_LIBRARIES png_bundled)
179
180 add_subdirectory (thirdparty/libpng EXCLUDE_FROM_ALL)
181 include_directories (${PNG_INCLUDE_DIR})
182 add_definitions (${PNG_DEFINITIONS})
183
184 if (MSVC)
185     add_subdirectory (thirdparty/getopt EXCLUDE_FROM_ALL)
186     include_directories (${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/getopt)
187     set (GETOPT_LIBRARIES getopt_bundled)
188 endif ()
189
190 if (WIN32)
191     add_subdirectory (thirdparty/less)
192 endif ()
193
194 # Always use bundled QJSon.
195 # - The packaged versions QJson are very old, and do not support NaN/Infinity.
196 # - To make it easier to build the GUI on Windows and MacOSX, as there are no
197 # binaries at all.
198 if (QT4_FOUND)
199     add_subdirectory (thirdparty/qjson EXCLUDE_FROM_ALL)
200     set (QJSON_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty)
201     set (QJSON_LIBRARY_DIRS)
202     set (QJSON_LIBRARIES qjson_bundled)
203     set (QJSON_FOUND TRUE)
204 endif ()
205
206 # We use bundled headers for all Khronos APIs, to guarantee support for both
207 # OpenGL and OpenGL ES at build time, because the OpenGL and OpenGL ES 1 APIs
208 # are so intertwined that conditional compilation extremely difficult. This
209 # also avoids missing/inconsistent declarations in system headers.
210 include_directories (BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/khronos)
211
212
213 ##############################################################################
214 # Installation directories
215
216 if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
217     # Debian multiarch support
218     execute_process(COMMAND dpkg-architecture -qDEB_HOST_MULTIARCH
219         OUTPUT_VARIABLE ARCH_SUBDIR
220         ERROR_QUIET
221         OUTPUT_STRIP_TRAILING_WHITESPACE
222     )
223 endif()
224
225 if (WIN32 OR APPLE)
226     # On Windows/MacOSX, applications are usually installed on a directory of
227     # their own
228     set (DOC_INSTALL_DIR doc)
229     set (LIB_INSTALL_DIR lib)
230     set (LIB_ARCH_INSTALL_DIR lib)
231 else ()
232     set (DOC_INSTALL_DIR share/doc/${CMAKE_PROJECT_NAME})
233     set (LIB_INSTALL_DIR lib/${CMAKE_PROJECT_NAME})
234     if (ARCH_SUBDIR)
235         set (LIB_ARCH_INSTALL_DIR lib/${ARCH_SUBDIR}/${CMAKE_PROJECT_NAME})
236     else ()
237         set (LIB_ARCH_INSTALL_DIR lib/${CMAKE_PROJECT_NAME})
238     endif ()
239 endif ()
240
241 set (SCRIPTS_INSTALL_DIR ${LIB_INSTALL_DIR}/scripts)
242 set (WRAPPER_INSTALL_DIR ${LIB_ARCH_INSTALL_DIR}/wrappers)
243
244 # Expose the binary/install directories to source
245 #
246 # TODO: Use the same directory layout, for both build and install directories,
247 # so that binaries can find each other using just relative paths.
248 #
249 add_definitions(
250     -DAPITRACE_BINARY_DIR="${CMAKE_BINARY_DIR}"
251     -DAPITRACE_INSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}"
252     -DAPITRACE_SCRIPTS_INSTALL_DIR="${CMAKE_INSTALL_PREFIX}/${SCRIPTS_INSTALL_DIR}"
253     -DAPITRACE_WRAPPERS_INSTALL_DIR="${CMAKE_INSTALL_PREFIX}/${WRAPPER_INSTALL_DIR}"
254 )
255
256
257 ##############################################################################
258 # Common libraries / utilities
259
260 include_directories (
261     ${CMAKE_CURRENT_BINARY_DIR}
262     ${CMAKE_CURRENT_SOURCE_DIR}
263     ${CMAKE_CURRENT_SOURCE_DIR}/common
264 )
265
266 if (WIN32)
267     set (os os_win32.cpp)
268     set (glws_os glws_wgl.cpp)
269 else ()
270     set (os os_posix.cpp)
271     if (APPLE)
272         set (glws_os glws_cocoa.mm)
273     else ()
274         set (glws_os glws_glx.cpp)
275     endif ()
276 endif ()
277
278 add_library (common STATIC
279     common/trace_callset.cpp
280     common/trace_dump.cpp
281     common/trace_file.cpp
282     common/trace_file_read.cpp
283     common/trace_file_write.cpp
284     common/trace_file_zlib.cpp
285     common/trace_file_snappy.cpp
286     common/trace_model.cpp
287     common/trace_parser.cpp
288     common/trace_parser_flags.cpp
289     common/trace_writer.cpp
290     common/trace_writer_local.cpp
291     common/trace_writer_model.cpp
292     common/trace_loader.cpp
293     common/trace_resource.cpp
294     common/trace_tools_trace.cpp
295     common/trace_profiler.cpp
296     common/image.cpp
297     common/image_bmp.cpp
298     common/image_pnm.cpp
299     common/image_png.cpp
300     common/${os}
301 )
302
303 set_target_properties (common PROPERTIES
304     COMPILE_DEFINITIONS APITRACE_SOURCE_DIR="${CMAKE_SOURCE_DIR}"
305     # Ensure it can be statically linked in shared libraries
306     COMPILE_FLAGS "${CMAKE_SHARED_LIBRARY_CXX_FLAGS}"
307 )
308
309 if (ANDROID)
310     target_link_libraries (common log)
311 endif ()
312
313
314 ##############################################################################
315 # Sub-directories
316
317 add_subdirectory (dispatch)
318 add_subdirectory (wrappers)
319 add_subdirectory (retrace)
320
321
322 ##############################################################################
323 # CLI
324
325 if (ENABLE_CLI)
326     add_subdirectory(cli)
327 endif ()
328
329 ##############################################################################
330 # Scripts (to support the CLI)
331
332 install (
333     PROGRAMS
334         ${CMAKE_CURRENT_SOURCE_DIR}/scripts/tracediff.py
335         ${CMAKE_CURRENT_SOURCE_DIR}/scripts/jsondiff.py
336         ${CMAKE_CURRENT_SOURCE_DIR}/scripts/snapdiff.py
337     DESTINATION ${SCRIPTS_INSTALL_DIR}
338 )
339
340 ##############################################################################
341 # GUI
342
343 if (ENABLE_GUI AND QT4_FOUND AND QJSON_FOUND)
344     add_subdirectory(gui)
345 endif ()
346
347
348 ##############################################################################
349 # Packaging
350
351 install (
352     FILES
353         BUGS.markdown
354         LICENSE
355         NEWS.markdown
356         README.markdown
357     DESTINATION ${DOC_INSTALL_DIR}
358 )
359
360 set (CPACK_PACKAGE_VERSION_MAJOR "3")
361 set (CPACK_PACKAGE_VERSION_MINOR "0")
362
363 # Use current date in YYYYMMDD format as patch number 
364 execute_process (
365     COMMAND ${PYTHON_EXECUTABLE} -c "import time, sys; sys.stdout.write(time.strftime('%Y%m%d'))"
366     OUTPUT_VARIABLE CPACK_PACKAGE_VERSION_PATCH
367 )
368
369 # cpack mistakenly detects Mingw-w64 as win32
370 if (MINGW)
371     if (CMAKE_SIZEOF_VOID_P EQUAL 8)
372         set (CPACK_SYSTEM_NAME win64)
373     endif ()
374 endif ()
375
376 # See http://www.vtk.org/Wiki/CMake:CPackPackageGenerators
377 if (WIN32)
378     set (CPACK_GENERATOR "ZIP")
379 elseif (APPLE)
380     set (CPACK_GENERATOR "DragNDrop")
381 else ()
382     set (CPACK_GENERATOR "TBZ2")
383 endif ()
384
385 include(CPack)