|
1 | 1 |
|
2 | 2 |
|
3 | 3 | import std/os
|
4 |
| -from std/strutils import parseInt, strip |
| 4 | +from std/strutils import parseInt, strip, multiReplace |
5 | 5 | import std/macros
|
6 | 6 |
|
7 | 7 | const weirdTarget = defined(js) or defined(nimscript)
|
@@ -60,12 +60,34 @@ template from_c_int*(variable; defval: int; precode): int =
|
60 | 60 | let variable{.importc, nodecl.}: cint
|
61 | 61 | stdout.write variable
|
62 | 62 |
|
| 63 | +proc from_c_int_expr(cacheName, cexpr: string; defval: NimNode): NimNode = |
| 64 | + let pureVarId = newLit cacheName |
| 65 | + result = quote do: |
| 66 | + from_c_int(`cexpr`, `defval`): |
| 67 | + {.emit: ["/*VARSECTION*/\n#define ", `pureVarId`, " ", `cexpr`, '\n'].} |
| 68 | + |
63 | 69 | macro from_c_int_underlined*(variable: static[string]; defval: int): int =
|
64 | 70 | let pureVar = variable.strip(chars = {'_'})
|
65 |
| - let pureVarId = newLit pureVar |
66 |
| - result = quote do: |
67 |
| - from_c_int(`variable`, `defval`): |
68 |
| - {.emit: ["/*VARSECTION*/\n#define ", `pureVarId`, " ", `variable`].} |
| 71 | + from_c_int_expr(pureVar, variable, defval) |
| 72 | + |
| 73 | +macro from_c_int_expr*(cExpr: static[string]; defval: int): int = |
| 74 | + let pureVar = cExpr.multiReplace( |
| 75 | + ("_", ""), |
| 76 | + ("(", "%28"), |
| 77 | + (")", "%29"), |
| 78 | + (" ", "%20"), |
| 79 | + (",", "%2C"), |
| 80 | + ("<", "%3C"), |
| 81 | + (">", "%3E"), |
| 82 | + (":", "%3A"), |
| 83 | + ("\"", "%22"), |
| 84 | + ("/", "%2F"), |
| 85 | + ("\\", "%5C"), |
| 86 | + ("|", "%7C"), |
| 87 | + ("?", "%3F"), |
| 88 | + ("*", "%2A"), |
| 89 | + ) |
| 90 | + from_c_int_expr(pureVar, cExpr, defval) |
69 | 91 |
|
70 | 92 | template from_c_int*(variable; includeFile: static[string], defval = low(int)): int =
|
71 | 93 | ## we know int.low is smaller than low(cint)
|
@@ -141,3 +163,30 @@ template c_defined*(variable; c_macro: string; headers: openArray = []) =
|
141 | 163 | );
|
142 | 164 | """.}
|
143 | 165 | main()
|
| 166 | + |
| 167 | +template AC_CHECK_FUNC*(res, function) = |
| 168 | + ## export const HAVE_`function` |
| 169 | + AC_LINK_IFELSE res, false: |
| 170 | + {.emit: [ |
| 171 | + "/*INCLUDESECTION*/\n", |
| 172 | + "#undef " & astToStr(function) & '\n', |
| 173 | + """/* The GNU C library defines this for functions which it implements |
| 174 | + to always fail with ENOSYS. Some functions are actually named |
| 175 | + something starting with __ and the normal name is an alias. */ |
| 176 | +#if defined __stub_`function` || defined __stub___`function` |
| 177 | +choke me |
| 178 | +#endif |
| 179 | +""" |
| 180 | + ].} #""" <- for code lint |
| 181 | + proc function(): cchar{.importc, header: "<limits.h>".} |
| 182 | + discard function() |
| 183 | + |
| 184 | +template AC_CHECK_FUNC*(function) = |
| 185 | + AC_CHECK_FUNC(`HAVE function`, function) |
| 186 | + |
| 187 | + |
| 188 | +macro AC_CHECK_FUNCS*(functions: varargs[untyped]): untyped = |
| 189 | + result = newStmtList() |
| 190 | + for fun in functions: |
| 191 | + result.add quote do: |
| 192 | + AC_CHECK_FUNC(`fun`) |
0 commit comments