find_package(PhysFS REQUIRED)
MARK_AS_ADVANCED(PHYSFS_LIBRARY PHYSFS_INCLUDE_DIR)
if(ENABLE_NLS)
	find_package (Intl REQUIRED)
endif()
find_package(Sodium 1.0.14 REQUIRED)

file(GLOB HEADERS "*.h")
file(GLOB SRC "*.cpp")

if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
	list(APPEND SRC "cocoa_wrapper.mm")
endif()

add_library(framework STATIC ${HEADERS} ${SRC})
set_property(TARGET framework PROPERTY FOLDER "lib")
include(WZTargetConfiguration)
WZ_TARGET_CONFIGURATION(framework)
target_link_libraries(framework PUBLIC ${PHYSFS_LIBRARY} unofficial-sodium::sodium)
target_link_libraries(framework PRIVATE utf8proc)
if(ENABLE_NLS)
	target_include_directories(framework PRIVATE "${Intl_INCLUDE_DIRS}")
	target_link_libraries(framework PUBLIC ${Intl_LIBRARIES})
endif()
target_include_directories(framework PUBLIC "${PHYSFS_INCLUDE_DIR}" "${CMAKE_SOURCE_DIR}/3rdparty")
target_compile_definitions(framework PRIVATE "YY_NO_UNISTD_H")
target_link_libraries(framework PUBLIC nlohmann_json optional-lite)
if(MSVC)
	target_compile_definitions(framework PUBLIC "_CRT_SECURE_NO_WARNINGS")
endif()
if (APPLE)
	target_link_libraries(framework PUBLIC "-framework ApplicationServices" "-framework AppKit")
endif (APPLE)

# Detect endianness (for crc.cpp)
if (NOT DEFINED CMAKE_CXX_BYTE_ORDER AND CMAKE_VERSION VERSION_LESS 3.20)
	# CMake < 3.20 does not have CMAKE_<LANG>_BYTE_ORDER
	# Instead, use the older TestBigEndian module (although this may not work for cross-compilation)
	if (NOT CMAKE_CROSSCOMPILING)
		include(TestBigEndian)
		test_big_endian(IS_BIGENDIAN)
		if (IS_BIGENDIAN)
			set(CMAKE_CXX_BYTE_ORDER "BIG_ENDIAN")
		else()
			set(CMAKE_CXX_BYTE_ORDER "LITTLE_ENDIAN")
		endif()
	else()
		message(WARNING "Unable to determine endianness for target architecture. Either upgrade to CMake 3.20+ or manually set the CMAKE_CXX_BYTE_ORDER cache variable to \"LITTLE_ENDIAN\" or \"BIG_ENDIAN\". Otherwise, will attempt preprocessor endian detection on compile.")
	endif()
endif()
if (DEFINED CMAKE_CXX_BYTE_ORDER AND NOT CMAKE_CXX_BYTE_ORDER STREQUAL "")
	message(STATUS "CMAKE_CXX_BYTE_ORDER: ${CMAKE_CXX_BYTE_ORDER}")
	if (CMAKE_CXX_BYTE_ORDER STREQUAL "LITTLE_ENDIAN")
		set_property(SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/crc.cpp" APPEND PROPERTY COMPILE_DEFINITIONS WZ_IS_BIG_ENDIAN=0)
	elseif (CMAKE_CXX_BYTE_ORDER STREQUAL "BIG_ENDIAN")
		set_property(SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/crc.cpp" APPEND PROPERTY COMPILE_DEFINITIONS WZ_IS_BIG_ENDIAN=1)
	else()
		message(FATAL_ERROR "Unsupported / unexpected endianness? (CMAKE_CXX_BYTE_ORDER: \"${CMAKE_CXX_BYTE_ORDER}\")")
	endif()
endif()
