Skip to content

Commit 13c86d9

Browse files
committed
toolchain: Abstract compiler capabilities
Abstract compiler capabilities to help simplify support for various toolchains. Signed-off-by: Peter Mitsis <peter.mitsis@gmail.com>
1 parent d59c4d4 commit 13c86d9

File tree

7 files changed

+338
-144
lines changed

7 files changed

+338
-144
lines changed

code/globalincs/pstypes.h

Lines changed: 1 addition & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <stdlib.h>
1818
#include <memory.h>
1919
#include <string.h>
20+
#include "globalincs/toolchain.h"
2021

2122
#if defined( __x86_64__ ) || defined( _WIN64 )
2223
#define IAM_64BIT 1
@@ -63,39 +64,6 @@ typedef unsigned short ushort;
6364
typedef unsigned int uint;
6465
typedef unsigned long ulong;
6566

66-
// Format specifier macros
67-
#ifdef DOXYGEN
68-
// Documentation macros, only used by DOXYGEN
69-
/**
70-
* @brief Identifies a printf-style format string
71-
*/
72-
#define SCP_FORMAT_STRING
73-
74-
/**
75-
* @brief Specifies which arguments are involved in printf-style string formatting
76-
*
77-
* @details Expands to a compiler specific attribute which specify where the
78-
* format arguments are located. Parameters are 1-based which also includes
79-
* the 'this' parameter at position 1 for class methods.
80-
*
81-
* @param formatArg The location of the format string argument in the argument list
82-
* @param varArgs The location where the variable arguments begin
83-
*/
84-
#define SCP_FORMAT_STRING_ARGS(formatArg, varArgs)
85-
#elif defined(_MSC_VER)
86-
#include <sal.h>
87-
88-
#define SCP_FORMAT_STRING _Printf_format_string_
89-
#define SCP_FORMAT_STRING_ARGS(x, y)
90-
#elif defined(__GNUC__) || defined(__clang__)
91-
// GCC and clang use function attributes
92-
#define SCP_FORMAT_STRING
93-
#define SCP_FORMAT_STRING_ARGS(x, y) __attribute__ ((format (printf, x, y)))
94-
#else
95-
#define SCP_FORMAT_STRING
96-
#define SCP_FORMAT_STRING_ARGS(x, y)
97-
#endif
98-
9967
#define HARDWARE_ONLY
10068

10169
//Stucture to store clipping codes in a word
@@ -264,11 +232,6 @@ extern int Global_error_count;
264232

265233
#include "osapi/outwnd.h"
266234

267-
// remove __attribute__ on non-GCC compilers
268-
#ifndef __GNUC__
269-
# define __attribute__(x) /*NOTHING*/
270-
#endif
271-
272235
// To debug printf do this:
273236
// mprintf(( "Error opening %s\n", filename ));
274237
#ifndef NDEBUG
@@ -290,22 +253,10 @@ extern int Global_error_count;
290253
// Please never uncomment the functionality of Assert in debug
291254
// The code, as with all development like this is littered with Asserts which are designed to throw
292255
// up an error message if variables are out of range.
293-
294-
#define ASSUME(x)
295-
296256
// Disabling this functionality is dangerous, crazy values can run rampent unchecked and the longer its disabled
297257
// the more likely you are to have problems getting it working again.
298258
#if defined(NDEBUG)
299259
# define Assert(expr) do { ASSUME(expr); } while (0)
300-
# ifndef _MSC_VER // non MS compilers
301-
# define Assertion(expr, msg, ...) do {} while (0)
302-
# else
303-
# if _MSC_VER >= 1400 // VC 2005 or greater
304-
# define Assertion(expr, msg, ...) do { ASSUME(expr); } while (0)
305-
# else
306-
# define Assertion(expr, msg) do {} while (0)
307-
# endif
308-
# endif
309260
#else
310261
void gr_activate(int);
311262
# define Assert(expr) do {\
@@ -314,30 +265,6 @@ extern int Global_error_count;
314265
}\
315266
ASSUME( expr );\
316267
} while (0)
317-
318-
// Assertion can only use its proper fuctionality in compilers that support variadic macro
319-
# ifndef _MSC_VER // non MS compilers
320-
# define Assertion(expr, msg, ...) do {\
321-
if (!(expr)) {\
322-
WinAssert(#expr,__FILE__,__LINE__, msg , ##__VA_ARGS__ );\
323-
}\
324-
} while (0)
325-
# else
326-
# if _MSC_VER >= 1400 // VC 2005 or greater
327-
# define Assertion(expr, msg, ...) do {\
328-
if (!(expr)) {\
329-
WinAssert(#expr,__FILE__,__LINE__, msg, __VA_ARGS__ );\
330-
}\
331-
ASSUME(expr);\
332-
} while (0)
333-
# else // older MSVC compilers
334-
# define Assertion(expr, msg) do {\
335-
if (!(expr)) {\
336-
WinAssert(#expr,__FILE__,__LINE__);\
337-
}\
338-
} while (0)
339-
# endif
340-
# endif
341268
#endif
342269
/*******************NEVER COMMENT Assert ************************************************/
343270

