cmake_minimum_required( VERSION 3.16 )

cmake_policy( SET CMP0067 NEW )
cmake_policy( SET CMP0091 NEW )
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

project(UZDoom)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(ABSL_BUILD_TESTING OFF) # don't build/run abseil tests

# make all targets use LTO - want to do this early in the cmake to ensure it applies to any targets
option( FORCE_NO_LTO "Turn off LTO even if it would be possible to use it." NO )

if(NOT FORCE_NO_LTO)
	include(CheckIPOSupported)
	check_ipo_supported(RESULT ipo_supported OUTPUT ipo_error)
	if( ipo_supported )
		message(STATUS "Using LTO on Release and RelWithDebInfo build types")
		set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
		set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
	else()
		message(STATUS "Not using LTO because unsupported: ${ipo_error}")
	endif()
endif()

if(WIN32)
	option( USE_UPDATER "enable auto-updater" OFF )
endif()

if(USE_UPDATER)
	set( CURL_REQUIRED TRUE )

	if (WIN32)
		option( LOAD_CURL_AT_RUNTIME "load curl at runtime instead of linking" ON )

		if (NOT MSVC)
			# NOTE: not a big issue for now but will need to be fixed if we switch over to mingw as the
			#       primary windows compiler (which we'll probably have to do eventually for C++26 support)
			message( FATAL_ERROR "updater.exe currently only builds with msvc" )
		endif()
		add_executable( updater WIN32 src/win32/updater.c )
	else()
		message( FATAL_ERROR "Updater not available on this platform" )
	endif()
endif()

find_package(Threads REQUIRED)
find_package(Python3 3.6 REQUIRED)

list( APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake )
include( FindPackageHandleStandardArgs )

# Support cross compiling
option( FORCE_CROSSCOMPILE "Turn on cross compiling." NO )
if( FORCE_CROSSCOMPILE )
	set( CMAKE_CROSSCOMPILING TRUE )
endif()

if(CMAKE_CROSSCOMPILING)
	set(IMPORT_EXECUTABLES "IMPORTFILE-NOTFOUND" CACHE FILEPATH "Export file from native build.")
	include(${IMPORT_EXECUTABLES})
endif()

# Recursive function to place PK3 archive source files into a hierarchy of source file in the IDE
function( assort_pk3_source_folder FOLDER_NAME )
	# Assort source files into folders in the IDE
	# Create wildcard patterns for all directories
	set(PK3_PATTERNS)
	foreach(PK3_DIR ${ARGN})
		list(APPEND PK3_PATTERNS "${PK3_DIR}/*")
	endforeach()
	file(GLOB PK3_SRCS ${PK3_PATTERNS}) # Create list of all files in these folders
	foreach(PK3_SRC ${PK3_SRCS})
		# If there are subfolders, recurse into them
		if(IS_DIRECTORY ${PK3_SRC})
			get_filename_component(DIRNAME ${PK3_SRC} NAME)
			# Exclude folder from list of source files
			list(REMOVE_ITEM PK3_SRCS ${PK3_SRC})
			# Recurse deeper into the filesystem folder tree
			assort_pk3_source_folder( ${FOLDER_NAME}\\${DIRNAME} ${PK3_SRC} )
		endif()
		# Assign IDE group for current top-level source files
		source_group(${FOLDER_NAME} FILES ${PK3_SRCS})
	endforeach()
endfunction()

option( PK3_QUIET_ZIPDIR "Do not list files processed by zipdir" NO )
if( PK3_QUIET_ZIPDIR )
	set( PK3_ZIPDIR_OPTIONS "-q" )
endif()

