move build tool selection into separate makefile fragment

Also adds toolchain detection allowing for warning flags selection
This commit is contained in:
Vincent Sanders 2024-02-27 09:26:37 +00:00
parent 91b29b626f
commit 138d8438d4
2 changed files with 133 additions and 32 deletions

View File

@ -40,13 +40,6 @@ MESSAGES_LANGUAGES=de en fr it nl zh_CN
# The target directory for the split messages
MESSAGES_TARGET=resources
# Defaults for tools
PERL=perl
MKDIR=mkdir
TOUCH=touch
STRIP?=strip
INSTALL?=install
# build verbosity
ifeq ($(V),1)
Q:=
@ -55,31 +48,11 @@ else
endif
VQ=@
# Override this only if the host compiler is called something different
BUILD_CC := cc
BUILD_CFLAGS = -g -W -Wall -Wundef -Wpointer-arith -Wcast-align \
-Wwrite-strings -Wmissing-declarations -Wuninitialized \
-Wno-unused-parameter
# compute HOST, TARGET and SUBTARGET
include frontends/Makefile.hts
# target specific tool overrides
include frontends/$(TARGET)/Makefile.tools
# compiler versioning to adjust warning flags
CC_VERSION := $(shell $(CC) -dumpfullversion -dumpversion)
CC_MAJOR := $(word 1,$(subst ., ,$(CC_VERSION)))
CC_MINOR := $(word 2,$(subst ., ,$(CC_VERSION)))
define cc_ver_ge
$(shell expr $(CC_MAJOR) \> $(1) \| \( $(CC_MAJOR) = $(1) \& $(CC_MINOR) \>= $(2) \) )
endef
# CCACHE
ifeq ($(origin CCACHE),undefined)
CCACHE=$(word 1,$(shell ccache -V 2>/dev/null))
endif
CC := $(CCACHE) $(CC)
# tools used in builds
include Makefile.tools
# Target paths
OBJROOT = build/$(HOST)-$(TARGET)$(SUBTARGET)
@ -97,6 +70,11 @@ include Makefile.macros
# General flag setup
# ----------------------------------------------------------------------------
# host compiler flags
BUILD_CFLAGS = -g -W -Wall -Wundef -Wpointer-arith -Wcast-align \
-Wwrite-strings -Wmissing-declarations -Wuninitialized \
-Wno-unused-parameter
# Set up the warning flags here so that they can be overridden in the
# Makefile.config
COMMON_WARNFLAGS = -W -Wall -Wundef -Wpointer-arith -Wcast-align \
@ -111,9 +89,14 @@ ifeq ($(call cc_ver_ge,4,6),1)
COMMON_WARNFLAGS += -Wno-unused-but-set-variable
endif
# Implicit fallthrough warnings suppressed by comment
ifeq ($(call cc_ver_ge,7,1),1)
COMMON_WARNFLAGS += -Wimplicit-fallthrough=3
ifeq ($(TOOLCHAIN),gcc)
# Implicit fallthrough warnings suppressed by comment
ifeq ($(call cc_ver_ge,7,1),1)
COMMON_WARNFLAGS += -Wimplicit-fallthrough=3
endif
else
# clang uses [[clang::fallthrough]] to annotate fallthrough instead of comments
COMMON_WARNFLAGS += -Wno-implicit-fallthrough
endif
# deal with chaging warning flags for different platforms

118
Makefile.tools Normal file
View File

@ -0,0 +1,118 @@
# -*- mode: makefile-gmake -*-
##
## Setup tools to build project including compiler
##
# Expected inputs:
#
# HOST
# TARGET
# SUBTARGET
#
# output:
# variables:
# CC
# BUILD_CC
# CCACHE
# TOOLCHAIN
# PERL
# MKDIR
# TOUCH
# STRIP
# INSTALL
# CC_VERSION
# CC_MAJOR
# CC_MINOR
#
# macros:
# cc_ver_ge
# Defaults for tools
PERL=perl
MKDIR=mkdir
TOUCH=touch
STRIP?=strip
INSTALL?=install
# Override this only if the host compiler is called something different
BUILD_CC := cc
# target specific tool overrides
include frontends/$(TARGET)/Makefile.tools
# CCACHE
ifeq ($(origin CCACHE),undefined)
CCACHE=$(word 1,$(shell ccache -V 2>/dev/null))
endif
CC := $(CCACHE) $(CC)
###############################################################################
# Auto-detect the toolchain
###############################################################################
# Check for GCC first, as that's most likely
# TODO: Using shell redirection like this probably hurts portability
CC_SPECS := $(shell $(CC) -dumpspecs 2>&1)
ifeq ($(findstring libgcc,$(CC_SPECS)),libgcc)
# Looks like GCC
TOOLCHAIN := gcc
else
# Not GCC, so enquire further
ccvsn := $(shell $(CC) --version 2>&1)
ifeq ($(ccvsn),)
# Version string is blank
ifeq ($(BUILD),arm-unknown-riscos)
# For some reason we never see the output of SCL apps, so might be
# Norcroft. However it might also be a GCC linked against a buggy
# UnixLib.
# TODO: Something more useful than blindly assuming GCC.
ccvsn := GCC
# ccvsn := Norcroft
endif
endif
ifeq ($(findstring lcc:,$(ccvsn)),lcc:)
# MCST LCC pretends to be gcc
TOOLCHAIN := gcc
endif
# "Norcroft ..."
ifeq ($(word 1,$(ccvsn)),Norcroft)
TOOLCHAIN := norcroft
endif
# "GCC ..."
ifeq ($(word 1,$(ccvsn)),GCC)
TOOLCHAIN := gcc
endif
# "clang ..."
ifeq ($(word 1,$(ccvsn)),clang)
TOOLCHAIN := clang
endif
ifeq ($(word 2,$(ccvsn)),clang)
# Some newer clangs have distributor as first word
# (ie, Debian, Apple, etc)
TOOLCHAIN := clang
endif
ifeq ($(word 2,$(ccvsn)),LLVM)
# Apple version is "Apple LLVM" to be differntly awkward
TOOLCHAIN := clang
endif
ifeq ($(word 1,$(ccvsn)),Open64)
TOOLCHAIN := open64
endif
endif
ifeq ($(TOOLCHAIN),)
$(error Unable to detect toolchain)
endif
###############################################################################
# Compiler Versioning (to adjust warning flags)
###############################################################################
CC_VERSION := $(shell $(CC) -dumpfullversion -dumpversion)
CC_MAJOR := $(word 1,$(subst ., ,$(CC_VERSION)))
CC_MINOR := $(word 2,$(subst ., ,$(CC_VERSION)))
define cc_ver_ge
$(shell expr $(CC_MAJOR) \> $(1) \| \( $(CC_MAJOR) = $(1) \& $(CC_MINOR) \>= $(2) \) )
endef