Could I use fbc -gen gcc with clang as the compiler?

New to FreeBASIC? Post your questions here.
systemctl
Posts: 182
Joined: Mar 27, 2020 5:15

Could I use fbc -gen gcc with clang as the compiler?

Postby systemctl » May 02, 2020 18:09

Well, clang used almost the same command line options as gcc. Did anyone tried it?
srvaldez
Posts: 2483
Joined: Sep 25, 2005 21:54

Re: Could I use fbc -gen gcc with clang as the compiler?

Postby srvaldez » May 03, 2020 11:30

if your OS is Linux, then install clang if not already installed, then instead of -gen gcc use -gen llvm but a lot of programs will fail to compile
on Windows you could do the same if you use the msys2 toolchain, you would need to install clang
pacman -S mingw-w64-i686-clang
pacman -S mingw-w64-x86_64-clang
I don't know whether FB would build using clang, if it did then perhaps more programs might compile
systemctl
Posts: 182
Joined: Mar 27, 2020 5:15

Re: Could I use fbc -gen gcc with clang as the compiler?

Postby systemctl » May 03, 2020 19:29

srvaldez wrote:if your OS is Linux, then install clang if not already installed, then instead of -gen gcc use -gen llvm but a lot of programs will fail to compile
on Windows you could do the same if you use the msys2 toolchain, you would need to install clang
pacman -S mingw-w64-i686-clang
pacman -S mingw-w64-x86_64-clang
I don't know whether FB would build using clang, if it did then perhaps more programs might compile


No, my question is about to use clang as compiler to compile C source generated by -gen gcc.
srvaldez
Posts: 2483
Joined: Sep 25, 2005 21:54

Re: Could I use fbc -gen gcc with clang as the compiler?

Postby srvaldez » May 03, 2020 20:06

well then, why don't you give it try?
use the -R, -gen gcc and -v options on the fbc command to get the C source and the compile, assemble and link commands
though I doubt it would work, gcc and clang don't support the same compiler options not to mention that the FB runtime lib's are probably incompatible.
TeeEmCee
Posts: 298
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Could I use fbc -gen gcc with clang as the compiler?

Postby TeeEmCee » May 04, 2020 10:47

fbc doesn't support clang out of the box when using -gen gcc yet, but I think it's not a lot of work to make this work. You can however manually compile the C produced from fbc using clang.
Not long ago I also tried to get -gen gcc working using Visual C++ instead of GCC. That's far more work.