# Simplify pk3 building, add_pk3(filename srcdirectory [srcdirectory2 ...])
function( add_pk3 PK3_NAME )
	# Generate target name. Just use "pk3" for main pk3 target.
	string( REPLACE "." "_" PK3_TARGET ${PK3_NAME} )

	add_custom_command( OUTPUT ${ZDOOM_OUTPUT_DIR}/${PK3_NAME}
		COMMAND zipdir -udf ${PK3_ZIPDIR_OPTIONS} ${ZDOOM_OUTPUT_DIR}/${PK3_NAME} ${ARGN}
		DEPENDS zipdir )

	# Create a list of source files for this PK3, for use in the IDE
	# Phase 1: Create a list of all source files for this PK3 archive, except
	#  for a couple of strife image file names that confuse CMake.
	# Create wildcard patterns for all directories
	set(PK3_PATTERNS)
	foreach(PK3_DIR ${ARGN})
		list(APPEND PK3_PATTERNS "${PK3_DIR}/*")
	endforeach()
	file(GLOB_RECURSE PK3_SRCS ${PK3_PATTERNS})
	# Exclude from the source list some files with brackets in the
	# file names here, because they confuse CMake.
	# This only affects the list of source files shown in the IDE.
	# It does not actually remove the files from the PK3 archive.
	# First replace that toxic bracket character with something we can handle
	string(REPLACE "[" confusing_bracket PK3_SRCS "${PK3_SRCS}")
	string(REPLACE "]" confusing_bracket PK3_SRCS "${PK3_SRCS}")
	foreach(PK3_SRC ${PK3_SRCS}) # All source files at all levels
		# Exclude those quarantined source file source file names that once had a bracket
		if(${PK3_SRC} MATCHES confusing_bracket)
			# message(STATUS "Ignoring PK3 file name containing brackets "${PK3_SRC})
			list(REMOVE_ITEM PK3_SRCS ${PK3_SRC})
		endif()
	endforeach()

	# Phase 2: Create the PK3 build rule, including the source file list for the IDE
	# Touch the zipdir executable here so that the pk3s are forced to
	# rebuild each time since their dependency has "changed."
	add_custom_target( ${PK3_TARGET} ALL
		COMMAND ${CMAKE_COMMAND} -E touch $<TARGET_FILE:zipdir>
		DEPENDS ${ZDOOM_OUTPUT_DIR}/${PK3_NAME}
		SOURCES ${PK3_SRCS})

	# Phase 3: Assign source files to a nice folder structure in the IDE
	assort_pk3_source_folder("Source Files" ${ARGN})

	# Phase 4: Create separate copy step, in order to make zdoom depend on pk3s without creating cycle
	set(COPY_TARGET_NAME "${PK3_TARGET}_copy")
	add_custom_target(${COPY_TARGET_NAME} ALL
		COMMAND ${CMAKE_COMMAND} -E copy_if_different
			"${ZDOOM_OUTPUT_DIR}/${PK3_NAME}"
			"${ZDOOM_RESOURCE_DIR}/${PK3_NAME}"
		DEPENDS ${PK3_TARGET}
	)
	add_dependencies(zdoom ${COPY_TARGET_NAME})

	# Phase 5: Add the resulting PK3 to the install target.
	if( WIN32 )
		set( INSTALL_PK3_PATH . CACHE STRING "Directory where zdoom.pk3 will be placed during install." )
	else()
		set( INSTALL_PK3_PATH share/games/${ZDOOM_EXE_NAME} CACHE STRING "Directory where zdoom.pk3 will be placed during install." )
	endif()
	install(FILES "${PROJECT_BINARY_DIR}/${PK3_NAME}"
		DESTINATION ${INSTALL_PK3_PATH}
		COMPONENT "Game resources")
endfunction()

macro( generate_language_files target destination identifier )
	# right now, this dumps temp files into the source tree and deletes them after build
	# This should be re-worked to dump stuff into the build tree instead
	set(lang_script "${CMAKE_CURRENT_SOURCE_DIR}/../libraries/Translation/scripts/compile.py")
	set(lang_dst "${CMAKE_CURRENT_SOURCE_DIR}/${destination}")
	set(lang_target "${target}_${destination}")

	string( REGEX REPLACE "[^a-zA-Z0-9]" "_" lang_target ${lang_target} )

	add_custom_target(${lang_target} COMMAND Python3::Interpreter -B -E -s "${lang_script}" --recipe ${identifier} --output "${lang_dst}")
	add_dependencies(${target} ${lang_target})
	add_custom_command(TARGET ${target} POST_BUILD COMMAND ${CMAKE_COMMAND} -E remove "${lang_dst}")
endmacro()

# Macro for building libraries without debugging information
macro( make_release_only )
	set( CMAKE_C_FLAGS_MINSIZEREL ${CMAKE_C_FLAGS_RELEASE} )
	set( CMAKE_C_FLAGS_RELWITHDEBINFO ${CMAKE_C_FLAGS_RELEASE} )
	string( REPLACE "/MT " "/MTd " CMAKE_C_FLAGS_DEBUG ${CMAKE_C_FLAGS_RELEASE} )
	set( CMAKE_CXX_FLAGS_MINSIZEREL ${CMAKE_CXX_FLAGS_RELEASE} )
	set( CMAKE_CXX_FLAGS_RELWITHDEBINFO ${CMAKE_CXX_FLAGS_RELEASE} )
	string( REPLACE "/MT " "/MTd " CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_RELEASE} )
endmacro()

IF( NOT CMAKE_BUILD_TYPE )
	SET( CMAKE_BUILD_TYPE Debug CACHE STRING
		"Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel."
		FORCE )
