Skip to content

Commit 5130b2a

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 f949b03 commit 5130b2a

File tree

23 files changed

+154
-93
lines changed

23 files changed

+154
-93
lines changed

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

Lines changed: 97 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,107 @@ 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+
}
168+
utils = []
169+
170+
# Note: LLVM_AR is currently required by API TestBSDArchives.py tests.
171+
# Assembly a full path to llvm-ar for given LLVM_TOOLS_DIR;
172+
# otherwise assume we have llvm-ar at the same place where is CC specified.
173+
if not os.getenv("LLVM_AR"):
174+
llvm_ar = getLlvmUtil("llvm-ar")
175+
utils.extend(['LLVM_AR="%s"' % llvm_ar])
176+
177+
if not lldbplatformutil.platformIsDarwin():
178+
if os.getenv("USE_LLVM_TOOLS"):
179+
# Use the llvm project tool instead of the system defaults.
180+
# Note: do not override explicity specified tool from the cmd line.
181+
for var, name in util_names.items():
182+
utils.append('%s="%s"' % (var, getLlvmUtil("llvm-" + name)))
183+
utils.extend(['AR="%s"' % llvm_ar])
184+
elif cc_type in ["clang", "cc", "gcc"]:
185+
util_paths = {}
186+
# Assembly a toolchain side tool cmd based on passed CC.
187+
for var, name in util_names.items():
188+
if not os.getenv(var):
189+
util_paths[var] = getToolchainUtil(name)
190+
utils.append('%s="%s"' % (var, util_paths[var]))
191+
else:
192+
util_paths[var] = os.getenv(var)
193+
utils.extend(['AR="%s"' % util_paths["ARCHIVER"]])
194+
else:
195+
utils.extend(['AR="%slibtool"' % os.getenv("CROSS_COMPILE", "")])
196+
197+
return [
198+
'CC="%s"' % cc,
199+
'CCC="%s"' % cc_type,
200+
'CXX="%s"' % cxx,
201+
] + utils
109202
return []
110203

