Clang/LLVM

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
faisal
Posts: 374
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 44 times
Been thanked: 60 times

Re: Clang/LLVM

Postby faisal » Sun May 27, 2018 1:07 pm

apmorton wrote:To use clang-tidy is a bit more involved, since you need to actually get your code to compile with clang, but it can be done.

I continue to compile my code with gcc in the end, but leverage tools from clang where possible.


Yep, that's why I want to get clang support.

Have you gotten chibi to build with clang? Care to share your makefile or any patches required?

apmorton
Posts: 36
Joined: Fri Sep 29, 2017 10:26 am
Been thanked: 16 times

Re: Clang/LLVM

Postby apmorton » Sun May 27, 2018 1:26 pm

I was experimenting with using cmake at the time, so I don't have any relevant patches for the chibios build system.
I can however provide parts of the CMakeLists.txt from the project, although note I have stripped out anything referencing my specific project.

Code: Select all

cmake_minimum_required (VERSION 3.9)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set (CMAKE_SYSTEM_NAME Generic)
set (CMAKE_SYSTEM_PROCESSOR arm)
set (TOOLCHAIN_ROOT C:/gcc-arm-none-eabi-7.2.1)
set (CMAKE_C_COMPILER ${TOOLCHAIN_ROOT}/bin/arm-none-eabi-gcc.exe)
set (CMAKE_CXX_COMPILER ${TOOLCHAIN_ROOT}/bin/arm-none-eabi-g++.exe)
set (CMAKE_C_FLAGS_INIT "-mcpu=cortex-m3 -mthumb")
set (CMAKE_EXE_LINKER_FLAGS_INIT "-nostartfiles")

project (myproject C CXX ASM)

set (
    COMMON_OPTIONS
    -mcpu=cortex-m3
    -ggdb3
    -fomit-frame-pointer
    -falign-functions=16
    -fshort-wchar
    -ffunction-sections
    -fdata-sections
    -fno-common
    -mthumb
    -mno-thumb-interwork
    -Wall
    -Wextra
    -Wundef
    -Werror
    -Wno-implicit-fallthrough
)

add_compile_options(
    ${COMMON_OPTIONS}
)

set (CMAKE_C_FLAGS "-std=c11 -Wstrict-prototypes")
set (CMAKE_CXX_FLAGS "-std=c++14")
#set (CMAKE_EXE_LINKER_FLAGS ${COMMON_OPTIONS})

add_definitions(
    -DCORTEX_USE_FPU=FALSE
    -DCORTEX_ENABLE_WFI_IDLE
    -DTHUMB_PRESENT
    -DTHUMB_NO_INTERWORKING
    -DTHUMB
    -DCRT0_FORCE_MSP_INIT=TRUE
)

include_directories(
    lib/chibios/os/license
    lib/chibios/os/common/startup/ARMCMx/compilers/GCC
    lib/chibios/os/common/startup/ARMCMx/devices/STM32F2xx
    lib/chibios/os/common/ext/CMSIS/include
    lib/chibios/os/common/ext/CMSIS/ST/STM32F2xx
    lib/chibios/os/rt/include
    lib/chibios/os/common/oslib/include
    lib/chibios/os/common/ports/ARMCMx
    lib/chibios/os/common/ports/ARMCMx/compilers/GCC
    lib/chibios/os/hal/osal/rt
    lib/chibios/os/hal/include
    lib/chibios/os/hal/ports/common/ARMCMx
    lib/chibios/os/hal/ports/STM32/STM32F4xx
    lib/chibios/os/hal/ports/STM32/LLD/ADCv2
    lib/chibios/os/hal/ports/STM32/LLD/CANv1
    lib/chibios/os/hal/ports/STM32/LLD/DACv1
    lib/chibios/os/hal/ports/STM32/LLD/DMAv2
    lib/chibios/os/hal/ports/STM32/LLD/EXTIv1
    lib/chibios/os/hal/ports/STM32/LLD/GPIOv2
    lib/chibios/os/hal/ports/STM32/LLD/I2Cv1
    lib/chibios/os/hal/ports/STM32/LLD/MACv1
    lib/chibios/os/hal/ports/STM32/LLD/OTGv1
    lib/chibios/os/hal/ports/STM32/LLD/QUADSPIv1
    lib/chibios/os/hal/ports/STM32/LLD/RTCv2
    lib/chibios/os/hal/ports/STM32/LLD/SPIv1
    lib/chibios/os/hal/ports/STM32/LLD/SDIOv1
    lib/chibios/os/hal/ports/STM32/LLD/TIMv1
    lib/chibios/os/hal/ports/STM32/LLD/USARTv1
    lib/chibios/os/hal/ports/STM32/LLD/xWDGv1
    lib/chibios/test/lib
    lib/chibios/test/rt/source/test
    lib/chibios/os/hal/lib/streams
    lib/chibios/os/various
    lib/chibios/os/various/cpp_wrappers
)

