compilation.nim 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. const
  2. NimMajor* {.intdefine.}: int = 1
  3. ## is the major number of Nim's version. Example:
  4. ## ```
  5. ## when (NimMajor, NimMinor, NimPatch) >= (1, 3, 1): discard
  6. ## ```
  7. # see also std/private/since
  8. NimMinor* {.intdefine.}: int = 9
  9. ## is the minor number of Nim's version.
  10. ## Odd for devel, even for releases.
  11. NimPatch* {.intdefine.}: int = 1
  12. ## is the patch number of Nim's version.
  13. ## Odd for devel, even for releases.
  14. {.push profiler: off.}
  15. let nimvm* {.magic: "Nimvm", compileTime.}: bool = false
  16. ## May be used only in `when` expression.
  17. ## It is true in Nim VM context and false otherwise.
  18. {.pop.}
  19. const
  20. isMainModule* {.magic: "IsMainModule".}: bool = false
  21. ## True only when accessed in the main module. This works thanks to
  22. ## compiler magic. It is useful to embed testing code in a module.
  23. CompileDate* {.magic: "CompileDate".}: string = "0000-00-00"
  24. ## The date (in UTC) of compilation as a string of the form
  25. ## `YYYY-MM-DD`. This works thanks to compiler magic.
  26. CompileTime* {.magic: "CompileTime".}: string = "00:00:00"
  27. ## The time (in UTC) of compilation as a string of the form
  28. ## `HH:MM:SS`. This works thanks to compiler magic.
  29. proc defined*(x: untyped): bool {.magic: "Defined", noSideEffect, compileTime.}
  30. ## Special compile-time procedure that checks whether `x` is
  31. ## defined.
  32. ##
  33. ## `x` is an external symbol introduced through the compiler's
  34. ## `-d:x switch <nimc.html#compiler-usage-compileminustime-symbols>`_ to enable
  35. ## build time conditionals:
  36. ## ```
  37. ## when not defined(release):
  38. ## # Do here programmer friendly expensive sanity checks.
  39. ## # Put here the normal code
  40. ## ```
  41. ##
  42. ## See also:
  43. ## * `compileOption <#compileOption,string>`_ for `on|off` options
  44. ## * `compileOption <#compileOption,string,string>`_ for enum options
  45. ## * `define pragmas <manual.html#implementation-specific-pragmas-compileminustime-define-pragmas>`_
  46. proc declared*(x: untyped): bool {.magic: "Declared", noSideEffect, compileTime.}
  47. ## Special compile-time procedure that checks whether `x` is
  48. ## declared. `x` has to be an identifier or a qualified identifier.
  49. ##
  50. ## This can be used to check whether a library provides a certain
  51. ## feature or not:
  52. ## ```
  53. ## when not declared(strutils.toUpper):
  54. ## # provide our own toUpper proc here, because strutils is
  55. ## # missing it.
  56. ## ```
  57. ##
  58. ## See also:
  59. ## * `declaredInScope <#declaredInScope,untyped>`_
  60. proc declaredInScope*(x: untyped): bool {.magic: "DeclaredInScope", noSideEffect, compileTime.}
  61. ## Special compile-time procedure that checks whether `x` is
  62. ## declared in the current scope. `x` has to be an identifier.
  63. proc compiles*(x: untyped): bool {.magic: "Compiles", noSideEffect, compileTime.} =
  64. ## Special compile-time procedure that checks whether `x` can be compiled
  65. ## without any semantic error.
  66. ## This can be used to check whether a type supports some operation:
  67. ## ```
  68. ## when compiles(3 + 4):
  69. ## echo "'+' for integers is available"
  70. ## ```
  71. discard
  72. proc astToStr*[T](x: T): string {.magic: "AstToStr", noSideEffect.}
  73. ## Converts the AST of `x` into a string representation. This is very useful
  74. ## for debugging.
  75. proc runnableExamples*(rdoccmd = "", body: untyped) {.magic: "RunnableExamples".} =
  76. ## A section you should use to mark `runnable example`:idx: code with.
  77. ##
  78. ## - In normal debug and release builds code within
  79. ## a `runnableExamples` section is ignored.
  80. ## - The documentation generator is aware of these examples and considers them
  81. ## part of the `##` doc comment. As the last step of documentation
  82. ## generation each runnableExample is put in its own file `$file_examples$i.nim`,
  83. ## compiled and tested. The collected examples are
  84. ## put into their own module to ensure the examples do not refer to
  85. ## non-exported symbols.
  86. runnableExamples:
  87. proc timesTwo*(x: int): int =
  88. ## This proc doubles a number.
  89. runnableExamples:
  90. # at module scope
  91. const exported* = 123
  92. assert timesTwo(5) == 10
  93. block: # at block scope
  94. defer: echo "done"
  95. runnableExamples "-d:foo -b:cpp":
  96. import std/compilesettings
  97. assert querySetting(backend) == "cpp"
  98. assert defined(foo)
  99. runnableExamples "-r:off": ## this one is only compiled
  100. import std/browsers
  101. openDefaultBrowser "https://forum.nim-lang.org/"
  102. 2 * x
  103. proc compileOption*(option: string): bool {.
  104. magic: "CompileOption", noSideEffect.} =
  105. ## Can be used to determine an `on|off` compile-time option.
  106. ##
  107. ## See also:
  108. ## * `compileOption <#compileOption,string,string>`_ for enum options
  109. ## * `defined <#defined,untyped>`_
  110. ## * `std/compilesettings module <compilesettings.html>`_
  111. runnableExamples("--floatChecks:off"):
  112. static: doAssert not compileOption("floatchecks")
  113. {.push floatChecks: on.}
  114. static: doAssert compileOption("floatchecks")
  115. # floating point NaN and Inf checks enabled in this scope
  116. {.pop.}
  117. proc compileOption*(option, arg: string): bool {.
  118. magic: "CompileOptionArg", noSideEffect.} =
  119. ## Can be used to determine an enum compile-time option.
  120. ##
  121. ## See also:
  122. ## * `compileOption <#compileOption,string>`_ for `on|off` options
  123. ## * `defined <#defined,untyped>`_
  124. ## * `std/compilesettings module <compilesettings.html>`_
  125. runnableExamples:
  126. when compileOption("opt", "size") and compileOption("gc", "boehm"):
  127. discard "compiled with optimization for size and uses Boehm's GC"
  128. template currentSourcePath*: string = instantiationInfo(-1, true).filename
  129. ## Returns the full file-system path of the current source.
  130. ##
  131. ## To get the directory containing the current source, use it with
  132. ## `os.parentDir() <os.html#parentDir%2Cstring>`_ as `currentSourcePath.parentDir()`.
  133. ##
  134. ## The path returned by this template is set at compile time.
  135. ##
  136. ## See the docstring of `macros.getProjectPath() <macros.html#getProjectPath>`_
  137. ## for an example to see the distinction between the `currentSourcePath`
  138. ## and `getProjectPath`.
  139. ##
  140. ## See also:
  141. ## * `getCurrentDir proc <os.html#getCurrentDir>`_
  142. proc slurp*(filename: string): string {.magic: "Slurp".}
  143. ## This is an alias for `staticRead <#staticRead,string>`_.
  144. proc staticRead*(filename: string): string {.magic: "Slurp".}
  145. ## Compile-time `readFile <syncio.html#readFile,string>`_ proc for easy
  146. ## `resource`:idx: embedding:
  147. ##
  148. ## The maximum file size limit that `staticRead` and `slurp` can read is
  149. ## near or equal to the *free* memory of the device you are using to compile.
  150. ## ```
  151. ## const myResource = staticRead"mydatafile.bin"
  152. ## ```
  153. ##
  154. ## `slurp <#slurp,string>`_ is an alias for `staticRead`.
  155. proc gorge*(command: string, input = "", cache = ""): string {.
  156. magic: "StaticExec".} = discard
  157. ## This is an alias for `staticExec <#staticExec,string,string,string>`_.
  158. proc staticExec*(command: string, input = "", cache = ""): string {.
  159. magic: "StaticExec".} = discard
  160. ## Executes an external process at compile-time and returns its text output
  161. ## (stdout + stderr).
  162. ##
  163. ## If `input` is not an empty string, it will be passed as a standard input
  164. ## to the executed program.
  165. ## ```
  166. ## const buildInfo = "Revision " & staticExec("git rev-parse HEAD") &
  167. ## "\nCompiled on " & staticExec("uname -v")
  168. ## ```
  169. ##
  170. ## `gorge <#gorge,string,string,string>`_ is an alias for `staticExec`.
  171. ##
  172. ## Note that you can use this proc inside a pragma like
  173. ## `passc <manual.html#implementation-specific-pragmas-passc-pragma>`_ or
  174. ## `passl <manual.html#implementation-specific-pragmas-passl-pragma>`_.
  175. ##
  176. ## If `cache` is not empty, the results of `staticExec` are cached within
  177. ## the `nimcache` directory. Use `--forceBuild` to get rid of this caching
  178. ## behaviour then. `command & input & cache` (the concatenated string) is
  179. ## used to determine whether the entry in the cache is still valid. You can
  180. ## use versioning information for `cache`:
  181. ## ```
  182. ## const stateMachine = staticExec("dfaoptimizer", "input", "0.8.0")
  183. ## ```
  184. proc gorgeEx*(command: string, input = "", cache = ""): tuple[output: string,
  185. exitCode: int] =
  186. ## Similar to `gorge <#gorge,string,string,string>`_ but also returns the
  187. ## precious exit code.
  188. discard