-# 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)
+
+# Put all executables into the same top level build directory, regardless of
+# which subdirectory they are declared
+set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
+
+
+##############################################################################
+# Bundled dependencies
+#
+# We always use the bundled zlib and libpng 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})
+link_libraries (${ZLIB_LIBRARIES})
+
+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})
+link_libraries (${SNAPPY_LIBRARIES})
+
+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})
+link_libraries (${PNG_LIBRARIES})
+
+# For glext headers
+include_directories (${CMAKE_CURRENT_SOURCE_DIR}/thirdparty)
+
+
+##############################################################################
+# Common libraries / utilities