diff options
| author | Saikari <[email protected]> | 2026-01-27 22:49:02 +0300 |
|---|---|---|
| committer | Saikari <[email protected]> | 2026-01-27 22:49:02 +0300 |
| commit | a9761ddbd051326d11682be707e672158cd2710b (patch) | |
| tree | b3cff09342eade8d461e7099e7d5aaba9de68f89 | |
| parent | 02f221f1877719da333190d203b2079b6d58513f (diff) | |
feat: implement getMsg function and update test files for static/shared libraries
| -rw-r--r-- | tests/projects/nim/link_library/inc/test.h | 11 | ||||
| -rw-r--r-- | tests/projects/nim/link_library/maindll.nim | 1 | ||||
| -rw-r--r-- | tests/projects/nim/link_library/mainlib.nim | 1 | ||||
| -rw-r--r-- | tests/projects/nim/link_library/shared.nim | 8 | ||||
| -rw-r--r-- | tests/projects/nim/link_library/static.nim | 10 | ||||
| -rw-r--r-- | tests/projects/nim/link_library/xmake.lua | 4 | ||||
| -rw-r--r-- | xmake/rules/nim/build/target.lua | 20 |
7 files changed, 55 insertions, 0 deletions
diff --git a/tests/projects/nim/link_library/inc/test.h b/tests/projects/nim/link_library/inc/test.h new file mode 100644 index 000000000..f7d68cac8 --- /dev/null +++ b/tests/projects/nim/link_library/inc/test.h @@ -0,0 +1,11 @@ + +#ifndef TEST_H +#define TEST_H + +#ifdef TEST_STATIC + #define TEST_MSG "Hello from Static Lib!" +#else + #define TEST_MSG "Hello from Shared Lib!" +#endif + +#endif diff --git a/tests/projects/nim/link_library/maindll.nim b/tests/projects/nim/link_library/maindll.nim index 0cd97df01..d5e4c8283 100644 --- a/tests/projects/nim/link_library/maindll.nim +++ b/tests/projects/nim/link_library/maindll.nim @@ -2,3 +2,4 @@ import shared echo "Calling shared lib mulTwo(10): ", mulTwo(10) echo "Calling shared lib countWords('hello, world, hello'): ", countWords("hello, world, hello") +echo "Calling shared lib getMsg('test'): ", getMsg() diff --git a/tests/projects/nim/link_library/mainlib.nim b/tests/projects/nim/link_library/mainlib.nim index a99c54b41..f9fe4d85b 100644 --- a/tests/projects/nim/link_library/mainlib.nim +++ b/tests/projects/nim/link_library/mainlib.nim @@ -2,3 +2,4 @@ import static echo "Calling static lib addTwo(10): ", addTwo(10) echo "Calling static lib getAlphabet(): ", getAlphabet() +echo "Calling shared lib getMsg('test'): ", getMsg() diff --git a/tests/projects/nim/link_library/shared.nim b/tests/projects/nim/link_library/shared.nim index 60e02b15e..61a204d09 100644 --- a/tests/projects/nim/link_library/shared.nim +++ b/tests/projects/nim/link_library/shared.nim @@ -9,3 +9,11 @@ proc countWords*(input: string): string = wordFrequencies.inc(word) return "The most frequent word is '" & $wordFrequencies.largest & "'" +{.emit: """ +#include "test.h" +""".} + +proc getMsg*(): cstring {.exportc, dynlib.} = + var msg: cstring + {.emit: "`msg` = TEST_MSG;".} + return msg diff --git a/tests/projects/nim/link_library/static.nim b/tests/projects/nim/link_library/static.nim index bd8847543..1a80c5a58 100644 --- a/tests/projects/nim/link_library/static.nim +++ b/tests/projects/nim/link_library/static.nim @@ -4,3 +4,13 @@ proc addTwo*(x: int): int = proc getAlphabet*(): string = for letter in 'a'..'z': result.add(letter) + +{.emit: """ +#define TEST_STATIC +#include "test.h" +""".} + +proc getMsg*(): cstring {.exportc, dynlib.} = + var msg: cstring + {.emit: "`msg` = TEST_MSG;".} + return msg diff --git a/tests/projects/nim/link_library/xmake.lua b/tests/projects/nim/link_library/xmake.lua index 05512a7cc..cbcef108b 100644 --- a/tests/projects/nim/link_library/xmake.lua +++ b/tests/projects/nim/link_library/xmake.lua @@ -14,7 +14,11 @@ target("executableshared") target("staticlib") set_kind("static") add_files("static.nim") + add_includedirs("inc") + add_headerfiles("inc/*.h") target("sharedlib") set_kind("shared") add_files("shared.nim") + add_includedirs("inc") + add_headerfiles("inc/*.h") diff --git a/xmake/rules/nim/build/target.lua b/xmake/rules/nim/build/target.lua index a292e7cf7..948e317f3 100644 --- a/xmake/rules/nim/build/target.lua +++ b/xmake/rules/nim/build/target.lua @@ -109,6 +109,26 @@ function build_sourcefiles(target, sourcebatch, opt) end end end + + -- add includedirs from dependencies (for static/shared lib with exportc) + -- the dependencies will be compiled via imported symbol at the end + -- we need pass includedirs of static/shared lib to the target + local includedirs = {} + for _, dep in ipairs(target:orderdeps()) do + if dep:kind() == "static" or dep:kind() == "shared" then + table.join2(includedirs, dep:get("includedirs")) + end + end + if #includedirs > 0 then + -- deduplicate + includedirs = table.unique(includedirs) + for _, includedir in ipairs(includedirs) do + local includeflags = compinst:_tool():nf_includedir(includedir) + if includeflags then + table.join2(compflags, includeflags) + end + end + end -- load dependent info local dependinfo = option.get("rebuild") and {} or (depend.load(dependfile) or {}) |
