Skip to content

Commit e40ca68

Browse files
committed
[lldb][test] Improve toolchain detection in Makefile.rules
This fix is based on a problem with cxx_compiler and cxx_linker macros on Windows. There was an issue with compiler detection in paths containing "icc". In such case, Makefile.rules thought it was provided with icc compiler. To solve that, utilities detection has been rewritten in Python. The last element of compiler's path is separated, taking into account platform path delimiter, and compiler type is extracted, with regard of possible cross-toolchain prefix. Paths for additional tools, like OBJCOPY, are initialized with tools built with LLVM if USE_LLVM_TOOLS is on, to achieve better portability for Windows. Quotes from paths are removed in Makefile.rules, that may be added when arguments are passed from Python to make.
1 parent 907c7eb commit e40ca68

File tree

23 files changed

+168
-104
lines changed

23 files changed

+168
-104
lines changed

lldb/packages/Python/lldbsuite/test/builders/builder.py

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import os
2+
import pathlib
23
import platform
34
import subprocess
45
import sys
56
import itertools
67

78
import lldbsuite.test.lldbtest as lldbtest
9+
import lldbsuite.test.lldbplatformutil as lldbplatformutil
810
import lldbsuite.test.lldbutil as lldbutil
911
from lldbsuite.test import configuration
1012
from lldbsuite.test_event import build_exception
@@ -96,16 +98,120 @@ def getArchSpec(self, architecture):
9698
"""
9799
return ["ARCH=" + architecture] if architecture else []
98100

99-
def getCCSpec(self, compiler):
101+
def getToolchainSpec(self, compiler):
100102
"""
101-
Helper function to return the key-value string to specify the compiler
103+
Helper function to return the key-value strings to specify the toolchain
102104
used for the make system.
103105
"""
104106
cc = compiler if compiler else None
105107
if not cc and configuration.compiler:
106108
cc = configuration.compiler
109+
107110
if cc:
108-
return ['CC="%s"' % cc]
111+
exe_ext = ""
112+
if lldbplatformutil.getHostPlatform() == "windows":
113+
exe_ext = ".exe"
114+
115+
cc = cc.strip()
116+
cc_path = pathlib.Path(cc)
117+
cc = cc_path.as_posix()
118+
119+
# We can get CC compiler string in the following formats:
120+
# [<tool>] <compiler> - such as 'xrun clang', 'xrun /usr/bin/clang' & etc
121+
#
122+
# Where <compiler> could contain the following parts:
123+
# <simple-name>[.<exe-ext>] - sucn as 'clang', 'clang.exe' ('clamg-cl.exe'?)
124+
# <target-triple>-<simple-name>[.<exe-ext>] - such as 'armv7-linux-gnueabi-gcc'
125+
# <path>/<simple-name>[.<exe-ext>] - such as '/usr/bin/clang', 'c:\path\to\compiler\clang,exe'
126+
# <path>/<target-triple>-<simple-name>[.<exe-ext>] - such as '/usr/bin/clang', 'c:\path\to\compiler\clang,exe'
127+
128+
cc_ext = cc_path.suffix
129+
# Compiler name without extension
130+
cc_name = cc_path.stem.split(" ")[-1]
131+
132+
# A kind of compiler (canonical name): clang, gcc, cc & etc.
133+
cc_type = cc_name
134+
# A triple prefix of compiler name: <armv7-none-linux-gnu->gcc
135+
cc_prefix = ""
136+
if not "clang-cl" in cc_name and not "llvm-gcc" in cc_name:
137+
cc_name_parts = cc_name.split("-")
138+
cc_type = cc_name_parts[-1]
139+
if len(cc_name_parts) > 1:
140+
cc_prefix = "-".join(cc_name_parts[:-1]) + "-"
141+
142+
# A kind of C++ compiler.
143+
cxx_types = {
144+
"icc": "icpc",
145+
"llvm-gcc": "llvm-g++",
146+
"gcc": "g++",
147+
"cc": "c++",
148+
"clang": "clang++",
149+
}
150+
cxx_type = cxx_types.get(cc_type, cc_type)
151+
152+
cc_dir = cc_path.parent
153+
154+
def getLlvmUtil(util_name):
155+
llvm_tools_dir = os.getenv("LLVM_TOOLS_DIR", cc_dir.as_posix())
156+
return llvm_tools_dir + "/" + util_name + exe_ext
157+
158+
def getToolchainUtil(util_name):
159+
return (cc_dir / (cc_prefix + util_name + cc_ext)).as_posix()
160+
161+
cxx = getToolchainUtil(cxx_type)
162+
163+
util_names = {
164+
"OBJCOPY": "objcopy",
165+
"STRIP": "objcopy",
166+
"ARCHIVER": "ar",
167+
"DWP": "dwp",
168+
}
169+
utils = []
170+
171+
# Note: LLVM_AR is currently required by API TestBSDArchives.py tests.
172+
# Assembly a full path to llvm-ar for given LLVM_TOOLS_DIR;
173+
# otherwise assume we have llvm-ar at the same place where is CC specified.
174+
if not os.getenv("LLVM_AR"):
175+
llvm_ar = getLlvmUtil("llvm-ar")
176+
utils.extend(['LLVM_AR="%s"' % llvm_ar])
177+
178+
if not lldbplatformutil.platformIsDarwin():
179+
if os.getenv("USE_LLVM_TOOLS"):
180+
# Use the llvm project tool instead of the system defaults.
181+
# Note: do not override explicity specified tool from the cmd line.
182+
for var, name in util_names.items():
183+
utils.append('%s="%s"' % (var, getLlvmUtil("llvm-" + name)))
184+
utils.extend(['AR="%s"' % llvm_ar])
185+
elif cc_type in ["clang", "cc", "gcc"]:
186+
util_paths = {}
187+
# Assembly a toolchain side tool cmd based on passed CC.
188+
for var, name in util_names.items():
189+
if not os.getenv(var):
190+
util_paths[var] = getToolchainUtil(name)
191+
else:
192+
util_paths[var] = os.getenv(var)
193+
utils.extend(['AR="%s"' % util_paths["ARCHIVER"]])
194+
195+
# Look for llvm-dwp or gnu dwp
196+
if not lldbutil.which(util_paths["DWP"]):
197+
util_paths["DWP"] = getToolchainUtil("llvm-dwp")
198+
if not lldbutil.which(util_paths["DWP"]):
199+
util_paths["DWP"] = lldbutil.which("llvm-dwp")
200+
if not util_paths["DWP"]:
201+
util_paths["DWP"] = lldbutil.which("dwp")
202+
if not util_paths["DWP"]:
203+
del util_paths["DWP"]
204+
205+
for var, path in util_paths.items():
206+
utils.append('%s="%s"' % (var, path))
207+
else:
208+
utils.extend(['AR="%slibtool"' % os.getenv("CROSS_COMPILE", "")])
209+
210+
return [
211+
'CC="%s"' % cc,
212+
'CCC="%s"' % cc_type,
213+
'CXX="%s"' % cxx,
214+
] + utils
109215
return []
110216

111217
def getSDKRootSpec(self):
@@ -178,7 +284,7 @@ def getBuildCommand(
178284
make_targets,
179285
self.getArchCFlags(architecture),
180286
self.getArchSpec(architecture),
181-
self.getCCSpec(compiler),
287+
self.getToolchainSpec(compiler),
182288
self.getExtraMakeArgs(),
183289
self.getSDKRootSpec(),
184290
self.getModuleCacheSpec(),

lldb/packages/Python/lldbsuite/test/make/Makefile.rules

Lines changed: 29 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,33 @@ endif
102102
# If you change the defaults of CC, be sure to also change it in the file
103103
# test/builders/builder_base.py, which provides a Python way to return the
104104
# value of the make variable CC -- getCompiler().
105-
#
106-
# See also these functions:
107-
# o cxx_compiler
108-
# o cxx_linker
109105
#----------------------------------------------------------------------
110106
ifeq "$(CC)" ""
111107
$(error "C compiler is not specified. Please run tests through lldb-dotest or lit")
112108
endif
113109

110+
# Remove all " and ' characters from the path. Also remove surrounding space chars.
111+
cstrip = $(strip $(subst ",,$(subst ',,$(1))))
112+
override CC := $(call cstrip,$(CC))
113+
override CCC := $(call cstrip,$(CCC))
114+
override CXX := $(call cstrip,$(CXX))
115+
# Always override the linker. Assign already normalized CC.
116+
override LD := $(call cstrip,$(CC))
117+
# A kind of linker. It always gets retrieved from CC.
118+
override LDC := $(call cstrip,$(CCC))
119+
override OBJCOPY := $(call cstrip,$(OBJCOPY))
120+
override STRIP := $(call cstrip,$(STRIP))
121+
override ARCHIVER := $(call cstrip,$(ARCHIVER))
122+
override AR := $(call cstrip,$(AR))
123+
override DWP := $(call cstrip,$(DWP))
124+
override LLVM_AR := $(call cstrip,$(LLVM_AR))
125+
126+
ifeq "$(HOST_OS)" "Windows_NT"
127+
# This function enframes the full path with the platform specific quotes. This is necessary to run the c++ executable
128+
# properly under 'sh' on Windows host (prevent the path breakage because of Windows style path separators).
129+
override CXX := $(QUOTE)$(CXX)$(QUOTE)
130+
endif
131+
114132
#----------------------------------------------------------------------
115133
# Handle SDKROOT for the cross platform builds.
116134
#----------------------------------------------------------------------
@@ -147,10 +165,8 @@ ifeq "$(OS)" "Darwin"
147165
DS := $(DSYMUTIL)
148166
DSFLAGS := $(DSFLAGS_EXTRAS)
149167
DSYM = $(EXE).dSYM
150-
AR := $(CROSS_COMPILE)libtool
151168
ARFLAGS := -static -o
152169
else
153-
AR := $(CROSS_COMPILE)ar
154170
# On non-Apple platforms, -arch becomes -m
155171
ARCHFLAG := -m
156172

@@ -213,7 +229,7 @@ endif
213229
LIMIT_DEBUG_INFO_FLAGS =
214230
NO_LIMIT_DEBUG_INFO_FLAGS =
215231
MODULE_DEBUG_INFO_FLAGS =
216-
ifneq (,$(findstring clang,$(CC)))
232+
ifeq ($(CCC), clang)
217233
LIMIT_DEBUG_INFO_FLAGS += -flimit-debug-info
218234
NO_LIMIT_DEBUG_INFO_FLAGS += -fno-limit-debug-info
219235
MODULE_DEBUG_INFO_FLAGS += -gmodules
@@ -279,7 +295,6 @@ endif
279295

280296
CFLAGS += $(CFLAGS_EXTRAS)
281297
CXXFLAGS += -std=c++11 $(CFLAGS) $(ARCH_CXXFLAGS)
282-
LD = $(CC)
283298
# Copy common options to the linker flags (dwarf, arch. & etc).
284299
# Note: we get some 'garbage' options for linker here (such as -I, --isystem & etc).
285300
LDFLAGS += $(CFLAGS)
@@ -312,61 +327,6 @@ ifneq "$(DYLIB_NAME)" ""
312327
endif
313328
endif
314329

315-
# Function that returns the counterpart C++ compiler, given $(CC) as arg.
316-
cxx_compiler_notdir = $(if $(findstring icc,$(1)), \
317-
$(subst icc,icpc,$(1)), \
318-
$(if $(findstring llvm-gcc,$(1)), \
319-
$(subst llvm-gcc,llvm-g++,$(1)), \
320-
$(if $(findstring gcc,$(1)), \
321-
$(subst gcc,g++,$(1)), \
322-
$(subst cc,c++,$(1)))))
323-
cxx_compiler = $(if $(findstring /,$(1)),$(join $(dir $(1)), $(call cxx_compiler_notdir,$(notdir $(1)))),$(call cxx_compiler_notdir,$(1)))
324-
325-
# Function that returns the C++ linker, given $(CC) as arg.
326-
cxx_linker_notdir = $(if $(findstring icc,$(1)), \
327-
$(subst icc,icpc,$(1)), \
328-
$(if $(findstring llvm-gcc,$(1)), \
329-
$(subst llvm-gcc,llvm-g++,$(1)), \
330-
$(if $(findstring gcc,$(1)), \
331-
$(subst gcc,g++,$(1)), \
332-
$(subst cc,c++,$(1)))))
333-
cxx_linker = $(if $(findstring /,$(1)),$(join $(dir $(1)), $(call cxx_linker_notdir,$(notdir $(1)))),$(call cxx_linker_notdir,$(1)))
334-
335-
ifneq "$(OS)" "Darwin"
336-
CLANG_OR_GCC := $(strip $(if $(findstring clang,$(CC)), \
337-
$(findstring clang,$(CC)), \
338-
$(if $(findstring gcc,$(CC)), \
339-
$(findstring gcc,$(CC)), \
340-
cc)))
341-
342-
CC_LASTWORD := $(strip $(lastword $(subst -, ,$(CC))))
343-
344-
replace_with = $(strip $(if $(findstring $(3),$(CC_LASTWORD)), \
345-
$(subst $(3),$(1),$(2)), \
346-
$(subst $(3),$(1),$(subst -$(CC_LASTWORD),,$(2)))))
347-
348-
ifeq "$(notdir $(CC))" "$(CC)"
349-
replace_cc_with = $(call replace_with,$(1),$(CC),$(CLANG_OR_GCC))
350-
else
351-
replace_cc_with = $(join $(dir $(CC)),$(call replace_with,$(1),$(notdir $(CC)),$(CLANG_OR_GCC)))
352-
endif
353-
354-
OBJCOPY ?= $(call replace_cc_with,objcopy)
355-
ARCHIVER ?= $(call replace_cc_with,ar)
356-
# Look for llvm-dwp or gnu dwp
357-
DWP ?= $(call replace_cc_with,llvm-dwp)
358-
ifeq ($(wildcard $(DWP)),)
359-
DWP = $(call replace_cc_with,dwp)
360-
ifeq ($(wildcard $(DWP)),)
361-
DWP = $(shell command -v llvm-dwp 2> /dev/null)
362-
ifeq ($(wildcard $(DWP)),)
363-
DWP = $(shell command -v dwp 2> /dev/null)
364-
endif
365-
endif
366-
endif
367-
override AR = $(ARCHIVER)
368-
endif
369-
370330
ifdef PIE
371331
LDFLAGS += -pie
372332
endif
@@ -375,7 +335,7 @@ endif
375335
# Windows specific options
376336
#----------------------------------------------------------------------
377337
ifeq "$(OS)" "Windows_NT"
378-
ifneq (,$(findstring clang,$(CC)))
338+
ifeq ($(CCC), clang)
379339
# Clang for Windows doesn't support C++ Exceptions
380340
CXXFLAGS += -fno-exceptions
381341
CXXFLAGS += -D_HAS_EXCEPTIONS=0
@@ -420,7 +380,7 @@ endif
420380

421381
ifeq (1,$(USE_LIBSTDCPP))
422382
# Clang requires an extra flag: -stdlib=libstdc++
423-
ifneq (,$(findstring clang,$(CC)))
383+
ifeq ($(CCC), clang)
424384
# Force clang looking for the gcc's headers at specific rootfs folder.
425385
CXXFLAGS += -stdlib=libstdc++ $(GCC_TOOLCHAIN_FLAGS)
426386
LDFLAGS += -stdlib=libstdc++ $(GCC_TOOLCHAIN_FLAGS)
@@ -458,7 +418,7 @@ ifeq (1, $(USE_SYSTEM_STDLIB))
458418
CXXFLAGS += -nostdlib++ -nostdinc++ -cxx-isystem $(SDKROOT)/usr/include/c++/v1
459419
LDFLAGS += -L$(SDKROOT)/usr/lib -Wl,-rpath,$(SDKROOT)/usr/lib -lc++
460420
else
461-
ifneq (,$(findstring clang,$(CC)))
421+
ifeq ($(CCC), clang)
462422
# Force clang looking for the gcc's headers at specific rootfs folder.
463423
CXXFLAGS += $(GCC_TOOLCHAIN_FLAGS)
464424
LDFLAGS += $(GCC_TOOLCHAIN_FLAGS)
@@ -485,8 +445,6 @@ DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o))
485445
DYLIB_OBJECTS +=$(strip $(DYLIB_OBJC_SOURCES:.m=.o))
486446
ifneq "$(strip $(DYLIB_CXX_SOURCES))" ""
487447
DYLIB_OBJECTS +=$(strip $(patsubst %.mm, %.o, $(DYLIB_CXX_SOURCES:.cpp=.o)))
488-
CXX = $(call cxx_compiler,$(CC))
489-
LD = $(call cxx_linker,$(CC))
490448
endif
491449

492450
#----------------------------------------------------------------------
@@ -509,8 +467,6 @@ endif
509467
#----------------------------------------------------------------------
510468
ifneq "$(strip $(CXX_SOURCES))" ""
511469
OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o))
512-
CXX = $(call cxx_compiler,$(CC))
513-
LD = $(call cxx_linker,$(CC))
514470
endif
515471

516472
#----------------------------------------------------------------------
@@ -526,19 +482,18 @@ endif
526482
#----------------------------------------------------------------------
527483
ifneq "$(strip $(OBJCXX_SOURCES))" ""
528484
OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o))
529-
CXX = $(call cxx_compiler,$(CC))
530-
LD = $(call cxx_linker,$(CC))
531485
ifeq "$(findstring lobjc,$(LDFLAGS))" ""
532486
LDFLAGS +=-lobjc
533487
endif
534488
endif
535489

536-
ifeq ($(findstring clang, $(CXX)), clang)
490+
ifeq ($(CCC), clang)
537491
CXXFLAGS += --driver-mode=g++
538492
endif
539493

540494
ifneq "$(CXX)" ""
541-
ifeq ($(findstring clang, $(LD)), clang)
495+
# Specify the driver mode parameter if we use clang as the linker.
496+
ifeq ($(LDC), clang)
542497
LDFLAGS += --driver-mode=g++
543498
endif
544499
endif

lldb/test/API/functionalities/breakpoint/breakpoint_ids/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CXX_SOURCES := main.cpp
22

3-
ifneq (,$(findstring icc,$(CC)))
3+
ifeq ($(CCC), icc)
44
CXXFLAGS_EXTRAS := -debug inline-debug-info
55
endif
66

lldb/test/API/functionalities/breakpoint/breakpoint_locations/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
C_SOURCES := main.c
22

3-
ifneq (,$(findstring icc,$(CC)))
3+
ifeq ($(CCC), icc)
44
CFLAGS_EXTRAS := -debug inline-debug-info
55
endif
66

lldb/test/API/functionalities/breakpoint/consecutive_breakpoints/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CXX_SOURCES := main.cpp
22

3-
ifneq (,$(findstring icc,$(CC)))
3+
ifeq ($(CCC), icc)
44
CXXFLAGS_EXTRAS := -debug inline-debug-info
55
endif
66

lldb/test/API/functionalities/breakpoint/cpp/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CXX_SOURCES := main.cpp
22
CXXFLAGS_EXTRAS := -std=c++14
33

4-
ifneq (,$(findstring icc,$(CC)))
4+
ifeq ($(CCC), icc)
55
CXXFLAGS_EXTRAS := -debug inline-debug-info
66
endif
77

lldb/test/API/functionalities/breakpoint/dummy_target_breakpoints/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
C_SOURCES := main.c
22

3-
ifneq (,$(findstring icc,$(CC)))
3+
ifeq ($(CCC), icc)
44
CFLAGS_EXTRAS := -debug inline-debug-info
55
endif
66

lldb/test/API/functionalities/breakpoint/hardware_breakpoints/require_hw_breakpoints/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
C_SOURCES := main.c
22

3-
ifneq (,$(findstring icc,$(CC)))
3+
ifeq ($(CCC), icc)
44
CFLAGS_EXTRAS := -debug inline-debug-info
55
endif
66

lldb/test/API/functionalities/breakpoint/step_over_breakpoint/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CXX_SOURCES := main.cpp
22

3-
ifneq (,$(findstring icc,$(CC)))
3+
ifeq ($(CCC), icc)
44
CXXFLAGS_EXTRAS := -debug inline-debug-info
55
endif
66

0 commit comments

Comments
 (0)