cmake_minimum_required (VERSION 3.15)
project (edb LANGUAGES CXX VERSION 1.5.0)

enable_testing()

if(NOT EXISTS "${PROJECT_SOURCE_DIR}/src/qhexview/.git" OR NOT EXISTS "${PROJECT_SOURCE_DIR}/lib/gdtoa-desktop/.git")
	message(SEND_ERROR "The git submodules are not available. Please run:\ngit submodule update --init --recursive")
endif()

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/Modules/")

include("GNUInstallDirs")
include("CheckIncludeFileCXX")
include("DetectCompiler")
include("DetectOS")
include("DetectArchitecture")
include("EnableSanitizers")
include("EnableSTLDebug")
include("ProjectDefaults")
include("AddWarnings")
include("DetectGitBranch")

if(TARGET_COMPILER_GCC AND ${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 7.0)
	message(FATAL_ERROR "Your g++ version is too old. At least 7.0 is required.")
endif()

git_get_branch(GIT_BRANCH)

configure_file(include/version.h.in include/version.h)
include_directories("${PROJECT_BINARY_DIR}/include")

find_package(Capstone REQUIRED)
include_directories(${CAPSTONE_INCLUDE_DIRS})
link_directories(${CAPSTONE_LIBRARY_DIRS})

if (UNIX)
	find_package(PkgConfig REQUIRED)
	pkg_check_modules(GRAPHVIZ libgvc>=2.38.0)
	if(GRAPHVIZ_FOUND)
		add_definitions(-DENABLE_GRAPH)
	endif()
endif()

message(STATUS "Checking for module 'double-conversion'")
# Using check_include_file_cxx instead of find_package etc. to support various versions of double-conversion. Some versions don't have CMake modules, some have a bit incompatible ones...
check_include_file_cxx("double-conversion/double-conversion.h" HAVE_DOUBLE_CONVERSION)
if(NOT HAVE_DOUBLE_CONVERSION)
   UNSET(HAVE_DOUBLE_CONVERSION CACHE)
   message(WARNING "libdouble-conversion header wasn't found. 32- and 64-bit floating-point values will be shown with max_digits10 digits of precision instead of shortest representation.")
else()
   find_library(DOUBLE_CONVERSION_LIBRARIES double-conversion)
   if(NOT DOUBLE_CONVERSION_LIBRARIES)
	   message(WARNING "libdouble-conversion library wasn't found. 32- and 64-bit floating-point values will be shown with max_digits10 digits of precision instead of shortest representation.")
   else()
       add_definitions("-DHAVE_DOUBLE_CONVERSION")
   endif()
endif()

find_package(Qt5Core)

include_directories("include")

if(TARGET_PLATFORM_LINUX)
	include_directories("include/os/unix")
	include_directories("include/os/unix/linux")
elseif(TARGET_PLATFORM_FREEBSD)
	include_directories("include/os/unix")
elseif(TARGET_PLATFORM_WINDOWS)
	include_directories("include/os/win32")
endif()

if(TARGET_ARCH_X86 OR TARGET_ARCH_ARM32)
	add_definitions(-DEDB_IS_32_BIT=true -DEDB_IS_64_BIT=false)
elseif(TARGET_ARCH_X64 OR TARGET_ARCH_ARM64)
	add_definitions(-DEDB_IS_32_BIT=false -DEDB_IS_64_BIT=true)
else()
	message(SEND_ERROR "Unexpected bitness: \"sizeof(void*)=${CMAKE_SIZEOF_VOID_P}.\"")
endif()

if(TARGET_ARCH_X86)
	add_definitions(-DEDB_X86)
	include_directories("include/arch/x86-generic")
elseif(TARGET_ARCH_X64)
	add_definitions(-DEDB_X86_64)
	include_directories("include/arch/x86-generic")
elseif(TARGET_ARCH_ARM32)
	add_definitions(-DEDB_ARM32)
	include_directories("include/arch/arm-generic")
elseif(TARGET_ARCH_ARM64)
	add_definitions(-DEDB_ARM64)
	include_directories("include/arch/arm-generic")
endif()

if(UNIX)
	if(TARGET_ARCH_FAMILY_X86)
		pkg_check_modules(GDTOA gdtoa-desktop)
		if(NOT GDTOA_FOUND)
			message(STATUS "gdtoa-desktop package wasn't found. Using built in version.")
		endif()
	endif()
endif()

add_subdirectory(src)
add_subdirectory(plugins)
add_subdirectory(lib)

install (FILES ${CMAKE_SOURCE_DIR}/edb.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
install (FILES ${CMAKE_SOURCE_DIR}/edb.desktop DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/applications/)
install (FILES ${CMAKE_SOURCE_DIR}/src/res/images/edb.png DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pixmaps/)
