Skip to content

Commit 97b8440

Browse files
authored
Merge pull request #51 from SciNim/add_example
Add example
2 parents 30ff581 + c9d7a7e commit 97b8440

4 files changed

Lines changed: 83 additions & 2 deletions

File tree

examples/ex11_external_deps.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ proc main() =
88
add(name="LinearAlgebra")
99
add("DSP")
1010

11+
defer: Julia.exit()
1112
Julia.useModule("Pkg")
1213
let jlpkg = Julia.getModule("Pkg")
1314
discard jlpkg.status()
1415

15-
Julia.exit()
1616

1717
when isMainModule:
1818
main()

examples/ex12_advanced_init.nim

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import nimjl
2+
import std/[os, strformat, exitprocs]
3+
4+
proc compileCurrentFile(curRun: static int) =
5+
let cmd = getCurrentCompilerExe() & &" cpp -d:run{curRun} -o:./run{curRun}.out " & currentSourcePath()
6+
debugEcho cmd
7+
discard execShellCmd(cmd)
8+
9+
proc cleanUp() =
10+
var envdir = "./julia-custom-env"
11+
if dirExists(envdir): removeDir(envdir)
12+
13+
envdir = "./julia-empty-env"
14+
if dirExists(envdir): removeDir(envdir)
15+
16+
discard tryRemoveFile("run1.out")
17+
discard tryRemoveFile("run2.out")
18+
discard tryRemoveFile("run3.out")
19+
20+
21+
proc postJlInit() =
22+
echo "code executed after Julia VM is initialized and env is activated but before dependencies"
23+
let Pkg = Julia.getModule("Pkg")
24+
25+
discard Pkg.update()
26+
discard Pkg.status()
27+
28+
proc firstRunmain() =
29+
## See https://pkgdocs.julialang.org/dev/api/#Pkg.add for more info
30+
Julia.init(4):
31+
activate("./julia-custom-env")
32+
# Call postJlInit() if it exists
33+
# Call Pkg + external dependencies
34+
Pkg:
35+
add(name="LinearAlgebra")
36+
add("DSP")
37+
defer: Julia.exit()
38+
39+
proc mainNoPkgSameEnv() =
40+
## See https://pkgdocs.julialang.org/dev/api/#Pkg.add for more info
41+
Julia.init(4):
42+
# activate julia venv
43+
activate("./julia-custom-env")
44+
defer: Julia.exit()
45+
46+
proc mainNoPkgDifferentEnv() =
47+
## See https://pkgdocs.julialang.org/dev/api/#Pkg.add for more info
48+
Julia.init(4):
49+
activate("./julia-empty-env")
50+
defer: Julia.exit()
51+
52+
when isMainModule:
53+
when defined(run1):
54+
echo "First Run:"
55+
echo " * Pkg.status show empty deps"
56+
echo " * dependencies are downloaded"
57+
echo "==================================================="
58+
firstRunMain()
59+
echo "==================================================="
60+
elif defined(run2):
61+
echo "No Pkg in init but same env:"
62+
echo " * Pkg.status show deps in projects"
63+
echo "==================================================="
64+
mainNoPkgSameEnv()
65+
echo "==================================================="
66+
elif defined(run3):
67+
echo "No Pkg in init and different env:"
68+
echo " * Pkg.status show empty deps"
69+
echo "==================================================="
70+
mainNoPkgDifferentEnv()
71+
echo "==================================================="
72+
else:
73+
compileCurrentFile(curRun=1)
74+
compileCurrentFile(curRun=2)
75+
compileCurrentFile(curRun=3)
76+
discard execShellCmd("./run1.out")
77+
discard execShellCmd("./run2.out")
78+
discard execShellCmd("./run3.out")
79+
cleanUp()
80+

nimjl.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Nimjl
22
# Licensed and distributed under MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
3-
version = "0.8.2"
3+
version = "0.8.3"
44
author = "Regis Caillaud"
55
description = "Nim Julia bridge"
66
license = "MIT"

nimjl/glucose.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ template init*(jl: type Julia, nthreads: int, body: untyped) =
134134
packages = jl_pkg_private_scope
135135

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

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

0 commit comments

Comments
 (0)