Skip to content

Makefile.rules: Replace Swift frontend invocations with Swift driver #7971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lldb/packages/Python/lldbsuite/test/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ def getSwiftCSpec(self):
return ['SWIFTC="{}"'.format(configuration.swiftCompiler)]
return []

def getPythonSpec(self):
"""
Helper function to return the key-value string to specify the Python
interpreter used for the make system.
"""
if configuration.python:
return ['PYTHON="{}"'.format(configuration.python)]
return []

def getSDKRootSpec(self):
"""
Helper function to return the key-value string to specify the SDK root
Expand Down Expand Up @@ -206,6 +215,7 @@ def getBuildCommand(
self.getSwiftTargetFlags(architecture),
self.getCCSpec(compiler),
self.getSwiftCSpec(),
self.getPythonSpec(),
self.getExtraMakeArgs(),
self.getSDKRootSpec(),
self.getModuleCacheSpec(),
Expand Down
3 changes: 2 additions & 1 deletion lldb/packages/Python/lldbsuite/test/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# System modules
import os
import platform

import sys

# Third-party modules
import unittest2
Expand Down Expand Up @@ -51,6 +51,7 @@
sdkroot = None
swiftCompiler = None
swiftLibrary = None
python = sys.executable

# The overriden dwarf verison.
dwarf_version = 0
Expand Down
131 changes: 65 additions & 66 deletions lldb/packages/Python/lldbsuite/test/make/Makefile.rules
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ ifeq "$(SDKROOT)" ""
else
SWIFTSDKROOT = $(SDKROOT)
endif
PYTHON ?= python3

#----------------------------------------------------------------------
# ARCHFLAG is the flag used to tell the compiler which architecture
Expand Down Expand Up @@ -293,23 +294,35 @@ ifeq "$(findstring -target,$(SWIFTFLAGS))" ""
SWIFTFLAGS += $(TARGET_SWIFTFLAGS)
endif

ifneq "$(SWIFT_ENABLE_EXPLICIT_MODULES)" ""
SWIFTFLAGS += -explicit-module-build
endif

# Swift bridging headers.
ifneq "$(SWIFT_BRIDGING_HEADER)" ""
# These are incompatible.
DISABLE_SWIFT_INTERFACE = YES
ifneq "$(SWIFT_PRECOMPILE_BRIDGING_HEADER)" "NO"
# With PCH.
SWIFT_BRIDGING_PCH=$(patsubst %.h,%.pch,$(SWIFT_BRIDGING_HEADER))
SWIFT_HFLAGS = -import-objc-header $(BUILDDIR)/$(SWIFT_BRIDGING_PCH)
SWIFT_HFLAGS = -import-objc-header $(SRCDIR)/$(SWIFT_BRIDGING_HEADER) -enable-bridging-pch -pch-output-dir $(BUILDDIR)
else
# From source.
SWIFT_BRIDGING_PCH :=
SWIFT_HFLAGS = -import-objc-header $(SRCDIR)/$(SWIFT_BRIDGING_HEADER)
SWIFT_HFLAGS = -import-objc-header $(SRCDIR)/$(SWIFT_BRIDGING_HEADER) -disable-bridging-pch
endif
else
# No bridging header.
SWIFT_BRIDGING_PCH :=
SWIFT_HFLAGS :=
endif

# Clang headers generated from Swift sources.
ifneq "$(SWIFT_OBJC_HEADER)" ""
SWIFTFLAGS += -emit-objc-header-path $(SWIFT_OBJC_HEADER)
endif
ifneq "$(SWIFT_CXX_HEADER)" ""
SWIFTFLAGS += -emit-clang-header-path $(SWIFT_CXX_HEADER)
$(SWIFT_CXX_HEADER): $(strip $(SWIFT_SOURCES:.swift=.swift.o))
endif

ifeq "$(OS)" "Linux"
SWIFT_HOSTDIR = $(shell dirname $(SWIFTC))/../lib/swift/linux
else
Expand Down Expand Up @@ -467,7 +480,7 @@ endif

ifeq (1, $(USE_SYSTEM_STDLIB))
ifneq ($(or $(USE_LIBSTDCPP), $(USE_LIBCPP)),)
$(error Cannot use system's standard library and a custom standard library together)
$(error Cannot use system standard library and a custom standard library together)
endif
endif

Expand Down Expand Up @@ -583,17 +596,17 @@ endif
# Check if we have any Swift source files
#----------------------------------------------------------------------
ifneq "$(strip $(SWIFT_SOURCES))" ""
OBJECTS +=$(strip $(SWIFT_SOURCES:.swift=.o))
OBJECTS +=$(strip $(SWIFT_SOURCES:.swift=.swift.o))
USESWIFTDRIVER = 1
endif

ifneq "$(strip $(DYLIB_SWIFT_SOURCES))" ""
DYLIB_OBJECTS +=$(strip $(DYLIB_SWIFT_SOURCES:.swift=.o))
DYLIB_OBJECTS +=$(strip $(DYLIB_SWIFT_SOURCES:.swift=.swift.o))
USESWIFTDRIVER = 1
endif

ifneq "$(strip $(SWIFT_SOURCES_FOR_CXX_HEADER))" ""
SWIFT_CXX_HEADER =$(strip $(SWIFT_SOURCES:.swift=.o))
SWIFT_CXX_HEADER =$(strip $(SWIFT_SOURCES:.swift=.swift.o))
USESWIFTDRIVER = 1
endif

Expand Down Expand Up @@ -635,12 +648,12 @@ ifeq "$(SWIFT_EMBEDDED_MODE)" "1"
SWIFTFLAGS += -gdwarf-types -enable-experimental-feature Embedded -Xfrontend -disable-objc-interop -runtime-compatibility-version none
SWIFT_WMO = 1
endif

#----------------------------------------------------------------------
# Set up variables to run the Swift frontend directly.
#----------------------------------------------------------------------
SWIFT=$(shell dirname $(SWIFTC))/swift
SWIFT_FEFLAGS = $(shell echo $(patsubst -Xfrontend,,$(SWIFTFLAGS)) | sed -e 's/-Xlinker [^ ]*//g') -enable-anonymous-context-mangled-names
SWIFT_FEFLAGS = $(shell echo $(patsubst -Xfrontend,,$(SWIFTFLAGS)) | sed -e 's/-Xlinker [^ ]*//g')
SWIFT_FE=$(SWIFT) -frontend

#----------------------------------------------------------------------
Expand Down Expand Up @@ -686,22 +699,45 @@ endif

VPATHSOURCES=$(patsubst %,$(VPATH)/%,$(SWIFT_SOURCES)) $(patsubst %,$(VPATH)/%,$(DYLIB_SWIFT_SOURCES))

# Whole module optimization and primary file directives are at direct odds with each other.
# Either pick one or the other.
ALL_SWIFT_SOURCES = $(SWIFT_SOURCES) $(DYLIB_SWIFT_SOURCES)
SWIFT_OBJECTS = $(strip $(ALL_SWIFT_SOURCES:.swift=.swift.o))

ifeq "$(DISABLE_SWIFT_INTERFACE)" ""
SWIFT_INTERFACE_FLAGS=-emit-module-interface-path $(BUILDDIR)/$(MODULENAME).swiftinterface
endif

ifeq "$(SWIFT_WMO)" "1"
SWIFT_COMPILE_MODE_DIRECTIVE = "-wmo"
SWIFTC_OUTPUT=-wmo -o $@
else
SWIFT_COMPILE_DIRECTIVE = "-primary-file"
SWIFTC_OUTPUT=-output-file-map $(BUILDDIR)/$(MODULENAME)-output-file-map.json
endif

%.o: %.swift $(SWIFT_BRIDGING_PCH) $(SWIFT_OBJC_HEADER)
@echo "### Compiling" $<
$(SWIFT_FE) -c $(SWIFT_COMPILE_DIRECTIVE) $< \
$(filter-out $(VPATH)/$<,$(filter-out $<,$(VPATHSOURCES))) \
$(SWIFT_FEFLAGS) $(SWIFT_HFLAGS) $(PARSE_AS_LIBRARY) \
-module-name $(MODULENAME) -emit-module-path \
$(patsubst %.o,$(BUILDDIR)/%.partial.swiftmodule,$@) \
-o $(BUILDDIR)/$@
# The Swift object files are named .swift.o so they don't conflict
# with the wrapped Swift module $(MODULENAME).o

$(SWIFT_OBJECTS): $(SWIFT_SOURCES) $(DYLIB_SWIFT_SOURCES)
ifneq "$(SWIFT_WMO)" "1"
@echo "### Generating output file map"
$(PYTHON) $(THIS_FILE_DIR)/gen-output-map.py $^ $(patsubst %,$(BUILDDIR)/%,$(SWIFT_OBJECTS)) >$(BUILDDIR)/$(MODULENAME)-output-file-map.json
endif
@echo "### Compiling" $^
@echo "### Swift driver expanded incovation will be:"
$(SWIFTC) -emit-object $^ $(SWIFTC_OUTPUT) \
$(SWIFTFLAGS) $(SWIFT_HFLAGS) $(PARSE_AS_LIBRARY) \
-module-name $(MODULENAME) \
-emit-module-path $(BUILDDIR)/$(MODULENAME).swiftmodule \
$(SWIFT_INTERFACE_FLAGS) -###
@echo "### Swift driver invocation:"
$(SWIFTC) -emit-object $^ $(SWIFTC_OUTPUT) \
$(SWIFTFLAGS) $(SWIFT_HFLAGS) $(PARSE_AS_LIBRARY) \
-module-name $(MODULENAME) \
-emit-module-path $(BUILDDIR)/$(MODULENAME).swiftmodule \
$(SWIFT_INTERFACE_FLAGS)
ifneq "$(OS)" "Darwin"
@echo "### Wrapping Swift module:"
$(SWIFT) -modulewrap $(BUILDDIR)/$(MODULENAME).swiftmodule \
-o $(BUILDDIR)/$(MODULENAME).o
endif

ifeq "$(OS)" "Darwin"
ifeq "$(HIDE_SWIFTMODULE)" ""
Expand All @@ -711,58 +747,21 @@ else
SWIFTMODULE =
LD_SWIFTFLAGS =
endif
$(EXE): $(SWIFTMODULE) $(OBJECTS)
$(EXE): $(OBJECTS)
@echo "### Linking" $(EXE)
$(SWIFTC) $(LD_EXTRAS) $(LD_SWIFTFLAGS) $(OBJECTS) $(patsubst -g,,$(SWIFTFLAGS)) -o "$(EXE)"
ifneq "$(CODESIGN)" ""
$(CODESIGN) -s - "$(EXE)"
endif
else # OS = Linux
$(EXE): $(MODULENAME).swiftmodule.o $(OBJECTS)
@echo "### Linking" $(EXE)
ifneq "$(HIDE_SWIFTMODULE)" ""
$(SWIFTC) $(LD_EXTRAS) $(LD_SWIFTFLAGS) $(patsubst %.swiftmodule.o,,$^) $(patsubst -g,,$(SWIFTFLAGS)) -o "$(EXE)"
else
$(SWIFTC) $(LD_EXTRAS) $(LD_SWIFTFLAGS) $^ $(patsubst -g,,$(SWIFTFLAGS)) -o "$(EXE)"
endif

$(MODULENAME).swiftmodule.o: $(MODULENAME).swiftmodule
@echo "### Wrapping swift module"
$(SWIFT) -modulewrap $^ -o $(BUILDDIR)/$@
endif

$(MODULENAME).swiftmodule: $(OBJECTS) $(DYLIB_OBJECTS)
@echo "### Merging swift modules for $(MODULENAME)"
$(SWIFT_FE) $(SWIFT_FEFLAGS) $(SWIFT_HFLAGS) -merge-modules \
-emit-module \
-emit-module-interface-path $(BUILDDIR)/$(MODULENAME).swiftinterface \
$(patsubst %.swift,%.partial.swiftmodule,$(SWIFT_SOURCES) $(DYLIB_SWIFT_SOURCES)) \
-disable-diagnostic-passes -disable-sil-perf-optzns \
-module-name $(MODULENAME) \
-o $(BUILDDIR)/$@

# Swift precompiled bridging headers. Don't override the Clang .pch build rule.
ifneq "$(SWIFT_BRIDGING_PCH)" ""
$(SWIFT_BRIDGING_PCH): $(SWIFT_BRIDGING_HEADER)
@echo "### Precompiling" $<
$(SWIFT_FE) -emit-pch $(SWIFT_FEFLAGS) $< -o $(BUILDDIR)/$@
ifeq "$(HIDE_SWIFTMODULE)" ""
WRAPPED_SWIFTMODULE = $(MODULENAME).o
endif

# Swift Obj-C header.
ifneq "$(SWIFT_OBJC_HEADER)" ""
$(SWIFT_OBJC_HEADER): $(SWIFT_SOURCES) $(DYLIB_SWIFT_SOURCES)
@echo "### Building Obj-C header"
$(SWIFT_FE) -typecheck $(VPATHSOURCES) \
$(SWIFT_FEFLAGS) $(SWIFT_HFLAGS) -module-name $(MODULENAME) \
-emit-objc-header-path $(SWIFT_OBJC_HEADER)
endif
$(EXE): $(OBJECTS)
@echo "### Linking" $(EXE)
$(SWIFTC) $(LD_EXTRAS) $(LD_SWIFTFLAGS) $(WRAPPED_SWIFTMODULE) $^ $(patsubst -g,,$(SWIFTFLAGS)) -o "$(EXE)"

ifneq "$(SWIFT_CXX_HEADER)" ""
$(SWIFT_CXX_HEADER): $(SWIFT_SOURCES) $(SWIFT_BRIDGING_PCH)
@echo "### Building C++ header from Swift" $<
$(SWIFT_FE) -typecheck $(VPATHSOURCES) \
$(SWIFT_FEFLAGS) $(SWIFT_HFLAGS) -module-name $(MODULENAME) \
$(SWIFT_CXX_HEADER_FLAGS) -emit-clang-header-path $(SWIFT_CXX_HEADER)
endif

else # USESWIFTDRIVER = 0
Expand Down
32 changes: 32 additions & 0 deletions lldb/packages/Python/lldbsuite/test/make/gen-output-map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/env python3
import json
import sys

args = sys.argv[1:]
if len(args) % 2:
print("""
Usage: {0} file1.swift file2.swift ... file1.o file2.o ...
Generates output-file-map.json
""".format(sys.argv[0]))
exit(1)

midpoint = int(len(args) / 2)
sources = args[:midpoint]
objects = args[midpoint:]

# {
# "": {
# "swift-dependencies": "/tmp/overall.swiftdeps",
# },
# "main.swift": {
# "object": "/tmp/main.o",
# "swift-dependencies": "/tmp/main.swiftdeps",
# },
# }

data = {}
for src, obj in zip(sources, objects):
data[src] = {"object": obj}

json.dump(data, sys.stdout)
print("")
8 changes: 4 additions & 4 deletions lldb/test/API/lang/swift/array_bridged_enum/main.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let array = [SampleEnum.zero, SampleEnum.one, SampleEnum.two, SampleEnum.three]
print(array) //%self.expect('frame variable array[0]', substrs=['[0]', '.SampleEnumZero'])
//%self.expect('frame variable array[1]', substrs=['[1]', '.SampleEnumOne'])
//%self.expect('frame variable array[2]', substrs=['[2]', '.SampleEnumTwo'])
//%self.expect('frame variable array[3]', substrs=['[3]', '.SampleEnumThree'])
print(array) //%self.expect('frame variable array[0]', substrs=['[0]', 'zero'])
//%self.expect('frame variable array[1]', substrs=['[1]', 'one'])
//%self.expect('frame variable array[2]', substrs=['[2]', 'two'])
//%self.expect('frame variable array[3]', substrs=['[3]', 'three'])
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class TestSwiftAnonymousClangTypes(lldbtest.TestBase):
@swiftTest
# FIMXE: This broke when adapting the swift-driver based Makefile.rules
@expectedFailureAll(oslist=['linux'])
def test(self):
self.build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ SWIFT_SOURCES := main.swift
MAKE_DSYM := NO
LD_EXTRAS := -ldylib -L.
SWIFTFLAGS_EXTRAS = -import-objc-header $(SRCDIR)/bridging-header.h \
-Xcc -I$(SRCDIR)/foo.hmap -I. -I$(SRCDIR)
-Xcc -I$(SRCDIR)/foo.hmap -I. -I$(SRCDIR) \
-disable-bridging-pch
DISABLE_SWIFT_INTERFACE := YES

all: libdylib.dylib a.out

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
SWIFT_SOURCES=main.swift
SWIFT_OBJC_INTEROP := 1
LD_EXTRAS=-lDylib -lConflict -L$(shell pwd)
SWIFTFLAGS_EXTRAS= -Xcc -I$(SRCDIR)/hidden/Bar -I. -import-objc-header $(SRCDIR)/bridging.h
SWIFTFLAGS_EXTRAS= -Xcc -I$(SRCDIR)/hidden/Bar -I.
SWIFT_BRIDGING_HEADER := bridging.h
SWIFT_PRECOMPILE_BRIDGING_HEADER := NO
DISABLE_SWIFT_INTERFACE := YES

all: libDylib.dylib libConflict.dylib a.out

Expand Down
5 changes: 3 additions & 2 deletions lldb/test/API/lang/swift/clangimporter/missing_pch/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ lib%.dylib: %.swift
$(MAKE) MAKE_DSYM=$(MAKE_DSYM) CC=$(CC) SWIFTC=$(SWIFTC) \
ARCH=$(ARCH) DSYMUTIL=$(DSYMUTIL) \
BASENAME=$(shell basename $< .swift) \
SWIFT_BRIDGING_HEADER=header.h \
SWIFT_PRECOMPILE_BRIDGING_HEADER=YES \
DISABLE_SWIFT_INTERFACE=YES \
SWIFTFLAGS_EXTRAS="-import-objc-header header.h -enable-bridging-pch -pch-output-dir $(BUILDDIR)" \
VPATH=$(SRCDIR) -I $(SRCDIR) -f $(SRCDIR)/dylib.mk all
rm -f $(OVERLAY)
rm header*.pch

clean::
$(MAKE) MAKE_DSYM=$(MAKE_DSYM) CC=$(CC) SWIFTC=$(SWIFTC) \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def test(self):
"""This used to be a test for a diagnostic, however,
this is no longer an unrecoverable error"""
self.build()
os.unlink(self.getBuildArtifact("header.pch"))
lldbutil.run_to_source_breakpoint(
self, "break here", lldb.SBFileSpec("main.swift"),
extra_images=["Foo"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def test(self):
'break here', lldb.SBFileSpec('Foo.swift'))
process.Continue()
self.expect("fr var foo", "expected result", substrs=["23"])
self.expect("expression foo", "expected result", substrs=["$R3", "23"])
self.expect("expression $R3", "expected result", substrs=["23"])
self.expect("expression $R4", "expected result", substrs=["23"])
# FIXME: This should work with precise compiler invocations.
#self.expect("expression foo", "expected result", substrs=["$R3", "23"])
#self.expect("expression $R3", "expected result", substrs=["23"])
#self.expect("expression $R4", "expected result", substrs=["23"])
Comment on lines +43 to +46

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question, should this remain commented out?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a regression, but in reality the output here is random based on the order in which modules are visited. The right thing to do is to enable this only when precise compiler invocations are on, but the decorator isn't powerful enough to detect this. I'll need to follow-up here.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ DYLIB_ONLY := YES
DYLIB_NAME := $(BASENAME)
DYLIB_SWIFT_SOURCES := $(DYLIB_NAME).swift
SWIFT_BRIDGING_HEADER := $(DYLIB_NAME)/$(DYLIB_NAME)-Bridging.h
SWIFT_PRECOMPILE_BRIDGING_HEADER := NO
SWIFT_OBJC_HEADER = $(DYLIB_NAME)-Swift.h
SWIFT_OBJC_INTEROP := 1
SWIFTFLAGS_EXTRAS = -Xcc -I$(SRCDIR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ $(EXE): $(SWIFT_SOURCES)
VPATH=$(SRCDIR) -I $(SRCDIR) \
SDKROOT=$(BUILD_SDK) \
SWIFTFLAGS_EXTRAS="$(SWIFTFLAGS_EXTRAS)" \
-f $(SRCDIR)/helper.mk clean main.o a.swiftmodule
-f $(SRCDIR)/helper.mk clean main.swift.o a.swiftmodule
echo "Sanity check that our SDK shenanigns worked"
dwarfdump -r 0 $(BUILDDIR)/main.o | egrep 'DW_AT_LLVM_i?sysroot' | grep -q LocalSDK
dwarfdump -r 0 $(BUILDDIR)/main.swift.o | egrep 'DW_AT_LLVM_i?sysroot' | grep -q LocalSDK
echo "Linking with regular SDK (otherwise the linker complains)"
$(MAKE) MAKE_DSYM=$(MAKE_DSYM) CC=$(CC) SWIFTC=$(SWIFTC) \
ARCH=$(ARCH) DSYMUTIL=$(DSYMUTIL) \
VPATH=$(SRCDIR) -I $(SRCDIR) \
SDKROOT=$(SDKROOT) \
-f $(SRCDIR)/helper.mk all
-f $(SRCDIR)/helper.mk all
echo "Deleting build SDK"
rm -rf $(BUILDDIR)/LocalSDK
Loading