-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
std/platformutils
: platform reflection at CT: test whether platform has some feature (C apis, intrinsics, include files, etc)
#414
Comments
|
I support this, provided the module is created as an external module first, and then later evaluated for inclusion into the standard library. |
it needs compiler support to implement the vmops, that's the whole point, so it can be available at CT; I don't see how an external package would work. |
Then perhaps some thought should be given as to how the compiler can support this kind of functionality for external modules. How would this work for cross-compilation? Even with access to header files for another system, it's not (usually) possible to test at compile-time whether a target system supports certain intrinsics. The best you can do is generate versions of a function that are specialized for certain target systems, and dispatch to them at runtime. |
Ref nim-lang/Nim#17059 (which is already reverted) |
that's precisely what
cross-compilation is always going to be trickier as assumptions on target platform must be specified somehow;
|
proposal
Add a low-level (usable in other low-level modules) module
std/backendutils
with APIs to test whether platform has certain features (eg RDTSC, __builtin_saddll_overflow, C include files/libraries etc). The API is implemented in the compiler by executing shell commands, typically by compiling and/or running C code and checking exit code or stdout/stdin or other means, depending on the use case.This provides similar functionality to what other build tools provide, eg
cmake
which runs tests like this: https://github.com/libjpeg-turbo/libjpeg-turbo/blob/main/CMakeLists.txt producing those results: https://gist.github.com/timotheecour/8e11b7617ac332ba59f44ed829f7dc72backendCompiles
example 1:
avoid having to guess platforms on which to pass
-d:nimEmulateOverflowChecks
(and associated logic which spills over to nimbase.h via NIM_EmulateOverflowChecks)instead, we'd write this code:
example 2:
likewise, would simplify and make the detection logic more robust in bitops, and avoid the need for flags like
-d:noIntrinsicsBitOpts
example 3:
check presence of RDTSC for std/cputicks (#411)
example 4:
for #399 std/int128s
other APIs
other APIs can be added in std/platformutils eg:
__STDC_VERSION__
, etcimplementation
backendCompiles
is implemented as a vmops which executes a shell command which compiles the provided code snippet and checks exit status, reusing the logic fromextccomp.nim
, in particular using same backend flags as would be used for compiling the main projects file.This in particular is what makes this feature worth having implemented in compiler rather that letting user code call
staticExec
which would make it more difficult to pass the same backend flags or having to take care about caching, temporary file creation etc (the config logic shouldn't have to be duplicated by user).caching
passc
), and invalidate it when--forceBuild
is passed. That said, compiling tiny C programs should not incur much overhead anyways.The text was updated successfully, but these errors were encountered: