nimhcr_integration.nim 5.1 KB

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