Skip to content

Commit

Permalink
Add example of advanced init + few comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Clonkk committed May 28, 2024
1 parent 30ff581 commit f5a37a9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/ex11_external_deps.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ proc main() =
add(name="LinearAlgebra")
add("DSP")

defer: Julia.exit()
Julia.useModule("Pkg")
let jlpkg = Julia.getModule("Pkg")
discard jlpkg.status()

Julia.exit()

when isMainModule:
main()
80 changes: 80 additions & 0 deletions examples/ex12_advanced_init.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import nimjl
import std/[os, strformat, exitprocs]

proc compileCurrentFile(curRun: static int) =
let cmd = getCurrentCompilerExe() & &" cpp -d:run{curRun} -o:./run{curRun}.out " & currentSourcePath()
debugEcho cmd
discard execShellCmd(cmd)

proc cleanUp() =
var envdir = "./julia-custom-env"
if dirExists(envdir): removeDir(envdir)

envdir = "./julia-empty-env"
if dirExists(envdir): removeDir(envdir)

discard tryRemoveFile("run1.out")
discard tryRemoveFile("run2.out")
discard tryRemoveFile("run3.out")


proc postJlInit() =
echo "code executed after Julia VM is initialized and env is activated but before dependencies"
let Pkg = Julia.getModule("Pkg")

discard Pkg.update()
discard Pkg.status()

proc firstRunmain() =
## See https://pkgdocs.julialang.org/dev/api/#Pkg.add for more info
Julia.init(4):
activate("./julia-custom-env")
# Call postJlInit() if it exists
# Call Pkg + external dependencies
Pkg:
add(name="LinearAlgebra")
add("DSP")
defer: Julia.exit()

proc mainNoPkgSameEnv() =
## See https://pkgdocs.julialang.org/dev/api/#Pkg.add for more info
Julia.init(4):
# activate julia venv
activate("./julia-custom-env")
defer: Julia.exit()

proc mainNoPkgDifferentEnv() =
## See https://pkgdocs.julialang.org/dev/api/#Pkg.add for more info
Julia.init(4):
activate("./julia-empty-env")
defer: Julia.exit()

when isMainModule:
when defined(run1):
echo "First Run:"
echo " * Pkg.status show empty deps"
echo " * dependencies are downloaded"
echo "==================================================="
firstRunMain()
echo "==================================================="
elif defined(run2):
echo "No Pkg in init but same env:"
echo " * Pkg.status show deps in projects"
echo "==================================================="
mainNoPkgSameEnv()
echo "==================================================="
elif defined(run3):
echo "No Pkg in init and different env:"
echo " * Pkg.status show empty deps"
echo "==================================================="
mainNoPkgDifferentEnv()
echo "==================================================="
else:
compileCurrentFile(curRun=1)
compileCurrentFile(curRun=2)
compileCurrentFile(curRun=3)
discard execShellCmd("./run1.out")
discard execShellCmd("./run2.out")
discard execShellCmd("./run3.out")
cleanUp()

1 change: 1 addition & 0 deletions nimjl/glucose.nim
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ template init*(jl: type Julia, nthreads: int, body: untyped) =
packages = jl_pkg_private_scope

template activate(env: string) {.used.} =
## Activate a Julia virtual env
pkgEnv = string(expandTilde(Path(env)))

template Embed(innerbody: untyped) {.used.} =
Expand Down

0 comments on commit f5a37a9

Please sign in to comment.