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")

	if(NOT EXISTS "${LIBBACKTRACE_BUILD_DIR}")
		file(COPY "${LIBBACKTRACE_SOURCE_DIR}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
		make_directory("${LIBBACKTRACE_INCLUDE_DIR}")
		file(COPY "${LIBBACKTRACE_SOURCE_DIR}/backtrace.h" DESTINATION "${LIBBACKTRACE_INCLUDE_DIR}")
		if(PSEMEK_PACKAGE_TARGET AND WIN32)
			execute_process(COMMAND "./configure" "--host=x86_64-w64-mingw32" "CC=${CMAKE_C_COMPILER}" WORKING_DIRECTORY "${LIBBACKTRACE_BUILD_DIR}" COMMAND_ECHO STDOUT)
		elseif(PSEMEK_PACKAGE_TARGET AND ANDROID)
			execute_process(COMMAND "./configure" "--host=aarch64-none-linux-android34" "CC=${CMAKE_C_COMPILER}" "CFLAGS=--target=aarch64-none-linux-android34" WORKING_DIRECTORY "${LIBBACKTRACE_BUILD_DIR}" COMMAND_ECHO STDOUT)
		else()
			execute_process(COMMAND "./configure" "CC=${CMAKE_C_COMPILER}" WORKING_DIRECTORY "${LIBBACKTRACE_BUILD_DIR}" COMMAND_ECHO STDOUT)
		endif()
		execute_process(COMMAND "make" WORKING_DIRECTORY "${LIBBACKTRACE_BUILD_DIR}" RESULT_VARIABLE LIBBACKTRACE_BUILD_RESULT COMMAND_ECHO STDOUT)
		if(NOT (${LIBBACKTRACE_BUILD_RESULT} EQUAL 0))
			message(FATAL_ERROR "libbacktrace build failed")
		endif()
	endif()

	add_library(libbacktrace INTERFACE)
	target_include_directories(libbacktrace INTERFACE "${LIBBACKTRACE_INCLUDE_DIR}")
	target_link_libraries(libbacktrace INTERFACE "${LIBBACKTRACE_LIBRARY}")
endif()

