VIM and YCM set up

YCM is code-completion engine plugin for vim.

It uses clangd to provide completions and highlight syntax errors.
Because RTEMS is cross complied, a bit of extra work is required to get things working with clangd.

compile_commands.json

First clangd will need the compiler flag. The easiest way to pass these into clang through ycm is to place a compile_commands.json at the top of the project. As pointed out by many others bear can be used to easily generate one. (personal thanks to prashant on discord for showing me the light).

bear -- ./waf

.clangd

when targeting the erc32 bsp, waf uses the -mcpu-cypress flag. This caused ycm’s clang to crash.

To resolve this add a .clangd file to the top of your project further tweak clang’s runtime.

Here is the .clangd that I used when exploring the rtems examples repo. The goal was to remove the mcpu flag and force add the assumed standard included path.
(I’ve snipped the absolute paths for public viewing)

CompileFlags:
  CompilationDatabase: /------/work/rtems/rtems-examples
  Compiler: /------/work/rtems/quick-start/rtems/6/bin/sparc-rtems6-gcc
  BuiltinHeaders: QueryDriver
  Remove:
    - -mcpu=*
  Add:
    - -isystem
    - /-----/work/rtems/quick-start/rtems/6/lib/gcc/sparc-rtems6/13.3.0/include

vimrc

Lastly clangd requires an additional flag when it is invoked by ycm to switch into a cross compiler mode. this can be done in your _vimrc.
(again modified absolute paths)

let g:ycm_clangd_args = [
      \ '--query-driver=/-----/work/rtems/quick-start/rtems/6/bin/*',
      \ ]

Useful Clangd Debugging

When figuring this out I found it very helpful to just invoke clangd directly bypassing ycm. Be sure to use the clang held by ycm i.e. the one you built when you installed ycm.

# example running clangd on a file in the rtems example repo
~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd --check=classic_api/triple_period/int.c --compile-commands-dir=$PWD --log=verbose --query-driver=$PATH_TO_RTEMS_BIN_same_as_vimrv

Look for unknown flag errors and include directories being ignored.

Lastly I believe vscode does something similar for its c syntax engine, and this maybe of some help there.

If there are general vim and ycm questions I’ll be happy to help.

Can you please provide the details on how you created compile_commands.json and how you used bear to do that?

yep, just call waf via bear.

bear -- ./waf

Note you may need to do a waf clean first otherwise waf won’t call any compiler commands on already built outputs for bear to capture.

How long does this command take to run and how many bear processes do you see?

When I running this command on FreeBSD 14.4 I get no output

(rpy3.11) ruru rtems.git $ ./waf clean
'clean' finished successfully (0.625s)
'clean_powerpc/mvme2700' finished successfully (0.048s)
'clean_powerpc/mvme5500' finished successfully (0.017s)
'clean_powerpc/beatnik' finished successfully (0.016s)
'clean_powerpc/qoriq_e500' finished successfully (0.017s)
'clean_arm/xilinx_zynq_microzed' finished successfully (0.027s)
'clean_aarch64/te0802' finished successfully (0.017s)
(rpy3.11) ruru rtems.git $ bear -- ./waf
^C(rpy3.11) ruru rtems.git $

and I stop the build once bear gets to 2500+ processes running:

ruru chris $ ps auxw | grep bear | wc -l
    2469
ruru chris $ ps auxw | grep bear | wc -l
    2510

maybe try it with only one configured BSP?

Im on WSL/ubuntu 24

hpfeiffer MIWP4133B⛩ ~/work/rtems/projects/standlone
  PREFIX=$PWD/rtems_prefix
hpfeiffer MIWP4133B⛩ ~/work/rtems/projects/standlone
  ./waf configure --rtems=$PREFIX --rtems-tools=$PREFIX --rtems-bsp=sparc/erc32
