# This file is automatically generated from cmake.toml - DO NOT EDIT
# See https://github.com/build-cpp/cmkr for more information

cmake_minimum_required(VERSION 3.16)

if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
	message(FATAL_ERROR "In-tree builds are not supported. Run CMake from a separate directory: cmake -B build")
endif()

set(CMKR_ROOT_PROJECT OFF)
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
	set(CMKR_ROOT_PROJECT ON)

	# Bootstrap cmkr and automatically regenerate CMakeLists.txt
	include("cmake/cmkr.cmake;" OPTIONAL RESULT_VARIABLE CMKR_INCLUDE_RESULT)
	if(CMKR_INCLUDE_RESULT)
		cmkr()
	endif()

	# Enable folder support
	set_property(GLOBAL PROPERTY USE_FOLDERS ON)

	# Create a configure-time dependency on cmake.toml to improve IDE support
	configure_file(cmake.toml cmake.toml COPYONLY)
endif()

# Variables
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

project(Mergen)

# Force release CRT (/MD) in all configs to match pre-built LLVM libs.
# Without this, Debug config uses /MDd which sets _ITERATOR_DEBUG_LEVEL=2,
# mismatching LLVM's _ITERATOR_DEBUG_LEVEL=0 and causing a linker error.
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL" CACHE STRING "MSVC runtime library" FORCE)

# Packages
find_package(Iced-Wrapper REQUIRED)

find_package(LLVM-Wrapper REQUIRED)

include(FetchContent)

# Fix warnings about DOWNLOAD_EXTRACT_TIMESTAMP
if(POLICY CMP0135)
	cmake_policy(SET CMP0135 NEW)
endif()
message(STATUS "Fetching linux-pe (be6d1f6)...")
FetchContent_Declare(linux-pe SYSTEM
	GIT_REPOSITORY
		"https://github.com/can1357/linux-pe"
	GIT_TAG
		be6d1f6
)
FetchContent_MakeAvailable(linux-pe)

if(ICED_NOT_FOUND) # NOTE: unnamed condition
	message(STATUS "Fetching Zydis (v4.1.0)...")
	FetchContent_Declare(Zydis SYSTEM
		GIT_REPOSITORY
			"https://github.com/zyantific/zydis"
		GIT_TAG
			v4.1.0
	)
	FetchContent_MakeAvailable(Zydis)

endif()
message(STATUS "Fetching magic_enum (a413fcc)...")
FetchContent_Declare(magic_enum SYSTEM
	GIT_REPOSITORY
		"https://github.com/Neargye/magic_enum"
	GIT_TAG
		a413fcc
)
FetchContent_MakeAvailable(magic_enum)

# Target: lifter
set(lifter_SOURCES
	"lifter/core/Lifter.cpp"
	"lifter/core/Utils.cpp"
	"lifter/analysis/PathSolver.cpp"
	"lifter/test/TestInstructions.cpp"
	"lifter/core/Includes.h"
	"lifter/core/OperandUtils.h"
	"lifter/core/Semantics.h"
	"lifter/core/Utils.h"
	"lifter/core/AbiCallContract.hpp"
	"lifter/core/CommandLineHelpers.hpp"
	"lifter/core/FunctionSignatures.hpp"
	"lifter/core/InlinePolicy.hpp"
	"lifter/core/LiftDriver.hpp"
	"lifter/core/LifterApplication.hpp"
	"lifter/core/LifterClass.hpp"
	"lifter/core/LifterClass_Concolic.hpp"
	"lifter/core/LifterClass_Symbolic.hpp"
	"lifter/core/LifterPipeline.hpp"
	"lifter/core/LifterPipelineStages.hpp"
	"lifter/core/LifterStages.hpp"
	"lifter/core/MergenPB.hpp"
	"lifter/core/RegisterManager.hpp"
	"lifter/core/RuntimeImageContext.hpp"
	"lifter/disasm/CommonDisassembler.hpp"
	"lifter/disasm/CommonMnemonics.h"
	"lifter/disasm/CommonRegisters.h"
	"lifter/disasm/IcedDisassembler.hpp"
	"lifter/disasm/IcedDisassemblerMnemonics.h"
	"lifter/disasm/IcedDisassemblerRegisters.h"
	"lifter/disasm/ZydisDisassembler.hpp"
	"lifter/disasm/ZydisDisassemblerMnemonics.h"
	"lifter/disasm/ZydisDisassemblerRegisters.h"
	"lifter/memory/FileReader.hpp"
	"lifter/memory/GEPTracker.h"
	"lifter/memory/GEPTracker.ipp"
	"lifter/memory/MemoryPolicy.hpp"
	"lifter/memory/MemoryPolicySetup.hpp"
	"lifter/analysis/CustomPasses.hpp"
	"lifter/analysis/PathSolver.h"
	"lifter/analysis/PathSolver.ipp"
	"lifter/semantics/OperandUtils.ipp"
	"lifter/semantics/PPMacros.hpp"
	"lifter/semantics/Semantics.ipp"
	"lifter/semantics/Semantics_Arithmetic.ipp"
	"lifter/semantics/Semantics_Bitwise.ipp"
	"lifter/semantics/Semantics_ControlFlow.ipp"
	"lifter/semantics/Semantics_Helpers.ipp"
	"lifter/semantics/Semantics_Misc.ipp"
	"lifter/test/TestInstructions.h"
	"lifter/test/Tester.hpp"
	cmake.toml
)

add_executable(lifter)

target_sources(lifter PRIVATE ${lifter_SOURCES})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${lifter_SOURCES})

