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