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.
User avatar
Giovanni
Site Admin
Posts: 14462
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1078 times
Been thanked: 922 times
Contact:

Re: Clang/LLVM

Postby Giovanni » Fri May 07, 2021 3:32 pm

That hightec compiler included a library and they worked on the linker on their side, it is not GPL so they can keep their optimizations/fixes private.

I never used LLVM as-is, feel free to propose patches.

Giovanni

faisal
Posts: 374
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 44 times
Been thanked: 60 times

Re: Clang/LLVM

Postby faisal » Wed Nov 10, 2021 12:46 am

Based on stable_20.3.x branch, here you go. Works with standard llvm/clang distribution, and ARM gcc compiler (used for linker and built-ins). For some reason LTO doesn't work, so disable that (may want to do that in rules.mk). Having trouble attaching files ..

I started with the GCC rules.mk, since that has been updated most recently. I used the same line ending style that was originally in the LLVM rules.mk (which are mixed) to keep the diff size down. Before comitting, I would harmonize the line endings ...

Code: Select all

diff --git c/os/common/startup/ARMCMx/compilers/LLVM/mk/clang.mk w/os/common/startup/ARMCMx/compilers/LLVM/mk/clang.mk
index db0f1e4a8..812f277d2 100644
--- c/os/common/startup/ARMCMx/compilers/LLVM/mk/clang.mk
+++ w/os/common/startup/ARMCMx/compilers/LLVM/mk/clang.mk
@@ -1,19 +1,20 @@
-##############################################################################
-# Compiler settings
-#
-
-TRGT = aarch32-
-CC   = clang
-CPPC = clang++
-LD   = clang
-CP   = $(TRGT)objcopy
-AS   = $(TRGT)as -x assembler-with-cpp
-AR   = $(TRGT)ar
-OD   = $(TRGT)objdump
-SZ   = $(TRGT)size
-HEX  = $(CP) -O ihex
-BIN  = $(CP) -O binary
-
-#
-# Compiler settings
-##############################################################################
+##############################################################################
+# Compiler settings
+#
+
+TRGT   = llvm-
+CC     = clang
+GCC_CC = arm-none-eabi-gcc
+CPPC   = clang++
+LD     = $(GCC_CC)
+CP     = $(TRGT)objcopy
+AS     = $(TRGT)as -x assembler-with-cpp
+AR     = $(TRGT)ar
+OD     = $(TRGT)objdump
+SZ     = $(TRGT)size
+HEX    = $(CP) -O ihex
+BIN    = $(CP) -O binary
+
+#
+# Compiler settings
+##############################################################################
diff --git c/os/common/startup/ARMCMx/compilers/LLVM/mk/rules.mk w/os/common/startup/ARMCMx/compilers/LLVM/mk/rules.mk
index 0d6af76e4..b448f8d07 100644
--- c/os/common/startup/ARMCMx/compilers/LLVM/mk/rules.mk
+++ w/os/common/startup/ARMCMx/compilers/LLVM/mk/rules.mk
@@ -98,14 +98,13 @@ LSTDIR    := $(BUILDDIR)/lst
 
 # Object files groups
 TCOBJS    := $(addprefix $(OBJDIR)/, $(notdir $(TCSRC:.c=.o)))
-TCPPOBJS  := $(addprefix $(OBJDIR)/, $(notdir $(TCPPSRC:.cpp=.o)))
+#TCPPOBJS  := $(addprefix $(OBJDIR)/, $(notdir $(TCPPSRC:.cpp=.o)))
+TCPPOBJS  := $(addprefix $(OBJDIR)/, $(notdir $(patsubst %.cpp, %.o, $(filter %.cpp, $(TCPPSRC)))))
+TCCOBJS   := $(addprefix $(OBJDIR)/, $(notdir $(patsubst %.cc, %.o, $(filter %.cc, $(TCPPSRC)))))
 ASMOBJS   := $(addprefix $(OBJDIR)/, $(notdir $(ASMSRC:.s=.o)))
 ASMXOBJS  := $(addprefix $(OBJDIR)/, $(notdir $(ASMXSRC:.S=.o)))
