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