@@ -693,79 +620,9 @@ class camid
693620
bool isValid();
694621
};
695622

696-
/* Restrict keyword semantics are different under VC and GCC */
697-
698-
#ifndef NO_RESTRICT_USE
699-
# ifdef _MSC_VER
700-
# if _MSC_VER >= 1400
701-
# define RESTRICT __restrict
702-
# else
703-
# define RESTRICT
704-
# endif
705-
# else
706-
# define RESTRICT restrict
707-
# endif
708-
#else
709-
# define RESTRICT
710-
#endif
711-
712623
#include "globalincs/vmallocator.h"
713624
#include "globalincs/safe_strings.h"
714625

715-
// Macros for portable printf argument specification
716-
#ifdef DOXYGEN
717-
// Special section for doxygen
718-
/**
719-
* @brief Format specifier for a @c size_t argument
720-
* Due to different runtimes using different format specifier for these types
721-
* it's necessary to hide these changes behind a macro. Use this in place of %zu
722-
*/
723-
#define SIZE_T_ARG
724-
/**
725-
* @brief Format specifier for a @c ptrdiff_t argument
726-
* Due to different runtimes using different format specifier for these types
727-
* it's necessary to hide these changes behind a macro. Use this in place of %zd
728-
*/
729-
#define PTRDIFF_T_ARG
730-
#elif defined(_MSC_VER)
731-
#define SIZE_T_ARG "%Iu"
732-
#define PTRDIFF_T_ARG "%Id"
733-
#else
734-
// Asume C99 compatibility for everyone else
735-
#define SIZE_T_ARG "%zu"
736-
#define PTRDIFF_T_ARG "%zd"
737-
#endif
738-
739-
// c++11 standard detection
740-
// for GCC with autotools, see AX_CXX_COMPILE_STDCXX_11 macro in configure.ac
741-
// this sets HAVE_CXX11 & -std=c++0x or -std=c++11 appropriately
742-
743-
#ifndef HAVE_CXX11
744-
// Use the visual studio version to detect C++11 support
745-
#if _MSC_VER >= 1600
746-
# define HAVE_CXX11
747-
#endif
748-
// clang doesn't seem to have a feature check for is_trivial
749-
// oh well, assume it'll be covered by one of the other two checks...
750-
// http://clang.llvm.org/docs/LanguageExtensions.html#feature_check
751-
#if defined(__clang__)
752-
#if __has_feature(cxx_static_assert)
753-
#if __has_feature(cxx_auto_type)
754-
#define HAVE_CXX11
755-
#endif // __has_feature(cxx_auto_type)
756-
#endif // __has_feature(cxx_static_assert)
757-
#endif // defined(__clang__)
758-
// TODO: sort out cmake/gcc
759-
#endif // HAVE_CXX11
760-
761-
// In compilers before VS 2015, the noexcept keyword isn't defined. Nonetheless, we need to define it in VS 2015 and non-MS compilers.
762-
#if defined(_MSC_VER) && _MSC_VER < 1900
763-
# define NOEXCEPT
764-
#else
765-
# define NOEXCEPT noexcept
766-
#endif
767-
768-
769626
// DEBUG compile time catch for dangerous uses of memset/memcpy/memmove
770627
// would prefer std::is_trivially_copyable but it's not supported by gcc yet
771628
// ref: http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html