(gcc and clang mostly support the same commandline options, but there are some incompatibilities, often ignorable. And there are standard ABIs on Linux (for each arch) used by all C compilers, so clang and gcc are compatible there too.)
Note that the -gen gcc output will not compile with clang if you pass -e (error checking), because -e also (inefficiently) causes fbc to use computed gotos for RESUME support (even though that's only actually used if you pass -ex).

But even if clang can compile the C code produced by fbc, running "GCC=clang fbc test.bas" doesn't work because fbc tries to run an assembler on the output of clang, but clang produces its own assembly variant not understood by `as`. One solution is to run fbc with -r to produce just a .c file, then manually compile that with clang (and then use fbc to link):

Code: Select all

> fbc -gen gcc -arch 64 -r -m ztest ztest.bas
> clang ztest.c -c -o ztest.o
> fbc -arch 64 ztest.o

You will get a lot of warnings.

Edit: The above created a working executable for me. I was able to compile much larger programs too, in fact the whole of the OHRRPGCE (150kLoC of FB), linking to C and C++ modules (and FB runtime) compiled with GCC, and pass all testcases. Didn't try running FB's own testcases.

There's probably another way by telling fbc to call llvm-as or llvm-ld (by setting AS and LD envvars) and the right as/ld flags.

Edit: Figured out a way to simplify compiling.
srvaldez
Posts: 2483
Joined: Sep 25, 2005 21:54

Re: Could I use fbc -gen gcc with clang as the compiler?

Postby srvaldez » May 04, 2020 12:12

TeeEmCee wrote:Edit: Figured out a way to simplify compiling.

please elaborate
TeeEmCee
Posts: 298
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Could I use fbc -gen gcc with clang as the compiler?

Postby TeeEmCee » May 04, 2020 12:18

Sorry for the confusion, I meant Iedited my post to use the simpler method (which is to use fbc to link instead of clang).
srvaldez
Posts: 2483
Joined: Sep 25, 2005 21:54

Re: Could I use fbc -gen gcc with clang as the compiler?

Postby srvaldez » May 04, 2020 13:59

thanks TeeEmCee
an interesting experiment, in 32-bit I had to pass -asm att to fbc otherwise clang would spit-out many errors related to fpu operand size not being specified.
it would be interesting to have FB built using clang.
Xusinboy Bekchanov
Posts: 246
Joined: Jul 26, 2018 18:28

Re: Could I use fbc -gen gcc with clang as the compiler?

Postby Xusinboy Bekchanov » May 04, 2020 14:23

srvaldez wrote:thanks TeeEmCee
an interesting experiment, in 32-bit I had to pass -asm att to fbc otherwise clang would spit-out many errors related to fpu operand size not being specified.
it would be interesting to have FB built using clang.

Must try MacOS and Android.
TeeEmCee
Posts: 298
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Could I use fbc -gen gcc with clang as the compiler?

Postby TeeEmCee » May 04, 2020 15:08

srvaldez wrote:in 32-bit I had to pass -asm att to fbc otherwise clang would spit-out many errors related to fpu operand size not being specified.

I'm confused... did you pass -r to fbc? If so, it should produce a .c file only, and "-asm att" would be ignored.

Xusinboy Bekchanov wrote:Must try MacOS and Android.

I haven't tried compiling FB code on Mac using clang. Up to now, I install gcc from macports or homebrew and use that for "fbc -gen gcc", but still use clang to compile some Objective-C code that gcc can't. It's very awkward. Note that Apple clang and (formerly) gcc are significantly patched; IIRC they even have a different version numbering scheme for clang!
And I'm still using old an old Android NDK that uses gcc instead of clang (that's actually the version I hadn't upgraded; thanks for reminding me).

Also the FreeBSD port of FB requires gcc to build, but gcc isn't in the base system, clang is. So that's yet another reason FB ought to support using clang instead of gcc.
systemctl
Posts: 182
Joined: Mar 27, 2020 5:15

Re: Could I use fbc -gen gcc with clang as the compiler?

Postby systemctl » May 04, 2020 15:27

TeeEmCee wrote:Edit: Figured out a way to simplify compiling.


Indeed it's the only way that work, even for compiling manually with gcc.
systemctl
Posts: 182
Joined: Mar 27, 2020 5:15

Re: Could I use fbc -gen gcc with clang as the compiler?

Postby systemctl » May 04, 2020 15:29

TeeEmCee wrote:Also the FreeBSD port of FB requires gcc to build, but gcc isn't in the base system, clang is. So that's yet another reason FB ought to support using clang instead of gcc.


Not instead of but in addition to gcc. Since clang and gcc are mostly compatible so think adapt fbc's -gen gcc to use clang as the compiler is not very difficult.
TeeEmCee
Posts: 298
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Could I use fbc -gen gcc with clang as the compiler?

Postby TeeEmCee » May 04, 2020 15:50

I agree, clang support shouldn't be hard. It's throwing a bunch of warnings about declaring memcpy, fopen, etc. instead of including the system header files, but just disabling those warnings may be unavoidable.

systemctl wrote:Indeed it's the only way that work, even for compiling manually with gcc.

If I understood correctly...
Actually no, I prefer to use g++ to link my FB, C and C++ modules together instead of fbc, because it's more portable, and adds the correct C++ libraries. clang works too (this replaces "fbc -arch 64 ztest.o"):

Code: Select all

clang ztest.c -L ~/local/fbc-git/lib/freebasic/linux-x86_64/ -lfb -lpthread -ltermcap -o ztest

Plus that way #inclib doesn't work, which is a good thing because there's some #inclib in modplug.bi which I don't want, because I dynamically load modplug instead if available. fbc ought to have a way to disable #inclibs.
systemctl
Posts: 182
Joined: Mar 27, 2020 5:15

Re: Could I use fbc -gen gcc with clang as the compiler?

Postby systemctl » May 04, 2020 15:56

TeeEmCee wrote:

Code: Select all

clang ztest.c -L ~/local/fbc-git/lib/freebasic/linux-x86_64/ -lfb -lpthread -ltermcap -o ztest


Fsck. I linked with tinfo not termcap, so this is the reason why it always said missing symbols.
srvaldez
Posts: 2483
Joined: Sep 25, 2005 21:54

Re: Could I use fbc -gen gcc with clang as the compiler?

Postby srvaldez » May 04, 2020 16:10

TeeEmCee wrote:
srvaldez wrote:in 32-bit I had to pass -asm att to fbc otherwise clang would spit-out many errors related to fpu operand size not being specified.

I'm confused... did you pass -r to fbc? If so, it should produce a .c file only, and "-asm att" would be ignored.

the command was fbc -gen gcc -arch 32 -asm att -r -m hello hello.bas
without the -asm att clang spits out error messages like: hello.c:71:3: error: ambiguous instructions require an explicit suffix (could be 'flds', 'fldl', or 'fldt')

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 6 guests