Add ability to exclude files or directories from wrapped output
This commit is contained in:
parent
fb2acc752a
commit
a9887cc6b2
6 changed files with 70 additions and 15 deletions
|
|
@ -41,12 +41,16 @@ https://github.com/nimterop/nimterop/compare/v0.5.9...v0.6.3
|
||||||
|
|
||||||
- `xxxJBB` now allows for customizing the base location to search packages with the `jbbFlags` param to `getHeader()`. Specifying `giturl=xxx` where `xxx` could be a full Git URL or just the username for Github.com allows changing the default Git repo. In addition, `url=xxx` is also supported to download project info and binaries compiled with BinaryBuilder.org but hosted at another non-Git location. (since v0.6.3)
|
- `xxxJBB` now allows for customizing the base location to search packages with the `jbbFlags` param to `getHeader()`. Specifying `giturl=xxx` where `xxx` could be a full Git URL or just the username for Github.com allows changing the default Git repo. In addition, `url=xxx` is also supported to download project info and binaries compiled with BinaryBuilder.org but hosted at another non-Git location. (since v0.6.3)
|
||||||
|
|
||||||
|
- It is now possible to exclude the contents of specific files or entire directories from the wrapped output using `--exclude | -X` with `toast` or `cExclude()` from a wrapper. This might be required when a header uses `#include` to pull in external dependencies. E.g. `sciter` has a `#include <gtk/gtk.h>` which pulls in the entire GTK ecosystem which is needed for successful preprocessing but we do not want to include those headers in the wrapped output when using `--recurse | -r`.
|
||||||
|
|
||||||
### Other improvements
|
### Other improvements
|
||||||
|
|
||||||
- Generated wrappers no longer depend on nimterop being present - no more `import nimterop/types`. Supporting code is directly included in the wrapper output and only when required. E.g. enum macro is only included if wrapper contains enums. [#125][i125] (since v0.6.1)
|
- Generated wrappers no longer depend on nimterop being present - no more `import nimterop/types`. Supporting code is directly included in the wrapper output and only when required. E.g. enum macro is only included if wrapper contains enums. [#125][i125] (since v0.6.1)
|
||||||
|
|
||||||
- `cImport()` now includes wrapper output from a file rather than inline. Errors in generated wrappers will no longer point to a line in `macros.nim` making debugging easier. (since v0.6.1)
|
- `cImport()` now includes wrapper output from a file rather than inline. Errors in generated wrappers will no longer point to a line in `macros.nim` making debugging easier. (since v0.6.1)
|
||||||
|
|
||||||
|
- `cIncludeDir()` can now accept a `seq[string]` of directories and an optional `exclude` param which sets those include directories to not be included in the wrapped output.
|
||||||
|
|
||||||
|
|
||||||
## Version 0.5.0
|
## Version 0.5.0
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -132,6 +132,7 @@ cDefine("HAS_ABC") # Set #defines for preprocessor and compiler
|
||||||
cDefine("HAS_ABC", "DEF")
|
cDefine("HAS_ABC", "DEF")
|
||||||
|
|
||||||
cIncludeDir("clib/include") # Setup any include directories
|
cIncludeDir("clib/include") # Setup any include directories
|
||||||
|
cExclude("clib/file.h") # Exclude file from wrapped output
|
||||||
|
|
||||||
cImport("clib.h") # Generate wrappers for header specified
|
cImport("clib.h") # Generate wrappers for header specified
|
||||||
|
|
||||||
|
|
@ -219,6 +220,7 @@ Options:
|
||||||
-d, --debug bool false enable debug output
|
-d, --debug bool false enable debug output
|
||||||
-D=, --defines= strings {} definitions to pass to preprocessor
|
-D=, --defines= strings {} definitions to pass to preprocessor
|
||||||
-l=, --dynlib= string "" {.dynlib.} pragma to import symbols - Nim const string or file path
|
-l=, --dynlib= string "" {.dynlib.} pragma to import symbols - Nim const string or file path
|
||||||
|
-X=, --exclude= strings {} files or directories to exclude from the wrapped output
|
||||||
-f=, --feature= Features {} flags to enable experimental features
|
-f=, --feature= Features {} flags to enable experimental features
|
||||||
-I=, --includeDirs= strings {} include directory to pass to preprocessor
|
-I=, --includeDirs= strings {} include directory to pass to preprocessor
|
||||||
-m=, --mode= string "" language parser: c or cpp
|
-m=, --mode= string "" language parser: c or cpp
|
||||||
|
|
|
||||||
|
|
@ -120,6 +120,9 @@ proc getToast(fullpaths: seq[string], recurse: bool = false, dynlib: string = ""
|
||||||
for i in gStateCT.includeDirs:
|
for i in gStateCT.includeDirs:
|
||||||
cmd.add &" --includeDirs+={i.sanitizePath}"
|
cmd.add &" --includeDirs+={i.sanitizePath}"
|
||||||
|
|
||||||
|
for i in gStateCT.exclude:
|
||||||
|
cmd.add &" --exclude+={i.sanitizePath}"
|
||||||
|
|
||||||
if not noNimout:
|
if not noNimout:
|
||||||
cmd.add &" --pnim"
|
cmd.add &" --pnim"
|
||||||
|
|
||||||
|
|
@ -415,22 +418,53 @@ proc cAddSearchDir*(dir: string) {.compileTime.} =
|
||||||
if dir notin gStateCT.searchDirs:
|
if dir notin gStateCT.searchDirs:
|
||||||
gStateCT.searchDirs.add(dir)
|
gStateCT.searchDirs.add(dir)
|
||||||
|
|
||||||
macro cIncludeDir*(dir: static string): untyped =
|
macro cIncludeDir*(dirs: static seq[string], exclude: static[bool] = false): untyped =
|
||||||
## Add an include directory that is forwarded to the C/C++ preprocessor if
|
## Add include directories that are forwarded to the C/C++ preprocessor if
|
||||||
## called within `cImport()` or `c2nImport()` as well as to the C/C++
|
## called within `cImport()` or `c2nImport()` as well as to the C/C++
|
||||||
## compiler during Nim compilation using `{.passC: "-IXXX".}`.
|
## compiler during Nim compilation using `{.passC: "-IXXX".}`.
|
||||||
|
##
|
||||||
|
## Set `exclude = true` if the contents of these include directories should
|
||||||
|
## not be included in the wrapped output.
|
||||||
|
for dir in dirs:
|
||||||
var dir = interpPath(dir)
|
var dir = interpPath(dir)
|
||||||
result = newNimNode(nnkStmtList)
|
result = newNimNode(nnkStmtList)
|
||||||
|
|
||||||
let fullpath = findPath(dir)
|
let fullpath = findPath(dir)
|
||||||
if fullpath notin gStateCT.includeDirs:
|
if fullpath notin gStateCT.includeDirs:
|
||||||
gStateCT.includeDirs.add(fullpath)
|
gStateCT.includeDirs.add(fullpath)
|
||||||
|
if exclude:
|
||||||
|
gStateCT.exclude.add(fullpath)
|
||||||
let str = &"-I{fullpath.quoteShell}"
|
let str = &"-I{fullpath.quoteShell}"
|
||||||
result.add quote do:
|
result.add quote do:
|
||||||
{.passC: `str`.}
|
{.passC: `str`.}
|
||||||
if gStateCT.debug:
|
if gStateCT.debug:
|
||||||
gecho result.repr
|
gecho result.repr
|
||||||
|
|
||||||
|
macro cIncludeDir*(dir: static[string], exclude: static[bool] = false): untyped =
|
||||||
|
## Add an include directory that is forwarded to the C/C++ preprocessor if
|
||||||
|
## called within `cImport()` or `c2nImport()` as well as to the C/C++
|
||||||
|
## compiler during Nim compilation using `{.passC: "-IXXX".}`.
|
||||||
|
##
|
||||||
|
## Set `exclude = true` if the contents of this include directory should
|
||||||
|
## not be included in the wrapped output.
|
||||||
|
return quote do:
|
||||||
|
cIncludeDir(@[`dir`], `exclude` == 1)
|
||||||
|
|
||||||
|
macro cExclude*(paths: static seq[string]): untyped =
|
||||||
|
## Exclude specified paths - files or directories from the wrapped output
|
||||||
|
##
|
||||||
|
## Full path to file or directory is required.
|
||||||
|
result = newNimNode(nnkStmtList)
|
||||||
|
for path in paths:
|
||||||
|
gStateCT.exclude.add path
|
||||||
|
|
||||||
|
macro cExclude*(path: static string): untyped =
|
||||||
|
## Exclude specified path - file or directory from the wrapped output.
|
||||||
|
##
|
||||||
|
## Full path to file or directory is required.
|
||||||
|
return quote do:
|
||||||
|
cExclude(@[`path`])
|
||||||
|
|
||||||
proc cAddStdDir*(mode = "c") {.compileTime.} =
|
proc cAddStdDir*(mode = "c") {.compileTime.} =
|
||||||
## Add the standard `c` [default] or `cpp` include paths to search
|
## Add the standard `c` [default] or `cpp` include paths to search
|
||||||
## path used in calls to `cSearchPath()`.
|
## path used in calls to `cSearchPath()`.
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ type
|
||||||
debug*: bool # `cDebug()` or `--debug | -d` to enable debug mode
|
debug*: bool # `cDebug()` or `--debug | -d` to enable debug mode
|
||||||
defines*: seq[string] # Symbols added by `cDefine()` and `--define | -D` for C/C++ preprocessor/compiler
|
defines*: seq[string] # Symbols added by `cDefine()` and `--define | -D` for C/C++ preprocessor/compiler
|
||||||
dynlib*: string # `cImport(dynlib)` or `--dynlib | -l` to specify variable containing library name
|
dynlib*: string # `cImport(dynlib)` or `--dynlib | -l` to specify variable containing library name
|
||||||
|
exclude*: seq[string] # files or directories to exclude from the wrapped output
|
||||||
feature*: seq[Feature] # `--feature | -f` feature flags enabled
|
feature*: seq[Feature] # `--feature | -f` feature flags enabled
|
||||||
includeDirs*: seq[string] # Paths added by `cIncludeDir()` and `--includeDirs | -I` for C/C++ preprocessor/compiler
|
includeDirs*: seq[string] # Paths added by `cIncludeDir()` and `--includeDirs | -I` for C/C++ preprocessor/compiler
|
||||||
mode*: string # `cImport(mode)` or `--mode | -m` to override detected compiler mode - c or cpp
|
mode*: string # `cImport(mode)` or `--mode | -m` to override detected compiler mode - c or cpp
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ var
|
||||||
preMainOut = ""
|
preMainOut = ""
|
||||||
|
|
||||||
proc process(gState: State, path: string) =
|
proc process(gState: State, path: string) =
|
||||||
doAssert existsFile(path), &"Invalid path {path}"
|
doAssert fileExists(path), &"Invalid path {path}"
|
||||||
|
|
||||||
if gState.mode.Bl:
|
if gState.mode.Bl:
|
||||||
gState.mode = getCompilerMode(path)
|
gState.mode = getCompilerMode(path)
|
||||||
|
|
@ -38,6 +38,7 @@ proc main(
|
||||||
debug = false,
|
debug = false,
|
||||||
defines: seq[string] = @[],
|
defines: seq[string] = @[],
|
||||||
dynlib: string = "",
|
dynlib: string = "",
|
||||||
|
exclude: seq[string] = @[],
|
||||||
feature: seq[Feature] = @[],
|
feature: seq[Feature] = @[],
|
||||||
includeDirs: seq[string] = @[],
|
includeDirs: seq[string] = @[],
|
||||||
mode = "",
|
mode = "",
|
||||||
|
|
@ -65,6 +66,7 @@ proc main(
|
||||||
debug: debug,
|
debug: debug,
|
||||||
defines: defines,
|
defines: defines,
|
||||||
dynlib: dynlib,
|
dynlib: dynlib,
|
||||||
|
exclude: exclude,
|
||||||
feature: feature,
|
feature: feature,
|
||||||
includeDirs: includeDirs,
|
includeDirs: includeDirs,
|
||||||
mode: mode,
|
mode: mode,
|
||||||
|
|
@ -222,6 +224,7 @@ when isMainModule:
|
||||||
"debug": "enable debug output",
|
"debug": "enable debug output",
|
||||||
"defines": "definitions to pass to preprocessor",
|
"defines": "definitions to pass to preprocessor",
|
||||||
"dynlib": "{.dynlib.} pragma to import symbols - Nim const string or file path",
|
"dynlib": "{.dynlib.} pragma to import symbols - Nim const string or file path",
|
||||||
|
"exclude": "files or directories to exclude from the wrapped output",
|
||||||
"feature": "flags to enable experimental features",
|
"feature": "flags to enable experimental features",
|
||||||
"includeDirs": "include directory to pass to preprocessor",
|
"includeDirs": "include directory to pass to preprocessor",
|
||||||
"mode": "language parser: c or cpp",
|
"mode": "language parser: c or cpp",
|
||||||
|
|
@ -247,6 +250,7 @@ when isMainModule:
|
||||||
"debug": 'd',
|
"debug": 'd',
|
||||||
"defines": 'D',
|
"defines": 'D',
|
||||||
"dynlib": 'l',
|
"dynlib": 'l',
|
||||||
|
"exclude": 'X',
|
||||||
"feature": 'f',
|
"feature": 'f',
|
||||||
"includeDirs": 'I',
|
"includeDirs": 'I',
|
||||||
"noComments": 'c',
|
"noComments": 'c',
|
||||||
|
|
|
||||||
|
|
@ -286,7 +286,16 @@ proc getKeyword*(kind: NimSymKind): string =
|
||||||
proc getCurrentHeader*(fullpath: string): string =
|
proc getCurrentHeader*(fullpath: string): string =
|
||||||
("header" & fullpath.splitFile().name.multiReplace([(".", ""), ("-", "")]))
|
("header" & fullpath.splitFile().name.multiReplace([(".", ""), ("-", "")]))
|
||||||
|
|
||||||
|
proc isIncluded(gState: State, file: string): bool {.inline.} =
|
||||||
|
# Check if the specified file should be excluded from wrapped output
|
||||||
|
if gState.exclude.nBl:
|
||||||
|
for excl in gState.exclude:
|
||||||
|
if file.startsWith(excl):
|
||||||
|
return
|
||||||
|
result = true
|
||||||
|
|
||||||
proc getPreprocessor*(gState: State, fullpath: string) =
|
proc getPreprocessor*(gState: State, fullpath: string) =
|
||||||
|
# Get preprocessed output from the C/C++ compiler
|
||||||
var
|
var
|
||||||
args: seq[string]
|
args: seq[string]
|
||||||
start = false
|
start = false
|
||||||
|
|
@ -332,13 +341,14 @@ proc getPreprocessor*(gState: State, fullpath: string) =
|
||||||
start = true
|
start = true
|
||||||
elif gState.recurse:
|
elif gState.recurse:
|
||||||
if (pDir.Bl or pDir in line) and line notin gState.headersProcessed:
|
if (pDir.Bl or pDir in line) and line notin gState.headersProcessed:
|
||||||
start = true
|
|
||||||
newHeaders.incl line
|
newHeaders.incl line
|
||||||
|
start = gState.isIncluded(line)
|
||||||
else:
|
else:
|
||||||
for inc in includeDirs:
|
for inc in includeDirs:
|
||||||
if line.startsWith(inc) and line notin gState.headersProcessed:
|
if line.startsWith(inc) and line notin gState.headersProcessed:
|
||||||
start = true
|
|
||||||
newHeaders.incl line
|
newHeaders.incl line
|
||||||
|
start = gState.isIncluded(line)
|
||||||
|
if start:
|
||||||
break
|
break
|
||||||
elif ": fatal error:" in line:
|
elif ": fatal error:" in line:
|
||||||
doAssert false,
|
doAssert false,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue