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