nimhcr_integration.nim 5.2 KB

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