diff options
| author | awr1 <41453959+awr1@users.noreply.github.com> | 2020-04-09 23:00:56 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-09 23:00:56 -0500 |
| commit | f5dd89904d8866456c62f4bf6b57b6c264d6dea2 (patch) | |
| tree | 7eaddf43ef4d6599f50f98dc867954ece473037b | |
| parent | 058261c2037d277436d1dc64b53e669f90f0db0e (diff) | |
| download | nimterop-f5dd89904d8866456c62f4bf6b57b6c264d6dea2.tar.gz nimterop-f5dd89904d8866456c62f4bf6b57b6c264d6dea2.zip | |
Preprocessor mode fixes (#176)
| -rw-r--r-- | nimterop/build.nim | 38 | ||||
| -rw-r--r-- | nimterop/getters.nim | 5 | ||||
| -rw-r--r-- | nimterop/globals.nim | 9 | ||||
| -rw-r--r-- | nimterop/toast.nim | 13 | ||||
| -rw-r--r-- | tests/tpcre.nim | 3 |
5 files changed, 35 insertions, 33 deletions
diff --git a/nimterop/build.nim b/nimterop/build.nim index c65ec2a..cc6dc22 100644 --- a/nimterop/build.nim +++ b/nimterop/build.nim @@ -620,6 +620,21 @@ proc make*(path, check: string, flags = "", regex = false) = doAssert findFile(check, path, regex = regex).len != 0, "# make failed" +proc getCompilerMode*(path: string): string = + ## Determines a target language mode from an input filename, if one is not already specified. + let file = path.splitFile() + if file.ext in [".hxx", ".hpp", ".hh", ".H", ".h++", ".cpp", ".cxx", ".cc", ".C", ".c++"]: + result = "cpp" + elif file.ext in [".h", ".c"]: + result = "c" + +proc getGccModeArg*(mode: string): string = + ## Produces a GCC argument that explicitly sets the language mode to be used by the compiler. + if mode == "cpp": + result = "-xc++" + elif mode == "c": + result = "-xc" + proc getCompiler*(): string = var compiler = @@ -632,13 +647,12 @@ proc getCompiler*(): string = result = getEnv("CC", compiler) -proc getGccPaths*(mode = "c"): seq[string] = +proc getGccPaths*(mode: string): seq[string] = var nul = when defined(Windows): "nul" else: "/dev/null" - mmode = if mode == "cpp": "c++" else: mode inc = false - (outp, _) = execAction(&"""{getCompiler()} -Wp,-v -x{mmode} {nul}""", die = false) + (outp, _) = execAction(&"""{getCompiler()} -Wp,-v {getGccModeArg(mode)} {nul}""", die = false) for line in outp.splitLines(): if "#include <...> search starts here" in line: @@ -655,13 +669,12 @@ proc getGccPaths*(mode = "c"): seq[string] = when defined(osx): result.add(execAction("xcrun --show-sdk-path").output.strip() & "/usr/include") -proc getGccLibPaths*(mode = "c"): seq[string] = +proc getGccLibPaths*(mode: string): seq[string] = var nul = when defined(Windows): "nul" else: "/dev/null" - mmode = if mode == "cpp": "c++" else: mode linker = when defined(OSX): "-Xlinker" else: "" - (outp, _) = execAction(&"""{getCompiler()} {linker} -v -x{mmode} {nul}""", die = false) + (outp, _) = execAction(&"""{getCompiler()} {linker} -v {getGccModeArg(mode)} {nul}""", die = false) for line in outp.splitLines(): if "LIBRARY_PATH=" in line: @@ -680,14 +693,14 @@ proc getGccLibPaths*(mode = "c"): seq[string] = when defined(osx): result.add "/usr/lib" -proc getStdPath(header: string): string = - for inc in getGccPaths(): +proc getStdPath(header, mode: string): string = + for inc in getGccPaths(mode): result = findFile(header, inc, recurse = false, first = true) if result.len != 0: break -proc getStdLibPath(lname: string): string = - for lib in getGccLibPaths(): +proc getStdLibPath(lname, mode: string): string = + for lib in getGccLibPaths(mode): result = findFile(lname, lib, recurse = false, first = true, regex = true) if result.len != 0: break @@ -973,6 +986,7 @@ macro getHeader*(header: static[string], giturl: static[string] = "", dlurl: sta gDefines[verStr] else: "" + mode = getCompilerMode(header) # Use alternate library names if specified for regex search if altNames.len != 0: @@ -1008,9 +1022,9 @@ macro getHeader*(header: static[string], giturl: static[string] = "", dlurl: sta # Look in standard path if requested by user stdPath = - when `nameStd`: getStdPath(`header`) else: "" + when `nameStd`: getStdPath(`header`, `mode`) else: "" stdLPath = - when `nameStd`: getStdLibPath(`lname`) else: "" + when `nameStd`: getStdLibPath(`lname`, `mode`) else: "" # Look elsewhere if requested while prioritizing standard paths prePath = diff --git a/nimterop/getters.nim b/nimterop/getters.nim index 044cde7..986c54d 100644 --- a/nimterop/getters.nim +++ b/nimterop/getters.nim @@ -495,11 +495,10 @@ proc removeStatic(content: string): string = result.add(body.replace(re"(?m)^(.*\n?)", "//$1")) ) -proc getPreprocessor*(gState: State, fullpath: string, mode = "cpp"): string = +proc getPreprocessor*(gState: State, fullpath: string): string = var - mmode = if mode == "cpp": "c++" else: mode cmts = if gState.nocomments: "" else: "-CC" - cmd = &"""{getCompiler()} -E {cmts} -dD -x{mmode} -w """ + cmd = &"""{getCompiler()} -E {cmts} -dD {getGccModeArg(gState.mode)} -w """ rdata: seq[string] = @[] start = false diff --git a/nimterop/globals.nim b/nimterop/globals.nim index 701db61..42f7cb7 100644 --- a/nimterop/globals.nim +++ b/nimterop/globals.nim @@ -98,9 +98,6 @@ type nodeBranch*: seq[string] - CompileMode = enum - c, cpp - Feature* = enum ast2 @@ -113,11 +110,9 @@ template nBl(s: typed): untyped {.used.} = template Bl(s: typed): untyped {.used.} = (s.len == 0) -const modeDefault {.used.} = $cpp - when not declared(CIMPORT): export gAtoms, gExpressions, gEnumVals, Kind, Ast, AstTable, State, NimState, - nBl, Bl, CompileMode, modeDefault + nBl, Bl # Redirect output to file when required template gecho*(args: string) {.dirty.} = @@ -133,4 +128,4 @@ when not declared(CIMPORT): template decho*(str: untyped): untyped = if nimState.gState.debug: - necho str.getCommented()
\ No newline at end of file + necho str.getCommented() diff --git a/nimterop/toast.nim b/nimterop/toast.nim index fe63fb9..c3d6a3d 100644 --- a/nimterop/toast.nim +++ b/nimterop/toast.nim @@ -2,23 +2,18 @@ import os, osproc, strformat, strutils, times import "."/treesitter/[api, c, cpp] -import "."/[ast, ast2, globals, getters, grammar] +import "."/[ast, ast2, globals, getters, grammar, build] proc process(gState: State, path: string, astTable: AstTable) = doAssert existsFile(path), &"Invalid path {path}" - var - parser = tsParserNew() - ext = path.splitFile().ext + var parser = tsParserNew() defer: parser.tsParserDelete() if gState.mode.Bl: - if ext in [".h", ".c"]: - gState.mode = "c" - elif ext in [".hxx", ".hpp", ".hh", ".H", ".h++", ".cpp", ".cxx", ".cc", ".C", ".c++"]: - gState.mode = "cpp" + gState.mode = getCompilerMode(path) if gState.preprocess: gState.code = gState.getPreprocessor(path) @@ -61,7 +56,7 @@ proc main( feature: seq[Feature] = @[], includeHeader = false, includeDirs: seq[string] = @[], - mode = modeDefault, + mode = "", nim: string = "nim", nocomments = false, output = "", diff --git a/tests/tpcre.nim b/tests/tpcre.nim index f896e9c..c8e8059 100644 --- a/tests/tpcre.nim +++ b/tests/tpcre.nim @@ -31,8 +31,7 @@ cPlugin: sym.name = sym.name.replace("pcre_", "") const FLAGS {.strdefine.} = "" -cImport(pcreH, dynlib="dynpcre", flags = FLAGS) - +cImport(pcreH, dynlib="dynpcre", flags="--mode=c " & FLAGS) echo version() when FLAGS.len != 0: |
