Skip to content
Open

C++20 #2275

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/bsd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ jobs:

- name: CMake
run: |
if [ "${{ matrix.operating_system }}" = "netbsd" ] && [ -x /usr/pkg/bin/clang ] && [ -x /usr/pkg/bin/clang++ ]; then
export CC=/usr/pkg/bin/clang
export CXX=/usr/pkg/bin/clang++
fi
cmake -B build -DUSE_LIBCDIO=ON -DCMAKE_BUILD_TYPE=Release -G Ninja -DLIBRETRO=${{ matrix.libretro }}
cmake --build build --config Release
shell: cpa.sh {0}
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@
[submodule "core/deps/freetype"]
path = core/deps/freetype
url = https://gitlab.freedesktop.org/freetype/freetype.git
[submodule "core/deps/fmt"]
path = core/deps/fmt
url = https://github.com/fmtlib/fmt.git
46 changes: 34 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ option(USE_LUA "Build with Lua support" ON)
option(ENABLE_GDB_SERVER "Build with GDB debugging support" OFF)
option(ENABLE_DC_PROFILER "Build with support for target machine (SH4) profiler" OFF)
option(ENABLE_FC_PROFILER "Build with support for host app (Flycast) profiler" OFF)
option(ENABLE_NO_STD_FORMAT_CHECK "Check changed core files for forbidden <format> includes" ON)
option(USE_DISCORD "Use Discord Presence API" OFF)
option(USE_LIBCDIO "Use libcdio for CDROM access" OFF)

Expand Down Expand Up @@ -268,23 +269,38 @@ else()
endif()

set_target_properties(${PROJECT_NAME} PROPERTIES
CMAKE_CXX_STANDARD 20
CMAKE_CXX_STANDARD_REQUIRED ON)
CXX_STANDARD 20
CXX_STANDARD_REQUIRED ON)

if(ENABLE_NO_STD_FORMAT_CHECK)
add_custom_target(check_no_std_format
COMMAND ${CMAKE_COMMAND} -DSOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR} -P ${CMAKE_CURRENT_SOURCE_DIR}/shell/cmake/CheckNoStdFormat.cmake
VERBATIM
)
add_dependencies(${PROJECT_NAME} check_no_std_format)
endif()

if(WINDOWS_STORE)
set(USE_OPENGL OFF)
set(USE_VULKAN OFF)
# SDL bootstrap requires C++/CX and is incompatible with C++20
set_source_files_properties(core/deps/SDL/src/main/winrt/SDL_winrt_main_NonXAML.cpp PROPERTIES COMPILE_FLAGS "/ZW /std:c++17")
endif()

# websocketpp is not C++20 compatible due to template-id destructors, waiting for v0.9 or we can switch to dev branch
if(MSVC)
set_source_files_properties(core/network/ice.cpp PROPERTIES COMPILE_FLAGS "/std:c++17")
else()
set_source_files_properties(core/network/ice.cpp PROPERTIES COMPILE_FLAGS "-std=c++17")
endif()

set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS_RELEASE -s)
if(MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE /GR /GS- /utf-8)
if(WINDOWS_STORE)
target_compile_options(${PROJECT_NAME} PRIVATE /ZW)
endif()
target_compile_options(${PROJECT_NAME} PRIVATE /GR /GS- /utf-8 "/Zc:char8_t-" "/Zc:__cplusplus")
else()
target_compile_options(${PROJECT_NAME} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:-fno-strict-aliasing>
$<$<COMPILE_LANGUAGE:CXX>:-fno-char8_t>
$<$<COMPILE_LANGUAGE:CXX>:-Wall>)
endif()

Expand Down Expand Up @@ -444,6 +460,10 @@ set(XXHASH_BUILD_XXHSUM OFF CACHE BOOL "Build the xxhsum binary")
add_subdirectory(core/deps/xxHash/cmake_unofficial)
target_link_libraries(${PROJECT_NAME} PRIVATE xxHash::xxhash)

set(FMT_INSTALL OFF CACHE BOOL "" FORCE)
add_subdirectory(core/deps/fmt EXCLUDE_FROM_ALL)
target_link_libraries(${PROJECT_NAME} PRIVATE fmt::fmt)

option(BUILD_SHARED_LIBS "Build shared library" OFF)
add_subdirectory(core/deps/glm)
target_link_libraries(${PROJECT_NAME} PRIVATE glm::glm)
Expand Down Expand Up @@ -512,6 +532,7 @@ if(NOT LIBRETRO)
# DreamPicoPort-API
option(DREAMPICOPORT_ADD_LIBUSB "Add internal libusb library" OFF) # Already included above, when applicable
add_subdirectory(core/deps/DreamPicoPort-API EXCLUDE_FROM_ALL)
set_target_properties(dream_pico_port_api PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED ON)
target_link_libraries(${PROJECT_NAME} PRIVATE dream_pico_port_api)
endif()

Expand All @@ -532,6 +553,12 @@ if(NOT LIBRETRO)

