Skip to content

Commit d3db75a

Browse files
Merge pull request #3 from MillerTechnologyPeru/trunk
Updated to Swift 5.7
2 parents 5a1f85a + ddc215d commit d3db75a

33 files changed

+1053
-352
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.swp

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# meta-swift
2-
Yocto meta-layer for swift-on-arm (Swift 5.1.2)
2+
Yocto meta-layer for swift-on-arm (Swift 5.7)
33

44
# Usage
55

@@ -8,7 +8,7 @@ Add this meta layer to your project (refer to yocto user manual)
88
Create a new swift application and include it in your yocto build as follows...
99

1010
```
11-
DESCRIPTION = "My swift 5.1.3 app"
11+
DESCRIPTION = "My swift 5.7 app"
1212
LICENSE = "CLOSED"
1313
1414
SRC_URI = "file://Sources/hello-world/main.swift \
@@ -20,8 +20,8 @@ inherit swift
2020

2121
This does a few things, when you `inherit swift` meta-layer class, it will does the following...
2222

23-
- Automatically download the x86_64 and ARMv7 swift 5.1.2 binaries and create a cross-compiling sys-root
24-
- Add an RDEPENDS_${PN} for `swift` which is the arm v7 runtime
23+
- Automatically download the x86_64 and ARMv7 swift 5.7 binaries and create a cross-compiling sys-root
24+
- Add an RDEPENDS_${PN} for `swift` which is the Armv7 runtime
2525
- Performs the required build steps
2626

2727
# Deployment

classes/swift-cmake-base.bbclass

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
inherit cmake
2+
3+
DEPENDS_append += " swift-native libgcc gcc glibc "
4+
5+
SWIFT_TARGET_ARCH = "${@oe.utils.conditional('TARGET_ARCH', 'arm', 'armv7', 'aarch64', d)}"
6+
SWIFT_TARGET_NAME = "${@oe.utils.conditional('TARGET_ARCH', 'arm', 'armv7-unknown-linux-gnueabihf', 'aarch64-unknown-linux-gnueabi', d)}"
7+
TARGET_CPU_NAME = "${@oe.utils.conditional('TARGET_ARCH', 'arm', 'armv7-a', 'aarch64', d)}"
8+
9+
HOST_CC_ARCH_prepend = "-target ${SWIFT_TARGET_NAME}"
10+
11+
################################################################################
12+
# NOTE: The host running bitbake must have lld available and the following #
13+
# must be added to the local.conf file: #
14+
# #
15+
# HOSTTOOLS += "ld.lld" #
16+
# #
17+
################################################################################
18+
19+
# Use lld (see note above)
20+
TARGET_LDFLAGS += "-fuse-ld=lld"
21+
22+
# Add build-id to generated binaries
23+
TARGET_LDFLAGS += "-Xlinker --build-id=sha1"
24+
25+
# Use Apple's provided clang (it understands Apple's custom compiler flags)
26+
# Made available via swift-native package.
27+
OECMAKE_C_COMPILER = "clang"
28+
OECMAKE_CXX_COMPILER = "clang++"
29+
30+
# Point clang to where the C++ runtime is for our target arch
31+
RUNTIME_FLAGS = "-w -fuse-ld=lld -B${STAGING_DIR_TARGET}/usr/lib/${TARGET_SYS}/current"
32+
TARGET_LDFLAGS += "-w -fuse-ld=lld -L${STAGING_DIR_TARGET}/usr/lib/${TARGET_SYS}/current"
33+
34+
EXTRA_INCLUDE_FLAGS ?= ""
35+
OECMAKE_C_FLAGS += "${RUNTIME_FLAGS} ${EXTRA_INCLUDE_FLAGS}"
36+
OECMAKE_CXX_FLAGS += "${RUNTIME_FLAGS} ${EXTRA_INCLUDE_FLAGS}"
37+
OECMAKE_ASM_FLAGS += "${RUNTIME_FLAGS} ${EXTRA_INCLUDE_FLAGS}"
38+
39+
BUILD_MODE = "${@['release', 'debug'][d.getVar('DEBUG_BUILD') == '1']}"
40+
41+
# Additional parameters to pass to swiftc
42+
EXTRA_SWIFTC_FLAGS ??= ""
43+
44+
SWIFT_FLAGS = "-target ${SWIFT_TARGET_NAME} -use-ld=lld \
45+
-resource-dir ${STAGING_DIR_TARGET}/usr/lib/swift \
46+
-module-cache-path ${B}/${BUILD_MODE}/ModuleCache \
47+
-Xclang-linker -B${STAGING_DIR_TARGET}/usr/lib/${TARGET_SYS}/current \
48+
-Xclang-linker -B${STAGING_DIR_TARGET}/usr/lib \
49+
-Xcc -I${STAGING_DIR_NATIVE}/usr/lib/${TARGET_SYS}/gcc/${TARGET_SYS}/current/include \
50+
-Xcc -I${STAGING_DIR_NATIVE}/usr/lib/${TARGET_SYS}/gcc/${TARGET_SYS}/current/include-fixed \
51+
-L${STAGING_DIR_TARGET} \
52+
-L${STAGING_DIR_TARGET}/lib \
53+
-L${STAGING_DIR_TARGET}/usr/lib \
54+
-L${STAGING_DIR_TARGET}/usr/lib/swift \
55+
-L${STAGING_DIR_TARGET}/usr/lib/swift/linux \
56+
-L${STAGING_DIR_TARGET}/usr/lib/${TARGET_SYS}/current \
57+
-sdk ${STAGING_DIR_TARGET} \
58+
${EXTRA_SWIFTC_FLAGS} \
59+
"
60+
61+
HOST_LLVM_PATH = "${STAGING_DIR_NATIVE}/opt/usr/lib/llvm-swift"
62+
EXTRA_OECMAKE += '-DCMAKE_Swift_FLAGS="${SWIFT_FLAGS}"'
63+
EXTRA_OECMAKE += " -DSWIFT_USE_LINKER=lld"
64+
EXTRA_OECMAKE += " -DLLVM_USE_LINKER=lld"
65+
EXTRA_OECMAKE += " -DLLVM_DIR=${HOST_LLVM_PATH}/lib/cmake/llvm"
66+
EXTRA_OECMAKE += " -DLLVM_BUILD_LIBRARY_DIR=${HOST_LLVM_PATH}"
67+
68+
EXTRANATIVEPATH += "swift-tools"
69+
70+
################################################################################
71+
# Create symlinks to the directories containing the gcc version specific #
72+
# headers, objects and libraries we need. #
73+
# #
74+
# We can't just use ${GCC_VERSION} in the path variables in the recipe #
75+
# because bitbake parses and expands variables before GCC_VERSION is #
76+
# defined. GCC_VERSION cannot be defined until the sysroot is populated #
77+
# because we inspect the sysroot to determine the GCC version number string. #
78+
# If there was an env or bitbake var with the GCC version, we could use that #
79+
# and avoid all of this but the closest thing we have access to is #
80+
# ${GCCVERSION} which yields and incomplete version number (ex: "9.%"). #
81+
# #
82+
# Also there is some suspicion that these path variables and these symlinks #
83+
# may not be necessary if the --gcc-toolchain clang flag was used. But that #
84+
# is an unproven theory. #
85+
################################################################################
86+
87+
do_create_gcc_version_symlinks() {
88+
GCC_VERSION=`basename ${STAGING_DIR_TARGET}/usr/include/c++/*`
89+
90+
if [ ! -L "${STAGING_DIR_TARGET}/usr/lib/${TARGET_SYS}/current" ]; then
91+
cd ${STAGING_DIR_TARGET}/usr/lib/${TARGET_SYS}
92+
ln -s -r ${GCC_VERSION} current
93+
fi
94+
95+
if [ ! -L "${STAGING_DIR_TARGET}/usr/include/c++/current" ]; then
96+
cd ${STAGING_DIR_TARGET}/usr/include/c++
97+
ln -s -r ${GCC_VERSION} current
98+
fi
99+
100+
if [ ! -L "${STAGING_DIR_NATIVE}/usr/lib/${TARGET_SYS}/gcc/${TARGET_SYS}/current" ]; then
101+
cd ${STAGING_DIR_NATIVE}/usr/lib/${TARGET_SYS}/gcc/${TARGET_SYS}
102+
ln -s -r ${GCC_VERSION} current
103+
fi
104+
}
105+
106+
addtask do_create_gcc_version_symlinks after do_prepare_recipe_sysroot before do_configure
107+

classes/swift-cmake.bbclass

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
inherit swift-cmake-base
2+
3+
DEPENDS_append += " swift-stdlib libdispatch libfoundation "
4+
5+
TARGET_LDFLAGS += "-L${STAGING_DIR_TARGET}/usr/lib/swift/linux"

classes/swift.bbclass

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,95 @@
11

2-
DEPENDS_prepend += "swift-native swift glibc gcc libgcc "
3-
RDEPENDS_${PN} += " swift "
2+
DEPENDS += "swift-native glibc gcc libgcc swift-stdlib libdispatch libfoundation"
3+
4+
# Default build directory for SPM is "./.build"
5+
# (see 'swift [build|package|run|test] --help')
6+
#
7+
# We can allow for this to be changed by changing ${B} but one must be careful to also set
8+
# "--build-path ${B}" for _ALL_ invocations of SPM within a recipe.
9+
B ?= "${S}/.build"
10+
EXTERNALSRC_BUILD ?= "${EXTERNALSRC}/.build"
11+
12+
BUILD_MODE = "${@['release', 'debug'][d.getVar('DEBUG_BUILD') == '1']}"
413

514
# Additional parameters to pass to SPM
615
EXTRA_OESWIFT ?= ""
716

17+
SWIFT_TARGET_NAME = "${@oe.utils.conditional('TARGET_ARCH', 'arm', 'armv7-unknown-linux-gnueabihf', 'aarch64-unknown-linux-gnueabi', d)}"
18+
19+
# Workaround complex macros that cannot be automatically imported by Swift.
20+
# https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/using_imported_c_macros_in_swift
21+
#
22+
# Seems that SO_RCVTIMEO and SO_SNDTIMEO definitions aren't working because they are expressions
23+
# and not simple constants.
24+
#
25+
# This could be improved to replace just the specific lines that need fixing rather than rewriting
26+
# the entire file.
827
def fix_socket_header(filename):
9-
with open(filename, 'r') as f:
10-
lines = f.readlines()
28+
with open(filename, 'r') as f:
29+
lines = f.readlines()
1130

12-
os.remove(filename)
31+
os.remove(filename)
32+
33+
with open(filename, 'w') as f:
34+
for line in lines:
35+
if line.startswith('#define SO_RCVTIMEO ') and ("SO_RCVTIMEO_OLD" in line) and ("?" in line):
36+
f.write('#define SO_RCVTIMEO SO_RCVTIMEO_OLD\n')
37+
elif line.startswith('#define SO_SNDTIMEO ') and ("SO_SNDTIMEO_OLD" in line) and ("?" in line):
38+
f.write('#define SO_SNDTIMEO SO_SNDTIMEO_OLD\n')
39+
else:
40+
f.write(line)
1341

14-
with open(filename, 'w') as f:
15-
for line in lines:
16-
if line.startswith('#define SO_RCVTIMEO ') and ("SO_RCVTIMEO_OLD" in line) and ("?" in line):
17-
f.write('#define SO_RCVTIMEO SO_RCVTIMEO_OLD\n')
18-
elif line.startswith('#define SO_SNDTIMEO ') and ("SO_SNDTIMEO_OLD" in line) and ("?" in line):
19-
f.write('#define SO_SNDTIMEO SO_SNDTIMEO_OLD\n')
20-
else:
21-
f.write(line)
2242

2343
python swift_do_configure() {
2444
import os
2545
import os.path
2646
import shutil
47+
2748
workdir = d.getVar("WORKDIR", True)
2849
recipe_sysroot = d.getVar("STAGING_DIR_TARGET", True)
29-
30-
#socket.h workaround. Seems that SO_RCVTIMEO and SO_SNDTIMEO definitions using ? aren't working
50+
51+
# Workaround complex macros that cannot be automatically imported by Swift.
52+
# https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/using_imported_c_macros_in_swift
53+
#
54+
# Seems that SO_RCVTIMEO and SO_SNDTIMEO definitions aren't working because they are expressions
55+
# and not simple constants.
3156
socket_header = recipe_sysroot + "/usr/include/asm-generic/socket.h"
3257
fix_socket_header(socket_header)
3358

59+
# Detect the version of the C++ runtime
60+
# This is used to determine necessary include paths
3461
cxx_include_base = recipe_sysroot + "/usr/include/c++"
3562
cxx_include_list = os.listdir(cxx_include_base)
3663
if len(cxx_include_list) != 1:
3764
bb.fatal("swift bbclass detected more than one c++ runtime, unable to determine which one to use")
3865
cxx_version = cxx_include_list[0]
39-
66+
4067
d.setVar('SWIFT_CXX_VERSION', cxx_version)
4168

4269
swift_destination_template = """{
4370
"version":1,
4471
"sdk":"${STAGING_DIR_TARGET}/",
45-
"toolchain-bin-dir":"${STAGING_DIR_NATIVE}/opt/swift-arm/usr/bin",
46-
"target":"armv7-unknown-linux-gnueabihf",
72+
"toolchain-bin-dir":"${STAGING_DIR_NATIVE}/opt/usr/bin",
73+
"target":"${SWIFT_TARGET_NAME}",
4774
"dynamic-library-extension":"so",
48-
"extra-cc-flags":[
75+
"extra-cc-flags":[
4976
"-fPIC",
5077
"-I${STAGING_DIR_TARGET}/usr/include/c++/${SWIFT_CXX_VERSION}",
5178
"-I${STAGING_DIR_TARGET}/usr/include/c++/${SWIFT_CXX_VERSION}/${TARGET_SYS}",
79+
"-I${STAGING_DIR_NATIVE}/opt/usr/lib/clang/13.0.0/include",
80+
"-I${STAGING_DIR_NATIVE}/opt/usr/lib/clang/13.0.0/include-fixed"
5281
],
53-
"extra-swiftc-flags":[
82+
"extra-swiftc-flags":[
5483
"-target",
55-
"armv7-unknown-linux-gnueabihf",
84+
"${SWIFT_TARGET_NAME}",
5685
"-use-ld=lld",
5786
"-tools-directory",
58-
"/usr/bin",
87+
"${STAGING_DIR_NATIVE}/opt/usr/bin",
88+
89+
"-Xlinker", "-rpath", "-Xlinker", "/usr/lib/swift/linux",
90+
91+
"-Xlinker",
92+
"-L${STAGING_DIR_TARGET}",
5993
6094
"-Xlinker",
6195
"-L${STAGING_DIR_TARGET}/lib",
@@ -68,29 +102,41 @@ python swift_do_configure() {
68102
69103
"-Xlinker",
70104
"-L${STAGING_DIR_TARGET}/usr/lib/${TARGET_SYS}/${SWIFT_CXX_VERSION}",
71-
105+
106+
"-Xlinker",
107+
"--build-id=sha1",
108+
109+
"-I${STAGING_INCDIR}",
72110
"-I${STAGING_DIR_TARGET}/usr/include/c++/${SWIFT_CXX_VERSION}",
73-
"-I${STAGING_DIR_TARGET}/usr/include/c++/${SWIFT_CXX_VERSION}/${TARGET_SYS}"
111+
"-I${STAGING_DIR_TARGET}/usr/include/c++/${SWIFT_CXX_VERSION}/${TARGET_SYS}",
112+
"-I${STAGING_DIR_NATIVE}/opt/usr/lib/clang/13.0.0/include",
113+
"-I${STAGING_DIR_NATIVE}/opt/usr/lib/clang/13.0.0/include-fixed",
114+
115+
"-resource-dir", "${STAGING_DIR_TARGET}/usr/lib/swift",
116+
"-module-cache-path", "${B}/${BUILD_MODE}/ModuleCache",
117+
"-Xclang-linker", "-B${STAGING_DIR_TARGET}/usr/lib/${TARGET_SYS}/${SWIFT_CXX_VERSION}",
118+
"-Xclang-linker", "-B${STAGING_DIR_TARGET}/usr/lib",
119+
120+
"-sdk", "${STAGING_DIR_TARGET}"
74121
],
75-
"extra-cpp-flags":[
122+
"extra-cpp-flags":[
76123
"-lstdc++"
77124
]
78125
}"""
79126

80127
swift_destination = d.expand(swift_destination_template)
81128

82129
d.delVar("SWIFT_CXX_VERSION")
83-
130+
84131
configJSON = open(workdir + "/destination.json", "w")
85132
configJSON.write(swift_destination)
86133
configJSON.close()
87134
}
88135

89136
swift_do_compile() {
90-
#Linker isn't finding crtbeginS.o and crtendS.o under ${TARGET_SYS} path
91-
cp -r ${WORKDIR}/recipe-sysroot/usr/lib/${TARGET_SYS}/*/* ${WORKDIR}/recipe-sysroot/usr/lib
92-
cd ${S}
93-
${WORKDIR}/recipe-sysroot-native/usr/bin/swift build -v -c release --destination ${WORKDIR}/destination.json ${EXTRA_OESWIFT}
137+
swift build --package-path ${S} --build-path ${B} --skip-update -c ${BUILD_MODE} --destination ${WORKDIR}/destination.json ${EXTRA_OESWIFT}
94138
}
95139

