Skip to content

Commit

Permalink
Merge branch 'moreCmake' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
narknon committed Oct 12, 2023
2 parents 852c937 + 3a52956 commit 9ea7187
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 245 deletions.
266 changes: 143 additions & 123 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,85 +5,42 @@ enable_language(CXX ASM_MASM)
include(CheckIPOSupported)
include(GNUInstallDirs)

# CMake Module / Tool Path -> START
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# CMake Module / Tool Path -> END

check_ipo_supported(RESULT supported OUTPUT error)
message("IPO - Supported: ${supported}; ${error}")

# Settings -> START
option(MAKE_DEPENDENCIES_SHARED "Make dependencies shared" OFF)
option(UE4SS_NO_CUSTOM_FLAGS "Disable compilation flags added by UE4SS" OFF)
option(UE4SS_CONSOLE_COLORS_ENABLED "Enable console colors" OFF)
# Settings -> END

# Projects -> START
set(PROJECTS "UE4SS" "UVTD")
# Projects -> END

# Tell WinAPI macros to map to unicode functions instead of ansi
add_compile_definitions(_UNICODE)
add_compile_definitions(UNICODE)

# Very temporary fix for ninja/clang thinking we should use MSVC simulation
unset(CMAKE_CXX_SIMULATE_ID)

message("CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}")

# Compile options -> START
set(PRIVATE_COMPILE_OPTIONS "")
set(PRIVATE_LINK_OPTIONS "")

