Skip to contentSkip to conversation

Makefile: do not use "libgcc" for clang

PATCH In review Bill Wendling 3 participants 4 messages Mar 9, 2021 – May 5, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Makefile: do not use "libgcc" for clang

The -nostdlib flag disables the driver from adding libclang_rt.*.a during linking. Adding a specific library to the command line then causes the linker to report unresolved symbols, because the libraries that resolve those symbols aren't automatically added. Turns out clang doesn't need to specify that library.

---
 Makefile            | 6 ++++++
 arm/Makefile.common | 2 ++
 x86/Makefile.common | 2 ++
 3 files changed, 10 insertions(+)
Makefile +6 -0
index e0828fe..61a1276 100644
@@ -22,10 +22,16 @@ DESTDIR := $(PREFIX)/share/kvm-unit-tests/
 cc-option = $(shell if $(CC) -Werror $(1) -S -o /dev/null -xc /dev/null \
               > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
 
+# cc-name
+# Expands to either gcc or clang
+cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc)
+
 #make sure env CFLAGS variable is not used
 CFLAGS =
 
+ifneq ($(cc-name),clang)
 libgcc := $(shell $(CC) --print-libgcc-file-name)
+endif
 
 libcflat := lib/libcflat.a
 cflatobjs := \
arm/Makefile.common +2 -0
index a123e85..94922aa 100644
@@ -58,7 +58,9 @@ OBJDIRS += lib/arm
 libeabi = lib/arm/libeabi.a
 eabiobjs = lib/arm/eabi_compat.o
 
+ifneq ($(cc-name),clang)
 libgcc := $(shell $(CC) $(machine) --print-libgcc-file-name)
+endif
 
 FLATLIBS = $(libcflat) $(LIBFDT_archive) $(libgcc) $(libeabi)
 %.elf: LDFLAGS = -nostdlib $(arch_LDFLAGS)
x86/Makefile.common +2 -0
index 55f7f28..a96b236 100644
@@ -37,7 +37,9 @@ COMMON_CFLAGS += -O1
 # stack.o relies on frame pointers.
 KEEP_FRAME_POINTER := y
 
+ifneq ($(cc-name),clang)
 libgcc := $(shell $(CC) -m$(bits) --print-libgcc-file-name)
+endif
 
 # We want to keep intermediate file: %.elf and %.o 
 .PRECIOUS: %.elf %.o
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#1 lore raw

Ping.

Quoted 66 lines · Bill Wendling

The -nostdlib flag disables the driver from adding libclang_rt.*.a during linking. Adding a specific library to the command line then causes the linker to report unresolved symbols, because the libraries that resolve those symbols aren't automatically added. Turns out clang doesn't need to specify that library.

---
 Makefile            | 6 ++++++
 arm/Makefile.common | 2 ++
 x86/Makefile.common | 2 ++
 3 files changed, 10 insertions(+)
Makefile +6 -0
index e0828fe..61a1276 100644
@@ -22,10 +22,16 @@ DESTDIR := $(PREFIX)/share/kvm-unit-tests/
 cc-option = $(shell if $(CC) -Werror $(1) -S -o /dev/null -xc /dev/null \
               > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
 
+# cc-name
+# Expands to either gcc or clang
+cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc)
+
 #make sure env CFLAGS variable is not used
 CFLAGS =
 
+ifneq ($(cc-name),clang)
 libgcc := $(shell $(CC) --print-libgcc-file-name)
+endif
 
 libcflat := lib/libcflat.a
 cflatobjs := \
arm/Makefile.common +2 -0
index a123e85..94922aa 100644
@@ -58,7 +58,9 @@ OBJDIRS += lib/arm
 libeabi = lib/arm/libeabi.a
 eabiobjs = lib/arm/eabi_compat.o
 
+ifneq ($(cc-name),clang)
 libgcc := $(shell $(CC) $(machine) --print-libgcc-file-name)
+endif
 
 FLATLIBS = $(libcflat) $(LIBFDT_archive) $(libgcc) $(libeabi)
 %.elf: LDFLAGS = -nostdlib $(arch_LDFLAGS)
x86/Makefile.common +2 -1
index 55f7f28..a96b236 100644
@@ -37,7 +37,9 @@ COMMON_CFLAGS += -O1
 # stack.o relies on frame pointers.
 KEEP_FRAME_POINTER := y
 
+ifneq ($(cc-name),clang)
 libgcc := $(shell $(CC) -m$(bits) --print-libgcc-file-name)
+endif
 
 # We want to keep intermediate file: %.elf and %.o
 .PRECIOUS: %.elf %.o
--

2.30.1.766.gb4fecdf3b7-goog

Reviewed-by on v1 1/1
#2 lore raw
In reply to Bill Wendling
Quoted 8 lines · Bill Wendling

The -nostdlib flag disables the driver from adding libclang_rt.*.a during linking. Adding a specific library to the command line then causes the linker to report unresolved symbols, because the libraries that resolve those symbols aren't automatically added. Turns out clang doesn't need to specify that library.

maintainer
#3 lore raw
In reply to Bill Wendling
Quoted 5 lines · Bill Wendling

The -nostdlib flag disables the driver from adding libclang_rt.*.a during linking. Adding a specific library to the command line then causes the linker to report unresolved symbols, because the libraries that resolve those symbols aren't automatically added. Turns out clang doesn't need to specify that library.

This breaks 32-bit build with clang due to __udivdi3/__umoddi3. Let me post an alternative.

Paolo

Quoted 57 lines
---
  Makefile            | 6 ++++++
  arm/Makefile.common | 2 ++
  x86/Makefile.common | 2 ++
  3 files changed, 10 insertions(+)
Makefile +6 -0
index e0828fe..61a1276 100644
@@ -22,10 +22,16 @@ DESTDIR := $(PREFIX)/share/kvm-unit-tests/
  cc-option = $(shell if $(CC) -Werror $(1) -S -o /dev/null -xc /dev/null \
                > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
 
+# cc-name
+# Expands to either gcc or clang
+cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc)
+
  #make sure env CFLAGS variable is not used
  CFLAGS =
 
+ifneq ($(cc-name),clang)
  libgcc := $(shell $(CC) --print-libgcc-file-name)
+endif
 
  libcflat := lib/libcflat.a
  cflatobjs := \
arm/Makefile.common +2 -0
index a123e85..94922aa 100644
@@ -58,7 +58,9 @@ OBJDIRS += lib/arm
  libeabi = lib/arm/libeabi.a
  eabiobjs = lib/arm/eabi_compat.o
 
+ifneq ($(cc-name),clang)
  libgcc := $(shell $(CC) $(machine) --print-libgcc-file-name)
+endif
 
  FLATLIBS = $(libcflat) $(LIBFDT_archive) $(libgcc) $(libeabi)
  %.elf: LDFLAGS = -nostdlib $(arch_LDFLAGS)
x86/Makefile.common +2 -0
index 55f7f28..a96b236 100644
@@ -37,7 +37,9 @@ COMMON_CFLAGS += -O1
  # stack.o relies on frame pointers.
  KEEP_FRAME_POINTER := y
 
+ifneq ($(cc-name),clang)
  libgcc := $(shell $(CC) -m$(bits) --print-libgcc-file-name)
+endif
 
  # We want to keep intermediate file: %.elf and %.o
  .PRECIOUS: %.elf %.o