Skip to content

Commit e881fb5

Browse files
committed
feat(pyconfig): util.from_c_int_expr,AC_CHECK_FUNC[S]
1 parent 6534a35 commit e881fb5

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

src/pylib/pyconfig/util.nim

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33
import std/os
4-
from std/strutils import parseInt, strip
4+
from std/strutils import parseInt, strip, multiReplace
55
import std/macros
66

77
const weirdTarget = defined(js) or defined(nimscript)
@@ -60,12 +60,34 @@ template from_c_int*(variable; defval: int; precode): int =
6060
let variable{.importc, nodecl.}: cint
6161
stdout.write variable
6262

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+
6369
macro from_c_int_underlined*(variable: static[string]; defval: int): int =
6470
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)
6991

7092
template from_c_int*(variable; includeFile: static[string], defval = low(int)): int =
7193
## we know int.low is smaller than low(cint)
@@ -141,3 +163,30 @@ template c_defined*(variable; c_macro: string; headers: openArray = []) =
141163
);
142164
""".}
143165
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

Comments
 (0)