Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experiment: allow nimony compile projects outside compiler folder #466

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bin/
nimcache/
nimblecache/
htmldocs/
Expand Down
4 changes: 2 additions & 2 deletions src/gear2/bridge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ proc closeAll*(r: var RContext) =
proc openNifModule*(r: var RContext; modname: string) =
if r.modules.hasKey(modname): return
if r.thisModule.len == 0: r.thisModule = modname
let filename = "nifcache/" & modname & ".nif"
let filename = modname & ".nif"
r.modules[modname] = RModule(
index: readIndex("nifcache/" & modname & ".idx.nif"),
index: readIndex(modname & ".idx.nif"),
s: nifstreams.open(filename)
)

Expand Down
6 changes: 2 additions & 4 deletions src/gear2/gear2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ proc moduleSuffix(conf: ConfigRef; fileIdx: FileIndex): string =
moduleSuffix(toFullPath(conf, fileIdx))

proc toNifFile(conf: ConfigRef; fileIdx: FileIndex): string =
let dest = moduleSuffix(toFullPath(conf, fileIdx))
result = "nifcache" / dest
result = moduleSuffix(toFullPath(conf, fileIdx))

proc importPipelineModule2(graph: ModuleGraph; s: PSym, fileIdx: FileIndex): PSym =
# this is called by the semantic checking phase
Expand Down Expand Up @@ -181,12 +180,11 @@ proc commandCheck(graph: ModuleGraph) =
elif conf.backend == backendJs:
setTarget(conf.target, osJS, cpuJS)
setPipeLinePass(graph, SemPass)
createDir "nifcache"
let module = compilePipelineProject2(graph)
when defined(debug):
echo renderTree(module.ast)
#let dest = moduleSuffix(toFullPath(conf, module.fileIdx))
#toNif(conf, module.ast, "nifcache" / dest.addFileExt".nif")
#toNif(conf, module.ast, dest.addFileExt".nif")

proc processCmdLine(pass: TCmdLinePass, cmd: string; config: ConfigRef) =
var p = parseopt.initOptParser(cmd)
Expand Down
77 changes: 43 additions & 34 deletions src/hastur.nim
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ proc markersToCmdLine(s: seq[LineInfo]): string =
for x in items(s):
result.add " --track:" & $x.line & ":" & $x.col & ":" & x.filename

proc execLocal(exe, cmd: string): (string, int) =
let bin = "bin" / exe.addFileExt(ExeExt)
proc execLocal(exe, cmd: string, binFolder = true): (string, int) =
var bin = exe.addFileExt(ExeExt)
if binFolder: bin = "bin" / bin
result = osproc.execCmdEx(bin & " " & cmd)

type
Expand Down Expand Up @@ -278,8 +279,8 @@ proc exec(cmd: string; showProgress = false) =
if exitCode != 0:
quit "FAILURE " & cmd & "\n" & s

proc exec(exe, cmd: string) =
let (s, exitCode) = execLocal(exe, cmd)
proc exec(exe, cmd: string, binFolder = true) =
let (s, exitCode) = execLocal(exe, cmd, binFolder)
if exitCode != 0:
quit "FAILURE " & cmd & "\n" & s

Expand Down Expand Up @@ -363,42 +364,50 @@ proc buildHexer(showProgress = false) =
robustMoveFile "src/hexer/" & exe, binDir() / exe

proc execNifc(cmd: string) =
exec "nifc", cmd
exec "../bin/nifc", cmd, binFolder=false

proc execHexer(cmd: string) =
exec "hexer", cmd
exec "../bin/hexer", cmd, binFolder=false

proc nifctests(overwrite: bool) =
let t1 = "tests/nifc/selectany/t1.nif"
let t2 = "tests/nifc/selectany/t2.nif"
let t3 = "tests/nifc/selectany/t3.nif"
execNifc " c -r " & t1 & " " & t2 & " " & t3
let app = "tests/nifc/app.c.nif"
execNifc " c -r " & app

let hello = "tests/nifc/hello.nif"
execNifc " c -r " & hello
execNifc " c -r --opt:speed " & hello
execNifc " c -r --opt:size " & hello
# TEST CPP
execNifc " cpp -r " & hello
execNifc " cpp -r --opt:speed " & hello

let tryIssues = "tests/nifc/try.nif"
execNifc " cpp -r " & tryIssues

let issues = "tests/nifc/issues.nif"
execNifc " c -r --linedir:on " & issues
execNifc " cpp -r --linedir:off " & issues
let root = getCurrentDir()
setCurrentDir(root / "nifcache")
block testing:
let t1 = "../tests/nifc/selectany/t1.nif"
let t2 = "../tests/nifc/selectany/t2.nif"
let t3 = "../tests/nifc/selectany/t3.nif"
execNifc " c -r " & t1 & " " & t2 & " " & t3
let app = "../tests/nifc/app.c.nif"
execNifc " c -r " & app

