Skip to content
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

Make fileExists and dirExists in os module available at compile time even when --os:any #22203

Closed
wants to merge 1 commit into from

Conversation

demotomohiro
Copy link
Contributor

This PR partly fix #19414.
With this PR, fileExists and dirExists works at compile time by importing std/private/oscommon even when --os:any option is set.

For example,
testosmod.nim:

import std/private/oscommon

static:
  doAssert fileExists(currentSourcePath())
  doAssert dirExists("..")
nim c --os:any -d:useMalloc testosmod.nim

By changing code in the same way as this PR, you will be able to import os module and procs and iterators in os module that can be used at compile time become available even when --os:any.
But changing code in this way is OK?

If there is fileExists or dirExists proc that is called at runtime when --os:any or target is unsupported OS, it should be a compile error.
But how to do so?

I defined following supportCompileTime pragma and added it to fileExists and dirExists procs so that they have compileTime pragma on unsupported OS.
But calling them at running doesn't cause compile error.

when supportedOS:
  {.pragma: supportCompileTime.}
else:
  # On unsupported OS, you can call the proc at compile but at runtime.
  {.pragma: supportCompileTime, compileTime.}

And cannot forbid using walkDir iterator at runtime with compileTime pragma because adding compileTime pragama to iterators is a compile error.

@Araq
Copy link
Member

Araq commented Jul 1, 2023

What about new procs staticFileExists and staticDirExists that are available at compile-time, always?

@demotomohiro
Copy link
Contributor Author

I'm ok with that idea.
Then, staticFileExists and staticDirExists should be defined in oscommon module like fileExists or dirExists?
Or create a new module like 'staticos' and define them in that module?

Currently, fileExists, dirExists and walkDir can be used at compile time unless you do cross compiling.
People might write large code that uses these procs/iterator at compile-time.
Then, that code is compiled with --os:any and get compile error and need to replace all fileExists with staticFileExists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Importing os module for use in macros fails when compiling for platform that doesn't support os
2 participants