96140
EXPORT_FUNCTIONS do_configure do_compile
141+
142+
EXTRANATIVEPATH += "swift-tools"

conf/layer.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ BBFILES += "${LAYERDIR}/recipes*/*/*.bb \
88
BBFILE_COLLECTIONS += "swift-layer"
99
BBFILE_PATTERN_swift-layer := "^${LAYERDIR}/"
1010
BBFILE_PRIORITY_swift-layer = "7"
11+
12+
LAYERSERIES_COMPAT_swift-layer = "dunfell"

recipes-devtools/swift/libdispatch.bb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
SUMMARY = "Libdispatch"
3+
HOMEPAGE = "https://github.com/apple/swift-corelibs-libdispatch"
4+
5+
LICENSE = "Apache-2.0"
6+
LIC_FILES_CHKSUM = "file://${S}/LICENSE;md5=1cd73afe3fb82e8d5c899b9d926451d0"
7+
8+
require swift-version.inc
9+
PV = "${SWIFT_VERSION}"
10+
11+
SRC_URI = "git://github.com/apple/swift-corelibs-libdispatch.git;protocol=https;tag=swift-${PV}-RELEASE;nobranch=1"
12+
13+
DEPENDS = "swift-stdlib ncurses"
14+
15+
S = "${WORKDIR}/git"
16+
17+
inherit swift-cmake-base
18+
19+
TARGET_LDFLAGS += "-L${STAGING_DIR_TARGET}/usr/lib/swift/linux"
20+
21+
# Enable Swift parts
22+
EXTRA_OECMAKE += "-DENABLE_SWIFT=YES"
23+
24+
# Ensure the right CPU is targeted
25+
cmake_do_generate_toolchain_file_append() {
26+
sed -i 's/set([ ]*CMAKE_SYSTEM_PROCESSOR .*[ ]*)/set(CMAKE_SYSTEM_PROCESSOR ${TARGET_CPU_NAME})/' ${WORKDIR}/toolchain.cmake
27+
}
28+
29+
do_install_append() {
30+
# Copy cmake build modules
31+
mkdir -p ${D}${libdir}/swift/dispatch/cmake
32+
cp -rf ${WORKDIR}/build/cmake/modules/* ${D}${libdir}/swift/dispatch/cmake/
33+
}
34+
35+
FILES_${PN} = "${libdir}/swift/*"
36+
INSANE_SKIP_${PN} = "file-rdeps"

0 commit comments

Comments
 (0)