Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example #51

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading