summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@profusion.mobi>2017-01-23 15:49:17 -0200
committerGustavo Sverzut Barbieri <barbieri@profusion.mobi>2017-01-23 16:09:50 -0200
commite8f2bdb079598ff406a2c8010cc8745d47ecc010 (patch)
treebe517d863b54d0b015f87c3c73c0dd787bce63d5
parentea83a197beb079256a95e521531b4140a33f6a32 (diff)
cmake: better functions to check headers, functions and types.
functions that handle all the required settings in one place.
-rw-r--r--cmake/helpers/CommonHeaderChecks.cmake228
1 files changed, 154 insertions, 74 deletions
diff --git a/cmake/helpers/CommonHeaderChecks.cmake b/cmake/helpers/CommonHeaderChecks.cmake
index a92dabd9b7..c895f793e0 100644
--- a/cmake/helpers/CommonHeaderChecks.cmake
+++ b/cmake/helpers/CommonHeaderChecks.cmake
@@ -1,81 +1,160 @@
1unset(HEADER_FILE_CONTENT CACHE) 1unset(HEADER_FILE_CONTENT CACHE)
2 2
3macro(HEADER_CHECK header var) 3# HEADER_CHECK(header [NAME variable] [INCLUDE_FILES extra1.h .. extraN.h])
4 CHECK_INCLUDE_FILE(${header} ${var}) 4#
5 if (${${var}}) 5# Check if the header file exists, in such case define variable
6 set(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#define ${var} 1\n" CACHE INTERNAL "") 6# in configuration file.
7 else() 7#
8 set(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#undef ${var}\n" CACHE INTERNAL "") 8# Variable defaults to HAVE_${HEADER}, where HEADER is the uppercase
9 endif() 9# representation of the first parameter. It can be overridden using
10endmacro() 10# NAME keyword.
11#
12# To include extra files, then use INCLUDE_FILES keyword.
13function(HEADER_CHECK header)
14 string(TOUPPER HAVE_${header} var)
15 string(REGEX REPLACE "[^a-zA-Z0-9]" "_" var "${var}")
16 string(REGEX REPLACE "_{2,}" "_" var "${var}")
11 17
12macro(FUNC_CHECK func header var) 18 cmake_parse_arguments(PARAMS "" "NAME" "INCLUDE_FILES" ${ARGN})
13 CHECK_SYMBOL_EXISTS(${func} ${header} ${var}) 19
14 if (${${var}} ) 20 if(PARAMS_NAME)
15 set(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#define ${var} 1\n" CACHE INTERNAL "") 21 set(var ${PARAMS_NAME})
16 else() 22 endif()
17 set(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#undef ${var}\n" CACHE INTERNAL "") 23
18 endif() 24 set(CMAKE_EXTRA_INCLUDE_FILES "${PARAMS_INCLUDE_FILES}")
19endmacro() 25
20 26 CHECK_INCLUDE_FILE(${header} ${var})
21macro(TYPE_CHECK func header var) 27
22 set(CMAKE_EXTRA_INCLUDE_FILES ${header}) 28 if(${${var}})
23 CHECK_TYPE_SIZE(${func} ${var}) 29 SET_GLOBAL(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#define ${var} 1\n")
24 unset(CMAKE_EXTRA_INCLUDE_FILES) 30 else()
25 if (${${var}} GREATER 0) 31 SET_GLOBAL(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#undef ${var}\n")
26 set(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#define ${var} 1\n" CACHE INTERNAL "") 32 endif()
27 else() 33endfunction()
28 set(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#undef ${var}\n" CACHE INTERNAL "") 34
29 endif() 35# FUNC_CHECK(func [NAME variable]
30endmacro() 36# [INCLUDE_FILES header1.h .. headerN.h]
31 37# [LIBRARIES lib1 ... libN]
32 38# [DEFINITIONS -DA=1 .. -DN=123]
33FUNC_CHECK(geteuid unistd.h HAVE_GETEUID) 39# [FLAGS -cmdlineparam1 .. -cmdlineparamN]
34FUNC_CHECK(getuid unistd.h HAVE_GETUID) 40# [CXX])
35FUNC_CHECK(getpagesize unistd.h HAVE_GETPAGESIZE) 41#
36FUNC_CHECK(strlcpy bsd/string.h HAVE_STRLCPY) 42# Check if the function exists, in such case define variable in
37FUNC_CHECK(mmap sys/mman.h HAVE_MMAP) 43# configuration file.
38FUNC_CHECK(fchmod sys/stat.h HAVE_FCHMOD) 44#
39FUNC_CHECK(clock_gettime time.h HAVE_CLOCK_GETTIME) 45# Variable defaults to HAVE_${FUNC}, where FUNC is the uppercase
40FUNC_CHECK(fstatat sys/stat.h HAVE_ATFILE_SOURCE) 46# representation of the first parameter. It can be overridden using
41FUNC_CHECK(dirfd "dirent.h;sys/types.h" HAVE_DIRFD) 47# NAME keyword.
42FUNC_CHECK(fpathconf "unistd.h" HAVE_FPATHCONF) 48#
43FUNC_CHECK(fpathconf "unistd.h" HAVE_FPATHCONF) 49# To define include files use INCLUDE_FILES keyword.
44FUNC_CHECK(listxattr "sys/types.h;sys/xattr.h" HAVE_LISTXATTR) 50#
45FUNC_CHECK(setxattr "sys/types.h;sys/xattr.h" HAVE_SETXATTR) 51# To use C++ compiler, use CXX keyword
46FUNC_CHECK(getxattr "sys/types.h;sys/xattr.h" HAVE_GETXATTR) 52function(FUNC_CHECK func)
47 53 string(TOUPPER HAVE_${func} var)
48set(CMAKE_REQUIRED_LIBRARIES "-ldl") 54 string(REGEX REPLACE "_{2,}" "_" var "${var}")
49FUNC_CHECK(dlopen dlfcn.h HAVE_DLOPEN) 55
50unset(CMAKE_REQUIRED_LIBRARIES) 56 cmake_parse_arguments(PARAMS "CXX" "NAME" "INCLUDE_FILES;LIBRARIES;DEFINITIONS;FLAGS" ${ARGN})
51 57
52FUNC_CHECK(mtrace mcheck.h HAVE_MTRACE) 58 set(CMAKE_REQUIRED_LIBRARIES "${PARAMS_LIBRARIES}")
53FUNC_CHECK(strerror_r string.h HAVE_STRERROR_R) 59 set(CMAKE_REQUIRED_DEFINITIONS "${PARAMS_DEFINITIONS}")
54set(CMAKE_REQUIRED_LIBRARIES "-lrt") 60 set(CMAKE_REQUIRED_FLAGS "${PARAMS_FLAGS}")
55FUNC_CHECK(shm_open "sys/mman.h;sys/stat.h;fcntl.h" HAVE_SHM_OPEN) 61
56 62 if(PARAMS_CXX)
57 63 check_cxx_symbol_exists(${func} "${PARAMS_INCLUDE_FILES}" ${var})
58set(CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE=1") 64 else()
59FUNC_CHECK(sched_getcpu sched.h HAVE_SCHED_GETCPU) 65 check_symbol_exists(${func} "${PARAMS_INCLUDE_FILES}" ${var})
60FUNC_CHECK(splice fcntl.h HAVE_SPLICE) 66 endif()
61FUNC_CHECK(fcntl fcntl.h HAVE_FCNTL) 67
62set(CMAKE_REQUIRED_LIBRARIES "-ldl") 68 if(${${var}} )
63FUNC_CHECK(dladdr dlfcn.h HAVE_DLADDR) 69 SET_GLOBAL(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#define ${var} 1\n")
64 70 else()
65TYPE_CHECK(siginfo_t signal.h HAVE_SIGINFO_T) 71 SET_GLOBAL(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#undef ${var}\n")
66 72 endif()
67HEADER_CHECK(stdlib.h HAVE_STDLIB_H) 73endfunction()
68HEADER_CHECK(sys/mman.h HAVE_SYS_MMAN_H) 74
69HEADER_CHECK(dirent.h HAVE_DIRENT_H) 75# TYPE_CHECK(type [NAME variable]
70HEADER_CHECK(iconv.h HAVE_ICONV_H) 76# [INCLUDE_FILES file1.h ... fileN.h]
71HEADER_CHECK(sys/auxv.h HAVE_SYS_AUXV_H) 77# [LIBRARIES lib1 ... libN]
72HEADER_CHECK(asm/hwcap.h HAVE_ASM_HWCAP_H) 78# [DEFINITIONS -DA=1 .. -DN=123]
73HEADER_CHECK(mcheck.h HAVE_MCHECK_H) 79# [FLAGS -cmdlineparam1 .. -cmdlineparamN]
74HEADER_CHECK(sys/types.h HAVE_SYS_TYPES_H) 80# [CXX])
75HEADER_CHECK(execinfo.h HAVE_EXECINFO_H) 81#
76HEADER_CHECK(libunwind.h HAVE_UNWIND) 82# Check if the type exists and its size, in such case define variable
77HEADER_CHECK(mtrace.h HAVE_MTRACE_H) 83# in configuration file.
84#
85# Variable defaults to HAVE_${TYPE}, where TYPE is the uppercase
86# representation of the first parameter. It can be overridden using
87# NAME keyword.
88#
89# To define include files use INCLUDE_FILES keyword.
90#
91# To use C++ compiler, use CXX keyword
92function(TYPE_CHECK type)
93 string(TOUPPER HAVE_${type} var)
94 string(REGEX REPLACE "_{2,}" "_" var "${var}")
95
96 cmake_parse_arguments(PARAMS "CXX" "NAME" "INCLUDE_FILES;LIBRARIES;DEFINITIONS;FLAGS" ${ARGN})
97
98 set(CMAKE_REQUIRED_LIBRARIES "${PARAMS_LIBRARIES}")
99 set(CMAKE_REQUIRED_DEFINITIONS "${PARAMS_DEFINITIONS}")
100 set(CMAKE_REQUIRED_FLAGS "${PARAMS_FLAGS}")
101 set(CMAKE_EXTRA_INCLUDE_FILES "${PARAMS_INCLUDE_FILES}")
102
103 if(PARAMS_CXX)
104 set(lang CXX)
105 else()
106 set(lang C)
107 endif()
108
109 CHECK_TYPE_SIZE(${type} ${var} LANGUAGE ${lang})
110
111 if(HAVE_${var})
112 SET_GLOBAL(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#define ${var} 1\n")
113 else()
114 SET_GLOBAL(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#undef ${var}\n")
115 endif()
116endfunction()
117
118
119FUNC_CHECK(geteuid INCLUDE_FILES unistd.h)
120FUNC_CHECK(getuid INCLUDE_FILES unistd.h)
121FUNC_CHECK(getpagesize INCLUDE_FILES unistd.h)
122FUNC_CHECK(strlcpy INCLUDE_FILES bsd/string.h)
123FUNC_CHECK(mmap INCLUDE_FILES sys/mman.h)
124FUNC_CHECK(fchmod INCLUDE_FILES sys/stat.h)
125FUNC_CHECK(clock_gettime INCLUDE_FILES time.h)
126FUNC_CHECK(fstatat NAME HAVE_ATFILE_SOURCE INCLUDE_FILES sys/stat.h)
127FUNC_CHECK(dirfd INCLUDE_FILES dirent.h sys/types.h)
128FUNC_CHECK(fpathconf INCLUDE_FILES unistd.h)
129FUNC_CHECK(listxattr INCLUDE_FILES sys/types.h sys/xattr.h)
130FUNC_CHECK(setxattr INCLUDE_FILES sys/types.h sys/xattr.h)
131FUNC_CHECK(getxattr INCLUDE_FILES sys/types.h;sys/xattr.h)
132FUNC_CHECK(dlopen INCLUDE_FILES dlfcn.h LIBRARIES dl)
133FUNC_CHECK(mtrace INCLUDE_FILES mcheck.h)
134FUNC_CHECK(strerror_r INCLUDE_FILES string.h)
135FUNC_CHECK(shm_open INCLUDE_FILES sys/mman.h sys/stat.h fcntl.h LIBRARIES rt)
136FUNC_CHECK(sched_getcpu INCLUDE_FILES sched.h DEFINITIONS "-D_GNU_SOURCE=1")
137FUNC_CHECK(splice INCLUDE_FILES fcntl.h DEFINITIONS "-D_GNU_SOURCE=1")
138FUNC_CHECK(fcntl INCLUDE_FILES fcntl.h)
139FUNC_CHECK(dladdr INCLUDE_FILES dlfcn.h LIBRARIES dl DEFINITIONS "-D_GNU_SOURCE=1")
140
141TYPE_CHECK(siginfo_t INCLUDE_FILES signal.h)
142
143HEADER_CHECK(stdlib.h)
144HEADER_CHECK(sys/mman.h)
145HEADER_CHECK(dirent.h)
146HEADER_CHECK(iconv.h)
147HEADER_CHECK(sys/auxv.h)
148HEADER_CHECK(asm/hwcap.h)
149HEADER_CHECK(mcheck.h)
150HEADER_CHECK(sys/types.h)
151HEADER_CHECK(execinfo.h)
152HEADER_CHECK(libunwind.h NAME HAVE_UNWIND)
78 153
154# EFL_HEADER_CHECKS_FINALIZE(file)
155#
156# Write the configuration gathered with HEADER_CHECK(), TYPE_CHECK()
157# and FUNC_CHECK() to the given file.
79function(EFL_HEADER_CHECKS_FINALIZE file) 158function(EFL_HEADER_CHECKS_FINALIZE file)
80 file(WRITE ${file}.new ${HEADER_FILE_CONTENT}) 159 file(WRITE ${file}.new ${HEADER_FILE_CONTENT})
81 if (NOT EXISTS ${file}) 160 if (NOT EXISTS ${file})
@@ -92,4 +171,5 @@ function(EFL_HEADER_CHECKS_FINALIZE file)
92 message(STATUS "${file} was updated.") 171 message(STATUS "${file} was updated.")
93 endif() 172 endif()
94 endif() 173 endif()
174 unset(HEADER_FILE_CONTENT CACHE) # allow to reuse with an empty contents
95endfunction() 175endfunction()