Skip to content

Commit

Permalink
Merge pull request #19855 from oltolm/cmake
Browse files Browse the repository at this point in the history
make compile with Clang/GCC on Windows
  • Loading branch information
hrydgard authored Jan 17, 2025
2 parents 0217038 + 9566065 commit 7a0ec74
Show file tree
Hide file tree
Showing 35 changed files with 172 additions and 365 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ debian/ppsspp/

# CMake stuff
CMakeFiles
CMakeUserPresets.json
compile_commands.json

# Clangd
.cache/
Expand Down
68 changes: 31 additions & 37 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ if(CMAKE_SYSTEM_PROCESSOR)
endif()

# the libraries in the ffmpeg/ directory are not compatible with mingw
if(MINGW AND NOT DEFINED USE_SYSTEM_FFMPEG)
if(MINGW AND NOT USE_SYSTEM_FFMPEG)
set(USE_SYSTEM_FFMPEG ON)
endif()

Expand Down Expand Up @@ -427,6 +427,12 @@ if(NOT MSVC)
endif()
endif()

include(CheckSymbolExists)
check_symbol_exists(_UCRT "windows.h" UCRT)
if (UCRT)
link_libraries("$<$<CONFIG:Debug>:ucrtbased>")
endif()

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -D_DEBUG")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} -Os -D_NDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -D_NDEBUG")
Expand All @@ -437,24 +443,24 @@ if(NOT MSVC)
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} -O2 -g -D_NDEBUG")

# Enable checking printf-like format strings (also works for logging functions)
add_definitions(-Wformat)
add_compile_options(-Wformat)

# Disable some warnings
add_definitions(-Wno-multichar)
add_compile_options(-Wno-multichar)

# Don't compile with strict aliasing, we're not 100% aliasing-safe
add_compile_options(-fno-strict-aliasing)
if(${CMAKE_C_COMPILER_ID} STREQUAL "Intel")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -parallel -fopenmp")
endif()

add_definitions(-fno-math-errno)
add_compile_options(-fno-math-errno)