let hello = "../tests/nifc/hello.nif"
execNifc " c -r " & hello
execNifc " c -r --opt:speed " & hello
execNifc " c -r --opt:size " & hello
# TEST CPP
execNifc " cpp -r " & hello
execNifc " cpp -r --opt:speed " & hello

let tryIssues = "../tests/nifc/try.nif"
execNifc " cpp -r " & tryIssues

let issues = "../tests/nifc/issues.nif"
execNifc " c -r --linedir:on " & issues
execNifc " cpp -r --linedir:off " & issues
setCurrentDir(root)

proc hexertests(overwrite: bool) =
let mod1 = "tests/hexer/mod1"
let helloworld = "tests/hexer/hexer_helloworld"
createIndex helloworld & ".nif", false
createIndex mod1 & ".nif", false
execHexer mod1 & ".nif"
execHexer helloworld & ".nif"
execNifc " c -r " & mod1 & ".c.nif " & helloworld & ".c.nif"
let root = getCurrentDir()
setCurrentDir(root / "nifcache")
block testing:
let mod1 = "../tests/hexer/mod1"
let helloworld = "../tests/hexer/hexer_helloworld"
createIndex helloworld & ".nif", false
createIndex mod1 & ".nif", false
execHexer mod1 & ".nif"
execHexer helloworld & ".nif"
execNifc " c -r " & mod1 & ".c.nif " & helloworld & ".c.nif"
setCurrentDir(root)

proc handleCmdLine =
var primaryCmd = ""
Expand Down
4 changes: 2 additions & 2 deletions src/nifc/nifc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ proc handleCmdLine() =
s.config.cCompiler = ccCLang
else:
s.config.cCompiler = ccGcc
s.config.nifcacheDir = "nifcache"

for kind, key, val in getopt():
case kind
Expand Down Expand Up @@ -153,7 +152,8 @@ proc handleCmdLine() =
else: writeHelp()
of cmdEnd: assert false, "cannot happen"

createDir(s.config.nifcacheDir)
if len(s.config.nifcacheDir) > 0:
createDir(s.config.nifcacheDir)
if actionTable.len != 0:
for action in actionTable.keys:
case action
Expand Down
42 changes: 22 additions & 20 deletions src/nimony/deps.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ type
nimFile: string
modname: string

proc indexFile(f: FilePair): string = "nifcache" / f.modname & ".2.idx.nif"
proc parsedFile(f: FilePair): string = "nifcache" / f.modname & ".1.nif"
proc depsFile(f: FilePair): string = "nifcache" / f.modname & ".1.deps.nif"
proc semmedFile(f: FilePair): string = "nifcache" / f.modname & ".2.nif"
proc nifcFile(f: FilePair): string = "nifcache" / f.modname & ".c.nif"
proc cFile(f: FilePair): string = "nifcache" / f.modname & ".c"
proc objFile(f: FilePair): string = "nifcache" / f.modname & ".o"
proc indexFile(f: FilePair): string = f.modname & ".2.idx.nif"
proc parsedFile(f: FilePair): string = f.modname & ".1.nif"
proc depsFile(f: FilePair): string = f.modname & ".1.deps.nif"
proc semmedFile(f: FilePair): string = f.modname & ".2.nif"
proc nifcFile(f: FilePair): string = f.modname & ".c.nif"
proc cFile(f: FilePair): string = f.modname & ".c"
proc objFile(f: FilePair): string = f.modname & ".o"

# It turned out to be too annoying in practice to have the exe file in
# the current directory per default so we now put it into the nifcache too:
proc exeFile(f: FilePair): string = "nifcache" / f.modname.addFileExt ExeExt
proc exeFile(f: FilePair): string = f.modname.addFileExt ExeExt

proc resolveFileWrapper(paths: openArray[string]; origin: string; toResolve: string): string =
result = resolveFile(paths, origin, toResolve)
Expand Down Expand Up @@ -202,7 +202,9 @@ proc execNifler(c: var DepContext; input, output: string) =
exec cmd

proc importSystem(c: var DepContext; current: Node) =
let p = c.toPair(stdlibFile("std/system.nim"))
var p = c.toPair(stdlibFile("std/system.nim"))
p.nimFile = toRelativePath(p.nimFile, c.config.nifcachePath)
# add system module
current.deps.add p
if not c.processedModules.containsOrIncl(p.modname):
#echo "NIFLING ", p.nimFile, " -> ", parsedFile(p)
Expand Down Expand Up @@ -252,9 +254,8 @@ type
name, obj, customArgs: string

