Skip to content

Commit b9c3062

Browse files
committed
cmakejs support for dylib
Signed-off-by: George Lemon <[email protected]>
1 parent b4b4ce2 commit b9c3062

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

src/denimpkg/commands/build.nim

+33-16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ proc getNodeGypConfig(getNimPath: string, release: bool = false): JsonNode =
1212
"linkflags": ["-ldl"]
1313
}
1414

15+
# https://stackoverflow.com/questions/52605527/cmake-or-g-include-dll-libraries
1516
const cMakeListsContent = """
1617
cmake_minimum_required(VERSION 3.15)
1718
cmake_policy(SET CMP0091 NEW)
@@ -25,9 +26,10 @@ include_directories(${CMAKE_JS_INC})
2526
2627
file(GLOB SOURCE_FILES "./denim_build/nimcache/*.c" "./denim_build/nimcache/*.h")
2728
28-
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
29-
set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CXX PREFIX "" SUFFIX ".node")
30-
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB})
29+
add_library(DENIM_PKG_NAME SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
30+
set_target_properties(DENIM_PKG_NAME PROPERTIES LINKER_LANGUAGE CXX PREFIX "" SUFFIX ".node")
31+
32+
DENIM_PKG_LINK_LIBS
3133
3234
if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET)
3335
# Generate node.lib
@@ -50,25 +52,23 @@ proc buildCommand*(v: Values) =
5052
QuitFailure.quit
5153

5254
if not isEmptyDir(addonPathDirectory):
53-
echo addonPathDirectory
5455
if not v.has("-y"):
55-
display("Directory is not empty: " & os.splitPath(addonPathDirectory).tail, indent=2, br="after")
56-
if promptConfirm("👉 Are you sure you want to remove contents?"):
56+
displayInfo("Directory is not empty: " & os.splitPath(addonPathDirectory).tail)
57+
if promptConfirm("👉 Do you want to remove current contents? (y/N)"):
5758
os.removeDir(addonPathDirectory)
5859
else:
5960
display("Canceled", indent=2, br="after")
6061
QuitFailure.quit
6162
else:
6263
os.removeDir(addonPathDirectory)
63-
display("🔥 Running Nim Compiler", indent=2, br="both")
64+
displayInfo("🔥 Running Nim Compiler")
6465

6566
var args = @[
6667
"--nimcache:$1",
6768
"--define:napibuild",
6869
"--compileOnly",
69-
"--noMain",
70+
"--noMain"
7071
]
71-
7272
if v.has("-r"):
7373
add args, "-d:release"
7474
add args, "--opt:speed"
@@ -87,7 +87,7 @@ proc buildCommand*(v: Values) =
8787
display(nimCmd.output)
8888
var getNimPath = execCmdEx("choosenim show path")
8989
if getNimPath.exitCode != 0:
90-
display("Can't find Nim path")
90+
displayError("Can't find Nim path")
9191
QuitFailure.quit
9292
discard execProcess("ln", args = [
9393
"-s",
@@ -96,16 +96,33 @@ proc buildCommand*(v: Values) =
9696
], options={poStdErrToStdOut, poUsePath})
9797

9898
if v.has("--cmake"):
99-
display("✨ Building with CMake.js", indent=2, br="after")
100-
writeFile(currDir / "CMakeLists.txt", cMakeListsContent.replace("DENIM_PKG_NAME", entryFile.splitFile.name))
99+
displayInfo("✨ Building with CMake.js")
100+
101+
# cmake - add target libs, if any
102+
var denimLinkLibs: seq[string]
103+
if v.has("--libs"):
104+
for x in v.get("--libs").getStr.split(","):
105+
denimLinkLibs.add(x)
106+
107+
let pkgName = entryFile.splitFile.name
108+
writeFile(currDir / "CMakeLists.txt",
109+
cMakeListsContent.multiReplace(
110+
("DENIM_PKG_NAME", pkgName),
111+
("DENIM_PKG_LINK_LIBS",
112+
if denimLinkLibs.len > 0:
113+
"target_link_libraries(" & pkgName & " " & denimLinkLibs.join(" ") & ")"
114+
else: ""
115+
)
116+
)
117+
)
101118
let cmakeCmd = execCmdEx("cmake-js compile --runtime node --out " & "denim_build" / "build")
102119
if cmakeCmd.exitCode != 0:
103120
display(cmakeCmd.output)
104121
QuitFailure.quit
105122
elif v.has("--verbose"):
106123
display(cmakeCmd.output)
107124
else:
108-
display("✨ Building with node-gyp", indent=2, br="after")
125+
displayInfo("✨ Building with node-gyp")
109126
var
110127
gyp = %* {"targets": [getNodeGypConfig(getNimPath.output.strip, v.has("-r"))]}
111128
jsonConfigPath = cachePathDirectory / entryFile.replace(".nim", ".json")
@@ -133,10 +150,10 @@ proc buildCommand*(v: Values) =
133150
binaryTargetPath = binDirectory / binName
134151

135152
if fileExists(binaryNodePath) == false:
136-
display("👉 Oups! $1 not found. Try build again" % [binName], indent=2)
153+
displayError("👉 Oups! $1 not found. Try build again" % [binName])
137154
QuitFailure.quit
138155
else:
139156
discard existsOrCreateDir(binDirectory) # ensure bin directory exists
140157
moveFile(binaryNodePath, binaryTargetPath) # move .node addon
141-
display("👌 Done! Check your /bin directory", indent=2, br="after")
142-
QuitSuccess.quit
158+
displaySuccess("👌 Done! Check your `bin` directory")
159+
displayInfo(binDirectory)

0 commit comments

Comments
 (0)