Skip to content
This repository was archived by the owner on Mar 21, 2024. It is now read-only.

Commit 8e09201

Browse files
committed
Check for macro collisions with system headers during header testing.
Related to NVIDIA/thrust#1244.
1 parent 2442f44 commit 8e09201

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

cmake/CubHeaderTesting.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
# .inl files are not globbed for, because they are not supposed to be used as public
55
# entrypoints.
66

7+
# Meta target for all configs' header builds:
8+
add_custom_target(cub.all.headers)
9+
710
file(GLOB_RECURSE headers
811
RELATIVE "${CUB_SOURCE_DIR}/cub"
912
CONFIGURE_DEPENDS
@@ -25,5 +28,6 @@ foreach(cub_target IN LISTS CUB_TARGETS)
2528
target_link_libraries(${headertest_target} PUBLIC ${cub_target})
2629
cub_clone_target_properties(${headertest_target} ${cub_target})
2730

31+
add_dependencies(cub.all.headers ${headertest_target})
2832
add_dependencies(${config_prefix}.all ${headertest_target})
2933
endforeach()

cmake/header_test.in

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,46 @@
1+
// This source file checks that:
2+
// 1) Header <cub/${header}> compiles without error.
3+
// 2) Common macro collisions with platform/system headers are avoided.
4+
5+
// Define CUB_MACRO_CHECK(macro, header), which emits a diagnostic indicating
6+
// a potential macro collision and halts.
7+
//
8+
// Use raw platform checks instead of the CUB_HOST_COMPILER macros since we
9+
// don't want to #include any headers other than the one being tested.
10+
//
11+
// This is only implemented for MSVC/GCC/Clang.
12+
#if defined(_MSC_VER) // MSVC
13+
14+
// Fake up an error for MSVC
15+
#define CUB_MACRO_CHECK_IMPL(msg) \
16+
/* Print message that looks like an error: */ \
17+
__pragma(message(__FILE__ ":" CUB_MACRO_CHECK_IMPL0(__LINE__) \
18+
": error: " #msg)) \
19+
/* abort compilation due to static_assert or syntax error: */ \
20+
static_assert(false, #msg);
21+
#define CUB_MACRO_CHECK_IMPL0(x) CUB_MACRO_CHECK_IMPL1(x)
22+
#define CUB_MACRO_CHECK_IMPL1(x) #x
23+
24+
#elif defined(__clang__) || defined(__GNUC__)
25+
26+
// GCC/clang are easy:
27+
#define CUB_MACRO_CHECK_IMPL(msg) CUB_MACRO_CHECK_IMPL0(GCC error #msg)
28+
#define CUB_MACRO_CHECK_IMPL0(expr) _Pragma(#expr)
29+
30+
#endif
31+
32+
// Hacky way to build a string, but it works on all tested platforms.
33+
#define CUB_MACRO_CHECK(MACRO, HEADER) \
34+
CUB_MACRO_CHECK_IMPL(Identifier MACRO should not be used from Thrust \
35+
headers due to conflicts with HEADER.)
36+
37+
// complex.h conflicts
38+
#define I CUB_MACRO_CHECK('I', complex.h)
39+
40+
// windows.h conflicts
41+
// Disabling for now; we use min/max in many places, but since most
42+
// projects build with NOMINMAX this doesn't seem to be high priority to fix.
43+
//#define min(...) CUB_MACRO_CHECK('min', windows.h)
44+
//#define max(...) CUB_MACRO_CHECK('max', windows.h)
45+
146
#include <cub/${header}>

0 commit comments

Comments
 (0)