# Compiling the kernel with another compiler ?



## Alain De Vos (Aug 29, 2022)

Making abstraction of it is wise, is it possible to compile the freebsd kernel with a clang compiler of choice ?
Is it possible to compile the kernel with gcc or tcc ?
[ Or are the kernel and the compiler version tighthly integrated ?]


----------



## zirias@ (Aug 29, 2022)

That's not supported. Probably not entirely _impossible_, but not a simple thing either.
It seems there _is_ some support for using gcc from ports (in a variant directly targeting FreeBSD) to build world, not sure whether it would apply to the kernel as well, see /usr/src/Makefile.
But then, I just have to ask: What's the point?


----------



## Alain De Vos (Aug 29, 2022)

1. An exercise, or how do you do it, learn something.
2. What are the problems and why ?
3. Compatible software should be independent of the compiler/type/version. Does this apply to the kernel as well ?


----------



## Alain De Vos (Aug 29, 2022)

For instance if you want to use clang to compile in gentoo packages you should:

```
CC="clang"
CXX="clang++"
AR="llvm-ar"
NM="llvm-nm"
RANLIB="llvm-ranlib"
LDFLAGS="${LDFLAGS} -fuse-ld=lld -rtlib=compiler-rt -unwindlib=libunwind -Wl,--as-needed"
```
And for hardening:

```
_HARDENING_FLAGS="-fstack-protector-strong -D_FORTIFY_SOURCE=2"
CFLAGS="${CFLAGS} ${_HARDENING_FLAGS}"
CXXFLAGS="${CXXFLAGS} ${_HARDENING_FLAGS}"
LDFLAGS="${LDFLAGS} -Wl,-z,relro,-z,now"
```
I try to understand this.


----------



## zirias@ (Aug 29, 2022)

Alain De Vos said:


> Compatible software should be independent of the compiler/type/version. Does this apply to the kernel as well ?


If by "compatible software", you mean code using nothing but standard C, then, sure. That's completely impossible for _some_ parts of an operating system, like e.g. a kernel. The ABI needs to be defined (things like calling conventions, including those for syscalls, sizes of some standard types depending on the machine arch, and so on). For some of that, some assembler code is needed, for other aspects, the compiler must know about it. A kernel needs assembler for other things as well, e.g. bootstrapping code. There are probably *lots* of other things where "just standard C" won't do.


Alain De Vos said:


> I try to understand this.


Those are just "standard" (as in convention) variables any sane build system understands. You could use most of them with FreeBSD ports as well (although some of the contents might not match). I don't see how compiling the FreeBSD kernel with a different compiler would ever help understanding these variables. And btw, I strongly recommend _against_ overriding them for port builds. Some ports will probably work, but you _will_ run into very strange issues.


----------

