Skip to content

Commit 5ee254e

Browse files
martingkellygregkh
authored andcommitted
tools: fix cross-compile var clobbering
commit 7ed1c19 upstream. Currently a number of Makefiles break when used with toolchains that pass extra flags in CC and other cross-compile related variables (such as --sysroot). Thus we get this error when we use a toolchain that puts --sysroot in the CC var: ~/src/linux/tools$ make iio [snip] iio_event_monitor.c:18:10: fatal error: unistd.h: No such file or directory #include <unistd.h> ^~~~~~~~~~ This occurs because we clobber several env vars related to cross-compiling with lines like this: CC = $(CROSS_COMPILE)gcc Although this will point to a valid cross-compiler, we lose any extra flags that might exist in the CC variable, which can break toolchains that rely on them (for example, those that use --sysroot). This easily shows up using a Yocto SDK: $ . [snip]/sdk/environment-setup-cortexa8hf-neon-poky-linux-gnueabi $ echo $CC arm-poky-linux-gnueabi-gcc -march=armv7-a -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a8 --sysroot=[snip]/sdk/sysroots/cortexa8hf-neon-poky-linux-gnueabi $ echo $CROSS_COMPILE arm-poky-linux-gnueabi- $ echo ${CROSS_COMPILE}gcc krm-poky-linux-gnueabi-gcc Although arm-poky-linux-gnueabi-gcc is a cross-compiler, we've lost the --sysroot and other flags that enable us to find the right libraries to link against, so we can't find unistd.h and other libraries and headers. Normally with the --sysroot flag we would find unistd.h in the sdk directory in the sysroot: $ find [snip]/sdk/sysroots -path '*/usr/include/unistd.h' [snip]/sdk/sysroots/cortexa8hf-neon-poky-linux-gnueabi/usr/include/unistd.h The perf Makefile adds CC = $(CROSS_COMPILE)gcc if and only if CC is not already set, and it compiles correctly with the above toolchain. So, generalize the logic that perf uses in the common Makefile and remove the manual CC = $(CROSS_COMPILE)gcc lines from each Makefile. Note that this patch does not fix cross-compile for all the tools (some have other bugs), but it does fix it for all except usb and acpi, which still have other unrelated issues. I tested both with and without the patch on native and cross-build and there appear to be no regressions. Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Martin Kelly <[email protected]> Acked-by: Mark Brown <[email protected]> Cc: Tejun Heo <[email protected]> Cc: Li Zefan <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Linus Walleij <[email protected]> Cc: "K. Y. Srinivasan" <[email protected]> Cc: Haiyang Zhang <[email protected]> Cc: Stephen Hemminger <[email protected]> Cc: Jonathan Cameron <[email protected]> Cc: Pali Rohar <[email protected]> Cc: Richard Purdie <[email protected]> Cc: Jacek Anaszewski <[email protected]> Cc: Pavel Machek <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Arnaldo Carvalho de Melo <[email protected]> Cc: Robert Moore <[email protected]> Cc: Lv Zheng <[email protected]> Cc: "Rafael J. Wysocki" <[email protected]> Cc: Greg Kroah-Hartman <[email protected]> Cc: Valentina Manea <[email protected]> Cc: Shuah Khan <[email protected]> Cc: Mario Limonciello <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> Cc: Ignat Korchagin <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 76369ed commit 5ee254e

File tree

12 files changed

+18
-21
lines changed

12 files changed

+18
-21
lines changed

tools/cgroup/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# SPDX-License-Identifier: GPL-2.0
22
# Makefile for cgroup tools
33

4-
CC = $(CROSS_COMPILE)gcc
54
CFLAGS = -Wall -Wextra
65

76
all: cgroup_event_listener

tools/gpio/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ endif
1212
# (this improves performance and avoids hard-to-debug behaviour);
1313
MAKEFLAGS += -r
1414

15-
CC = $(CROSS_COMPILE)gcc
16-
LD = $(CROSS_COMPILE)ld
1715
CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
1816

1917
ALL_TARGETS := lsgpio gpio-hammer gpio-event-mon

tools/hv/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# SPDX-License-Identifier: GPL-2.0
22
# Makefile for Hyper-V tools
33

4-
CC = $(CROSS_COMPILE)gcc
54
WARNINGS = -Wall -Wextra
65
CFLAGS = $(WARNINGS) -g $(shell getconf LFS_CFLAGS)
76

