nimhcr_integration.nim 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. discard """
  2. disabled: "true"
  3. output: '''
  4. main: HELLO!
  5. main: hasAnyModuleChanged? true
  6. main: before
  7. 0: after
  8. main: after
  9. The answer is: 1000
  10. main: hasAnyModuleChanged? false
  11. The answer is: 1000
  12. main: hasAnyModuleChanged? true
  13. 0: before
  14. main: before
  15. 1: print me once!
  16. 1: 1
  17. 1: 2
  18. 1: 3
  19. 1: 4
  20. 1: 5
  21. 1: 5
  22. 1: 5
  23. 1: Type1.a:42
  24. 1
  25. bar
  26. 0: after - improved!
  27. main: after
  28. The answer is: 110
  29. main: hasAnyModuleChanged? true
  30. 0: before - improved!
  31. main: before
  32. 2: random string
  33. max mutual recursion reached!
  34. 1
  35. bar
  36. 0: after - closure iterator: 0
  37. 0: after - closure iterator: 1
  38. 0: after - c_2 = [1, 2, 3]
  39. main: after
  40. The answer is: 9
  41. main: hasAnyModuleChanged? true
  42. 2: before!
  43. main: before
  44. 2: after!
  45. 0: after - closure iterator! after reload! does it remember? :2
  46. 0: after - closure iterator! after reload! does it remember? :3
  47. main: after
  48. The answer is: 1000
  49. main: hasAnyModuleChanged? true
  50. main: before
  51. main: after
  52. The answer is: 42
  53. done
  54. '''
  55. """
  56. #[
  57. xxx disabled: "openbsd" because it would otherwise give:
  58. /home/build/Nim/lib/nimhcr.nim(532) hcrInit
  59. /home/build/Nim/lib/nimhcr.nim(503) initModules
  60. /home/build/Nim/lib/nimhcr.nim(463) initPointerData
  61. /home/build/Nim/lib/nimhcr.nim(346) hcrRegisterProc
  62. /home/build/Nim/lib/pure/reservedmem.nim(223) setLen
  63. /home/build/Nim/lib/pure/reservedmem.nim(97) setLen
  64. /home/build/Nim/lib/pure/includes/oserr.nim(94) raiseOSError
  65. Error: unhandled exception: Not supported [OSError]
  66. After instrumenting code, the stacktrace actually points to the call to `check mprotect`
  67. ]#
  68. ## This is perhaps the most complex test in the nim test suite - calling the
  69. ## compiler on the file itself with the same set or arguments and reloading
  70. ## parts of the program at runtime! In the same folder there are a few modules
  71. ## with names such as `nimhcr_<number>.nim`. Each of them has a few versions which
  72. ## are in the format of `nimhcr_<number>_<version>.nim`. The below code uses the
  73. ## `update` proc to say which of the modules should bump its version (and that
  74. ## is done by copying `nimhcr_<number>_<version>.nim` onto `nimhcr_<number>.nim`).
  75. ## The files should refer to each other (when importing) without the versions.
  76. ## A few files can be updated by calling `update` for each of their indexes
  77. ## and after that with a single call to `compileReloadExecute` the new version
  78. ## of the program will be compiled, reloaded, and the only thing the main module
  79. ## calls from `nimhcr_0.nim` (the procedure `getInt` proc) is called for a result.
  80. ##
  81. ## This test is expected to be executed with arguments - the full nim compiler
  82. ## command used for building it - so it can rebuild iself the same way - example:
  83. ##
  84. ## compiling:
  85. ## nim c --hotCodeReloading:on --nimCache:<folder> <this_file>.nim
  86. ## executing:
  87. ## <this_file>.exe nim c --hotCodeReloading:on --nimCache:<folder> <this_file>.nim
  88. import os, osproc, strutils, hotcodereloading
  89. import nimhcr_0 # getInt() - the only thing we continually call from the main module
  90. proc compileReloadExecute() =
  91. # Remove the `--forceBuild` option - is there in the first place because:
  92. # - when `koch test` is ran for the first time the nimcache is empty
  93. # - when each of the variants are built (debug, release after that, different GCs)
  94. # the main executable that gets built into the appropriate nimcache folder
  95. # gets copied to the originally intended destination and is executed
  96. # (this behaviour is only when the --hotCodeReloading option is used).
  97. # - when `koch test` is ran again and the nimcache is full the executable files
  98. # in the nimcache folder aren't relinked and therefore aren't copied to the
  99. # originally intended destination - so when the binary at the intended
  100. # destination is executed - it is actually a remnant from a previous execution.
  101. # That is a problem because it points to shared objects to load from its own
  102. # nimcache folder - the one used for building it - a previous run! And when
  103. # this test changes other modules it references but the main module (this file)
  104. # remains intact - the binary isn't replaced. `--forceBuild` fixes this but has
  105. # to be applied only for the main build - the one done from koch, but when this
  106. # binary triggers rebuilding itself here it shouldn't rebuild the main module -
  107. # that would lead to replacing the main binary executable which is running!
  108. let cmd = commandLineParams()[0..^1].join(" ").replace(" --forceBuild")
  109. doAssert cmd.len > 0
  110. let (stdout, exitcode) = execCmdEx(cmd)
  111. if exitcode != 0:
  112. echo "COMPILATION ERROR!"
  113. echo "COMMAND: ", cmd
  114. echo "STDOUT: ", stdout
  115. quit 1
  116. echo "main: hasAnyModuleChanged? ", hasAnyModuleChanged()
  117. performCodeReload()
  118. echo " The answer is: ", getInt()
  119. # there are 3 files and all of them start from their 1st version
  120. var vers = [1, 1, 1]
  121. proc update(file: int) =
  122. proc getfile(mid: string): string =
  123. let (path, _, _) = splitFile(currentSourcePath())
  124. return path & "/nimhcr_" & mid & ".nim"
  125. copyFile(getfile($file & "_" & $vers[file]), getfile($file))
  126. inc vers[file]
  127. beforeCodeReload:
  128. echo "main: before"
  129. afterCodeReload:
  130. echo "main: after"
  131. echo "main: HELLO!"
  132. update 0
  133. compileReloadExecute() # versions are: 1 - -
  134. compileReloadExecute() # no change
  135. update 0
  136. update 1
  137. compileReloadExecute() # versions are: 2 1 -
  138. update 0
  139. update 2
  140. compileReloadExecute() # versions are: 3 1 1
  141. update 0
  142. update 1
  143. update 2
  144. compileReloadExecute() # versions are: 4 2 2
  145. update 0
  146. compileReloadExecute() # versions are: 5 2 2
  147. # final update so there are no git modifications left after everything
  148. # (the last versions are like the first files without a version suffix)
  149. update 0
  150. update 1
  151. update 2
  152. echo "done"