Skip to content

Commit 989f373

Browse files
committed
Merge pull request #169 from aciidb0mb3r/pass_fd
pass fd to PackageDescription while parsing manifests instead of using SWIFT_DUMP_PACKAGE
2 parents cc15171 + d2e3c97 commit 989f373

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

Sources/ManifestParser/Manifest+parse.swift

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import struct PackageType.Manifest
1212
import func POSIX.realpath
1313
import PackageDescription
1414
import Utility
15+
import func POSIX.fopen
16+
import func libc.fileno
17+
import func libc.unlink
18+
import func libc.fclose
1519

1620
extension Manifest {
1721
public init(path pathComponents: String..., baseURL: String) throws {
@@ -49,16 +53,10 @@ extension Manifest {
4953

5054
private func parse(manifestPath: String) throws -> String? {
5155

52-
// For now, we load the manifest by having Swift interpret it directly
53-
// and using a special environment variable to trigger the PackageDescription
54-
// library to dump the package (as TOML) at exit. Eventually, we should
55-
// have two loading processes, one that loads only the the declarative
56-
// package specification using the Swift compiler directly and validates
57-
// it.
58-
//
59-
// FIXME: We also should make the mechanism for communicating the
60-
// package between the PackageDescription module more robust, for example by passing
61-
// in the id of another file descriptor to write the output onto.
56+
// For now, we load the manifest by having Swift interpret it directly.
57+
// Eventually, we should have two loading processes, one that loads only
58+
// the the declarative package specification using the Swift compiler
59+
// directly and validates it.
6260

6361
let libdir = Resources.runtimeLibPath
6462

@@ -71,5 +69,17 @@ private func parse(manifestPath: String) throws -> String? {
7169
#endif
7270
cmd += [manifestPath]
7371

74-
return try popen(cmd, environment: ["SWIFT_DUMP_PACKAGE": "1"]).chuzzle()
72+
//Create and open a temporary file to write toml to
73+
let filePath = Path.join(manifestPath.parentDirectory, ".Package.toml")
74+
let fp = try fopen(filePath, mode: .Write)
75+
defer { fclose(fp) }
76+
77+
//Pass the fd in arguments
78+
cmd += ["-fileno", "\(fileno(fp))"]
79+
try system(cmd)
80+
81+
let toml = try File(path: filePath).enumerate().reduce("") { $0 + "\n" + $1 }
82+
unlink(filePath) //Delete the temp file after reading it
83+
84+
return toml != "" ? toml : nil
7585
}

Sources/PackageDescription/Package.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import Glibc
1414
import Darwin.C
1515
#endif
1616

17-
1817
/// The description for a complete package.
1918
public final class Package {
2019
/// The description for a package dependency.
@@ -69,8 +68,10 @@ public final class Package {
6968
// FIXME: This doesn't belong here, but for now is the mechanism we use
7069
// to get the interpreter to dump the package when attempting to load a
7170
// manifest.
72-
if getenv("SWIFT_DUMP_PACKAGE") != nil {
73-
dumpPackageAtExit(self)
71+
72+
if let fileNoOptIndex = Process.arguments.indexOf("-fileno"),
73+
fileNo = Int32(Process.arguments[fileNoOptIndex + 1]) {
74+
dumpPackageAtExit(self, fileNo: fileNo)
7475
}
7576
}
7677
}
@@ -128,16 +129,21 @@ public func ==(lhs: Package.Dependency, rhs: Package.Dependency) -> Bool {
128129

129130
// MARK: Package Dumping
130131

131-
private var thePackageToDump: Package? = nil
132-
private func dumpPackageAtExit(package: Package) {
132+
private var dumpInfo: (package: Package, fileNo: Int32)? = nil
133+
private func dumpPackageAtExit(package: Package, fileNo: Int32) {
133134
func dump() {
134-
print(thePackageToDump!.toTOML())
135+
guard let dumpInfo = dumpInfo else { return }
136+
let fd = fdopen(dumpInfo.fileNo, "w")
137+
guard fd != nil else { return }
138+
fputs(dumpInfo.package.toTOML(), fd)
135139
for product in products {
136-
print("[[products]]")
137-
print(product.toTOML())
138-
print()
140+
fputs("[[products]]", fd)
141+
fputs("\n", fd)
142+
fputs(product.toTOML(), fd)
143+
fputs("\n", fd)
139144
}
145+
fclose(fd)
140146
}
141-
thePackageToDump = package
147+
dumpInfo = (package, fileNo)
142148
atexit(dump)
143149
}

0 commit comments

Comments
 (0)