Echo Writes Code

CrucibleCheck.cmake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
include_guard(GLOBAL)

include(CheckCXXSourceCompiles)

function(crucible_check_platform_is_64_bit)
  message(STATUS "Checking if target system is 64-bit...")

  set(CMAKE_REQUIRED_QUIET TRUE)

  check_cxx_source_compiles(
    "
    #include <climits>
    static_assert(sizeof(void *) == 8);
    static_assert(CHAR_BIT == 8);
    int main() { return 0; }
    "
    CRUCIBLE_PLATFORM_IS_64_BIT)

  if(CRUCIBLE_PLATFORM_IS_64_BIT)
    message(STATUS "  Yes!")
  else()
    message(FATAL_ERROR "Crucible only supports 64-bit targets")
  endif()
endfunction()

function(crucible_check_platform_is_posix_like)
  message(STATUS "Checking if target system is POSIX-like...")

  if(UNIX)
    set(CRUCIBLE_PLATFORM_IS_POSIX_LIKE TRUE CACHE INTERNAL "")
    message(STATUS "  Yes!")
  else()
    message(STATUS "  No!")
  endif()
endfunction()

function(crucible_check_platform_is_windows_like)
  message(STATUS "Checking if target system is Windows-like...")

  if(WIN32)
    set(CRUCIBLE_PLATFORM_IS_WINDOWS_LIKE TRUE CACHE INTERNAL "")
    message(STATUS "  Yes!")
  else()
    message(STATUS "  No!")
  endif()
endfunction()

function(crucible_check_platform_has_cxa_demangle)
	message(STATUS "Checking if target system has `::abi::__cxa_demangle()...`")

	set(CMAKE_REQUIRED_QUIET TRUE)

	check_cxx_source_compiles(
		"
		#include <cstddef>
		#include <cxxabi.h>
		int main()
		{
			char *demangled_symbol_data { nullptr };
			std::size_t demangled_symbol_size { 0 };
			int demangled_symbol_status { 0 };
			demangled_symbol_data = ::abi::__cxa_demangle(\"\", nullptr, &demangled_symbol_size, &demangled_symbol_status);
			static_cast<void>(demangled_symbol_data);
			static_cast<void>(demangled_symbol_size);
			static_cast<void>(demangled_symbol_status);
			return 0;
		}
		"
		CRUCIBLE_PLATFORM_HAS_CXA_DEMANGLE)

	if(CRUCIBLE_PLATFORM_HAS_CXA_DEMANGLE)
		message(STATUS "  Yes!")
	else()
		message(STATUS "  No!")
	endif()
endfunction()

function(crucible_check_platform_has_dirent_d_namlen)
  message(STATUS "Checking if target system has `struct dirent::d_namlen`...")

  set(CMAKE_REQUIRED_QUIET TRUE)

  check_cxx_source_compiles(
    "
    #include <dirent.h>
    int main()
    {
      struct ::dirent entry;
      entry.d_namlen = 0;
      return 0;
    }
    "
    CRUCIBLE_PLATFORM_HAS_DIRENT_D_NAMLEN)

  if(CRUCIBLE_PLATFORM_HAS_DIRENT_D_NAMLEN)
    message(STATUS "  Yes!")
  else()
    message(STATUS "  No!")
  endif()
endfunction()

function(crucible_check_platform_path_separator)
  message(STATUS "Checking platform native path separator...")

  # Might break, but CMake has no other way to get this information
  file(TO_NATIVE_PATH "/" PLATFORM_PATH_SEPARATOR)

  # Double-escape because if it's `\`, the C++ compiler will _also_ parse it as an escape sequence
  string(REPLACE "\\" "\\\\" PLATFORM_PATH_SEPARATOR_ESCAPED "${PLATFORM_PATH_SEPARATOR}")

  set(CRUCIBLE_PLATFORM_PATH_SEPARATOR "${PLATFORM_PATH_SEPARATOR_ESCAPED}" CACHE INTERNAL "")
  message(STATUS "  ${CRUCIBLE_PLATFORM_PATH_SEPARATOR}")
endfunction()

function(crucible_check_platform_newline_sequence)
  message(STATUS "Checking platform native newline sequence...")

  if(WIN32)
    set(CRUCIBLE_PLATFORM_NEWLINE_SEQUENCE "\\r\\n" CACHE INTERNAL "")
  else()
    set(CRUCIBLE_PLATFORM_NEWLINE_SEQUENCE "\\n" CACHE INTERNAL "")
  endif()

  message(STATUS "  ${CRUCIBLE_PLATFORM_NEWLINE_SEQUENCE}")
endfunction()