specialpaths.nim 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #[
  2. todo: move findNimStdLibCompileTime, findNimStdLib here
  3. xxx: factor pending https://github.com/timotheecour/Nim/issues/616
  4. ## note: $lib vs $nim
  5. note: these can resolve to 3 different paths if running via `nim c --lib:lib foo`,
  6. eg if compiler was installed via nimble (or is in nim path), and nim is external
  7. (ie not in `$lib/../bin/` dir)
  8. import "$lib/../compiler/nimpaths" # <- most robust if you want to favor --lib:lib
  9. import "$nim/compiler/nimpaths"
  10. import compiler/nimpaths
  11. ]#
  12. import os
  13. when defined(nimPreviewSlimSystem):
  14. import std/assertions
  15. # Note: all the const paths defined here are known at compile time and valid
  16. # so long Nim repo isn't relocated after compilation.
  17. # This means the binaries they produce will embed hardcoded paths, which
  18. # isn't appropriate for some applications that need to be relocatable.
  19. const
  20. sourcePath = currentSourcePath()
  21. # robust way to derive other paths here
  22. # We don't depend on PATH so this is robust to having multiple nim binaries
  23. nimRootDir* = sourcePath.parentDir.parentDir.parentDir.parentDir ## root of Nim repo
  24. testsFname* = "tests"
  25. stdlibDir* = nimRootDir / "lib"
  26. systemPath* = stdlibDir / "system.nim"
  27. testsDir* = nimRootDir / testsFname
  28. buildDir* = nimRootDir / "build"
  29. ## refs #10268: all testament generated files should go here to avoid
  30. ## polluting .gitignore
  31. proc splitTestFile*(file: string): tuple[cat: string, path: string] =
  32. ## At least one directory is required in the path, to use as a category name
  33. runnableExamples:
  34. doAssert splitTestFile("tests/fakedir/tfakename.nim") == ("fakedir", "tests/fakedir/tfakename.nim".unixToNativePath)
  35. for p in file.parentDirs(inclusive = false):
  36. let parent = p.parentDir
  37. if parent.lastPathPart == testsFname:
  38. result.cat = p.lastPathPart
  39. let dir = getCurrentDir()
  40. if file.isRelativeTo(dir):
  41. result.path = file.relativePath(dir)
  42. else:
  43. result.path = file
  44. return result
  45. doAssert false, "file must match this pattern: '/pathto/tests/dir/**/tfile.nim', got: '" & file & "'"
  46. static:
  47. # sanity check
  48. doAssert fileExists(systemPath)