diff --git a/nimterop.nimble b/nimterop.nimble index 49c40d0..15a8ed1 100644 --- a/nimterop.nimble +++ b/nimterop.nimble @@ -1,11 +1,11 @@ # Package -version = "0.6.11" +version = "0.6.13" author = "genotrance" description = "C/C++ interop for Nim" license = "MIT" -bin = @["nimterop/toast"] +bin = @["nimterop/toast", "nimterop/loaf"] installDirs = @["nimterop"] # Dependencies @@ -33,6 +33,9 @@ proc execTest(test: string, flags = "", runDocs = true) = task buildTimeit, "build timer": exec "nim c --hints:off -d:danger tests/timeit" +task buildLoaf, "build loaf": + execCmd("nim c --hints:off -d:danger nimterop/loaf.nim") + task buildToast, "build toast": execCmd("nim c --hints:off -d:danger nimterop/toast.nim") @@ -46,6 +49,7 @@ task docs, "Generate docs": buildDocs(@["nimterop/all.nim"], "build/htmldocs") task minitest, "Test for Nim CI": + exec "nim c -f -d:danger nimterop/loaf.nim" exec "nim c -f -d:danger nimterop/toast" exec "nim c -f -d:checkAbi -r tests/tast2.nim" exec "nim c -f -d:checkAbi -d:zlibJBB -d:zlibSetVer=1.2.11 -r tests/zlib.nim" @@ -89,6 +93,7 @@ task test, "Test": rmFile("tests/timeit.txt") buildTimeitTask() + buildLoafTask() buildToastTask() basicTask() diff --git a/nimterop/build/ccompiler.nim b/nimterop/build/ccompiler.nim index 550e1b2..f21f45a 100644 --- a/nimterop/build/ccompiler.nim +++ b/nimterop/build/ccompiler.nim @@ -75,7 +75,7 @@ proc getGccLibPaths*(mode: string): seq[string] = when defined(osx): result.add "/usr/lib" -proc getGccInfo*(): tuple[arch, os, compiler, version: string] = +proc getGccInfo*(): tuple[arch, os, compiler, version, libc: string] = let (outp, _) = execAction(&"{getCompiler()} -v") for line in outp.splitLines(): @@ -101,3 +101,5 @@ proc getGccInfo*(): tuple[arch, os, compiler, version: string] = result.compiler = "clang" else: result.compiler = "gcc" + if "musl" in outp: + result.libc = "musl" diff --git a/nimterop/build/conan.nim b/nimterop/build/conan.nim index 6df25d8..ac74089 100644 --- a/nimterop/build/conan.nim +++ b/nimterop/build/conan.nim @@ -16,6 +16,8 @@ type channel*: string recipes*: OrderedTableRef[string, seq[ConanBuild]] + arch*, os*, compiler*, compversion*: string + bhash*: string shared*: bool sharedLibs*: seq[string] @@ -89,6 +91,11 @@ proc `==`*(pkg1, pkg2: ConanPackage): bool = pkg1.user == pkg2.user and pkg1.channel == pkg2.channel and + pkg1.arch == pkg2.arch and + pkg1.os == pkg2.os and + pkg1.compiler == pkg2.compiler and + pkg1.compversion == pkg2.compversion and + pkg1.bhash == pkg2.bhash and pkg1.shared == pkg2.shared) @@ -101,6 +108,15 @@ proc newConanPackage*(name, version, user = "_", channel = "_", bhash = "", shar result.channel = channel result.recipes = newOrderedTable[string, seq[ConanBuild]](2) + let + (arch, os, compiler, compversion, libc) = getGccInfo() + doAssert libc != "musl", "Conan does not provide precompiled binaries using musl" + + result.arch = arch + result.os = os + result.compiler = compiler + result.compversion = compversion + result.bhash = bhash result.shared = shared @@ -189,9 +205,7 @@ proc getConanBuilds*(pkg: ConanPackage, filter = "") = ## `filter` can be used to tweak search terms ## e.g. build_type=Debug&compiler=clang let - (arch, os, compiler, version) = getGccInfo() - - vsplit = version.split('.') + vsplit = pkg.compversion.split('.') vfilter = when defined(OSX): @@ -203,18 +217,18 @@ proc getConanBuilds*(pkg: ConanPackage, filter = "") = if pkg.bhash.Bl: block: var - query = &"?q=arch={arch}&os={os.capitalizeAscii()}" + query = &"?q=arch={pkg.arch}&os={pkg.os.capitalizeAscii()}" if "build_type" notin filter: query &= "&build_type=Release" if "shared=" notin filter: query &= &"&options.shared={($pkg.shared).capitalizeAscii()}" if filter.nBl: query &= &"&{filter}" - if "compiler=" notin filter and os != "windows": - query &= &"&compiler={compiler}&compiler.version=" & vfilter - if "compiler.runtime=" notin filter and os == "windows": + if "compiler=" notin filter and pkg.os != "windows": + query &= &"&compiler={pkg.compiler}&compiler.version=" & vfilter + if "compiler.runtime=" notin filter and pkg.os == "windows": query &= &"&compiler.runtime=MD" - if "compiler.version=" notin filter and os == "windows": + if "compiler.version=" notin filter and pkg.os == "windows": query &= &"&compiler.version=14" query.replace("&", "%20and%20") diff --git a/nimterop/build/getheader.nim b/nimterop/build/getheader.nim index 36dd50c..918e923 100644 --- a/nimterop/build/getheader.nim +++ b/nimterop/build/getheader.nim @@ -59,17 +59,6 @@ macro isDefined*(def: untyped): untyped = false ) -macro getDefine*(def: untyped): untyped = - let version = newIdentNode(def.strVal()) - let verVal = - if gDefines.hasKey(def.strVal()): - gDefines[def.strVal()] - else: - "" - result = quote do: - const `version` {.strdefine.} = `verVal` - `version` - proc getDynlibExt(): string = when defined(Windows): result = "[0-9.\\-]*\\.dll" @@ -219,11 +208,12 @@ proc buildLibrary(lname, outdir, conFlags, cmakeFlags, makeFlags: string, buildT var lpath = findFile(lname, outdir, regex = true) makePath = outdir + buildStatus: BuildStatus + errors: seq[string] if lpath.nBl: return lpath - var buildStatus: BuildStatus for buildType in buildTypes: case buildType @@ -234,6 +224,8 @@ proc buildLibrary(lname, outdir, conFlags, cmakeFlags, makeFlags: string, buildT if buildStatus.built: break + elif buildStatus.error.nBl: + errors.add buildStatus.error if buildStatus.buildPath.len > 0: let libraryExists = findFile(lname, buildStatus.buildPath, regex = true).len > 0 @@ -242,7 +234,7 @@ proc buildLibrary(lname, outdir, conFlags, cmakeFlags, makeFlags: string, buildT make(buildStatus.buildPath, lname, makeFlags, regex = true) buildStatus.built = true - let error = if buildStatus.error.len > 0: buildStatus.error else: "No build files found in " & outdir + let error = if errors.len > 0: errors.join("\n") else: "No build files found in " & outdir doAssert buildStatus.built, &"\nBuild configuration failed - {error}\n" result = findFile(lname, outdir, regex = true) diff --git a/nimterop/build/jbb.nim b/nimterop/build/jbb.nim index c8d86c3..392f848 100644 --- a/nimterop/build/jbb.nim +++ b/nimterop/build/jbb.nim @@ -17,6 +17,8 @@ type url*: string # Download URL + arch*, os*, libc*: string # Target + sharedLibs*: seq[string] staticLibs*: seq[string] requires*: seq[JBBPackage] @@ -39,7 +41,11 @@ proc `==`*(pkg1, pkg2: JBBPackage): bool = ## Check if two JBBPackage objects are equal (not pkg1.isNil and not pkg2.isNil and pkg1.name == pkg2.name and - pkg1.version == pkg2.version) + pkg1.version == pkg2.version and + + pkg1.arch == pkg2.arch and + pkg1.os == pkg2.os and + pkg1.libc == pkg2.libc) proc newJBBPackage*(name, version: string): JBBPackage = ## Create a new JBBPackage with specified name and version @@ -49,6 +55,12 @@ proc newJBBPackage*(name, version: string): JBBPackage = result.baseUrl = jbbBaseUrl result.isGit = true + let + (arch, os, _, _, libc) = getGccInfo() + result.arch = arch + result.os = os + result.libc = libc + proc parseJBBProject(pkg: JBBPackage, outdir: string) = # Get all dependencies from Project.toml let @@ -87,8 +99,6 @@ proc parseJBBArtifacts(pkg: JBBPackage, outdir: string) = let file = outdir / jbbArtifacts - (arch, os, _, _) = getGccInfo() - if fileExists(file): let data = readFile(file) @@ -109,12 +119,14 @@ proc parseJBBArtifacts(pkg: JBBPackage, outdir: string) = # Match arch, os and glibc on Linux to find download URL case name of "arch": - if val == arch and not found: found = true + if val == pkg.arch and not found: found = true of "os": - if val != os and found: found = false + if val != pkg.os and found: found = false of "libc": when defined(Linux): - if val != "glibc" and found: found = false + if found: + let libc = if pkg.libc.nBl: pkg.libc else: "glibc" + if val != libc: found = false of "url": if found: pkg.url = val @@ -125,7 +137,7 @@ proc parseJBBArtifacts(pkg: JBBPackage, outdir: string) = proc findJBBLibs(pkg: JBBPackage, outdir: string) = pkg.sharedLibs = findFiles("(bin|lib)[\\\\/].*\\.(so|dll|dylib)[0-9.]*", outdir) - for lib in findFiles("lib[\\\\/].*\\.(a|lib)$", outdir): + for lib in findFiles("lib[\\\\/].*\\.(a|lib)", outdir): if not lib.endsWith(".dll.a"): pkg.staticLibs.add lib diff --git a/nimterop/build/shell.nim b/nimterop/build/shell.nim index a8511c2..24075cf 100644 --- a/nimterop/build/shell.nim +++ b/nimterop/build/shell.nim @@ -206,13 +206,13 @@ proc getFileDate*(fullpath: string): string = when defined(Windows): let (head, tail) = fullpath.splitPath() - &"forfiles /P {head.sanitizePath()} /M {tail.sanitizePath} /C \"cmd /c echo @fdate @ftime\"" + &"cmd /c forfiles /P {head.sanitizePath()} /M {tail.sanitizePath} /C \"cmd /c echo @fdate @ftime @fsize\"" elif defined(Linux): - &"stat -c %Y {fullpath.sanitizePath}" + &"stat -c %y {fullpath.sanitizePath}" elif defined(OSX) or defined(FreeBSD): &"stat -f %m {fullpath.sanitizePath}" - (result, ret) = execAction(cmd, die=false) + (result, ret) = execAction(cmd) proc touchFile*(fullpath: string) = ## Touch file to update modified date @@ -407,29 +407,24 @@ proc gitTags*(outdir: string): seq[string] = if tag.len != 0: result.add tag +proc loafExePath(): string = + currentSourcePath.parentDir.parentDir / ("loaf".addFileExt ExeExt) + proc findFiles*(file: string, dir: string, recurse = true, regex = false): seq[string] = ## Find all matching files in the specified directory ## ## `file` is a regular expression if `regex` is true ## ## Turn off recursive search with `recurse` + let + loafExe = loafExePath() + + doAssert fileExists(loafExe), "loaf not compiled: " & loafExe.sanitizePath & + " make sure 'nimble build' or 'nimble install' built it" + var - cmd = - when defined(Windows): - "nimgrep --filenames --oneline --nocolor $1 \"$2\" $3" - elif defined(linux): - "find $3 $1 -regextype egrep -regex $2" - elif defined(osx) or defined(FreeBSD): - "find -E $3 $1 -regex $2" - - recursive = "" - - if recurse: - when defined(Windows): - recursive = "--recursive" - else: - when not defined(Windows): - recursive = "-maxdepth 1" + cmd = loafExe.quoteShell & " find --rexp $1 \"$2\" $3" + recursive = if recurse: "--recurse" else: "" var dir = dir @@ -443,22 +438,14 @@ proc findFiles*(file: string, dir: string, recurse = true, regex = false): seq[s file = file.extractFilename - cmd = cmd % [recursive, (".*[\\\\/]" & file & "$").quoteShell, dir.sanitizePath] + cmd = cmd % [recursive, (".*[\\\\/]" & file & "$"), dir.sanitizePath] let (files, ret) = execAction(cmd, die = false) if ret == 0: for line in files.splitLines(): - let f = - when defined(Windows): - if ": " in line: - line.split(": ", maxsplit = 1)[1] - else: - "" - else: - line - if f.len != 0: - result.add f + if line.len != 0: + result.add line proc findFile*(file: string, dir: string, recurse = true, first = false, regex = false): string = ## Find the file in the specified directory diff --git a/nimterop/enumtypepub.nim b/nimterop/enumtypepub.nim deleted file mode 100644 index 7700e30..0000000 --- a/nimterop/enumtypepub.nim +++ /dev/null @@ -1,42 +0,0 @@ -import macros - -macro defineEnum*(typ: untyped): untyped = - result = newNimNode(nnkStmtList) - - # Enum mapped to distinct cint - result.add quote do: - type `typ`* = distinct cint - - for i in ["+", "-", "*", "div", "mod", "shl", "shr", "or", "and", "xor", "<", "<=", "==", ">", ">="]: - let - ni = newIdentNode(i) - typout = if i[0] in "<=>": newIdentNode("bool") else: typ # comparisons return bool - if i[0] == '>': # cannot borrow `>` and `>=` from templates - let - nopp = if i.len == 2: newIdentNode("<=") else: newIdentNode("<") - result.add quote do: - proc `ni`*(x: `typ`, y: cint): `typout` = `nopp`(y, x) - proc `ni`*(x: cint, y: `typ`): `typout` = `nopp`(y, x) - proc `ni`*(x, y: `typ`): `typout` = `nopp`(y, x) - else: - result.add quote do: - proc `ni`*(x: `typ`, y: cint): `typout` {.borrow.} - proc `ni`*(x: cint, y: `typ`): `typout` {.borrow.} - proc `ni`*(x, y: `typ`): `typout` {.borrow.} - result.add quote do: - proc `ni`*(x: `typ`, y: int): `typout` = `ni`(x, y.cint) - proc `ni`*(x: int, y: `typ`): `typout` = `ni`(x.cint, y) - - let - divop = newIdentNode("/") # `/`() - dlrop = newIdentNode("$") # `$`() - notop = newIdentNode("not") # `not`() - result.add quote do: - proc `divop`*(x, y: `typ`): `typ` = `typ`((x.float / y.float).cint) - proc `divop`*(x: `typ`, y: cint): `typ` = `divop`(x, `typ`(y)) - proc `divop`*(x: cint, y: `typ`): `typ` = `divop`(`typ`(x), y) - proc `divop`*(x: `typ`, y: int): `typ` = `divop`(x, y.cint) - proc `divop`*(x: int, y: `typ`): `typ` = `divop`(x.cint, y) - - proc `dlrop`*(x: `typ`): string {.borrow.} - proc `notop`*(x: `typ`): `typ` {.borrow.} diff --git a/nimterop/loaf.nim b/nimterop/loaf.nim new file mode 100644 index 0000000..04b377e --- /dev/null +++ b/nimterop/loaf.nim @@ -0,0 +1,39 @@ +import system except find + +import os + +import cligen + +import strutils except find +import regex except find + +proc findRec(dir: string, pattern: string | Regex, recurse: bool) = + for kind, path in walkDir(dir): + if kind in [pcDir, pcLinkToDir]: + if recurse: findRec(path, pattern, recurse) + elif pattern in path: + echo path.absolutePath() + +proc find(recurse = false, rexp = false, args: seq[string]) = + var + pat = "" + rpat: Regex + for arg in args: + if not arg.startsWith("-"): + if dirExists(arg): + if rexp: + findRec(arg, rpat, recurse) + else: + findRec(arg, pat, recurse) + else: + pat = arg + if rexp: rpat = re(arg) + +when isMainModule: + dispatchMulti([ + find, help = { + "recurse": "recursive search", + "rexp": "patterns are regular expressions", + "args": "pattern1 dir1 dir2 pattern2 dir3 ..." + } + ]) diff --git a/nimterop/toastlib/exprparser.nim b/nimterop/toastlib/exprparser.nim index a77dbf9..a67ed59 100644 --- a/nimterop/toastlib/exprparser.nim +++ b/nimterop/toastlib/exprparser.nim @@ -379,18 +379,28 @@ proc processBinaryExpression(gState: State, node: TSNode, typeofNode: var PNode) result.add gState.getIdent(nimSym) let leftNode = gState.processTSNode(left, typeofNode) + var tyNode = typeofNode if typeofNode.isNil: typeofNode = nkCall.newTree( gState.getIdent("typeof"), leftNode ) + tyNode = typeofNode - let rightNode = gState.processTSNode(right, typeofNode) + # Special case of setting the shift left/right type + # to be the type of the direct left operand + if binarySym in [">>", "<<"]: + tyNode = nkCall.newTree( + gState.getIdent("typeof"), + leftNode + ) + + let rightNode = gState.processTSNode(right, tyNode) result.add leftNode result.add nkCall.newTree( - typeofNode, + tyNode, rightNode ) if binarySym == "/": @@ -399,7 +409,7 @@ proc processBinaryExpression(gState: State, node: TSNode, typeofNode: var PNode) # So we need to emulate C here and cast the whole # expression to the type of the first arg result = nkCall.newTree( - typeofNode, + tyNode, result ) diff --git a/nimterop/toastlib/getters.nim b/nimterop/toastlib/getters.nim index 2e1ac4c..f0a4735 100644 --- a/nimterop/toastlib/getters.nim +++ b/nimterop/toastlib/getters.nim @@ -31,7 +31,7 @@ yield""".split(Whitespace).toHashSet() const # Enum macro read from file - written into wrapper when required - gEnumMacroConst = "import nimterop / enumtypepub" + gEnumMacroConst = staticRead(currentSourcePath.parentDir().parentDir() / "enumtype.nim") var gEnumMacro* = gEnumMacroConst diff --git a/tests/include/tast2.h b/tests/include/tast2.h index 38c5f18..ede71d5 100644 --- a/tests/include/tast2.h +++ b/tests/include/tast2.h @@ -35,6 +35,7 @@ extern "C" { #define EQ4 AVAL < BVAL #define EQ5 AVAL != BVAL #define EQ6 AVAL == BVAL +#define SX_NEAR_ZERO (1.0f / (1 << 28)) // testing integer out of long int range #define INT_FAST16_MIN (-9223372036854775807L-1) diff --git a/tests/tast2.nim b/tests/tast2.nim index a410de4..e301fb5 100644 --- a/tests/tast2.nim +++ b/tests/tast2.nim @@ -134,6 +134,8 @@ assert EQ4 == (AVAL < BVAL) assert EQ5 == (AVAL != BVAL) assert EQ6 == (AVAL == BVAL) +assert SX_NEAR_ZERO == 3.725290298461914e-09 + assert SIZEOF == 1 assert COERCE == 645635670332'u64