111204
def getSDKRootSpec(self):
@@ -178,7 +271,7 @@ def getBuildCommand(
178271
make_targets,
179272
self.getArchCFlags(architecture),
180273
self.getArchSpec(architecture),
181-
self.getCCSpec(compiler),
274+
self.getToolchainSpec(compiler),
182275
self.getExtraMakeArgs(),
183276
self.getSDKRootSpec(),
184277
self.getModuleCacheSpec(),

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

Lines changed: 28 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,32 @@ 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 LLVM_AR := $(call cstrip,$(LLVM_AR))
124+
125+
ifeq "$(HOST_OS)" "Windows_NT"
126+
# This function enframes the full path with the platform specific quotes. This is necessary to run the c++ executable
127+
# properly under 'sh' on Windows host (prevent the path breakage because of Windows style path separators).
128+
override CXX := $(QUOTE)$(CXX)$(QUOTE)
129+
endif
130+
114131
#----------------------------------------------------------------------
115132
# Handle SDKROOT for the cross platform builds.
116133
#----------------------------------------------------------------------
@@ -147,10 +164,8 @@ ifeq "$(OS)" "Darwin"
147164
DS := $(DSYMUTIL)
148165
DSFLAGS := $(DSFLAGS_EXTRAS)
149166
DSYM = $(EXE).dSYM
150-
AR := $(CROSS_COMPILE)libtool
151167
ARFLAGS := -static -o
152168
else
153-
AR := $(CROSS_COMPILE)ar
154169
# On non-Apple platforms, -arch becomes -m
155170
ARCHFLAG := -m
156171

@@ -207,7 +222,7 @@ endif
207222
LIMIT_DEBUG_INFO_FLAGS =
208223
NO_LIMIT_DEBUG_INFO_FLAGS =
209224
MODULE_DEBUG_INFO_FLAGS =
210-
ifneq (,$(findstring clang,$(CC)))
225+
ifeq ($(CCC), clang)
211226
LIMIT_DEBUG_INFO_FLAGS += -flimit-debug-info
212227
NO_LIMIT_DEBUG_INFO_FLAGS += -fno-limit-debug-info
213228
MODULE_DEBUG_INFO_FLAGS += -gmodules
@@ -273,7 +288,6 @@ endif
273288

274289
CFLAGS += $(CFLAGS_EXTRAS)
275290
CXXFLAGS += -std=c++11 $(CFLAGS) $(ARCH_CXXFLAGS)
276-
LD = $(CC)
277291
# Copy common options to the linker flags (dwarf, arch. & etc).
278292
# Note: we get some 'garbage' options for linker here (such as -I, --isystem & etc).
279293
LDFLAGS += $(CFLAGS)
@@ -306,50 +320,6 @@ ifneq "$(DYLIB_NAME)" ""
306320
endif
307321
endif
308322

309-
# Function that returns the counterpart C++ compiler, given $(CC) as arg.
310-
cxx_compiler_notdir = $(if $(findstring icc,$(1)), \
311-
$(subst icc,icpc,$(1)), \
312-
$(if $(findstring llvm-gcc,$(1)), \
313-
$(subst llvm-gcc,llvm-g++,$(1)), \
314-
$(if $(findstring gcc,$(1)), \
315-
$(subst gcc,g++,$(1)), \
316-
$(subst cc,c++,$(1)))))
317-
cxx_compiler = $(if $(findstring /,$(1)),$(join $(dir $(1)), $(call cxx_compiler_notdir,$(notdir $(1)))),$(call cxx_compiler_notdir,$(1)))
318-
319-
# Function that returns the C++ linker, given $(CC) as arg.
320-
cxx_linker_notdir = $(if $(findstring icc,$(1)), \
321-
$(subst icc,icpc,$(1)), \
322-
$(if $(findstring llvm-gcc,$(1)), \
323-
$(subst llvm-gcc,llvm-g++,$(1)), \
324-
$(if $(findstring gcc,$(1)), \
325-
$(subst gcc,g++,$(1)), \
326-
$(subst cc,c++,$(1)))))
327-
cxx_linker = $(if $(findstring /,$(1)),$(join $(dir $(1)), $(call cxx_linker_notdir,$(notdir $(1)))),$(call cxx_linker_notdir,$(1)))
328-
329-
ifneq "$(OS)" "Darwin"
330-
CLANG_OR_GCC := $(strip $(if $(findstring clang,$(CC)), \
331-
$(findstring clang,$(CC)), \
332-
$(if $(findstring gcc,$(CC)), \
333-
$(findstring gcc,$(CC)), \
334-
cc)))
335-
336-
CC_LASTWORD := $(strip $(lastword $(subst -, ,$(CC))))
337-
338-
replace_with = $(strip $(if $(findstring $(3),$(CC_LASTWORD)), \
339-
$(subst $(3),$(1),$(2)), \
340-
$(subst $(3),$(1),$(subst -$(CC_LASTWORD),,$(2)))))
341-
342-
ifeq "$(notdir $(CC))" "$(CC)"
343-
replace_cc_with = $(call replace_with,$(1),$(CC),$(CLANG_OR_GCC))
344-
else
345-
replace_cc_with = $(join $(dir $(CC)),$(call replace_with,$(1),$(notdir $(CC)),$(CLANG_OR_GCC)))
346-
endif
347-
348-
OBJCOPY ?= $(call replace_cc_with,objcopy)
349-
ARCHIVER ?= $(call replace_cc_with,ar)
350-
override AR = $(ARCHIVER)
351-
endif
352-
353323
ifdef PIE
354324
LDFLAGS += -pie
355325
endif
@@ -358,7 +328,7 @@ endif
358328
# Windows specific options
359329
#----------------------------------------------------------------------
360330
ifeq "$(OS)" "Windows_NT"
361-
ifneq (,$(findstring clang,$(CC)))
331+
ifeq ($(CCC), clang)
362332
# Clang for Windows doesn't support C++ Exceptions
363333
CXXFLAGS += -fno-exceptions
364334
CXXFLAGS += -D_HAS_EXCEPTIONS=0
@@ -403,7 +373,7 @@ endif
403373

404374
ifeq (1,$(USE_LIBSTDCPP))
405375
# Clang requires an extra flag: -stdlib=libstdc++
406-
ifneq (,$(findstring clang,$(CC)))
376+
ifeq ($(CCC), clang)
407377
# Force clang looking for the gcc's headers at specific rootfs folder.
408378
CXXFLAGS += -stdlib=libstdc++ $(GCC_TOOLCHAIN_FLAGS)
409379
LDFLAGS += -stdlib=libstdc++ $(GCC_TOOLCHAIN_FLAGS)
@@ -441,7 +411,7 @@ ifeq (1, $(USE_SYSTEM_STDLIB))
441411
CXXFLAGS += -nostdlib++ -nostdinc++ -cxx-isystem $(SDKROOT)/usr/include/c++/v1
442412
LDFLAGS += -L$(SDKROOT)/usr/lib -Wl,-rpath,$(SDKROOT)/usr/lib -lc++
443413
else
444-
ifneq (,$(findstring clang,$(CC)))
414+
ifeq ($(CCC), clang)
445415
# Force clang looking for the gcc's headers at specific rootfs folder.
446416
CXXFLAGS += $(GCC_TOOLCHAIN_FLAGS)
447417
LDFLAGS += $(GCC_TOOLCHAIN_FLAGS)
@@ -468,8 +438,6 @@ DYLIB_OBJECTS +=$(strip $(DYLIB_C_SOURCES:.c=.o))
468438
DYLIB_OBJECTS +=$(strip $(DYLIB_OBJC_SOURCES:.m=.o))
469439
ifneq "$(strip $(DYLIB_CXX_SOURCES))" ""
470440
DYLIB_OBJECTS +=$(strip $(patsubst %.mm, %.o, $(DYLIB_CXX_SOURCES:.cpp=.o)))
471-
CXX = $(call cxx_compiler,$(CC))
472-
LD = $(call cxx_linker,$(CC))
473441
endif
474442

475443
#----------------------------------------------------------------------
@@ -492,8 +460,6 @@ endif
492460
#----------------------------------------------------------------------
493461
ifneq "$(strip $(CXX_SOURCES))" ""
494462
OBJECTS +=$(strip $(CXX_SOURCES:.cpp=.o))
495-
CXX = $(call cxx_compiler,$(CC))
496-
LD = $(call cxx_linker,$(CC))
497463
endif
498464

499465
#----------------------------------------------------------------------
@@ -509,19 +475,18 @@ endif
509475
#----------------------------------------------------------------------
510476
ifneq "$(strip $(OBJCXX_SOURCES))" ""
511477
OBJECTS +=$(strip $(OBJCXX_SOURCES:.mm=.o))
512-
CXX = $(call cxx_compiler,$(CC))
513-
LD = $(call cxx_linker,$(CC))
514478
ifeq "$(findstring lobjc,$(LDFLAGS))" ""
515479
LDFLAGS +=-lobjc
516480
endif
517481
endif
518482

519-
ifeq ($(findstring clang, $(CXX)), clang)
483+
ifeq ($(CCC), clang)
520484
CXXFLAGS += --driver-mode=g++
521485
endif
522486

523487
ifneq "$(CXX)" ""
524-
ifeq ($(findstring clang, $(LD)), clang)
488+
# Specify the driver mode parameter if we use clang as the linker.
489+
ifeq ($(LDC), clang)
525490
LDFLAGS += --driver-mode=g++
526491
endif
527492
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

lldb/test/API/functionalities/breakpoint/thread_plan_user_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

lldb/test/API/functionalities/data-formatter/data-formatter-objc/ObjCDataFormatterTestCase.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ def appkit_tester_impl(self, commands, use_constant_classes):
1616
self.build()
1717
else:
1818
disable_constant_classes = {
19-
"CC": "xcrun clang", # FIXME: Remove when flags are available upstream.
2019
"CFLAGS_EXTRAS": "-fno-constant-nsnumber-literals "
2120
+ "-fno-constant-nsarray-literals "
2221
+ "-fno-constant-nsdictionary-literals",
2322
}
24-
self.build(dictionary=disable_constant_classes)
23+
# FIXME: Remove compiler when flags are available upstream.
24+
self.build(dictionary=disable_constant_classes, compiler="xcrun clang")
2525
self.appkit_common_data_formatters_command()
2626
commands()
2727

0 commit comments

Comments
 (0)