Skip to content

Commit 1946480

Browse files
committed
Add support to build a host GCC for building the toolchain
A significant amount of users of this project are using it for toolchain development. These users need to ensure that their code is warning free before submitting it upstream. Let's support this use case by adding a configure flag '--enable-host-gcc', which does exactly that: * build a host GCC before building other components * setting PATH such that this new GCC is used to build the cross toolchain * enable -Werror for the GCC build This patch was tested on a Fedora 39 machine (GCC 13), with the following modification in a GCC source file: #if __GNUC__ == 13 #error Host compiler in use! #endif This fails when building without the new flag and does not fail when building with latest upstream/master (GCC 14 prerelease). The '--enable-werror-always' was tested with a warning that showed up recently in upstream GCC. Signed-off-by: Christoph Müllner <[email protected]>
1 parent aac79d6 commit 1946480

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

Makefile.in

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,14 @@ MUSL_CC_FOR_TARGET ?= $(MUSL_TUPLE)-gcc
119119
MUSL_CXX_FOR_TARGET ?= $(MUSL_TUPLE)-g++
120120

121121
CONFIGURE_HOST = @configure_host@
122+
PREPARATION_STAMP:=stamps/check-write-permission
122123

123124
all: @default_target@
125+
ifeq (@enable_host_gcc@,--enable-host-gcc)
126+
PREPARATION_STAMP+= stamps/install-host-gcc
127+
PATH := $(builddir)/install-host-gcc/bin:$(PATH)
128+
GCC_CHECKING_FLAGS := $(GCC_CHECKING_FLAGS) --enable-werror-always
129+
endif
124130
newlib: stamps/build-gcc-newlib-stage2
125131
linux: stamps/build-gcc-linux-stage2
126132
ifneq (,$(findstring riscv32,$(MUSL_TUPLE)))
@@ -324,11 +330,25 @@ $(srcdir)/%/.git:
324330
flock `git rev-parse --git-dir`/config git submodule init $(dir $@) && \
325331
flock `git rev-parse --git-dir`/config git submodule update $(dir $@)
326332

333+
stamps/install-host-gcc: $(GCC_SRCDIR) $(GCC_SRC_GIT)
334+
if test -f $</contrib/download_prerequisites && test "@NEED_GCC_EXTERNAL_LIBRARIES@" = "true"; then cd $< && ./contrib/download_prerequisites; fi
335+
rm -rf $@ $(notdir $@)
336+
mkdir $(notdir $@)
337+
cd $(notdir $@) && $</configure \
338+
--prefix=$(builddir)/install-host-gcc \
339+
@with_system_zlib@ \
340+
--enable-languages=c,c++ \
341+
--disable-bootstrap \
342+
--disable-multilib
343+
$(MAKE) -C $(notdir $@)
344+
$(MAKE) -C $(notdir $@) install
345+
mkdir -p $(dir $@) && touch $@
346+
327347
#
328348
# GLIBC
329349
#
330350

331-
stamps/build-binutils-linux: $(BINUTILS_SRCDIR) $(BINUTILS_SRC_GIT) stamps/check-write-permission
351+
stamps/build-binutils-linux: $(BINUTILS_SRCDIR) $(BINUTILS_SRC_GIT) $(PREPARATION_STAMP)
332352
rm -rf $@ $(notdir $@)
333353
mkdir $(notdir $@)
334354
# CC_FOR_TARGET is required for the ld testsuite.
@@ -351,7 +371,7 @@ stamps/build-binutils-linux: $(BINUTILS_SRCDIR) $(BINUTILS_SRC_GIT) stamps/check
351371
$(MAKE) -C $(notdir $@) install
352372
mkdir -p $(dir $@) && touch $@
353373

