diff --git a/CMakeLists.txt b/CMakeLists.txt index 60dac082..504948b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,16 +1,21 @@ -cmake_minimum_required(VERSION 3.13) - -include(CheckSymbolExists) -include(CheckLibraryExists) +cmake_minimum_required(VERSION 3.15) cmake_policy(VERSION 3.13) if (POLICY CMP0076) cmake_policy(SET CMP0076 NEW) endif() -set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15" CACHE STRING "Minimum OS X deployment version") +set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) -project(melonDS CXX) +project(melonDS C CXX) + +include(CheckSymbolExists) +include(CheckLibraryExists) +include(CMakeDependentOption) +include(CheckIPOSupported) + + +set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15" CACHE STRING "Minimum OS X deployment version") set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 17) @@ -24,18 +29,12 @@ list(GET VERSION_LIST 0 MELONDS_VERSION_MAJOR) list(GET VERSION_LIST 1 MELONDS_VERSION_MINOR) # Check if melonDS version is three digits or two digits list(LENGTH VERSION_LIST MELONDS_VER_LEN) -if (${MELONDS_VER_LEN} GREATER 2) +if (${MELONDS_VER_LEN} GREATER 2) list(GET VERSION_LIST 2 MELONDS_VERSION_PATCH) else() set(MELONDS_VERSION_PATCH 0) endif() - -check_library_exists(m pow "" LIBM) -if(LIBM) - link_libraries(m) -endif() - if (NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() @@ -61,74 +60,38 @@ detect_architecture("__i386__" x86) detect_architecture("__arm__" ARM) detect_architecture("__aarch64__" ARM64) -if (ARCHITECTURE STREQUAL x86_64 OR ARCHITECTURE STREQUAL ARM64) - option(ENABLE_JIT "Enable x64 JIT recompiler" ON) -endif() - -if (ENABLE_JIT) - add_definitions(-DJIT_ENABLED) - - option(ENABLE_JIT_PROFILING "Enable JIT profiling with VTune" OFF) - - if (ENABLE_JIT_PROFILING) - include(cmake/FindVTune.cmake) - add_definitions(-DJIT_PROFILING_ENABLED) - endif() -endif() - -if (CMAKE_BUILD_TYPE STREQUAL Release) - option(ENABLE_LTO "Enable link-time optimization" ON) -else() - option(ENABLE_LTO "Enable link-time optimization" OFF) -endif() - +cmake_dependent_option(ENABLE_JIT "Enable JIT recompiler" ON + "ARCHITECTURE STREQUAL x86_64 OR ARCHITECTURE STREQUAL ARM64" OFF) +cmake_dependent_option(ENABLE_JIT_PROFILING "Enable JIT profiling with VTune" OFF "ENABLE_JIT" OFF) option(ENABLE_OGLRENDERER "Enable OpenGL renderer" ON) -if (ENABLE_OGLRENDERER) - add_definitions(-DOGLRENDERER_ENABLED) +check_ipo_supported(RESULT IPO_SUPPORTED) +cmake_dependent_option(ENABLE_LTO "Enable link-time optimizations" "Release" "IPO_SUPPORTED" "OFF") + +if (ENABLE_LTO STREQUAL "Release") + set_property(GLOBAL PROPERTY INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE) +elseif(ENABLE_LTO STREQUAL "ON") + set_property(GLOBAL PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) endif() -if (CMAKE_BUILD_TYPE STREQUAL Debug) - add_compile_options(-Og) -endif() +set(CMAKE_C_FLAGS_DEBUG "-Og") +set(CMAKE_CXX_FLAGS_DEBUG "-Og") +set(CMAKE_C_FLAGS_RELEASE "-O3") +set(CMAKE_CXX_FLAGS_RELEASE "-O3") -if (CMAKE_BUILD_TYPE STREQUAL Release) - add_compile_options(-O3) - if (NOT APPLE) - add_link_options(-s) - endif() +if (NOT APPLE) + set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -s") endif() if (WIN32) - option(BUILD_STATIC "Statically link dependencies" OFF) + option(BUILD_STATIC "Statically link dependencies" OFF) endif() if (BUILD_STATIC AND WIN32) set(CMAKE_FIND_LIBRARY_SUFFIXES .a) endif() -if (ENABLE_LTO) - if (WIN32 OR APPLE) - add_compile_options(-flto) - add_link_options(-flto) - else() - add_compile_options(-flto -fPIC) - add_link_options(-flto -fuse-linker-plugin -pie) - endif() - if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - set(CMAKE_AR "gcc-ar") - set(CMAKE_RANLIB "gcc-ranlib") - elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - find_program(LLD NAMES ld.lld ld64.lld lld-link) - if (NOT LLD STREQUAL "LLD-NOTFOUND") - add_link_options(-fuse-ld=lld) - endif() - if (NOT APPLE) - set(CMAKE_AR "llvm-ar") - set(CMAKE_RANLIB "llvm-ranlib") - endif() - endif() -endif() +set(CMAKE_POSITION_INDEPENDENT_CODE ON) find_program(CCACHE "ccache") if (CCACHE) @@ -142,5 +105,5 @@ option(BUILD_QT_SDL "Build Qt/SDL frontend" ON) add_subdirectory(src) if (BUILD_QT_SDL) - add_subdirectory(src/frontend/qt_sdl) + add_subdirectory(src/frontend/qt_sdl) endif() diff --git a/cmake/FixInterfaceIncludes.cmake b/cmake/FixInterfaceIncludes.cmake new file mode 100644 index 00000000..2a3b5b12 --- /dev/null +++ b/cmake/FixInterfaceIncludes.cmake @@ -0,0 +1,24 @@ +# The entire codebase quire reasonably does things like #include or +# CMake apparently doesn't think you should be doing this, so just includes $PREFIX/include/packagename for a given +# package as include directories when using `target_link_libraries` with an imported target, this hacky function fixes +# that up so includes can keep working as they always did but we can still use fancy imported targets. +# This is stupid. + +function(fix_interface_includes) + foreach (target ${ARGN}) + set(NEW_DIRS) + get_target_property(DIRS "${target}" INTERFACE_INCLUDE_DIRECTORIES) + + foreach (DIR ${DIRS}) + get_filename_component(PARENT_DIR "${DIR}" DIRECTORY) + + if (PARENT_DIR MATCHES "include$") + list(APPEND NEW_DIRS "${PARENT_DIR}") + endif() + endforeach() + + list(APPEND DIRS ${NEW_DIRS}) + set_target_properties("${target}" PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${DIRS}") + endforeach() +endfunction() + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7288b540..6e765898 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,141 +1,146 @@ -project(core) - set (CMAKE_CXX_STANDARD 17) -add_library(core STATIC - ARCodeFile.cpp - AREngine.cpp - ARM.cpp - ARM_InstrTable.h - ARMInterpreter.cpp - ARMInterpreter_ALU.cpp - ARMInterpreter_Branch.cpp - ARMInterpreter_LoadStore.cpp - CP15.cpp - CRC32.cpp - DMA.cpp - DMA_Timings.h - DSi.cpp - DSi_AES.cpp - DSi_Camera.cpp - DSi_DSP.cpp - DSi_I2C.cpp - DSi_NAND.cpp - DSi_NDMA.cpp - DSi_NWifi.cpp - DSi_SD.cpp - DSi_SPI_TSC.cpp - FATStorage.cpp - FIFO.h - GBACart.cpp - GPU.cpp - GPU2D.cpp - GPU2D_Soft.cpp - GPU3D.cpp - GPU3D_Soft.cpp - melonDLDI.h - NDS.cpp - NDSCart.cpp - Platform.h - ROMList.h - FreeBIOS.h - RTC.cpp - Savestate.cpp - SPI.cpp - SPU.cpp - types.h - version.h - Wifi.cpp - WifiAP.cpp +include(FixInterfaceIncludes) - fatfs/diskio.c - fatfs/ff.c - fatfs/ffsystem.c - fatfs/ffunicode.c - fatfs/ffconf.h - - sha1/sha1.c - tiny-AES-c/aes.c - xxhash/xxhash.c -) +add_library(core STATIC + ARCodeFile.cpp + AREngine.cpp + ARM.cpp + ARM_InstrTable.h + ARMInterpreter.cpp + ARMInterpreter_ALU.cpp + ARMInterpreter_Branch.cpp + ARMInterpreter_LoadStore.cpp + CP15.cpp + CRC32.cpp + DMA.cpp + DMA_Timings.h + DSi.cpp + DSi_AES.cpp + DSi_Camera.cpp + DSi_DSP.cpp + DSi_I2C.cpp + DSi_NAND.cpp + DSi_NDMA.cpp + DSi_NWifi.cpp + DSi_SD.cpp + DSi_SPI_TSC.cpp + FATStorage.cpp + FIFO.h + GBACart.cpp + GPU.cpp + GPU2D.cpp + GPU2D_Soft.cpp + GPU3D.cpp + GPU3D_Soft.cpp + melonDLDI.h + NDS.cpp + NDSCart.cpp + Platform.h + ROMList.h + FreeBIOS.h + RTC.cpp + Savestate.cpp + SPI.cpp + SPU.cpp + types.h + version.h + Wifi.cpp + WifiAP.cpp + + fatfs/diskio.c + fatfs/ff.c + fatfs/ffsystem.c + fatfs/ffunicode.c + fatfs/ffconf.h + + sha1/sha1.c + tiny-AES-c/aes.c + xxhash/xxhash.c) if (ENABLE_OGLRENDERER) - target_sources(core PRIVATE - GPU_OpenGL.cpp - GPU_OpenGL_shaders.h - GPU3D_OpenGL.cpp - GPU3D_OpenGL_shaders.h - OpenGLSupport.cpp - ) + target_sources(core PRIVATE + GPU_OpenGL.cpp + GPU_OpenGL_shaders.h + GPU3D_OpenGL.cpp + GPU3D_OpenGL_shaders.h + OpenGLSupport.cpp) endif() if (ENABLE_JIT) - enable_language(ASM) + enable_language(ASM) - target_sources(core PRIVATE - ARM_InstrInfo.cpp + target_sources(core PRIVATE + ARM_InstrInfo.cpp - ARMJIT.cpp - ARMJIT_Memory.cpp + ARMJIT.cpp + ARMJIT_Memory.cpp - dolphin/CommonFuncs.cpp - ) + dolphin/CommonFuncs.cpp) - if (ARCHITECTURE STREQUAL x86_64) - target_sources(core PRIVATE - dolphin/x64ABI.cpp - dolphin/x64CPUDetect.cpp - dolphin/x64Emitter.cpp + if (ARCHITECTURE STREQUAL x86_64) + target_sources(core PRIVATE + dolphin/x64ABI.cpp + dolphin/x64CPUDetect.cpp + dolphin/x64Emitter.cpp - ARMJIT_x64/ARMJIT_Compiler.cpp - ARMJIT_x64/ARMJIT_ALU.cpp - ARMJIT_x64/ARMJIT_LoadStore.cpp - ARMJIT_x64/ARMJIT_Branch.cpp + ARMJIT_x64/ARMJIT_Compiler.cpp + ARMJIT_x64/ARMJIT_ALU.cpp + ARMJIT_x64/ARMJIT_LoadStore.cpp + ARMJIT_x64/ARMJIT_Branch.cpp - ARMJIT_x64/ARMJIT_Linkage.S - ) - endif() - if (ARCHITECTURE STREQUAL ARM64) - target_sources(core PRIVATE - dolphin/Arm64Emitter.cpp - dolphin/MathUtil.cpp + ARMJIT_x64/ARMJIT_Linkage.S) + endif() + if (ARCHITECTURE STREQUAL ARM64) + target_sources(core PRIVATE + dolphin/Arm64Emitter.cpp + dolphin/MathUtil.cpp - ARMJIT_A64/ARMJIT_Compiler.cpp - ARMJIT_A64/ARMJIT_ALU.cpp - ARMJIT_A64/ARMJIT_LoadStore.cpp - ARMJIT_A64/ARMJIT_Branch.cpp + ARMJIT_A64/ARMJIT_Compiler.cpp + ARMJIT_A64/ARMJIT_ALU.cpp + ARMJIT_A64/ARMJIT_LoadStore.cpp + ARMJIT_A64/ARMJIT_Branch.cpp - ARMJIT_A64/ARMJIT_Linkage.S - ) - endif() + ARMJIT_A64/ARMJIT_Linkage.S) + endif() endif() add_subdirectory(teakra EXCLUDE_FROM_ALL) -target_link_libraries(core teakra) +target_link_libraries(core PRIVATE teakra) +find_library(m MATH_LIBRARY) + +if (MATH_LIBRARY) + target_link_libraries(core PRIVATE ${MATH_LIBRARY}) +endif() if (ENABLE_OGLRENDERER) find_package(PkgConfig REQUIRED) - pkg_check_modules(EPOXY REQUIRED epoxy) + pkg_check_modules(Epoxy REQUIRED IMPORTED_TARGET epoxy) + fix_interface_includes(PkgConfig::Epoxy) - target_include_directories(core PRIVATE ${EPOXY_INCLUDE_DIRS}) - if (WIN32) - target_link_libraries(core ole32 comctl32 ws2_32 ${EPOXY_LIBRARIES}) - elseif (APPLE) - target_link_libraries(core ${EPOXY_LIBRARIES}) - else() - target_link_libraries(core rt ${EPOXY_LIBRARIES}) + target_include_directories(core PRIVATE PkgConfig::Epoxy) + target_link_libraries(core PUBLIC PkgConfig::Epoxy) + + target_compile_definitions(core PUBLIC OGLRENDERER_ENABLED) +endif() + +if (ENABLE_JIT) + target_compile_definitions(core PUBLIC JIT_ENABLED) + + + if (ENABLE_JIT_PROFILING) + include(cmake/FindVTune.cmake) + add_definitions(-DJIT_PROFILING_ENABLED) endif() -else() - if (WIN32) - target_link_libraries(core ole32 comctl32 ws2_32) - elseif (APPLE) - target_link_libraries(core) - else() - target_link_libraries(core rt) - endif() +endif() + +if (WIN32) + target_link_libraries(core PRIVATE ole32 comctl32 ws2_32) +elseif(NOT APPLE) + target_link_libraries(core PRIVATE rt) endif() if (ENABLE_JIT_PROFILING) - target_link_libraries(core jitprofiling) + target_link_libraries(core PRIVATE jitprofiling) endif() diff --git a/src/frontend/qt_sdl/CMakeLists.txt b/src/frontend/qt_sdl/CMakeLists.txt index ec88026f..5b23f96d 100644 --- a/src/frontend/qt_sdl/CMakeLists.txt +++ b/src/frontend/qt_sdl/CMakeLists.txt @@ -1,10 +1,12 @@ -project(qt_sdl) +include(CMakeDependentOption) -SET(SOURCES_QT_SDL +include(FixInterfaceIncludes) + +set(SOURCES_QT_SDL main.cpp main_shaders.h CheatsDialog.cpp - Config.cpp + Config.cpp EmuSettingsDialog.cpp PowerManagement/PowerManagementDialog.cpp PowerManagement/resources/battery.qrc @@ -28,9 +30,9 @@ SET(SOURCES_QT_SDL font.h Platform.cpp QPathInput.h - ROMManager.cpp - SaveManager.cpp - + ROMManager.cpp + SaveManager.cpp + ArchiveUtil.h ArchiveUtil.cpp @@ -40,7 +42,7 @@ SET(SOURCES_QT_SDL ../mic_blow.h ${CMAKE_SOURCE_DIR}/res/melon.qrc -) + ) if (APPLE) option(USE_QT6 "Build using Qt 6 instead of 5" ON) @@ -86,9 +88,12 @@ set(CMAKE_AUTORCC ON) find_package(Threads REQUIRED) find_package(PkgConfig REQUIRED) find_package(Iconv REQUIRED) -pkg_check_modules(SDL2 REQUIRED sdl2) -pkg_check_modules(SLIRP REQUIRED slirp) -pkg_check_modules(LIBARCHIVE REQUIRED libarchive) +find_package(SDL2 REQUIRED) +pkg_check_modules(Slirp REQUIRED IMPORTED_TARGET slirp) +pkg_check_modules(LibArchive REQUIRED IMPORTED_TARGET libarchive) + +fix_interface_includes(SDL2::SDL2 PkgConfig::Slirp PkgConfig::LibArchive) + add_compile_definitions(ARCHIVE_SUPPORT_ENABLED) if (WIN32 AND (CMAKE_BUILD_TYPE STREQUAL Release)) @@ -97,49 +102,40 @@ else() add_executable(melonDS ${SOURCES_QT_SDL}) endif() -target_link_libraries(melonDS ${CMAKE_THREAD_LIBS_INIT}) - -target_include_directories(melonDS PRIVATE ${SDL2_INCLUDE_DIRS} ${SDL2_PREFIX}/include ${SLIRP_INCLUDE_DIRS} ${LIBARCHIVE_INCLUDE_DIRS}) -target_link_directories(melonDS PRIVATE ${SDL2_LIBRARY_DIRS} ${SLIRP_LIBRARY_DIRS}) -target_link_directories(melonDS PRIVATE ${LIBARCHIVE_LIBRARY_DIRS}) +if (BUILD_STATIC) + qt_import_plugins(melonDS INCLUDE Qt::QSvgPlugin) + target_link_options(melonDS PRIVATE -static) +endif() target_include_directories(melonDS PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") target_include_directories(melonDS PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/..") target_include_directories(melonDS PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../..") -target_link_libraries(melonDS core) - -if (BUILD_STATIC) - target_link_libraries(melonDS -static ${SDL2_STATIC_LIBRARIES} ${SLIRP_STATIC_LIBRARIES} ${LIBARCHIVE_STATIC_LIBRARIES}) - qt_import_plugins(melonDS INCLUDE Qt::QSvgPlugin) -else() - target_link_libraries(melonDS ${SDL2_LIBRARIES} ${SLIRP_LIBRARIES} ${LIBARCHIVE_LIBRARIES}) -endif() +target_link_libraries(melonDS PRIVATE core) +target_link_libraries(melonDS PRIVATE SDL2::SDL2 PkgConfig::Slirp PkgConfig::LibArchive) if (NOT Iconv_IS_BUILT_IN) - target_link_libraries(melonDS ${Iconv_LIBRARIES}) + target_link_libraries(melonDS PRIVATE Iconv::Iconv) endif() if (UNIX) option(PORTABLE "Make a portable build that looks for its configuration in the current directory" OFF) - target_link_libraries(melonDS ${QT_LINK_LIBS}) - if (CMAKE_SYSTEM_NAME STREQUAL "Linux") - target_link_libraries(melonDS dl) - endif() + target_link_libraries(melonDS PRIVATE ${QT_LINK_LIBS} ${CMAKE_DL_LIBS}) elseif (WIN32) option(PORTABLE "Make a portable build that looks for its configuration in the current directory" ON) + configure_file("${CMAKE_SOURCE_DIR}/res/melon.rc.in" "${CMAKE_SOURCE_DIR}/melon.rc") target_sources(melonDS PUBLIC "${CMAKE_SOURCE_DIR}/melon.rc") - target_link_libraries(melonDS comctl32 d2d1 dwrite uxtheme ws2_32 iphlpapi gdi32) + target_link_libraries(melonDS PRIVATE comctl32 d2d1 dwrite uxtheme ws2_32 iphlpapi gdi32) if (BUILD_STATIC) - target_link_libraries(melonDS imm32 winmm version setupapi -static z zstd ${QT_LINK_LIBS}) + target_link_libraries(melonDS PRIVATE imm32 winmm version setupapi -static z zstd ${QT_LINK_LIBS}) else() - target_link_libraries(melonDS ${QT_LINK_LIBS}) + target_link_libraries(melonDS PRIVATE ${QT_LINK_LIBS}) endif() endif() if (PORTABLE) - add_definitions(-DPORTABLE) + target_compile_definitions(melonDS PRIVATE PORTABLE) endif() if (APPLE) @@ -152,7 +148,7 @@ if (APPLE) MACOSX_BUNDLE_INFO_PLIST ${CMAKE_SOURCE_DIR}/res/melon.plist.in OUTPUT_NAME melonDS RESOURCE "${RESOURCE_FILES}") - + option(MACOS_BUNDLE_LIBS "Bundle libraries with the app on macOS" OFF) option(MACOS_BUILD_DMG "Build DMG image of the macOS application bundle" OFF) @@ -170,10 +166,10 @@ endif() if (UNIX AND NOT APPLE) foreach(SIZE 16 32 48 64 128 256) install(FILES ${CMAKE_SOURCE_DIR}/res/icon/melon_${SIZE}x${SIZE}.png - DESTINATION ${CMAKE_INSTALL_PREFIX}/share/icons/hicolor/${SIZE}x${SIZE}/apps - RENAME net.kuribo64.melonDS.png) + DESTINATION ${CMAKE_INSTALL_PREFIX}/share/icons/hicolor/${SIZE}x${SIZE}/apps + RENAME net.kuribo64.melonDS.png) endforeach() install(FILES ${CMAKE_SOURCE_DIR}/res/net.kuribo64.melonDS.desktop DESTINATION ${CMAKE_INSTALL_PREFIX}/share/applications) install(TARGETS melonDS BUNDLE DESTINATION ${CMAKE_BINARY_DIR} RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin) -endif() +endif() \ No newline at end of file