proc rootPath(c: DepContext): string =
# XXX: makefile is executed parent to nifcachePath
result = absoluteParentDir(c.rootNode.files[0].nimFile)
result = relativePath(result, parentDir c.config.nifcachePath)
result = relativePath(result, c.config.nifcachePath)

proc toBuildList(c: DepContext): seq[CFile] =
result = @[]
Expand Down Expand Up @@ -290,7 +291,7 @@ proc generateFinalMakefile(c: DepContext; passC, passL: string): string =
s.add "\n" & mescape(exeFile(c.rootNode.files[0])) & ":"

for cfile in buildList:
s.add " " & mescape("nifcache" / cfile.obj)
s.add " " & mescape(cfile.obj)

for v in c.nodes:
s.add " " & mescape(objFile(v.files[0]))
Expand All @@ -299,26 +300,26 @@ proc generateFinalMakefile(c: DepContext; passC, passL: string): string =
s.add " " & mescape(passL)

for cfile in buildList:
s.add "\n" & mescape("nifcache" / cfile.obj) & ": " & mescape(cfile.name) &
s.add "\n" & mescape(cfile.obj) & ": " & mescape(cfile.name) &
"\n\t$(CC) -c $(CFLAGS) $(CPPFLAGS) " &
mescape(cfile.customArgs) & " $< -o $@"

# The .o files depend on all of their .c files:
s.add "\n%.o: %.c\n\t$(CC) -c $(CFLAGS) -I$(ROOT_PATH) $(CPPFLAGS) $< -o $@"

# entry point is special:
let nifc = findTool("nifc")
let nifc = findTool("nifc", c.config)
s.add "\n" & mescape(cFile(c.rootNode.files[0])) & ": " & mescape(nifcFile c.rootNode.files[0])
s.add "\n\t" & mescape(nifc) & " c --compileOnly --isMain $<"

# The .c files depend on their .c.nif files:
s.add "\n%.c: %.c.nif\n\t" & mescape(nifc) & " c --compileOnly $<"

# The .c.nif files depend on all of their .2.nif files:
let hexer = findTool("hexer")
let hexer = findTool("hexer", c.config)
s.add "\n%.c.nif: %.2.nif %.2.idx.nif\n\t" & mescape(hexer) & " --bits:" & $c.config.bits & " $<"

result = "nifcache" / c.rootNode.files[0].modname & ".final.makefile"
result = c.rootNode.files[0].modname & ".final.makefile"
writeFile result, s

proc generateFrontendMakefile(c: DepContext; commandLineArgs: string): string =
Expand Down Expand Up @@ -352,21 +353,22 @@ proc generateFrontendMakefile(c: DepContext; commandLineArgs: string): string =
s.add "\n\t" & mescape(c.nifler) & " --portablePaths --deps parse " & mescape(nimFile) & " " &
mescape(f)

result = "nifcache" / c.rootNode.files[0].modname & ".makefile"
result = c.rootNode.files[0].modname & ".makefile"
writeFile result, s

proc buildGraph*(config: sink NifConfig; project: string; forceRebuild, silentMake: bool;
commandLineArgs: string; moduleFlags: set[ModuleFlag]; cmd: Command; passC, passL: string) =
let nifler = findTool("nifler")
let nifler = findTool("nifler", config)
let nimsem = findTool("nimsem", config)

if config.compat:
let cfgNif = "nifcache" / moduleSuffix(project, []) & ".cfg.nif"
let cfgNif = moduleSuffix(project, []) & ".cfg.nif"
exec quoteShell(nifler) & " config " & quoteShell(project) & " " &
quoteShell(cfgNif)
parseNifConfig cfgNif, config

