Skip to content

Commit 09871d2

Browse files
committed
Improve platform compatibility
Replace a bunch of non-standard defines with standard ones Don't #define POSIX in public header or test for it. Fixes #228. (We shoulkd probably also make this change to Steamworks headers.) Use IsLinux(), IsPosix(), etc instead of #ifdef LINUX or #ifdef POSIX Moved some platform socket stuff out of steamnetworkingsockets_platform.h and into platform_sockets.h. (none of this is actually particular to steamnetworkingsockets.) Some platforms don't have IPv6 support.
1 parent 78481e0 commit 09871d2

24 files changed

+335
-216
lines changed

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,11 @@ function(set_target_common_gns_properties TGT)
158158
endif()
159159

160160
if(CMAKE_SYSTEM_NAME MATCHES Linux)
161-
target_compile_definitions(${TGT} PUBLIC POSIX LINUX)
161+
target_compile_definitions(${TGT} PUBLIC LINUX)
162162
elseif(CMAKE_SYSTEM_NAME MATCHES Darwin)
163-
target_compile_definitions(${TGT} PUBLIC POSIX OSX)
163+
target_compile_definitions(${TGT} PUBLIC OSX)
164164
elseif(CMAKE_SYSTEM_NAME MATCHES FreeBSD)
165-
target_compile_definitions(${TGT} PUBLIC POSIX FREEBSD)
165+
target_compile_definitions(${TGT} PUBLIC FREEBSD)
166166
elseif(CMAKE_SYSTEM_NAME MATCHES Windows)
167167
target_compile_definitions(${TGT} PUBLIC _WINDOWS)
168168
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")

examples/example_chat.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <steam/steam_api.h>
2323
#endif
2424

