# Setting it to a range tells it that it supports the features on the newer
# versions as well, avoiding setting policies.
cmake_minimum_required(VERSION 3.12...3.24)

#Enabling this cmake policy gets rid of warnings regarding LTO.
cmake_policy(SET CMP0069 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)

if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
	message(FATAL_ERROR "PCSX2 does not support in-tree builds.  Please make a build directory that is not the PCSX2 source directory and generate your CMake project there using either `cmake -B build_directory` or by running cmake from the build directory.")
endif()

# Project Name
project(Pcsx2)
option(PCAP "Build the libpcap ethernet backend for DEV9 (hard-links libpcap on POSIX)" OFF)
option(C89_EMITTER "Emit through the C89 macro core (header-only); OFF builds the C++ reference emitter instead" ON)
option(XE_AB "Shadow-compare verification: every converted emission is byte-checked against the C++ twin at the same address (verification builds only)" OFF)
option(PROFILER "Build the per-subsystem wall-time profiler; prints a breakdown every 60 frames. Off by default: no sources, no symbols, no overhead" OFF)
if(XE_AB)
	add_compile_definitions(PCSX2_XE_AB)
endif()
if(C89_EMITTER)
	add_compile_definitions(PCSX2_C89_EMITTER)
endif()

# Variable to check that people use the good file
set(TOP_CMAKE_WAS_SOURCED TRUE)

# set module path
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)

# Write binaries to the bin directory under the build directory.
# This makes it simple to run development builds directly out of the build directory.
if (UNIX AND NOT APPLE)
	set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
	set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
endif()

# include some generic functions to ensure correctness of the env
include(Pcsx2Utils)

check_no_parenthesis_in_path()
detectOperatingSystem()
check_compiler_version("7.0" "7.0")
if(NOT MSVC)
   find_program(CCACHE_FOUND ccache)
   if(CCACHE_FOUND)
       set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE_FOUND})
       set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ${CCACHE_FOUND})
   endif()
endif()

#-------------------------------------------------------------------------------
# Include specific module
# GNUInstallDirs must be done before BuildParameters
include(GNUInstallDirs)
# BuildParameters Must be done before SearchForStuff
include(BuildParameters)
include(SearchForStuff)

# make common
add_subdirectory(common)

# make pcsx2
add_subdirectory(pcsx2)
add_subdirectory(libretro)
