Skip to content

Fix some stream operator deprecation warnings #6487

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 16 commits into from
Apr 27, 2023
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
4 changes: 0 additions & 4 deletions Sources/Basics/FileSystem/FileSystem+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,6 @@ extension FileSystem {
public func writeFileContents(_ path: AbsolutePath, string: String) throws {
return try self.writeFileContents(path, bytes: .init(encodingAsUTF8: string))
}

public func writeFileContents(_ path: AbsolutePath, provider: () -> String) throws {
return try self.writeFileContents(path, body: { stream in stream <<< provider() })
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This version that was using a stream operator created all parent directories of path if those didn't exist, which is inconsistent with the other overloads. This version feels quite redundant to writeFileContents(_ path: AbsolutePath, string: String), so I've cleaned it up and adjusted tests to create parent directories when needed.

}
}

extension FileSystem {
Expand Down
48 changes: 26 additions & 22 deletions Sources/Build/BuildDescription/ClangTargetBuildDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -310,20 +310,22 @@ public final class ClangTargetBuildDescription {
let bundleBasename = bundlePath.basename

let implFileStream = BufferedOutputByteStream()
implFileStream <<< """
#import <Foundation/Foundation.h>
implFileStream.send(
"""
#import <Foundation/Foundation.h>
NSBundle* \(target.c99name)_SWIFTPM_MODULE_BUNDLE() {
NSURL *bundleURL = [[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:@"\(bundleBasename)"];
NSBundle* \(target.c99name)_SWIFTPM_MODULE_BUNDLE() {
NSURL *bundleURL = [[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:@"\(bundleBasename)"];
NSBundle *preferredBundle = [NSBundle bundleWithURL:bundleURL];
if (preferredBundle == nil) {
return [NSBundle bundleWithPath:@"\(bundlePath.pathString)"];
}
NSBundle *preferredBundle = [NSBundle bundleWithURL:bundleURL];
if (preferredBundle == nil) {
return [NSBundle bundleWithPath:@"\(bundlePath.pathString)"];
}
return preferredBundle;
}
"""
return preferredBundle;
}
"""
)

let implFileSubpath = try RelativePath(validating: "resource_bundle_accessor.m")

Expand All @@ -338,21 +340,23 @@ public final class ClangTargetBuildDescription {
)

let headerFileStream = BufferedOutputByteStream()
headerFileStream <<< """
#import <Foundation/Foundation.h>
headerFileStream.send(
"""
#import <Foundation/Foundation.h>
#if __cplusplus
extern "C" {
#endif
#if __cplusplus
extern "C" {
#endif
NSBundle* \(target.c99name)_SWIFTPM_MODULE_BUNDLE(void);
NSBundle* \(target.c99name)_SWIFTPM_MODULE_BUNDLE(void);
#define SWIFTPM_MODULE_BUNDLE \(target.c99name)_SWIFTPM_MODULE_BUNDLE()
#define SWIFTPM_MODULE_BUNDLE \(target.c99name)_SWIFTPM_MODULE_BUNDLE()
#if __cplusplus
}
#endif
"""
#if __cplusplus
}
#endif
"""
)
let headerFile = derivedSources.root.appending("resource_bundle_accessor.h")
self.resourceAccessorHeaderFile = headerFile

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
let stream = BufferedOutputByteStream()

for object in self.objects {
stream <<< object.pathString.spm_shellEscaped() <<< "\n"
stream.send("\(object.pathString.spm_shellEscaped())\n")
}

try fs.createDirectory(self.linkFileListPath.parentDirectory, recursive: true)
Expand Down
130 changes: 86 additions & 44 deletions Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,23 +309,23 @@ public final class SwiftTargetBuildDescription {
guard needsResourceEmbedding else { return }

let stream = BufferedOutputByteStream()
stream <<< """
struct PackageResources {
stream.send(
"""
struct PackageResources {
"""
"""
)

try resources.forEach {
guard $0.rule == .embedInCode else { return }

let variableName = $0.path.basename.spm_mangledToC99ExtendedIdentifier()
let fileContent = try Data(contentsOf: URL(fileURLWithPath: $0.path.pathString)).map { String($0) }.joined(separator: ",")

stream <<< "static let \(variableName): [UInt8] = [\(fileContent)]\n"
stream.send("static let \(variableName): [UInt8] = [\(fileContent)]\n")
}

stream <<< """
}
"""
stream.send("}")

let subpath = try RelativePath(validating: "embedded_resources.swift")
self.derivedSources.relativePaths.append(subpath)
Expand Down Expand Up @@ -355,24 +355,26 @@ public final class SwiftTargetBuildDescription {
}

let stream = BufferedOutputByteStream()
stream <<< """
\(self.toolsVersion < .vNext ? "import" : "@_implementationOnly import") class Foundation.Bundle
stream.send(
"""
\(self.toolsVersion < .vNext ? "import" : "@_implementationOnly import") class Foundation.Bundle
extension Foundation.Bundle {
static let module: Bundle = {
let mainPath = \(mainPathSubstitution)
let buildPath = "\(bundlePath.pathString.asSwiftStringLiteralConstant)"
extension Foundation.Bundle {
static let module: Bundle = {
let mainPath = \(mainPathSubstitution)
let buildPath = "\(bundlePath.pathString.asSwiftStringLiteralConstant)"
let preferredBundle = Bundle(path: mainPath)
let preferredBundle = Bundle(path: mainPath)
guard let bundle = preferredBundle ?? Bundle(path: buildPath) else {
fatalError("could not load resource bundle: from \\(mainPath) or \\(buildPath)")
}
guard let bundle = preferredBundle ?? Bundle(path: buildPath) else {
fatalError("could not load resource bundle: from \\(mainPath) or \\(buildPath)")
}
return bundle
}()
}
"""
return bundle
}()
}
"""
)

let subpath = try RelativePath(validating: "resource_bundle_accessor.swift")

Expand Down Expand Up @@ -671,21 +673,41 @@ public final class SwiftTargetBuildDescription {
let path = self.tempsPath.appending("output-file-map.json")
let stream = BufferedOutputByteStream()

stream <<< "{\n"

let masterDepsPath = self.tempsPath.appending("master.swiftdeps")
stream <<< " \"\": {\n"
stream.send(
#"""
{
"": {
"""#
)
if self.buildParameters.useWholeModuleOptimization {
let moduleName = self.target.c99name
stream <<< " \"dependencies\": \"" <<< self.tempsPath.appending(component: moduleName + ".d")
.nativePathString(escaped: true) <<< "\",\n"
stream.send(
#"""
"dependencies": "\#(
self.tempsPath.appending(component: moduleName + ".d").nativePathString(escaped: true)
)",
"""#
)
// FIXME: Need to record this deps file for processing it later.
stream <<< " \"object\": \"" <<< self.tempsPath.appending(component: moduleName + ".o")
.nativePathString(escaped: true) <<< "\",\n"
stream.send(
#"""
"object": "\#(
self.tempsPath.appending(component: moduleName + ".o").nativePathString(escaped: true)
)",
"""#
)
}
stream <<< " \"swift-dependencies\": \"" <<< masterDepsPath.nativePathString(escaped: true) <<< "\"\n"
stream.send(
#"""
"swift-dependencies": "\#(masterDepsPath.nativePathString(escaped: true))"
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

some lines in the string output here used 2-space indentation inconsistently, preserving just in case

stream <<< " },\n"
"""#
)

// Write out the entries for each source file.
let sources = self.target.sources.paths + self.derivedSources.paths + self.pluginDerivedSources.paths
Expand All @@ -697,23 +719,39 @@ public final class SwiftTargetBuildDescription {

let swiftDepsPath = objectDir.appending(component: sourceFileName + ".swiftdeps")

stream <<< " \"" <<< source.nativePathString(escaped: true) <<< "\": {\n"
stream.send(
#"""
"\#(source.nativePathString(escaped: true))": {
"""#
)

if !self.buildParameters.useWholeModuleOptimization {
let depsPath = objectDir.appending(component: sourceFileName + ".d")
stream <<< " \"dependencies\": \"" <<< depsPath.nativePathString(escaped: true) <<< "\",\n"
stream.send(
#"""
"dependencies": "\#(depsPath.nativePathString(escaped: true))",
"""#
)
// FIXME: Need to record this deps file for processing it later.
}

stream <<< " \"object\": \"" <<< object.nativePathString(escaped: true) <<< "\",\n"

let partialModulePath = objectDir.appending(component: sourceFileName + "~partial.swiftmodule")
stream <<< " \"swiftmodule\": \"" <<< partialModulePath.nativePathString(escaped: true) <<< "\",\n"
stream <<< " \"swift-dependencies\": \"" <<< swiftDepsPath.nativePathString(escaped: true) <<< "\"\n"
stream <<< " }" <<< ((idx + 1) < sources.count ? "," : "") <<< "\n"

stream.send(
#"""
"object": "\#(object.nativePathString(escaped: true))",
"swiftmodule": "\#(partialModulePath.nativePathString(escaped: true))",
"swift-dependencies": "\#(swiftDepsPath.nativePathString(escaped: true))"
}\#((idx + 1) < sources.count ? "," : "")

"""#
)
}

stream <<< "}\n"
stream.send("}\n")

try self.fileSystem.createDirectory(path.parentDirectory, recursive: true)
try self.fileSystem.writeFileContents(path, bytes: stream.bytes)
Expand All @@ -724,19 +762,23 @@ public final class SwiftTargetBuildDescription {
private func generateModuleMap() throws -> AbsolutePath {
let path = self.tempsPath.appending(component: moduleMapFilename)

let stream = BufferedOutputByteStream()
stream <<< "module \(self.target.c99name) {\n"
stream <<< " header \"" <<< self.objCompatibilityHeaderPath.pathString <<< "\"\n"
stream <<< " requires objc\n"
stream <<< "}\n"
let bytes = ByteString(
#"""
module \#(self.target.c99name) {
header "\#(self.objCompatibilityHeaderPath.pathString)"
requires objc
}
"""#.utf8
)

// Return early if the contents are identical.
if self.fileSystem.isFile(path), try self.fileSystem.readFileContents(path) == stream.bytes {
if self.fileSystem.isFile(path), try self.fileSystem.readFileContents(path) == bytes {
return path
}

try self.fileSystem.createDirectory(path.parentDirectory, recursive: true)
try self.fileSystem.writeFileContents(path, bytes: stream.bytes)
try self.fileSystem.writeFileContents(path, bytes: bytes)

return path
}
Expand Down
Loading