summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSaikari <[email protected]>2026-01-27 13:47:50 +0300
committerSaikari <[email protected]>2026-01-27 13:47:50 +0300
commitd0b381b22c7b6e75a607c5a61203b3f7ce7c9bdb (patch)
tree06f9bacdf61c15ffcd8d808f6569174dfc7d8ced
parenta217ee2dc800dc9d3a022003f493c01fdebe745c (diff)
refactor: update runpath function and add support for multiple platforms; add Nim test files for static and shared libraries
-rw-r--r--tests/projects/nim/link_library/maindll.nim4
-rw-r--r--tests/projects/nim/link_library/mainlib.nim4
-rw-r--r--tests/projects/nim/link_library/shared.nim11
-rw-r--r--tests/projects/nim/link_library/static.nim6
-rw-r--r--tests/projects/nim/link_library/xmake.lua20
-rw-r--r--xmake/modules/private/action/run/runenvs.lua24
6 files changed, 63 insertions, 6 deletions
diff --git a/tests/projects/nim/link_library/maindll.nim b/tests/projects/nim/link_library/maindll.nim
new file mode 100644
index 000000000..0cd97df01
--- /dev/null
+++ b/tests/projects/nim/link_library/maindll.nim
@@ -0,0 +1,4 @@
+import shared
+
+echo "Calling shared lib mulTwo(10): ", mulTwo(10)
+echo "Calling shared lib countWords('hello, world, hello'): ", countWords("hello, world, hello")
diff --git a/tests/projects/nim/link_library/mainlib.nim b/tests/projects/nim/link_library/mainlib.nim
new file mode 100644
index 000000000..a99c54b41
--- /dev/null
+++ b/tests/projects/nim/link_library/mainlib.nim
@@ -0,0 +1,4 @@
+import static
+
+echo "Calling static lib addTwo(10): ", addTwo(10)
+echo "Calling static lib getAlphabet(): ", getAlphabet()
diff --git a/tests/projects/nim/link_library/shared.nim b/tests/projects/nim/link_library/shared.nim
new file mode 100644
index 000000000..60e02b15e
--- /dev/null
+++ b/tests/projects/nim/link_library/shared.nim
@@ -0,0 +1,11 @@
+proc mulTwo*(x: int): int =
+ return x * 2
+
+import tables, strutils
+
+proc countWords*(input: string): string =
+ var wordFrequencies = initCountTable[string]()
+ for word in input.split(", "):
+ wordFrequencies.inc(word)
+ return "The most frequent word is '" & $wordFrequencies.largest & "'"
+
diff --git a/tests/projects/nim/link_library/static.nim b/tests/projects/nim/link_library/static.nim
new file mode 100644
index 000000000..bd8847543
--- /dev/null
+++ b/tests/projects/nim/link_library/static.nim
@@ -0,0 +1,6 @@
+proc addTwo*(x: int): int =
+ return x + 2
+
+proc getAlphabet*(): string =
+ for letter in 'a'..'z':
+ result.add(letter)
diff --git a/tests/projects/nim/link_library/xmake.lua b/tests/projects/nim/link_library/xmake.lua
new file mode 100644
index 000000000..05512a7cc
--- /dev/null
+++ b/tests/projects/nim/link_library/xmake.lua
@@ -0,0 +1,20 @@
+set_project("link_libs")
+add_rules("mode.debug", "mode.release")
+
+target("executablestatic")
+ set_kind("binary")
+ add_files("mainlib.nim")
+ add_deps("staticlib")
+
+target("executableshared")
+ set_kind("binary")
+ add_files("maindll.nim")
+ add_deps("sharedlib")
+
+target("staticlib")
+ set_kind("static")
+ add_files("static.nim")
+
+target("sharedlib")
+ set_kind("shared")
+ add_files("shared.nim")
diff --git a/xmake/modules/private/action/run/runenvs.lua b/xmake/modules/private/action/run/runenvs.lua
index 43d150e66..7aa97a018 100644
--- a/xmake/modules/private/action/run/runenvs.lua
+++ b/xmake/modules/private/action/run/runenvs.lua
@@ -21,8 +21,8 @@
-- imports
import("core.base.hashset")
--- add search directories for all dependent shared libraries on windows
-function _make_runpath_on_windows(target)
+-- add search directories for all dependent shared libraries
+function _make_runpath(target, envname)
local pathenv = {}
local searchdirs = hashset.new()
local function insert(dir)
@@ -59,8 +59,8 @@ function _make_runpath_on_windows(target)
end
for _, toolchain in ipairs(target:toolchains()) do
local runenvs = toolchain:runenvs()
- if runenvs and runenvs.PATH then
- for _, env in ipairs(path.splitenv(runenvs.PATH)) do
+ if runenvs and runenvs[envname] then
+ for _, env in ipairs(path.splitenv(runenvs[envname])) do
insert(env)
end
end
@@ -151,15 +151,27 @@ function make(target)
-- add package run environments
_add_target_pkgenvs(addenvs, target, {})
- -- add search directories for all dependent shared libraries on windows
+ -- add search directories for all dependent shared libraries
if target:is_plat("windows") or (target:is_plat("mingw") and is_host("windows")) then
local pathenv = addenvs["PATH"] or setenvs["PATH"]
- local runpath = _make_runpath_on_windows(target)
+ local runpath = _make_runpath(target, "PATH")
if pathenv == nil then
addenvs["PATH"] = runpath
else
table.join2(pathenv, runpath)
end
+ else
+ local envname = "LD_LIBRARY_PATH"
+ if target:is_plat("macosx") then
+ envname = "DYLD_LIBRARY_PATH"
+ end
+ local pathenv = addenvs[envname] or setenvs[envname]
+ local runpath = _make_runpath(target, envname)
+ if pathenv == nil then
+ addenvs[envname] = runpath
+ else
+ table.join2(pathenv, runpath)
+ end
end
-- deduplicate envs