tools/iio/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ endif
1212
# (this improves performance and avoids hard-to-debug behaviour);
1313
MAKEFLAGS += -r
1414

15-
CC = $(CROSS_COMPILE)gcc
16-
LD = $(CROSS_COMPILE)ld
1715
CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
1816

1917
ALL_TARGETS := iio_event_monitor lsiio iio_generic_buffer

tools/laptop/freefall/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
PREFIX ?= /usr
33
SBINDIR ?= sbin
44
INSTALL ?= install
5-
CC = $(CROSS_COMPILE)gcc
65

76
TARGET = freefall
87

tools/leds/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# SPDX-License-Identifier: GPL-2.0
22
# Makefile for LEDs tools
33

4-
CC = $(CROSS_COMPILE)gcc
54
CFLAGS = -Wall -Wextra -g -I../../include/uapi
65

76
all: uledmon led_hw_brightness_mon

tools/perf/Makefile.perf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,6 @@ define allow-override
144144
$(eval $(1) = $(2)))
145145
endef
146146

147-
# Allow setting CC and AR and LD, or setting CROSS_COMPILE as a prefix.
148-
$(call allow-override,CC,$(CROSS_COMPILE)gcc)
149-
$(call allow-override,AR,$(CROSS_COMPILE)ar)
150-
$(call allow-override,LD,$(CROSS_COMPILE)ld)
151-
$(call allow-override,CXX,$(CROSS_COMPILE)g++)
152-
153147
LD += $(EXTRA_LDFLAGS)
154148

155149
HOSTCC ?= gcc

tools/power/acpi/Makefile.config

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ INSTALL_SCRIPT = ${INSTALL_PROGRAM}
5656
# to compile vs uClibc, that can be done here as well.
5757
CROSS = #/usr/i386-linux-uclibc/usr/bin/i386-uclibc-
5858
CROSS_COMPILE ?= $(CROSS)
59-
CC = $(CROSS_COMPILE)gcc
60-
LD = $(CROSS_COMPILE)gcc
61-
STRIP = $(CROSS_COMPILE)strip
6259
HOSTCC = gcc
6360

6461
# check if compiler option is supported

tools/scripts/Makefile.include

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ EXTRA_WARNINGS += -Wformat
4242

4343
CC_NO_CLANG := $(shell $(CC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?)
4444

45+
# Makefiles suck: This macro sets a default value of $(2) for the
46+
# variable named by $(1), unless the variable has been set by
47+
# environment or command line. This is necessary for CC and AR
48+
# because make sets default values, so the simpler ?= approach
49+
# won't work as expected.
50+
define allow-override
51+
$(if $(or $(findstring environment,$(origin $(1))),\
52+
$(findstring command line,$(origin $(1)))),,\
53+
$(eval $(1) = $(2)))
54+
endef
55+
56+
# Allow setting various cross-compile vars or setting CROSS_COMPILE as a prefix.
57+
$(call allow-override,CC,$(CROSS_COMPILE)gcc)
58+
$(call allow-override,AR,$(CROSS_COMPILE)ar)
59+
$(call allow-override,LD,$(CROSS_COMPILE)ld)
60+
$(call allow-override,CXX,$(CROSS_COMPILE)g++)
61+
$(call allow-override,STRIP,$(CROSS_COMPILE)strip)
62+
4563
ifeq ($(CC_NO_CLANG), 1)
4664
EXTRA_WARNINGS += -Wstrict-aliasing=3
4765
endif

tools/spi/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ endif
1111
# (this improves performance and avoids hard-to-debug behaviour);
1212
MAKEFLAGS += -r
1313

14-
CC = $(CROSS_COMPILE)gcc
15-
LD = $(CROSS_COMPILE)ld
1614
CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
1715

1816
ALL_TARGETS := spidev_test spidev_fdx

tools/usb/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# SPDX-License-Identifier: GPL-2.0
22
# Makefile for USB tools
33

4-
CC = $(CROSS_COMPILE)gcc
54
PTHREAD_LIBS = -lpthread
65
WARNINGS = -Wall -Wextra
76
CFLAGS = $(WARNINGS) -g -I../include

tools/vm/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ TARGETS=page-types slabinfo page_owner_sort
66
LIB_DIR = ../lib/api
77
LIBS = $(LIB_DIR)/libapi.a
88

9-
CC = $(CROSS_COMPILE)gcc
109
CFLAGS = -Wall -Wextra -I../lib/
1110
LDFLAGS = $(LIBS)
1211

0 commit comments

Comments
 (0)