target_include_directories(lifter PRIVATE
	"lifter/core"
	"lifter/semantics"
	"lifter/disasm"
	"lifter/memory"
	"lifter/analysis"
	"lifter/test"
)

if(DEFINED MERGEN_TEST) # testmode
	target_compile_definitions(lifter PRIVATE
		MERGEN_TEST
	)
endif()

target_compile_features(lifter PRIVATE
	cxx_std_20
)

target_compile_options(lifter PRIVATE
	"$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<NOT:$<CXX_COMPILER_ID:Clang>>>:/Zc:preprocessor>"
)

target_link_libraries(lifter PRIVATE
	LLVM-Wrapper
	linux-pe
	magic_enum
)

if(ICED_NOT_FOUND) # NOTE: unnamed condition
	target_link_libraries(lifter PRIVATE
		Zydis
	)
endif()

if(ICED_FOUND) # NOTE: unnamed condition
	target_link_libraries(lifter PRIVATE
		Iced_Wrapper
	)
endif()

if(WIN32) # windows
	target_link_libraries(lifter PRIVATE
		Ws2_32
	)
endif()

get_directory_property(CMKR_VS_STARTUP_PROJECT DIRECTORY ${PROJECT_SOURCE_DIR} DEFINITION VS_STARTUP_PROJECT)
if(NOT CMKR_VS_STARTUP_PROJECT)
	set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT lifter)
endif()

# Target: rewrite_microtests
set(rewrite_microtests_SOURCES
	"lifter/core/Lifter.cpp"
	"lifter/core/Utils.cpp"
	"lifter/analysis/PathSolver.cpp"
	"lifter/test/TestInstructions.cpp"
	"lifter/core/Includes.h"
	"lifter/core/OperandUtils.h"
	"lifter/core/Semantics.h"
	"lifter/core/Utils.h"
	"lifter/core/AbiCallContract.hpp"
	"lifter/core/CommandLineHelpers.hpp"
	"lifter/core/FunctionSignatures.hpp"
	"lifter/core/InlinePolicy.hpp"
	"lifter/core/LiftDriver.hpp"
	"lifter/core/LifterApplication.hpp"
	"lifter/core/LifterClass.hpp"
	"lifter/core/LifterClass_Concolic.hpp"
	"lifter/core/LifterClass_Symbolic.hpp"
	"lifter/core/LifterPipeline.hpp"
	"lifter/core/LifterPipelineStages.hpp"
	"lifter/core/LifterStages.hpp"
	"lifter/core/MergenPB.hpp"
	"lifter/core/RegisterManager.hpp"
	"lifter/core/RuntimeImageContext.hpp"
	"lifter/disasm/CommonDisassembler.hpp"
	"lifter/disasm/CommonMnemonics.h"
	"lifter/disasm/CommonRegisters.h"
	"lifter/disasm/IcedDisassembler.hpp"
	"lifter/disasm/IcedDisassemblerMnemonics.h"
	"lifter/disasm/IcedDisassemblerRegisters.h"
	"lifter/disasm/ZydisDisassembler.hpp"
	"lifter/disasm/ZydisDisassemblerMnemonics.h"
	"lifter/disasm/ZydisDisassemblerRegisters.h"
	"lifter/memory/FileReader.hpp"
	"lifter/memory/GEPTracker.h"
	"lifter/memory/GEPTracker.ipp"
	"lifter/memory/MemoryPolicy.hpp"
	"lifter/memory/MemoryPolicySetup.hpp"
	"lifter/analysis/CustomPasses.hpp"
	"lifter/analysis/PathSolver.h"
	"lifter/analysis/PathSolver.ipp"
	"lifter/semantics/OperandUtils.ipp"
	"lifter/semantics/PPMacros.hpp"
	"lifter/semantics/Semantics.ipp"
	"lifter/semantics/Semantics_Arithmetic.ipp"
	"lifter/semantics/Semantics_Bitwise.ipp"
	"lifter/semantics/Semantics_ControlFlow.ipp"
	"lifter/semantics/Semantics_Helpers.ipp"
	"lifter/semantics/Semantics_Misc.ipp"
	"lifter/test/TestInstructions.h"
	"lifter/test/Tester.hpp"
	cmake.toml
)

add_executable(rewrite_microtests)

target_sources(rewrite_microtests PRIVATE ${rewrite_microtests_SOURCES})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${rewrite_microtests_SOURCES})

target_include_directories(rewrite_microtests PRIVATE
	"lifter/core"
	"lifter/semantics"
	"lifter/disasm"
	"lifter/memory"
	"lifter/analysis"
	"lifter/test"
)

target_compile_definitions(rewrite_microtests PRIVATE
	MERGEN_TEST
)

target_compile_features(rewrite_microtests PRIVATE
	cxx_std_20
)

target_compile_options(rewrite_microtests PRIVATE
	"$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<NOT:$<CXX_COMPILER_ID:Clang>>>:/Zc:preprocessor>"
)

target_link_libraries(rewrite_microtests PRIVATE
	LLVM-Wrapper
	linux-pe
	magic_enum
)

if(ICED_NOT_FOUND) # NOTE: unnamed condition
	target_link_libraries(rewrite_microtests PRIVATE
		Zydis
	)
endif()

if(ICED_FOUND) # NOTE: unnamed condition
	target_link_libraries(rewrite_microtests PRIVATE
		Iced_Wrapper
	)
endif()

if(WIN32) # windows
	target_link_libraries(rewrite_microtests PRIVATE
		Ws2_32
	)
endif()
