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