7 variable ReservedFlowNames {
8 CREATE CREATEWORKFLOW WORKFLOW
9 SYNTHESIS SYNTH IMPLEMENTATION IMPL IMPLEMENT
10 SIMULATION SIMULATE BITSTREAM
13 foreach _reserved_name $Flow::ReservedFlowNames {
15 "'$_reserved_name' is a reserved project-flow verb; a top-level command\
16 would shadow the same-named flow on every tool. Rename it, or reach\
17 the flow via 'tool <tool> flow $_reserved_name <project>'."
24 proc RegisterFlow {tool key raw_dict args} {
25 set tool_ns [
expr {[namespace exists $tool] ? $tool : [Tools::ResolveAlias $tool]}]
27 Msg Warning "Skipping flow '$key': unknown tool '$tool'"
30 set tool_key [
string toupper [
namespace tail $tool_ns]]
31 set tool_short [
string tolower [
namespace tail $tool_ns]]
34 dict for {k v} $raw_dict { dict set norm [string tolower $k] $v }
37 dict set raw_dict requires_proj true
38 dict set raw_dict ide $tool_short
40 set flow_group "TOOL.$tool_key.FLOW"
43 "Flows provided by [
namespace tail $tool_ns].\
44 Usage: $tool_short flow <flow> <project>"]
49 proc RegisterCustomFlows {flow_dir} {
50 if {![
file isdirectory $flow_dir]} {
return}
51 foreach f [lsort [glob -nocomplain -directory $flow_dir *.tcl]] {
71 namespace eval Artifact {
75 if {![CurrentProject::Exists project_name]} {
return ""}
76 return [
file join [CurrentProject::Get build_dir] .hog tokens]
79 proc Write {name record} {
82 Msg Warning "Artifact::Write $name: no project is current, nothing persisted"
86 set fh [open [
file join $dir "$name.tcl"] w]
93 if {$dir eq ""} {
return {}}
94 set path [
file join $dir "$name.tcl"]
95 if {![
file exists $path]} {
return {}}
104 if {$dir eq ""} {
return 0}
105 return [
file exists [
file join $dir "$name.tcl"]]
110 if {$dir eq ""} {
return}
111 file delete -force [
file join $dir "$name.tcl"]
115 namespace eval FlowControl {
117 if {![
info exists _state]} {
118 set _state [tdict create \
119 stages [tlist create] \
120 status [
tstr continue] \
122 tokens [tlist create] \
131 tlist foreachval s [tdict get $_state stages] { lappend r $s }
134 proc _set_stages {lst} {
136 tdict set _state stages [tlist create {*}$lst]
141 tlist foreachval t [tdict get $_state tokens] { lappend r $t }
152 proc _stage_context {} {
154 return [
expr {[tdict getval $_state stage] ne "" && [CurrentProject::Exists project_name]}]
162 proc _persist {name path} {
164 set producer [
StageProc [tdict getval $_state stage]]
165 if {$producer eq ""} {
set producer [tdict getval $_state stage]}
166 set record [dict create producer $producer describe [Repo::Get Tag] mtime [
clock seconds]]
168 if {![
file exists $path]} {
169 return -code error "Produce $name: no file at '$path' - the stage claimed to have\
170 written it, but it isn't there."
172 dict set record path $path
173 dict set record file_mtime [
file mtime $path]
179 proc _check_freshness {name {_seen {}}} {
180 if {$name in $_seen} {
return}
183 if {[dict size $record] == 0} {
return}
184 set my_mtime [dict get $record mtime]
187 if {$pnode eq {}} continue
188 set upstream [tdict getobjor $pnode requires [tlist create]]
189 tlist foreachval up_name $upstream {
190 set up_record [Artifact::Read $up_name]
191 # A required artifact that was never produced is normal (conditional
192 # artifacts), not something to warn about.
193 if {[dict size $up_record] == 0} continue
194 if {[dict get $up_record mtime] > $my_mtime} {
195 Msg Warning "'$name' (produced by $producer_canon) is older than '$up_name',\
196 which it requires - '$up_name' was produced more recently. Consider rebuilding\
199 _check_freshness $up_name $_seen
208 proc Produce {name {path {}}} {
213 tdict set _state tokens [tlist create {*}$tok]
220 proc ProducePersistent {name} {
232 foreach token $args {
233 if {$token in $tok} { continue}
236 if {[dict exists $record path]} {
237 set path [dict get $record path]
238 if {![
file exists $path]} {
239 Msg Warning "Artifact '$token' was recorded (by [dict get $record producer]) but its\
240 file is missing: $path - dropping the stale record."
244 if {[dict exists $record file_mtime] && [
file mtime $path] != [dict get $record file_mtime]} {
245 Msg Warning "Artifact '$token' at $path has changed on disk since it was recorded (by\
246 [dict get $record producer]) - dropping the stale record."
256 proc Require {args} {
258 foreach token $args {
259 if {[
Has $token]} continue
261 set by [
expr {[llength $producers] > 0 ? " (produced by [join $producers {, }])" : ""}]
262 tdict set _state status abort
263 tdict set _state reason "Required token '$token' not found$by while executing proc [
lindex [
info level -1] 0]. "
265 set _flow_run_level -1
266 for {
set i 1} {$i < [
info level]} {
incr i} {
267 if {[
lindex [
info level $i] 0] eq "FlowControl::Run"} {
268 set _flow_run_level $i
273 if {$_flow_run_level < 0} {
274 Msg Warning "Require called outside of FlowControl::Run. Don't know what to do... returning..."
277 return -level [
expr {[info level] - $_flow_run_level}]
283 proc RequireOr {token script} {
284 if {[
Has $token]} {
return}
285 set _code [
catch {uplevel 1 $script} _res _opts]
286 if {$_code == 0} {
return}
287 if {$_code != 2} {
return -options $_opts $_res}
289 set _flow_run_level -1
290 for {
set i 1} {$i < [
info level]} {
incr i} {
291 if {[
lindex [
info level $i] 0] eq "FlowControl::Run"} {
292 set _flow_run_level $i
296 if {$_flow_run_level < 0} {
return -options $_opts $_res}
297 return -level [
expr {[info level] - $_flow_run_level}] $_res
301 proc ArtifactPath {name} {
303 if {![dict exists $record path]} {
return ""}
304 return [dict get $record path]
307 proc ClearTokens {} {
309 tdict set _state tokens [tlist create]
316 proc AppendStages {new} {
318 lappend stages {*}$new
322 proc InsertStagesAfter {anchor new} {
324 set idx [lsearch -exact $stages $anchor]
326 set stages [
linsert $stages [
expr {$idx + 1}] {*}$new]
328 Msg Warning "FlowControl InsertStagesAfter: '$anchor' not found... skipping..."
333 proc InsertStagesBefore {anchor new} {
335 set idx [lsearch -exact $stages $anchor]
337 set stages [
linsert $stages $idx {*}$new]
339 Msg Warning "FlowControl InsertStagesBefore: '$anchor' not found... skipping..."
344 proc InsertStages {new} {
347 _set_stages [
linsert $stages [
expr {$_i + 1}] {*}$new]
350 proc RemoveStages {to_remove} {
352 foreach s $to_remove {
353 set idx [lsearch -exact $stages $s]
355 set stages [
lreplace $stages $idx $idx]
356 set idx [lsearch -exact $stages $s]
362 proc ReplaceStage {old new_stages} {
364 set idx [lsearch -exact $stages $old]
366 _set_stages [
lreplace $stages $idx $idx {*}$new_stages]
368 Msg Warning "FlowControl ReplaceStage: '$old' not found"
376 proc StageProc {stage} {
377 if {[
string match "::*" $stage]} {
return $stage}
379 if {$tool_ns eq "" || $tool_ns eq "tclsh"} {
return ""}
381 if {$node ne {}} {
return [tdict getval $node script]}
382 return ${tool_ns}::${stage}
385 proc ClearRemaining {} {
390 proc Exit {{reason ""}} {
392 tdict set _state status [
tstr exit]
393 tdict set _state reason [
tstr $reason]
395 set _flow_run_level -1
396 for {
set i 1} {$i < [
info level]} {
incr i} {
397 if {[
lindex [
info level $i] 0] eq "FlowControl::Run"} {
398 set _flow_run_level $i
403 if {$_flow_run_level < 0} {
404 Msg Warning "Require called outside of FlowControl::Run. Don't know what to do... returning..."
408 return -level [
expr {[info level] - $_flow_run_level}]
411 proc Run {stages flow} {
417 if {[
llength $stages] == 0} {
418 Msg Error "Flow $flow has no stages to run - it either declares none, or\
419 every stage it @references failed to resolve."
420 return -code error "flow '$flow' has no stages"
423 tdict set _state stages [tlist create {*}$stages]
424 tdict set _state status [
tstr continue]
425 tdict set _state reason [
tstr ""]
428 Msg Info "Running flow $flow: $stages"
430 while {$_i < [
llength [
_stages]]} {
431 set stage [
lindex [
_stages] $_i]
432 tdict set _state stage [
tstr $stage]
435 Msg Info "Flow $flow: Running stage $stage..."
440 if {$_proc eq "" || [
info commands $_proc] eq ""} {
441 tdict set _state status [
tstr abort]
442 tdict set _state reason [
tstr "stage '$stage' has no implementation\
443 ([
expr {$_proc eq "" ? "no active tool to resolve it against" : "no proc $_proc"}])"]
445 set _ns [
namespace qualifiers $_proc]
446 if {[
info commands ${_ns}::@PRE_$stage] ne ""} { ${_ns}::@PRE_$stage}
447 Msg Debug "Flow $flow: stage '$stage' -> $_proc"
449 if {[
info commands ${_ns}::@POST_$stage] ne ""} { ${_ns}::@POST_$stage}
456 set status [tdict getval $_state status]
457 set reason [tdict getval $_state reason]
460 tdict set _state stage [
tstr ""]
461 Msg Error "Flow $flow aborted at '$stage': $reason"
462 return -code error $reason
465 tdict set _state stage [
tstr ""]
466 if {$reason ne ""} {
Msg Info "Flow \[$flow:$stage\] exiting: $reason"}
474 tdict set _state stage [
tstr ""]