code/globalincs/toolchain.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (C) Volition, Inc. 1999. All rights reserved.
3+
*
4+
* All source code herein is the property of Volition, Inc. You may not sell
5+
* or otherwise commercially exploit the source or things you created based on
6+
* the source.
7+
*/
8+
9+
/**
10+
* @file
11+
*
12+
* @brief Macros to abstract compiler capabilities for various toolchains
13+
*/
14+
15+
#ifndef _TOOLCHAIN_H
16+
#define _TOOLCHAIN_H
17+
18+
#if defined(DOXYGEN)
19+
# include "globalincs/toolchain/doxygen.h"
20+
#elif defined(__MINGW32__)
21+
# include "globalincs/toolchain/mingw.h"
22+
#elif defined(__clang__)
23+
# include "globalincs/toolchain/clang.h"
24+
#elif defined(__GNUC__)
25+
# include "globalincs/toolchain/gcc.h"
26+
#elif defined(_MSC_VER)
27+
# include "globalincs/toolchain/msvc.h"
28+
#else
29+
# error "Unknown toolchain detected!\n" \
30+
"Currently supported toolchains include:\n" \
31+
"\tMingW, Clang, GCC, MSVC\n" \
32+
"Update toolchain.h to add support for additional toolchains.\n"
33+
#endif
34+
35+
#endif /* _TOOLCHAIN_H */

