Skip to content

Commit 4a532e6

Browse files
committed
Detect the sysroot path of the ARM GNU toolchain automatically
Originally, the default path of ld-linux.so is set to a specific path for Arm architecture. However, Considering that developers may manually install the ARM GNU toolchain, the sysroot path may not match the default path. This commit improves arm.mk to use a more convenient method to automatically detect the correct sysroot path.
1 parent 3a62757 commit 4a532e6

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

mk/arm.mk

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,46 @@ ARCH_DEFS = \
1212
\#define PLT_ENT_SIZE 12\n$\
1313
\#define R_ARCH_JUMP_SLOT 0x16\n$\
1414
"
15-
RUNNER_LD_PREFIX=-L /usr/arm-linux-gnueabihf/
15+
16+
# Find the sysroot of the ARM GNU toolchain if using dynamic linking.
17+
#
18+
# Since developers may install the toolchain manually instead of
19+
# using a package manager such as apt, we cannot assume that the
20+
# path of ld-linux is always "/usr/arm-linux-gnueabihf".
21+
#
22+
# Therefore, the following process first locates find the correct
23+
# sysroot of the toolchain, and then generate the ELF interpreter
24+
# prefix for later use.
25+
ifneq ($(HOST_ARCH),$(ARCH_NAME))
26+
ifeq ($(LINK_MODE),dynamic)
27+
CROSS_COMPILE = arm-none-linux-gnueabihf-
28+
ARM_CC = $(CROSS_COMPILE)gcc
29+
ARM_CC := $(shell which $(ARM_CC))
30+
ifndef ARM_CC
31+
CROSS_COMPILE = arm-linux-gnueabihf-
32+
ARM_CC = $(CROSS_COMPILE)gcc
33+
ARM_CC := $(shell which $(ARM_CC))
34+
ifndef ARM_CC
35+
$(error "Unable to find ARM GNU toolchain.")
36+
endif
37+
endif
38+
39+
LD_LINUX_PATH := $(shell cd $(shell $(ARM_CC) --print-sysroot) 2>/dev/null && pwd)
40+
ifeq ("$(LD_LINUX_PATH)","/")
41+
LD_LINUX_PATH := $(shell dirname "$(shell which $(ARM_CC))")/..
42+
LD_LINUX_PATH := $(shell cd $(LD_LINUX_PATH) 2>/dev/null && pwd)
43+
LD_LINUX_PATH := $(LD_LINUX_PATH)/$(shell echo $(CROSS_COMPILE) | sed s'/.$$//')/libc
44+
LD_LINUX_PATH := $(shell cd $(LD_LINUX_PATH) 2>/dev/null && pwd)
45+
ifndef LD_LINUX_PATH
46+
LD_LINUX_PATH = /usr/$(shell echo $(CROSS_COMPILE) | sed s'/.$$//')
47+
LD_LINUX_PATH := $(shell cd $(LD_LINUX_PATH) 2>/dev/null && pwd)
48+
endif
49+
endif
50+
51+
ifndef LD_LINUX_PATH
52+
$(error "Dynamic linking mode requires ld-linux.so")
53+
endif
54+
55+
RUNNER_LD_PREFIX = -L $(LD_LINUX_PATH)
56+
endif
57+
endif

0 commit comments

Comments
 (0)