diff --git a/Package.swift b/Package.swift index 8237c1a4..488ba05a 100644 --- a/Package.swift +++ b/Package.swift @@ -2,8 +2,13 @@ import PackageDescription +let gitCommit: String? = Context.gitInformation?.currentCommit +let gitTag: String? = Context.gitInformation?.currentTag + let cSettings: [CSetting] = [ .define("_GNU_SOURCE", .when(platforms: [.linux])), + .define("XTL_GIT_COMMIT", to: gitCommit.map { "\"\($0)\"" } ?? "NULL"), + .define("XTL_GIT_TAG", to: gitTag.map { "\"\($0)\"" } ?? "NULL"), ] let package = Package( diff --git a/Sources/CXKit/include/version_shims.h b/Sources/CXKit/include/version_shims.h new file mode 100644 index 00000000..19b0e7b4 --- /dev/null +++ b/Sources/CXKit/include/version_shims.h @@ -0,0 +1,7 @@ +inline static const char * _Nullable xtl_git_commit() { + return XTL_GIT_COMMIT; +} + +inline static const char * _Nullable xtl_git_tag() { + return XTL_GIT_TAG; +} diff --git a/Sources/XToolSupport/XToolCommand.swift b/Sources/XToolSupport/XToolCommand.swift index ca5600fe..056c06c0 100644 --- a/Sources/XToolSupport/XToolCommand.swift +++ b/Sources/XToolSupport/XToolCommand.swift @@ -12,6 +12,7 @@ struct XToolCommand: AsyncParsableCommand { static let configuration = CommandConfiguration( commandName: "xtool", abstract: "Cross-platform Xcode replacement", + version: XTool.version?.description ?? "unknown", groupedSubcommands: [ CommandGroup( name: "Configuration", diff --git a/Sources/XToolSupport/XToolVersion.swift b/Sources/XToolSupport/XToolVersion.swift new file mode 100644 index 00000000..c96f478e --- /dev/null +++ b/Sources/XToolSupport/XToolVersion.swift @@ -0,0 +1,26 @@ +import CXKit + +extension XTool { + public struct Version: Sendable, Hashable { + public var commit: String + public var tag: String? + } + + public static let version: Version? = { + guard let commit = xtl_git_commit() else { return nil } + return Version( + commit: String(cString: commit), + tag: xtl_git_tag().map { String(cString: $0) } + ) + }() +} + +extension XTool.Version: CustomStringConvertible { + public var description: String { + if let tag { + "xtool version \(tag)" + } else { + "xtool commit \(commit)" + } + } +}