Skip to content

Commit

Permalink
vita3k: Add support for mingw-w64 (Vita3K#3406)
Browse files Browse the repository at this point in the history
* make the code compile on mingw-w64

* make compile with Clang 19

* make compile with GCC
  • Loading branch information
oltolm authored Oct 26, 2024
1 parent f8893fb commit c61895e
Show file tree
Hide file tree
Showing 30 changed files with 76 additions and 69 deletions.
1 change: 1 addition & 0 deletions external/CppCommon/include/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef CPPCOMMON_SYSTEM_CPU_H
#define CPPCOMMON_SYSTEM_CPU_H

#include <cstdint>
#include <string>

namespace CppCommon {
Expand Down
3 changes: 1 addition & 2 deletions external/CppCommon/source/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ extern char **environ;
#include <windows.h>
#include <winternl.h>
#define STATUS_SUCCESS 0x00000000
#include <map>
#include <vector>
#include <locale>
#endif

namespace CppCommon {
Expand Down
4 changes: 2 additions & 2 deletions tools/gen-modules/gen-modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include <yaml-cpp/yaml.h>

#ifdef WIN32
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#else
Expand Down Expand Up @@ -148,7 +148,7 @@ static void gen_module_stubs(const Modules &modules) {
for (const Module &module : modules) {
const std::string module_path = "vita3k/modules/" + module.first;

#ifdef WIN32
#ifdef _WIN32
CreateDirectoryA(module_path.c_str(), nullptr);
#else
const int mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
Expand Down
6 changes: 3 additions & 3 deletions vita3k/app/src/app_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#include <SDL_video.h>
#include <SDL_vulkan.h>

#ifdef WIN32
#ifdef _WIN32
#include <SDL_syswm.h>
#include <dwmapi.h>
#endif
Expand Down Expand Up @@ -298,7 +298,7 @@ bool init(EmuEnvState &state, Config &cfg, const Root &root_paths) {
state.display.fullscreen = true;
window_type |= SDL_WINDOW_FULLSCREEN_DESKTOP;
}
#if defined(WIN32) || defined(__linux__)
#if defined(_WIN32) || defined(__linux__)
const auto isSteamDeck = []() {
#ifdef __linux__
std::ifstream file("/etc/os-release");
Expand Down Expand Up @@ -329,7 +329,7 @@ bool init(EmuEnvState &state, Config &cfg, const Root &root_paths) {
return false;
}

#ifdef WIN32
#ifdef _WIN32
// Disable round corners for the game window
SDL_SysWMinfo wm_info;
SDL_VERSION(&wm_info.version);
Expand Down
1 change: 1 addition & 0 deletions vita3k/ctrl/include/ctrl/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <SDL_haptic.h>
#include <SDL_joystick.h>

#include <cstring>
#include <map>
#include <memory>
#include <mutex>
Expand Down
4 changes: 2 additions & 2 deletions vita3k/gui/src/app_context_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,11 @@ void draw_app_context_menu(GuiState &gui, EmuEnvState &emuenv, const std::string
"%23 Vita3K summary%0A- Version: {}%0A- Build number: {}%0A- Commit hash: https://github.com/vita3k/vita3k/commit/{}%0A- CPU backend: {}%0A- GPU backend: {}",
app_version, app_number, app_hash, get_cpu_backend(gui, emuenv, app_path), emuenv.cfg.backend_renderer);

#ifdef WIN32
#ifdef _WIN32
const auto user = std::getenv("USERNAME");
#else
auto user = std::getenv("USER");
#endif // WIN32
#endif // _WIN32

// Test environment summary
const auto test_env_summary = fmt::format(
Expand Down
2 changes: 1 addition & 1 deletion vita3k/gui/src/settings_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ void draw_settings_dialog(GuiState &gui, EmuEnvState &emuenv) {
SetTooltipEx(lang.emulator["select_position"].c_str());
}
ImGui::Spacing();
#ifndef WIN32
#ifndef _WIN32
ImGui::Checkbox(lang.emulator["case_insensitive"].c_str(), &emuenv.io.case_isens_find_enabled);
SetTooltipEx(lang.emulator["case_insensitive_description"].c_str());
#endif
Expand Down
6 changes: 3 additions & 3 deletions vita3k/gui/src/vita3k_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include <SDL.h>

#ifdef WIN32
#ifdef _WIN32
#include <combaseapi.h>
#endif

Expand Down Expand Up @@ -153,7 +153,7 @@ static void download_update(const fs::path &base_path) {
progress_state.pause = false;
std::thread download([base_path]() {
std::string download_continuous_link = "https://github.com/Vita3K/Vita3K/releases/download/continuous";
#ifdef WIN32
#ifdef _WIN32
download_continuous_link += "/windows-latest.zip";
#elif defined(__APPLE__)
download_continuous_link += "/macos-latest.dmg";
Expand Down Expand Up @@ -214,7 +214,7 @@ static void download_update(const fs::path &base_path) {
event.type = SDL_QUIT;
SDL_PushEvent(&event);

#ifdef WIN32
#ifdef _WIN32
const auto vita3K_batch = fmt::format("\"{}\\update-vita3k.bat\"", base_path);
FreeConsole();
#elif defined(__APPLE__)
Expand Down
4 changes: 2 additions & 2 deletions vita3k/io/include/io/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <util/fs.h>

#ifdef WIN32
#ifdef _WIN32
#include <util/string_utils.h>
#endif

Expand All @@ -31,7 +31,7 @@ typedef std::shared_ptr<FILE> FilePtr;

// For opening Boost.Filesystem files, Boost returns wide strings for Windows, normal strings for other OS
// Dirent and FILE only accept and return wide char strings for Windows, and normal for other OS
#ifdef WIN32
#ifdef _WIN32
const wchar_t *translate_open_mode(const int flags);

inline FilePtr create_shared_file(const fs::path &path, const int open_mode) {
Expand Down
2 changes: 1 addition & 1 deletion vita3k/io/src/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <io/filesystem.h>
#include <io/util.h>

#ifdef WIN32
#ifdef _WIN32
// To open wide files for Boost.Filesystem, we also need the appropriate wide mode flags for Windows, and normal flags for other OS
const wchar_t *translate_open_mode(const int flags) {
if (flags & SCE_O_WRONLY) {
Expand Down
6 changes: 3 additions & 3 deletions vita3k/io/src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <util/preprocessor.h>
#include <util/string_utils.h>

#ifdef WIN32
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#else
Expand Down Expand Up @@ -125,7 +125,7 @@ bool init(IOState &io, const fs::path &cache_path, const fs::path &log_path, con

io.redirect_stdio = redirect_stdio;

#ifndef WIN32
#ifndef _WIN32
io.case_isens_find_enabled = true;
#endif

Expand Down Expand Up @@ -552,7 +552,7 @@ int stat_file(IOState &io, const char *file, SceIoStat *statp, const fs::path &p
creation_time_ticks = (uint64_t)sb.st_ctime * VITA_CLOCKS_PER_SEC;
last_modification_time_ticks = (uint64_t)sb.st_mtime * VITA_CLOCKS_PER_SEC;

#ifndef WIN32
#ifndef _WIN32
#undef st_atime
#undef st_mtime
#undef st_ctime
Expand Down
10 changes: 5 additions & 5 deletions vita3k/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#include <app/discord.h>
#endif

#ifdef WIN32
#ifdef _WIN32
#include <combaseapi.h>
#include <process.h>
#endif
Expand Down Expand Up @@ -78,7 +78,7 @@ static void run_execv(char *argv[], EmuEnvState &emuenv) {
args[3] = nullptr;

// Execute the emulator again with some arguments
#ifdef WIN32
#ifdef _WIN32
FreeConsole();
_execv(argv[0], args);
#elif defined(__unix__) || defined(__APPLE__) && defined(__MACH__)
Expand All @@ -97,7 +97,7 @@ int main(int argc, char *argv[]) {

// Check admin privs before init starts to avoid creating of file as other user by accident
bool adminPriv = false;
#ifdef WIN32
#ifdef _WIN32
// https://stackoverflow.com/questions/8046097/how-to-check-if-a-process-has-the-administrative-rights
HANDLE hToken = NULL;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
Expand Down Expand Up @@ -163,7 +163,7 @@ int main(int argc, char *argv[]) {
return InitConfigFailed;
}

#ifdef WIN32
#ifdef _WIN32
{
auto res = CoInitializeEx(NULL, COINIT_MULTITHREADED);
LOG_ERROR_IF(res == S_FALSE, "Failed to initialize COM Library");
Expand Down Expand Up @@ -460,7 +460,7 @@ int main(int argc, char *argv[]) {
FrameMark; // Tracy - Frame end mark for game rendering loop
}

#ifdef WIN32
#ifdef _WIN32
CoUninitialize();
#endif

Expand Down
4 changes: 2 additions & 2 deletions vita3k/mem/include/mem/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

#include <memory>

#if WIN32
#ifdef _MSC_VER
#include <intrin.h>
#endif

#if WIN32
#ifdef _MSC_VER

inline bool atomic_compare_and_swap(volatile uint8_t *pointer, uint8_t value, uint8_t expected) {
const uint8_t result = _InterlockedCompareExchange8(reinterpret_cast<volatile char *>(pointer), value, expected);
Expand Down
22 changes: 11 additions & 11 deletions vita3k/mem/src/mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <mutex>
#include <utility>

#ifdef WIN32
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#else
Expand All @@ -48,7 +48,7 @@ static void register_access_violation_handler(const AccessViolationHandler &hand
static Address alloc_inner(MemState &state, uint32_t start_page, int page_count, const char *name, const bool force);
static void delete_memory(uint8_t *memory);

#ifdef WIN32
#ifdef _WIN32
static std::string get_error_msg() {
return std::system_category().message(GetLastError());
}
Expand All @@ -59,7 +59,7 @@ static std::string get_error_msg() {
#endif

bool init(MemState &state, const bool use_page_table) {
#ifdef WIN32
#ifdef _WIN32
SYSTEM_INFO system_info = {};
GetSystemInfo(&system_info);
state.page_size = system_info.dwPageSize;
Expand All @@ -73,7 +73,7 @@ bool init(MemState &state, const bool use_page_table) {

void *preferred_address = reinterpret_cast<void *>(1ULL << 34);

#ifdef WIN32
#ifdef _WIN32
state.memory = Memory(static_cast<uint8_t *>(VirtualAlloc(preferred_address, TOTAL_MEM_SIZE, MEM_RESERVE, PAGE_NOACCESS)), delete_memory);
if (!state.memory) {
// fallback
Expand Down Expand Up @@ -111,7 +111,7 @@ bool init(MemState &state, const bool use_page_table) {

const Address null_address = alloc_inner(state, 0, 1, "null", true);
assert(null_address == 0);
#ifdef WIN32
#ifdef _WIN32
DWORD old_protect = 0;
const BOOL ret = VirtualProtect(state.memory.get(), state.page_size, PAGE_NOACCESS, &old_protect);
LOG_CRITICAL_IF(!ret, "VirtualAlloc failed: {}", get_error_msg());
Expand All @@ -132,7 +132,7 @@ bool init(MemState &state, const bool use_page_table) {

static void delete_memory(uint8_t *memory) {
if (memory != nullptr) {
#ifdef WIN32
#ifdef _WIN32
const BOOL ret = VirtualFree(memory, 0, MEM_RELEASE);
assert(ret);
#else
Expand Down Expand Up @@ -170,7 +170,7 @@ static Address alloc_inner(MemState &state, uint32_t start_page, int page_count,
uint8_t *const memory = &state.memory[addr];

// Make memory chunk available to access
#ifdef WIN32
#ifdef _WIN32
const void *const ret = VirtualAlloc(memory, size, MEM_COMMIT, PAGE_READWRITE);
LOG_CRITICAL_IF(!ret, "VirtualAlloc failed: {}", get_error_msg());
#else
Expand Down Expand Up @@ -227,7 +227,7 @@ void unprotect_inner(MemState &state, Address addr, uint32_t size) {
}
uint8_t *addr_ptr = state.use_page_table ? state.page_table[addr / KiB(4)] : state.memory.get();

#ifdef WIN32
#ifdef _WIN32
DWORD old_protect = 0;
const BOOL ret = VirtualProtect(&addr_ptr[addr], size - 1, PAGE_READWRITE, &old_protect);
LOG_CRITICAL_IF(!ret, "VirtualAlloc failed: {}", get_error_msg());
Expand All @@ -240,7 +240,7 @@ void unprotect_inner(MemState &state, Address addr, uint32_t size) {
void protect_inner(MemState &state, Address addr, uint32_t size, const MemPerm perm) {
uint8_t *addr_ptr = state.use_page_table ? state.page_table[addr / KiB(4)] : state.memory.get();

#ifdef WIN32
#ifdef _WIN32
DWORD old_protect = 0;
const BOOL ret = VirtualProtect(&addr_ptr[addr], size - 1, (perm == MemPerm::None) ? PAGE_NOACCESS : ((perm == MemPerm::ReadOnly) ? PAGE_READONLY : PAGE_READWRITE), &old_protect);
LOG_CRITICAL_IF(!ret, "VirtualAlloc failed: {}", get_error_msg());
Expand Down Expand Up @@ -543,7 +543,7 @@ void free(MemState &state, Address address) {
assert(!state.use_page_table || state.page_table[address / KiB(4)] == state.memory.get());
uint8_t *const memory = &state.memory[page_num * state.page_size];

#ifdef WIN32
#ifdef _WIN32
const BOOL ret = VirtualFree(memory, page.size * state.page_size, MEM_DECOMMIT);
LOG_CRITICAL_IF(!ret, "VirtualFree failed: {}", get_error_msg());
#else
Expand All @@ -565,7 +565,7 @@ const char *mem_name(Address address, MemState &state) {
return "";
}

#ifdef WIN32
#ifdef _WIN32

static LONG WINAPI exception_handler(PEXCEPTION_POINTERS pExp) noexcept {
if (pExp->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT && IsDebuggerPresent()) {
Expand Down
2 changes: 1 addition & 1 deletion vita3k/modules/SceAppUtil/SceAppUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <util/safe_time.h>
#include <util/tracy.h>

#ifdef WIN32
#ifdef _WIN32
#include <winsock.h>
#else
#include <unistd.h>
Expand Down
6 changes: 3 additions & 3 deletions vita3k/modules/SceHttp/SceHttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <filesystem>
#include <http/state.h>

#ifdef WIN32 // windows moment
#ifdef _WIN32 // windows moment
#include <io.h>
#include <winsock2.h>
#include <ws2tcpip.h>
Expand Down Expand Up @@ -567,11 +567,11 @@ EXPORT(SceInt, sceHttpDeleteConnection, SceInt connId) {

if (connIt == emuenv.http.connections.end())
return RET_ERROR(SCE_HTTP_ERROR_INVALID_ID);
#ifdef WIN32
#ifdef _WIN32
closesocket(connIt->second.sockfd);
#else
close(connIt->second.sockfd);
#endif // WIN32
#endif // _WIN32

emuenv.http.connections.erase(connIt);

Expand Down
2 changes: 1 addition & 1 deletion vita3k/modules/SceLibKernel/SceLibKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ EXPORT(int, sceClibStrlcpyChk) {

EXPORT(int, sceClibStrncasecmp, const char *s1, const char *s2, SceSize len) {
TRACY_FUNC(sceClibStrncasecmp, s1, s2, len);
#ifdef WIN32
#ifdef _WIN32
return _strnicmp(s1, s2, len);
#else
return strncasecmp(s1, s2, len);
Expand Down
Loading

0 comments on commit c61895e

Please sign in to comment.