-OBJS      := $(ASMXOBJS) $(ASMOBJS) $(ACOBJS) $(TCOBJS) $(ACPPOBJS) $(TCPPOBJS)
-
-# Paths
-IINCDIR   := $(patsubst %,-I%,$(INCDIR) $(DINCDIR) $(UINCDIR))
-LLIBDIR   := $(patsubst %,-L%,$(DLIBDIR) $(ULIBDIR))
+#OBJS      := $(ASMXOBJS) $(ASMOBJS) $(ACOBJS) $(TCOBJS) $(ACPPOBJS) $(TCPPOBJS)
+OBJS      := $(ASMXOBJS) $(ASMOBJS) $(ACOBJS) $(TCOBJS) $(ACPPOBJS) $(TCPPOBJS) $(TCCOBJS)
 
 # Macros
 DEFS      := $(DDEFS) $(UDEFS)
@@ -121,11 +120,18 @@ ASFLAGS   = $(MCFLAGS) $(OPT) $(ADEFS)
 ASXFLAGS  = $(MCFLAGS) $(OPT) $(ADEFS)
 CFLAGS    = $(MCFLAGS) $(OPT) $(COPT) $(CWARN) $(DEFS)
 CPPFLAGS  = $(MCFLAGS) $(OPT) $(CPPOPT) $(CPPWARN) $(DEFS)
-#ASFLAGS   = $(MCFLAGS) $(OPT) -Wa,-amhls=$(LSTDIR)/$(notdir $(<:.s=.lst)) $(ADEFS)
-#ASXFLAGS  = $(MCFLAGS) $(OPT) -Wa,-amhls=$(LSTDIR)/$(notdir $(<:.S=.lst)) $(ADEFS)
-#CFLAGS    = $(MCFLAGS) $(OPT) $(COPT) $(CWARN) -Wa,-alms=$(LSTDIR)/$(notdir $(<:.c=.lst)) $(DEFS)
-#CPPFLAGS  = $(MCFLAGS) $(OPT) $(CPPOPT) $(CPPWARN) -Wa,-alms=$(LSTDIR)/$(notdir $(<:.cpp=.lst)) $(DEFS)
-LDFLAGS   = $(MCFLAGS) $(OPT) -nostartfiles $(LLIBDIR) -Wl,-Map=$(BUILDDIR)/$(PROJECT).map,--cref,--no-warn-mismatch,--library-path=$(STARTUPLD),--script=$(LDSCRIPT)$(LDOPT)
+
+ARM_CORTEXM_SYSROOT := \
+   $(shell $(GCC_CC) $(CFLAGS) -print-sysroot 2>&1)
+ARM_CORTEXM_MULTI_DIR := \
+   $(shell $(GCC_CC) $(CFLAGS) -print-multi-directory 2>&1)
+ARM_CORTEXM_BUILTINS := \
+   $(shell $(GCC_CC) $(CFLAGS) -print-libgcc-file-name 2>&1)
+
+ASFLAGS   += --target=arm-none-eabi --sysroot=$(ARM_CORTEXM_SYSROOT)
+ASXFLAGS  += --target=arm-none-eabi --sysroot=$(ARM_CORTEXM_SYSROOT)
+CFLAGS    += --target=arm-none-eabi --sysroot=$(ARM_CORTEXM_SYSROOT)
+CPPFLAGS  += --target=arm-none-eabi --sysroot=$(ARM_CORTEXM_SYSROOT)
 
 # Generate dependency information
 ASFLAGS  += -MD -MP -MF $(DEPDIR)/$(@F).d
@@ -133,6 +139,11 @@ ASXFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
 CFLAGS   += -MD -MP -MF $(DEPDIR)/$(@F).d
 CPPFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
 
+# Paths
+IINCDIR   := $(patsubst %,-I%,$(INCDIR) $(DINCDIR) $(UINCDIR))
+LLIBDIR   := $(patsubst %,-L%,$(DLIBDIR) $(ULIBDIR) $(ARM_CORTEXM_SYSROOT)/lib/$(ARM_CORTEXM_MULTI_DIR))
+LDFLAGS   = $(MCFLAGS) $(OPT) -nostartfiles $(LLIBDIR) $(ARM_CORTEXM_BUILTINS) -Wl,-Map=$(BUILDDIR)/$(PROJECT).map,--cref,--no-warn-mismatch,--library-path=$(STARTUPLD),--script=$(LDSCRIPT)$(LDOPT)
+
 # Paths where to search for sources
 VPATH     = $(SRCPATHS)
 
