From 42a96bd4bf3ec0baf9518eb8704922ebef912cb7 Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Mon, 7 Oct 2019 14:46:26 -0700 Subject: [PATCH] Fix #134, time64_t, buildDocs error on Win --- nimterop/build.nim | 9 +++-- nimterop/docs.nim | 84 +++++++++++++++++++++++++++++--------------- nimterop/grammar.nim | 15 ++++++-- nimterop/types.nim | 5 ++- 4 files changed, 76 insertions(+), 37 deletions(-) diff --git a/nimterop/build.nim b/nimterop/build.nim index bcbd96a..2f8765e 100644 --- a/nimterop/build.nim +++ b/nimterop/build.nim @@ -45,7 +45,7 @@ proc execAction*(cmd: string, retry = 0, nostderr = false): string = sleep(500) result = execAction(cmd, retry = retry - 1) else: - doAssert true, "Command failed: " & $(ret, nostderr) & "\nccmd: " & ccmd & "\nresult:\n" & result + doAssert true, "Command failed: " & $(ret, nostderr) & "\ncmd: " & ccmd & "\nresult:\n" & result proc findExe*(exe: string): string = ## Find the specified executable using the `which`/`where` command - supported @@ -278,10 +278,9 @@ proc findFile*(file: string, dir: string, recurse = true, first = false, regex = when not defined(windows): recursive = "-maxdepth 1" - if not regex: - let - dir = dir / file.parentDir() - file = file.extractFilename + let + dir = if not regex: dir / file.parentDir() else: dir + file = if not regex: file.extractFilename else: file cmd = cmd % [recursive, (".*[\\\\/]" & file & "$").quoteShell, dir.sanitizePath] diff --git a/nimterop/docs.nim b/nimterop/docs.nim index 8fd396b..9663d6c 100644 --- a/nimterop/docs.nim +++ b/nimterop/docs.nim @@ -1,7 +1,7 @@ -import macros, strformat +import macros, regex, strformat when (NimMajor, NimMinor, NimPatch) >= (0, 19, 9): - from os import parentDir, getCurrentCompilerExe + from os import parentDir, getCurrentCompilerExe, DirSep proc getNimRootDir(): string = #[ hack, but works @@ -15,43 +15,69 @@ else: proc getCurrentCompilerExe*(): string = "nim" -proc buildDocs*(files: openArray[string], path: string, baseDir = getProjectPath() & "/", + const + DirSep = when defined(windows): '\\' else: '/' + +proc execAction(cmd: string): string = + var + ccmd = "" + ret = 0 + when defined(Windows): + ccmd = "cmd /c " & cmd + elif defined(posix): + ccmd = cmd + else: + doAssert false + + (result, ret) = gorgeEx(ccmd) + doAssert ret == 0, "Command failed: " & $ret & "\ncmd: " & ccmd & "\nresult:\n" & result + +proc buildDocs*(files: openArray[string], path: string, baseDir = getProjectPath() & $DirSep, defines: openArray[string] = @[]) = ## Generate docs for all specified nim `files` to the specified `path` ## ## `baseDir` is the project path by default and `files` and `path` are relative ## to that directory. Set to "" if using absolute paths. ## + ## `defines` is a list of `-d:xxx` define flags (the `xxx` part) that should be passed + ## to `nim doc` so that `getHeader()` is invoked correctly. + ## ## Use the `--publish` flag with nimble to publish docs contained in ## `path` to Github in the `gh-pages` branch. This requires the ghp-import ## package for Python: `pip install ghp-import` ## ## WARNING: `--publish` will destroy any existing content in this branch. - let - baseDir = - if baseDir == "/": - getCurrentDir() & "/" - else: - baseDir - path = baseDir & path - defStr = block: - var defStr = "" - for def in defines: - defStr &= " -d:" & def - defStr - nim = getCurrentCompilerExe() - for file in files: - echo gorge(&"{nim} doc {defStr} -o:{path} --project --index:on {baseDir & file}") + ## + ## NOTE: `buildDocs()` only works correctly on Windows with Nim 1.0+ since + ## https://github.com/nim-lang/Nim/pull/11814 is required. + when defined(windows) and (NimMajor, NimMinor, NimPatch) < (1, 0, 0): + echo "buildDocs() unsupported on Windows for Nim < 1.0 - requires PR #11814" + else: + let + baseDir = + if baseDir == $DirSep: + getCurrentDir() & $DirSep + else: + baseDir + path = baseDir & path.replace(re"[/\\]", $DirSep) + defStr = block: + var defStr = "" + for def in defines: + defStr &= " -d:" & def + defStr + nim = getCurrentCompilerExe() + for file in files: + echo execAction(&"{nim} doc {defStr} -o:{path} --project --index:on {baseDir & file}") - echo gorge(&"{nim} buildIndex -o:{path}/theindex.html {path}") - when declared(getNimRootDir): - #[ - this enables doc search, works at least locally with: - cd {path} && python -m SimpleHTTPServer 9009 - ]# - echo gorge(&"{nim} js -o:{path}/dochack.js {getNimRootDir()}/tools/dochack/dochack.nim") + echo execAction(&"{nim} buildIndex -o:{path}/theindex.html {path}") + when declared(getNimRootDir): + #[ + this enables doc search, works at least locally with: + cd {path} && python -m SimpleHTTPServer 9009 + ]# + echo execAction(&"{nim} js -o:{path}/dochack.js {getNimRootDir()}/tools/dochack/dochack.nim") - for i in 0 .. paramCount(): - if paramStr(i) == "--publish": - echo gorge(&"ghp-import --no-jekyll -fp {path}") - break + for i in 0 .. paramCount(): + if paramStr(i) == "--publish": + echo execAction(&"ghp-import --no-jekyll -fp {path}") + break \ No newline at end of file diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim index b14a208..57cbbdd 100644 --- a/nimterop/grammar.nim +++ b/nimterop/grammar.nim @@ -142,6 +142,7 @@ proc initGrammar(): Grammar = nname = "" tptr = "" aptr = "" + pragmas: seq[string] = @[] i += 1 while i < nimState.data.len and "pointer" in nimState.data[i].name: @@ -158,8 +159,11 @@ proc initGrammar(): Grammar = nname = nimState.getIdentifier(name, nskType) i += 1 + if nimState.gState.dynlib.len == 0: + pragmas.add nimState.getImportC(name, nname) + let - pragma = nimState.getPragma(nimState.getImportC(name, nname)) + pragma = nimState.getPragma(pragmas) if nname notin gTypeMap and typ.nBl and nname.nBl and nimState.addNewIdentifer(nname): if i < nimState.data.len and nimState.data[^1].name == "function_declarator": @@ -255,8 +259,15 @@ proc initGrammar(): Grammar = if nimState.data.len == 1: nimState.typeStr &= &"{nimState.getComments()}\n {nname}* {{.bycopy.}} = object{union}" else: + var + pragmas: seq[string] = @[] + if nimState.gState.dynlib.len == 0: + pragmas.add nimState.getImportC(prefix & name, nname) + pragmas.add "bycopy" + let - pragma = nimState.getPragma(nimState.getImportC(prefix & name, nname), "bycopy") + pragma = nimState.getPragma(pragmas) + nimState.typeStr &= &"{nimState.getComments()}\n {nname}*{pragma} = object{union}" var diff --git a/nimterop/types.nim b/nimterop/types.nim index 8492f00..3dd8f39 100644 --- a/nimterop/types.nim +++ b/nimterop/types.nim @@ -15,10 +15,13 @@ when (NimMajor, NimMinor, NimPatch) < (0, 19, 9): import posix type time_t* = Time + time64_t* = Time wchar_t* {.importc.} = object else: import std/time_t as time_t_temp - type time_t* = time_t_temp.Time + type + time_t* = time_t_temp.Time + time64_t* = time_t_temp.Time when defined(c) or defined(nimdoc): # http://www.cplusplus.com/reference/cwchar/wchar_t/