From 66fe786cf03647da35e452e8eb46563b21a64fc3 Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Thu, 17 Jan 2019 16:25:49 -0600 Subject: [PATCH 1/7] Fix walkDir, cCompile mode, add tsoloud, fix void * --- README.md | 6 +++++ nimterop.nimble | 5 ++++- nimterop/cimport.nim | 53 +++++++++++++++++++++++++++++++++++--------- nimterop/grammar.nim | 5 ++--- 4 files changed, 55 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 457903f..a4d3098 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,12 @@ Detailed documentation is still forthcoming. `cImport("header.h", recurse=true)` - import all supported definitions from header file and #includes +`cCompile("file.c")` - compile C/C++ implementation into binary + +`cCompile("path/to/*.c")` - compile in all files matching wildcard + +`cCompile("path/to/dir", "cpp")` - compile in all C++ files found recursively + `cAddSearchDir("XXX")` - add directory XXX to search path in calls to `cSearchPath()` `cAddStdDir("XXX")` - add standard "c" [default] or "cpp" include paths to search path diff --git a/nimterop.nimble b/nimterop.nimble index ca9040e..368ec29 100644 --- a/nimterop.nimble +++ b/nimterop.nimble @@ -12,7 +12,7 @@ installDirs = @["nimterop"] requires "nim >= 0.19.0", "regex >= 0.10.0", "cligen >= 0.9.17" -proc execCmd(cmd:string)= +proc execCmd(cmd: string)= echo cmd exec cmd @@ -23,3 +23,6 @@ task test, "Test": when defined(windows): execCmd "nim c -r tests/tmath.nim" execCmd "nim cpp -r tests/tmath.nim" + when not defined(OSX): + execCmd "nim c -r tests/tsoloud.nim" + execCmd "nim cpp -r tests/tsoloud.nim" \ No newline at end of file diff --git a/nimterop/cimport.nim b/nimterop/cimport.nim index 91de727..2ff6251 100644 --- a/nimterop/cimport.nim +++ b/nimterop/cimport.nim @@ -1,6 +1,6 @@ import macros, os, strformat, strutils -const CIMPORT = 1 +const CIMPORT {.used.} = 1 include "."/globals @@ -23,6 +23,36 @@ proc findPath(path: string, fail = true): string = else: return "" +proc walkDirImpl(indir, inext: string, file=true): seq[string] = + let + dir = joinPathIfRel(getProjectPath(), indir) + ext = + if inext.len != 0: + when not defined(Windows): + "-name " & inext + else: + "\\" & inext + else: + "" + + let + cmd = + when defined(Windows): + if file: + "cmd /c dir /s/b/a-d " & dir.replace("/", "\\") & ext + else: + "cmd /c dir /s/b/ad " & dir.replace("/", "\\") + else: + if file: + "find $1 -type f $2" % [dir, ext] + else: + "find $1 -type d" % dir + + (output, ret) = gorgeEx(cmd) + + if ret == 0: + result = output.splitLines() + proc getToast(fullpath: string, recurse: bool = false): string = var cmd = when defined(Windows): "cmd /c " else: "" @@ -125,12 +155,11 @@ macro cAddStdDir*(mode = "c"): untyped = result.add quote do: cAddSearchDir(`sline`) -macro cCompile*(path: static string): untyped = +macro cCompile*(path: static string, mode = "c"): untyped = result = newNimNode(nnkStmtList) var stmt = "" - flags = "" proc fcompile(file: string): string = let fn = file.splitFile().name @@ -147,22 +176,26 @@ macro cCompile*(path: static string): untyped = else: return "{.compile: (\"../$#\", \"$#.o\").}" % [file.replace("\\", "/"), ufn] - proc dcompile(dir: string) = - for f in walkFiles(dir): - stmt &= fcompile(f) & "\n" + proc dcompile(dir: string, ext=""): string = + let + files = walkDirImpl(dir, ext) + + for f in files: + if f.len != 0: + result &= fcompile(f) & "\n" if path.contains("*") or path.contains("?"): - dcompile(path) + stmt &= dcompile(path) else: let fpath = findPath(path) if fileExists(fpath): stmt &= fcompile(fpath) & "\n" elif dirExists(fpath): - if flags.contains("cpp"): + if mode.strVal().contains("cpp"): for i in @["*.C", "*.cpp", "*.c++", "*.cc", "*.cxx"]: - dcompile(fpath / i) + stmt &= dcompile(fpath, i) else: - dcompile(fpath / "*.c") + stmt &= dcompile(fpath, "*.c") result.add stmt.parseStmt() diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim index e6a0fe2..d2632b8 100644 --- a/nimterop/grammar.nim +++ b/nimterop/grammar.nim @@ -82,8 +82,7 @@ proc initGrammar() = pname = "a" & $count count += 1 i += 1 - if ptyp != "object": - pout &= &"{pname}: {getPtrType(pptr&ptyp)}," + pout &= &"{pname}: {getPtrType(pptr&ptyp)}," # typedef int X # typedef X Y @@ -149,7 +148,7 @@ proc initGrammar() = flen = gStateRT.data[i].val.getIdentifier() gStateRT.typeStr &= &" {name}* = {aptr}array[{flen}, {getPtrType(tptr&typ)}]\n" else: - if name == typ or typ == "object": + if name == typ: gStateRT.typeStr &= &" {name}* = object\n" else: gStateRT.typeStr &= &" {name}* = {getPtrType(tptr&typ)}\n" From a18ab8fd1d5db37fcb4d88b6769fdc2be629257f Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Thu, 17 Jan 2019 18:21:28 -0600 Subject: [PATCH 2/7] Add tsoloud test --- tests/tsoloud.nim | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/tsoloud.nim diff --git a/tests/tsoloud.nim b/tests/tsoloud.nim new file mode 100644 index 0000000..5a919f6 --- /dev/null +++ b/tests/tsoloud.nim @@ -0,0 +1,37 @@ +import os, nimterop/[cimport, git] + +gitPull("https://github.com/jarikomppa/soloud", "soloud", "include/*\nsrc & *\n") + +cDebug() + +const + inc = "soloud/include" + src = "soloud/src" + +cIncludeDir(inc) + +when defined(Linux): + {.passL: "-lpthread".} + cDefine("WITH_OSS") + cCompile(src/"backend/oss/*.cpp") + +when defined(Windows): + {.passC: "-msse".} + {.passL: "-lwinmm".} + cDefine("WITH_WINMM") + cCompile(src/"backend/winmm/*.cpp") + +cCompile(src/"c_api/soloud_c.cpp") +cCompile(src/"core/*.cpp") +cCompile(src/"audiosource", "cpp") +cCompile(src/"audiosource", "c") +cCompile(src/"filter/*.cpp") + +cImport(inc/"soloud_c.h") + +var + s = Soloud_create() + +echo s.Soloud_init() + +s.Soloud_destroy() \ No newline at end of file From a97cd56a324cc7ce4158df09f3689684f89c48e7 Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Thu, 17 Jan 2019 18:26:04 -0600 Subject: [PATCH 3/7] Add tsoloud test --- tests/tsoloud.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tsoloud.nim b/tests/tsoloud.nim index 5a919f6..544c1af 100644 --- a/tests/tsoloud.nim +++ b/tests/tsoloud.nim @@ -1,6 +1,6 @@ import os, nimterop/[cimport, git] -gitPull("https://github.com/jarikomppa/soloud", "soloud", "include/*\nsrc & *\n") +gitPull("https://github.com/jarikomppa/soloud", "soloud", "include/*\nsrc/*\n") cDebug() From a7fd97b59bd70699f726f3981c9361aeed60c54a Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Thu, 17 Jan 2019 18:46:10 -0600 Subject: [PATCH 4/7] Fix Linux tsoloud --- tests/tsoloud.nim | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/tsoloud.nim b/tests/tsoloud.nim index 544c1af..6e09aed 100644 --- a/tests/tsoloud.nim +++ b/tests/tsoloud.nim @@ -11,9 +11,10 @@ const cIncludeDir(inc) when defined(Linux): - {.passL: "-lpthread".} - cDefine("WITH_OSS") - cCompile(src/"backend/oss/*.cpp") + {.passC: "-std=c++0x".} + {.passL: "-lpthread -lasound".} + cDefine("WITH_ALSA") + cCompile(src/"backend/alsa/*.cpp") when defined(Windows): {.passC: "-msse".} From 9ee4fe2dac2613001e042940c61f962331edf271 Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Thu, 17 Jan 2019 18:51:52 -0600 Subject: [PATCH 5/7] soloud OSS for Linux --- tests/tsoloud.nim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/tsoloud.nim b/tests/tsoloud.nim index 6e09aed..3a7197c 100644 --- a/tests/tsoloud.nim +++ b/tests/tsoloud.nim @@ -12,9 +12,9 @@ cIncludeDir(inc) when defined(Linux): {.passC: "-std=c++0x".} - {.passL: "-lpthread -lasound".} - cDefine("WITH_ALSA") - cCompile(src/"backend/alsa/*.cpp") + {.passL: "-lpthread".} + cDefine("WITH_OSS") + cCompile(src/"backend/oss/*.cpp") when defined(Windows): {.passC: "-msse".} From 96e0eea609bb7905943022305564d6c1c77c4e1c Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Thu, 17 Jan 2019 18:59:23 -0600 Subject: [PATCH 6/7] std guess for travis --- tests/tsoloud.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tsoloud.nim b/tests/tsoloud.nim index 3a7197c..0890ca1 100644 --- a/tests/tsoloud.nim +++ b/tests/tsoloud.nim @@ -11,7 +11,7 @@ const cIncludeDir(inc) when defined(Linux): - {.passC: "-std=c++0x".} + {.passC: "-std=c++11".} {.passL: "-lpthread".} cDefine("WITH_OSS") cCompile(src/"backend/oss/*.cpp") From 79409d27b262157b1f9714afc9c327dab0f21d0d Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Thu, 17 Jan 2019 20:20:50 -0600 Subject: [PATCH 7/7] No tsoloud on Travis Linux --- nimterop.nimble | 13 ++++++++++--- tests/tsoloud.nim | 1 - 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/nimterop.nimble b/nimterop.nimble index 368ec29..11d8342 100644 --- a/nimterop.nimble +++ b/nimterop.nimble @@ -12,10 +12,14 @@ installDirs = @["nimterop"] requires "nim >= 0.19.0", "regex >= 0.10.0", "cligen >= 0.9.17" -proc execCmd(cmd: string)= +proc execCmd(cmd: string) = echo cmd exec cmd +proc tsoloud() = + execCmd "nim c -r tests/tsoloud.nim" + execCmd "nim cpp -r tests/tsoloud.nim" + task test, "Test": execCmd "nim c -r tests/tnimterop_c.nim" execCmd "nim cpp -r tests/tnimterop_c.nim" @@ -24,5 +28,8 @@ task test, "Test": execCmd "nim c -r tests/tmath.nim" execCmd "nim cpp -r tests/tmath.nim" when not defined(OSX): - execCmd "nim c -r tests/tsoloud.nim" - execCmd "nim cpp -r tests/tsoloud.nim" \ No newline at end of file + when defined(Windows): + tsoloud() + else: + if not existsEnv("TRAVIS"): + tsoloud() \ No newline at end of file diff --git a/tests/tsoloud.nim b/tests/tsoloud.nim index 0890ca1..544c1af 100644 --- a/tests/tsoloud.nim +++ b/tests/tsoloud.nim @@ -11,7 +11,6 @@ const cIncludeDir(inc) when defined(Linux): - {.passC: "-std=c++11".} {.passL: "-lpthread".} cDefine("WITH_OSS") cCompile(src/"backend/oss/*.cpp")