Skip to content

Commit bcb515f

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 8888be7 commit bcb515f

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

0 commit comments

Comments
 (0)