Debian Codegraph @

matio-1.5.30/cmake/compilerOptions.cmake

raw
1 # Check functions
2 include(CheckSymbolExists)
3 check_symbol_exists(vsnprintf stdio.h HAVE_VSNPRINTF)
4 check_symbol_exists(snprintf stdio.h HAVE_SNPRINTF)
5 check_symbol_exists(vasprintf stdio.h HAVE_VASPRINTF)
6 check_symbol_exists(asprintf stdio.h HAVE_ASPRINTF)
7 check_symbol_exists(strcasecmp "strings.h;string.h" HAVE_STRCASECMP)
8 check_symbol_exists(getopt unistd.h HAVE_GETOPT)
9 check_symbol_exists(va_copy stdarg.h HAVE_VA_COPY)
10 check_symbol_exists(__va_copy stdarg.h HAVE___VA_COPY)
11 check_symbol_exists(localeconv locale.h HAVE_LOCALECONV)
12 check_symbol_exists(fseeko stdio.h HAVE_FSEEKO)
13 check_symbol_exists(ftello stdio.h HAVE_FTELLO)
14 check_symbol_exists(fseeko64 stdio.h HAVE_FSEEKO64)
15 check_symbol_exists(ftello64 stdio.h HAVE_FTELLO64)
16 check_symbol_exists(_fseeki64 stdio.h HAVE__FSEEKI64)
17 check_symbol_exists(_ftelli64 stdio.h HAVE__FTELLI64)
18
19 include(CheckIncludeFile)
20 check_include_file(inttypes.h HAVE_INTTYPES_H)
21 check_include_file(stdint.h HAVE_STDINT_H)
22 check_include_file(intsafe.h HAVE_INTSAFE_H)
23 check_include_file(strings.h HAVE_STRINGS_H)
24 check_include_file(ctype.h HAVE_CTYPE_H)
25 check_include_file(stdlib.h HAVE_STDLIB_H)
26 check_include_file(string.h HAVE_STRING_H)
27 check_include_file(stdarg.h HAVE_STDARG_H)
28 check_include_file(memory.h HAVE_MEMORY_H)
29 check_include_file(unistd.h HAVE_UNISTD_H)
30 check_include_file(sys/stat.h HAVE_SYS_STAT_H)
31 check_include_file(varargs.h HAVE_VARARGS_H)
32 check_include_file(locale.h HAVE_LOCALE_H)
33 check_include_file(dlfcn.h HAVE_DLFCN_H)
34 check_include_file(float.h HAVE_FLOAT_H)
35 set(MATIO_HAVE_STDINT_H ${HAVE_STDINT_H})
36 set(MATIO_HAVE_INTTYPES_H ${HAVE_INTTYPES_H})
37 if(NOT MATIO_HAVE_STDINT_H AND MSVC)
38 set(STDINT_MSVC 1)
39 endif()
40
41 # Check C types
42 include(CheckTypeSize)
43 check_type_size(char SIZEOF_CHAR)
44 check_type_size(double SIZEOF_DOUBLE)
45 check_type_size(float SIZEOF_FLOAT)
46 check_type_size(int SIZEOF_INT)
47 check_type_size(long SIZEOF_LONG)
48 check_type_size("long long" SIZEOF_LONG_LONG)
49 check_type_size(short SIZEOF_SHORT)
50 check_type_size(size_t SIZEOF_SIZE_T)
51 check_type_size(intmax_t INTMAX_T)
52 check_type_size(uintmax_t UINTMAX_T)
53 check_type_size(uintptr_t UINTPTR_T)
54 check_type_size(ptrdiff_t PTRDIFF_T)
55 check_type_size("long double" LONG_DOUBLE)
56 check_type_size("long long int" LONG_LONG_INT)
57 check_type_size("unsigned long long int" UNSIGNED_LONG_LONG_INT)
58 set(SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})
59
60 # Make the variables HAVE_MAT_UINT8_T, etc...
61 set(TYPES uint8_t uint16_t uint32_t uint64_t int8_t int16_t int32_t int64_t)
62 foreach(type ${TYPES})
63 check_type_size(${type} "SIZEOF_${type}")
64 set(_mat_${type} ${type})
65 string(TOUPPER ${type} TYPE_UPPER)
66 set(HAVE_MAT_${TYPE_UPPER} ${HAVE_SIZEOF_${type}})
67 if(STDINT_MSVC)
68 set(HAVE_MAT_${TYPE_UPPER} 1)
69 endif()
70 endforeach()
71
72 include(CheckLibraryExists)
73 check_library_exists(m pow "" HAVE_LIBM)
74
75 include(CheckCSourceCompiles)
76 set(TEST_CODE_DECIMAL_POINT "
77 #include <locale.h>
78 int main() { struct lconv l; l.decimal_point; return 0; }
79 "
80 )
81 check_c_source_compiles("${TEST_CODE_DECIMAL_POINT}" HAVE_STRUCT_LCONV_DECIMAL_POINT)
82
83 set(TEST_CODE_THOUSANDS_SEP "
84 #include <locale.h>
85 int main(){ struct lconv l; l.thousands_sep; return 0;}
86 "
87 )
88 check_c_source_compiles("${TEST_CODE_THOUSANDS_SEP}" HAVE_STRUCT_LCONV_THOUSANDS_SEP)
89
90 set(USE_GNU_LINK_FLAGS 0)
91 set(USE_LLVM_MACOS_LINK_FLAGS 0)
92 set(REQUIRE_EXPLICIT_LIBC_LINK 0)
93 if(NOT MSVC)
94 # OpenBSD apparently requires an explicit -lc if -Wl,--no-undefined
95 # is used, but that is NOT required on other platforms, and on even
96 # other platforms (e.g. MinGW) -lc might not even exist at all.
97 # Therefore detect if -lc works and if it is required, and omit it
98 # if it doesn't.
99 # Just running check_linker_flag() doesn't help here, because we
100 # actually need to reference a symbol from libc to cause an error.
101 # Use -shared in CMAKE_REQUIRED_LINK_OPTIONS because how symbols
102 # (esp. undefined ones) are resolved during linking can differ
103 # between shared libraries and executables.
104 set(TEST_SRC_LINK_NO_UNDEFINED "
105 #include <stdlib.h>
106 int main() { int* foo = (int*) malloc(sizeof(int)); free(foo); return 0; }
107 ")
108
109 set(CMAKE_REQUIRED_FLAGS "-fPIC")
110 set(CMAKE_REQUIRED_LINK_OPTIONS "-shared;-Wl,--no-undefined")
111 check_c_source_compiles("${TEST_SRC_LINK_NO_UNDEFINED}" HAVE_LINK_NO_UNDEFINED_IMPLICIT_LIBC)
112 set(CMAKE_REQUIRED_LINK_OPTIONS "-shared;-Wl,--no-undefined;-lc")
113 check_c_source_compiles("${TEST_SRC_LINK_NO_UNDEFINED}" HAVE_LINK_NO_UNDEFINED_EXPLICIT_LIBC)
114 # Reuse the same source code for other linker flag tests (This was
115 # previously guarded by CMake version >= 3.17 using
116 # check_linker_flag, but this variant should work for even older
117 # CMake versions.)
118 set(CMAKE_REQUIRED_LINK_OPTIONS "-shared;-Wl,--retain-symbols-file,${PROJECT_SOURCE_DIR}/src/matio.sym")
119 check_c_source_compiles("${TEST_SRC_LINK_NO_UNDEFINED}" HAVE_LINK_RETAIN_SYMBOLS_FILE)
120 set(CMAKE_REQUIRED_LINK_OPTIONS "-shared;-Wl,-undefined,error")
121 check_c_source_compiles("${TEST_SRC_LINK_NO_UNDEFINED}" HAVE_LINK_UNDEFINED_ERROR)
122 set(CMAKE_REQUIRED_FLAGS "")
123 set(CMAKE_REQUIRED_LINK_OPTIONS "")
124
125 if((HAVE_LINK_NO_UNDEFINED_IMPLICIT_LIBC OR HAVE_LINK_NO_UNDEFINED_EXPLICIT_LIBC) AND HAVE_LINK_RETAIN_SYMBOLS_FILE)
126 message(VERBOSE "Using GNU-style linker flags")
127 set(USE_GNU_LINK_FLAGS 1)
128 elseif(APPLE AND HAVE_LINK_UNDEFINED_ERROR)
129 message(VERBOSE "Using Apple-clang-style linker flags")
130 set(USE_LLVM_MACOS_LINK_FLAGS 1)
131 endif()
132
133 if(NOT HAVE_LINK_NO_UNDEFINED_IMPLICIT_LIBC AND HAVE_LINK_NO_UNDEFINED_EXPLICIT_LIBC)
134 message(VERBOSE "The usage of -Wl,--no-undefined requires the explicit usage of -lc on this platform.")
135 set(REQUIRE_EXPLICIT_LIBC_LINK 1)
136 endif()
137 endif()