-
Notifications
You must be signed in to change notification settings - Fork 57
Added utility for generating a pkg-config file #32
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| #!/usr/bin/env swift | ||
| import Foundation | ||
|
|
||
| /// Runs the specified program at the provided path. | ||
| /// - parameter path: The full path of the executable you | ||
| /// wish to run. | ||
| /// - parameter args: The arguments you wish to pass to the | ||
| /// process. | ||
| /// - returns: The standard output of the process, or nil if it was empty. | ||
| func run(_ path: String, args: [String] = []) -> String? { | ||
| let pipe = Pipe() | ||
| let process = Process() | ||
| process.launchPath = path | ||
| process.arguments = args | ||
| process.standardOutput = pipe | ||
| process.launch() | ||
| process.waitUntilExit() | ||
|
|
||
| let data = pipe.fileHandleForReading.readDataToEndOfFile() | ||
| guard let result = String(data: data, encoding: .utf8)? | ||
| .trimmingCharacters(in: .whitespacesAndNewlines), | ||
| !result.isEmpty else { return nil } | ||
| return result | ||
| } | ||
|
|
||
| /// Finds the location of the provided binary on your system. | ||
| func which(_ name: String) -> String? { | ||
| return run("/usr/bin/which", args: [name]) | ||
| } | ||
|
|
||
| extension String: Error { | ||
| /// Replaces all occurrences of characters in the provided set with | ||
| /// the provided string. | ||
| func replacing(charactersIn characterSet: CharacterSet, | ||
| with separator: String) -> String { | ||
| let components = self.components(separatedBy: characterSet) | ||
| return components.joined(separator: separator) | ||
| } | ||
| } | ||
|
|
||
| func makeFile() throws { | ||
| let pkgConfigPath = "/usr/local/lib/pkgconfig" | ||
| let pkgConfigDir = URL(fileURLWithPath: pkgConfigPath) | ||
|
|
||
| // Make /usr/local/lib/pkgconfig if it doesn't already exist | ||
| if !FileManager.default.fileExists(atPath: pkgConfigPath) { | ||
| try FileManager.default.createDirectory(at: pkgConfigDir, | ||
| withIntermediateDirectories: true) | ||
| } | ||
| let cllvmPath = pkgConfigDir.appendingPathComponent("cllvm.pc") | ||
|
|
||
| /// Ensure we have llvm-config in the PATH | ||
| guard let llvmConfig = which("llvm-config") else { | ||
| throw "Failed to find llvm-config. Ensure llvm-config is installed and " + | ||
| "in your PATH" | ||
| } | ||
|
|
||
| /// Extract the info we need from llvm-config | ||
|
|
||
| print("Found llvm-config at \(llvmConfig)...") | ||
| print("Running llvm-config --libs all...") | ||
| let ldFlags = run(llvmConfig, args: ["--libs", "all"])! | ||
| .replacing(charactersIn: .newlines, with: "") | ||
| print("Running llvm-config --version...") | ||
| let version = run(llvmConfig, args: ["--version"])! | ||
| .replacing(charactersIn: .newlines, with: "") | ||
|
|
||
| // SwiftPM has a whitelisted set of cflags that it understands, and | ||
| // unfortunately that includes almost everything but the include dir. | ||
|
|
||
| print("Running llvm-config --cflags...") | ||
| let cFlags = run(llvmConfig, args: ["--cflags"])! | ||
| .replacing(charactersIn: .newlines, with: "") | ||
| .components(separatedBy: " ") | ||
| .filter { $0.hasPrefix("-I") } | ||
| .joined(separator: " ") | ||
|
|
||
| /// Emit the pkg-config file to the path | ||
|
|
||
| let s = [ | ||
| "Name: cllvm", | ||
| "Description: The llvm library", | ||
| "Version: \(version)", | ||
| "Libs: \(ldFlags)", | ||
| "Requires.private:", | ||
| "Cflags: \(cFlags)", | ||
| ].joined(separator: "\n") | ||
|
|
||
| print("Writing pkg-config file to \(cllvmPath.path)...") | ||
|
|
||
| try s.write(toFile: cllvmPath.path, atomically: true, encoding: .utf8) | ||
|
|
||
| print("\nSuccessfully wrote pkg-config file!") | ||
| print("Make sure to re-run this script when you update LLVM.") | ||
| } | ||
|
|
||
| do { | ||
| try makeFile() | ||
| } catch { | ||
| print("error: \(error)") | ||
| exit(-1) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is all that helpful, users don't really need to know this location to build. Maybe mention invoking
which llvm-configor something.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It helped me because it didn't end up in my path after installing from homebrew :sad:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see. Carry on.