@@ -174,6 +185,15 @@ else
    @$(CPPC) -c $(CPPFLAGS) -I. $(IINCDIR) $< -o $@
 endif
 
+$(TCCOBJS) : $(OBJDIR)/%.o : %.cc $(MAKEFILE_LIST)
+ifeq ($(USE_VERBOSE_COMPILE),yes)
+   @echo
+   $(CPPC) -c $(CPPFLAGS) -I. $(IINCDIR) $< -o $@
+else
+   @echo Compiling $(<F)
+   @$(CPPC) -c $(CPPFLAGS) -I. $(IINCDIR) $< -o $@
+endif
+
 $(TCOBJS) : $(OBJDIR)/%.o : %.c $(MAKEFILE_LIST)
 ifeq ($(USE_VERBOSE_COMPILE),yes)
    @echo

faisal
Posts: 374
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 44 times
Been thanked: 60 times

Re: Clang/LLVM

Postby faisal » Wed Dec 01, 2021 6:27 am

Bump

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

Re: Clang/LLVM

Postby Giovanni » Wed Dec 01, 2021 1:41 pm

Please attach the file, tabs are lost by posting that way.

Giovanni

faisal
Posts: 374
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 44 times
Been thanked: 60 times

Re: Clang/LLVM

Postby faisal » Wed Dec 01, 2021 7:04 pm

Here you go
Attachments
llvm_patch.zip
(1.47 KiB) Downloaded 182 times

faisal
Posts: 374
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 44 times
Been thanked: 60 times

Re: Clang/LLVM

Postby faisal » Tue Jan 11, 2022 9:07 pm

Have to add another variable to pass arguments to the compiler driver for the linker. I called it LDOPT_DRIVER.
This is especially necessary if you use the --specs option, which many of us may use. For example, I use --specs=nano.specs all the time . The --specs option is for the compiler driver, which uses it to generate option flags and arguments for the linker it ends up invoking. When compiling with GCC I would put the --specs in OPT, when using LLVM (which uses the clang compiler, and gcc and ld for linking) I would put --specs in LDOPT_DRIVER.

Code: Select all

diff --git a/ChibiOS_20.x.x/os/common/startup/ARMCMx/compilers/LLVM/mk/rules.mk b/ChibiOS_20.x.x/os/common/startup/ARMCMx/compilers/LLVM/mk/rules.mk
index bcbdcd9ac..0ce140672 100644
--- a/ChibiOS_20.x.x/os/common/startup/ARMCMx/compilers/LLVM/mk/rules.mk
+++ b/ChibiOS_20.x.x/os/common/startup/ARMCMx/compilers/LLVM/mk/rules.mk
@@ -5,6 +5,7 @@
 #
 
 # Compiler options
+LDOPT_DRIVER := $(USE_LDOPT_DRIVER)
 OPT    := $(USE_OPT)
 COPT   := $(USE_COPT)
 CPPOPT := $(USE_CPPOPT)
@@ -142,7 +143,7 @@ CPPFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
 # Paths
 IINCDIR   := $(patsubst %,-I%,$(INCDIR) $(DINCDIR) $(UINCDIR))
 LLIBDIR   := $(patsubst %,-L%,$(DLIBDIR) $(ULIBDIR) $(ARM_CORTEXM_SYSROOT)/lib/$(ARM_CORTEXM_MULTI_DIR))
-LDFLAGS   = $(MCFLAGS) $(OPT) -nostartfiles $(LLIBDIR) $(ARM_CORTEXM_BUILTINS) -Wl,-Map=$(BUILDDIR)/$(PROJECT).map,--cref,--no-warn-mismatch,--library-path=$(STARTUPLD),--script=$(LDSCRIPT)$(LDOPT)
+LDFLAGS   = $(MCFLAGS) $(OPT) $(LDOPT_DRIVER) -nostartfiles $(LLIBDIR) $(ARM_CORTEXM_BUILTINS) -Wl,-Map=$(BUILDDIR)/$(PROJECT).map,--cref,--no-warn-mismatch,--library-path=$(STARTUPLD),--script=$(LDSCRIPT)$(LDOPT)
 
 # Paths where to search for sources
 VPATH     = $(SRCPATHS)


Return to “Development and Feedback”

Who is online

Users browsing this forum: No registered users and 10 guests