diff options
| author | Ganesh Viswanathan <dev@genotrance.com> | 2019-10-10 07:56:47 -0500 |
|---|---|---|
| committer | Ganesh Viswanathan <dev@genotrance.com> | 2019-10-10 07:56:47 -0500 |
| commit | 6db698ea7a57a9fb9e4790ec768340a36b9e90e3 (patch) | |
| tree | 23054a82a84b45b10a1e2201980440176f3c0391 | |
| parent | f2a0d4b0c9ed29ad83f3cc87ad8f4f02bde34dee (diff) | |
| download | nimterop-6db698ea7a57a9fb9e4790ec768340a36b9e90e3.tar.gz nimterop-6db698ea7a57a9fb9e4790ec768340a36b9e90e3.zip | |
Move all artifacts to nimcache
| -rw-r--r-- | nimterop.nimble | 7 | ||||
| -rw-r--r-- | nimterop/build.nim | 40 | ||||
| -rw-r--r-- | nimterop/cimport.nim | 4 | ||||
| -rw-r--r-- | nimterop/paths.nim | 12 | ||||
| -rw-r--r-- | nimterop/setup.nim | 30 | ||||
| -rw-r--r-- | nimterop/treesitter/api.nim | 2 | ||||
| -rw-r--r-- | nimterop/treesitter/c.nim | 2 | ||||
| -rw-r--r-- | nimterop/treesitter/cpp.nim | 14 | ||||
| -rw-r--r-- | nimterop/treesitter/tsgen.nim | 2 | ||||
| -rw-r--r-- | tests/getheader.nims | 11 | ||||
| -rw-r--r-- | tests/lzma.nim | 2 | ||||
| -rw-r--r-- | tests/tpcre.nim | 4 | ||||
| -rw-r--r-- | tests/tsoloud.nim | 2 | ||||
| -rw-r--r-- | tests/zlib.nim | 2 |
14 files changed, 80 insertions, 54 deletions
diff --git a/nimterop.nimble b/nimterop.nimble index 5825f5a..8e27d8e 100644 --- a/nimterop.nimble +++ b/nimterop.nimble @@ -21,10 +21,13 @@ proc execCmd(cmd: string) = exec cmd proc execTest(test: string) = - execCmd "nim c -r " & test + execCmd "nim c -f -r " & test execCmd "nim cpp -r " & test task buildToast, "build toast": + execCmd("nim c -f -d:danger nimterop/toast.nim") + +task bt, "build toast": execCmd("nim c -d:danger nimterop/toast.nim") task docs, "Generate docs": @@ -34,7 +37,7 @@ task test, "Test": buildToastTask() execTest "tests/tnimterop_c.nim" - execCmd "nim cpp -r tests/tnimterop_cpp.nim" + execCmd "nim cpp -f -r tests/tnimterop_cpp.nim" execTest "tests/tpcre.nim" # Platform specific tests diff --git a/nimterop/build.nim b/nimterop/build.nim index 620f83e..91daee0 100644 --- a/nimterop/build.nim +++ b/nimterop/build.nim @@ -110,9 +110,45 @@ proc rmFile*(source: string, dir = false) = discard execAction(&"{cmd} {source.sanitizePath}", retry = 2) -proc rmDir*(source: string) = +proc rmDir*(dir: string) = ## Remove a directory or pattern at compile time - rmFile(source, dir = true) + rmFile(dir, dir = true) + +proc getOsCacheDir(): string = + when defined(posix): + result = getEnv("XDG_CACHE_HOME", getHomeDir() / ".cache") / "nim" + else: + result = getHomeDir() / "nimcache" + +proc getNimteropCacheDir(): string = + result = getOsCacheDir() / "nimterop" + +proc getProjectCacheDir*(name: string, forceClean = true): string = + ## Get a cache directory where all nimterop artifacts can be stored + ## + ## Projects can use this location to download source code and build binaries + ## that can be then accessed by multiple apps. This is created under the + ## per-user Nim cache directory. + ## + ## Use `name` to specify the subdirectory name for a project. + ## + ## `forceClean` is enabled by default and effectively deletes the folder + ## if Nim is compiled with the `-f` or `--forceBuild` flag. This allows + ## any project to start out with a clean cache dir on a forced build. + ## + ## NOTE: avoid calling `getProjectCacheDir()` multiple times on the same + ## `name` when `forceClean = true` else checked out source might get deleted + ## at the wrong time during build. + ## + ## E.g. + ## `nimgit2` downloads `libgit2` source so `name = "libgit2"` + ## + ## `nimarchive` downloads `libarchive`, `bzlib`, `liblzma` and `zlib` so + ## `name = "nimarchive" / "libarchive"` for `libarchive`, etc. + result = getNimteropCacheDir() / name + + if forceClean and compileOption("forceBuild"): + rmDir(result) proc extractZip*(zipfile, outdir: string) = ## Extract a zip file using `powershell` on Windows and `unzip` on other diff --git a/nimterop/cimport.nim b/nimterop/cimport.nim index b295344..4380311 100644 --- a/nimterop/cimport.nim +++ b/nimterop/cimport.nim @@ -107,12 +107,12 @@ proc getNimCheckError(output: string): tuple[tmpFile, errors: string] = let hash = output.hash().abs() - result.tmpFile = getTempDir() / "nimterop_" & $hash & ".nim" + result.tmpFile = getProjectCacheDir("cPlugins", forceClean = false) / "nimterop_" & $hash & ".nim" if not fileExists(result.tmpFile) or gStateCT.nocache or compileOption("forceBuild"): writeFile(result.tmpFile, output) - doAssert fileExists(result.tmpFile), "Bad codegen - unable to write to TEMP: " & result.tmpFile + doAssert fileExists(result.tmpFile), "Failed to write to cache dir: " & result.tmpFile let nim = diff --git a/nimterop/paths.nim b/nimterop/paths.nim index 5a2bb73..d55276e 100644 --- a/nimterop/paths.nim +++ b/nimterop/paths.nim @@ -1,21 +1,19 @@ import os +import "."/build + +const + cacheDir* = getProjectCacheDir("nimterop") + proc nimteropRoot*(): string = currentSourcePath.parentDir.parentDir -proc nimteropBuildDir*(): string = - ## all nimterop generated files go under here (gitignored) - nimteropRoot() / "build" - proc nimteropSrcDir*(): string = nimteropRoot() / "nimterop" proc toastExePath*(): string = nimteropSrcDir() / ("toast".addFileExt ExeExt) -proc incDir*(): string = - nimteropBuildDir() / "inc" - proc testsIncludeDir*(): string = nimteropRoot() / "tests" / "include" diff --git a/nimterop/setup.nim b/nimterop/setup.nim index 4ee375b..3f16dd7 100644 --- a/nimterop/setup.nim +++ b/nimterop/setup.nim @@ -3,24 +3,24 @@ import os, strutils import "."/[build, paths] proc treesitterSetup*() = - gitPull("https://github.com/tree-sitter/tree-sitter", incDir() / "treesitter", """ + gitPull("https://github.com/tree-sitter/tree-sitter", cacheDir / "treesitter", """ lib/include/* lib/src/* """, "0.15.5") - gitPull("https://github.com/JuliaStrings/utf8proc", incDir() / "utf8proc", """ + gitPull("https://github.com/JuliaStrings/utf8proc", cacheDir / "utf8proc", """ *.c *.h """) let - tbase = incDir() / "treesitter/lib" - stack = tbase / "src/stack.c" - parser = tbase / "include/tree_sitter/parser.h" + tbase = cacheDir / "treesitter" / "lib" + stack = tbase / "src" / "stack.c" + parser = tbase / "include" / "tree_sitter" / "parser.h" tparser = parser.replace("parser", "tparser") - language = tbase / "src/language.h" - lexer = tbase / "src/lexer.h" - subtree = tbase / "src/subtree.h" + language = tbase / "src" / "language.h" + lexer = tbase / "src" / "lexer.h" + subtree = tbase / "src" / "subtree.h" stack.writeFile(stack.readFile().replace("inline Stack", "Stack")) @@ -31,31 +31,25 @@ lib/src/* subtree.writeFile(subtree.readFile().replace("parser.h", "tparser.h")) proc treesitterCSetup*() = - gitPull("https://github.com/tree-sitter/tree-sitter-c", incDir() / "treesitter_c", """ + gitPull("https://github.com/tree-sitter/tree-sitter-c", cacheDir / "treesitter_c", """ src/*.h src/*.c src/*.cc src/tree_sitter/parser.h """, "v0.15.0") - let - headerc = incDir() / "treesitter_c/src/api.h" - - headerc.writeFile(""" + writeFile(cacheDir / "treesitter_c" / "src" / "api.h", """ const TSLanguage *tree_sitter_c(); """) proc treesitterCppSetup*() = - gitPull("https://github.com/tree-sitter/tree-sitter-cpp", incDir() / "treesitter_cpp", """ + gitPull("https://github.com/tree-sitter/tree-sitter-cpp", cacheDir / "treesitter_cpp", """ src/*.h src/*.c src/*.cc src/tree_sitter/parser.h """, "v0.15.0") - let - headercpp = incDir() / "treesitter_cpp/src/api.h" - - headercpp.writeFile(""" + writeFile(cacheDir / "treesitter_cpp" / "src" / "api.h", """ const TSLanguage *tree_sitter_cpp(); """) diff --git a/nimterop/treesitter/api.nim b/nimterop/treesitter/api.nim index 2c11a60..09e328d 100644 --- a/nimterop/treesitter/api.nim +++ b/nimterop/treesitter/api.nim @@ -7,7 +7,7 @@ import ".."/[setup, paths, types] static: treesitterSetup() -const sourcePath = incDir() / "treesitter" / "lib" +const sourcePath = cacheDir / "treesitter" / "lib" when defined(Linux): {.passC: "-std=c11".} diff --git a/nimterop/treesitter/c.nim b/nimterop/treesitter/c.nim index 38fa003..d779bfd 100644 --- a/nimterop/treesitter/c.nim +++ b/nimterop/treesitter/c.nim @@ -5,7 +5,7 @@ import ".."/[setup, paths] static: treesitterCSetup() -const srcDir = incDir() / "treesitter_c" / "src" +const srcDir = cacheDir / "treesitter_c" / "src" import "."/api diff --git a/nimterop/treesitter/cpp.nim b/nimterop/treesitter/cpp.nim index 131e57f..2fe3128 100644 --- a/nimterop/treesitter/cpp.nim +++ b/nimterop/treesitter/cpp.nim @@ -1,24 +1,20 @@ import strutils, os -import ".."/[setup, paths] +import ".."/[build, setup, paths] static: treesitterCppSetup() -const srcDir = incDir() / "treesitter_cpp" / "src" +const srcDir = cacheDir / "treesitter_cpp" / "src" {.passC: "-I$1" % srcDir.} import "."/api -when (NimMajor, NimMinor, NimPatch) < (0, 19, 9): - const srcDirRel = "../../build/inc/treesitter_cpp/src" -else: - const srcDirRel = srcDir.relativePath(currentSourcePath.parentDir) +static: + cpFile(srcDir / "parser.c", srcDir / "parser_cpp.c") -# pending https://github.com/nim-lang/Nim/issues/9370 we need srcDirRel instead -# of srcDir -{.compile: (srcDirRel / "parser.c", "nimtero_cpp_parser.c.o").} +{.compile: srcDir / "parser_cpp.c".} {.compile: srcDir / "scanner.cc".} proc treeSitterCpp*(): ptr TSLanguage {.importc: "tree_sitter_cpp", header: srcDir / "api.h".} diff --git a/nimterop/treesitter/tsgen.nim b/nimterop/treesitter/tsgen.nim index 335f28f..484483c 100644 --- a/nimterop/treesitter/tsgen.nim +++ b/nimterop/treesitter/tsgen.nim @@ -15,4 +15,4 @@ cPlugin: static: cDebug() -cImport(incDir()/"treesitter/lib/include/tree_sitter/api.h")
\ No newline at end of file +cImport(cacheDir / "treesitter" /"lib" / "include" / "tree_sitter" / "api.h") diff --git a/tests/getheader.nims b/tests/getheader.nims index c122484..5a08429 100644 --- a/tests/getheader.nims +++ b/tests/getheader.nims @@ -1,16 +1,17 @@ import strutils proc testCall(cmd, output: string, exitCode: int, delete = true) = - if delete: - rmDir("build/liblzma") - rmDir("build/zlib") - echo cmd var ccmd = when defined(windows): "cmd /c " & cmd else: cmd + + if not delete: + ccmd = ccmd.replace(" -f ", " ") + + var (outp, exitC) = gorgeEx(ccmd) echo outp doAssert exitC == exitCode, $exitC @@ -41,7 +42,6 @@ when defined(posix): # git tag testCall(cmd & " -d:lzmaGit -d:lzmaSetVer=v5.2.0" & lrcmd, lexp & "5.2.0", 0) testCall(cmd & " -d:lzmaGit -d:lzmaStatic -d:lzmaSetVer=v5.2.0" & lrcmd, lexp & "5.2.0", 0, delete = false) - testCall("cd build/liblzma && git branch", "v5.2.0", 0, delete = false) # git testCall(cmd & " -d:envTest" & zrcmd, zexp, 0) @@ -50,7 +50,6 @@ testCall(cmd & " -d:envTestStatic" & zrcmd, zexp, 0, delete = false) # git tag testCall(cmd & " -d:zlibGit -d:zlibSetVer=v1.2.10" & zrcmd, zexp & "1.2.10", 0) testCall(cmd & " -d:zlibGit -d:zlibStatic -d:zlibSetVer=v1.2.10" & zrcmd, zexp & "1.2.10", 0, delete = false) -testCall("cd build/zlib && git branch", "v1.2.10", 0, delete = false) # dl testCall(cmd & " -d:lzmaDL" & lrcmd, "Need version", 1) diff --git a/tests/lzma.nim b/tests/lzma.nim index 2368b55..785e0bb 100644 --- a/tests/lzma.nim +++ b/tests/lzma.nim @@ -3,7 +3,7 @@ import os, strutils import nimterop/[build, cimport] const - baseDir = currentSourcePath.parentDir()/"build"/"liblzma" + baseDir = getProjectCacheDir("nimterop" / "tests" / "liblzma") static: cDebug() diff --git a/tests/tpcre.nim b/tests/tpcre.nim index 5588850..5bc97da 100644 --- a/tests/tpcre.nim +++ b/tests/tpcre.nim @@ -3,7 +3,7 @@ import os import nimterop/[cimport, build, paths] const - baseDir = nimteropBuildDir()/"pcre" + baseDir = getProjectCacheDir("nimterop" / "tests" / "pcre") pcreH = baseDir/"pcre.h.in" static: @@ -32,4 +32,4 @@ cPlugin: cImport(pcreH, dynlib="dynpcre") -echo version()
\ No newline at end of file +echo version() diff --git a/tests/tsoloud.nim b/tests/tsoloud.nim index 12966a8..9281236 100644 --- a/tests/tsoloud.nim +++ b/tests/tsoloud.nim @@ -1,7 +1,7 @@ import os, nimterop/[cimport, build, paths] const - baseDir = nimteropBuildDir()/"soloud" + baseDir = getProjectCacheDir("nimterop" / "tests" / "soloud") incl = baseDir/"include" src = baseDir/"src" diff --git a/tests/zlib.nim b/tests/zlib.nim index 371d41f..572580c 100644 --- a/tests/zlib.nim +++ b/tests/zlib.nim @@ -3,7 +3,7 @@ import os, strutils import nimterop/[build, cimport] const - baseDir = currentSourcePath.parentDir()/"build"/"zlib" + baseDir = getProjectCacheDir("nimterop" / "tests" / "zlib") static: cDebug() |