25-
#ifdef WIN32
25+
#ifdef _WIN32
2626
#include <windows.h> // Ug, for NukeProcess -- see below
2727
#else
2828
#include <unistd.h>
@@ -43,7 +43,7 @@ SteamNetworkingMicroseconds g_logTimeZero;
4343
// down the thread that is reading from stdin.
4444
static void NukeProcess( int rc )
4545
{
46-
#ifdef WIN32
46+
#ifdef _WIN32
4747
ExitProcess( rc );
4848
#else
4949
(void)rc; // Unused formal parameter

examples/trivial_signaling_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <steam/isteamnetworkingutils.h>
1515
#include <steam/steamnetworkingcustomsignaling.h>
1616

17-
#ifdef POSIX
17+
#ifdef VALVE_POSIX
1818
#include <unistd.h>
1919
#include <sys/socket.h>
2020
#include <sys/types.h>

include/steam/steamtypes.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
typedef unsigned char uint8;
1818
#endif
1919

20-
#if defined( __GNUC__ ) && !defined(_WIN32) && !defined(POSIX)
20+
#if ( defined(POSIX) || defined(_POSIX_VERSION) || ( defined( __GNUC__ ) && !defined(_WIN32) ) ) && !defined(VALVE_POSIX)
21+
#define VALVE_POSIX 1
22+
#endif
23+
#ifdef __GNUC__
2124
#if __GNUC__ < 4
2225
#error "Steamworks requires GCC 4.X (4.2 or 4.4 have been tested)"
2326
#endif
24-
#define POSIX 1
2527
#endif
2628

2729
#if defined(__LP64__) || defined(__x86_64__) || defined(_WIN64) || defined(__aarch64__) || defined(__s390x__)

src/common/crypto_openssl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#define _X86INTRIN_H_INCLUDED
1111
#endif
1212
#include "winlite.h"
13-
#elif defined(POSIX)
13+
#elif IsPosix()
1414
#include <sys/types.h>
1515
#include <sys/socket.h>
1616
#include <unistd.h>
@@ -397,7 +397,7 @@ void CCrypto::GenerateRandomBlock( void *pvDest, int cubDest )
397397
bool bRtlGenRandomOK = s_pfnRtlGenRandom && ( s_pfnRtlGenRandom( pubDest, (unsigned long)cubDest ) == TRUE );
398398
AssertFatal( bRtlGenRandomOK );
399399

400-
#elif defined(POSIX)
400+
#elif IsPosix()
401401

402402
// Reading from /dev/urandom is threadsafe, but possibly slow due to a kernel
403403
// spinlock or mutex protecting access to the internal PRNG state. In theory,

src/public/minbase/minbase_decls.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@
8585

8686
// Linux had a few areas where it didn't construct objects in the same order that Windows does.
8787
// So when CVProfile::CVProfile() would access g_pMemAlloc, it would crash because the allocator wasn't initalized yet.
88-
#if defined( GNUC ) || defined ( COMPILER_GCC ) || defined( COMPILER_SNC )
88+
#if defined( __GNUC__ ) || defined ( COMPILER_GCC ) || defined( COMPILER_SNC )
8989
#define CONSTRUCT_EARLY __attribute__((init_priority(101)))
9090
#else
9191
#define CONSTRUCT_EARLY
9292
#endif
9393

9494
#ifdef _WIN32
9595
#define SELECTANY __declspec(selectany)
96-
#elif defined(GNUC) || defined ( COMPILER_GCC ) || defined( COMPILER_SNC )
96+
#elif defined(__GNUC__) || defined ( COMPILER_GCC ) || defined( COMPILER_SNC )
9797
#define SELECTANY __attribute__((weak))
9898
#else
9999
#define SELECTANY static
@@ -105,7 +105,7 @@
105105
#if defined(_WIN32) && !defined(_XBOX)
106106
#define PLAT_DECL_EXPORT __declspec( dllexport )
107107
#define PLAT_DECL_IMPORT __declspec( dllimport )
108-
#elif defined(GNUC) || defined(COMPILER_GCC)
108+
#elif defined(__GNUC__) || defined(COMPILER_GCC)
109109
#define PLAT_DECL_EXPORT __attribute__((visibility("default")))
110110
#define PLAT_DECL_IMPORT
111111
#elif defined(_XBOX) || defined(COMPILER_SNC)
@@ -156,7 +156,7 @@
156156
#define __stdcall __attribute__ ((__stdcall__))
157157
#endif
158158
#define FASTCALL
159-
#elif defined(POSIX)
159+
#elif IsPosix()
160160
#define __stdcall
161161
#define __cdecl
162162
#define STDCALL
@@ -167,7 +167,7 @@
167167
#define NOINLINE __declspec(noinline)
168168
#define NORETURN __declspec(noreturn)
169169
#define FORCEINLINE __forceinline
170-
#elif defined(GNUC) || defined(COMPILER_GCC) || defined(COMPILER_SNC)
170+
#elif defined(__GNUC__) || defined(COMPILER_GCC) || defined(COMPILER_SNC)
171171
#define NOINLINE __attribute__ ((noinline))
172172
#define NORETURN __attribute__ ((noreturn))
173173
#if defined(COMPILER_GCC) || defined(COMPILER_SNC)

src/public/minbase/minbase_identify.h

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@
4848
#endif
4949
#endif
5050

51-
#if ( defined(LINUX) || defined(OSX) || defined(ANDROID) ) && !defined(POSIX)
52-
#define POSIX
53-
#endif
54-
5551
#if defined(_WIN32) && !defined(WINDED)
5652
#if defined(_M_IX86)
5753
#define __i386__ 1
@@ -110,6 +106,10 @@
110106
#define IsRetail() false
111107
#endif
112108

109+
#ifdef IsPosix
110+
#error "Too soon"
111+
#endif
112+
113113
#ifdef _DEBUG
114114
#define IsRelease() false
115115
#define IsDebug() true
@@ -122,29 +122,41 @@
122122
#define IsXboxOne() true
123123
#define IsConsole() true
124124
#elif defined( NN_NINTENDO_SDK )
125-
#if !defined(POSIX) && !defined(_WIN32)
126-
#define POSIX
125+
#ifndef _WIN32
126+
#define IsPosix() true
127127
#endif
128128
#define IsNintendoSwitch() true
129129
#define IsConsole() true
130-
#elif defined( _PS5 )
131-
#ifndef POSIX
132-
#define POSIX
133-
#endif
130+
#elif defined( __PROSPERO__ )
131+
#define IsPosix() true
134132
#define IsPS5() true
135133
#define IsConsole() true
134+
#elif defined( __ORBIS__ )
135+
#define IsPosix() true
136+
#define IsPS4() true
137+
#define IsConsole() true
136138
#elif defined( _WIN32 )
137139
#define IsWindows() true
138140
#define IsPC() true
139-
#elif defined(POSIX)
140-
#define IsPC() true
141-
#ifdef LINUX
142-
#define IsLinux() true
143-
#endif
144-
#ifdef OSX
141+
#elif defined( __ANDROID__ ) || defined( ANDROID )
142+
#define IsAndroid() true
143+
#define IsPosix() true
144+
#elif defined(__APPLE__)
145+
#include <TargetConditionals.h>
146+
#if defined( TARGET_OS_MAC )
145147
#define SUPPORTS_IOPOLLINGHELPER
146148
#define IsOSX() true
149+
#define IsPosix() true
150+
//#elif defined( TARGET_OS_IPHONE )
151+
#else
152+
#error "Unsupported platform"
147153
#endif
154+
#elif defined( LINUX ) || defined( __LINUX__ ) || defined(linux) || defined(__linux) || defined(__linux__)
155+
#define IsLinux() true
156+
#define IsPosix() true
157+
#elif defined( _POSIX_VERSION ) || defined( POSIX ) || defined( VALVE_POSIX )
158+
#define IsPosix() true
159+
#define IsPC() true
148160
#else
149161
#error Undefined platform
150162
#endif
@@ -155,6 +167,9 @@
155167
#ifndef IsPC
156168
#define IsPC() false
157169
#endif
170+
#ifndef IsAndroid
171+
#define IsAndroid() false
172+
#endif
158173
#ifndef IsConsole
159174
#define IsConsole() false
160175
#endif
@@ -164,15 +179,17 @@
164179
#ifndef IsXboxOne
165180
#define IsXboxOne() false
166181
#endif
182+
#ifndef IsPS4
183+
#define IsPS4() false
184+
#endif
167185
#ifndef IsPS5
168186
#define IsPS5() false
169187
#endif
188+
#define IsPlaystation() ( IsPS4() || IsPS5() )
170189
#ifndef IsLinux
171190
#define IsLinux() false
172191
#endif
173-
#ifdef POSIX
174-
#define IsPosix() true
175-
#else
192+
#ifndef IsPosix
176193
#define IsPosix() false
177194
#endif
178195
#ifndef IsOSX
@@ -185,13 +202,6 @@
185202
#define IsARM() false
186203
#endif
187204
#endif
188-
#ifndef IsAndroid
189-
#ifdef ANDROID
190-
#define IsAndroid() true
191-
#else
192-
#define IsAndroid() false
193-
#endif
194-
#endif
195205

196206
// Detect if RTTI is enabled in the current compile
197207
#if defined(__clang__)

src/public/minbase/minbase_macros.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,26 @@
4747

4848
#if defined(_WIN32)
4949
#define PLAT_DLL_EXT "dll"
50-
#elif defined(LINUX)
50+
#elif IsLinux()
5151
#define PLAT_DLL_EXT "so"
52-
#elif defined(OSX)
52+
#elif IsOSX()
5353
#define PLAT_DLL_EXT "dylib"
5454
#endif
5555

5656
#if defined(_WIN32)
5757
#define PLAT_PATH_SLASH "\\"
58-
#elif defined(POSIX)
58+
#elif IsPosix()
5959
#define PLAT_PATH_SLASH "/"
60+
#else
61+
#error "?"
6062
#endif
6163

6264
#if defined( _PS3 )
6365
#include <sysutil/sysutil_gamecontent.h>
6466
#define PATH_MAX CELL_GAME_PATH_MAX
6567
#endif
6668

67-
#if (!defined(_WIN32) || defined(WINDED)) && defined(POSIX)
69+
#if (!defined(_WIN32) || defined(WINDED)) && IsPosix()
6870
#define MAX_PATH PATH_MAX
6971
#define _MAX_PATH PATH_MAX
7072
#endif
@@ -149,7 +151,7 @@ FORCEINLINE unsigned __int64 PLAT_CPU_TIME()
149151
}
150152

