Preprocessor mode fixes (#176)

This commit is contained in:
awr1 2020-04-09 23:00:56 -05:00 • committed by GitHub
commit f5dd89904d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 33 deletions

View file

@ -620,6 +620,21 @@ proc make*(path, check: string, flags = "", regex = false) =
doAssert findFile(check, path, regex = regex).len != 0, "# make failed" 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 = proc getCompiler*(): string =
var var
compiler = compiler =
@ -632,13 +647,12 @@ proc getCompiler*(): string =
result = getEnv("CC", compiler) result = getEnv("CC", compiler)
proc getGccPaths*(mode = "c"): seq[string] = proc getGccPaths*(mode: string): seq[string] =
var var
nul = when defined(Windows): "nul" else: "/dev/null" nul = when defined(Windows): "nul" else: "/dev/null"
mmode = if mode == "cpp": "c++" else: mode
inc = false 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(): for line in outp.splitLines():
if "#include <...> search starts here" in line: if "#include <...> search starts here" in line:
@ -655,13 +669,12 @@ proc getGccPaths*(mode = "c"): seq[string] =
when defined(osx): when defined(osx):
result.add(execAction("xcrun --show-sdk-path").output.strip() & "/usr/include") result.add(execAction("xcrun --show-sdk-path").output.strip() & "/usr/include")
proc getGccLibPaths*(mode = "c"): seq[string] = proc getGccLibPaths*(mode: string): seq[string] =
var var
nul = when defined(Windows): "nul" else: "/dev/null" nul = when defined(Windows): "nul" else: "/dev/null"
mmode = if mode == "cpp": "c++" else: mode
linker = when defined(OSX): "-Xlinker" else: "" 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(): for line in outp.splitLines():
if "LIBRARY_PATH=" in line: if "LIBRARY_PATH=" in line:
@ -680,14 +693,14 @@ proc getGccLibPaths*(mode = "c"): seq[string] =
when defined(osx): when defined(osx):
result.add "/usr/lib" result.add "/usr/lib"
proc getStdPath(header: string): string = proc getStdPath(header, mode: string): string =
for inc in getGccPaths(): for inc in getGccPaths(mode):
result = findFile(header, inc, recurse = false, first = true) result = findFile(header, inc, recurse = false, first = true)
if result.len != 0: if result.len != 0:
break break
proc getStdLibPath(lname: string): string = proc getStdLibPath(lname, mode: string): string =
for lib in getGccLibPaths(): for lib in getGccLibPaths(mode):
result = findFile(lname, lib, recurse = false, first = true, regex = true) result = findFile(lname, lib, recurse = false, first = true, regex = true)
if result.len != 0: if result.len != 0:
break break
@ -973,6 +986,7 @@ macro getHeader*(header: static[string], giturl: static[string] = "", dlurl: sta
gDefines[verStr] gDefines[verStr]
else: else:
"" ""
mode = getCompilerMode(header)
# Use alternate library names if specified for regex search # Use alternate library names if specified for regex search
if altNames.len != 0: 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 # Look in standard path if requested by user
stdPath = stdPath =
when `nameStd`: getStdPath(`header`) else: "" when `nameStd`: getStdPath(`header`, `mode`) else: ""
stdLPath = stdLPath =
when `nameStd`: getStdLibPath(`lname`) else: "" when `nameStd`: getStdLibPath(`lname`, `mode`) else: ""
# Look elsewhere if requested while prioritizing standard paths # Look elsewhere if requested while prioritizing standard paths
prePath = prePath =

View file

@ -495,11 +495,10 @@ proc removeStatic(content: string): string =
result.add(body.replace(re"(?m)^(.*\n?)", "//$1")) 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 var
mmode = if mode == "cpp": "c++" else: mode
cmts = if gState.nocomments: "" else: "-CC" 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] = @[] rdata: seq[string] = @[]
start = false start = false

View file

@ -98,9 +98,6 @@ type
nodeBranch*: seq[string] nodeBranch*: seq[string]
CompileMode = enum
c, cpp
Feature* = enum Feature* = enum
ast2 ast2
@ -113,11 +110,9 @@ template nBl(s: typed): untyped {.used.} =
template Bl(s: typed): untyped {.used.} = template Bl(s: typed): untyped {.used.} =
(s.len == 0) (s.len == 0)
const modeDefault {.used.} = $cpp
when not declared(CIMPORT): when not declared(CIMPORT):
export gAtoms, gExpressions, gEnumVals, Kind, Ast, AstTable, State, NimState, export gAtoms, gExpressions, gEnumVals, Kind, Ast, AstTable, State, NimState,
nBl, Bl, CompileMode, modeDefault nBl, Bl
# Redirect output to file when required # Redirect output to file when required
template gecho*(args: string) {.dirty.} = template gecho*(args: string) {.dirty.} =

View file

@ -2,23 +2,18 @@ import os, osproc, strformat, strutils, times
import "."/treesitter/[api, c, cpp] 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) = proc process(gState: State, path: string, astTable: AstTable) =
doAssert existsFile(path), &"Invalid path {path}" doAssert existsFile(path), &"Invalid path {path}"
var var parser = tsParserNew()
parser = tsParserNew()
ext = path.splitFile().ext
defer: defer:
parser.tsParserDelete() parser.tsParserDelete()
if gState.mode.Bl: if gState.mode.Bl:
if ext in [".h", ".c"]: gState.mode = getCompilerMode(path)
gState.mode = "c"
elif ext in [".hxx", ".hpp", ".hh", ".H", ".h++", ".cpp", ".cxx", ".cc", ".C", ".c++"]:
gState.mode = "cpp"
if gState.preprocess: if gState.preprocess:
gState.code = gState.getPreprocessor(path) gState.code = gState.getPreprocessor(path)
@ -61,7 +56,7 @@ proc main(
feature: seq[Feature] = @[], feature: seq[Feature] = @[],
includeHeader = false, includeHeader = false,
includeDirs: seq[string] = @[], includeDirs: seq[string] = @[],
mode = modeDefault, mode = "",
nim: string = "nim", nim: string = "nim",
nocomments = false, nocomments = false,
output = "", output = "",

View file

@ -31,8 +31,7 @@ cPlugin:
sym.name = sym.name.replace("pcre_", "") sym.name = sym.name.replace("pcre_", "")
const FLAGS {.strdefine.} = "" const FLAGS {.strdefine.} = ""
cImport(pcreH, dynlib="dynpcre", flags = FLAGS) cImport(pcreH, dynlib="dynpcre", flags="--mode=c " & FLAGS)
echo version() echo version()
when FLAGS.len != 0: when FLAGS.len != 0: