@@ -73,7 +73,7 @@ private final class ToolWrapper {
7373 _delegate. execute_command = { return BuildSystem . toCommandWrapper ( $0!) . executeCommand ( $1!, $2!, $3!, $4!) }
7474
7575 // Create the low-level command.
76- wrapper. _command = Command ( llb_buildsystem_external_command_create ( name, _delegate) )
76+ wrapper. _command = Command ( handle : llb_buildsystem_external_command_create ( name, _delegate) )
7777
7878 return wrapper. _command. handle
7979 }
@@ -99,7 +99,7 @@ private final class CommandWrapper {
9999
100100 init ( command: ExternalCommand ) {
101101 self . command = command
102- self . _command = Command ( nil )
102+ self . _command = Command ( handle : nil )
103103 }
104104
105105 func getSignature( _: OpaquePointer , _ data: UnsafeMutablePointer < llb_data_t > ) {
@@ -199,42 +199,91 @@ extension SchedulerAlgorithm {
199199
200200/// Handle for a command as invoked by the low-level BuildSystem.
201201public struct Command : Hashable , CustomStringConvertible , CustomDebugStringConvertible {
202- fileprivate let handle : OpaquePointer ?
202+ private struct Values : Hashable {
203+ let name : String
204+ let shouldShowStatus : Bool
205+ let description : String
206+ let verboseDescription : String
207+ }
203208
204- fileprivate init ( _ handle: OpaquePointer ? ) {
205- self . handle = handle
209+ private enum Representation : Hashable {
210+ case handle( OpaquePointer ? )
211+ case values( Values )
212+ }
213+
214+ private let representation : Representation
215+
216+ fileprivate init ( handle: OpaquePointer ? ) {
217+ self . representation = . handle( handle)
218+ }
219+
220+ /// Creates a dummy `Command` with specific values for testing purposes.
221+ public init ( name: String , shouldShowStatus: Bool , description: String , verboseDescription: String ) {
222+ self . representation = . values( Values (
223+ name: name,
224+ shouldShowStatus: shouldShowStatus,
225+ description: description,
226+ verboseDescription: verboseDescription
227+ ) )
228+ }
229+
230+ fileprivate var handle : OpaquePointer ? {
231+ switch representation {
232+ case . handle( let handle) :
233+ return handle
234+ case . values:
235+ return nil
236+ }
206237 }
207238
208239 /// The command name.
209240 //
210241 // FIXME: We shouldn't need to expose this to use for mapping purposes, we should be able to use something more efficient.
211242 public var name : String {
212- var data = llb_data_t ( )
213- withUnsafeMutablePointer ( to: & data) { ( ptr: UnsafeMutablePointer < llb_data_t > ) in
214- llb_buildsystem_command_get_name ( handle, ptr)
243+ switch representation {
244+ case . handle( let handle) :
245+ var data = llb_data_t ( )
246+ withUnsafeMutablePointer ( to: & data) { ( ptr: UnsafeMutablePointer < llb_data_t > ) in
247+ llb_buildsystem_command_get_name ( handle, ptr)
248+ }
249+ return stringFromData ( data)
250+ case . values( let values) :
251+ return values. name
215252 }
216- return stringFromData ( data)
217253 }
218254
219255 /// Whether the default status reporting shows status for the command.
220256 public var shouldShowStatus : Bool {
221- return llb_buildsystem_command_should_show_status ( handle)
257+ switch representation {
258+ case . handle( let handle) :
259+ return llb_buildsystem_command_should_show_status ( handle)
260+ case . values( let values) :
261+ return values. shouldShowStatus
262+ }
222263 }
223264
224265 /// The description provided by the command.
225266 public var description : String {
226- let name = llb_buildsystem_command_get_description ( handle) !
227- defer { free ( name) }
228-
229- return String ( cString: name)
267+ switch representation {
268+ case . handle( let handle) :
269+ let name = llb_buildsystem_command_get_description ( handle) !
270+ defer { free ( name) }
271+ return String ( cString: name)
272+ case . values( let values) :
273+ return values. description
274+ }
230275 }
231276
232277 /// The verbose description provided by the command.
233278 public var verboseDescription : String {
234- let name = llb_buildsystem_command_get_verbose_description ( handle) !
235- defer { free ( name) }
236-
237- return String ( cString: name)
279+ switch representation {
280+ case . handle( let handle) :
281+ let name = llb_buildsystem_command_get_verbose_description ( handle) !
282+ defer { free ( name) }
283+ return String ( cString: name)
284+ case . values( let values) :
285+ return values. verboseDescription
286+ }
238287 }
239288
240289 /// The debug description provides the verbose description.
@@ -243,11 +292,11 @@ public struct Command: Hashable, CustomStringConvertible, CustomDebugStringConve
243292 }
244293
245294 public func hash( into hasher: inout Hasher ) {
246- hasher. combine ( handle! )
295+ hasher. combine ( representation )
247296 }
248297
249298 public static func == ( lhs: Command , rhs: Command ) -> Bool {
250- return lhs. handle == rhs. handle
299+ return lhs. representation == rhs. representation
251300 }
252301}
253302
@@ -571,28 +620,28 @@ public final class BuildSystem {
571620 _delegate. lookup_tool = { return BuildSystem . toSystem ( $0!) . lookupTool ( $1!) }
572621 _delegate. had_command_failure = { BuildSystem . toSystem ( $0!) . hadCommandFailure ( ) }
573622 _delegate. handle_diagnostic = { BuildSystem . toSystem ( $0!) . handleDiagnostic ( $1, String ( cString: $2!) , Int ( $3) , Int ( $4) , String ( cString: $5!) ) }
574- _delegate. command_status_changed = { BuildSystem . toSystem ( $0!) . commandStatusChanged ( Command ( $1) , $2) }
575- _delegate. command_preparing = { BuildSystem . toSystem ( $0!) . commandPreparing ( Command ( $1) ) }
576- _delegate. command_started = { BuildSystem . toSystem ( $0!) . commandStarted ( Command ( $1) ) }
577- _delegate. should_command_start = { BuildSystem . toSystem ( $0!) . shouldCommandStart ( Command ( $1) ) }
578- _delegate. command_finished = { BuildSystem . toSystem ( $0!) . commandFinished ( Command ( $1) , $2) }
579- _delegate. command_had_error = { BuildSystem . toSystem ( $0!) . commandHadError ( Command ( $1) , $2!) }
580- _delegate. command_had_note = { BuildSystem . toSystem ( $0!) . commandHadNote ( Command ( $1) , $2!) }
581- _delegate. command_had_warning = { BuildSystem . toSystem ( $0!) . commandHadWarning ( Command ( $1) , $2!) }
623+ _delegate. command_status_changed = { BuildSystem . toSystem ( $0!) . commandStatusChanged ( Command ( handle : $1) , $2) }
624+ _delegate. command_preparing = { BuildSystem . toSystem ( $0!) . commandPreparing ( Command ( handle : $1) ) }
625+ _delegate. command_started = { BuildSystem . toSystem ( $0!) . commandStarted ( Command ( handle : $1) ) }
626+ _delegate. should_command_start = { BuildSystem . toSystem ( $0!) . shouldCommandStart ( Command ( handle : $1) ) }
627+ _delegate. command_finished = { BuildSystem . toSystem ( $0!) . commandFinished ( Command ( handle : $1) , $2) }
628+ _delegate. command_had_error = { BuildSystem . toSystem ( $0!) . commandHadError ( Command ( handle : $1) , $2!) }
629+ _delegate. command_had_note = { BuildSystem . toSystem ( $0!) . commandHadNote ( Command ( handle : $1) , $2!) }
630+ _delegate. command_had_warning = { BuildSystem . toSystem ( $0!) . commandHadWarning ( Command ( handle : $1) , $2!) }
582631 _delegate. command_cannot_build_output_due_to_missing_inputs = {
583632 let inputsPtr = $3!
584633 let inputs = ( 0 ..< Int ( $4) ) . map { BuildKey ( key: inputsPtr [ $0] ) }
585- BuildSystem . toSystem ( $0!) . commandCannotBuildOutputDueToMissingInputs ( Command ( $1) , BuildKey ( key: $2!. pointee) , inputs)
634+ BuildSystem . toSystem ( $0!) . commandCannotBuildOutputDueToMissingInputs ( Command ( handle : $1) , BuildKey ( key: $2!. pointee) , inputs)
586635 }
587636 _delegate. cannot_build_node_due_to_multiple_producers = {
588637 let commandsPtr = $2!
589- let commands = ( 0 ..< Int ( $3) ) . map { Command ( commandsPtr [ $0] ) }
638+ let commands = ( 0 ..< Int ( $3) ) . map { Command ( handle : commandsPtr [ $0] ) }
590639 BuildSystem . toSystem ( $0!) . cannotBuildNodeDueToMultipleProducers ( BuildKey ( key: $1!. pointee) , commands)
591640 }
592- _delegate. command_process_started = { BuildSystem . toSystem ( $0!) . commandProcessStarted ( Command ( $1) , ProcessHandle ( $2!) ) }
593- _delegate. command_process_had_error = { BuildSystem . toSystem ( $0!) . commandProcessHadError ( Command ( $1) , ProcessHandle ( $2!) , $3!) }
594- _delegate. command_process_had_output = { BuildSystem . toSystem ( $0!) . commandProcessHadOutput ( Command ( $1) , ProcessHandle ( $2!) , $3!) }
595- _delegate. command_process_finished = { BuildSystem . toSystem ( $0!) . commandProcessFinished ( Command ( $1) , ProcessHandle ( $2!) , CommandExtendedResult ( $3!) ) }
641+ _delegate. command_process_started = { BuildSystem . toSystem ( $0!) . commandProcessStarted ( Command ( handle : $1) , ProcessHandle ( $2!) ) }
642+ _delegate. command_process_had_error = { BuildSystem . toSystem ( $0!) . commandProcessHadError ( Command ( handle : $1) , ProcessHandle ( $2!) , $3!) }
643+ _delegate. command_process_had_output = { BuildSystem . toSystem ( $0!) . commandProcessHadOutput ( Command ( handle : $1) , ProcessHandle ( $2!) , $3!) }
644+ _delegate. command_process_finished = { BuildSystem . toSystem ( $0!) . commandProcessFinished ( Command ( handle : $1) , ProcessHandle ( $2!) , CommandExtendedResult ( $3!) ) }
596645 _delegate. cycle_detected = {
597646 var rules = [ BuildKey] ( )
598647 UnsafeBufferPointer ( start: $1, count: Int ( $2) ) . forEach {
0 commit comments