151153

152-
#elif defined(POSIX)
154+
#elif IsPosix()
153155

154156
#include <sys/time.h>
155157

@@ -252,7 +254,7 @@ char (*RtlpNumberOf( UNALIGNED T (&)[N] ))[N];
252254
#endif
253255
#endif
254256

255-
#elif defined(LINUX)
257+
#elif IsLinux()
256258

257259
// On Linux/gcc, RtlpNumberOf doesn't work well with pointers to structs
258260
// that are defined function locally. Here we have an alternative implementation
@@ -318,7 +320,7 @@ struct ErrorIfIsPointerNotArrayGivenToARRAYSIZE<false>
318320
#endif
319321

320322
// Don't define for Windows, since it is defined unconditionally in winnt.h.
321-
#if !defined( UNREFERENCED_PARAMETER ) && !defined( WIN32 )
323+
#if !defined( UNREFERENCED_PARAMETER ) && !defined( _WIN32 )
322324
#define UNREFERENCED_PARAMETER(parm) NOTE_UNUSED(parm)
323325
#endif
324326

src/public/minbase/minbase_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ typedef uint32 uint32_t;
7575
// NOTE: int64_t must match the compiler stdint.h definition
7676
// and so may not match the Steam int64. Mixing the two is
7777
// error-prone so always use the Steam non-_t types in Steam code.
78-
#if defined(COMPILER_GCC) && defined(PLATFORM_64BITS) && !defined(__MINGW32__) && !defined(OSX) && !(defined(IOS) || defined(TVOS))
78+
#if defined(COMPILER_GCC) && defined(PLATFORM_64BITS) && !defined(__MINGW32__) && !IsOSX() && !(defined(IOS) || defined(TVOS))
7979
#define INT64_DIFFERENT_FROM_INT64_T 1
8080
typedef long int int64_t;
8181
typedef unsigned long int uint64_t;

src/public/minbase/minbase_warnings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#pragma GCC diagnostic ignored "-Wtype-limits"
3131
#endif
3232

33-
#if IsPS5() /* This is the wrong way to detect this. HALP */
33+
#if defined( __PROSPERO__ ) || defined( __ORBIS__ ) /* This is the wrong way to detect this. HALP */
3434
#pragma GCC diagnostic ignored "-Wunused-function"
3535
#pragma GCC diagnostic ignored "-Wreorder-ctor"
3636
#endif

0 commit comments

Comments
 (0)