compilation.nim 8.6 KB

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