invalid lock file in /home/hpfeiffer/work/rtems/projects/standlone
Setting top to                           : /home/hpfeiffer/work/rtems/projects/standlone
Setting out to                           : /home/hpfeiffer/work/rtems/projects/standlone/build
RTEMS Version                            : 6
Architectures                            : arm-rtems6, sparc-rtems6
Board Support Package (BSP)              : sparc-rtems6-erc32
Show commands                            : no
Long commands                            : no
Checking for program 'sparc-rtems6-gcc'  : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-gcc
Checking for program 'sparc-rtems6-g++'  : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-g++
Checking for program 'sparc-rtems6-gcc'  : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-gcc
Checking for program 'sparc-rtems6-ld'   : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-ld
Checking for program 'sparc-rtems6-ar'   : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-ar
Checking for program 'sparc-rtems6-nm'   : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-nm
Checking for program 'sparc-rtems6-objdump' : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-objdump
Checking for program 'sparc-rtems6-objcopy' : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-objcopy
Checking for program 'sparc-rtems6-readelf' : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-readelf
Checking for program 'sparc-rtems6-strip'   : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-strip
Checking for program 'sparc-rtems6-ranlib'  : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-ranlib
Checking for program 'rtems-ld'             : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/rtems-ld
Checking for program 'rtems-tld'            : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/rtems-tld
Checking for program 'rtems-syms'           : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/rtems-syms
Checking for program 'rtems-bin2c'          : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/rtems-bin2c
Checking for program 'tar'                  : /usr/bin/tar
Checking for program 'gcc, cc'              : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-gcc
Checking for program 'ar'                   : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-ar
Checking for program 'g++, c++'             : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-g++
Checking for program 'ar'                   : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-ar
Checking for program 'gas, gcc'             : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-gcc
Checking for program 'ar'                   : /home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-ar
Checking for c flags '-MMD'                 : yes
Checking for cxx flags '-MMD'               : yes
Compiler version (sparc-rtems6-gcc)         : 13.3.0 20240521 (RTEMS 6, RSB 3814cb0e7f86cca2be403eac831f9bf571984659, Newlib 1b3dcfd)
Checking for a valid RTEMS BSP installation : yes
Checking for RTEMS_DEBUG                    : no
Checking for RTEMS_MULTIPROCESSING          : no
Checking for RTEMS_NEWLIB                   : yes
Checking for RTEMS_POSIX_API                : yes
Checking for RTEMS_SMP                      : no
Checking for RTEMS_NETWORKING               : no
'configure' finished successfully (0.617s)
hpfeiffer MIWP4133B⛩ ~/work/rtems/projects/standlone
  ./waf clean
'clean-sparc-rtems6-erc32' finished successfully (0.023s)
hpfeiffer MIWP4133B⛩ ~/work/rtems/projects/standlone
  bear -- ./waf
Waf: Entering directory `/home/hpfeiffer/work/rtems/projects/standlone/build/sparc-rtems6-erc32'
[1/2] Compiling app/3task_sleep/3task_sleep.c
[2/2] Linking build/sparc-rtems6-erc32/3task_sleep_demo.exe
Waf: Leaving directory `/home/hpfeiffer/work/rtems/projects/standlone/build/sparc-rtems6-erc32'
'build-sparc-rtems6-erc32' finished successfully (0.468s)
hpfeiffer MIWP4133B⛩ ~/work/rtems/projects/standlone
  cat compile_commands.json
[
  {
    "arguments": [
      "/home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/bin/sparc-rtems6-gcc",
      "-mcpu=cypress",
      "-isystem/home/hpfeiffer/work/rtems/projects/standlone/rtems_prefix/sparc-rtems6/erc32/lib/include",
      "-O2",
      "-g",
      "-c",
      "-o/home/hpfeiffer/work/rtems/projects/standlone/build/sparc-rtems6-erc32/app/3task_sleep/3task_sleep.c.1.o",
      "../../app/3task_sleep/3task_sleep.c"
    ],
    "directory": "/home/hpfeiffer/work/rtems/projects/standlone/build/sparc-rtems6-erc32",
    "file": "/home/hpfeiffer/work/rtems/projects/standlone/app/3task_sleep/3task_sleep.c"
  }
]

an alternative would be to manually add the compile flags to a .ycm_extra_conf.py

Ah you have a stand alone project. I tried with rtems.git and also a stand alone project and saw the same result.

Like gedare mentioned. I wonder if building for multiple target bsps at the same time breaks bear