CMake: Define targets early instead of gathering variables

This commit is contained in:
TellowKrinkle 2021-07-18 01:45:18 -05:00 committed by Kojin
parent ae2ae8b982
commit 7ed0f38623
3 changed files with 75 additions and 104 deletions

View File

@ -5,8 +5,10 @@ if(NOT TOP_CMAKE_WAS_SOURCED)
It is advice to delete all wrongly generated cmake stuff => CMakeFiles & CMakeCache.txt") It is advice to delete all wrongly generated cmake stuff => CMakeFiles & CMakeCache.txt")
endif(NOT TOP_CMAKE_WAS_SOURCED) endif(NOT TOP_CMAKE_WAS_SOURCED)
# variable with all sources of this library add_library(Utilities)
set(UtilitiesSources
# Utilities sources
target_sources(Utilities PRIVATE
VirtualMemory.cpp VirtualMemory.cpp
AlignedMalloc.cpp AlignedMalloc.cpp
../../include/Utilities/FixedPointTypes.inl ../../include/Utilities/FixedPointTypes.inl
@ -38,8 +40,8 @@ set(UtilitiesSources
wxHelpers.cpp wxHelpers.cpp
) )
# variable with all headers of this library # Utilities headers
set(UtilitiesHeaders target_sources(Utilities PRIVATE
../../include/Utilities/Assertions.h ../../include/Utilities/Assertions.h
../../include/Utilities/boost_spsc_queue.hpp ../../include/Utilities/boost_spsc_queue.hpp
../../include/Utilities/CheckedStaticBox.h ../../include/Utilities/CheckedStaticBox.h
@ -76,40 +78,31 @@ set(UtilitiesHeaders
) )
if(APPLE) if(APPLE)
LIST(APPEND UtilitiesSources target_sources(Utilities PRIVATE
Darwin/DarwinThreads.cpp Darwin/DarwinThreads.cpp
Darwin/DarwinMisc.cpp Darwin/DarwinMisc.cpp
Darwin/DarwinSemaphore.cpp Darwin/DarwinSemaphore.cpp
) )
elseif(Windows) elseif(Windows)
LIST(APPEND UtilitiesSources target_sources(Utilities PRIVATE
x86/MemcpyFast.cpp x86/MemcpyFast.cpp
Windows/WinThreads.cpp Windows/WinThreads.cpp
Windows/WinHostSys.cpp Windows/WinHostSys.cpp
Windows/WinMisc.cpp Windows/WinMisc.cpp
) )
else() else()
LIST(APPEND UtilitiesSources target_sources(Utilities PRIVATE
Linux/LnxThreads.cpp Linux/LnxThreads.cpp
Linux/LnxMisc.cpp Linux/LnxMisc.cpp
Semaphore.cpp Semaphore.cpp
) )
endif() endif()
set(UtilitiesFinalSources target_link_libraries(Utilities PRIVATE ${LIBC_LIBRARIES} PUBLIC wxWidgets::all)
${UtilitiesSources} target_compile_features(Utilities PUBLIC cxx_std_17)
${UtilitiesHeaders} target_include_directories(Utilities PUBLIC ../../../3rdparty/include ../../include PRIVATE ../../include/Utilities .)
) target_compile_definitions(Utilities PUBLIC "${PCSX2_DEFS}")
target_compile_options(Utilities PRIVATE "${PCSX2_WARNINGS}")
foreach(library Utilities Utilities_NO_TLS) if(COMMAND target_precompile_headers)
add_library(${library} "${UtilitiesFinalSources}") target_precompile_headers(Utilities PRIVATE PrecompiledHeader.h)
target_link_libraries(${library} PRIVATE ${LIBC_LIBRARIES} PUBLIC wxWidgets::all) endif()
target_compile_features(${library} PUBLIC cxx_std_17)
target_include_directories(${library} PUBLIC ../../../3rdparty/include ../../include PRIVATE ../../include/Utilities .)
target_compile_definitions(${library} PUBLIC "${PCSX2_DEFS}")
target_compile_options(${library} PRIVATE "${PCSX2_WARNINGS}")
if(COMMAND target_precompile_headers)
target_precompile_headers(${library} PRIVATE PrecompiledHeader.h)
endif()
endforeach()
target_compile_definitions(Utilities_NO_TLS PUBLIC PCSX2_THREAD_LOCAL=0)

View File

@ -5,10 +5,10 @@ if(NOT TOP_CMAKE_WAS_SOURCED)
It is advice to delete all wrongly generated cmake stuff => CMakeFiles & CMakeCache.txt") It is advice to delete all wrongly generated cmake stuff => CMakeFiles & CMakeCache.txt")
endif(NOT TOP_CMAKE_WAS_SOURCED) endif(NOT TOP_CMAKE_WAS_SOURCED)
set(x86emitterFinalFlags ${CommonFlags}) add_library(x86emitter)
# variable with all sources of this library # x86emitter sources
set(x86emitterSources target_sources(x86emitter PRIVATE
bmi.cpp bmi.cpp
cpudetect.cpp cpudetect.cpp
fpu.cpp fpu.cpp
@ -21,8 +21,8 @@ set(x86emitterSources
simd.cpp simd.cpp
x86emitter.cpp) x86emitter.cpp)
# variable with all headers of this library # x86emitter headers
set(x86emitterHeaders target_sources(x86emitter PRIVATE
../../include/x86emitter/implement/dwshift.h ../../include/x86emitter/implement/dwshift.h
../../include/x86emitter/implement/group1.h ../../include/x86emitter/implement/group1.h
../../include/x86emitter/implement/group2.h ../../include/x86emitter/implement/group2.h
@ -52,21 +52,12 @@ set(x86emitterHeaders
) )
if(Windows) if(Windows)
LIST(APPEND x86emitterSources WinCpuDetect.cpp) target_sources(x86emitter PRIVATE WinCpuDetect.cpp)
else() else()
LIST(APPEND x86emitterSources LnxCpuDetect.cpp) target_sources(x86emitter PRIVATE LnxCpuDetect.cpp)
endif() endif()
set(x86emitterFinalSources target_link_libraries(x86emitter PRIVATE wxWidgets::all)
${x86emitterSources}
${x86emitterHeaders}
)
set(x86emitterFinalLibs
wxWidgets::all
)
add_pcsx2_lib(x86emitter "${x86emitterFinalSources}" "${x86emitterFinalLibs}" "${x86emitterFinalFlags}")
target_compile_features(x86emitter PUBLIC cxx_std_17) target_compile_features(x86emitter PUBLIC cxx_std_17)
target_include_directories(x86emitter PUBLIC ../../include PRIVATE ../../include/x86emitter) target_include_directories(x86emitter PUBLIC ../../include PRIVATE ../../include/x86emitter)
target_compile_definitions(x86emitter PUBLIC "${PCSX2_DEFS}") target_compile_definitions(x86emitter PUBLIC "${PCSX2_DEFS}")

View File

@ -1,5 +1,16 @@
include(macros/GlibCompileResourcesSupport) include(macros/GlibCompileResourcesSupport)
add_executable(PCSX2)
target_compile_features(PCSX2 PRIVATE cxx_std_17)
target_compile_definitions(PCSX2 PUBLIC "${PCSX2_DEFS}")
target_compile_options(PCSX2 PRIVATE "${PCSX2_WARNINGS}")
if (PACKAGE_MODE)
install(TARGETS PCSX2 DESTINATION ${CMAKE_INSTALL_BINDIR})
else()
install(TARGETS PCSX2 DESTINATION ${CMAKE_SOURCE_DIR}/bin)
endif()
if (openSUSE) if (openSUSE)
# openSUSE don't install wx in a standard library system # openSUSE don't install wx in a standard library system
@ -14,8 +25,6 @@ if(NOT TOP_CMAKE_WAS_SOURCED)
It is advice to delete all wrongly generated cmake stuff => CMakeFiles & CMakeCache.txt") It is advice to delete all wrongly generated cmake stuff => CMakeFiles & CMakeCache.txt")
endif() endif()
include_directories ("${CMAKE_SOURCE_DIR}/3rdparty/xbyak/")
set(CommonFlags set(CommonFlags
-fno-strict-aliasing -fno-strict-aliasing
-Wstrict-aliasing # Allow to track strict aliasing issue. -Wstrict-aliasing # Allow to track strict aliasing issue.
@ -27,38 +36,35 @@ set(CommonFlags
if(GCC_VERSION VERSION_EQUAL "8.0" OR GCC_VERSION VERSION_GREATER "8.0") if(GCC_VERSION VERSION_EQUAL "8.0" OR GCC_VERSION VERSION_GREATER "8.0")
# gs is pretty bad at this # gs is pretty bad at this
set(CommonFlags ${CommonFlags} -Wno-packed-not-aligned -Wno-class-memaccess) list(APPEND CommonFlags -Wno-packed-not-aligned -Wno-class-memaccess)
endif() endif()
target_compile_options(PCSX2 PRIVATE ${CommonFlags})
if ("${PGO}" STREQUAL "generate") if ("${PGO}" STREQUAL "generate")
set(pcsx2FinalFlags -fprofile-generate) target_compile_options(PCSX2 PRIVATE -fprofile-generate)
elseif("${PGO}" STREQUAL "use") elseif("${PGO}" STREQUAL "use")
set(pcsx2FinalFlags -fprofile-use) target_compile_options(PCSX2 PRIVATE -fprofile-use)
else()
set(pcsx2FinalFlags "")
endif() endif()
set(pcsx2FinalFlags ${pcsx2FinalFlags} ${CommonFlags})
if(PORTAUDIO_FOUND) if(PORTAUDIO_FOUND)
set(pcsx2FinalFlags ${pcsx2FinalFlags} -DSPU2X_PORTAUDIO) target_compile_definitions(PCSX2 PRIVATE SPU2X_PORTAUDIO)
target_link_libraries(PCSX2 PRIVATE PkgConfig::PORTAUDIO)
endif() endif()
if(PULSEAUDIO_FOUND) if(PULSEAUDIO_FOUND)
set(pcsx2FinalFlags ${pcsx2FinalFlags} -DSPU2X_PULSEAUDIO) target_compile_definitions(PCSX2 PRIVATE SPU2X_PULSEAUDIO)
target_link_libraries(PCSX2 PRIVATE PulseAudio::PulseAudio)
endif() endif()
if(XDG_STD) if(XDG_STD)
set(pcsx2FinalFlags ${pcsx2FinalFlags} -DXDG_STD) target_compile_definitions(PCSX2 PRIVATE XDG_STD)
endif() endif()
if(SDL_FOUND OR SDL2_FOUND) if(SDL_FOUND OR SDL2_FOUND)
set(pcsx2FinalFlags ${pcsx2FinalFlags} -DSDL_BUILD) target_compile_definitions(PCSX2 PRIVATE SDL_BUILD)
endif() endif()
set(Output PCSX2)
# Main pcsx2 source # Main pcsx2 source
set(pcsx2Sources set(pcsx2Sources
Cache.cpp Cache.cpp
@ -264,7 +270,7 @@ set(pcsx2SPU2Sources
) )
if(PORTAUDIO_FOUND) if(PORTAUDIO_FOUND)
set(pcsx2SPU2Sources ${pcsx2SPU2Sources} SPU2/SndOut_Portaudio.cpp) list(APPEND pcsx2SPU2Sources SPU2/SndOut_Portaudio.cpp)
endif() endif()
# SPU2 headers # SPU2 headers
@ -474,19 +480,16 @@ set(pcsx2USBHeaders
USB/usb-eyetoy/cam-linux.h USB/usb-eyetoy/cam-linux.h
USB/qemu-usb/input-keymap-linux-to-qcode.h USB/qemu-usb/input-keymap-linux-to-qcode.h
) )
include_directories ("../3rdparty/jpgd/")
if(PULSEAUDIO_FOUND) if(PULSEAUDIO_FOUND)
set(pcsx2USBSources ${pcsx2USBSources} USB/usb-mic/audiodev-pulse.cpp) list(APPEND pcsx2USBSources USB/usb-mic/audiodev-pulse.cpp)
set(pcsx2USBHeaders ${pcsx2USBHeaders} USB/usb-mic/audiodev-pulse.h) list(APPEND pcsx2USBHeaders USB/usb-mic/audiodev-pulse.h)
endif() endif()
# PAD resources pre-compilation # PAD resources pre-compilation
set(PADImgHeader "${CMAKE_BINARY_DIR}/pcsx2/PAD/Linux/ImgHeader") set(PADImgHeader "${CMAKE_BINARY_DIR}/pcsx2/PAD/Linux/ImgHeader")
set(PADImg "${CMAKE_SOURCE_DIR}/pcsx2/PAD/Linux/Img") set(PADImg "${CMAKE_SOURCE_DIR}/pcsx2/PAD/Linux/Img")
include_directories ("${CMAKE_BINARY_DIR}/pcsx2/PAD/Linux/") target_include_directories(PCSX2 PRIVATE "${CMAKE_BINARY_DIR}/pcsx2/PAD/Linux/")
file(MAKE_DIRECTORY ${PADImgHeader}) file(MAKE_DIRECTORY ${PADImgHeader})
@ -721,8 +724,8 @@ set(pcsx2GSResources
GS/res/glsl/tfx_vgs.glsl) GS/res/glsl/tfx_vgs.glsl)
set(GSBin "${CMAKE_BINARY_DIR}/pcsx2/GS") set(GSBin "${CMAKE_BINARY_DIR}/pcsx2/GS")
include_directories ("${GSBin}") target_include_directories(PCSX2 PRIVATE "${GSBin}")
file(MAKE_DIRECTORY ${GSBin}) file(MAKE_DIRECTORY "${GSBin}")
add_custom_command( add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/pcsx2/GS/GS_res.h OUTPUT ${CMAKE_BINARY_DIR}/pcsx2/GS/GS_res.h
@ -1158,7 +1161,7 @@ set(pcsx2x86Headers
) )
# common Sources # common Sources
set(Common target_sources(PCSX2 PRIVATE
${pcsx2Sources} ${pcsx2Sources}
${pcsx2Headers} ${pcsx2Headers}
${pcsx2CDVDSources} ${pcsx2CDVDSources}
@ -1195,14 +1198,14 @@ set(Common
# platform sources # platform sources
# Linux # Linux
if(Linux) if(Linux)
set(Platform target_sources(PCSX2 PRIVATE
${pcsx2LinuxSources} ${pcsx2LinuxSources}
${pcsx2LinuxHeaders} ${pcsx2LinuxHeaders}
${pcsx2USBSources} ${pcsx2USBSources}
${pcsx2USBHeaders} ${pcsx2USBHeaders}
) )
set(Platform_Libs target_link_libraries(PCSX2 PRIVATE
PkgConfig::AIO PkgConfig::AIO
PkgConfig::EGL PkgConfig::EGL
PkgConfig::LIBUDEV PkgConfig::LIBUDEV
@ -1212,19 +1215,19 @@ if(Linux)
ALSA::ALSA ALSA::ALSA
) )
elseif(UNIX AND NOT APPLE) elseif(UNIX AND NOT APPLE)
set(Platform_Libs X11::X11) target_link_libraries(PCSX2 PRIVATE X11::X11)
endif() endif()
# Windows # Windows
if(Windows) if(Windows)
set(Platform target_sources(PCSX2 PRIVATE
${pcsx2WindowsSources} ${pcsx2WindowsSources}
${pcsx2WindowsHeaders}) ${pcsx2WindowsHeaders})
endif() endif()
# MacOSX # MacOSX
if(APPLE) if(APPLE)
set(Platform target_sources(PCSX2 PRIVATE
${pcsx2OSXSources} ${pcsx2OSXSources}
${pcsx2LinuxHeaders} ${pcsx2LinuxHeaders}
${pcsx2USBNullSources} ${pcsx2USBNullSources}
@ -1232,17 +1235,12 @@ if(APPLE)
endif() endif()
if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" OR ${CMAKE_SYSTEM_NAME} MATCHES "NetBSD") if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" OR ${CMAKE_SYSTEM_NAME} MATCHES "NetBSD")
set(Platform target_sources(PCSX2 PRIVATE
${pcsx2FreeBSDSources} ${pcsx2FreeBSDSources}
${pcsx2LinuxHeaders}) ${pcsx2LinuxHeaders})
endif() endif()
set(pcsx2FinalSources target_link_libraries(PCSX2 PRIVATE
${Common}
${Platform}
)
set(pcsx2FinalLibs
Utilities Utilities
x86emitter x86emitter
fmt::fmt fmt::fmt
@ -1261,17 +1259,8 @@ set(pcsx2FinalLibs
PNG::PNG PNG::PNG
Freetype::Freetype Freetype::Freetype
LibLZMA::LibLZMA LibLZMA::LibLZMA
${Platform_Libs}
) )
if(PORTAUDIO_FOUND)
set(pcsx2FinalLibs ${pcsx2FinalLibs} PkgConfig::PORTAUDIO)
endif()
if(PULSEAUDIO_FOUND)
set(pcsx2FinalLibs ${pcsx2FinalLibs} PulseAudio::PulseAudio)
endif()
### Generate the resources files ### Generate the resources files
file(MAKE_DIRECTORY ${res_bin}) file(MAKE_DIRECTORY ${res_bin})
@ -1291,41 +1280,39 @@ foreach(res_file IN ITEMS
endforeach() endforeach()
if(USE_VTUNE) if(USE_VTUNE)
set(pcsx2FinalLibs ${pcsx2FinalLibs} Vtune::Vtune) target_link_libraries(PCSX2 PRIVATE Vtune::Vtune)
endif() endif()
add_pcsx2_executable(${Output} "${pcsx2FinalSources}" "${pcsx2FinalLibs}" "${pcsx2FinalFlags}")
target_compile_features(${Output} PRIVATE cxx_std_17)
target_compile_definitions(${Output} PUBLIC "${PCSX2_DEFS}")
target_compile_options(${Output} PRIVATE "${PCSX2_WARNINGS}")
# additonal include directories # additonal include directories
target_include_directories(${Output} PRIVATE target_include_directories(PCSX2 PRIVATE
.
gui gui
x86 x86
${CMAKE_BINARY_DIR}/pcsx2/gui ${CMAKE_BINARY_DIR}/pcsx2/gui
${CMAKE_BINARY_DIR}/common/include/ ${CMAKE_BINARY_DIR}/common/include/
"${CMAKE_SOURCE_DIR}/3rdparty/jpgd/"
"${CMAKE_SOURCE_DIR}/3rdparty/xbyak/"
) )
if(COMMAND target_precompile_headers) if(COMMAND target_precompile_headers)
message("Using precompiled headers.") message("Using precompiled headers.")
target_precompile_headers(${Output} PRIVATE PrecompiledHeader.h) target_precompile_headers(PCSX2 PRIVATE PrecompiledHeader.h)
endif() endif()
if (APPLE) if (APPLE)
# MacOS defaults to having a maximum protection of the __DATA segment of rw (non-executable) # MacOS defaults to having a maximum protection of the __DATA segment of rw (non-executable)
# We have a bunch of page-sized arrays in bss that we use for jit # We have a bunch of page-sized arrays in bss that we use for jit
# Obviously not being able to make those arrays executable would be a problem # Obviously not being able to make those arrays executable would be a problem
target_link_options(${Output} PRIVATE -Wl,-segprot,__DATA,rwx,rw) target_link_options(PCSX2 PRIVATE -Wl,-segprot,__DATA,rwx,rw)
set_target_properties(${Output} PROPERTIES set_target_properties(PCSX2 PROPERTIES
MACOSX_BUNDLE true MACOSX_BUNDLE true
MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/gui/Resources/Info.plist.in" MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/gui/Resources/Info.plist.in"
OUTPUT_NAME PCSX2 OUTPUT_NAME PCSX2
) )
target_sources(${Output} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/gui/Resources/PCSX2.icns") target_sources(PCSX2 PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/gui/Resources/PCSX2.icns")
target_sources(${Output} PRIVATE "${CMAKE_SOURCE_DIR}/bin/GameIndex.yaml") target_sources(PCSX2 PRIVATE "${CMAKE_SOURCE_DIR}/bin/GameIndex.yaml")
set_source_files_properties("${CMAKE_CURRENT_SOURCE_DIR}/gui/Resources/PCSX2.icns" PROPERTIES MACOSX_PACKAGE_LOCATION Resources) set_source_files_properties("${CMAKE_CURRENT_SOURCE_DIR}/gui/Resources/PCSX2.icns" PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
set_source_files_properties("${CMAKE_SOURCE_DIR}/bin/GameIndex.yaml" PROPERTIES MACOSX_PACKAGE_LOCATION Resources) set_source_files_properties("${CMAKE_SOURCE_DIR}/bin/GameIndex.yaml" PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
@ -1338,10 +1325,10 @@ if (APPLE)
endif() endif()
add_custom_target(pcsx2-postprocess-bundle ${postprocessBundleType} add_custom_target(pcsx2-postprocess-bundle ${postprocessBundleType}
COMMAND ${CMAKE_COMMAND} "-DPCSX2_BUNDLE_PATH=$<TARGET_FILE_DIR:${Output}>/../.." COMMAND ${CMAKE_COMMAND} "-DPCSX2_BUNDLE_PATH=$<TARGET_FILE_DIR:PCSX2>/../.."
-P ${CMAKE_SOURCE_DIR}/cmake/Pcsx2PostprocessBundle.cmake -P ${CMAKE_SOURCE_DIR}/cmake/Pcsx2PostprocessBundle.cmake
) )
add_dependencies(pcsx2-postprocess-bundle ${Output}) add_dependencies(pcsx2-postprocess-bundle PCSX2)
# Set the right file types for .inl files in Xcode # Set the right file types for .inl files in Xcode
get_target_property(PCSX2_SOURCES PCSX2 SOURCES) get_target_property(PCSX2_SOURCES PCSX2 SOURCES)
@ -1354,9 +1341,9 @@ endif()
if(NOT DISABLE_SETCAP) if(NOT DISABLE_SETCAP)
if(PACKAGE_MODE) if(PACKAGE_MODE)
install(CODE "execute_process(COMMAND /bin/bash -c \"echo 'Enabling networking capability on Linux...';set -x; [ -f '${CMAKE_INSTALL_FULL_BINDIR}/${Output}' ] && sudo setcap 'CAP_NET_RAW+eip CAP_NET_ADMIN+eip' '${CMAKE_INSTALL_FULL_BINDIR}/${Output}'; set +x\")") install(CODE "execute_process(COMMAND /bin/bash -c \"echo 'Enabling networking capability on Linux...';set -x; [ -f '${CMAKE_INSTALL_FULL_BINDIR}/PCSX2' ] && sudo setcap 'CAP_NET_RAW+eip CAP_NET_ADMIN+eip' '${CMAKE_INSTALL_FULL_BINDIR}/PCSX2'; set +x\")")
else() else()
install(CODE "execute_process(COMMAND /bin/bash -c \"echo 'Enabling networking capability on Linux...';set -x; [ -f '${CMAKE_SOURCE_DIR}/bin/${Output}' ] && sudo setcap 'CAP_NET_RAW+eip CAP_NET_ADMIN+eip' '${CMAKE_SOURCE_DIR}/bin/${Output}'; set +x\")") install(CODE "execute_process(COMMAND /bin/bash -c \"echo 'Enabling networking capability on Linux...';set -x; [ -f '${CMAKE_SOURCE_DIR}/bin/PCSX2' ] && sudo setcap 'CAP_NET_RAW+eip CAP_NET_ADMIN+eip' '${CMAKE_SOURCE_DIR}/bin/PCSX2'; set +x\")")
endif() endif()
endif() endif()