forked from GrammaTech/gtirb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlignOf.cmake
52 lines (46 loc) · 1.39 KB
/
AlignOf.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
macro(ALIGNOF TYPE LANG NAME)
if(NOT ALIGNOF_${NAME})
#
# Try to compile and run a foo grogram. The alignment result will be stored
# in ALIGNOF_${CHECK_TYPE}
#
set(INCLUDE_HEADERS
"#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>"
)
foreach(File ${CMAKE_REQUIRED_INCLUDES})
set(INCLUDE_HEADERS "${INCLUDE_HEADERS}\n#include <${File}>\n")
endforeach()
set(INCLUDE_HEADERS "${INCLUDE_HEADERS}\n#include <stdint.h>\n")
file(
WRITE
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/c_get_${NAME}_alignment.${LANG}"
"${INCLUDE_HEADERS}
int main(){
char diff;
struct foo {char a; ${TYPE} b;};
struct foo *p = (struct foo *) malloc(sizeof(struct foo));
diff = ((char *)&p->b) - ((char *)&p->a);
return diff;
}"
)
try_run(
ALIGNOF_${NAME}
COMPILE_RESULT
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/"
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/c_get_${NAME}_alignment.${LANG}"
COMPILE_OUTPUT_VARIABLE "ALIGNOF_${NAME}_COMPILE_VAR"
)
if(NOT COMPILE_RESULT)
message(
FATAL_ERROR
"Check alignment of ${TYPE} in ${LANG}: compilation failed: ${ALIGNOF_${NAME}_COMPILE_VAR}"
)
else()
message(
STATUS "Check alignment of ${TYPE} in ${LANG}: ${ALIGNOF_${NAME}}"
)
endif()
endif()
endmacro()