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