code/globalincs/toolchain/clang.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (C) Volition, Inc. 1999. All rights reserved.
3+
*
4+
* All source code herein is the property of Volition, Inc. You may not sell
5+
* or otherwise commercially exploit the source or things you created based on
6+
* the source.
7+
*/
8+
9+
/**
10+
* @file
11+
*
12+
* @brief Macros to abstract compiler capabilities for the Clang toolchain
13+
*
14+
* @internal
15+
* This file should never be included directly; instead, one should
16+
* include toolchain.h which will pull in the file appropriate to
17+
* the detected toolchain.
18+
*/
19+
20+
#define SCP_FORMAT_STRING
21+
#define SCP_FORMAT_STRING_ARGS(x,y) __attribute__((format(printf, x, y)))
22+
23+
#ifdef NO_RESTRICT_USE
24+
# define RESTRICT
25+
#else
26+
# define RESTRICT restrict
27+
#endif
28+
29+
#define ASSUME(x)
30+
31+
#if defined(NDEBUG)
32+
# define Assertion(expr, msg, ...) do { } while (0)
33+
#else
34+
/*
35+
* NOTE: Assertion() can only use its proper functionality in compilers
36+
* that support variadic macros.
37+
*/
38+
# define Assertion(expr, msg, ...) \
39+
do { \
40+
if (!(expr)) { \
41+
WinAssert(#espr, __FILE__, __LINE__, msg, ##_VA_ARGS__); \
42+
} \
43+
} while (0)
44+
#endif
45+
46+
/* C++11 Standard Detection */
47+
#if !defined(HAVE_CX11)
48+
/*
49+
* Clang does not seem to have a feature check for 'is_trivial'.
50+
* Assume it will be covered by one of the following checks ...
51+
* http://clang.llvm.org/docs/LanguageExtensions.html#feature_check
52+
*/
53+
# if __has_feature(cxx_static_assert) && __has_feature(cxx_auto_type)
54+
# define HAVE_CXX11
55+
# endif
56+
#endif
57+
58+
#define SIZE_T_ARG "%zu"
59+
#define PTRDIFF_T_ARG "%zd"
60+
61+
#define NOEXCEPT noexcept
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (C) Volition, Inc. 1999. All rights reserved.
3+
*
4+
* All source code herein is the property of Volition, Inc. You may not sell
5+
* or otherwise commercially exploit the source or things you created based on
6+
* the source.
7+
*/
8+
9+
/**
10+
* @file
11+
*
12+
* @brief Macros to abstract compiler capabilities (for doxygen use only)
13+
*/
14+
15+
/**
16+
* @brief Identifies a printf-style format string
17+
*/
18+
#define SCP_FORMAT_STRING
19+
20+
/**
21+
* @brief Specify which arguments are involved in printf-style string formatting
22+
*
23+
* @details Expands to a compiler specific attribute which specify where the
24+
* format arguments are located. Parameters are 1-based which also
25+
* includes the 'this' parameter at position 1 for class methods.
26+
*
27+
* @param formatArg Location of format string argument in the argument list
28+
* @param varArgs Location where the variable arguments begin
29+
*/
30+
#define SCP_FORMAT_STRING_ARGS(formatArg,varArgs)
31+
32+
/**
33+
* @brief Format specifier for a @c size_t argument
34+
*
35+
* Due to different runtimes using different format specifier for these types
36+
* it's necessary to hide these changes behind a macro. Use this in place of %zu
37+
*/
38+
#define SIZE_T_ARG "%zu"
39+
40+
/**
41+
* @brief Format specifier for a @c ptrdiff_t argument
42+
*
43+
* Due to different runtimes using different format specifier for these types
44+
* it's necessary to hide these changes behind a macro. Use this in place of %zd
45+
*/
46+
#define PTRDIFF_T_ARG "%zd"

code/globalincs/toolchain/gcc.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (C) Volition, Inc. 1999. All rights reserved.
3+
*
4+
* All source code herein is the property of Volition, Inc. You may not sell
5+
* or otherwise commercially exploit the source or things you created based on
6+
* the source.
7+
*/
8+
9+
/**
10+
* @file
11+
*
12+
* @brief Macros to abstract compiler capabilities for the GCC toolchain
13+
*
14+
* @internal
15+
* This file should never be included directly; instead, one should
16+
* include toolchain.h which will pull in the file appropriate to
17+
* the detected toolchain.
18+
*/
19+
20+
#define SCP_FORMAT_STRING
21+
#define SCP_FORMAT_STRING_ARGS(x,y) __attribute__((format(printf, x, y)))
22+
23+
#ifdef NO_RESTRICT_USE
24+
# define RESTRICT
25+
#else
26+
# define RESTRICT restrict
27+
#endif
28+
29+
#define ASSUME(x)
30+
31+
#if defined(NDEBUG)
32+
# define Assertion(expr, msg, ...) do {} while (0)
33+
#else
34+
/*
35+
* NOTE: Assertion() can only use its proper functionality in compilers
36+
* that support variadic macros.
37+
*/
38+
# define Assertion(expr, msg, ...) \
39+
do { \
40+
if (!(expr)) { \
41+
WinAssert(#espr, __FILE__, __LINE__, msg, ##_VA_ARGS__); \
42+
} \
43+
} while (0)
44+
#endif
45+
46+
/* C++11 Standard Detection */
47+
#if !defined(HAVE_CX11)
48+
/*
49+
* For GCC with autotools, see AX_CXX_comPiLE_STDCXX_11 macro in the
50+
* file "configure.ac". This sets HAVE_CXX11 & -std=c++0x or -std=c++11
51+
* as appropriate.
52+
*
53+
* TODO: Is anything else required here?
54+
*/
55+
#endif
56+
57+
#define SIZE_T_ARG "%zu"
58+
#define PTRDIFF_T_ARG "%zd"
59+
60+
#define NOEXCEPT noexcept

0 commit comments

Comments
 (0)