if (NOT UE4SS_NO_CUSTOM_FLAGS)
if (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
set(PRIVATE_COMPILE_OPTIONS "$<IF:$<COMPILE_LANGUAGE:ASM_MASM>,$<$<CONFIG:GAME_WIN64_SHIPPING>:/Zi>,/MP;$<$<CONFIG:GAME_WIN64_SHIPPING>:/Zi>;/W3;/wd4005;/wd4251;/wd4068;/Zc:inline;/Zc:strictStrings>")
set(PRIVATE_LINK_OPTIONS /DEBUG:FULL)
set(UE4SS_SHIPPING_COMPILER_FLAGS "/O2 /Oi")
set(UE4SS_DEBUG_COMPILER_FLAGS "/Od /GS")
set(CMAKE_CXX_FLAGS_GAME_WIN64_SHIPPING "${UE4SS_SHIPPING_COMPILER_FLAGS}")
set(CMAKE_C_FLAGS_GAME_WIN64_SHIPPING "${UE4SS_SHIPPING_COMPILER_FLAGS}")
set(CMAKE_CXX_FLAGS_GAME_WIN64_DEBUG "${UE4SS_DEBUG_COMPILER_FLAGS}")
set(CMAKE_C_FLAGS_GAME_WIN64_DEBUG "${UE4SS_DEBUG_COMPILER_FLAGS}")
set(CMAKE_CXX_FLAGS_GAME_WIN64_DEV "${UE4SS_DEBUG_COMPILER_FLAGS}")
set(CMAKE_C_FLAGS_GAME_WIN64_DEV "${UE4SS_DEBUG_COMPILER_FLAGS}")
set(CMAKE_CXX_FLAGS_GAME_WIN64_TEST "${UE4SS_SHIPPING_COMPILER_FLAGS}")
set(CMAKE_C_FLAGS_GAME_WIN64_TEST "${UE4SS_SHIPPING_COMPILER_FLAGS}")
elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
set(PRIVATE_COMPILE_OPTIONS "$<IF:$<COMPILE_LANGUAGE:ASM_MASM>,,-g;-gcodeview;-fcolor-diagnostics;-Wno-unknown-pragmas;-Wno-unused-parameter;-fms-extensions;-Wignored-attributes;>")
set(PRIVATE_LINK_OPTIONS "$<IF:$<COMPILE_LANGUAGE:ASM_MASM>,,-g>")
set(UE4SS_SHIPPING_COMPILER_FLAGS "-O3")
set(UE4SS_DEBUG_COMPILER_FLAGS "-O0")
set(CMAKE_CXX_FLAGS_GAME_WIN64_SHIPPING "${UE4SS_SHIPPING_COMPILER_FLAGS}")
set(CMAKE_C_FLAGS_GAME_WIN64_SHIPPING "${UE4SS_SHIPPING_COMPILER_FLAGS}")
set(CMAKE_CXX_FLAGS_GAME_WIN64_DEBUG "${UE4SS_DEBUG_COMPILER_FLAGS}")
set(CMAKE_C_FLAGS_GAME_WIN64_DEBUG "${UE4SS_DEBUG_COMPILER_FLAGS}")
set(CMAKE_CXX_FLAGS_GAME_WIN64_DEV "${UE4SS_DEBUG_COMPILER_FLAGS}")
set(CMAKE_C_FLAGS_GAME_WIN64_DEV "${UE4SS_DEBUG_COMPILER_FLAGS}")
set(CMAKE_CXX_FLAGS_GAME_WIN64_TEST "${UE4SS_SHIPPING_COMPILER_FLAGS}")
set(CMAKE_C_FLAGS_GAME_WIN64_TEST "${UE4SS_SHIPPING_COMPILER_FLAGS}")
# add_compile_definitions(printf_s=printf)
elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
set(PRIVATE_COMPILE_OPTIONS "$<IF:$<COMPILE_LANGUAGE:ASM_MASM>,,-fms-extensions>")
set(UE4SS_SHIPPING_COMPILER_FLAGS "-O3")
set(UE4SS_DEBUG_COMPILER_FLAGS "-O0")
set(CMAKE_CXX_FLAGS_GAME_WIN64_SHIPPING "${UE4SS_SHIPPING_COMPILER_FLAGS}")
set(CMAKE_C_FLAGS_GAME_WIN64_SHIPPING "${UE4SS_SHIPPING_COMPILER_FLAGS}")
set(CMAKE_CXX_FLAGS_GAME_WIN64_DEBUG "${UE4SS_DEBUG_COMPILER_FLAGS}")
set(CMAKE_C_FLAGS_GAME_WIN64_DEBUG "${UE4SS_DEBUG_COMPILER_FLAGS}")
set(CMAKE_CXX_FLAGS_GAME_WIN64_DEV "${UE4SS_DEBUG_COMPILER_FLAGS}")
set(CMAKE_C_FLAGS_GAME_WIN64_DEV "${UE4SS_DEBUG_COMPILER_FLAGS}")
set(CMAKE_CXX_FLAGS_GAME_WIN64_TEST "${UE4SS_SHIPPING_COMPILER_FLAGS}")
set(CMAKE_C_FLAGS_GAME_WIN64_TEST "${UE4SS_SHIPPING_COMPILER_FLAGS}")
endif ()

if (UE4SS_CONSOLE_COLORS_ENABLED)
list(APPEND TARGET_COMPILE_DEFINITIONS UE4SS_CONSOLE_COLORS_ENABLED)
endif ()

add_compile_options("${PRIVATE_COMPILE_OPTIONS}")
add_link_options("${PRIVATE_LINK_OPTIONS}")
# Tell WinAPI macros to map to unicode functions instead of ansi
list(APPEND TARGET_COMPILE_DEFINITIONS _UNICODE)
list(APPEND TARGET_COMPILE_DEFINITIONS UNICODE)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Compile options -> END

# CLion fixes -> START
# There's a bug with CLion that causes it to not recognize our API macros.
# This in turn causes it to break completely as it cannot see our struct definitions properly.
if ($ENV{CLION_IDE})
add_compile_definitions(
list(APPEND TARGET_COMPILE_DEFINITIONS
RC_UE4SS_API=
RC_UE_API=
RC_ASM_API=
Expand All @@ -100,15 +57,116 @@ if ($ENV{CLION_IDE})
endif ()
# CLion fixes -> END


# Output path -> START
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/Output/bin/$<CONFIG>)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/Output/lib/$<CONFIG>)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/Output/archive/$<CONFIG>)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/Output/$<CONFIG>/${CMAKE_INSTALL_BINDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/Output/$<CONFIG>/${CMAKE_INSTALL_BINDIR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/Output/$<CONFIG>/${CMAKE_INSTALL_BINDIR})
# Output path -> END

set(ASMTK_NO_CUSTOM_FLAGS ON CACHE BOOL "" FORCE)
set(ASMJIT_NO_CUSTOM_FLAGS ON CACHE BOOL "" FORCE)
# Build configurations -> START
list(APPEND TARGET_TYPES "Game" "CasePreserving")
list(APPEND CONFIGURATION_TYPES "Debug" "Shipping" "Test")
# list(APPEND CONFIGURATION_TYPES "Development")
list(APPEND PLATFORM_TYPES "Win64")

# Definitions -> START
# Target definitions
set(Game_DEFINITIONS UE_GAME)
set(CasePreserving_DEFINITIONS ${Game_DEFINITIONS} WITH_CASE_PRESERVING_NAME)

# Configuration definitions
set(Dev_DEFINITIONS UE_BUILD_DEVELOPMENT)
set(Debug_DEFINITIONS UE_BUILD_DEBUG)
set(Shipping_DEFINITIONS UE_BUILD_SHIPPING)
set(Test_DEFINITIONS UE_BUILD_TEST)

# Platform definitions
set(Win64_DEFINITIONS PLATFORM_WINDOWS)

# Definitions -> END

# Initializing compiler-specific options
add_subdirectory("cmake/CompilerOptions")

# Compiler Options -> START
add_compile_options("$<$<NOT:$<COMPILE_LANGUAGE:ASM_MASM>>:${DEFAULT_COMPILER_FLAGS}>")
add_link_options("${DEFAULT_SHARED_LINKER_FLAGS}" "${DEFAULT_EXE_LINKER_FLAGS}")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Compiler Options -> END

# Compiler Flags -> START
function (listToString VARNAME VALUE)
string(REPLACE ";" " " result "${VALUE}")
set(${VARNAME} "${result}" PARENT_SCOPE)
endfunction ()

function (stringToList VARNAME VALUE)
string(REPLACE " " ";" result "${VALUE}")
set(${VARNAME} "${result}" PARENT_SCOPE)
endfunction ()

# APPEND is needed here because compiler_options might set additional args
# Target compiler flags
list(APPEND Game_FLAGS "")
list(APPEND CasePreserving_FLAGS ${Game_FLAGS})

# Configuration compiler flags
stringToList(DEBUG_FLAGS_LIST "${CMAKE_CXX_FLAGS_DEBUG}")
list(APPEND Debug_FLAGS ${DEBUG_FLAGS_LIST})
# list(APPEND Dev_FLAGS ${CMAKE_CXX_FLAGS_DEBUG})
stringToList(RELEASE_FLAGS_LIST "${CMAKE_CXX_FLAGS_RELEASE}")
list(APPEND Shipping_FLAGS ${RELEASE_FLAGS_LIST})
stringToList(RELWITHDEBINFO_FLAGS_LIST "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
list(APPEND Test_FLAGS ${RELWITHDEBINFO_FLAGS_LIST})
# Compiler Flags -> END

set(BUILD_CONFIGS)
set(TARGET_COMPILE_OPTIONS "$<$<NOT:$<COMPILE_LANGUAGE:ASM_MASM>>:${DEFAULT_COMPILER_FLAGS}>")
set(TARGET_LINK_OPTIONS "${DEFAULT_EXE_LINKER_FLAGS}" "${DEFAULT_SHARED_LINKER_FLAGS}")

foreach (target_type ${TARGET_TYPES})
foreach (configuration_type ${CONFIGURATION_TYPES})
foreach (platform_type ${PLATFORM_TYPES})
set(triplet ${target_type}__${configuration_type}__${platform_type})
list(APPEND BUILD_CONFIGS ${triplet})

set(definitions
${${target_type}_DEFINITIONS}
${${configuration_type}_DEFINITIONS}
${${platform_type}_DEFINITIONS})
list(APPEND TARGET_COMPILE_DEFINITIONS "$<$<STREQUAL:$<CONFIG>,${triplet}>:${definitions}>")
add_compile_definitions("${TARGET_COMPILE_DEFINITIONS}")
string(TOUPPER ${triplet} triplet_upper)

set(compiler_flags
${${target_type}_FLAGS}
${${configuration_type}_FLAGS}
${${platform_type}_FLAGS})

listToString(final_compiler_flags "${DEFAULT_COMPILER_FLAGS}" "${compiler_flags}")
set(CMAKE_CXX_FLAGS_${triplet_upper} "${final_compiler_flags}" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_${triplet_upper} "${final_compiler_flags}" CACHE STRING "" FORCE)

list(APPEND TARGET_COMPILE_OPTIONS "$<$<STREQUAL:$<CONFIG>,${triplet}>:${compiler_flags}>")

set(linker_flags
${${target_type}_LINKER_FLAGS}
${${configuration_type}_LINKER_FLAGS}
${${platform_type}_LINKER_FLAGS})

listToString(exe_linker_flags "${DEFAULT_EXE_LINKER_FLAGS}" "${linker_flags}")
set(CMAKE_EXE_LINKER_FLAGS_${triplet_upper} "${exe_linker_flags}" CACHE STRING "" FORCE)

listToString(shared_linker_flags "${DEFAULT_SHARED_LINKER_FLAGS}" "${linker_flags}")
set(CMAKE_SHARED_LINKER_FLAGS_${triplet_upper} "${shared_linker_flags}" CACHE STRING "" FORCE)

listToString(target_linker_flags "${DEFAULT_EXE_LINKER_FLAGS}" "${DEFAULT_SHARED_LINKER_FLAGS}" "${linker_flags}")
list(APPEND TARGET_LINK_OPTIONS "$<$<STREQUAL:$<CONFIG>,${triplet}>:${target_linker_flags}>")
endforeach ()
endforeach ()
endforeach ()
# Build configurations -> END

add_subdirectory("deps")

foreach (project ${PROJECTS})
Expand All @@ -134,81 +192,43 @@ endforeach ()

foreach (target ${TARGETS})
set_target_properties(${target} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/Output/${target}/$<CONFIG>
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/Output/${target}/$<CONFIG>
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/Output/${target}/$<CONFIG>)
endforeach ()
# Output path -> END

# Build configurations -> START
list(APPEND GAME_TYPES "Game")
list(APPEND PLATFORM_TYPES "Win64")
list(APPEND SHIPPING_TYPES "Dev" "Debug" "Shipping" "Test")

# Game definitions
set(Game_DEFINITIONS UE_GAME)

# Platform definitions
set(Win64_DEFINITIONS PLATFORM_WINDOWS)

# Shipping definitions
set(Dev_DEFINITIONS UE_BUILD_DEVELOPMENT)
set(Debug_DEFINITIONS UE_BUILD_DEBUG)
set(Shipping_DEFINITIONS UE_BUILD_SHIPPING)
set(Test_DEFINITIONS UE_BUILD_TEST)

# Fallback linker flags, if the triplet doesn't have flags defined
set(DEFAULT_SHARED_LINKER_FLAGS)
set(DEFAULT_EXE_LINKER_FLAGS)
# Fallback compiler flags if the triplet doesn't have flags defined
set(DEFAULT_CXX_FLAGS)
set(DEFAULT_C_FLAGS)

set(BUILD_CONFIGS)

foreach (game_type ${GAME_TYPES})
foreach (platform_type ${PLATFORM_TYPES})
foreach (shipping_type ${SHIPPING_TYPES})
set(triplet ${game_type}_${platform_type}_${shipping_type})
list(APPEND BUILD_CONFIGS ${triplet})

set(definitions
${${game_type}_DEFINITIONS}
${${platform_type}_DEFINITIONS}
${${shipping_type}_DEFINITIONS})
add_compile_definitions("$<$<STREQUAL:$<CONFIG>,${triplet}>:${definitions}>")

if (NOT DEFINED CMAKE_SHARED_LINKER_FLAGS_${triplet})
set(CMAKE_SHARED_LINKER_FLAGS_${triplet} ${DEFAULT_SHARED_LINKER_FLAGS})
endif ()

if (NOT DEFINED CMAKE_EXE_LINKER_FLAGS_${triplet})
set(CMAKE_EXE_LINKER_FLAGS_${triplet} ${DEFAULT_EXE_LINKER_FLAGS})
endif ()

if (NOT DEFINED CMAKE_CXX_FLAGS_${triplet})
set(CMAKE_CXX_FLAGS_${triplet} ${DEFAULT_CXX_FLAGS})
endif ()
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/Output/$<CONFIG>/${target}/${CMAKE_INSTALL_BINDIR}
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/Output/$<CONFIG>/${target}/${CMAKE_INSTALL_BINDIR}
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/Output/$<CONFIG>/${target}/${CMAKE_INSTALL_BINDIR})

get_target_property(TARGET_TYPE ${target} TYPE)
if (NOT ${TARGET_TYPE} STREQUAL UTILITY)
target_compile_options(${target} PUBLIC "${TARGET_COMPILE_OPTIONS}")
target_link_options(${target} PUBLIC "${TARGET_LINK_OPTIONS}")
target_compile_definitions(${target} PUBLIC "${TARGET_COMPILE_DEFINITIONS}")
endif ()

if (NOT DEFINED CMAKE_C_FLAGS_${triplet})
set(CMAKE_C_FLAGS_${triplet} ${DEFAULT_C_FLAGS})
get_target_property(DEPS ${target} LINK_LIBRARIES)
foreach (dep_target ${DEPS})
if (TARGET ${dep_target})
get_target_property(DEP_IMPORTED ${dep_target} IMPORTED)
get_target_property(DEP_TARGET_TYPE ${dep_target} TYPE)
if (NOT ${DEP_IMPORTED} AND NOT ${DEP_TARGET_TYPE} STREQUAL INTERFACE_LIBRARY)
target_compile_options(${dep_target} PUBLIC "${TARGET_COMPILE_OPTIONS}")
target_link_options(${dep_target} PUBLIC "${TARGET_LINK_OPTIONS}")
target_compile_definitions(${target} PUBLIC "${TARGET_COMPILE_DEFINITIONS}")
endif ()
endforeach ()
endif ()
endforeach ()
endforeach ()

# Default configuration -> START
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)