if(X86 OR X86_64)
# enable sse2 code generation
add_definitions(-msse2)
add_compile_options(-msse2)
if(NOT X86_64 AND NOT CLANG)
add_definitions(-mfpmath=sse)
add_compile_options(-mfpmath=sse)
# add_definitions(-mstackrealign)
endif()
endif()
Expand All @@ -472,7 +478,7 @@ if(NOT MSVC)
elseif(NOT ANDROID)
# TODO: See if we can get rid of no-psabi
if(NOT ${CMAKE_C_COMPILER_ID} STREQUAL "Intel" AND NOT ${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
add_definitions(-Wno-psabi)
add_compile_options(-Wno-psabi)
endif()
add_definitions(-D_XOPEN_SOURCE=700)
add_definitions(-D_XOPEN_SOURCE_EXTENDED -D__BSD_VISIBLE=1 -D_BSD_SOURCE -D_DEFAULT_SOURCE)
Expand All @@ -483,7 +489,7 @@ if(NOT MSVC)
add_definitions(-D_NETBSD_SOURCE)
endif()
elseif(ANDROID)
add_definitions(-fsigned-char)
add_compile_options(-fsigned-char)
endif()
else()
# Disable warnings about MS-specific _s variants of libc functions
Expand All @@ -502,8 +508,8 @@ if(WIN32)
endif()

if(NOT ANDROID)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
endif()

# This sets up the MSVC project dirs according to the physical project dirs
Expand All @@ -523,12 +529,6 @@ endmacro()

add_subdirectory(ext)

if(WIN32)
include_directories(dx9sdk/Include)
include_directories(dx9sdk/Include/DX11)
endif()


set(CommonJIT
Core/MIPS/JitCommon/JitCommon.cpp
Core/MIPS/JitCommon/JitCommon.h
Expand Down Expand Up @@ -1193,6 +1193,7 @@ else()
ext/libzip/zip_err_str.c
)
if(WIN32)
target_compile_options(libzip PRIVATE $<$<C_COMPILER_ID:Clang>:-Wno-incompatible-function-pointer-types> $<$<C_COMPILER_ID:GNU>:-Wno-incompatible-pointer-types>)
target_sources(libzip PRIVATE
ext/libzip/zip_source_file_win32.c
ext/libzip/zip_source_file_win32_named.c
Expand Down Expand Up @@ -1421,11 +1422,6 @@ elseif(USING_QT_UI)
elseif(WIN32)
# Don't care about SDL.
set(TargetBin PPSSPPWindows)
if(X86_64)
link_directories(dx9sdk/Lib/x64)
else()
link_directories(dx9sdk/Lib/x86)
endif()
elseif(LIBRETRO)
else()
if(GOLD)
Expand Down Expand Up @@ -1503,14 +1499,7 @@ else()
endif()

if(WIN32)
if(MINGW)
set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> -O coff <INCLUDES> <SOURCE> <OBJECT>")
# required when using the dx9sdk include paths
add_definitions(-include ${CMAKE_CURRENT_SOURCE_DIR}/Windows/mingw_defines.h)
else()
target_link_libraries(Common dxguid)
endif()
target_link_libraries(Common winmm d3d9 dsound)
target_link_libraries(Common winmm d3d9 dsound dxguid Version)
endif()

if(NOT LIBRETRO)
Expand Down Expand Up @@ -2480,14 +2469,11 @@ target_link_libraries(${CoreLibName} Common native chdr kirk cityhash sfmt19937

# Winsock
if(WIN32)
target_link_libraries(${CoreLibName} ws2_32 winhttp)
target_link_libraries(${CoreLibName} ws2_32)
endif()

if(NOT HTTPS_NOT_AVAILABLE)
target_link_libraries(${CoreLibName} naett)
if(WIN32)
target_link_libraries(${CoreLibName} winhttp)
endif()
endif()

target_compile_features(${CoreLibName} PUBLIC cxx_std_17)
Expand Down Expand Up @@ -2591,6 +2577,9 @@ if(USE_MINIUPNPC)
# Suppress noise warnings
target_compile_definitions(miniupnpc PRIVATE _CRT_SECURE_NO_WARNINGS _WINSOCK_DEPRECATED_NO_WARNINGS)
endif()
if (WIN32)
target_link_libraries(miniupnpc ws2_32)
endif()
endif()
endif()
endif()
Expand Down Expand Up @@ -2813,6 +2802,12 @@ if(UNITTEST)
Core/MIPS/ARM/ArmRegCache.cpp
Core/MIPS/ARM/ArmRegCacheFPU.cpp
)
if(WIN32)
target_sources(PPSSPPUnitTest PRIVATE
Windows/CaptureDevice.cpp
Windows/CaptureDevice.h
)
endif()
target_link_libraries(PPSSPPUnitTest ${COCOA_LIBRARY} ${QUARTZ_CORE_LIBRARY} ${IOKIT_LIBRARY} ${LinkCommon} Common)
setup_target_project(PPSSPPUnitTest unittest)
add_test(arm64_emitter PPSSPPUnitTest Arm64Emitter)
Expand Down Expand Up @@ -2891,10 +2886,9 @@ if(TargetBin)
endif()
elseif(WIN32)
add_executable(${TargetBin} WIN32 ${NativeAppSource})
if(MSVC)
set_target_properties(${TargetBin} PROPERTIES LINK_FLAGS "/SUBSYSTEM:WINDOWS")
else()
set_target_properties(${TargetBin} PROPERTIES LINK_FLAGS "-Wl,-subsystem,windows")
if(NOT MSVC)
target_compile_options(${TargetBin} PRIVATE $<$<CXX_COMPILER_ID:Clang>:-Wno-c++11-narrowing>)
target_link_libraries(${TargetBin} Version Dwmapi)
endif()
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ${TargetBin})
else()
Expand Down
23 changes: 23 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": 2,
"configurePresets": [
{
"name": "gcc-debug",
"binaryDir": "build-gcc-debug",
"generator": "Ninja",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
}
},
{
"name": "clang-debug",
"binaryDir": "build-clang-debug",
"generator": "Ninja",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
}
}
]
}
9 changes: 0 additions & 9 deletions Common/CPUDetect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,7 @@ void do_cpuid(u32 regs[4], u32 cpuid_leaf) {
__cpuid((int *)regs, cpuid_leaf);
}

#ifdef __MINGW32__
static uint64_t do_xgetbv(unsigned int index) {
unsigned int eax, edx;
// This is xgetbv directly, so we can avoid compilers warning we need runtime checks.
asm(".byte 0x0f, 0x01, 0xd0" : "=a"(eax), "=d"(edx) : "c"(index));
return ((uint64_t)edx << 32) | eax;
}
#else
#define do_xgetbv _xgetbv
#endif

#else // _WIN32

