]> git.cworth.org Git - apitrace/blob - CMakeLists.txt
Bundle QJSON sources.
[apitrace] / CMakeLists.txt
1 cmake_minimum_required (VERSION 2.8)
2
3 project (apitrace)
4
5
6 ##############################################################################
7 # Options
8 #
9 # We use a cached string variable instead of the standard (boolean) OPTION
10 # command so that we can default to auto-detecting optional depencies, while
11 # still providing a mechanism to force/disable these optional dependencies, as
12 # prescribed in http://www.gentoo.org/proj/en/qa/automagic.xml
13
14 set (ENABLE_GUI "AUTO" CACHE STRING "Enable Qt GUI.")
15
16
17 ##############################################################################
18 # Find dependencies
19
20 set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
21
22 set (CMAKE_USE_PYTHON_VERSION 2.7 2.6)
23
24 find_package (PythonInterp REQUIRED)
25 find_package (OpenGL REQUIRED)
26
27 if (ENABLE_GUI)
28     if (NOT (ENABLE_GUI STREQUAL "AUTO"))
29         set (REQUIRE_GUI REQUIRED)
30     endif ()
31     find_package (Qt4 4.7 COMPONENTS QtCore QtGui QtWebKit ${REQUIRE_GUI})
32     find_package (QJSON ${REQUIRE_GUI})
33 endif ()
34
35 if (NOT WIN32)
36     find_package (X11 REQUIRED)
37
38     # On Mac OS X, GLX is provided as a separate OpenGL implementation, different
39     # from the standard OpenGL framework which provides support for native Mac OS X
40     # applications.
41     if (APPLE)
42         find_path (X11_GL_INCLUDE_PATH GL/glx.h ${X11_INC_SEARCH_PATH})
43         if (NOT X11_GL_INCLUDE_PATH)
44             message (SEND_ERROR "Could not find GL/glx.h")
45         endif (NOT X11_GL_INCLUDE_PATH)
46         set (X11_INCLUDE_DIR ${X11_INCLUDE_DIR} ${X11_GL_INCLUDE_PATH})
47
48         find_library (X11_GL_LIB GL ${X11_LIB_SEARCH_PATH})
49         if (NOT X11_GL_LIB)
50             message (SEND_ERROR "Could not find libGL.dylib")
51         endif (NOT X11_GL_LIB)
52     else ()
53         set (X11_INCLUDE_DIR ${X11_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR})
54         set (X11_GL_LIB ${OPENGL_gl_LIBRARY})
55     endif ()
56
57     include_directories (${X11_INCLUDE_DIR})
58 endif (NOT WIN32)
59
60 if (WIN32)
61     find_package (DirectX)
62 endif (WIN32)
63
64
65 ##############################################################################
66 # Set global build options
67
68 include (CheckCXXCompilerFlag)
69
70 if (WIN32)
71     # MSVC & MinGW only define & use APIENTRY
72     add_definitions (-DGLAPIENTRY=__stdcall)
73
74     # http://msdn.microsoft.com/en-us/library/aa383745.aspx
75     add_definitions (-D_WIN32_WINNT=0x0500 -DWINVER=0x0500)
76 else (WIN32)
77     CHECK_CXX_COMPILER_FLAG("-fvisibility=hidden" CXX_COMPILER_FLAG_VISIBILITY)
78     if (CXX_COMPILER_FLAG_VISIBILITY)
79         add_definitions ("-fvisibility=hidden")
80     endif (CXX_COMPILER_FLAG_VISIBILITY)
81 endif (WIN32)
82
83 if (MSVC)
84     # C99 includes for msvc
85     include_directories (${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/msvc)
86
87     # Enable math constants defines
88     add_definitions (-D_USE_MATH_DEFINES)
89
90     # No min/max macros
91     add_definitions (-DNOMINMAX)
92
93     # Adjust warnings
94     add_definitions (-D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS)
95     add_definitions (-D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS)
96     add_definitions (-W4)
97     add_definitions (-wd4063) # not a valid value for switch of enum
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 (-wd4800) # forcing value to bool 'true' or 'false' (performance warning)
102     # XXX: it's safer to use ssize_t everywhere instead of disabling warning
103     add_definitions (-wd4018) # signed/unsigned mismatch
104     
105     # Use static runtime
106     # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
107     foreach (flag_var
108         CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
109         CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
110     )
111         if (${flag_var} MATCHES "/MD")
112             string (REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
113         endif (${flag_var} MATCHES "/MD")
114     endforeach (flag_var)
115 else ()
116     # Adjust warnings
117     add_definitions (-Wall)
118     # XXX: it's safer to use ssize_t everywhere instead of disabling warning
119     add_definitions (-Wno-sign-compare) # comparison between signed and unsigned integer expressions
120 endif ()
121
122
123 # Put all executables into the same top level build directory, regardless of
124 # which subdirectory they are declared
125 set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
126
127
128 ##############################################################################
129 # Bundled dependencies
130 #
131 # We always use the bundled zlib, libpng, and snappy sources:
132 # - on Windows to make it easy to deploy the wrappers DLLs
133 # - on unices to prevent symbol collisions when tracing applications that link
134 # against other versions of these libraries
135
136 set (ZLIB_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/zlib)
137 set (ZLIB_LIBRARIES z_bundled)
138 add_subdirectory (thirdparty/zlib EXCLUDE_FROM_ALL)
139
140 include_directories (${ZLIB_INCLUDE_DIRS})
141 link_libraries (${ZLIB_LIBRARIES})
142
143 set (SNAPPY_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/snappy)
144 set (SNAPPY_LIBRARIES snappy_bundled)
145 add_subdirectory (thirdparty/snappy EXCLUDE_FROM_ALL)
146
147 include_directories (${SNAPPY_INCLUDE_DIRS})
148 link_libraries (${SNAPPY_LIBRARIES})
149
150 set (PNG_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libpng)
151 set (PNG_DEFINITIONS "")
152 set (PNG_LIBRARIES png_bundled)
153
154 add_subdirectory (thirdparty/libpng EXCLUDE_FROM_ALL)
155 include_directories (${PNG_INCLUDE_DIR})
156 add_definitions (${PNG_DEFINITIONS})
157 link_libraries (${PNG_LIBRARIES})
158
159 # The Qt website provides binaries for Windows and MacOSX, and they are
160 # automatically found by cmake without any manual intervention.  The situation
161 # for QJSON is substantially different: there are no binaries for QJSON
162 # available, and there is no standard installation directory that is detected
163 # by cmake.
164 #
165 # By bundling the QJSON source, we make it much more easier to build the GUI on
166 # Windows and MacOSX.  But we only use the bundled sources when ENABLE_GUI is
167 # AUTO.
168 if (QT4_FOUND AND NOT QJSON_FOUND AND (ENABLE_GUI STREQUAL "AUTO"))
169     add_subdirectory (thirdparty/qjson EXCLUDE_FROM_ALL)
170     set (QJSON_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/qjson)
171     set (QJSON_LIBRARY_DIRS)
172     set (QJSON_LIBRARIES qjson_bundled)
173     set (QJSON_FOUND TRUE)
174 endif ()
175
176 # For glext headers
177 include_directories (${CMAKE_CURRENT_SOURCE_DIR}/thirdparty)
178
179
180 ##############################################################################
181 # Common libraries / utilities
182
183 include_directories (
184     ${CMAKE_CURRENT_SOURCE_DIR}
185     ${CMAKE_CURRENT_SOURCE_DIR}/common
186 )
187
188 add_custom_command (
189     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp
190     COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/glproc.py > ${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp
191     DEPENDS glproc.py dispatch.py wglapi.py glxapi.py cglapi.py glapi.py gltypes.py stdapi.py
192 )
193
194 if (WIN32)
195     set (os os_win32.cpp)
196     set (glws glws_wgl.cpp)
197 else (WIN32)
198     set (os os_posix.cpp)
199     set (glws glws_glx.cpp)
200 endif (WIN32)
201
202 add_library (common
203     common/trace_file.cpp
204     common/trace_snappyfile.cpp
205     common/trace_model.cpp
206     common/trace_parser.cpp
207     common/trace_writer.cpp
208     common/trace_local_writer.cpp
209     common/trace_model_writer.cpp
210     common/trace_loader.cpp
211     common/image.cpp
212     common/image_bmp.cpp
213     common/image_pnm.cpp
214     common/image_png.cpp
215     common/${os}
216 )
217
218 set_target_properties (common PROPERTIES
219     # Ensure it can be statically linked in shared libraries
220     COMPILE_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}"
221 )
222
223 link_libraries (common)
224
225 add_executable (tracedump tracedump.cpp)
226 install (TARGETS tracedump RUNTIME DESTINATION bin) 
227
228
229 ##############################################################################
230 # API tracers
231
232 if (WIN32)
233     # ddraw.dll
234     if (DirectX_D3D_INCLUDE_DIR)
235         include_directories (SYSTEM ${DirectX_D3D_INCLUDE_DIR})
236         add_custom_command (
237             OUTPUT ddrawtrace.cpp
238             COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/ddrawtrace.py > ${CMAKE_CURRENT_BINARY_DIR}/ddrawtrace.cpp
239             DEPENDS ddrawtrace.py d3d.py d3dtypes.py d3dcaps.py ddraw.py trace.py winapi.py stdapi.py
240         )
241         add_library (ddraw MODULE ddraw.def ddrawtrace.cpp)
242         set_target_properties (ddraw
243             PROPERTIES PREFIX ""
244             RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
245             LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
246         )
247         install (TARGETS ddraw LIBRARY DESTINATION wrappers)
248     endif (DirectX_D3D_INCLUDE_DIR)
249
250     # d3d8.dll
251     if (DirectX_D3D8_INCLUDE_DIR AND DirectX_D3DX9_INCLUDE_DIR)
252         include_directories (SYSTEM ${DirectX_D3D8_INCLUDE_DIR} ${DirectX_D3DX9_INCLUDE_DIR})
253         add_custom_command (
254             OUTPUT d3d8trace.cpp
255             COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/d3d8trace.py > ${CMAKE_CURRENT_BINARY_DIR}/d3d8trace.cpp
256             DEPENDS d3d8trace.py d3d8.py trace.py d3d8types.py d3d8caps.py winapi.py stdapi.py
257         )
258         add_library (d3d8 MODULE d3d8.def d3d8trace.cpp d3dshader.cpp)
259         set_target_properties (d3d8
260             PROPERTIES PREFIX ""
261             RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
262             LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
263         )
264         install (TARGETS d3d8 LIBRARY DESTINATION wrappers)
265     endif (DirectX_D3D8_INCLUDE_DIR AND DirectX_D3DX9_INCLUDE_DIR)
266
267     # d3d9.dll
268     if (DirectX_D3DX9_INCLUDE_DIR)
269         include_directories (SYSTEM ${DirectX_D3DX9_INCLUDE_DIR})
270         add_custom_command (
271             OUTPUT d3d9trace.cpp
272             COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/d3d9trace.py > ${CMAKE_CURRENT_BINARY_DIR}/d3d9trace.cpp
273             DEPENDS d3d9trace.py d3d9.py trace.py d3d9types.py d3d9caps.py winapi.py stdapi.py
274         )
275         add_library (d3d9 MODULE d3d9.def d3d9trace.cpp d3dshader.cpp)
276         set_target_properties (d3d9
277             PROPERTIES PREFIX ""
278             RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
279             LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
280         )
281         install (TARGETS d3d9 LIBRARY DESTINATION wrappers)
282     endif (DirectX_D3DX9_INCLUDE_DIR)
283
284     # d3d10.dll
285     if (DirectX_D3D10_INCLUDE_DIR)
286         include_directories (SYSTEM ${DirectX_D3D10_INCLUDE_DIR})
287         add_custom_command (
288             OUTPUT d3d10trace.cpp
289             COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/d3d10trace.py > ${CMAKE_CURRENT_BINARY_DIR}/d3d10trace.cpp
290             DEPENDS d3d10trace.py d3d10misc.py d3d10.py dxgi.py winapi.py stdapi.py
291         )
292         add_library (d3d10 MODULE d3d10.def d3d10trace.cpp)
293         set_target_properties (d3d10 PROPERTIES PREFIX "")
294         install (TARGETS d3d10 LIBRARY DESTINATION wrappers)
295     endif (DirectX_D3D10_INCLUDE_DIR)
296
297     # opengl32.dll
298     add_custom_command (
299         OUTPUT wgltrace.cpp
300         COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/wgltrace.py > ${CMAKE_CURRENT_BINARY_DIR}/wgltrace.cpp
301         DEPENDS wgltrace.py gltrace.py trace.py wglapi.py wglenum.py glapi.py glparams.py gltypes.py winapi.py stdapi.py
302     )
303     add_library (wgltrace MODULE opengl32.def
304         wgltrace.cpp
305         glcaps.cpp
306         ${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp
307     )
308     set_target_properties (wgltrace PROPERTIES
309         PREFIX ""
310         OUTPUT_NAME opengl32
311         RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
312         LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
313     )
314     if (MINGW)
315         set_target_properties(wgltrace PROPERTIES LINK_FLAGS "-Wl,--enable-stdcall-fixup ${CMAKE_CURRENT_SOURCE_DIR}/opengl32.def")
316     endif (MINGW)
317     install (TARGETS wgltrace LIBRARY DESTINATION wrappers)
318
319 elseif (APPLE)
320     # OpenGL framework
321     add_custom_command (
322         OUTPUT cgltrace.cpp
323         COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/cgltrace.py > ${CMAKE_CURRENT_BINARY_DIR}/cgltrace.cpp
324         DEPENDS cgltrace.py gltrace.py trace.py cglapi.py glapi.py glparams.py gltypes.py stdapi.py
325     )
326
327     add_library (cgltrace SHARED
328         cgltrace.cpp
329         glcaps.cpp
330         ${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp
331     )
332
333     set_target_properties (cgltrace PROPERTIES
334         # OpenGL framework name
335         PREFIX "" OUTPUT_NAME "OpenGL" SUFFIX ""
336         # Specificy the version and reexport GLU symbols
337         LINK_FLAGS "-compatibility_version 1 -current_version 1.0.0 -Wl,-reexport_library,/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGLU.dylib"
338         RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
339         LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
340     )
341
342     target_link_libraries (cgltrace dl)
343
344     install (TARGETS cgltrace LIBRARY DESTINATION wrappers)
345 else ()
346     # libGL.so
347     add_custom_command (
348         OUTPUT glxtrace.cpp
349         COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/glxtrace.py > ${CMAKE_CURRENT_BINARY_DIR}/glxtrace.cpp
350         DEPENDS glxtrace.py gltrace.py trace.py glxapi.py glapi.py glparams.py gltypes.py stdapi.py
351     )
352
353     add_library (glxtrace SHARED
354         ${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp
355         glxtrace.cpp
356         glcaps.cpp
357         glsnapshot.cpp
358     )
359
360     set_target_properties (glxtrace PROPERTIES
361         # avoid the default "lib" prefix
362         PREFIX ""
363     )
364
365     # Prevent symbol relocations internal to our wrapper library to be
366     # overwritten by the application.
367     set_target_properties (glxtrace PROPERTIES
368         LINK_FLAGS "-Wl,-Bsymbolic -Wl,-Bsymbolic-functions"
369     )
370
371     target_link_libraries (glxtrace dl ${X11_X11_LIB})
372
373     install (TARGETS glxtrace LIBRARY DESTINATION lib)
374 endif ()
375
376
377 ##############################################################################
378 # API retracers
379
380 add_custom_command (
381     OUTPUT glretrace_gl.cpp
382     COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/glretrace.py > ${CMAKE_CURRENT_BINARY_DIR}/glretrace_gl.cpp
383     DEPENDS glretrace.py retrace.py codegen.py glapi.py gltypes.py stdapi.py
384 )
385
386 add_custom_command (
387     OUTPUT glstate_params.cpp
388     COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/glstate.py > ${CMAKE_CURRENT_BINARY_DIR}/glstate_params.cpp
389     DEPENDS glstate.py glparams.py gltypes.py stdapi.py
390 )
391
392 include_directories (
393     ${CMAKE_CURRENT_BINARY_DIR}
394     ${OPENGL_INCLUDE_PATH}
395 )
396
397 add_executable (glretrace
398     glretrace_gl.cpp
399     glretrace_cgl.cpp
400     glretrace_glx.cpp
401     glretrace_wgl.cpp
402     glretrace_main.cpp
403     glstate.cpp
404     glstate_params.cpp
405     retrace.cpp
406     ${glws}
407     ${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp
408 )
409
410 set_property (
411     TARGET glretrace
412     APPEND
413     PROPERTY COMPILE_DEFINITIONS "RETRACE"
414 )
415
416 target_link_libraries (glretrace
417     common
418 )
419
420 if (WIN32)
421     target_link_libraries (glretrace ${OPENGL_gl_LIBRARY})
422 elseif (APPLE)
423     # XXX: We use GLX on MacOSX, which is in a separate library.
424     target_link_libraries (glretrace
425         ${X11_GL_LIB}
426         ${X11_X11_LIB}
427         "-framework ApplicationServices" # CGS*
428         ${OPENGL_gl_LIBRARY} # CGL*
429     )
430 else ()
431     target_link_libraries (glretrace ${OPENGL_gl_LIBRARY} ${X11_X11_LIB})
432 endif ()
433
434 install (TARGETS glretrace RUNTIME DESTINATION bin) 
435
436
437 ##############################################################################
438 # GUI
439
440 if (ENABLE_GUI AND QT4_FOUND AND QJSON_FOUND)
441     add_subdirectory(gui)
442 endif ()
443
444
445 ##############################################################################
446 # Packaging
447
448 install (
449     FILES
450         BUGS.markdown
451         LICENSE
452         NEWS.markdown
453         README.markdown
454         TODO.markdown
455     DESTINATION doc)
456
457 set (CPACK_PACKAGE_VERSION_MAJOR "1")
458 set (CPACK_PACKAGE_VERSION_MINOR "0")
459
460 # Use current date in YYYYMMDD format as patch number 
461 execute_process (
462     COMMAND ${PYTHON_EXECUTABLE} -c "import time, sys; sys.stdout.write(time.strftime('%Y%m%d'))"
463     OUTPUT_VARIABLE CPACK_PACKAGE_VERSION_PATCH
464 )
465
466 # See http://www.vtk.org/Wiki/CMake:CPackPackageGenerators
467 if (WIN32)
468     set (CPACK_GENERATOR "ZIP")
469 elseif (APPLE)
470     set (CPACK_GENERATOR "DragNDrop")
471 else ()
472     set (CPACK_GENERATOR "TBZ2")
473 endif ()
474
475 include(CPack)