if((APPLE OR WIN32) AND TARGET SDL2::SDL2-static)
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2-static)
if(MSVC AND WINDOWS_STORE)
# SDL2 (C++/CX) is incompatible with C++20. /WHOLEARCHIVE prevents the linker from
# stripping WinRT metadata, avoiding LNK4264 and "Class not registered" errors.
# Transitional fix until we move to SDL3.
target_link_options(${PROJECT_NAME} PRIVATE "/WHOLEARCHIVE:SDL2-static")
endif()
elseif(TARGET SDL2::SDL2)
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2)
else()
Expand Down Expand Up @@ -1069,6 +1096,7 @@ set(IMGUI_BACKENDS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/core/deps/imgui/backends")

if(USE_VULKAN)
target_compile_definitions(${PROJECT_NAME} PUBLIC VK_ENABLE_BETA_EXTENSIONS VK_NO_PROTOTYPES)
target_compile_definitions(${PROJECT_NAME} PRIVATE VULKAN_HPP_NO_SPACESHIP_OPERATOR)
if(ANDROID)
target_compile_definitions(${PROJECT_NAME} PUBLIC VK_USE_PLATFORM_ANDROID_KHR)
elseif(X11_FOUND)
Expand Down Expand Up @@ -1103,9 +1131,6 @@ if(ENABLE_GDB_SERVER)
endif()

if("arm" IN_LIST ARCHITECTURE AND NOT APPLE)
if(MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE "/Zc:__cplusplus")
endif()
target_include_directories(${PROJECT_NAME} PRIVATE core/deps/vixl)
target_sources(${PROJECT_NAME} PRIVATE
core/rec-ARM/rec_arm.cpp
Expand Down Expand Up @@ -1139,9 +1164,6 @@ if("arm" IN_LIST ARCHITECTURE AND NOT APPLE)
core/deps/vixl/utils-vixl.h)
endif()
if("arm64" IN_LIST ARCHITECTURE)
if(MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE "/Zc:__cplusplus")
endif()
target_include_directories(${PROJECT_NAME} PRIVATE core/deps/vixl)
target_sources(${PROJECT_NAME} PRIVATE
core/deps/vixl/aarch64/abi-aarch64.h
Expand Down
1 change: 1 addition & 0 deletions core/deps/fmt
Submodule fmt added at 496843
8 changes: 0 additions & 8 deletions core/deps/imgui/misc/freetype/imgui_freetype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@
#include "imgui_freetype.h"
#include "imgui_internal.h" // ImMin,ImMax,ImFontAtlasBuild*,
#include <stdint.h>
#ifdef _MSC_VER
#pragma push_macro("generic")
#undef generic
#define generic __generic
#endif
#include <ft2build.h>
#include FT_FREETYPE_H // <freetype/freetype.h>
#include FT_MODULE_H // <freetype/ftmodapi.h>
Expand All @@ -55,9 +50,6 @@
#include FT_SYNTHESIS_H // <freetype/ftsynth.h>
#include FT_TRUETYPE_TABLES_H // <freetype/tttables.h>
#include FT_TRUETYPE_TAGS_H // <freetype/tttags.h>
#ifdef _MSC_VER
#pragma pop_macro("generic")
#endif

// Handle LunaSVG and PlutoSVG
#if defined(IMGUI_ENABLE_FREETYPE_LUNASVG) && defined(IMGUI_ENABLE_FREETYPE_PLUTOSVG)
Expand Down
1 change: 0 additions & 1 deletion core/oslib/http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ int get(const std::string& url, std::vector<u8>& content, std::string& contentTy

#ifdef _WIN32
#ifndef TARGET_UWP
#include "stdclass.h"
#include <windows.h>
#include <wininet.h>

Expand Down
35 changes: 15 additions & 20 deletions core/oslib/oslib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
#include "input/dreampotato.h"
#include "i18n.h"

#ifdef TARGET_UWP
#include <winrt/Windows.Storage.h>
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/Windows.Foundation.h>
#endif

namespace hostfs
{

Expand Down Expand Up @@ -341,37 +347,26 @@ static std::string getScreenshotsPath()

#elif defined(TARGET_UWP)
//TODO move to shell/uwp?
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Storage;
using namespace winrt;
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Storage;

void saveScreenshot(const std::string& name, const std::vector<u8>& data)
{
try {
StorageFolder^ folder = KnownFolders::PicturesLibrary; // or SavedPictures?
StorageFolder folder = KnownFolders::PicturesLibrary(); // or SavedPictures?
if (folder == nullptr) {
INFO_LOG(COMMON, "KnownFolders::PicturesLibrary is null");
throw FlycastException(i18n::Ts("Can't find Pictures library"));
}
nowide::wstackstring wstr;
wchar_t *wname = wstr.convert(name.c_str());
String^ msname = ref new String(wname);
ArrayReference<u8> arrayRef(const_cast<u8*>(&data[0]), data.size());

IAsyncOperation<StorageFile^>^ op = folder->CreateFileAsync(msname, CreationCollisionOption::FailIfExists);
cResetEvent asyncEvent;
op->Completed = ref new AsyncOperationCompletedHandler<StorageFile^>(
[&asyncEvent, &arrayRef](IAsyncOperation<StorageFile^>^ op, AsyncStatus) {
IAsyncAction^ action = FileIO::WriteBytesAsync(op->GetResults(), arrayRef);
action->Completed = ref new AsyncActionCompletedHandler(
[&asyncEvent](IAsyncAction^, AsyncStatus){
asyncEvent.Set();
});
});
asyncEvent.Wait();

StorageFile file = folder.CreateFileAsync(wname, CreationCollisionOption::FailIfExists).get();
FileIO::WriteBytesAsync(file, data).get();
}
catch (COMException^ e) {
WARN_LOG(COMMON, "Save screenshot failed: %S", e->Message->Data());
catch (hresult_error const& e) {
WARN_LOG(COMMON, "Save screenshot failed: %ls", e.message().c_str());
throw FlycastException("");
}
}
Expand Down
39 changes: 32 additions & 7 deletions core/rend/dx11/dx11context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
#ifdef TARGET_UWP
#include <windows.h>
#include <gamingdeviceinformation.h>
#include <winrt/Windows.Graphics.Display.Core.h>
#include <winrt/Windows.Graphics.Display.h>
#include <winrt/Windows.UI.ViewManagement.h>
#include <winrt/Windows.Foundation.h>
#endif

#include "nowide/stackstring.hpp"
Expand All @@ -47,14 +51,35 @@ bool DX11Context::init(bool keepCurrentWindow)
#ifdef TARGET_UWP
GAMING_DEVICE_MODEL_INFORMATION info {};
GetGamingDeviceModelInformation(&info);
if (info.vendorId == GAMING_DEVICE_VENDOR_ID_MICROSOFT && info.deviceId != GAMING_DEVICE_DEVICE_ID_NONE)
bool isXbox = info.vendorId == GAMING_DEVICE_VENDOR_ID_MICROSOFT && info.deviceId != GAMING_DEVICE_DEVICE_ID_NONE;
if (isXbox)
{
Windows::Graphics::Display::Core::HdmiDisplayInformation^ dispInfo = Windows::Graphics::Display::Core::HdmiDisplayInformation::GetForCurrentView();
Windows::Graphics::Display::Core::HdmiDisplayMode^ displayMode = dispInfo->GetCurrentDisplayMode();
NOTICE_LOG(RENDERER, "HDMI resolution: %d x %d", displayMode->ResolutionWidthInRawPixels, displayMode->ResolutionHeightInRawPixels);
settings.display.width = displayMode->ResolutionWidthInRawPixels;
settings.display.height = displayMode->ResolutionHeightInRawPixels;
settings.display.uiScale = settings.display.width / 1920.0f * 1.4f;
using namespace winrt::Windows::Graphics::Display::Core;
HdmiDisplayInformation dispInfo = HdmiDisplayInformation::GetForCurrentView();
if (dispInfo)
{
HdmiDisplayMode displayMode = dispInfo.GetCurrentDisplayMode();
if (displayMode)
{
NOTICE_LOG(RENDERER, "HDMI resolution: %d x %d", displayMode.ResolutionWidthInRawPixels(), displayMode.ResolutionHeightInRawPixels());
settings.display.width = displayMode.ResolutionWidthInRawPixels();
settings.display.height = displayMode.ResolutionHeightInRawPixels();
settings.display.uiScale = settings.display.width / 1920.0f * 1.4f;
}
}
}
if (!isXbox || (settings.display.width == 640 && settings.display.height == 480))
{
using namespace winrt::Windows::Graphics::Display;
using namespace winrt::Windows::UI::ViewManagement;
auto displayInfo = DisplayInformation::GetForCurrentView();
settings.display.dpi = displayInfo.LogicalDpi();
auto view = ApplicationView::GetForCurrentView();
auto bounds = view.VisibleBounds();
double scale = (double)displayInfo.RawPixelsPerViewPixel();
settings.display.width = (int)(bounds.Width * scale);
settings.display.height = (int)(bounds.Height * scale);
settings.display.uiScale = (float)scale;
}
#endif

Expand Down
4 changes: 3 additions & 1 deletion core/sdl/dreampicoport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@
#include <dirent.h>
#endif

#if defined(_WIN32) && !defined(TARGET_UWP)
#if defined(_WIN32)
#include <windows.h>
#ifndef TARGET_UWP
#include <setupapi.h>
#endif
#endif

//! Interface class for different DreamPicoPort communications interface
class DreamPicoPortComms
Expand Down
10 changes: 10 additions & 0 deletions core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,13 @@ constexpr unsigned operator""_sh4ms(unsigned long long t) {
constexpr unsigned operator""_sh4us(unsigned long long t) {
return SH4_MAIN_CLOCK / 1'000'000 * t;
}

#if defined(TARGET_MAC)
// Do not use std::format in Flycast code.
// Use #include <fmt/format.h> and fmt::format(...) instead.
// std::to_chars for floating point requires macOS 13.3+
// Disable std::format support to avoid link errors on older targets.
#ifdef __cpp_lib_format
#undef __cpp_lib_format
#endif
#endif
Loading
Loading