Skip to content

Commit

Permalink
Merge pull request #51 from SciNim/add_example
Browse files Browse the repository at this point in the history
Add example
  • Loading branch information
Clonkk authored May 29, 2024
2 parents 30ff581 + c9d7a7e commit 97b8440
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
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()

2 changes: 1 addition & 1 deletion nimjl.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Nimjl
# Licensed and distributed under MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
version = "0.8.2"
version = "0.8.3"
author = "Regis Caillaud"
description = "Nim Julia bridge"
license = "MIT"
Expand Down
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 97b8440

Please sign in to comment.