var c = DepContext(nifler: nifler, config: config, rootNode: nil, includeStack: @[],
forceRebuild: forceRebuild, moduleFlags: moduleFlags, nimsem: findTool("nimsem"),
forceRebuild: forceRebuild, moduleFlags: moduleFlags, nimsem: nimsem,
cmd: cmd)
let p = c.toPair(project)
c.rootNode = Node(files: @[p], id: 0, parent: -1, active: 0, isSystem: IsSystem in moduleFlags)
Expand Down
34 changes: 28 additions & 6 deletions src/nimony/nimony.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Options:
-p, --path:PATH add PATH to the search path
-f, --forcebuild force a rebuild
-r, --run also run the compiled program
--nifcache:PATH set nifcache workspace path
--compat turn on compatibility mode
--noenv do not read configuration from `NIM_*`
environment variables
Expand All @@ -47,8 +48,8 @@ proc processSingleModule(nimFile: string; config: sink NifConfig; moduleFlags: s
commandLineArgs: string; forceRebuild: bool) =
let nifler = findTool("nifler")
let name = moduleSuffix(nimFile, config.paths)
let src = "nifcache" / name & ".1.nif"
let dest = "nifcache" / name & ".2.nif"
let src = name & ".1.nif"
let dest = name & ".2.nif"
let toforceRebuild = if forceRebuild: " -f " else: ""
exec quoteShell(nifler) & " --portablePaths p " & toforceRebuild & quoteShell(nimFile) & " " &
quoteShell(src)
Expand All @@ -67,8 +68,6 @@ proc handleCmdLine() =
var doRun = false
var moduleFlags: set[ModuleFlag] = {}
var config = NifConfig()
# XXX: harcoded relative nifcache path for now
config.nifcachePath = toAbsolutePath("nifcache")
config.defines.incl "nimony"
config.bits = sizeof(int)*8
var commandLineArgs = ""
Expand Down Expand Up @@ -101,6 +100,9 @@ proc handleCmdLine() =
of "compat": config.compat = true
of "path", "p": config.paths.add val
of "define", "d": config.defines.incl val
of "nifcache":
config.nifcachePath = toAbsolutePath(val)
forwardArg = false
of "noenv": useEnv = false
of "nosystem": moduleFlags.incl SkipSystem
of "issystem":
Expand Down Expand Up @@ -135,34 +137,54 @@ proc handleCmdLine() =
commandLineArgs.add ":" & quoteShell(val)

of cmdEnd: assert false, "cannot happen"

if args.len == 0:
quit "too few command line arguments"
elif args.len > 2 - int(cmd == FullProject):
quit "too many command line arguments"

# configure default nifcache path
if len(config.nifcachePath) == 0:
if isChild: config.nifcachePath = getCurrentDir()
else: config.nifcachePath = toAbsolutePath("nifcache")
# make main file relative to nifcache
args[0] = absolutePath(args[0]).normalizedPath()
.relativePath(config.nifcachePath)

if useEnv:
let nimPath = getEnv("NIMPATH")
for entry in split(nimPath, PathSep):
if entry.strip != "":
config.paths.add entry
# fundamental compiler path
config.paths.add compilerDir()

case cmd
of None:
quit "command missing"
of SingleModule:
if not isChild:
createDir("nifcache")
createDir(binDir())
createDir(config.nifcachePath)
# prepare required tools
setCurrentDir(compilerDir())
requiresTool "nifler", "src/nifler/nifler.nim", forceRebuild
requiresTool "nifc", "src/nifc/nifc.nim", forceRebuild
setCurrentDir(config.nifcachePath)
processSingleModule(args[0].addFileExt(".nim"), config, moduleFlags,
commandLineArgs, forceRebuild)
of FullProject:
createDir("nifcache")
createDir(binDir())
createDir(config.nifcachePath)
# prepare required tools
setCurrentDir(compilerDir())
exec "git submodule update --init"
requiresTool "nifler", "src/nifler/nifler.nim", forceRebuild
requiresTool "nimsem", "src/nimony/nimsem.nim", forceRebuild
requiresTool "hexer", "src/hexer/hexer.nim", forceRebuild
requiresTool "nifc", "src/nifc/nifc.nim", forceRebuild
# compile full project modules
setCurrentDir(config.nifcachePath)
buildGraph config, args[0], forceRebuild, silentMake,
commandLineArgs, moduleFlags, (if doRun: DoRun else: DoCompile),
passC, passL
Expand Down
7 changes: 5 additions & 2 deletions src/nimony/nimsem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ proc handleCmdLine() =
var useEnv = true
var moduleFlags: set[ModuleFlag] = {}
var config = NifConfig()
# XXX: harcoded relative nifcache path for now
config.nifcachePath = toAbsolutePath("nifcache")
config.nifcachePath = getCurrentDir()
config.defines.incl "nimony"
config.bits = sizeof(int)*8
var commandLineArgs = ""
Expand Down Expand Up @@ -105,11 +104,15 @@ proc handleCmdLine() =
of cmdEnd: assert false, "cannot happen"
if args.len != 3:
quit "want exactly 3 command line arguments"

if useEnv:
let nimPath = getEnv("NIMPATH")
for entry in split(nimPath, PathSep):
if entry.strip != "":
config.paths.add entry
# fundamental compiler path
config.paths.add compilerDir()

case cmd
of None:
quit "command missing"
Expand Down
Loading
Loading