Skip to content

Commit a0e323b

Browse files
ninolomatadanielRep
authored andcommitted
feat(clang): add llvm/clang support in makefile
Signed-off-by: Bruno Sa <[email protected]> Signed-off-by: David Cerdeira <[email protected]>
1 parent 4bd165c commit a0e323b

File tree

1 file changed

+64
-22
lines changed

1 file changed

+64
-22
lines changed

Makefile

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,28 @@ define current_directory
1111
$(realpath $(dir $(lastword $(MAKEFILE_LIST))))
1212
endef
1313

14+
# Check cross compiler
15+
ifneq ($(findstring clang,$(CROSS_COMPILE)),)
16+
CC_IS_CLANG = y
17+
else
18+
CC_IS_GCC = y
19+
endif
20+
1421
# Setup toolchain macros
22+
23+
ifdef CC_IS_CLANG
24+
clang_version:=$(strip $(patsubst clang%, %, $(notdir $(CROSS_COMPILE))))
25+
clang_path:=$(dir $(wildcard $(abspath $(CROSS_COMPILE))))
26+
cpp= $(clang_path)clang-cpp$(clang_version)
27+
sstrip= $(clang_path)llvm-strip$(clang_version)
28+
cc= $(clang_path)clang$(clang_version)
29+
ld = $(clang_path)ld.lld$(clang_version)
30+
as= $(clang_path)llvm-as$(clang_version)
31+
objcopy= $(clang_path)llvm-objcopy$(clang_version)
32+
objdump= $(clang_path)llvm-objdump$(clang_version)
33+
readelf= $(clang_path)llvm-readelf$(clang_version)
34+
size= $(clang_path)llvm-size$(clang_version)
35+
else
1536
cpp= $(CROSS_COMPILE)cpp
1637
sstrip= $(CROSS_COMPILE)strip
1738
cc= $(CROSS_COMPILE)gcc
@@ -21,6 +42,7 @@ objcopy= $(CROSS_COMPILE)objcopy
2142
objdump= $(CROSS_COMPILE)objdump
2243
readelf= $(CROSS_COMPILE)readelf
2344
size= $(CROSS_COMPILE)size
45+
endif
2446

2547
HOST_CC:=gcc
2648

@@ -174,36 +196,56 @@ objs-y+=$(config_obj)
174196

175197
build_macros:=
176198
ifeq ($(arch_mem_prot),mmu)
177-
build_macros+=-DMEM_PROT_MMU
199+
build_macros+=-DMEM_PROT_MMU
178200
endif
179201
ifeq ($(arch_mem_prot),mpu)
180-
build_macros+=-DMEM_PROT_MPU
202+
build_macros+=-DMEM_PROT_MPU
203+
endif
204+
205+
ifeq ($(CC_IS_GCC),y)
206+
build_macros+=-DCC_IS_GCC
207+
else ifeq ($(CC_IS_CLANG),y)
208+
build_macros+=-DCC_IS_CLANG
181209
endif
182210

183211
override CPPFLAGS+=$(addprefix -I, $(inc_dirs)) $(arch-cppflags) \
184212
$(platform-cppflags) $(build_macros)
185213
vpath:.=CPPFLAGS
186214

215+
HOST_CPPFLAGS+=$(addprefix -I, $(inc_dirs)) $(arch-cppflags) \
216+
$(platform-cppflags) $(build_macros)
217+
187218
ifeq ($(DEBUG), y)
188219
debug_flags:=-g
189220
OPTIMIZATIONS:=g
190221
endif
191222

192-
cflags_warns:= \
193-
-Warith-conversion -Wbuiltin-declaration-mismatch \
194-
-Wcomments -Wdiscarded-qualifiers \
195-
-Wimplicit-fallthrough \
196-
-Wswitch-unreachable -Wreturn-local-addr \
197-
-Wshift-count-negative -Wuninitialized \
198-
-Wunused -Wunused-local-typedefs -Wunused-parameter \
199-
-Wunused-result -Wvla \
200-
-Wconversion -Wsign-conversion \
201-
-Wmissing-prototypes -Wmissing-declarations \
202-
-Wswitch-default -Wshadow -Wshadow=global \
203-
-Wcast-qual -Wunused-macros
223+
224+
ifeq ($(CC_IS_GCC),y)
225+
cflags_warns:= \
226+
-Warith-conversion -Wbuiltin-declaration-mismatch \
227+
-Wcomments -Wdiscarded-qualifiers \
228+
-Wimplicit-fallthrough \
229+
-Wswitch-unreachable -Wreturn-local-addr \
230+
-Wshift-count-negative -Wuninitialized \
231+
-Wunused -Wunused-local-typedefs -Wunused-parameter \
232+
-Wunused-result -Wvla \
233+
-Wconversion -Wsign-conversion \
234+
-Wmissing-prototypes -Wmissing-declarations \
235+
-Wswitch-default -Wshadow -Wshadow=global \
236+
-Wcast-qual -Wunused-macros
237+
238+
override CFLAGS+=-Wno-unused-command-line-argument \
239+
-pedantic -pedantic-errors
240+
override LDFLAGS+=--no-check-sections
241+
else ifeq ($(CC_IS_CLANG), y)
242+
override CFLAGS+=-Wno-unused-command-line-argument --target=$(clang_arch_target)
243+
override CPPFLAGS+=--target=$(clang_arch_target) -ffreestanding
244+
override LDFLAGS+=--no-check-sections
245+
endif
204246

