ac_converter.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #!/usr/bin/env python3
  2. # Copyright 2015 The Meson development team
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. """This script reads config.h.meson, looks for header
  13. checks and writes the corresponding meson declaration.
  14. Copy config.h.in to config.h.meson, replace #undef
  15. with #mesondefine and run this. We can't do this automatically
  16. because some configure scripts have #undef statements
  17. that are unrelated to configure checks.
  18. """
  19. import sys
  20. # Add stuff here as it is encountered.
  21. function_data = \
  22. {'HAVE_FEENABLEEXCEPT': ('feenableexcept', 'fenv.h'),
  23. 'HAVE_FECLEAREXCEPT': ('feclearexcept', 'fenv.h'),
  24. 'HAVE_FEDISABLEEXCEPT': ('fedisableexcept', 'fenv.h'),
  25. 'HAVE_MMAP': ('mmap', 'sys/mman.h'),
  26. 'HAVE_GETPAGESIZE': ('getpagesize', 'unistd.h'),
  27. 'HAVE_GETISAX': ('getisax', 'sys/auxv.h'),
  28. 'HAVE_GETTIMEOFDAY': ('gettimeofday', 'sys/time.h'),
  29. 'HAVE_MPROTECT': ('mprotect', 'sys/mman.h'),
  30. 'HAVE_POSIX_MEMALIGN': ('posix_memalign', 'stdlib.h'),
  31. 'HAVE_SIGACTION': ('sigaction', 'signal.h'),
  32. 'HAVE_ALARM': ('alarm', 'unistd.h'),
  33. 'HAVE_CLOCK_GETTIME': ('clock_gettime', 'time.h'),
  34. 'HAVE_CTIME_R': ('ctime_r', 'time.h'),
  35. 'HAVE_DRAND48': ('drand48', 'stdlib.h'),
  36. 'HAVE_FLOCKFILE': ('flockfile', 'stdio.h'),
  37. 'HAVE_FORK': ('fork', 'unistd.h'),
  38. 'HAVE_FUNLOCKFILE': ('funlockfile', 'stdio.h'),
  39. 'HAVE_GETLINE': ('getline', 'stdio.h'),
  40. 'HAVE_LINK': ('link', 'unistd.h'),
  41. 'HAVE_RAISE': ('raise', 'signal.h'),
  42. 'HAVE_STRNDUP': ('strndup', 'string.h'),
  43. 'HAVE_SCHED_GETAFFINITY': ('sched_getaffinity', 'sched.h'),
  44. 'HAVE_WAITPID': ('waitpid', 'sys/wait.h'),
  45. 'HAVE_XRENDERCREATECONICALGRADIENT': ('XRenderCreateConicalGradient', 'xcb/render.h'),
  46. 'HAVE_XRENDERCREATELINEARGRADIENT': ('XRenderCreateLinearGradient', 'xcb/render.h'),
  47. 'HAVE_XRENDERCREATERADIALGRADIENT': ('XRenderCreateRadialGradient', 'xcb/render.h'),
  48. 'HAVE_XRENDERCREATESOLIDFILL': ('XRenderCreateSolidFill', 'xcb/render.h'),
  49. 'HAVE_DCGETTEXT': ('dcgettext', 'libintl.h'),
  50. 'HAVE_ENDMNTENT': ('endmntent', 'mntent.h'),
  51. 'HAVE_ENDSERVENT': ('endservent', 'netdb.h'),
  52. 'HAVE_EVENTFD': ('eventfd', 'sys/eventfd.h'),
  53. 'HAVE_FALLOCATE': ('fallocate', 'fcntl.h'),
  54. 'HAVE_FCHMOD': ('fchmod', 'sys/stat.h'),
  55. 'HAVE_FCHOWN': ('fchown', 'unistd.h'),
  56. 'HAVE_FDWALK': ('fdwalk', 'stdlib.h'),
  57. 'HAVE_FSYNC': ('fsync', 'unistd.h'),
  58. 'HAVE_GETC_UNLOCKED': ('getc_unlocked', 'stdio.h'),
  59. 'HAVE_GETFSSTAT': ('getfsstat', 'sys/mount.h'),
  60. 'HAVE_GETMNTENT_R': ('getmntent_r', 'mntent.h'),
  61. 'HAVE_GETPROTOBYNAME_R': ('getprotobyname_r', 'netdb.h'),
  62. 'HAVE_GETRESUID': ('getresuid', 'unistd.h'),
  63. 'HAVE_GETVFSSTAT': ('getvfsstat', 'sys/statvfs.h'),
  64. 'HAVE_GMTIME_R': ('gmtime_r', 'time.h'),
  65. 'HAVE_HASMNTOPT': ('hasmntopt', 'mntent.h'),
  66. 'HAVE_IF_INDEXTONAME': ('if_indextoname', 'net/if.h'),
  67. 'HAVE_IF_NAMETOINDEX': ('if_nametoindex', 'net/if.h'),
  68. 'HAVE_INOTIFY_INIT1': ('inotify_init1', 'sys/inotify.h'),
  69. 'HAVE_ISSETUGID': ('issetugid', 'unistd.h'),
  70. 'HAVE_KEVENT': ('kevent', 'sys/event.h'),
  71. 'HAVE_KQUEUE': ('kqueue', 'sys/event.h'),
  72. 'HAVE_LCHMOD': ('lchmod', 'sys/stat.h'),
  73. 'HAVE_LCHOWN': ('lchown', 'unistd.h'),
  74. 'HAVE_LSTAT': ('lstat', 'sys/stat.h'),
  75. 'HAVE_MEMCPY': ('memcpy', 'string.h'),
  76. 'HAVE_MEMALIGN': ('memalign', 'stdlib.h'),
  77. 'HAVE_MEMMEM': ('memmem', 'string.h'),
  78. 'HAVE_NEWLOCALE': ('newlocale', 'locale.h'),
  79. 'HAVE_PIPE2': ('pipe2', 'fcntl.h'),
  80. 'HAVE_POLL': ('poll', 'poll.h'),
  81. 'HAVE_PRLIMIT': ('prlimit', 'sys/resource.h'),
  82. 'HAVE_PTHREAD_ATTR_SETSTACKSIZE': ('pthread_attr_setstacksize', 'pthread.h'),
  83. 'HAVE_PTHREAD_CONDATTR_SETCLOCK': ('pthread_condattr_setclock', 'pthread.h'),
  84. 'HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP': ('pthread_cond_timedwait_relative_np', 'pthread.h'),
  85. 'HAVE_READLINK': ('readlink', 'unistd.h'),
  86. 'HAVE_RES_INIT': ('res_init', 'resolv.h'),
  87. 'HAVE_SENDMMSG': ('sendmmsg', 'sys/socket.h'),
  88. 'HAVE_SOCKET': ('socket', 'sys/socket.h'),
  89. 'HAVE_GETENV': ('getenv', 'stdlib.h'),
  90. 'HAVE_SETENV': ('setenv', 'stdlib.h'),
  91. 'HAVE_PUTENV': ('putenv', 'stdlib.h'),
  92. 'HAVE_UNSETENV': ('unsetenv', 'stdlib.h'),
  93. 'HAVE_SETMNTENT': ('setmntent', 'mntent.h'),
  94. 'HAVE_SNPRINTF': ('snprintf', 'stdio.h'),
  95. 'HAVE_SPLICE': ('splice', 'fcntl.h'),
  96. 'HAVE_STATFS': ('statfs', 'mount.h'),
  97. 'HAVE_STATVFS': ('statvfs', 'sys/statvfs.h'),
  98. 'HAVE_STPCOPY': ('stpcopy', 'string.h'),
  99. 'HAVE_STRCASECMP': ('strcasecmp', 'strings.h'),
  100. 'HAVE_STRLCPY': ('strlcpy', 'string.h'),
  101. 'HAVE_STRNCASECMP': ('strncasecmp', 'strings.h'),
  102. 'HAVE_STRSIGNAL': ('strsignal', 'signal.h'),
  103. 'HAVE_STRTOD_L': ('strtod_l', 'stdlib.h'),
  104. 'HAVE_STRTOLL_L': ('strtoll_l', 'stdlib.h'),
  105. 'HAVE_STRTOULL_L': ('strtoull_l', 'stdlib.h'),
  106. 'HAVE_SYMLINK': ('symlink', 'unistd.h'),
  107. 'HAVE_SYSCTLBYNAME': ('sysctlbyname', 'sys/sysctl.h'),
  108. 'HAVE_TIMEGM': ('timegm', 'time.h'),
  109. 'HAVE_USELOCALE': ('uselocale', 'xlocale.h'),
  110. 'HAVE_UTIMES': ('utimes', 'sys/time.h'),
  111. 'HAVE_VALLOC': ('valloc', 'stdlib.h'),
  112. 'HAVE_VASPRINTF': ('vasprintf', 'stdio.h'),
  113. 'HAVE_VSNPRINTF': ('vsnprintf', 'stdio.h'),
  114. 'HAVE_BCOPY': ('bcopy', 'strings.h'),
  115. 'HAVE_STRERROR': ('strerror', 'string.h'),
  116. 'HAVE_MEMMOVE': ('memmove', 'string.h'),
  117. 'HAVE_STRTOIMAX': ('strtoimax', 'inttypes.h'),
  118. 'HAVE_STRTOLL': ('strtoll', 'stdlib.h'),
  119. 'HAVE_STRTOQ': ('strtoq', 'stdlib.h'),
  120. 'HAVE_ACCEPT4': ('accept4', 'sys/socket.h'),
  121. 'HAVE_CHMOD': ('chmod', 'sys/stat.h'),
  122. 'HAVE_CHOWN': ('chown', 'unistd.h'),
  123. 'HAVE_FSTAT': ('fstat', 'sys/stat.h'),
  124. 'HAVE_GETADDRINFO': ('getaddrinfo', 'netdb.h'),
  125. 'HAVE_GETGRGID_R': ('getgrgid_r', 'grp.h'),
  126. 'HAVE_GETGRNAM_R': ('getgrnam_r', 'grp.h'),
  127. 'HAVE_GETGROUPS': ('getgroups', 'grp.h'),
  128. 'HAVE_GETOPT_LONG': ('getopt_long', 'getopt.h'),
  129. 'HAVE_GETPWNAM_R': ('getpwnam', 'pwd.h'),
  130. 'HAVE_GETPWUID_R': ('getpwuid_r', 'pwd.h'),
  131. 'HAVE_GETUID': ('getuid', 'unistd.h'),
  132. 'HAVE_LRINTF': ('lrintf', 'math.h'),
  133. 'HAVE_DECL_ISNAN': ('isnan', 'math.h'),
  134. 'HAVE_DECL_ISINF': ('isinf', 'math.h'),
  135. 'HAVE_ROUND': ('round', 'math.h'),
  136. 'HAVE_NEARBYINT': ('nearbyint', 'math.h'),
  137. 'HAVE_RINT': ('rint', 'math.h'),
  138. 'HAVE_MKFIFO': ('mkfifo', 'sys/stat.h'),
  139. 'HAVE_MLOCK': ('mlock', 'sys/mman.h'),
  140. 'HAVE_NANOSLEEP': ('nanosleep', 'time.h'),
  141. 'HAVE_PIPE': ('pipe', 'unistd.h'),
  142. 'HAVE_PPOLL': ('ppoll', 'poll.h'),
  143. 'HAVE_REGEXEC': ('regexec', 'regex.h'),
  144. 'HAVE_SETEGID': ('setegid', 'unistd.h'),
  145. 'HAVE_SETEUID': ('seteuid', 'unistd.h'),
  146. 'HAVE_SETPGID': ('setpgid', 'unistd.h'),
  147. 'HAVE_SETREGID': ('setregid', 'unistd.h'),
  148. 'HAVE_SETRESGID': ('setresgid', 'unistd.h'),
  149. 'HAVE_SETRESUID': ('setresuid', 'unistd.h'),
  150. 'HAVE_SHM_OPEN': ('shm_open', 'fcntl.h'),
  151. 'HAVE_SLEEP': ('sleep', 'unistd.h'),
  152. 'HAVE_STRERROR_R': ('strerror_r', 'string.h'),
  153. 'HAVE_STRTOF': ('strtof', 'stdlib.h'),
  154. 'HAVE_SYSCONF': ('sysconf', 'unistd.h'),
  155. 'HAVE_USLEEP': ('usleep', 'unistd.h'),
  156. 'HAVE_VFORK': ('vfork', 'unistd.h'),
  157. 'HAVE_MALLOC': ('malloc', 'stdlib.h'),
  158. 'HAVE_CALLOC': ('calloc', 'stdlib.h'),
  159. 'HAVE_REALLOC': ('realloc', 'stdlib.h'),
  160. 'HAVE_FREE': ('free', 'stdlib.h'),
  161. 'HAVE_ALLOCA': ('alloca', 'alloca.h'),
  162. 'HAVE_QSORT': ('qsort', 'stdlib.h'),
  163. 'HAVE_ABS': ('abs', 'stdlib.h'),
  164. 'HAVE_MEMSET': ('memset', 'string.h'),
  165. 'HAVE_MEMCMP': ('memcmp', 'string.h'),
  166. 'HAVE_STRLEN': ('strlen', 'string.h'),
  167. 'HAVE_STRLCAT': ('strlcat', 'string.h'),
  168. 'HAVE_STRDUP': ('strdup', 'string.h'),
  169. 'HAVE__STRREV': ('_strrev', 'string.h'),
  170. 'HAVE__STRUPR': ('_strupr', 'string.h'),
  171. 'HAVE__STRLWR': ('_strlwr', 'string.h'),
  172. 'HAVE_INDEX': ('index', 'strings.h'),
  173. 'HAVE_RINDEX': ('rindex', 'strings.h'),
  174. 'HAVE_STRCHR': ('strchr', 'string.h'),
  175. 'HAVE_STRRCHR': ('strrchr', 'string.h'),
  176. 'HAVE_STRSTR': ('strstr', 'string.h'),
  177. 'HAVE_STRTOL': ('strtol', 'stdlib.h'),
  178. 'HAVE_STRTOUL': ('strtoul', 'stdlib.h'),
  179. 'HAVE_STRTOULL': ('strtoull', 'stdlib.h'),
  180. 'HAVE_STRTOD': ('strtod', 'stdlib.h'),
  181. 'HAVE_ATOI': ('atoi', 'stdlib.h'),
  182. 'HAVE_ATOF': ('atof', 'stdlib.h'),
  183. 'HAVE_STRCMP': ('strcmp', 'string.h'),
  184. 'HAVE_STRNCMP': ('strncmp', 'string.h'),
  185. 'HAVE_VSSCANF': ('vsscanf', 'stdio.h'),
  186. 'HAVE_ATAN': ('atan', 'math.h'),
  187. 'HAVE_ATAN2': ('atan2', 'math.h'),
  188. 'HAVE_ACOS': ('acos', 'math.h'),
  189. 'HAVE_ASIN': ('asin', 'math.h'),
  190. 'HAVE_ASINH': ('asinh', 'math.h'),
  191. 'HAVE_CEIL': ('ceil', 'math.h'),
  192. 'HAVE_COPYSIGN': ('copysign', 'math.h'),
  193. 'HAVE_COS': ('cos', 'math.h'),
  194. 'HAVE_COSH': ('cosh', 'math.h'),
  195. 'HAVE_COSF': ('cosf', 'math.h'),
  196. 'HAVE_FABS': ('fabs', 'math.h'),
  197. 'HAVE_FLOOR': ('floor', 'math.h'),
  198. 'HAVE_ISINF': ('isinf', 'math.h'),
  199. 'HAVE_LOG': ('log', 'math.h'),
  200. 'HAVE_POW': ('pow', 'math.h'),
  201. 'HAVE_SCALBN': ('scalbn', 'math.h'),
  202. 'HAVE_SIN': ('sin', 'math.h'),
  203. 'HAVE_SINF': ('sinf', 'math.h'),
  204. 'HAVE_SINH': ('sinh', 'math.h'),
  205. 'HAVE_SQRT': ('sqrt', 'math.h'),
  206. 'HAVE_FSEEKO': ('fseeko', 'stdio.h'),
  207. 'HAVE_FSEEKO64': ('fseeko64', 'stdio.h'),
  208. 'HAVE_SETJMP': ('setjmp', 'setjmp.h'),
  209. 'HAVE_PTHREAD_SETNAME_NP': ('pthread_setname_np', 'pthread.h'),
  210. 'HAVE_PTHREAD_SET_NAME_NP': ('pthread_set_name_np', 'pthread.h'),
  211. }
  212. headers = []
  213. functions = []
  214. sizes = []
  215. with open(sys.argv[1]) as f:
  216. for line in f:
  217. line = line.strip()
  218. arr = line.split()
  219. # Check for headers.
  220. if line.startswith('#mesondefine') and line.endswith('_H'):
  221. token = line.split()[1]
  222. tarr = token.split('_')[1:-1]
  223. tarr = [x.lower() for x in tarr]
  224. hname = '/'.join(tarr) + '.h'
  225. headers.append((token, hname))
  226. # Check for functions.
  227. try:
  228. token = arr[1]
  229. if token in function_data:
  230. fdata = function_data[token]
  231. functions.append((token, fdata[0], fdata[1]))
  232. elif token.startswith('HAVE_') and not token.endswith('_H'):
  233. functions.append((token, ))
  234. except Exception:
  235. pass
  236. # Check for sizeof tests.
  237. if len(arr) != 2:
  238. continue
  239. elem = arr[1]
  240. if elem.startswith('SIZEOF_'):
  241. typename = elem.split('_', 1)[1] \
  242. .replace('_P', '*') \
  243. .replace('_', ' ') \
  244. .lower() \
  245. .replace('size t', 'size_t')
  246. sizes.append((elem, typename))
  247. print('''cc = meson.get_compiler('c')
  248. cdata = configuration_data()''')
  249. # Convert header checks.
  250. print('check_headers = [')
  251. for token, hname in headers:
  252. print(" ['%s', '%s']," % (token, hname))
  253. print(']\n')
  254. print('''foreach h : check_headers
  255. if cc.has_header(h.get(1))
  256. cdata.set(h.get(0), 1)
  257. endif
  258. endforeach
  259. ''')
  260. # Convert function checks.
  261. print('check_functions = [')
  262. for token in functions:
  263. if len(token) == 3:
  264. token, fdata0, fdata1 = token
  265. print(" ['%s', '%s', '#include<%s>']," % (token, fdata0, fdata1))
  266. else:
  267. print('# check token', token)
  268. print(']\n')
  269. print('''foreach f : check_functions
  270. if cc.has_function(f.get(1), prefix : f.get(2))
  271. cdata.set(f.get(0), 1)
  272. endif
  273. endforeach
  274. ''')
  275. # Convert sizeof checks.
  276. for elem, typename in sizes:
  277. print("cdata.set('%s', cc.sizeof('%s'))" % (elem, typename))
  278. print('''
  279. configure_file(input : 'config.h.meson',
  280. output : 'config.h',
  281. configuration : cdata)''')