# -------------- post unzip steps --------------------------------------------- import strutils, os, osproc, streams, browsers const arch = $(sizeof(int)*8) mingw = "mingw$1.7z" % arch url = r"https://nim-lang.org/download/" & mingw var interactive = true type DownloadResult = enum Failure, Manual, Success proc expand(s: string): string = try: result = expandFilename(s) except OSError, IOError: result = s proc unzip(): bool = if not fileExists("dist" / mingw): echo "Could not find ", "dist" / mingw return false try: let p = osproc.startProcess(r"bin\7zG.exe", getCurrentDir() / r"dist", ["x", mingw]) if p.waitForExit != 0: echo "Unpacking failed: " & mingw else: result = true except: result = false proc downloadMingw(): DownloadResult = let curl = findExe"curl" var cmd: string if curl.len > 0: cmd = quoteShell(curl) & " --output " & "dist" / mingw & " " & url elif fileExists"bin/nimgrab.exe": cmd = r"bin\nimgrab.exe " & url & " dist" / mingw if cmd.len > 0: if execShellCmd(cmd) != 0: echo "download failed! ", cmd if interactive: openDefaultBrowser(url) result = Manual else: result = Failure else: if unzip(): result = Success else: if interactive: openDefaultBrowser(url) result = Manual else: result = Failure when defined(windows): import registry proc askBool(m: string): bool = stdout.write m if not interactive: stdout.writeLine "y (non-interactive mode)" return true while true: try: let answer = stdin.readLine().normalize case answer of "y", "yes": return true of "n", "no": return false else: echo "Please type 'y' or 'n'" except EOFError: quit(1) proc askNumber(m: string; a, b: int): int = stdout.write m stdout.write " [" & $a & ".." & $b & "] " if not interactive: stdout.writeLine $a & " (non-interactive mode)" return a while true: let answer = stdin.readLine() try: result = parseInt answer if result < a or result > b: raise newException(ValueError, "number out of range") break except ValueError: echo "Please type in a number between ", a, " and ", b proc patchConfig(mingw: string) = const cfgFile = "config/nim.cfg" lookFor = """#gcc.path = r"$nim\dist\mingw\bin"""" replacePattern = """gcc.path = r"$1"""" try: let cfg = readFile(cfgFile) let newCfg = cfg.replace(lookFor, replacePattern % mingw) if newCfg == cfg: echo "Could not patch 'config/nim.cfg' [Error]" echo "Reason: patch substring not found:" echo lookFor else: writeFile(cfgFile, newCfg) except IOError: echo "Could not access 'config/nim.cfg' [Error]" proc tryGetUnicodeValue(path, key: string; handle: HKEY): string = try: result = getUnicodeValue(path, key, handle) except: result = "" proc addToPathEnv*(e: string) = var p = tryGetUnicodeValue(r"Environment", "Path", HKEY_CURRENT_USER) if p.len > 0: p.add ";" p.add e else: p = e setUnicodeValue(r"Environment", "Path", p, HKEY_CURRENT_USER) proc createShortcut(src, dest: string; icon = "") = var cmd = "bin\\makelink.exe \"" & src & "\" \"\" \"" & dest & ".lnk\" \"\" 1 \"" & splitFile(src).dir & "\"" if icon.len != 0: cmd.add " \"" & icon & "\" 0" discard execShellCmd(cmd) proc createStartMenuEntry*(override = false) = let appdata = getEnv("APPDATA") if appdata.len == 0: return let dest = appdata & r"\Microsoft\Windows\Start Menu\Programs\Nim-" & NimVersion if dirExists(dest): return if override or askBool("Would like to add Nim-" & NimVersion & " to your start menu? (y/n) "): createDir(dest) createShortcut(getCurrentDir() / "tools" / "start.bat", dest / "Nim", getCurrentDir() / r"icons\nim.ico") if fileExists("doc/overview.html"): createShortcut(getCurrentDir() / "doc" / "html" / "overview.html", dest / "Overview") if dirExists(r"dist\aporia-0.4.0"): createShortcut(getCurrentDir() / r"dist\aporia-0.4.0\bin\aporia.exe", dest / "Aporia") proc checkGccArch(mingw: string): bool = let gccExe = mingw / r"gcc.exe" if fileExists(gccExe): const nimCompat = "nim_compat.c" writeFile(nimCompat, """typedef int Nim_and_C_compiler_disagree_on_target_architecture[ $# == sizeof(void*) ? 1 : -1]; """ % $sizeof(int)) try: let p = startProcess(gccExe, "", ["-c", nimCompat], nil, {poStdErrToStdOut, poUsePath}) #echo p.outputStream.readAll() result = p.waitForExit() == 0 except OSError, IOError: result = false finally: removeFile(nimCompat) removeFile(nimCompat.changeFileExt("o")) proc defaultMingwLocations(): seq[string] = proc probeDir(dir: string; result: var seq[string]) = for k, x in walkDir(dir, relative=true): if k in {pcDir, pcLinkToDir}: if x.contains("mingw") or x.contains("posix"): let dest = dir / x probeDir(dest, result) result.add(dest) result = @["dist/mingw", ".
#
#
#