This page is intended to capture notes and information about using Clang to compile RTEMS.

All supporting files, patches, replacement files, etc. needed should be in If something is missing, let Joel know.

Development Log

Open Issues

These are issues in clang for them to address:

These are issues in clang for us to address:

  • clang does not know the RTEMS include paths. No immediate solution.
  • clang when targeting i386-rtems needs to define USER_LABEL_PREFIX. Other ports probably have something similar and something like a register prefix macro.
  • clang has problem to handle the "-B" option which does not search the header files under the directory include specified by option "-B".

These are issues in newlib:

  • newlib has warnings for PTHREAD_MUTEX_xxx being redefined - need to submit patch.
  • newlib has warnings in rtems specific crt0.c - need to submit patch.

There are the issues identified so far in RTEMS:

  • Remove dependency on -specs or change the format of -specs option from "-specs specs_file" by default to "-specs=specs_file".

Commit History

2011-07-07: Small patch to make sure rtems was defined was committed.

2011-07-01: The initial patches which add RTEMS support to clang and llvm (clang-rtems-20110629.diff and llvm-rtems-20110629.diff) were merged. This was reported in

Base Tools

For now, you need the regular RTEMS toolchain installed. We are using the normal RTEMS binutils and gdb. In addition, the way I have been building requires using the CPU-rtems4.11-gcc as a helper wrapper.

Building Clang

Check out LLVM and Clang using the instructions at In my case, I started in a working directory (/users/joel/llvm) and checked out into /users/joel/llvm/llvm. After the checkout completed, I performed the following commands:

cd /users/joel/llvm
mkdir b-llvm
cd b-llvm
../llvm/configure --prefix=/users/joel/llvm/install
make install

UPDATE (7/30/19): configure is no longer supported. cmake is used instead. Try the following command instead of the configure line above:

cmake -DCMAKE_INSTALL_PREFIX=/users/joel/llvm/install -DLLVM_ENABLE_PROJECTS=clang -G "Unix Makefiles" ../llvm

And then you can make && make install. (You should also pass -j option to make for a faster build.)

After this completed, I ensured that my PATH included /opt/rtems-4.11/bin and /users/joel/llvm/install/bin for subsequent builds.

Building Newlib With Clang

I created a script file named /users/joel/llvm/j-newlib with the following contents.

../newlib-1.19.0/configure --host=${CPU}-rtems4.11 \
  --prefix=/users/joel/llvm/install \
  --with-newlib \
  CC="clang -ccc-host-triple ${CPU}-rtems4.11 -ccc-gcc-name ${CPU}-rtems4.11-gcc" \
  CC_FOR_TARGET="clang -ccc-host-triple ${CPU}-rtems4.11 -ccc-gcc-name ${CPU}-rtems4.11-gcc" \
  CC_FOR_BUILD="clang " \
  PONIES=true \
  AR=${CPU}-rtems4.11-ar \
  AS=${CPU}-rtems4.11-as >c.log 2>&1 && \
make -j4 >b.log 2>&1 && \
make install >i.log 2>&1 

echo $?

UPDATE (7/30/19): -ccc-host-triple is no longer supported. Replace with -target instead.

newlib must be patched as appropriate for the version of RTEMS being built.

I used the j-newlib script to build newlib as follows:

cd /users/joel/llvm
mkdir b-newlib
cd b-newlib
sh -x ../j-newlib

clang cannot build the file newlib/libc/sys/rtems/crt0.c. There is a replacement of that file on the ftp site. I am not sure if this is a clang bug, questionable code in crt0.c or a combination of the two. UPDATE (7/30/19): clang can now build crt0.c from newlib.

Building RTEMS

I have successfully built RTEMS follow the command below from a build directory parallel to your RTEMS source tree (e.g., /users/joel/b-rtems)

../rtems/configure --target=${CPU}-rtems4.11 \
  --prefix=/users/joel/llvm/install \
  AR=${CPU}-rtems4.11-ar \
  AS=${CPU}-rtems4.11-as \
  CC_FOR_BUILD="clang " \
  CC_FOR_TARGET="clang -ccc-host-triple ${CPU}-rtems4.11 -ccc-gcc-name ${CPU}-rtems4.11-gcc " \
  CC_FOR_HOST="clang -ccc-host-triple ${CPU}-rtems4.11 -ccc-gcc-name ${CPU}-rtems4.11-gcc " >c.log 2>&1 

make RTEMS_BSP="pc386" >b.log 2>&1

*UPDATE (7/30/19):* I had to provide the path to the installed newlib header files. I did this by providing a CPATH environment variable, for example

export CPATH=/users/joel/llvm/install/i386-rtems5/include

There are maybe some bugs in the rtems configure system. when attach the CC_FOR_TARGET,CC_FOR_BUILD flags to the cofigure command, it will generate some config.status inside which will add CC="cross compiler" to every subdir,but some subdir do not expect this CC, like in dir i386-rtems4.11/c/pc386/lib/libbsp/i386/pc386/tools. It contains some host code compiled by host compiler which should be auto detected, if you add CC="cross compiler" it will failed, So if you encounter this error you need delete the CC="cross compiler" command in the file i386-rtems4.11/c/pc386/lib/libbsp/i386/pc386/config.status for tools.


Last modified on 07/30/19 at 23:00:32 Last modified on 07/30/19 23:00:32