if(CMAKE_SYSTEM_NAME STREQUAL Emscripten)
  set(BOOST_INCLUDE_LIBRARIES random stacktrace math)
  set(BOOST_ENABLE_COMPATIBILITY_TARGETS 1)
  add_compile_options(-fdeclspec)  # math doesn't build otherwise

  # Use out-of-tree sources for boost, since we can't use pre-built
  # libraries for the web build
  if(BOOST_SOURCE_ROOT)
    add_subdirectory(${BOOST_SOURCE_ROOT} boost EXCLUDE_FROM_ALL)
  else()
    message(FATAL_ERROR "For web builds BOOST_SOURCE_ROOT must be provided")
  endif()

  # TODO: make this available via find_package so we wouldn't have to
  # special-case it in every library
endif()

file(GLOB_RECURSE RAPIDJSON_SOURCES "${CMAKE_CURRENT_LIST_DIR}/rapidjson/include/*")
add_library(rapidjson INTERFACE EXCLUDE_FROM_ALL "${RAPIDJSON_SOURCES}")
target_include_directories(rapidjson INTERFACE "${CMAKE_CURRENT_LIST_DIR}/rapidjson/include")

if(PSEMEK_STACKTRACE)
	set(LIBBACKTRACE_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/libbacktrace")
	set(LIBBACKTRACE_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace")
	set(LIBBACKTRACE_INCLUDE_DIR "${LIBBACKTRACE_BUILD_DIR}/include")
	set(LIBBACKTRACE_LIBRARY "${LIBBACKTRACE_BUILD_DIR}/.libs/libbacktrace.a")
	set(LIBBACKTRACE_CONFIGURED_STAMP "${LIBBACKTRACE_BUILD_DIR}/.configure-success-stamp")

	if(PSEMEK_PACKAGE_TARGET AND WIN32)
		set(LIBBACKTRACE_CONFIGURE_ARGS "--host=x86_64-w64-mingw32" "CC=${CMAKE_C_COMPILER}")
	elseif(PSEMEK_PACKAGE_TARGET AND ANDROID)
		set(LIBBACKTRACE_CONFIGURE_ARGS "--host=aarch64-none-linux-android34" "CC=${CMAKE_C_COMPILER}" "CFLAGS=--target=aarch64-none-linux-android34")
	elseif(APPLE)
		set(LIBBACKTRACE_CONFIGURE_ARGS "CC=${CMAKE_C_COMPILER}  -isysroot ${CMAKE_OSX_SYSROOT}")
	elseif(UNIX)
		set(LIBBACKTRACE_CONFIGURE_ARGS "CC=${CMAKE_C_COMPILER}")
	else()
		message(FATAL_ERROR "Unknown platform")
	endif()

	if(NOT EXISTS "${LIBBACKTRACE_CONFIGURED_STAMP}")
		file(REMOVE_RECURSE "${LIBBACKTRACE_BUILD_DIR}")
		file(COPY "${LIBBACKTRACE_SOURCE_DIR}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
		execute_process(
			COMMAND ./configure ${LIBBACKTRACE_CONFIGURE_ARGS}
			WORKING_DIRECTORY "${LIBBACKTRACE_BUILD_DIR}"
			RESULT_VARIABLE LIBBACKTRACE_CONFIGURE_RESULT
			COMMAND_ECHO STDOUT
		)
		if(NOT LIBBACKTRACE_CONFIGURE_RESULT EQUAL 0)
			message(FATAL_ERROR "libbacktrace configure failed")
		endif()
		file(WRITE "${LIBBACKTRACE_CONFIGURED_STAMP}" "")
	endif()

	include(ExternalProject)

	ExternalProject_Add(libbacktrace-external
		SOURCE_DIR "${LIBBACKTRACE_BUILD_DIR}"
		BUILD_IN_SOURCE 1
		CONFIGURE_COMMAND ""
		BUILD_COMMAND make
		INSTALL_COMMAND ""
		BUILD_BYPRODUCTS "${LIBBACKTRACE_LIBRARY}"
	)

	add_library(libbacktrace STATIC IMPORTED GLOBAL)
	set_target_properties(libbacktrace PROPERTIES IMPORTED_LOCATION "${LIBBACKTRACE_LIBRARY}")
	file(MAKE_DIRECTORY "${LIBBACKTRACE_INCLUDE_DIR}")
	file(COPY "${LIBBACKTRACE_SOURCE_DIR}/backtrace.h" DESTINATION "${LIBBACKTRACE_INCLUDE_DIR}")
	target_include_directories(libbacktrace INTERFACE "${LIBBACKTRACE_INCLUDE_DIR}")
	add_dependencies(libbacktrace libbacktrace-external)
endif()

