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