-set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
-
-# Use bundled ZLIB if system one can't be found
-if (ZLIB_FOUND)
- include_directories (${ZLIB_INCLUDE_DIRS})
- link_libraries (${ZLIB_LIBRARIES})
-else (ZLIB_FOUND)
- add_library (zlib STATIC
- zlib/adler32.c
- zlib/compress.c
- zlib/crc32.c
- zlib/gzio.c
- zlib/uncompr.c
- zlib/deflate.c
- zlib/trees.c
- zlib/zutil.c
- zlib/inflate.c
- zlib/infback.c
- zlib/inftrees.c
- zlib/inffast.c
- )
- include_directories (zlib)
- link_libraries (zlib)
-endif (ZLIB_FOUND)
-
-# Use bundled LIBPNG if system one can't be found
-if (PNG_FOUND)
- include_directories (${PNG_INCLUDE_DIR})
- add_definitions (${PNG_DEFINITIONS})
- link_libraries (${PNG_LIBRARIES})
-else (PNG_FOUND)
- add_library (png STATIC
- libpng/png.c
- libpng/pngerror.c
- libpng/pngget.c
- libpng/pngmem.c
- libpng/pngpread.c
- libpng/pngread.c
- libpng/pngrio.c
- libpng/pngrtran.c
- libpng/pngrutil.c
- libpng/pngset.c
- libpng/pngtrans.c
- libpng/pngwio.c
- libpng/pngwrite.c
- libpng/pngwtran.c
- libpng/pngwutil.c
- )
- include_directories (libpng)
- link_libraries (png)
-endif (PNG_FOUND)
+set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
+
+
+##############################################################################
+# Bundled dependencies
+#
+# We always use the bundled zlib, libpng, and snappy sources:
+# - on Windows to make it easy to deploy the wrappers DLLs
+# - on unices to prevent symbol collisions when tracing applications that link
+# against other versions of these libraries
+
+set (ZLIB_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/zlib)
+set (ZLIB_LIBRARIES z_bundled)
+add_subdirectory (thirdparty/zlib EXCLUDE_FROM_ALL)
+
+include_directories (${ZLIB_INCLUDE_DIRS})
+
+set (SNAPPY_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/snappy)
+set (SNAPPY_LIBRARIES snappy_bundled)
+add_subdirectory (thirdparty/snappy EXCLUDE_FROM_ALL)
+
+include_directories (${SNAPPY_INCLUDE_DIRS})
+
+set (PNG_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libpng)
+set (PNG_DEFINITIONS "")
+set (PNG_LIBRARIES png_bundled)
+
+add_subdirectory (thirdparty/libpng EXCLUDE_FROM_ALL)
+include_directories (${PNG_INCLUDE_DIR})
+add_definitions (${PNG_DEFINITIONS})
+
+if (MSVC)
+ add_subdirectory (thirdparty/getopt EXCLUDE_FROM_ALL)
+ include_directories (${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/getopt)
+ set (GETOPT_LIBRARIES getopt_bundled)
+endif ()
+
+if (WIN32)
+ add_subdirectory (thirdparty/less)
+endif ()
+
+# The Qt website provides binaries for Windows and MacOSX, and they are
+# automatically found by cmake without any manual intervention. The situation
+# for QJSON is substantially different: there are no binaries for QJSON
+# available, and there is no standard installation directory that is detected
+# by cmake.
+#
+# By bundling the QJSON source, we make it much more easier to build the GUI on
+# Windows and MacOSX. But we only use the bundled sources when ENABLE_GUI is
+# AUTO.
+if (QT4_FOUND AND NOT QJSON_FOUND AND (ENABLE_GUI STREQUAL "AUTO"))
+ add_subdirectory (thirdparty/qjson EXCLUDE_FROM_ALL)
+ set (QJSON_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty)
+ set (QJSON_LIBRARY_DIRS)
+ set (QJSON_LIBRARIES qjson_bundled)
+ set (QJSON_FOUND TRUE)
+endif ()
+
+# We use bundled headers for all Khronos APIs, to guarantee support for both
+# OpenGL and OpenGL ES at build time, because the OpenGL and OpenGL ES 1 APIs
+# are so intertwined that conditional compilation extremely difficult. This
+# also avoids missing/inconsistent declarations in system headers.
+include_directories (BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/khronos)
+
+##############################################################################
+# Installation directories
+
+if (WIN32 OR APPLE)
+ # On Windows/MacOSX, applications are usually installed on a directory of
+ # their own
+ set (DOC_INSTALL_DIR doc)
+ set (LIB_INSTALL_DIR lib)
+else ()
+ set (DOC_INSTALL_DIR share/doc/${CMAKE_PROJECT_NAME})
+ set (LIB_INSTALL_DIR lib/${CMAKE_PROJECT_NAME})
+endif ()
+
+set(SCRIPTS_INSTALL_DIR ${LIB_INSTALL_DIR}/scripts)
+set(WRAPPER_INSTALL_DIR ${LIB_INSTALL_DIR}/wrappers)
+
+# Expose the binary/install directories to source
+#
+# TODO: Use the same directory layout, for both build and install directories,
+# so that binaries can find each other using just relative paths.
+#
+add_definitions(
+ -DAPITRACE_BINARY_DIR="${CMAKE_BINARY_DIR}"
+ -DAPITRACE_INSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}"
+ -DAPITRACE_WRAPPER_INSTALL_DIR="${CMAKE_INSTALL_PREFIX}/${WRAPPER_INSTALL_DIR}"
+)
+