Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP newlib.mk: fix regressions and global cleanups, tracking evolutions of newlib.mk #9512

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
21 changes: 13 additions & 8 deletions makefiles/libc/newlib.mk
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ export LINKFLAGS += -lc

# Search for Newlib include directories

# Function that returns absolute path to directories that contain newlib.h
# It returns the directory without trailing slash
dir_contains_newlib_h = $(realpath $(dir $(wildcard $(addsuffix /newlib.h, $(1)))))

# Try to search for newlib in the standard search path of the compiler for includes
ifeq (,$(NEWLIB_INCLUDE_DIR))
COMPILER_INCLUDE_PATHS := $(shell $(PREFIX)gcc -v -x c -E /dev/null 2>&1 | \
sed \
-e '1,/\#include <...> search starts here:/d' \
-e '/End of search list./,$$d' \
-e 's/^ *//')
NEWLIB_INCLUDE_DIR := $(firstword $(realpath $(dir $(wildcard $(addsuffix /newlib.h, $(COMPILER_INCLUDE_PATHS))))))
NEWLIB_INCLUDE_DIR := $(firstword $(call dir_contains_newlib_h, $(COMPILER_INCLUDE_PATHS)))
endif

ifeq (,$(NEWLIB_INCLUDE_DIR))
Expand Down Expand Up @@ -63,13 +67,14 @@ ifeq (,$(NEWLIB_INCLUDE_DIR))
# the patterns above. We use the -isystem gcc/clang argument to add the include
# directories as system include directories, which means they will not be
# searched until after all the project specific include directories (-I/path)
NEWLIB_INCLUDE_DIR := $(firstword $(realpath $(dir $(wildcard $(addsuffix /newlib.h, $(NEWLIB_INCLUDE_PATTERNS))))))
NEWLIB_INCLUDE_DIR := $(firstword $(call dir_contains_newlib_h, $(NEWLIB_INCLUDE_PATTERNS)))
endif

# If nothing was found we will try to fall back to searching for a cross-gcc in
# the current PATH and use a relative path for the includes
ifeq (,$(NEWLIB_INCLUDE_DIR))
NEWLIB_INCLUDE_DIR := $(realpath $(wildcard $(dir $(shell command -v $(PREFIX)gcc 2>/dev/null))/../$(TARGET_ARCH)/include))
GCC_RELATIVE_INCLUDE_PATH := $(dir $(shell command -v $(PREFIX)gcc 2>/dev/null))/../$(TARGET_ARCH)/include
NEWLIB_INCLUDE_DIR := $(call dir_contains_newlib_h, $(GCC_RELATIVE_INCLUDE_PATH))
endif

ifeq ($(TOOLCHAIN),llvm)
Expand All @@ -78,7 +83,7 @@ ifeq ($(TOOLCHAIN),llvm)
# for the system being built.
# We also add -nostdinc to avoid including the host system headers by mistake
# in case some header is missing from the cross tool chain
NEWLIB_INCLUDES := -isystem $(NEWLIB_INCLUDE_DIR) -nostdinc
NEWLIB_INCLUDES := $(addprefix -isystem ,$(NEWLIB_INCLUDE_DIR)) -nostdinc
NEWLIB_INCLUDES += $(addprefix -isystem ,$(realpath $(wildcard $(dir $(NEWLIB_INCLUDE_DIR))/usr/include)))

# Newlib includes should go before GCC includes. This is especially important
Expand All @@ -90,10 +95,10 @@ ifeq ($(TOOLCHAIN),llvm)
endif

ifeq (1,$(USE_NEWLIB_NANO))
NEWLIB_NANO_INCLUDE_DIR ?= $(firstword $(wildcard $(NEWLIB_INCLUDE_DIR)/newlib-nano \
$(NEWLIB_INCLUDE_DIR)/newlib/nano \
$(NEWLIB_INCLUDE_DIR)/nano))
NEWLIB_NANO_INCLUDE_PATTERNS ?= $(addprefix $(NEWLIB_INCLUDE_DIR)/, newlib-nano newlib/nano nano)
NEWLIB_NANO_INCLUDE_DIR ?= $(firstword $(call dir_contains_newlib_h, $(NEWLIB_NANO_INCLUDE_PATTERNS)))

# newlib-nano overrides newlib.h and its include dir should therefore go before
# the regular system include dirs.
INCLUDES := -isystem $(NEWLIB_NANO_INCLUDE_DIR) $(INCLUDES)
INCLUDES := $(addprefix -isystem ,$(NEWLIB_NANO_INCLUDE_DIR)) $(INCLUDES)
endif