354-
stamps/build-gdb-linux: $(GDB_SRCDIR) $(GDB_SRC_GIT)
374+
stamps/build-gdb-linux: $(GDB_SRCDIR) $(GDB_SRC_GIT) $(PREPARATION_STAMP)
355375
rm -rf $@ $(notdir $@)
356376
mkdir $(notdir $@)
357377
# CC_FOR_TARGET is required for the ld testsuite.
@@ -498,7 +518,7 @@ stamps/build-gcc-linux-stage2: $(GCC_SRCDIR) $(GCC_SRC_GIT) $(addprefix stamps/b
498518
cp -a $(INSTALL_DIR)/$(LINUX_TUPLE)/lib* $(SYSROOT)
499519
mkdir -p $(dir $@) && touch $@
500520

501-
stamps/build-binutils-linux-native: $(BINUTILS_SRCDIR) $(BINUTILS_SRC_GIT) stamps/build-gcc-linux-stage2 stamps/check-write-permission
521+
stamps/build-binutils-linux-native: $(BINUTILS_SRCDIR) $(BINUTILS_SRC_GIT) stamps/build-gcc-linux-stage2 $(PREPARATION_STAMP)
502522
rm -rf $@ $(notdir $@)
503523
mkdir $(notdir $@)
504524
cd $(notdir $@) && $</configure \
@@ -555,7 +575,7 @@ stamps/build-gcc-linux-native: $(GCC_SRCDIR) $(GCC_SRC_GIT) stamps/build-gcc-lin
555575
# NEWLIB
556576
#
557577

558-
stamps/build-binutils-newlib: $(BINUTILS_SRCDIR) $(BINUTILS_SRC_GIT) stamps/check-write-permission
578+
stamps/build-binutils-newlib: $(BINUTILS_SRCDIR) $(BINUTILS_SRC_GIT) $(PREPARATION_STAMP)
559579
rm -rf $@ $(notdir $@)
560580
mkdir $(notdir $@)
561581
# CC_FOR_TARGET is required for the ld testsuite.
@@ -575,7 +595,7 @@ stamps/build-binutils-newlib: $(BINUTILS_SRCDIR) $(BINUTILS_SRC_GIT) stamps/chec
575595
$(MAKE) -C $(notdir $@) install
576596
mkdir -p $(dir $@) && touch $@
577597

578-
stamps/build-gdb-newlib: $(GDB_SRCDIR) $(GDB_SRC_GIT)
598+
stamps/build-gdb-newlib: $(GDB_SRCDIR) $(GDB_SRC_GIT) $(PREPARATION_STAMP)
579599
rm -rf $@ $(notdir $@)
580600
mkdir $(notdir $@)
581601
# CC_FOR_TARGET is required for the ld testsuite.
@@ -741,7 +761,7 @@ stamps/build-gcc-newlib-stage2: $(GCC_SRCDIR) $(GCC_SRC_GIT) stamps/build-newlib
741761
# MUSL
742762
#
743763

744-
stamps/build-binutils-musl: $(BINUTILS_SRCDIR) $(BINUTILS_SRC_GIT) stamps/check-write-permission
764+
stamps/build-binutils-musl: $(BINUTILS_SRCDIR) $(BINUTILS_SRC_GIT) $(PREPARATION_STAMP)
745765
rm -rf $@ $(notdir $@)
746766
mkdir $(notdir $@)
747767
# CC_FOR_TARGET is required for the ld testsuite.
@@ -870,7 +890,7 @@ stamps/build-gcc-musl-stage2: $(GCC_SRCDIR) $(GCC_SRC_GIT) stamps/build-musl-lin
870890
cp -a $(INSTALL_DIR)/$(MUSL_TUPLE)/lib* $(SYSROOT)
871891
mkdir -p $(dir $@) && touch $@
872892

873-
stamps/build-spike: $(SPIKE_SRCDIR) $(SPIKE_SRC_GIT)
893+
stamps/build-spike: $(SPIKE_SRCDIR) $(SPIKE_SRC_GIT) $(PREPARATION_STAMP)
874894
rm -rf $@ $(notdir $@)
875895
mkdir $(notdir $@)
876896
cd $(notdir $@) && $</configure \
@@ -906,7 +926,7 @@ stamps/build-pk64: $(PK_SRCDIR) $(PK_SRC_GIT) stamps/build-gcc-newlib-stage2
906926
mkdir -p $(dir $@)
907927
date > $@
908928

909-
stamps/build-qemu: $(QEMU_SRCDIR) $(QEMU_SRC_GIT)
929+
stamps/build-qemu: $(QEMU_SRCDIR) $(QEMU_SRC_GIT) $(PREPARATION_STAMP)
910930
rm -rf $@ $(notdir $@)
911931
mkdir $(notdir $@)
912932
cd $(notdir $@) && $</configure \
@@ -971,7 +991,7 @@ stamps/build-llvm-newlib: $(LLVM_SRCDIR) $(LLVM_SRC_GIT) stamps/build-gcc-newlib
971991
ln -s -f clang++ $(NEWLIB_TUPLE)-clang++
972992
mkdir -p $(dir $@) && touch $@
973993

974-
stamps/build-dejagnu: $(DEJAGNU_SRCDIR) $(DEJAGNU_SRC_GIT)
994+
stamps/build-dejagnu: $(DEJAGNU_SRCDIR) $(DEJAGNU_SRC_GIT) $(PREPARATION_STAMP)
975995
rm -rf $@ $(notdir $@)
976996
mkdir $(notdir $@)
977997
cd $(notdir $@) && $</configure \
@@ -1101,7 +1121,7 @@ report-binutils-linux: stamps/check-binutils-linux
11011121
`find build-binutils-linux/ -name *.sum |paste -sd "," -`
11021122

11031123
clean:
1104-
rm -rf build-* stamps install-newlib-nano
1124+
rm -rf build-* install-* stamps install-newlib-nano
11051125

11061126
.PHONY: report-gdb-newlib report-gdb-newlib-nano
11071127
report-gdb-newlib: stamps/check-gdb-newlib

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,13 @@ Here is the list of configure option for specify source tree:
447447
--with-pk-src
448448
--with-qemu-src
449449
--with-spike-src
450+
451+
#### Build host GCC to check for compiler warnings
452+
453+
GCC contributions have to meet several requirements to qualify for upstream
454+
inclusion. Warning free compilation with a compiler build from the same
455+
sources is among them. The flag `--enable-host-gcc` does exaclty that:
456+
457+
* Initially a host GCC will be built
458+
* This host GCC is then used to build the cross compiler
459+
* The cross compiler will be built with `-Werror` to identify code issues

configure

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ with_glibc_src
598598
with_newlib_src
599599
with_binutils_src
600600
with_gcc_src
601+
enable_host_gcc
601602
enable_llvm
602603
enable_gdb
603604
with_guile
@@ -697,6 +698,7 @@ with_system_zlib
697698
with_guile
698699
enable_gdb
699700
enable_llvm
701+
enable_host_gcc
700702
with_gcc_src
701703
with_binutils_src
702704
with_newlib_src
@@ -1340,6 +1342,7 @@ Optional Features:
13401342
[--disable-gcc-checking]
13411343
--disable-gdb Don't build GDB, as it's not upstream
13421344
--enable-llvm Build LLVM (clang)
1345+
--enable-host-gcc Build host GCC to build cross toolchain
13431346
--enable-libsanitizer Build libsanitizer, which only supports rv64
13441347
--enable-qemu-system Build qemu with system-mode emulation
13451348
@@ -3623,6 +3626,20 @@ else
36233626
36243627
fi
36253628
3629+
# Check whether --enable-host-gcc was given.
3630+
if test "${enable_host_gcc+set}" = set; then :
3631+
enableval=$enable_host_gcc; enable_host_gcc=yes
3632+
fi
3633+
3634+
3635+
if test "x$enable_host_gcc" != xyes; then :
3636+
enable_host_gcc=--disable-host-gcc
3637+
3638+
else
3639+
enable_host_gcc=--enable-host-gcc
3640+
3641+
fi
3642+
36263643
36273644
36283645
{

configure.ac

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@ AS_IF([test "x$enable_llvm" != xyes],
259259
[AC_SUBST(enable_llvm, --disable-llvm)],
260260
[AC_SUBST(enable_llvm, --enable-llvm)])
261261

262+
AC_ARG_ENABLE(host_gcc,
263+
[AS_HELP_STRING([--enable-host-gcc],
264+
[Build host GCC to build cross toolchain])],
265+
[enable_host_gcc=yes],
266+
[])
267+
268+
AS_IF([test "x$enable_host_gcc" != xyes],
269+
[AC_SUBST(enable_host_gcc, --disable-host-gcc)],
270+
[AC_SUBST(enable_host_gcc, --enable-host-gcc)])
271+
262272
AC_DEFUN([AX_ARG_WITH_SRC],
263273
[{m4_pushdef([opt_name], with_$1_src)
264274
AC_ARG_WITH($1-src,

0 commit comments

Comments
 (0)