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

os./ on compiletime + crossCompilation behaves incorrectly. #21534

Open
enthus1ast opened this issue Mar 16, 2023 · 3 comments
Open

os./ on compiletime + crossCompilation behaves incorrectly. #21534

enthus1ast opened this issue Mar 16, 2023 · 3 comments

Comments

@enthus1ast
Copy link
Contributor

Description

import macros
static:
  echo getProjectPath()
  echo getProjectPath() / "foo.nim"

Then cross compile from linux for windows:

nim c -f -d:mingw bug.nim

The output:

/root/bug/src/modules2
\root\bug\src\modules2\foo.nim

I expect the second line to be:

/root/bug/src/modules2/foo.nim

Nim Version

Nim Compiler Version 1.9.1 [Linux: amd64]
Compiled at 2023-03-14
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: 26b7a74
active boot switches: -d:release

Current Output

/root/bug/src/modules2
\root\bug\src\modules2\foo.nim

Expected Output

/root/bug/src/modules2
/root/bug/src/modules2/foo.nim

Possible Solution

Fix os./ for compile time.

Additional Information

No response

@demotomohiro
Copy link
Contributor

A path created at compile time can be used at both compile time and run time.
It can be used for staticRead at compile time.
It can be used for reading file in fixed path at run time.

I think explicity specifying which OS the path used is better.
For example,

type
  PathOS = enum
    poUnix
    poWindows

  Path[P: PathOS] = distinct string

when BuilOS == "windows":
  BuildOSPath = Path[poWindows]
else:
  BuildOSPath = Path[poUnix]

when TargetOS == "windows":
  TargetOSPath = Path[poWindows]
else:
  TargetOSPath = Path[poUnix]

static:
  # Suppose cross compiling from linux for windows:
  # echo unix path
  echo getProjectPath().BuildOS / "foo.nim"

  # echo windows path
  echo getProjectPath().TargetOS / "foo.nim"

Or add a proc explicity convert path:

type
  PathOS = enum
    poUnix
    poWindows

when BuilOS == "windows":
  const BuildOSPath = poWindows
else:
  const BuildOSPath = poUnix

when TargetOS == "windows":
  const TargetOSPath = poWindows
else:
  const TargetOSPath = poUnix

proc convertPath(to: PathOS): string

static:
  # Suppose cross compiling from linux for windows:
  # echo unix path
  echo (getProjectPath() / "foo.nim").convertPath(BuilOSPath)

  # echo windows path
  echo (getProjectPath() / "foo.nim").convertPath(TargetOSPath)

@demotomohiro
Copy link
Contributor

Related issue:#19558

@enthus1ast
Copy link
Contributor Author

Thank you @demotomohiro

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

No branches or pull requests

2 participants