nim.nim 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. when defined(gcc) and defined(windows):
  10. when defined(x86):
  11. {.link: "icons/nim.res".}
  12. else:
  13. {.link: "icons/nim_icon.o".}
  14. when defined(amd64) and defined(windows) and defined(vcc):
  15. {.link: "icons/nim-amd64-windows-vcc.res".}
  16. when defined(i386) and defined(windows) and defined(vcc):
  17. {.link: "icons/nim-i386-windows-vcc.res".}
  18. import
  19. commands, lexer, condsyms, options, msgs, nversion, nimconf, ropes,
  20. extccomp, strutils, os, osproc, platform, main, parseopt, service,
  21. nodejs, scriptconfig, idents, modulegraphs
  22. when hasTinyCBackend:
  23. import tccgen
  24. when defined(profiler) or defined(memProfiler):
  25. {.hint: "Profiling support is turned on!".}
  26. import nimprof
  27. proc prependCurDir(f: string): string =
  28. when defined(unix):
  29. if os.isAbsolute(f): result = f
  30. else: result = "./" & f
  31. else:
  32. result = f
  33. proc handleCmdLine(cache: IdentCache; config: ConfigRef) =
  34. if paramCount() == 0:
  35. writeCommandLineUsage()
  36. else:
  37. # Process command line arguments:
  38. processCmdLine(passCmd1, "")
  39. if gProjectName == "-":
  40. gProjectName = "stdinfile"
  41. gProjectFull = "stdinfile"
  42. gProjectPath = canonicalizePath getCurrentDir()
  43. gProjectIsStdin = true
  44. elif gProjectName != "":
  45. try:
  46. gProjectFull = canonicalizePath(gProjectName)
  47. except OSError:
  48. gProjectFull = gProjectName
  49. let p = splitFile(gProjectFull)
  50. let dir = if p.dir.len > 0: p.dir else: getCurrentDir()
  51. gProjectPath = canonicalizePath dir
  52. gProjectName = p.name
  53. else:
  54. gProjectPath = canonicalizePath getCurrentDir()
  55. loadConfigs(DefaultConfig, config) # load all config files
  56. let scriptFile = gProjectFull.changeFileExt("nims")
  57. if fileExists(scriptFile):
  58. runNimScript(cache, scriptFile, freshDefines=false, config)
  59. # 'nim foo.nims' means to just run the NimScript file and do nothing more:
  60. if scriptFile == gProjectFull: return
  61. elif fileExists(gProjectPath / "config.nims"):
  62. # directory wide NimScript file
  63. runNimScript(cache, gProjectPath / "config.nims", freshDefines=false, config)
  64. # now process command line arguments again, because some options in the
  65. # command line can overwite the config file's settings
  66. extccomp.initVars()
  67. processCmdLine(passCmd2, "")
  68. if options.command == "":
  69. rawMessage(errNoCommand, command)
  70. mainCommand(newModuleGraph(config), cache)
  71. if optHints in gOptions and hintGCStats in gNotes: echo(GC_getStatistics())
  72. #echo(GC_getStatistics())
  73. if msgs.gErrorCounter == 0:
  74. when hasTinyCBackend:
  75. if gCmd == cmdRun:
  76. tccgen.run(commands.arguments)
  77. if optRun in gGlobalOptions:
  78. if gCmd == cmdCompileToJS:
  79. var ex: string
  80. if options.outFile.len > 0:
  81. ex = options.outFile.prependCurDir.quoteShell
  82. else:
  83. ex = quoteShell(
  84. completeCFilePath(changeFileExt(gProjectFull, "js").prependCurDir))
  85. execExternalProgram(findNodeJs() & " " & ex & ' ' & commands.arguments)
  86. elif gCmd == cmdCompileToPHP:
  87. var ex: string
  88. if options.outFile.len > 0:
  89. ex = options.outFile.prependCurDir.quoteShell
  90. else:
  91. ex = quoteShell(
  92. completeCFilePath(changeFileExt(gProjectFull, "php").prependCurDir))
  93. execExternalProgram("php " & ex & ' ' & commands.arguments)
  94. else:
  95. var binPath: string
  96. if options.outFile.len > 0:
  97. # If the user specified an outFile path, use that directly.
  98. binPath = options.outFile.prependCurDir
  99. else:
  100. # Figure out ourselves a valid binary name.
  101. binPath = changeFileExt(gProjectFull, ExeExt).prependCurDir
  102. var ex = quoteShell(binPath)
  103. execExternalProgram(ex & ' ' & commands.arguments)
  104. when declared(GC_setMaxPause):
  105. GC_setMaxPause 2_000
  106. when compileOption("gc", "v2") or compileOption("gc", "refc"):
  107. # the new correct mark&sweet collector is too slow :-/
  108. GC_disableMarkAndSweep()
  109. condsyms.initDefines()
  110. when not defined(selftest):
  111. handleCmdLine(newIdentCache(), newConfigRef())
  112. when declared(GC_setMaxPause):
  113. echo GC_getStatistics()
  114. msgQuit(int8(msgs.gErrorCounter > 0))