Open data-man opened 3 years ago
Just the idea: a configuration file (c3c.toml
?) in some dir (~
, ~/.config/c3c
, etc.) with paths to system libs/files.
Zig actually provides its own musl-based linker libraries for cross compilation. I think this must be the goal, since making a linker work like this is not adding any extra capabilities: cross compilation still breaks.
What would pkgconf do?
I checked dev
branch now.
Is built-in lld
completely removed? :(
No, don't mind what's there for now, I need to experiment a bit with getting the right arguments. lld will return.
You can start looking at dev now: it should have what's necessary to cross compile to MSVC and MacOS. Using LLD also works fine.
Yes, thank you, I've already tested it.
Maybe use LLD by default and rename --forcelinker
to --externallinker
?
It will all get cleaned up.
--wincrt=<optoion>
->
--wincrt=<option>
Great, I'll fix that right away.
Perhaps static, dynamic
can be common options for all platforms?
Unfortunately not. MacOS for example does not support static linking of libc
Issue: the linking time in the --debug-stats
output still empty with --forcelinker
switch.
Feature request: use LLD by default and rename --forcelinker
to --externallinker
.
How does this work now? Is it better?
$ c3c compile --debug-stats something.c3
-- AST/EXPR/TYPE INFO --
* Ast size: 56 bytes
* Decl size: 136 bytes
* Expr size: 56 bytes
* TypeInfo size: 40 bytes
* Ast memory use: 203kb (3719 elements)
* Decl memory use: 349kb (2630 elements)
* Expr memory use: 1062kb (19437 elements)
* TypeInfo memory use: 123kb (3170 elements)
-- ARENA INFO --
* Memory used: 113614 Kb
* Allocations: 13118
* String memory used: 48 Kb
Program linked to executable 'sysclock3'.
Timings
-------
Initialization took: 2.7250 ms
Parsing took: 9.8670 ms
Analysis took: 3.8890 ms
Ir gen took: 16.9390 ms
Codegen took: 1222.0340 ms
Linking took: 0.117000 ms
$ c3c compile --debug-stats --forcelinker something.c3
-- AST/EXPR/TYPE INFO --
* Ast size: 56 bytes
* Decl size: 136 bytes
* Expr size: 56 bytes
* TypeInfo size: 40 bytes
* Ast memory use: 203kb (3719 elements)
* Decl memory use: 349kb (2630 elements)
* Expr memory use: 1062kb (19437 elements)
* TypeInfo memory use: 123kb (3170 elements)
-- ARENA INFO --
* Memory used: 113614 Kb
* Allocations: 13118
* String memory used: 48 Kb
Timings
-------
Initialization took: 1.3180 ms
Parsing took: 5.7270 ms
Analysis took: 2.3850 ms
Ir gen took: 19.6230 ms
Codegen took: 1186.6120 ms
Failed to find the C runtime at link time. <---
Do you know where libc is found for you?
@data-man can you see what possible paths there are to crt1.o and crtbegin.o? The simpler the better.
Sure:
/usr/lib/x86_64-linux-gnu/crt1.o
/usr/local/musl/lib/crt1.o
/usr/lib/gcc/x86_64-linux-gnu/11/crtbegin.o
/usr/lib/gcc/x86_64-linux-gnu/12/crtbegin.o
Please try the very latest version.
Sorry, In my previous message I omitted wasm and mingw paths as irrelevant, but ...
-- AST/EXPR/TYPE INFO --
* Ast size: 56 bytes
* Decl size: 136 bytes
* Expr size: 56 bytes
* TypeInfo size: 40 bytes
* Ast memory use: 205kb (3749 elements)
* Decl memory use: 350kb (2638 elements)
* Expr memory use: 1083kb (19810 elements)
* TypeInfo memory use: 124kb (3183 elements)
-- ARENA INFO --
* Memory used: 113622 Kb
* Allocations: 13272
* String memory used: 48 Kb
Timings
-------
Initialization took: 0.8010 ms
Parsing took: 4.8600 ms
Analysis took: 2.6860 ms
Ir gen took: 16.2730 ms
Codegen took: 1205.5990 ms
ld.lld: error: /usr/lib/wasm32-wasi/crt1.o: unknown file type
ld.lld: error: /usr/lib/gcc/i686-w64-mingw32/10-posix/crtbegin.o: unknown file type
ld.lld: error: cannot open /usr/lib/wasm32-wasi/crti.o: No such file or directory
ld.lld: error: /usr/lib/gcc/i686-w64-mingw32/10-posix/crtend.o: unknown file type
ld.lld: error: cannot open /usr/lib/wasm32-wasi/crtn.o: No such file or directory
ld.lld: warning: /usr/lib/wasm32-wasi/libc.a: archive member 'dlmalloc.o' is neither ET_REL nor LLVM bitcode
ld.lld: warning: /usr/lib/wasm32-wasi/libc.a: archive member 'write.o' is neither ET_REL nor LLVM bitcode
...
Failed to create an executable: (null)
Does this work now @data-man
$ c3c compile --target linux-x86 --forcelinker hello_world_many.c3
WARNING! This architecture is not supported.
ld.lld: error: /usr/lib/wasm32-wasi/crt1.o: unknown file type
ld.lld: error: /usr/lib/gcc/i686-w64-mingw32/12-posix/crtbegin.o: unknown file type
ld.lld: error: cannot open /usr/lib/wasm32-wasi/crti.o: No such file or directory
ld.lld: error: /usr/lib/gcc/i686-w64-mingw32/12-posix/crtend.o: unknown file type
ld.lld: error: cannot open /usr/lib/wasm32-wasi/crtn.o: No such file or directory
ld.lld: warning: /usr/lib/wasm32-wasi/libc.a: archive member 'dlmalloc.o' is neither ET_REL nor LLVM bitcode
ld.lld: warning: /usr/lib/wasm32-wasi/libc.a: archive member 'write.o' is neither ET_REL nor LLVM bitcode
ld.lld: warning: /usr/lib/wasm32-wasi/libc.a: archive member 'usleep.o' is neither ET_REL nor LLVM bitcode
ld.lld: warning: /usr/lib/wasm32-wasi/libc.a: archive member 'unlinkat.o' is neither ET_REL nor LLVM bitcode
...skipped
Failed to create an executable: (null)
Oh wait, it's x86 and not x64?
Ok so it accidentally grabs your wasm32 and mingw installs
Also musl library can be located in /usr/include/x86_64-linux-musl
directory if it installed by some system's package manager, or in /usr/local/musl/lib
if it installed manually with default make
options.
It's really difficult to add a good heuristic. You can look at "find_linux_crt" and "find_linux_crtbegin" on how this is done.
If we know that some particular variant of linux uses some fixed paths, then we could check for those first.
Some time ago I proposed use libpkgconf. It's small dependency (~256K). Now c3c contains miniz's sources. Can we discuss my proposal again? :)
Maybe make it optional, like mimalloc?
Ok, a few things:
1) I just added --linux-crt
and --linux-crtbegin
options. With those you can set the correct paths and override anything detected by default.
2) To me the endgame is that the compiler either (1) is able to create a single binary for Linux, or (2) to have compile options like "ubuntu20-x64" "ubuntu22-x64" "debian11-x64". pkgconf
helps with native builds but that's about it.
@data-man Can you test if --linux-crt
and --linux-crtbegin
resolves your issues?
I tried this (with musl libc in the same directory):
$ c3c compile hello_world_many.c3 --forcelinker --nolibc -L . -l libc (or libc.a)
> ld.lld: error: unable to find library -llibc
or
> ld.lld: error: unable to find library -llibc.a
But you're not setting --linux-crt and --linux-crtbegin?
NVM, sorry. -l c
should be used.
Can you try without --nolibc and just pointing to --linux-crt and --linux-crtbegin?
Package musl-dev has this:
musl-dev: /usr/lib/x86_64-linux-musl/Scrt1.o
musl-dev: /usr/lib/x86_64-linux-musl/crt1.o
musl-dev: /usr/lib/x86_64-linux-musl/crti.o
musl-dev: /usr/lib/x86_64-linux-musl/crtn.o
musl-dev: /usr/lib/x86_64-linux-musl/libc.a
musl-dev: /usr/lib/x86_64-linux-musl/libdl.a
musl-dev: /usr/lib/x86_64-linux-musl/libm.a
musl-dev: /usr/lib/x86_64-linux-musl/libpthread.a
musl-dev: /usr/lib/x86_64-linux-musl/libresolv.a
musl-dev: /usr/lib/x86_64-linux-musl/librt.a
musl-dev: /usr/lib/x86_64-linux-musl/libutil.a
musl-dev: /usr/lib/x86_64-linux-musl/libxnet.a
musl-dev: /usr/lib/x86_64-linux-musl/musl-gcc.specs
musl-dev: /usr/lib/x86_64-linux-musl/rcrt1.o
Is this working now?
I'm currently using this patch in my OS. It's bad, but it works.