if (is_multi_config)
set(CMAKE_CONFIGURATION_TYPES ${BUILD_CONFIGS} CACHE STRING "" FORCE)
else ()
if (NOT CMAKE_BUILD_TYPE)
message("Defaulting to Game_Win64_Shipping")
set(CMAKE_BUILD_TYPE Game_Win64_Shipping CACHE STRING "" FORCE)
message("Defaulting to Game__Shipping__Win64")
set(CMAKE_BUILD_TYPE Game__Shipping__Win64 CACHE STRING "" FORCE)
endif ()

set_property(CACHE CMAKE_BUILD_TYPE PROPERTY HELPSTRING "Choose build type")
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${BUILD_CONFIGS})
endif ()

# Build configurations -> END
# Default configuration -> END
15 changes: 1 addition & 14 deletions UE4SS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,6 @@ option(UE4SS_LIB_BETA_IS_STARTED "Have beta releases started for the current maj
option(UE4SS_LIB_IS_BETA "Is this a beta release" ON)
# Release settings -> END

# Settings -> START
option(UE4SS_CONSOLE_COLORS_ENABLED "Enable console colors" OFF)
option(UE4SS_WITH_CASE_PRESERVING_NAME "Use case-preserving FNames" OFF)

if (UE4SS_CONSOLE_COLORS_ENABLED)
add_compile_definitions(UE4SS_CONSOLE_COLORS_ENABLED)
endif ()

if (UE4SS_WITH_CASE_PRESERVING_NAME)
add_compile_definitions(WITH_CASE_PRESERVING_NAME)
endif ()
# Settings -> END

# Codegen settings -> START
set(UE4SS_GENERATED_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/generated_include")
set(UE4SS_GENERATED_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/generated_src")
Expand Down Expand Up @@ -171,4 +158,4 @@ set_property(TARGET ${TARGET} PROPERTY INTERPROCEDURAL_OPTIMIZATION ON)
# Making sure that the linked C runtime is a dll so that C++ mods will work correctly
set_property(TARGET ${TARGET} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")

set_target_properties(${TARGET} PROPERTIES OUTPUT_NAME "ue4ss")
set_target_properties(${TARGET} PROPERTIES OUTPUT_NAME "UE4SS")
2 changes: 1 addition & 1 deletion UE4SS/proxy_generator/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ int _tmain(int argc, TCHAR* argv[])
cpp_file << " {" << endl;
cpp_file << " load_original_dll();" << endl;
cpp_file << " setup_functions();" << endl;
cpp_file << " LoadLibrary(STR(\"ue4ss.dll\"));" << endl;
cpp_file << " LoadLibrary(STR(\"UE4SS.dll\"));" << endl;
cpp_file << " }" << endl;
cpp_file << " else if (fdwReason == DLL_PROCESS_DETACH)" << endl;
cpp_file << " {" << endl;
Expand Down
Loading

0 comments on commit 9ea7187

Please sign in to comment.