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