Commit 5ee254ef76e8c5f052487a5c3e0cd5fcfa805ec7

Authored by Martin Kelly
Committed by Greg Kroah-Hartman
1 parent 76369ed5bc

tools: fix cross-compile var clobbering

commit 7ed1c1901fe52e6c5828deb155920b44b0adabb1 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/20180107214028.23771-1-martin@martingkelly.com
Signed-off-by: Martin Kelly <martin@martingkelly.com>
Acked-by: Mark Brown <broonie@kernel.org>
Cc: Tejun Heo <tj@kernel.org>
Cc: Li Zefan <lizefan@huawei.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Linus Walleij <linus.walleij@linaro.org>
Cc: "K. Y. Srinivasan" <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Cc: Jonathan Cameron <jic23@kernel.org>
Cc: Pali Rohar <pali.rohar@gmail.com>
Cc: Richard Purdie <rpurdie@rpsys.net>
Cc: Jacek Anaszewski <jacek.anaszewski@gmail.com>
Cc: Pavel Machek <pavel@ucw.cz>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Robert Moore <robert.moore@intel.com>
Cc: Lv Zheng <lv.zheng@intel.com>
Cc: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Valentina Manea <valentina.manea.m@gmail.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Mario Limonciello <mario.limonciello@dell.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Ignat Korchagin <ignat@cloudflare.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Showing 12 changed files with 18 additions and 21 deletions Side-by-side Diff

tools/cgroup/Makefile
1 1 # SPDX-License-Identifier: GPL-2.0
2 2 # Makefile for cgroup tools
3 3  
4   -CC = $(CROSS_COMPILE)gcc
5 4 CFLAGS = -Wall -Wextra
6 5  
7 6 all: cgroup_event_listener
... ... @@ -12,8 +12,6 @@
12 12 # (this improves performance and avoids hard-to-debug behaviour);
13 13 MAKEFLAGS += -r
14 14  
15   -CC = $(CROSS_COMPILE)gcc
16   -LD = $(CROSS_COMPILE)ld
17 15 CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
18 16  
19 17 ALL_TARGETS := lsgpio gpio-hammer gpio-event-mon
1 1 # SPDX-License-Identifier: GPL-2.0
2 2 # Makefile for Hyper-V tools
3 3  
4   -CC = $(CROSS_COMPILE)gcc
5 4 WARNINGS = -Wall -Wextra
6 5 CFLAGS = $(WARNINGS) -g $(shell getconf LFS_CFLAGS)
7 6  
... ... @@ -12,8 +12,6 @@
12 12 # (this improves performance and avoids hard-to-debug behaviour);
13 13 MAKEFLAGS += -r
14 14  
15   -CC = $(CROSS_COMPILE)gcc
16   -LD = $(CROSS_COMPILE)ld
17 15 CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
18 16  
19 17 ALL_TARGETS := iio_event_monitor lsiio iio_generic_buffer
tools/laptop/freefall/Makefile
... ... @@ -2,7 +2,6 @@
2 2 PREFIX ?= /usr
3 3 SBINDIR ?= sbin
4 4 INSTALL ?= install
5   -CC = $(CROSS_COMPILE)gcc
6 5  
7 6 TARGET = freefall
8 7  
1 1 # SPDX-License-Identifier: GPL-2.0
2 2 # Makefile for LEDs tools
3 3  
4   -CC = $(CROSS_COMPILE)gcc
5 4 CFLAGS = -Wall -Wextra -g -I../../include/uapi
6 5  
7 6 all: uledmon led_hw_brightness_mon
tools/perf/Makefile.perf
... ... @@ -144,12 +144,6 @@
144 144 $(eval $(1) = $(2)))
145 145 endef
146 146  
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   -
153 147 LD += $(EXTRA_LDFLAGS)
154 148  
155 149 HOSTCC ?= gcc
tools/power/acpi/Makefile.config
... ... @@ -56,9 +56,6 @@
56 56 # to compile vs uClibc, that can be done here as well.
57 57 CROSS = #/usr/i386-linux-uclibc/usr/bin/i386-uclibc-
58 58 CROSS_COMPILE ?= $(CROSS)
59   -CC = $(CROSS_COMPILE)gcc
60   -LD = $(CROSS_COMPILE)gcc
61   -STRIP = $(CROSS_COMPILE)strip
62 59 HOSTCC = gcc
63 60  
64 61 # check if compiler option is supported
tools/scripts/Makefile.include
... ... @@ -42,6 +42,24 @@
42 42  
43 43 CC_NO_CLANG := $(shell $(CC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?)
44 44  
  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 +
45 63 ifeq ($(CC_NO_CLANG), 1)
46 64 EXTRA_WARNINGS += -Wstrict-aliasing=3
47 65 endif
... ... @@ -11,8 +11,6 @@
11 11 # (this improves performance and avoids hard-to-debug behaviour);
12 12 MAKEFLAGS += -r
13 13  
14   -CC = $(CROSS_COMPILE)gcc
15   -LD = $(CROSS_COMPILE)ld
16 14 CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include
17 15  
18 16 ALL_TARGETS := spidev_test spidev_fdx
1 1 # SPDX-License-Identifier: GPL-2.0
2 2 # Makefile for USB tools
3 3  
4   -CC = $(CROSS_COMPILE)gcc
5 4 PTHREAD_LIBS = -lpthread
6 5 WARNINGS = -Wall -Wextra
7 6 CFLAGS = $(WARNINGS) -g -I../include
... ... @@ -6,7 +6,6 @@
6 6 LIB_DIR = ../lib/api
7 7 LIBS = $(LIB_DIR)/libapi.a
8 8  
9   -CC = $(CROSS_COMPILE)gcc
10 9 CFLAGS = -Wall -Wextra -I../lib/
11 10 LDFLAGS = $(LIBS)
12 11