Skip to content

Commit 93cd98d

Browse files
authored
move tinyc to a separate repo and allow installing external dependencency (eg tinyc) from koch / library code (#13850)
* remove tinyc * installDeps * update tinyc paths
1 parent 6b9ffc7 commit 93cd98d

File tree

390 files changed

+80
-99388
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

390 files changed

+80
-99388
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ testament.db
6565
/tests/**/*.json
6666
/tests/**/*.js
6767
/csources
68-
dist/
68+
/dist/
6969

7070
# Private directories and files (IDEs)
7171
.*/

compiler/cgen.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,8 +1941,8 @@ proc writeModule(m: BModule, pending: bool) =
19411941
var code = genModule(m, cf)
19421942
if code != nil or m.config.symbolFiles != disabledSf:
19431943
when hasTinyCBackend:
1944-
if conf.cmd == cmdRun:
1945-
tccgen.compileCCode($code)
1944+
if m.config.cmd == cmdRun:
1945+
tccgen.compileCCode($code, m.config)
19461946
onExit()
19471947
return
19481948

compiler/main.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ proc mainCommand*(graph: ModuleGraph) =
200200
of "run":
201201
conf.cmd = cmdRun
202202
when hasTinyCBackend:
203-
extccomp.setCC("tcc")
203+
extccomp.setCC(conf, "tcc", unknownLineInfo)
204204
commandCompileToC(graph)
205205
else:
206206
rawMessage(conf, errGenerated, "'run' command not available; rebuild with -d:tinyc")

compiler/nim.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ proc handleCmdLine(cache: IdentCache; conf: ConfigRef) =
9898
if conf.errorCounter != 0: return
9999
when hasTinyCBackend:
100100
if conf.cmd == cmdRun:
101-
tccgen.run(conf.arguments)
101+
tccgen.run(conf, conf.arguments)
102102
if optRun in conf.globalOptions:
103103
var ex = quoteShell conf.absOutFile
104104
if conf.cmd == cmdCompileToJS:

compiler/tccgen.nim

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@
88
#
99

1010
import
11-
os, strutils, options, msgs, tinyc
11+
os, strutils, options, msgs, tinyc, lineinfos, sequtils
1212

13-
{.compile: "../tinyc/libtcc.c".}
13+
const tinyPrefix = "dist/nim-tinyc-archive".unixToNativePath
14+
const nimRoot = currentSourcePath.parentDir.parentDir
15+
const tinycRoot = nimRoot / tinyPrefix
16+
when not dirExists(tinycRoot):
17+
static: doAssert false, $(tinycRoot, "requires: ./koch installdeps tinyc")
18+
{.compile: tinycRoot / "tinyc/libtcc.c".}
19+
20+
var
21+
gConf: ConfigRef # ugly but can be cleaned up if this is revived
1422

1523
proc tinyCErrorHandler(closure: pointer, msg: cstring) {.cdecl.} =
16-
rawMessage(errGenerated, $msg)
24+
rawMessage(gConf, errGenerated, $msg)
1725

1826
proc initTinyCState: PccState =
1927
result = openCCState()
@@ -25,7 +33,7 @@ var
2533

2634
proc addFile(filename: string) =
2735
if addFile(gTinyC, filename) != 0'i32:
28-
rawMessage(errCannotOpenFile, filename)
36+
rawMessage(gConf, errCannotOpenFile, filename)
2937

3038
proc setupEnvironment =
3139
when defined(amd64):
@@ -35,42 +43,47 @@ proc setupEnvironment =
3543
when defined(linux):
3644
defineSymbol(gTinyC, "__linux__", nil)
3745
defineSymbol(gTinyC, "__linux", nil)
38-
var nimDir = getPrefixDir()
46+
47+
var nimDir = getPrefixDir(gConf).string
48+
var tinycRoot = nimDir / tinyPrefix
49+
let libpath = nimDir / "lib"
3950

4051
addIncludePath(gTinyC, libpath)
4152
when defined(windows):
42-
addSysincludePath(gTinyC, nimDir / "tinyc/win32/include")
43-
addSysincludePath(gTinyC, nimDir / "tinyc/include")
53+
addSysincludePath(gTinyC, tinycRoot / "tinyc/win32/include")
54+
addSysincludePath(gTinyC, tinycRoot / "tinyc/include")
4455
when defined(windows):
4556
defineSymbol(gTinyC, "_WIN32", nil)
4657
# we need Mingw's headers too:
47-
var gccbin = getConfigVar("gcc.path") % ["nim", nimDir]
58+
var gccbin = getConfigVar("gcc.path") % ["nim", tinycRoot]
4859
addSysincludePath(gTinyC, gccbin /../ "include")
49-
#addFile(nimDir / r"tinyc\win32\wincrt1.o")
50-
addFile(nimDir / r"tinyc\win32\alloca86.o")
51-
addFile(nimDir / r"tinyc\win32\chkstk.o")
52-
#addFile(nimDir / r"tinyc\win32\crt1.o")
60+
#addFile(tinycRoot / r"tinyc\win32\wincrt1.o")
61+
addFile(tinycRoot / r"tinyc\win32\alloca86.o")
62+
addFile(tinycRoot / r"tinyc\win32\chkstk.o")
63+
#addFile(tinycRoot / r"tinyc\win32\crt1.o")
5364

54-
#addFile(nimDir / r"tinyc\win32\dllcrt1.o")
55-
#addFile(nimDir / r"tinyc\win32\dllmain.o")
56-
addFile(nimDir / r"tinyc\win32\libtcc1.o")
65+
#addFile(tinycRoot / r"tinyc\win32\dllcrt1.o")
66+
#addFile(tinycRoot / r"tinyc\win32\dllmain.o")
67+
addFile(tinycRoot / r"tinyc\win32\libtcc1.o")
5768

58-
#addFile(nimDir / r"tinyc\win32\lib\crt1.c")
59-
#addFile(nimDir / r"tinyc\lib\libtcc1.c")
69+
#addFile(tinycRoot / r"tinyc\win32\lib\crt1.c")
70+
#addFile(tinycRoot / r"tinyc\lib\libtcc1.c")
6071
else:
6172
addSysincludePath(gTinyC, "/usr/include")
6273
when defined(amd64):
6374
addSysincludePath(gTinyC, "/usr/include/x86_64-linux-gnu")
6475

65-
proc compileCCode*(ccode: string) =
76+
proc compileCCode*(ccode: string, conf: ConfigRef) =
77+
gConf = conf
6678
if not libIncluded:
6779
libIncluded = true
6880
setupEnvironment()
6981
discard compileString(gTinyC, ccode)
7082

71-
proc run*(args: string) =
72-
var s = @[cstring(gProjectName)] & map(split(args), proc(x: string): cstring = cstring(x))
83+
proc run*(conf: ConfigRef, args: string) =
84+
doAssert gConf == conf
85+
var s = @[cstring(conf.projectName)] & map(split(args), proc(x: string): cstring = cstring(x))
7386
var err = tinyc.run(gTinyC, cint(s.len), cast[cstringArray](addr(s[0]))) != 0'i32
7487
closeCCState(gTinyC)
75-
if err: rawMessage(errExecutionOfProgramFailed, "")
88+
if err: rawMessage(conf, errUnknown, "")
7689

koch.nim

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import
3131
os, strutils, parseopt, osproc
3232

3333
import tools / kochdocs
34+
import tools / deps
3435

3536
const VersionAsString = system.NimVersion
3637

@@ -73,6 +74,7 @@ Commands for core developers:
7374
zip builds the installation zip package
7475
xz builds the installation tar.xz package
7576
testinstall test tar.xz package; Unix only!
77+
installdeps [options] installs external dependency (eg tinyc) to dist/
7678
tests [options] run the testsuite (run a subset of tests by
7779
specifying a category, e.g. `tests cat async`)
7880
temp options creates a temporary compiler for testing
@@ -120,25 +122,21 @@ proc copyExe(source, dest: string) =
120122

121123
const
122124
compileNimInst = "tools/niminst/niminst"
125+
distDir = "dist"
123126

124127
proc csource(args: string) =
125128
nimexec(("cc $1 -r $3 --var:version=$2 --var:mingw=none csource " &
126129
"--main:compiler/nim.nim compiler/installer.ini $1") %
127130
[args, VersionAsString, compileNimInst])
128131

129132
proc bundleC2nim(args: string) =
130-
if not dirExists("dist/c2nim/.git"):
131-
exec("git clone -q https://github.com/nim-lang/c2nim.git dist/c2nim")
133+
cloneDependency(distDir, "https://github.com/nim-lang/c2nim.git")
132134
nimCompile("dist/c2nim/c2nim",
133135
options = "--noNimblePath --path:. " & args)
134136

135137
proc bundleNimbleExe(latest: bool, args: string) =
136-
if not dirExists("dist/nimble/.git"):
137-
exec("git clone -q https://github.com/nim-lang/nimble.git dist/nimble")
138-
if not latest:
139-
withDir("dist/nimble"):
140-
exec("git fetch")
141-
exec("git checkout " & NimbleStableCommit)
138+
let commit = if latest: "HEAD" else: NimbleStableCommit
139+
cloneDependency(distDir, "https://github.com/nim-lang/nimble.git", commit = commit)
142140
# installer.ini expects it under $nim/bin
143141
nimCompile("dist/nimble/src/nimble.nim",
144142
options = "-d:release --nilseqs:on " & args)
@@ -156,6 +154,7 @@ proc buildNimble(latest: bool, args: string) =
156154
while dirExists("dist/nimble" & $id):
157155
inc id
158156
installDir = "dist/nimble" & $id
157+
# consider using/adapting cloneDependency
159158
exec("git clone -q https://github.com/nim-lang/nimble.git " & installDir)
160159
withDir(installDir):
161160
if latest:
@@ -504,6 +503,17 @@ proc hostInfo(): string =
504503
"hostOS: $1, hostCPU: $2, int: $3, float: $4, cpuEndian: $5, cwd: $6" %
505504
[hostOS, hostCPU, $int.sizeof, $float.sizeof, $cpuEndian, getCurrentDir()]
506505

506+
proc installDeps(dep: string, commit = "") =
507+
# the hashes/urls are version controlled here, so can be changed seamlessly
508+
# and tied to a nim release (mimicking git submodules)
509+
var commit = commit
510+
case dep
511+
of "tinyc":
512+
if commit.len == 0: commit = "916cc2f94818a8a382dd8d4b8420978816c1dfb3"
513+
cloneDependency(distDir, "https://github.com/timotheecour/nim-tinyc-archive", commit)
514+
else: doAssert false, "unsupported: " & dep
515+
# xxx: also add linenoise, niminst etc, refs https://github.com/nim-lang/RFCs/issues/206
516+
507517
proc runCI(cmd: string) =
508518
doAssert cmd.len == 0, cmd # avoid silently ignoring
509519
echo "runCI: ", cmd
@@ -648,6 +658,7 @@ when isMainModule:
648658
of "distrohelper": geninstall()
649659
of "install": install(op.cmdLineRest)
650660
of "testinstall": testUnixInstall(op.cmdLineRest)
661+
of "installdeps": installDeps(op.cmdLineRest)
651662
of "runci": runCI(op.cmdLineRest)
652663
of "test", "tests": tests(op.cmdLineRest)
653664
of "temp": temp(op.cmdLineRest)

tinyc/arm-asm.c

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)