]> git.cworth.org Git - apitrace/blob - CMakeLists.txt
Update android.toolchain.cmake to latest from android-cmake project.
[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     find_package (QJSON ${REQUIRE_GUI})
49 endif ()
50
51 if (WIN32)
52     find_package (DirectX)
53     set (ENABLE_EGL false)
54 elseif (APPLE)
55     set (ENABLE_EGL false)
56 else ()
57     find_package (X11)
58
59     if (X11_FOUND)
60         include_directories (${X11_INCLUDE_DIR})
61         add_definitions (-DHAVE_X11)
62     endif ()
63
64     if (ENABLE_EGL)
65         add_definitions (-DHAVE_EGL)
66     endif ()
67 endif ()
68
69
70 ##############################################################################
71 # Set global build options
72
73 include (CheckCXXCompilerFlag)
74
75 if (WIN32)
76     # http://msdn.microsoft.com/en-us/library/aa383745.aspx
77     add_definitions (-D_WIN32_WINNT=0x0500 -DWINVER=0x0500)
78 else (WIN32)
79     CHECK_CXX_COMPILER_FLAG("-fvisibility=hidden" CXX_COMPILER_FLAG_VISIBILITY)
80     if (CXX_COMPILER_FLAG_VISIBILITY)
81         add_definitions ("-fvisibility=hidden")
82     endif (CXX_COMPILER_FLAG_VISIBILITY)
83 endif (WIN32)
84
85 if (MSVC)
86     # C99 includes for msvc
87     include_directories (${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/msvc)
88
89     # Enable math constants defines
90     add_definitions (-D_USE_MATH_DEFINES)
91
92     # No min/max macros
93     add_definitions (-DNOMINMAX)
94
95     # Adjust warnings
96     add_definitions (-D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS)
97     add_definitions (-D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS)
98     add_definitions (-W4)
99     add_definitions (-wd4063) # not a valid value for switch of enum
100     add_definitions (-wd4127) # conditional expression is constant
101     add_definitions (-wd4244) # conversion from 'type1' to 'type2', possible loss of data
102     add_definitions (-wd4505) # unreferenced local function has been removed
103     add_definitions (-wd4800) # forcing value to bool 'true' or 'false' (performance warning)
104     # XXX: it's safer to use ssize_t everywhere instead of disabling warning
105     add_definitions (-wd4018) # signed/unsigned mismatch
106     
107     # Use static runtime
108     # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
109     foreach (flag_var
110         CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
111         CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
112     )
113         if (${flag_var} MATCHES "/MD")
114             string (REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
115         endif (${flag_var} MATCHES "/MD")
116     endforeach (flag_var)
117 else ()
118     # Adjust warnings
119     add_definitions (-Wall)
120     # XXX: it's safer to use ssize_t everywhere instead of disabling warning
121     add_definitions (-Wno-sign-compare) # comparison between signed and unsigned integer expressions
122 endif ()
123
124 if (MINGW)
125     # Avoid depending on MinGW runtime DLLs
126     check_cxx_compiler_flag (-static-libgcc HAVE_STATIC_LIBGCC_FLAG)
127     if (HAVE_STATIC_LIBGCC_FLAG)
128         set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc")
129         set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libgcc")
130         set (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -static-libgcc")
131     endif ()
132     check_cxx_compiler_flag (-static-libstdc++ HAVE_STATIC_LIBSTDCXX_FLAG)
133     if (HAVE_STATIC_LIBSTDCXX_FLAG)
134         set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++")
135         set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libstdc++")
136         set (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -static-libstdc++")
137     endif ()
138 endif ()
139
140
141 # Put all executables into the same top level build directory, regardless of
142 # which subdirectory they are declared
143 set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
144
145
146 ##############################################################################
147 # Bundled dependencies
148 #
149 # We always use the bundled zlib, libpng, and snappy sources:
150 # - on Windows to make it easy to deploy the wrappers DLLs
151 # - on unices to prevent symbol collisions when tracing applications that link
152 # against other versions of these libraries
153
154 set (ZLIB_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/zlib)
155 set (ZLIB_LIBRARIES z_bundled)
156 add_subdirectory (thirdparty/zlib EXCLUDE_FROM_ALL)
157
158 include_directories (${ZLIB_INCLUDE_DIRS})
159
160 set (SNAPPY_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/snappy)
161 set (SNAPPY_LIBRARIES snappy_bundled)
162 add_subdirectory (thirdparty/snappy EXCLUDE_FROM_ALL)
163
164 include_directories (${SNAPPY_INCLUDE_DIRS})
165
166 set (PNG_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libpng)
167 set (PNG_DEFINITIONS "")
168 set (PNG_LIBRARIES png_bundled)
169
170 add_subdirectory (thirdparty/libpng EXCLUDE_FROM_ALL)
171 include_directories (${PNG_INCLUDE_DIR})
172 add_definitions (${PNG_DEFINITIONS})
173
174 if (MSVC)
175     add_subdirectory (thirdparty/getopt EXCLUDE_FROM_ALL)
176     include_directories (${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/getopt)
177     set (GETOPT_LIBRARIES getopt_bundled)
178 endif ()
179
180 if (WIN32)
181     add_subdirectory (thirdparty/less)
182 endif ()
183
184 # The Qt website provides binaries for Windows and MacOSX, and they are
185 # automatically found by cmake without any manual intervention.  The situation
186 # for QJSON is substantially different: there are no binaries for QJSON
187 # available, and there is no standard installation directory that is detected
188 # by cmake.
189 #
190 # By bundling the QJSON source, we make it much more easier to build the GUI on
191 # Windows and MacOSX.  But we only use the bundled sources when ENABLE_GUI is
192 # AUTO.
193 if (QT4_FOUND AND NOT QJSON_FOUND AND (ENABLE_GUI STREQUAL "AUTO"))
194     add_subdirectory (thirdparty/qjson EXCLUDE_FROM_ALL)
195     set (QJSON_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty)
196     set (QJSON_LIBRARY_DIRS)
197     set (QJSON_LIBRARIES qjson_bundled)
198     set (QJSON_FOUND TRUE)
199 endif ()
200
201 # We use bundled headers for all Khronos APIs, to guarantee support for both
202 # OpenGL and OpenGL ES at build time, because the OpenGL and OpenGL ES 1 APIs
203 # are so intertwined that conditional compilation extremely difficult. This
204 # also avoids missing/inconsistent declarations in system headers.
205 include_directories (BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/khronos)
206
207 ##############################################################################
208 # Installation directories
209
210 if (WIN32 OR APPLE)
211     # On Windows/MacOSX, applications are usually installed on a directory of
212     # their own
213     set (DOC_INSTALL_DIR doc)
214     set (LIB_INSTALL_DIR lib)
215 else ()
216     set (DOC_INSTALL_DIR share/doc/${CMAKE_PROJECT_NAME})
217     set (LIB_INSTALL_DIR lib/${CMAKE_PROJECT_NAME})
218 endif ()
219
220 set(SCRIPTS_INSTALL_DIR ${LIB_INSTALL_DIR}/scripts)
221 set(WRAPPER_INSTALL_DIR ${LIB_INSTALL_DIR}/wrappers)
222
223 # Expose the binary/install directories to source
224 #
225 # TODO: Use the same directory layout, for both build and install directories,
226 # so that binaries can find each other using just relative paths.
227 #
228 add_definitions(
229     -DAPITRACE_BINARY_DIR="${CMAKE_BINARY_DIR}"
230     -DAPITRACE_INSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}"
231     -DAPITRACE_WRAPPER_INSTALL_DIR="${CMAKE_INSTALL_PREFIX}/${WRAPPER_INSTALL_DIR}"
232 )
233
234
235 ##############################################################################
236 # Common libraries / utilities
237
238 include_directories (
239     ${CMAKE_CURRENT_BINARY_DIR}
240     ${CMAKE_CURRENT_SOURCE_DIR}
241     ${CMAKE_CURRENT_SOURCE_DIR}/common
242 )
243
244 add_custom_command (
245     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp
246     COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/glproc.py > ${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp
247     DEPENDS glproc.py dispatch.py specs/wglapi.py specs/glxapi.py specs/cglapi.py specs/eglapi.py specs/glesapi.py specs/glapi.py specs/gltypes.py specs/stdapi.py
248 )
249
250 # Wrap glproc.hpp as a target to prevent the command from being executed
251 # multiple times simulatenously, when the targets that depend on it are built
252 # in parallel.
253 add_custom_target (glproc DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp)
254
255 if (WIN32)
256     set (os os_win32.cpp)
257     set (glws_os glws_wgl.cpp)
258 else ()
259     set (os os_posix.cpp)
260     if (APPLE)
261         set (glws_os glws_cocoa.mm)
262     else ()
263         set (glws_os glws_glx.cpp)
264     endif ()
265 endif ()
266
267 add_library (common STATIC
268     common/trace_callset.cpp
269     common/trace_dump.cpp
270     common/trace_file.cpp
271     common/trace_file_read.cpp
272     common/trace_file_write.cpp
273     common/trace_file_zlib.cpp
274     common/trace_file_snappy.cpp
275     common/trace_model.cpp
276     common/trace_parser.cpp
277     common/trace_parser_flags.cpp
278     common/trace_writer.cpp
279     common/trace_writer_local.cpp
280     common/trace_writer_model.cpp
281     common/trace_loader.cpp
282     common/trace_resource.cpp
283     common/trace_tools_trace.cpp
284     common/image.cpp
285     common/image_bmp.cpp
286     common/image_pnm.cpp
287     common/image_png.cpp
288     common/${os}
289 )
290
291 set_target_properties (common PROPERTIES
292     COMPILE_DEFINITIONS APITRACE_SOURCE_DIR="${CMAKE_SOURCE_DIR}"
293     # Ensure it can be statically linked in shared libraries
294     COMPILE_FLAGS "${CMAKE_SHARED_LIBRARY_CXX_FLAGS}"
295 )
296
297
298 ##############################################################################
299 # API tracers
300
301 if (WIN32)
302     if (MINGW)
303         # Silence warnings about @nn suffix mismatch
304         set (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--enable-stdcall-fixup")
305     endif (MINGW)
306
307     # ddraw.dll
308     if (DirectX_D3D_INCLUDE_DIR)
309         include_directories (SYSTEM ${DirectX_D3D_INCLUDE_DIR})
310         add_custom_command (
311             OUTPUT ddrawtrace.cpp
312             COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/ddrawtrace.py > ${CMAKE_CURRENT_BINARY_DIR}/ddrawtrace.cpp
313             DEPENDS ddrawtrace.py trace.py specs/d3d.py specs/d3dtypes.py specs/d3dcaps.py specs/ddraw.py specs/winapi.py specs/stdapi.py
314         )
315         add_library (ddraw MODULE specs/ddraw.def ddrawtrace.cpp)
316         target_link_libraries (ddraw
317             common
318             ${ZLIB_LIBRARIES}
319             ${SNAPPY_LIBRARIES}
320         )
321         set_target_properties (ddraw
322             PROPERTIES PREFIX ""
323             RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
324             LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
325         )
326         install (TARGETS ddraw LIBRARY DESTINATION ${WRAPPER_INSTALL_DIR})
327     endif (DirectX_D3D_INCLUDE_DIR)
328
329     # d3d8.dll
330     if (DirectX_D3D8_INCLUDE_DIR AND DirectX_D3DX9_INCLUDE_DIR)
331         include_directories (SYSTEM ${DirectX_D3D8_INCLUDE_DIR} ${DirectX_D3DX9_INCLUDE_DIR})
332         add_custom_command (
333             OUTPUT d3d8trace.cpp
334             COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/d3d8trace.py > ${CMAKE_CURRENT_BINARY_DIR}/d3d8trace.cpp
335             DEPENDS d3d8trace.py trace.py specs/d3d8.py specs/d3d8types.py specs/d3d8caps.py specs/winapi.py specs/stdapi.py
336         )
337         add_library (d3d8 MODULE specs/d3d8.def d3d8trace.cpp d3dshader.cpp)
338         target_link_libraries (d3d8
339             common
340             ${ZLIB_LIBRARIES}
341             ${SNAPPY_LIBRARIES}
342         )
343         set_target_properties (d3d8
344             PROPERTIES PREFIX ""
345             RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
346             LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
347         )
348         install (TARGETS d3d8 LIBRARY DESTINATION ${WRAPPER_INSTALL_DIR})
349     endif (DirectX_D3D8_INCLUDE_DIR AND DirectX_D3DX9_INCLUDE_DIR)
350
351     # d3d9.dll
352     if (DirectX_D3DX9_INCLUDE_DIR)
353         include_directories (SYSTEM ${DirectX_D3DX9_INCLUDE_DIR})
354         add_custom_command (
355             OUTPUT d3d9trace.cpp
356             COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/d3d9trace.py > ${CMAKE_CURRENT_BINARY_DIR}/d3d9trace.cpp
357             DEPENDS d3d9trace.py trace.py specs/d3d9.py specs/d3d9types.py specs/d3d9caps.py specs/winapi.py specs/stdapi.py
358         )
359         add_library (d3d9 MODULE specs/d3d9.def d3d9trace.cpp d3dshader.cpp)
360         target_link_libraries (d3d9
361             common
362             ${ZLIB_LIBRARIES}
363             ${SNAPPY_LIBRARIES}
364         )
365         set_target_properties (d3d9
366             PROPERTIES PREFIX ""
367             RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
368             LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
369         )
370         install (TARGETS d3d9 LIBRARY DESTINATION ${WRAPPER_INSTALL_DIR})
371     endif (DirectX_D3DX9_INCLUDE_DIR)
372
373     # d3d10.dll
374     if (DirectX_D3D10_INCLUDE_DIR)
375         include_directories (SYSTEM ${DirectX_D3D10_INCLUDE_DIR})
376         add_custom_command (
377             OUTPUT d3d10trace.cpp
378             COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/d3d10trace.py > ${CMAKE_CURRENT_BINARY_DIR}/d3d10trace.cpp
379             DEPENDS d3d10trace.py trace.py specs/d3d10misc.py specs/d3d10.py specs/dxgi.py specs/dxgitype.py specs/dxgiformat.py specs/winapi.py specs/stdapi.py
380         )
381         add_library (d3d10 MODULE specs/d3d10.def d3d10trace.cpp)
382         target_link_libraries (d3d10
383             common
384             ${ZLIB_LIBRARIES}
385             ${SNAPPY_LIBRARIES}
386         )
387         set_target_properties (d3d10
388             PROPERTIES PREFIX ""
389             RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
390             LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
391         )
392         install (TARGETS d3d10 LIBRARY DESTINATION ${WRAPPER_INSTALL_DIR})
393     endif (DirectX_D3D10_INCLUDE_DIR)
394
395     # opengl32.dll
396     add_custom_command (
397         OUTPUT wgltrace.cpp
398         COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/wgltrace.py > ${CMAKE_CURRENT_BINARY_DIR}/wgltrace.cpp
399         DEPENDS wgltrace.py gltrace.py trace.py specs/wglapi.py specs/wglenum.py specs/glapi.py specs/glparams.py specs/gltypes.py specs/winapi.py specs/stdapi.py
400     )
401     add_library (wgltrace MODULE specs/opengl32.def
402         wgltrace.cpp
403         glcaps.cpp
404         glproc_gl.cpp
405     )
406     add_dependencies (wgltrace glproc)
407     target_link_libraries (wgltrace
408         common
409         ${ZLIB_LIBRARIES}
410         ${SNAPPY_LIBRARIES}
411     )
412     set_target_properties (wgltrace PROPERTIES
413         PREFIX ""
414         OUTPUT_NAME opengl32
415         RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
416         LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
417     )
418     install (TARGETS wgltrace LIBRARY DESTINATION ${WRAPPER_INSTALL_DIR})
419
420 elseif (APPLE)
421     # OpenGL framework
422     add_custom_command (
423         OUTPUT cgltrace.cpp
424         COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/cgltrace.py > ${CMAKE_CURRENT_BINARY_DIR}/cgltrace.cpp
425         DEPENDS cgltrace.py gltrace.py trace.py specs/cglapi.py specs/glapi.py specs/glparams.py specs/gltypes.py specs/stdapi.py
426     )
427
428     add_library (cgltrace SHARED
429         cgltrace.cpp
430         glcaps.cpp
431         glproc_gl.cpp
432     )
433
434     add_dependencies (cgltrace glproc)
435
436     set_target_properties (cgltrace PROPERTIES
437         # OpenGL framework name
438         PREFIX "" OUTPUT_NAME "OpenGL" SUFFIX ""
439         # Specificy the version and reexport GLU symbols
440         LINK_FLAGS "-compatibility_version 1 -current_version 1.0.0 -Wl,-reexport_library,/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGLU.dylib"
441         RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
442         LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
443     )
444
445     target_link_libraries (cgltrace
446         common
447         ${ZLIB_LIBRARIES}
448         ${SNAPPY_LIBRARIES}
449         ${CMAKE_THREAD_LIBS_INIT}
450         dl
451     )
452
453     install (TARGETS cgltrace LIBRARY DESTINATION ${WRAPPER_INSTALL_DIR})
454 elseif (X11_FOUND)
455     # libGL.so
456     add_custom_command (
457         OUTPUT glxtrace.cpp
458         COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/glxtrace.py > ${CMAKE_CURRENT_BINARY_DIR}/glxtrace.cpp
459         DEPENDS glxtrace.py gltrace.py trace.py specs/glxapi.py specs/glapi.py specs/glparams.py specs/gltypes.py specs/stdapi.py
460     )
461
462     add_library (glxtrace SHARED
463         glxtrace.cpp
464         glcaps.cpp
465         glproc_gl.cpp
466     )
467
468     add_dependencies (glxtrace glproc)
469
470     set_target_properties (glxtrace PROPERTIES
471         # avoid the default "lib" prefix
472         PREFIX ""
473         # Prevent symbol relocations internal to our wrapper library to be
474         # overwritten by the application.
475         LINK_FLAGS "-Wl,-Bsymbolic -Wl,-Bsymbolic-functions"
476         RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
477         LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
478     )
479
480     target_link_libraries (glxtrace
481         common
482         ${ZLIB_LIBRARIES}
483         ${SNAPPY_LIBRARIES}
484         ${X11_X11_LIB}
485         ${CMAKE_THREAD_LIBS_INIT}
486         dl
487     )
488
489     install (TARGETS glxtrace LIBRARY DESTINATION ${WRAPPER_INSTALL_DIR})
490 endif ()
491
492
493 if (ENABLE_EGL AND NOT WIN32 AND NOT APPLE)
494     # libEGL.so/libGL.so
495     add_custom_command (
496         OUTPUT egltrace.cpp
497         COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/egltrace.py > ${CMAKE_CURRENT_BINARY_DIR}/egltrace.cpp
498         DEPENDS egltrace.py gltrace.py trace.py specs/eglapi.py specs/glesapi.py specs/glapi.py specs/glparams.py specs/gltypes.py specs/stdapi.py
499     )
500
501     add_library (egltrace SHARED
502         egltrace.cpp
503         glcaps.cpp
504         glproc_egl.cpp
505     )
506
507     add_dependencies (egltrace glproc)
508
509     set_property (
510         TARGET egltrace
511         APPEND
512         PROPERTY COMPILE_DEFINITIONS "TRACE_EGL"
513     )
514
515     set_target_properties (egltrace PROPERTIES
516         # avoid the default "lib" prefix
517         PREFIX ""
518         LINK_FLAGS "-Wl,-Bsymbolic -Wl,-Bsymbolic-functions"
519         # Prevent symbol relocations internal to our wrapper library to be
520         # overwritten by the application.
521         RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
522         LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
523     )
524
525     target_link_libraries (egltrace
526         common
527         ${ZLIB_LIBRARIES}
528         ${SNAPPY_LIBRARIES}
529         ${CMAKE_THREAD_LIBS_INIT}
530         dl
531     )
532
533     install (TARGETS egltrace LIBRARY DESTINATION ${WRAPPER_INSTALL_DIR})
534 endif ()
535
536 ##############################################################################
537 # API retracers
538
539 add_custom_command (
540     OUTPUT glretrace_gl.cpp
541     COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/glretrace.py > ${CMAKE_CURRENT_BINARY_DIR}/glretrace_gl.cpp
542     DEPENDS glretrace.py retrace.py specs/glapi.py specs/gltypes.py specs/stdapi.py
543 )
544
545 add_custom_command (
546     OUTPUT glstate_params.cpp
547     COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/glstate.py > ${CMAKE_CURRENT_BINARY_DIR}/glstate_params.cpp
548     DEPENDS glstate.py specs/glparams.py specs/gltypes.py specs/stdapi.py
549 )
550
551 set (retrace_sources
552     glretrace_gl.cpp
553     glretrace_cgl.cpp
554     glretrace_glx.cpp
555     glretrace_wgl.cpp
556     glretrace_egl.cpp
557     glretrace_main.cpp
558     glstate.cpp
559     glstate_params.cpp
560     retrace.cpp
561     retrace_stdc.cpp
562     glws.cpp
563 )
564
565 if (WIN32 OR APPLE OR X11_FOUND)
566     add_executable (glretrace
567         ${retrace_sources}
568         ${glws_os}
569         glproc_gl.cpp
570     )
571
572     add_dependencies (glretrace glproc)
573
574     set_property (
575         TARGET glretrace
576         APPEND
577         PROPERTY COMPILE_DEFINITIONS "RETRACE"
578     )
579
580     target_link_libraries (glretrace
581         common
582         ${PNG_LIBRARIES}
583         ${ZLIB_LIBRARIES}
584         ${SNAPPY_LIBRARIES}
585     )
586
587     if (WIN32)
588     else ()
589         if (APPLE)
590             target_link_libraries (glretrace
591                 "-framework Cocoa"
592                 "-framework ApplicationServices" # CGS*
593                 #"-framework OpenGL" # CGL*
594             )
595         else ()
596             target_link_libraries (glretrace ${X11_X11_LIB})
597         endif ()
598
599         target_link_libraries (glretrace
600             # gdb doesn't like when pthreads is loaded through dlopen (which happens
601             # when dlopen'ing libGL), so link pthreads to avoid this issue.  See also
602             # http://stackoverflow.com/questions/2702628/gdb-cannot-find-new-threads-generic-error
603             ${CMAKE_THREAD_LIBS_INIT}
604             dl
605         )
606
607         if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
608             target_link_libraries (glretrace rt)
609         endif ()
610
611     endif ()
612
613     install (TARGETS glretrace RUNTIME DESTINATION bin) 
614 endif ()
615
616 if (ENABLE_EGL AND X11_FOUND AND NOT WIN32 AND NOT APPLE)
617     add_executable (eglretrace
618         ${retrace_sources}
619         glws_egl_xlib.cpp
620         glproc_egl.cpp
621     )
622
623     add_dependencies (eglretrace glproc)
624
625     set_property (
626         TARGET eglretrace
627         APPEND
628         PROPERTY COMPILE_DEFINITIONS "RETRACE"
629         PROPERTY COMPILE_DEFINITIONS "TRACE_EGL"
630     )
631
632     target_link_libraries (eglretrace
633         common
634         ${PNG_LIBRARIES}
635         ${ZLIB_LIBRARIES}
636         ${SNAPPY_LIBRARIES}
637         ${X11_X11_LIB}
638         ${CMAKE_THREAD_LIBS_INIT}
639         dl
640     )
641
642     if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
643         target_link_libraries (eglretrace rt)
644     endif ()
645
646     install (TARGETS eglretrace RUNTIME DESTINATION bin) 
647 endif ()
648
649 ##############################################################################
650 # CLI
651
652 if (ENABLE_CLI)
653     add_subdirectory(cli)
654 endif ()
655
656 ##############################################################################
657 # Scripts (to support the CLI)
658
659 install (
660     PROGRAMS
661         ${CMAKE_CURRENT_SOURCE_DIR}/scripts/tracediff.py
662         ${CMAKE_CURRENT_SOURCE_DIR}/scripts/jsondiff.py
663         ${CMAKE_CURRENT_SOURCE_DIR}/scripts/snapdiff.py
664     DESTINATION ${SCRIPTS_INSTALL_DIR}
665 )
666
667 ##############################################################################
668 # GUI
669
670 if (ENABLE_GUI AND QT4_FOUND AND QJSON_FOUND)
671     add_subdirectory(gui)
672 endif ()
673
674
675 ##############################################################################
676 # Packaging
677
678 install (
679     FILES
680         BUGS.markdown
681         LICENSE
682         NEWS.markdown
683         README.markdown
684     DESTINATION ${DOC_INSTALL_DIR}
685 )
686
687 set (CPACK_PACKAGE_VERSION_MAJOR "3")
688 set (CPACK_PACKAGE_VERSION_MINOR "0")
689
690 # Use current date in YYYYMMDD format as patch number 
691 execute_process (
692     COMMAND ${PYTHON_EXECUTABLE} -c "import time, sys; sys.stdout.write(time.strftime('%Y%m%d'))"
693     OUTPUT_VARIABLE CPACK_PACKAGE_VERSION_PATCH
694 )
695
696 # cpack mistakenly detects Mingw-w64 as win32
697 if (MINGW)
698     if (CMAKE_SIZEOF_VOID_P EQUAL 8)
699         set (CPACK_SYSTEM_NAME win64)
700     endif ()
701 endif ()
702
703 # See http://www.vtk.org/Wiki/CMake:CPackPackageGenerators
704 if (WIN32)
705     set (CPACK_GENERATOR "ZIP")
706 elseif (APPLE)
707     set (CPACK_GENERATOR "DragNDrop")
708 else ()
709     set (CPACK_GENERATOR "TBZ2")
710 endif ()
711
712 include(CPack)