ENDIF()

set( ZDOOM_OUTPUT_DIR ${CMAKE_BINARY_DIR} CACHE PATH "Directory where zdoom.pk3 and the executable will be created." )
set( ZDOOM_EXE_NAME "uzdoom" CACHE FILEPATH "Name of the executable to create" )

if(DEFINED CMAKE_RUNTIME_OUTPUT_DIRECTORY)
	set(ZDOOM_RESOURCE_DIR "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
elseif(WIN32)
	set(ZDOOM_RESOURCE_DIR "${PROJECT_BINARY_DIR}/$<CONFIG>")
elseif(APPLE)
	set(ZDOOM_RESOURCE_DIR "${PROJECT_BINARY_DIR}/${ZDOOM_EXE_NAME}.app/Contents/Resources")
else()
	set(ZDOOM_RESOURCE_DIR "${PROJECT_BINARY_DIR}")
endif()

# Replacement variables for a possible long list of C/C++ compilers compatible with GCC
if( "${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" MATCHES "Clang" )
	set( ZD_CMAKE_COMPILER_IS_GNUC_COMPATIBLE TRUE )
else()
	set( ZD_CMAKE_COMPILER_IS_GNUC_COMPATIBLE FALSE )
endif()

if( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" )
	set( DEM_CMAKE_COMPILER_IS_GNUCXX_COMPATIBLE TRUE )
else()
	set( DEM_CMAKE_COMPILER_IS_GNUCXX_COMPATIBLE FALSE )
endif()

if( DEM_CMAKE_COMPILER_IS_GNUCXX_COMPATIBLE )
	set( PROFILE 0 CACHE BOOL "Enable profiling with gprof for Debug and RelWithDebInfo build types." )
endif()

# Fast math flags, required by some subprojects
set( ZD_FASTMATH_FLAG "" )
if( DEM_CMAKE_COMPILER_IS_GNUCXX_COMPATIBLE )
	set( ZD_FASTMATH_FLAG "-ffast-math -ffp-contract=fast" )
elseif( MSVC )
	set( ZD_FASTMATH_FLAG "/fp:fast" )
endif()

macro( use_fast_math )
	set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${ZD_FASTMATH_FLAG}" )
	set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ZD_FASTMATH_FLAG}" )
endmacro()

include( CheckFunctionExists )

macro( gz_require_stricmp )
	CHECK_FUNCTION_EXISTS( stricmp STRICMP_EXISTS )
	if( NOT STRICMP_EXISTS )
		add_definitions( -Dstricmp=strcasecmp )
	endif()
endmacro()

macro( gz_require_strnicmp )
	CHECK_FUNCTION_EXISTS( strnicmp STRNICMP_EXISTS )
	if( NOT STRNICMP_EXISTS )
		add_definitions( -Dstrnicmp=strncasecmp )
	endif()
endmacro()

option( NO_OPENAL "Disable OpenAL sound support" OFF )

find_package( BZip2 )
find_package( VPX )

if (CURL_REQUIRED)
	find_package( CURL )
	if( MSVC AND NOT CURL_FOUND )
		# Use prebuilt library
		if ( ARM64 )
			set( CURL_ROOT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/bin/Windows/curl/arm64" )
		else()
			set( CURL_ROOT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/bin/Windows/curl/x64" )
		endif()
		set( CURL_INCLUDE_DIRS ${CURL_ROOT_PATH}/include )
		set( CURL_LIBRARIES ${CURL_ROOT_PATH}/lib/libcurl.dll.a )
		set( CURL_FOUND TRUE )
	endif()
endif()

include( TargetArch )

target_architecture(TARGET_ARCHITECTURE)

if( ${TARGET_ARCHITECTURE} MATCHES "x86_64" )
	set( HAVE_VM_JIT ON )
endif()

option (HAVE_VULKAN "Enable Vulkan support" ON)
if( APPLE )
	option (HAVE_GLES2 "Enable GLES2 support" OFF)
else()
	option (HAVE_GLES2 "Enable GLES2 support" ON)
endif()

if( MSVC )
	# Eliminate unreferenced functions and data
	# Perform identical COMDAT folding
	set( REL_LINKER_FLAGS "/opt:ref /opt:icf /nodefaultlib:msvcrt /TSAWARE /LARGEADDRESSAWARE" )

	# String pooling
	# Function-level linking
	# Disable run-time type information
	set( ALL_C_FLAGS "/GF /Gy /permissive- /utf-8" )

	if ( HAVE_VULKAN )
		set( ALL_C_FLAGS "${ALL_C_FLAGS} /DHAVE_VULKAN" )
	endif()

	if ( HAVE_GLES2 )
		set( ALL_C_FLAGS "${ALL_C_FLAGS} /DHAVE_GLES2" )
	endif()

	# Use SSE 2 as minimum always as the true color drawers needs it for __vectorcall
	#set( ALL_C_FLAGS "${ALL_C_FLAGS} /arch:SSE2") # This is already the default

	# Avoid CRT DLL dependancies in release builds, optionally generate assembly output for checking crash locations.
	option( ZDOOM_GENERATE_ASM "Generate assembly output." OFF )
	if( ZDOOM_GENERATE_ASM )
		set( REL_C_FLAGS "/Oy /Oi /FAcs /GS-" )
	else()
		set( REL_C_FLAGS "/Oy /Oi /GS-" )
	endif()

	# Debug allocations in debug builds
	set( DEB_C_FLAGS "/D _CRTDBG_MAP_ALLOC" )

	# Disable warnings for unsecure CRT functions from VC8+
	set( ALL_C_FLAGS "${ALL_C_FLAGS} /DUNICODE /D_UNICODE /D_WIN32_WINNT=0x0600 /D_CRT_SECURE_NO_DEPRECATE /D_CRT_SECURE_NO_WARNINGS /D_CRT_NONSTDC_NO_WARNINGS" )

	if( WITH_ASAN )
		set( ALL_C_FLAGS "${ALL_C_FLAGS} /fsanitize=address" )
	endif()

	string(REPLACE "/Ob1 " "/Ob2 " CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}" )
	string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
	string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
	string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
	string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
else()
	set( REL_LINKER_FLAGS "" )
	set( ALL_C_FLAGS "-ffp-contract=off" )

	if ( HAVE_VULKAN )
		set( ALL_C_FLAGS "${ALL_C_FLAGS} -DHAVE_VULKAN" )
	endif()

	if ( HAVE_GLES2 )
		set( ALL_C_FLAGS "${ALL_C_FLAGS} -DHAVE_GLES2" )
	endif()

	if ( UNIX )
		include(CheckSymbolExists)
		check_symbol_exists( "fts_set" "sys/types.h;sys/stat.h;fts.h" HAVE_FTS )
		if ( NOT HAVE_FTS )
			include ( FindPkgConfig )
			pkg_check_modules( MUSL_FTS musl-fts )
			if ( MUSL_FTS_FOUND )
				list(JOIN MUSL_FTS_LDFLAGS " " MUSL_FTS_LDFLAGS_JOINED)
				set ( ALL_C_FLAGS "${ALL_C_FLAGS} ${MUSL_FTS_LDFLAGS_JOINED}" )
			else ( MUSL_FTS_FOUND )
				message (ERROR "fts_* functions not found in the system" )
			endif ( MUSL_FTS_FOUND )
		endif ( NOT HAVE_FTS )
	endif ( UNIX )

	set( REL_C_FLAGS "" )
	set( DEB_C_FLAGS "" )

	if( APPLE )
		list(PREPEND CMAKE_PREFIX_PATH "/opt/local")    # port
		list(PREPEND CMAKE_PREFIX_PATH "/opt/homebrew") # brew
		set(CMAKE_OSX_DEPLOYMENT_TARGET "10.13")
		if( CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
			# If we're compiling with a custom GCC on the Mac (which we know since g++-4.2 doesn't support C++11) statically link libgcc.
			set( ALL_C_FLAGS "-static-libgcc" )
		endif()
	elseif( NOT MINGW AND NOT HAIKU AND NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS" )
		# Generic GCC/Clang requires position independent executable to be enabled explicitly
		set( ALL_C_FLAGS "${ALL_C_FLAGS} -fPIE" )
		set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie" )
	endif( APPLE )
endif()

set( CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${REL_LINKER_FLAGS}" )
set( CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} ${REL_LINKER_FLAGS}" )
set( CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} ${REL_LINKER_FLAGS}" )

set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${ALL_C_FLAGS}" )
set( CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${REL_C_FLAGS}" )
set( CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} ${REL_C_FLAGS}" )
set( CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} ${REL_C_FLAGS}" )
set( CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${DEB_C_FLAGS} -D_DEBUG" )

set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ALL_C_FLAGS}" )
set( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${REL_C_FLAGS}" )
set( CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} ${REL_C_FLAGS}" )
set( CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${REL_C_FLAGS}" )
set( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${DEB_C_FLAGS} -D_DEBUG" )

option(FORCE_INTERNAL_BZIP2 "Use internal bzip2")
option(FORCE_INTERNAL_CPPDAP "Use internal cppdap" ON)

if (HAVE_VULKAN)
	add_subdirectory( libraries/ZVulkan )
endif()

if( MSVC )
	set(ABSL_MSVC_STATIC_RUNTIME ON)
endif()
add_subdirectory( libraries/abseil )

set(ZWIDGET_BUILD_EXAMPLE OFF CACHE INTERNAL "" FORCE)
add_subdirectory( libraries/ZWidget )

add_subdirectory( libraries/webp )

if( HAVE_VM_JIT AND UNIX )
	check_symbol_exists( "backtrace" "execinfo.h" HAVE_BACKTRACE )
	if( NOT HAVE_BACKTRACE )
		set( CMAKE_REQUIRED_FLAGS "-lexecinfo" )
		check_symbol_exists( "backtrace" "execinfo.h" HAVE_LIBEXECINFO )
		if( HAVE_LIBEXECINFO )
			set( ALL_C_FLAGS "${ALL_C_FLAGS} -lexecinfo" )
		else( HAVE_LIBEXECINFO )
			set( HAVE_VM_JIT NO )
		endif( HAVE_LIBEXECINFO )
		set( CMAKE_REQUIRED_FLAGS )
	endif( NOT HAVE_BACKTRACE )
endif( HAVE_VM_JIT AND UNIX )

if( ${HAVE_VM_JIT} )
	set( SKIP_INSTALL_ALL TRUE ) # Avoid installing asmjit alongside zdoom
	add_subdirectory( libraries/asmjit )
	set( ASMJIT_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libraries/asmjit )
	set( ASMJIT_LIBRARIES asmjit )
endif()

if( BZIP2_FOUND AND NOT FORCE_INTERNAL_BZIP2 )
	message( STATUS "Using system bzip2 library, includes found at ${BZIP2_INCLUDE_DIR}" )
else()
	message( STATUS "Using internal bzip2 library" )
	add_subdirectory( libraries/bzip2 )
	set( BZIP2_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/libraries/bzip2" )
	set( BZIP2_LIBRARIES bz2 )
	set( BZIP2_LIBRARY bz2 )
endif()

if ( CPPDAP_FOUND AND NOT FORCE_INTERNAL_CPPDAP )
	message( STATUS "Using system cppdap library, includes found at ${CPPDAP_INCLUDE_DIR}" )
else()
	message( STATUS "Using internal cppdap library" )
	add_subdirectory( libraries/cppdap )
	set( CPPDAP_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/libraries/cppdap" )
	set( CPPDAP_LIBRARIES cppdap )
	set( CPPDAP_LIBRARY cppdap )
	set( CPPDAP_FOUND TRUE )
endif()

# ZMUSIC
set( BUILD_SHARED_LIBS OFF CACHE INTERNAL "" FORCE )
add_subdirectory( libraries/ZMusic )
set( ZMUSIC_ROOT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/libraries/zmusic" )
set( ZMUSIC_INCLUDE_DIR ${ZMUSIC_ROOT_PATH}/include )
set( ZMUSIC_LIBRARIES zmusic )

set( LZMA_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/libraries/lzma/C" )

if( NOT CMAKE_CROSSCOMPILING )
	if( NOT CROSS_EXPORTS )
		set( CROSS_EXPORTS "" )
	endif()
endif()

# Install the entire docs directory in the distributed zip package
if( WIN32 )
	set( INSTALL_DOCS_PATH docs CACHE STRING "Directory where the documentation will be placed during install." )
else()
	set( INSTALL_DOCS_PATH share/doc/${ZDOOM_EXE_NAME} CACHE STRING "Directory where the zdoom documentation will be placed during install." )
endif()
install(DIRECTORY docs/
		DESTINATION ${INSTALL_DOCS_PATH}
		COMPONENT "Documentation")

option( DYN_OPENAL "Dynamically load OpenAL" ON )
option( BUILD_NONFREE "Build non-free assets used by commercial games" ON )

add_subdirectory( src )
add_subdirectory( libraries/lzma )
add_subdirectory( tools )
add_subdirectory( wadsrc )
add_subdirectory( wadsrc_bm )
add_subdirectory( wadsrc_lights )
add_subdirectory( wadsrc_extra )
add_subdirectory( wadsrc_widepix )

if( ZMUSIC_SYSTEM_INSTALL )
	# Internal ZMusic library provides miniz target, miniz.h file in partuclar
	# It's not the case with system-wide ZMusic installation
	add_subdirectory( libraries/miniz )
endif()

if( NOT CMAKE_CROSSCOMPILING )
	export(TARGETS ${CROSS_EXPORTS} FILE "${CMAKE_BINARY_DIR}/ImportExecutables.cmake" )
endif()