205247
override CFLAGS+=-O$(OPTIMIZATIONS) -Wall -Werror -Wextra $(cflags_warns) \
206-
-ffreestanding -std=c11 -pedantic -pedantic-errors -fno-pic \
248+
-ffreestanding -std=c11 -fno-pic \
207249
$(arch-cflags) $(platform-cflags) $(CPPFLAGS) $(debug_flags)
208250

209251
override ASFLAGS+=$(CFLAGS) $(arch-asflags) $(platform-asflags)
@@ -231,7 +273,7 @@ endif
231273

232274
$(ld_script_temp):
233275
@echo "Pre-processing $(patsubst $(cur_dir)/%, %, $(ld_script))"
234-
@$(cc) -E $(addprefix -I, $(inc_dirs)) -x assembler-with-cpp $(CPPFLAGS) \
276+
@$(cc) $(CFLAGS) -E $(addprefix -I, $(inc_dirs)) -x assembler-with-cpp $(CPPFLAGS) \
235277
$(ld_script) | grep -v '^\#' > $(ld_script_temp)
236278

237279
ifneq ($(build_targets),)
@@ -245,7 +287,7 @@ $(ld_script_temp).d: $(ld_script)
245287

246288
$(build_dir)/%.d : $(src_dir)/%.[c,S]
247289
@echo "Creating dependency $(patsubst $(cur_dir)/%, %, $<)"
248-
@$(cc) -MM -MG -MT "$(patsubst %.d, %.o, $@) $@" $(CPPFLAGS) $< > $@
290+
@$(cc) $(CFLAGS) -MM -MG -MT "$(patsubst %.d, %.o, $@) $@" $(CPPFLAGS) $< > $@
249291

250292
$(objs-y):
251293
@echo "Compiling source $(patsubst $(cur_dir)/%, %, $<)"
@@ -264,7 +306,7 @@ ifneq ($(wildcard $(asm_defs_src)),)
264306
$(asm_defs_hdr): $(asm_defs_src)
265307
@echo "Generating header $(patsubst $(cur_dir)/%, %, $@)"
266308
@$(cc) -S $(CFLAGS) -DGENERATING_DEFS $< -o - \
267-
| awk '($$1 == "->") \
309+
| awk '($$1 == "//#" || $$1 == "##" || $$1 == "@#") \
268310
{ gsub("#", "", $$3); print "#define " $$2 " " $$3 }' > $@
269311

270312
$(asm_defs_hdr).d: $(asm_defs_src)
@@ -276,13 +318,13 @@ endif
276318
$(config_dep): $(config_src)
277319
@echo "Creating dependency $(patsubst $(cur_dir)/%, %,\
278320
$(patsubst %.d,%, $@))"
279-
@$(cc) -MM -MG -MT "$(config_obj) $@" $(CPPFLAGS) $(filter %.c, $^) > $@
280-
@$(cc) $(CPPFLAGS) -S $(config_src) -o - | grep ".incbin" | \
321+
@$(cc) $(CFLAGS) -MM -MG -MT "$(config_obj) $@" $(CPPFLAGS) $(filter %.c, $^) > $@
322+
@$(cc) $(CFLAGS) $(CPPFLAGS) -S $(config_src) -o - | grep ".incbin" | \
281323
awk '{ gsub("\"", "", $$2); print "$(config_obj): " $$2 }' >> $@
282324

283325
$(config_def_generator): $(config_def_generator_src) $(config_src)
284326
@echo "Compiling generator $(patsubst $(cur_dir)/%, %, $@)"
285-
@$(HOST_CC) $^ $(build_macros) $(CPPFLAGS) -DGENERATING_DEFS \
327+
@$(HOST_CC) $^ $(build_macros) $(HOST_CPPFLAGS) -DGENERATING_DEFS \
286328
$(addprefix -I, $(inc_dirs)) -o $@
287329

288330
$(config_defs): $(config_def_generator)
@@ -291,7 +333,7 @@ $(config_defs): $(config_def_generator)
291333

292334
$(platform_def_generator): $(platform_def_generator_src) $(platform_description)
293335
@echo "Compiling generator $(patsubst $(cur_dir)/%, %, $@)"
294-
@$(HOST_CC) $^ $(build_macros) $(CPPFLAGS) -DGENERATING_DEFS -D$(ARCH) \
336+
@$(HOST_CC) $^ $(build_macros) $(HOST_CPPFLAGS) -DGENERATING_DEFS -D$(ARCH) \
295337
$(addprefix -I, $(inc_dirs)) -o $@
296338

297339
$(platform_defs): $(platform_def_generator)

0 commit comments

Comments
 (0)