link_directories (
    .
)

file (
    GLOB_RECURSE SOURCES
    src/*.c
    src/*.cpp
)

set (
    SOURCES

    lib/chibios/os/common/startup/ARMCMx/compilers/GCC/crt1.c
    lib/chibios/os/common/startup/ARMCMx/compilers/GCC/vectors.c
    lib/chibios/os/common/startup/ARMCMx/compilers/GCC/crt0_v7m.S
    lib/chibios/os/hal/src/hal.c
    lib/chibios/os/hal/src/hal_buffers.c
    lib/chibios/os/hal/src/hal_queues.c
    lib/chibios/os/hal/src/hal_mmcsd.c
    lib/chibios/os/hal/src/hal_adc.c
    lib/chibios/os/hal/src/hal_can.c
    lib/chibios/os/hal/src/hal_dac.c
    lib/chibios/os/hal/src/hal_ext.c
    lib/chibios/os/hal/src/hal_gpt.c
    lib/chibios/os/hal/src/hal_i2c.c
    lib/chibios/os/hal/src/hal_i2s.c
    lib/chibios/os/hal/src/hal_icu.c
    lib/chibios/os/hal/src/hal_mac.c
    lib/chibios/os/hal/src/hal_mmc_spi.c
    lib/chibios/os/hal/src/hal_pal.c
    lib/chibios/os/hal/src/hal_pwm.c
    lib/chibios/os/hal/src/hal_qspi.c
    lib/chibios/os/hal/src/hal_rtc.c
    lib/chibios/os/hal/src/hal_sdc.c
    lib/chibios/os/hal/src/hal_serial.c
    lib/chibios/os/hal/src/hal_serial_usb.c
    lib/chibios/os/hal/src/hal_spi.c
    lib/chibios/os/hal/src/hal_st.c
    lib/chibios/os/hal/src/hal_uart.c
    lib/chibios/os/hal/src/hal_usb.c
    lib/chibios/os/hal/src/hal_wdg.c
    lib/chibios/os/hal/ports/common/ARMCMx/nvic.c
    lib/chibios/os/hal/ports/STM32/STM32F4xx/hal_lld.c
    lib/chibios/os/hal/ports/STM32/STM32F4xx/hal_ext_lld_isr.c
    lib/chibios/os/hal/ports/STM32/LLD/ADCv2/hal_adc_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/CANv1/hal_can_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/DACv1/hal_dac_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/DMAv2/stm32_dma.c
    lib/chibios/os/hal/ports/STM32/LLD/EXTIv1/hal_ext_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/GPIOv2/hal_pal_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/I2Cv1/hal_i2c_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/MACv1/hal_mac_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/OTGv1/hal_usb_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/QUADSPIv1/hal_qspi_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/RTCv2/hal_rtc_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/SPIv1/hal_i2s_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/SPIv1/hal_spi_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/SDIOv1/hal_sdc_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/TIMv1/hal_st_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/TIMv1/hal_gpt_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/TIMv1/hal_icu_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/TIMv1/hal_pwm_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/USARTv1/hal_serial_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/USARTv1/hal_uart_lld.c
    lib/chibios/os/hal/ports/STM32/LLD/xWDGv1/hal_wdg_lld.c
    lib/chibios/os/hal/osal/rt/osal.c
    lib/chibios/os/rt/src/chsys.c
    lib/chibios/os/rt/src/chdebug.c
    lib/chibios/os/rt/src/chtrace.c
    lib/chibios/os/rt/src/chvt.c
    lib/chibios/os/rt/src/chschd.c
    lib/chibios/os/rt/src/chthreads.c
    lib/chibios/os/rt/src/chsys.c
    lib/chibios/os/rt/src/chdebug.c
    lib/chibios/os/rt/src/chtrace.c
    lib/chibios/os/rt/src/chvt.c
    lib/chibios/os/rt/src/chschd.c
    lib/chibios/os/rt/src/chthreads.c
    lib/chibios/os/rt/src/chtm.c
    lib/chibios/os/rt/src/chstats.c
    lib/chibios/os/rt/src/chregistry.c
    lib/chibios/os/rt/src/chsem.c
    lib/chibios/os/rt/src/chmtx.c
    lib/chibios/os/rt/src/chcond.c
    lib/chibios/os/rt/src/chevents.c
    lib/chibios/os/rt/src/chmsg.c
    lib/chibios/os/rt/src/chdynamic.c
    lib/chibios/os/common/oslib/src/chmboxes.c
    lib/chibios/os/common/oslib/src/chmemcore.c
    lib/chibios/os/common/oslib/src/chheap.c
    lib/chibios/os/common/oslib/src/chmempools.c
    lib/chibios/os/common/ports/ARMCMx/chcore.c
    lib/chibios/os/common/ports/ARMCMx/chcore_v7m.c
    lib/chibios/os/common/ports/ARMCMx/compilers/GCC/chcoreasm_v7m.S
    lib/chibios/os/hal/lib/streams/chprintf.c
    lib/chibios/os/hal/lib/streams/memstreams.c
    lib/chibios/os/hal/lib/streams/nullstreams.c
    lib/chibios/os/various/cpp_wrappers/ch.cpp
    lib/chibios/os/various/cpp_wrappers/syscalls_cpp.cpp

    src/main.c
)

add_executable("${PROJECT_NAME}.elf" ${SOURCES})
target_link_libraries(
    "${PROJECT_NAME}.elf"
    ${COMMON_OPTIONS}
    -Wl,-Map=${CMAKE_BINARY_DIR}/${PROJECT_NAME}.map,--cref,--no-warn-mismatch,--library-path=lib/chibios/os/common/startup/ARMCMx/compilers/GCC/ld,--script=src/link.ld,--gc-sections,--defsym=__process_stack_size__=0x2000,--defsym=__main_stack_size__=0x2000
    --specs=nano.specs
    -nostdlib
    -lg -lgcc -lstdc++
)

set (
    CLANG_TIDY
    "C:\\Program Files\\LLVM\\bin\\clang-tidy.exe"
    "-checks=*,-readability-braces-around-statements,-clang-diagnostic-unused-command-line-argument,-clang-diagnostic-ignored-optimization-argument"
)

set_property(
    TARGET "${PROJECT_NAME}.elf"
    PROPERTY CXX_CLANG_TIDY
    ${CLANG_TIDY}
)
set_property(
    TARGET "${PROJECT_NAME}.elf"
    PROPERTY C_CLANG_TIDY
    ${CLANG_TIDY}
)


the most relevant bits are "-clang-diagnostic-unused-command-line-argument,-clang-diagnostic-ignored-optimization-argument" as these tell clang-tidy not to be concerned with some gcc specific compiler flags that it would otherwise complain about.

apmorton
Posts: 36
Joined: Fri Sep 29, 2017 10:26 am
Been thanked: 16 times

Re: Clang/LLVM

Postby apmorton » Sun May 27, 2018 1:33 pm

One other note that might help you - the key to using clang-tidy without completely overhauling your build system is generating a compile_commands.json file, which is a format that most clang tools can use as input - including clang-tidy.

Several projects aim to bridge the gap here. I have no personal experience with them, since we decided to go the route of CMake for other reasons, but we looked into them at the time.

https://github.com/nickdiego/compiledb-generator
https://github.com/rizsotto/Bear

these work as a sort of wrapper to make, in which your command line to build your project becomes (in the case of bear)
"bear make" instead of just "make"

They they run your normal make build, taking note of everything that happens during the build and in the end spit out compile_commands.json for you to pass to clang-tidy.

The main downside to both compiledb-generator and Bear is lack of support for windows platforms (which was ultimately the dealbreaker for my needs).

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Clang/LLVM

Postby Giovanni » Sun May 27, 2018 3:38 pm

I installed it but apparently there are significant differences compared to GCC, the normal rules.mk has problems, it will need its own compiler rules.

Giovanni

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Clang/LLVM

Postby Giovanni » Fri Jul 06, 2018 2:04 pm

Update, apparently the above toolchain does not include the C library so it cannot really be used. Is there any real and supported free ARM toolchain out there?

I tried with a commercial toolchain based on LLVM and managed to make things work, it just takes a modified rules.mk (because a clang bug, it fails with option -Wa,xxx), everything else is fine.

I am "less than impressed" about code size and speed anyway.

Giovanni

kiran
Posts: 4
Joined: Tue Jul 10, 2018 7:20 am
Has thanked: 1 time

Re: Clang/LLVM

Postby kiran » Tue Jul 10, 2018 7:42 am

I am trying to cross compile ChibiOS using clang for STM32L476(i am trying to build the demo application provided(as part of ChibiOS) along with entire ChibiOS source).
I have followed - "http://llvm.org/docs/GettingStarted.html#requirements" for clang/llvm build.

While building with clang , currently I am facing similar issue as you have mentioned with "option -Wa,xxx" (clang bug).
Could you please share how to over come this clang bug ?
Also is it possible to share the modified "rules.mk" file which you have used to build for ARM?

Regards,
Kiran

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Clang/LLVM

Postby Giovanni » Tue Jul 10, 2018 7:47 am

Hi,

I already committed a rules.mk for LLVM, I fixed -Wa by removing it (no workaround found), it is not final yet.

Results on code size and speed make me think this is wasted time, GCC looks way better.

I compiled the STM32F401RE Nucleo demo.

For GCC:

Code: Select all

Options: -Os
Size: 35,516 bytes

Options: -O2
Size: 39,260 bytes

Options: -O3
Size: 50,864 bytes


For Clang:

Code: Select all

Options: -Os
Size: 48,044 bytes

Options: -O2
Size: 90,036 bytes

Options: -O3
Size: 92,064 bytes


GCC at -O3 is about equal to Clang at -Os. I could be doing something wrong, still checking but without listings it is hard to compare.

In addition, some bugs caused me to have to make separate .ld files, linkers are supposed to be similar but behave very differently.

Giovanni

kiran
Posts: 4
Joined: Tue Jul 10, 2018 7:20 am
Has thanked: 1 time

Re: Clang/LLVM

Postby kiran » Tue Jul 10, 2018 8:24 am

Thank you for the update.

I could not find the modified "rules.mk" file in the git repositories.
Could you please share the repository link from i can checkout the modified "rules.mk".

Regards,
Kiran

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Clang/LLVM

Postby Giovanni » Tue Jul 10, 2018 8:35 am

It is on subversion: https://svn.code.sf.net/p/chibios/svn2/ ... ilers/LLVM

The git repository is updated once in a while by community, do not assume it is the most recent code.

Giovanni

kiran
Posts: 4
Joined: Tue Jul 10, 2018 7:20 am
Has thanked: 1 time

Re: Clang/LLVM

Postby kiran » Tue Jul 10, 2018 12:42 pm

Thank you for sharing the source path.

Currently i am facing errors in Compiling crt0_v7m.S :
crt0_v7m.S:171:17: error: unknown directive
.syntax unified

Did you faced similar kind of issues , if so any fix for it ?

Regards,
Kiran


Return to “Development and Feedback”

Who is online

Users browsing this forum: No registered users and 33 guests