specialpaths.nim 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. result = ("", "")
  36. for p in file.parentDirs(inclusive = false):
  37. let parent = p.parentDir
  38. if parent.lastPathPart == testsFname:
  39. result.cat = p.lastPathPart
  40. let dir = getCurrentDir()
  41. if file.isRelativeTo(dir):
  42. result.path = file.relativePath(dir)
  43. else:
  44. result.path = file
  45. return result
  46. raiseAssert "file must match this pattern: '/pathto/tests/dir/**/tfile.nim', got: '" & file & "'"
  47. static:
  48. # sanity check
  49. doAssert fileExists(systemPath)