Expand Down
2 changes: 2 additions & 0 deletions Common/CommonFuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ inline u64 __rotr64(u64 x, unsigned int shift){
#define strncasecmp _strnicmp
#endif

#ifndef __MINGW32__
#define unlink _unlink
#define __rotl _rotl
#define __rotl64 _rotl64
#define __rotr _rotr
#define __rotr64 _rotr64
#endif

// 64 bit offsets for windows
#ifndef __MINGW32__
Expand Down
7 changes: 7 additions & 0 deletions Common/File/DirListing.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#include "ppsspp_config.h"

#ifdef __MINGW32__
#include <unistd.h>
#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
#define _POSIX_THREAD_SAFE_FUNCTIONS 200112L
#endif
#endif

#if PPSSPP_PLATFORM(WINDOWS)
#define WIN32_LEAN_AND_MEAN
#include "Common/CommonWindows.h"
Expand Down
7 changes: 0 additions & 7 deletions Common/File/FileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@

#include "android/jni/app-android.h"

#ifdef __MINGW32__
#include <unistd.h>
#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
#define _POSIX_THREAD_SAFE_FUNCTIONS 200112L
#endif
#endif

#include <cstring>
#include <ctime>
#include <memory>
Expand Down
5 changes: 0 additions & 5 deletions Common/GPU/D3D11/thin3d_d3d11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
#include <d3d11_1.h>
#include <D3Dcompiler.h>

#ifdef __MINGW32__
#undef __uuidof
#define __uuidof(type) IID_##type
#endif

namespace Draw {

static constexpr int MAX_BOUND_TEXTURES = 8;
Expand Down
1 change: 1 addition & 0 deletions Common/Log/LogManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "Common/Log/LogManager.h"

#if PPSSPP_PLATFORM(WINDOWS)
#include <io.h>
#include "Common/Log/ConsoleListener.h"
#endif

Expand Down
1 change: 1 addition & 0 deletions Common/OSVersion.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <cstdint>
#include <string>

#ifdef _WIN32
Expand Down
12 changes: 0 additions & 12 deletions Common/Thread/ThreadUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

#include "Common/CommonWindows.h"

#ifdef __MINGW32__
#include <excpt.h>
#endif

#define TLS_SUPPORTED

#elif defined(__ANDROID__)
Expand Down Expand Up @@ -197,19 +193,11 @@ void SetCurrentThreadNameThroughException(const char *threadName) {
info.dwThreadID = -1; //dwThreadID;
info.dwFlags = 0;

#ifdef __MINGW32__
__try1 (ehandler)
#else
__try
#endif
{
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info);
}
#ifdef __MINGW32__
__except1
#else
__except(EXCEPTION_CONTINUE_EXECUTION)
#endif
{}
#endif
}
Expand Down
1 change: 1 addition & 0 deletions Core/Debugger/DebugInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include "Common/CommonTypes.h"
#include <cstddef>

struct MemMap;

Expand Down
3 changes: 3 additions & 0 deletions Core/HLE/NetInetConstants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "Core/HLE/NetInetConstants.h"
#include "Core/HLE/sceKernel.h"
#include "Core/HLE/HLE.h"
#ifdef __MINGW32__
#include <mswsock.h>
#endif

int convertMsgFlagPSP2Host(int flag) {
switch (flag) {
Expand Down
5 changes: 4 additions & 1 deletion Core/HLE/sceMpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,10 @@ static bool decodePmpVideo(PSPPointer<SceMpegRingBuffer> ringbuffer, u32 pmpctxA
sws_freeContext(img_convert_ctx);

// update timestamp
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(55, 58, 100)
#if LIBAVUTIL_VERSION_MAJOR >= 59
int64_t bestPts = mediaengine->m_pFrame->best_effort_timestamp;
int64_t ptsDuration = mediaengine->m_pFrame->duration;
#elif LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(55, 58, 100)
int64_t bestPts = mediaengine->m_pFrame->best_effort_timestamp;
int64_t ptsDuration = mediaengine->m_pFrame->pkt_duration;
#else
Expand Down
5 changes: 4 additions & 1 deletion Core/HLE/sceNetInet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#include "Core/MemMapHelpers.h"
#include "Core/Util/PortManager.h"
#include "Core/Instance.h"
#ifdef __MINGW32__
#include <mswsock.h>
#endif

// This is the PSP networking errno.
// TODO: It should probably be thread-local - one value for each PSP thread?
Expand Down Expand Up @@ -1004,7 +1007,7 @@ static int sceNetInetSendmsg(int socket, u32 msghdrPtr, int flags) {
// Note: Many existing implementations of CMSG_FIRSTHDR never look at msg_controllen and just return the value of cmsg_control.
if (pspMsghdr->msg_controllen >= sizeof(InetCmsghdr)) {
// TODO: Creates our own CMSG_* macros (32-bit version of it, similar to the one on PSP) to avoid alignment/size issue that can lead to memory corruption/out of bound issue.
for (WSACMSGHDR* cmsgptr = CMSG_FIRSTHDR(&hdr); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&hdr, cmsgptr)) {
for (WSACMSGHDR* cmsgptr = WSA_CMSG_FIRSTHDR(&hdr); cmsgptr != NULL; cmsgptr = WSA_CMSG_NXTHDR(&hdr, cmsgptr)) {
cmsgptr->cmsg_type = convertCMsgTypePSP2Host(cmsgptr->cmsg_type, cmsgptr->cmsg_level);
cmsgptr->cmsg_level = convertSockoptLevelPSP2Host(cmsgptr->cmsg_level);
}
Expand Down
Loading

0 comments on commit 7a0ec74

Please sign in to comment.