Skip to content

Commit 95cd4b6

Browse files
committed
Compile libarchive with the same clang and musl as used with swiftly
1 parent ab2354c commit 95cd4b6

File tree

2 files changed

+71
-13
lines changed

2 files changed

+71
-13
lines changed

Tools/build-swiftly-release/BuildSwiftlyRelease.swift

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public struct Error: LocalizedError {
1313
}
1414

1515
public func runProgramEnv(_ args: String..., quiet: Bool = false, env: [String: String]?) throws {
16+
print("\(args.joined(separator: " "))")
1617
let process = Process()
1718
process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
1819
process.arguments = args
@@ -40,6 +41,7 @@ public func runProgramEnv(_ args: String..., quiet: Bool = false, env: [String:
4041
}
4142

4243
public func runProgram(_ args: String..., quiet: Bool = false) throws {
44+
print("\(args.joined(separator: " "))")
4345
let process = Process()
4446
process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
4547
process.arguments = args
@@ -295,10 +297,18 @@ struct BuildSwiftlyRelease: AsyncParsableCommand {
295297
let sdkName = "swift-6.0.3-RELEASE_static-linux-0.0.1"
296298

297299
// FIXME: Adjust the URL and checksum to match the toolchain that is being used
300+
301+
#if arch(arm64)
302+
let arch = "aarch64"
303+
#else
304+
let arch = "x86_64"
305+
#endif
306+
298307
try runProgram(swift, "sdk", "install", "https://download.swift.org/swift-6.0.3-release/static-sdk/swift-6.0.3-RELEASE/swift-6.0.3-RELEASE_static-linux-0.0.1.artifactbundle.tar.gz", "--checksum", "67f765e0030e661a7450f7e4877cfe008db4f57f177d5a08a6e26fd661cdd0bd")
299308

300309
var customEnv = ProcessInfo.processInfo.environment
301-
customEnv["CC"] = "musl-gcc"
310+
customEnv["CC"] = "\(cwd)/Tools/build-swiftly-release/musl-clang"
311+
customEnv["MUSL_PREFIX"] = "\(FileManager.default.homeDirectoryForCurrentUser.path)/.swiftpm/swift-sdks/\(sdkName).artifactbundle/\(sdkName)/swift-linux-musl/musl-1.2.5.sdk/\(arch)/usr"
302312

303313
try runProgramEnv(
304314
"./configure",
@@ -328,18 +338,9 @@ struct BuildSwiftlyRelease: AsyncParsableCommand {
328338

329339
FileManager.default.changeCurrentDirectoryPath(cwd)
330340

331-
#if arch(arm64)
332-
let muslTriple = "aarch64-swift-linux-musl"
333-
#else
334-
let muslTriple = "x86_64-swift-linux-musl"
335-
#endif
336-
337-
do {
338-
try runProgram(swift, "build", "--swift-sdk", sdkName, "--triple=\(muslTriple)", "--product=swiftly", "--pkg-config-path=\(pkgConfigPath)/lib/pkgconfig", "--static-swift-stdlib", "--configuration=release")
339-
} catch {
340-
try runProgram(swift, "sdk", "remove", sdkName)
341-
throw error
342-
}
341+
// FIXME: running this twice fixes certain linker errors
342+
try? runProgram(swift, "build", "--swift-sdk", sdkName, "--product=swiftly", "--pkg-config-path=\(pkgConfigPath)/lib/pkgconfig", "--static-swift-stdlib", "--configuration=release")
343+
try runProgram(swift, "build", "--swift-sdk", sdkName, "--product=swiftly", "--pkg-config-path=\(pkgConfigPath)/lib/pkgconfig", "--static-swift-stdlib", "--configuration=release")
343344
try runProgram(swift, "sdk", "remove", sdkName)
344345

345346
let releaseDir = cwd + "/.build/release"
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/sh
2+
3+
PREFIX=${MUSL_PREFIX:-"/usr/local/musl"}
4+
if [ ! -d "${PREFIX}" ]; then
5+
echo "invalid prefix: ${PREFIX}"
6+
return 1
7+
fi
8+
9+
CLANG=${REALCLANG:-"clang"}
10+
11+
hasNo() {
12+
pat="$1"
13+
shift 1
14+
15+
for e in "$@"; do
16+
if [ "$e" = "${pat}" ]; then
17+
return 1
18+
fi
19+
done
20+
return 0
21+
}
22+
23+
ARGS="-nostdinc"
24+
TAIL=""
25+
26+
if hasNo '-nostdinc' "$@"; then
27+
ARGS="${ARGS} -isystem ${PREFIX}/include"
28+
fi
29+
30+
if \
31+
hasNo '-c' "$@" && \
32+
hasNo '-S' "$@" && \
33+
hasNo '-E' "$@"
34+
then
35+
ARGS="${ARGS} -nostdlib"
36+
ARGS="${ARGS} -Wl,-dynamic-linker=${PREFIX}/lib/libc.so"
37+
ARGS="${ARGS} -L${PREFIX}/lib -L${PREFIX}/lib/swift/clang/lib/linux"
38+
39+
if hasNo '-nostartfiles' "$@" && \
40+
hasNo '-nostdlib' "$@" && \
41+
hasNo '-nodefaultlibs' "$@"
42+
then
43+
ARGS="${ARGS} ${PREFIX}/lib/crt1.o"
44+
ARGS="${ARGS} ${PREFIX}/lib/crti.o"
45+
46+
TAIL="${TAIL} ${PREFIX}/lib/crtn.o"
47+
fi
48+
49+
if hasNo '-nostdlib' "$@" && \
50+
hasNo '-nodefaultlibs' "$@"
51+
then
52+
TAIL="${TAIL} -lc -lclang_rt.builtins-$(uname -m)"
53+
fi
54+
fi
55+
56+
exec ${CLANG} ${ARGS} "$@" ${TAIL} -static
57+

0 commit comments

Comments
 (0)