Debian Codegraph @

tcl/modeval.tcl

raw
1 ##########################################################################
2
3 # MODEVAL.TCL, module evaluation procedures
4 # Copyright (C) 2002-2004 Mark Lakata
5 # Copyright (C) 2004-2017 Kent Mein
6 # Copyright (C) 2016-2026 Xavier Delaruelle
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ##########################################################################
22
23 # gather for the current top evaluation the information on all evaluations
24 # happening under its umbrella
25 proc registerModuleEval {context msgrecid {failedmod {}} {failedcontext {}}} {
26 set evalid [topState evalid]
27 set unset [expr {$failedmod eq {} ? 0 : 1}]
28 set contextset 0
29 # unload dependent reload evaluations are mixed with dependent unload ones
30 if {$context eq {depre_un}} {
31 set context depun
32 }
33
34 # add msgrecid to existing evaluation context list
35 if {[info exists ::g_moduleEval($evalid)]} {
36 for {set i 0} {$i < [llength $::g_moduleEval($evalid)]} {incr i 1} {
37 set contextevallist [lindex $::g_moduleEval($evalid) $i]
38 if {[lindex $contextevallist 0] eq $context} {
39 if {$unset} {
40 lrem contextevallist $msgrecid
41 } else {
42 lappend contextevallist $msgrecid
43 }
44 set ::g_moduleEval($evalid) [expr {[llength $contextevallist] > 1\
45 ? [lreplace $::g_moduleEval($evalid) $i $i $contextevallist]\
46 : [lreplace $::g_moduleEval($evalid) $i $i]}]
47 set contextset 1
48 break
49 }
50 }
51 }
52
53 # add msgrecid to new evaluation context list
54 if {!$unset && !$contextset} {
55 lappend ::g_moduleEval($evalid) [list $context $msgrecid]
56 }
57
58 # add mod to failed evaluation list
59 if {$unset} {
60 lappend ::g_moduleFailedEval($evalid) $failedcontext $failedmod
61 if {[depthState reportholdid]} {
62 lappend ::g_holdModuleFailedEval([currentState reportholdid])\
63 $evalid $failedcontext $failedmod
64 }
65 }
66 }
67
68 # record that module evaluation is set hidden
69 proc registerModuleEvalHidden {context msgrecid} {
70 set evalid [topState evalid]
71 # unload dependent reload evaluations are mixed with dependent unload ones
72 if {$context eq {depre_un}} {
73 set context depun
74 }
75 lappend ::g_moduleHiddenEval($evalid:$context) $msgrecid
76 }
77
78 proc changeContextOfModuleEval {mod old_context new_context} {
79 set evalid [topState evalid]
80 # find old and new context evaluations
81 for {set i 0} {$i < [llength $::g_moduleEval($evalid)]} {incr i 1} {
82 set context_eval_list [lindex $::g_moduleEval($evalid) $i]
83 if {[lindex $context_eval_list 0] eq $old_context} {
84 set old_context_idx $i
85 set old_context_eval_list $context_eval_list
86 } elseif {[lindex $context_eval_list 0] eq $new_context} {
87 set new_context_idx $i
88 set new_context_eval_list $context_eval_list
89 }
90 if {[info exists old_context_idx] && [info exists new_context_idx]} {
91 break
92 }
93 }
94
95 # module evaluation not found
96 if {![info exists old_context_idx]} {
97 return
98 }
99
100 # new context has no evaluation yet
101 if {![info exists new_context_idx]} {
102 set new_context_idx end+1
103 set new_context_eval_list [list $new_context]
104 }
105
106 # find evaluation id of module
107 foreach msgrecid [lrange $old_context_eval_list 1 end] {
108 if {$mod eq [getModuleFromEvalId $msgrecid]} {
109 set mod_evalid $msgrecid
110 break
111 }
112 }
113 if {![info exists mod_evalid]} {
114 return
115 }
116
117 # remove evaluation from old context
118 lrem old_context_eval_list $mod_evalid
119 lset ::g_moduleEval($evalid) $old_context_idx $old_context_eval_list
120
121 # insert evaluation at start of new context
122 set new_context_eval_list [linsert $new_context_eval_list 1 $mod_evalid]
123 # on Tcl 8.5, lset command cannot append a list
124 if {$new_context_idx eq {end+1}} {
125 lappend ::g_moduleEval($evalid) $new_context_eval_list
126 } else {
127 lset ::g_moduleEval($evalid) $new_context_idx $new_context_eval_list
128 }
129
130 # change context of hidden evaluation
131 set old_hidden_evalid $evalid:$old_context
132 if {[info exists ::g_moduleHiddenEval($old_hidden_evalid)]} {
133 if {$mod_evalid in $::g_moduleHiddenEval($old_hidden_evalid)} {
134 lrem ::g_moduleHiddenEval($old_hidden_evalid) $mod_evalid
135 set new_hidden_evalid $evalid:$new_context
136 lappend ::g_moduleHiddenEval($new_hidden_evalid) $mod_evalid
137 }
138 }
139 }
140
141 # get context of currently evaluated module
142 proc currentModuleEvalContext {} {
143 return [lindex $::g_moduleEvalAttempt([currentState modulenamevr]) end]
144 }
145
146 # record module evaluation attempt and corresponding context
147 proc registerModuleEvalAttempt {context mod mod_file} {
148 lappendNoDup ::g_moduleEvalAttempt($mod) $context
149 lappendNoDup ::g_moduleFileEvalAttempt($mod) $mod_file
150 }
151
152 proc unregisterModuleEvalAttempt {context mod mod_file} {
153 lrem ::g_moduleEvalAttempt($mod) $context
154 lrem ::g_moduleFileEvalAttempt($mod) $mod_file
155 }
156
157 # is at least one module passed as argument evaluated in passed context
158 proc isModuleEvaluated {context exclmod modulepath_list args} {
159 set ret 0
160 # look at all evaluated mod except excluded one (currently evaluated mod)
161 foreach evalmod [lsearch -all -inline -not [array names\
162 ::g_moduleEvalAttempt] $exclmod] {
163 # test evaluated module matches specified modulepaths
164 if {[llength $modulepath_list]} {
165 set eval_mod_file_match 0
166 foreach eval_mod_file $::g_moduleFileEvalAttempt($evalmod) {
167 if {[isModulefileInModulepathList $eval_mod_file\
168 $modulepath_list]} {
169 set eval_mod_file_match 1
170 break
171 }
172 }
173 if {!$eval_mod_file_match} {
174 continue
175 }
176 }
177 set evalmatch 0
178 # test arguments against all names of evaluated module (translate
179 # eventual modspec in evalmod into module names, in case module
180 # evaluation stopped prior module name setup)
181 # retrieve variants to pass them directly to modEq
182 set modvrlist [getVariantList $evalmod 0 0 1]
183 foreach mod [getAllModulesFromVersSpec $evalmod] {
184 foreach name $args {
185 # indicate module is loading to also compare against all alt names
186 if {[modEq $name $mod eqstart 1 1 1 $modvrlist]} {
187 set evalmatch 1
188 if {$context eq {any} || $context in\
189 $::g_moduleEvalAttempt($evalmod)} {
190 set ret 1
191 }
192 break
193 }
194 }
195 if {$evalmatch} {
196 break
197 }
198 }
199 if {$ret} {
200 break
201 }
202 }
203 return $ret
204 }
205
206 # was passed mod already evaluated for context and failed
207 proc isModuleEvalFailed {context mod} {
208 set ret 0
209 set evalid [topState evalid]
210 if {[info exists ::g_moduleFailedEval($evalid)]} {
211 foreach {curcon curmod} $::g_moduleFailedEval($evalid) {
212 if {$context eq $curcon && $mod eq $curmod} {
213 set ret 1
214 break
215 }
216 }
217 }
218 return $ret
219 }
220
221 # return list of currently loading modules in stack
222 proc getLoadingModuleList {} {
223 set modlist [list]
224 for {set i 0} {$i < [depthState modulename]} {incr i 1} {
225 if {[lindex [getState mode] $i] eq {load}} {
226 lappend modlist [lindex [getState modulename] $i]
227 }
228 }
229 return $modlist
230 }
231
232 # return list of currently loading modulefiles in stack
233 proc getLoadingModuleFileList {} {
234 set modlist [list]
235 for {set i 0} {$i < [depthState modulefile]} {incr i 1} {
236 if {[lindex [getState mode] $i] eq {load}} {
237 lappend modlist [lindex [getState modulefile] $i]
238 }
239 }
240 return $modlist
241 }
242
243 # return list of currently unloading modules in stack
244 proc getUnloadingModuleList {} {
245 set modlist [list]
246 for {set i 0} {$i < [depthState modulename]} {incr i 1} {
247 if {[lindex [getState mode] $i] eq {unload}} {
248 lappend modlist [lindex [getState modulename] $i]
249 }
250 }
251 return $modlist
252 }
253
254 # sort passed module list following both loaded and dependency orders
255 proc sortModulePerLoadedAndDepOrder {modlist {nporeq 0} {loading 0}} {
256 # sort per loaded order
257 set sortlist {}
258 if {[llength $modlist]} {
259 foreach lmmod [getEnvLoadedModulePropertyParsedList name] {
260 if {$lmmod in $modlist} {
261 lappend sortlist $lmmod
262 }
263 }
264 # also sort eventual loading modules if asked
265 if {$loading} {
266 foreach loadingmod [lreverse [getLoadingModuleList]] {
267 if {$loadingmod in $modlist} {
268 lappend sortlist $loadingmod
269 }
270 }
271 }
272 }
273
274 # then refine sort with dependencies between loaded modules: a dependent
275 # module should be placed prior the loaded module requiring it
276 set reqListVar [expr {$nporeq ? {::g_moduleNPODepend} :\
277 {::g_moduleDepend}}]
278 set i 0
279 set imax [llength $sortlist]
280 while {$i < $imax} {
281 set mod [lindex $sortlist $i]
282 set jmin $imax
283
284 ##nagelfar ignore #4 Suspicious variable name
285 if {[info exists ${reqListVar}($mod)]} {
286 # goes over all dependent modules to find the first one in the loaded
287 # order list located after requiring mod
288 foreach lmmodlist [set ${reqListVar}($mod)] {
289 foreach lmmod $lmmodlist {
290 set j [lsearch -exact $sortlist $lmmod]
291 if {$j > $i && $j < $jmin} {
292 set jmin $j
293 set jminmod $lmmod
294 }
295 }
296 }
297 }
298
299 # move first dependent module found after currently inspected mod right
300 # before it
301 if {$jmin != $imax} {
302 set sortlist [linsert [lreplace $sortlist $jmin $jmin] $i $jminmod]
303 # or go to next element in list if current element has not been changed
304 } else {
305 incr i
306 }
307 }
308
309 return $sortlist
310 }
311
312 # return list of loaded modules having an unmet requirement on passed mod
313 # and their recursive dependent
314 proc getUnmetDependentLoadedModuleList {modnamevr mod_file} {
315 set unmetdeplist {}
316 set depmodlist {}
317 set mod [getModuleNameAndVersFromVersSpec $modnamevr]
318 set vrlist [getVariantList $modnamevr 0 0 1]
319
320 # skip dependent analysis if mod has a conflict with a loaded module
321 set modconlist [getModuleLoadedConflict $mod]
322 if {![llength $modconlist]} {
323 foreach ummod [array names ::g_unmetDepHash] {
324 if {[modEq $ummod $mod eqstart 1 2 1 $vrlist]} {
325 foreach {depmod prereq_path_list} $::g_unmetDepHash($ummod) {
326 if {![isModulefileMatchSpecificPath $mod_file\
327 $prereq_path_list]} {
328 continue
329 }
330
331 lappend depmodlist $depmod
332 # temporarily remove prereq violation of depmod if mod
333 # load solves it (no other prereq is missing)
334 if {[info exists ::g_prereqViolation($depmod)]} {
335 foreach prereq $::g_prereqViolation($depmod) {
336 foreach modpre $prereq {
337 # also temporarily remove prereq violation for
338 # requirements loaded after dependent module
339 if {[modEq $modpre $mod eqstart 1 2 1 $vrlist] ||\
340 [is-loaded $modpre]} {
341 # backup original violation to restore it later
342 if {![info exists preunvioarr($depmod)]} {
343 set preunvioarr($depmod)\
344 $::g_prereqViolation($depmod)
345 }
346 # temporarily remove matching violation
347 lrem ::g_prereqViolation($depmod) $prereq
348 if {![llength $::g_prereqViolation($depmod)]} {
349 unset ::g_prereqViolation($depmod)
350 }
351 break
352 }
353 }
354 }
355 }
356 }
357 }
358 }
359 }
360
361 # select dependent if all its constraint are now satisfied (after removing
362 # eventual prereq violation toward mod)
363 foreach depmod $depmodlist {
364 if {[areModuleConstraintsSatisfied $depmod]} {
365 lappendNoDup unmetdeplist $depmod
366 }
367 }
368
369 # get dependent of dependent
370 set deplist [getDependentLoadedModuleList $unmetdeplist 0 0 0 0 1]
371
372 # restore temporarily lift prereq violation
373 if {[array exists preunvioarr]} {
374 foreach depmod [array names preunvioarr] {
375 set ::g_prereqViolation($depmod) $preunvioarr($depmod)
376 }
377 }
378
379 set sortlist [sortModulePerLoadedAndDepOrder [list {*}$unmetdeplist\
380 {*}$deplist]]
381 reportDebug "got '$sortlist'"
382 return $sortlist
383 }
384
385 # return list of loaded modules declaring a prereq on passed mod with
386 # distinction made with strong prereqs (no alternative loaded) or weak and
387 # also with prereq loaded after their dependent module
388 proc getDirectDependentList {mod {strong 0} {nporeq 0} {loading 0}\
389 {othmodlist {}}} {
390 set deplist {}
391
392 # include or not requirements loaded after their dependent
393 if {$nporeq} {
394 set depListVar ::g_dependNPOHash
395 set reqListVar ::g_moduleNPODepend
396 } else {
397 set depListVar ::g_dependHash
398 set reqListVar ::g_moduleDepend
399 }
400
401 ##nagelfar ignore #2 Suspicious variable name
402 if {[info exists ${depListVar}($mod)]} {
403 foreach depmod [set ${depListVar}($mod)] {
404 set add 1
405 # skip optional dependency if only looking for strong ones
406 # look at an additionally processed mod list to determine if all
407 # mods from a dependent list (composed of optional parts) are part
408 # of the search, which means mod is not optional but strong dependent
409 if {$strong && [llength $depmod] > 1} {
410 ##nagelfar ignore Suspicious variable name
411 foreach lmmodlist [set ${reqListVar}([lindex $depmod 0])] {
412 if {$mod in $lmmodlist} {
413 foreach lmmod $lmmodlist {
414 # other mod part of the opt list is not there so mod
415 # is considered optional
416 if {$lmmod ni $othmodlist} {
417 set add 0
418 break
419 }
420 }
421 break
422 }
423 }
424 }
425
426 if {$add} {
427 lappend deplist [lindex $depmod 0]
428 }
429 }
430 }
431
432 # take currently loading modules into account if asked
433 if {$loading} {
434 foreach loading_mod [getLoadingModuleList] {
435 foreach prereq_match [getLoadedModulePrereqListAndLoadedMatch\
436 $loading_mod] {
437 set lmprelist {}
438 set item_match_list [lassign $prereq_match prereq\
439 prereq_path_list]
440 foreach {prereq_mod_item loaded_mod_match ign} $item_match_list {
441 if {[string length $loaded_mod_match]} {
442 lappend lmprelist $loaded_mod_match
443 }
444 }
445 if {$mod in $lmprelist && (!$strong || [llength $lmprelist]\
446 == 1)} {
447 lappend deplist $loading_mod
448 break
449 }
450 }
451 }
452 }
453
454 return $deplist
455 }
456
457 # gets the list of all loaded modules which are dependent of passed modlist
458 # ordered by load position. strong argument controls whether only the active
459 # dependent modules should be returned or also those that are optional. direct
460 # argument controls if only dependent module directly requiring passed mods
461 # should be returned or its full dependent tree. nporeq argument tells if
462 # requirement loaded after their dependent should be returned. sat_constraint
463 # argument controls whether only the loaded module satisfying their constraint
464 # should be part or not of the resulting list. being_unload argument controls
465 # whether loaded modules in conflict with one or multiple modules from modlist
466 # should be added to the dependent list as these modules are currently being
467 # unloaded and these conflicting loaded modules should be refreshed.
468 proc getDependentLoadedModuleList {modlist {strong 1} {direct 1} {nporeq 0}\
469 {loading 1} {sat_constraint 0} {being_unload 0}} {
470 reportDebug "get loaded mod dependent of '$modlist' (strong=$strong,\
471 direct=$direct, nporeq=$nporeq, loading=$loading,\
472 sat_constraint=$sat_constraint, being_unload=$being_unload)"
473
474 set deplist {}
475 set fulllist $modlist
476 # look at consistent requirements for unloading modules
477 set unlonporeq [expr {$being_unload ? 0 : $nporeq}]
478 foreach mod $modlist {
479 # no duplicates or modules from query list
480 lappendNoDup fulllist {*}[getDirectDependentList $mod $strong\
481 $unlonporeq $loading $fulllist]
482 }
483
484 if {$being_unload} {
485 # invite modules in violation with mods to be part of the dependent list
486 # with their own dependent modules as mod is being unloaded. Achieve so
487 # by faking that conflict violation is gone
488 foreach mod $modlist {
489 set modconlist [getModuleLoadedConflict $mod]
490 if {[llength $modconlist]} {
491 unsetModuleConflictViolation $mod
492 set conunvioarr($mod) $modconlist
493 lappendNoDup fulllist {*}$modconlist
494 }
495 }
496 }
497 set unloadingmodlist [getUnloadingModuleList]
498 for {set i [llength $modlist]} {$i < [llength $fulllist]} {incr i 1} {
499 set depmod [lindex $fulllist $i]
500
501 # skip already added mod or mod violating constraints if asked
502 if {!$sat_constraint || [areModuleConstraintsSatisfied $depmod\
503 $nporeq]} {
504 # get dependent mod of dep mod when looking at full dep tree
505 if {!$direct} {
506 lappendNoDup fulllist {*}[getDirectDependentList $depmod $strong\
507 $nporeq 0 $fulllist]
508 }
509 # avoid module currently unloading from result list
510 if {$depmod ni $unloadingmodlist} {
511 lappend deplist $depmod
512 }
513 }
514 }
515
516 # restore conflict violation if any
517 if {[array exists conunvioarr]} {
518 foreach conunvio [array names conunvioarr] {
519 setModuleConflictViolation $conunvio $conunvioarr($conunvio)
520 }
521 }
522
523 # sort complete result list to match both loaded and dependency orders
524 set sortlist [sortModulePerLoadedAndDepOrder $deplist $nporeq $loading]
525 reportDebug "got '$sortlist'"
526 return $sortlist
527 }
528
529 # may given module be automatically unloaded
530 # unmodlist: pass a list of modules that are going to be unloaded
531 proc isModuleUnloadable {mod {unmodlist {}}} {
532 set ret 1
533
534 # module may be unloaded if it has been automatically loaded, it is not
535 # tagged keep-loaded and it is not sticky.
536 if {[isModuleTagged $mod loaded 1] || [isModuleTagged $mod keep-loaded\
537 1] || [isModuleSticky $mod]} {
538 set ret 0
539 # there should no one requiring module or these dependent should be part of
540 # the unloaded/unloading list
541 } else {
542 foreach depmod [getDirectDependentList $mod] {
543 if {$depmod ni $unmodlist} {
544 set ret 0
545 break
546 }
547 }
548 }
549
550 return $ret
551 }
552
553 # gets the list of all loaded modules which are required by passed mod_list
554 # ordered by load position. a list of modules to exclude may be provided
555 # (usually to skip requirements that will be unloaded)
556 proc getRequiredLoadedModuleList {mod_list {excluded_mod_list {}}} {
557 # search over all list of loaded modules, starting with passed module
558 # list, then adding in turns their requirements
559 set full_list $mod_list
560 for {set i 0} {$i < [llength $full_list]} {incr i 1} {
561 # gets the list of loaded modules which are required by depmod
562 foreach req_mod_list $::g_moduleDepend([lindex $full_list $i]) {
563 foreach req_mod $req_mod_list {
564 if {$req_mod ni $excluded_mod_list} {
565 lappendNoDup full_list $req_mod
566 }
567 }
568 }
569 }
570
571 # sort complete result list to match both loaded and dependency orders
572 set sort_list [sortModulePerLoadedAndDepOrder [lrange $full_list [llength\
573 $mod_list] end]]
574 reportDebug "got '$sort_list'"
575 return $sort_list
576 }
577
578 # how many settings bundle are currently saved
579 proc getSavedSettingsStackDepth {} {
580 return [llength $::g_SAVE_var]
581 }
582
583 # manage settings to save as a stack to have a separate set of settings
584 # for each module loaded or unloaded in order to be able to restore the
585 # correct set in case of failure
586 proc pushSettings {} {
587 foreach var {g_clearedEnvVars g_Aliases g_stateEnvVars g_stateAliases\
588 g_stateFunctions g_Functions g_stateCompletes g_Completes\
589 g_newXResources g_delXResources g_loadedModules g_loadedModuleFiles\
590 g_loadedModuleVariant g_loadedModuleConflict g_loadedModulePrereq\
591 g_loadedModulesRefresh g_loadedModuleAltname g_loadedModuleAutoAltname\
592 g_loadedModuleAliasAltname g_moduleDepend g_dependHash\
593 g_moduleNPODepend g_dependNPOHash g_prereqViolation\
594 g_prereqNPOViolation g_conflictViolation g_moduleUnmetDep\
595 g_unmetDepHash g_moduleEval g_moduleHiddenEval g_scanModuleVariant\
596 g_savedLoReqOfReloadMod g_savedLoReqOfUnloadMod g_savedPropsOfReloadMod\
597 g_loadedModulePrereqPath g_tagHash g_loadedByModroot g_modrootByLoaded\
598 g_loadedModulePosition g_modrootByLoadedConflict\
599 g_loadedConflictByModroot} {
600 ##nagelfar ignore Suspicious variable name
601 lappend save_array $var [array get ::$var]
602 }
603 lappend ::g_SAVE_array $save_array
604 lappend ::g_SAVE_env [array get ::env]
605
606 # save non-array variable and indication if it was set
607 foreach var {g_changeDir g_stdoutPuts g_prestdoutPuts g_return_text\
608 g_uReqUnFromDepReList g_depReList} {
609 ##nagelfar ignore #2 Badly formed if statement
610 lappend save_var $var {*}[if {[info exists ::$var]} {list 1 [set\
611 ::$var]} {list 0 {}}]
612 }
613 lappend ::g_SAVE_var $save_var
614 reportDebug "settings saved (#[getSavedSettingsStackDepth])"
615 }
616
617 proc popSettings {} {
618 foreach var {::g_SAVE_array ::g_SAVE_env ::g_SAVE_var} {
619 ##nagelfar vartype var varName
620 lpop $var
621 }
622 reportDebug "previously saved settings flushed\
623 (#[getSavedSettingsStackDepth])"
624 }
625
626 proc restoreSettings {} {
627 foreach {var val} [lindex $::g_SAVE_array end] {
628 ##nagelfar ignore #2 Suspicious variable name
629 array unset ::$var
630 array set ::$var $val
631 }
632 # specific restore mechanism for ::env as unsetting this array will make
633 # Tcl stop monitoring env accesses and not update env variables anymore
634 array set saved_env [lindex $::g_SAVE_env end]
635 array set ::env [array get saved_env]
636 set saved_env_var_list [array names saved_env]
637 foreach var [array names ::env] {
638 if {$var ni $saved_env_var_list} {
639 unset ::env($var)
640 }
641 }
642 # restore non-array variable if it was set
643 foreach {var is_defined val} [lindex $::g_SAVE_var end] {
644 ##nagelfar ignore #4 Suspicious variable name
645 if {$is_defined} {
646 set ::$var $val
647 } else {
648 unset -nocomplain ::$var
649 }
650 }
651 reportDebug "previously saved settings restored\
652 (#[getSavedSettingsStackDepth])"
653 }
654
655 # clear environment change related variables to undo modifications produced by
656 # evaluated modulefile(s)
657 proc flushEnvSettings {} {
658 foreach var {g_Aliases g_stateEnvVars g_stateAliases g_stateFunctions\
659 g_Functions g_stateCompletes g_Completes g_newXResources\
660 g_delXResources g_changeDir g_stdoutPuts g_prestdoutPuts\
661 g_return_text} {
662 ##nagelfar ignore #2 Suspicious variable name
663 if {[info exists ::$var]} {
664 unset ::$var
665 }
666 }
667 }
668
669 # load modules passed as args designated as requirement
670 proc loadRequirementModuleList {tryload optional tag_list modulepath_list\
671 args} {
672 set ret 0
673 set prereqloaded 0
674
675 # return cached result if any
676 set searchid $tryload:$optional:$tag_list:$modulepath_list:$args
677 if {[info exists ::g_loadRequirementModuleListMemCache($searchid)]} {
678 return $::g_loadRequirementModuleListMemCache($searchid)
679 }
680
681 # calling procedure must have already parsed module specification in args
682 set loadedmod_list {}
683 foreach mod $args {
684 # get all loaded or loading mod in args list
685 if {[set loadedmod [getLoadedMatchingName $mod returnfirst 0 {}\
686 $modulepath_list]] ne {} || [set loadedmod [getLoadedMatchingName\
687 $mod returnfirst 1 {} $modulepath_list]] ne {}} {
688 lappend loadedmod_list $loadedmod
689 }
690 }
691
692 if {![llength $loadedmod_list]} {
693 set imax [llength $args]
694 # if prereq list specified, try to load first then
695 # try next if load of first module not successful
696 for {set i 0} {$i<$imax && !$prereqloaded} {incr i 1} {
697 set arg [lindex $args $i]
698
699 # hold output from current evaluation to catch 'module not found'
700 # message that occurs outside of sub evaluation
701 lappendState reportholdrecid [currentState msgrecordid]
702 # hold output of each sub evaluation until they are all done to drop
703 # those that failed if one succeed or if optional
704 set curholdid load-$i-$arg
705 lappendState reportholdid $curholdid
706 if {[catch {set retlo [cmdModuleLoad reqlo 0 $tryload 0 $tag_list\
707 $modulepath_list $arg]} errorMsg]} {
708 # if an error is raised, release output and rethrow the error
709 # (could be raised if no modulepath defined for instance)
710 lpopState reportholdid
711 lpopState reportholdrecid
712 lappend holdidlist $curholdid report
713 releaseHeldReport {*}$holdidlist
714 knerror $errorMsg
715 }
716 # update return value if an issue occurred in cmdModuleLoad
717 if {$retlo != 0} {
718 set ret $retlo
719 }
720 lpopState reportholdid
721 lpopState reportholdrecid
722
723 if {[string length [getLoadedMatchingName $arg returnfirst 0 {}\
724 $modulepath_list]]} {
725 set prereqloaded 1
726 # set previous reports to be dropped as this one succeed
727 if {[info exists holdidlist]} {
728 foreach {holdid action} $holdidlist {
729 lappend newholdidlist $holdid drop
730 }
731 set holdidlist $newholdidlist
732 }
733 }
734 # drop report if not loaded and optional
735 set action [expr {$prereqloaded || !$optional ? {report} : {drop}}]
736 lappend holdidlist $curholdid $action
737 }
738 # output held messages
739 releaseHeldReport {*}$holdidlist
740 } else {
741 set prereqloaded 1
742 # apply missing tag to all loaded module found
743 cmdModuleTag 0 0 $tag_list {*}$loadedmod_list
744 }
745
746 set ::g_loadRequirementModuleListMemCache($searchid) [list $ret\
747 $prereqloaded]
748 }
749
750 # save reloading module properties before they vanish with unload phase
751 proc savePropsOfReloadingModule {mod} {
752 set is_user_asked [isModuleTagged $mod loaded 1]
753 set vr_list [getVariantList $mod 1 2]
754 set tag_list [getTagList $mod]
755 set extra_tag_list [getExtraTagList $mod]
756 set conflict_list [getLoadedConflict $mod]
757 set altname_list [getLoadedAltname $mod]
758 set prereq_list [getLoadedPrereq $mod]
759 set prereq_path_list [getLoadedPrereqPath $mod]
760 set modpath [getModulepathFromLoadedOrLoadingModule $mod]
761 set ::g_savedPropsOfReloadMod($mod) [list $is_user_asked $vr_list\
762 $tag_list $extra_tag_list $conflict_list $altname_list $prereq_list\
763 $prereq_path_list $modpath]
764 }
765
766 proc getSavedPropsOfReloadingModule {mod} {
767 return $::g_savedPropsOfReloadMod($mod)
768 }
769
770 # unload phase of a list of modules reload process
771 proc reloadModuleListUnloadPhase {mod_list {err_msg_tpl {}} {context\
772 unload}} {
773 # unload one by one to ensure same behavior whatever auto_handling state
774 foreach mod [lreverse $mod_list] {
775 if {[reloadModuleUnloadPhase $mod $err_msg_tpl $context]} {
776 lrem mod_list $mod
777 }
778 }
779 return $mod_list
780 }
781
782 proc reloadModuleUnloadPhase {mod {err_msg_tpl {}} {context unload}} {
783 # record hint that mod will be reloaded (useful in case mod is sticky)
784 lappendState reloading_sticky $mod
785 lappendState reloading_supersticky $mod
786 savePropsOfReloadingModule $mod
787 # avoid failing module on unload phase
788 # if force state is enabled, cmdModuleUnload returns 0
789 if {[set ret [cmdModuleUnload $context match 0 s 0 $mod]]} {
790 # no process stop if ongoing reload command in continue behavior
791 if {![isStateEqual commandname reload] || [commandAbortOnError]} {
792 set err_msg [string map [list _MOD_ [getModuleDesignation loaded\
793 $mod]] $err_msg_tpl]
794 lpopState reloading_sticky
795 lpopState reloading_supersticky
796 knerror $err_msg
797 }
798 }
799 lpopState reloading_sticky
800 lpopState reloading_supersticky
801 return $ret
802 }
803
804 # load phase of a list of modules reload process
805 proc reloadModuleListLoadPhase {mod_list {errmsgtpl {}} {context load}} {
806 if {![llength $mod_list]} {
807 return
808 }
809 # loads are made with auto handling mode disabled to avoid disturbances
810 # from a missing prereq automatically reloaded, so these module loads may
811 # fail as prereq may not be satisfied anymore
812 setConf auto_handling 0
813 foreach mod $mod_list {
814 lassign [getSavedPropsOfReloadingModule $mod] is_user_asked vr_list\
815 tag_list extra_tag_list conflict_list altname_list prereq_list\
816 prereq_path_list modpath
817 # if an auto set default was excluded, module spec need parsing
818 lassign [parseModuleSpecification 0 0 0 0 $mod {*}$vr_list] modnamevr
819
820 set is_sticky [isModuleStickyFromTagList {*}$tag_list\
821 {*}$extra_tag_list]
822
823 # do not try to reload DepRe module if requirements are not satisfied
824 # unless if sticky
825 if {$context eq {depre} && ![isModuleLoadable $mod $modnamevr\
826 $conflict_list $altname_list $prereq_list $prereq_path_list\
827 $modpath] && !$is_sticky} {
828 continue
829 }
830
831 # reload module with user asked property and extra tags preserved
832 if {[cmdModuleLoad $context $is_user_asked 0 0 $extra_tag_list {}\
833 $modnamevr]} {
834 set errMsg [string map [list _MOD_ [getModuleDesignation spec\
835 $modnamevr]] $errmsgtpl]
836 if {$is_sticky} {
837 set errMsg [string map {dependent {sticky dependent}} $errMsg]
838 }
839 # unless sticky no process stop if forced, or ongoing reload or
840 # switch cmd in continue behavior
841 if {!$is_sticky && ([getState force] || (([isStateEqual commandname\
842 reload] || [isStateEqual commandname switch]) &&\
843 ![commandAbortOnError]))} {
844 # no msg for reload sub-cmd which provides an empty msg template
845 reportWarning $errMsg
846 # stop if one load fails unless force mode enabled
847 } else {
848 knerror $errMsg
849 }
850 }
851 }
852 setConf auto_handling 1
853 }
854
855 proc isModuleLoadable {mod mod_vr conflict_list altname_list prereq_list\
856 prereq_path_list modpath} {
857 # must set mod alternative names to correctly fetch its conflicts
858 setLoadedAltname $mod {*}$altname_list
859 setLoadedConflict $mod {*}$conflict_list
860 set is_conflicting [llength [getModuleLoadedConflict $mod]]
861 unsetLoadedConflict $mod
862 unsetLoadedAltname $mod
863
864 if {$is_conflicting} {
865 return 0
866 }
867
868 array set prereq_path_arr $prereq_path_list
869 foreach prereq_arg $prereq_list {
870 if {[info exists prereq_path_arr($prereq_arg)]} {
871 set prereq_arg_path $prereq_path_arr($prereq_arg)
872 } else {
873 set prereq_arg_path {}
874 }
875 set is_requirement_loaded 0
876 foreach req_mod $prereq_arg {
877 # is requirement loaded, loading or optional
878 if {[string length [getLoadedMatchingName $req_mod returnfirst 0 {}\
879 $prereq_arg_path]] || [string length [getLoadedMatchingName\
880 $req_mod returnfirst 1 {} $prereq_arg_path]] || $req_mod eq\
881 $mod} {
882 set is_requirement_loaded 1
883 break
884 }
885 }
886 if {!$is_requirement_loaded} {
887 return 0
888 }
889 }
890
891 # module is loadable if its modulepath is still enabled or if it can be
892 # found in another modulepath (variant availability is currently not
893 # checked by is-avail procedure)
894 if {![is-used $modpath] && ![is-avail $mod_vr]} {
895 return 0
896 }
897
898 return 1
899 }
900
901 proc isModuleStickyFromTagList {args} {
902 return [expr {{super-sticky} in $args || ({sticky} in $args && ![getState\
903 force])}]
904 }
905
906 # test if loaded module 'mod' is sticky and if stickiness definition applies
907 # to one of the reloading module
908 proc isStickinessReloading {mod reloading_mod_list {tag sticky}} {
909 set res 0
910 set mod_name_vers [getModuleNameAndVersFromVersSpec $mod]
911 if {[isModuleTagged $mod_name_vers $tag 1]} {
912 # sticky rules (module-tag definitions) applying to loaded module have
913 # been evaluated when charging loaded environment
914 set full_path_mod [getModulefileFromLoadedModule $mod_name_vers]
915 set tag_rule_list [getModuleTagRuleList $mod $full_path_mod $tag]
916
917 # no rule found (in env), means sticky applies to exact same module
918 if {![llength $tag_rule_list]} {
919 set mod_should_reload 1
920 } else {
921 set mod_should_reload 0
922 # if tag specifically applies to fully qualified module or module
923 # name and version, exact same module should be found in reload list
924 foreach tag_rule $tag_rule_list {
925 if {[modEq $tag_rule $mod equal 1 0 1] || [modEq $tag_rule\
926 $full_path_mod equal 1 0 1]} {
927 set mod_should_reload 1
928 break
929 }
930 }
931 }
932
933 if {$mod_should_reload} {
934 set res [expr {$mod in $reloading_mod_list}]
935 } else {
936 # check if a reloading module satisfies each sticky rules
937 foreach tag_rule $tag_rule_list {
938 set res 0
939 foreach reloading_mod $reloading_mod_list {
940 if {[modEq $tag_rule $reloading_mod eqstart 1 0 1]} {
941 set res 1
942 break
943 }
944 }
945 if {!$res} {
946 break
947 }
948 }
949 }
950 reportDebug "stickiness ($tag), applying to $tag_rule_list, is\
951 reloading=$res"
952 }
953
954 return $res
955 }
956
957 proc isModuleSticky {mod} {
958 return [expr {[isModuleTagged $mod super-sticky 1] || ([isModuleTagged\
959 $mod sticky 1] && ![getState force])}]
960 }
961
962 proc getModuleStickyDependentTag {mod} {
963 set sticky_tag {}
964 # look at dependents whatever their load order to also protect a
965 # requirement loaded after its sticky dependent
966 foreach dep_mod [getDependentLoadedModuleList [list $mod] 1 0 1 0] {
967 if {[isModuleTagged $dep_mod super-sticky 1]} {
968 set sticky_tag super-sticky
969 break
970 } elseif {[isModuleSticky $dep_mod]} {
971 set sticky_tag sticky
972 }
973 }
974 return $sticky_tag
975 }
976
977 proc saveLoadedReqOfUnloadingModule {unload_mod} {
978 # fetch requirements of unloading module
979 set ::g_savedLoReqOfUnloadMod($unload_mod)\
980 [getRequiredLoadedModuleList [list $unload_mod]]
981 }
982
983 proc getLoadedReqOfUnloadingModuleList {} {
984 set unloading_req_mod_list {}
985 foreach unloading_mod [array names ::g_savedLoReqOfUnloadMod] {
986 lappendNoDup unloading_req_mod_list\
987 {*}$::g_savedLoReqOfUnloadMod($unloading_mod)
988 }
989 return [sortModulePerLoadedAndDepOrder $unloading_req_mod_list]
990 }
991
992 proc clearLoadedReqOfUnloadingModuleList {} {
993 array unset ::g_savedLoReqOfUnloadMod
994 }
995
996 proc saveLoadedReqOfReloadingModuleList {reload_mod_list unload_mod_list} {
997 # fetch requirements of reloading modules skipping unloading ones
998 foreach reload_mod $reload_mod_list {
999 set ::g_savedLoReqOfReloadMod($reload_mod) [getRequiredLoadedModuleList\
1000 [list $reload_mod] $unload_mod_list]
1001 }
1002 }
1003
1004 proc getLoadedReqOfReloadingModuleList {} {
1005 set reloading_req_mod_list {}
1006 foreach reloading_mod [array names ::g_savedLoReqOfReloadMod] {
1007 lappendNoDup reloading_req_mod_list\
1008 {*}$::g_savedLoReqOfReloadMod($reloading_mod)
1009 }
1010 return [sortModulePerLoadedAndDepOrder $reloading_req_mod_list]
1011 }
1012
1013 proc unsetLoadedReqOfReloadingModule {mod} {
1014 unset -nocomplain ::g_savedLoReqOfReloadMod($mod)
1015 }
1016
1017 proc clearLoadedReqOfReloadingModuleList {} {
1018 array unset ::g_savedLoReqOfReloadMod
1019 }
1020
1021 proc identityUReqUnFromDepRe {depre_list unload_mod_list} {
1022 foreach depre [lreverse $depre_list] {
1023 if {[isModuleUnloadable $depre $unload_mod_list]} {
1024 lappend unload_mod_list $depre
1025 lappend ::g_uReqUnFromDepReList $depre
1026 }
1027 }
1028 }
1029
1030 proc clearUReqUnFromDepReList {} {
1031 unset -nocomplain ::g_uReqUnFromDepReList
1032 }
1033
1034 proc getIdentifiedUReqUnFromDepRe {} {
1035 if {[info exists ::g_uReqUnFromDepReList]} {
1036 return $::g_uReqUnFromDepReList
1037 }
1038 }
1039
1040 proc removeUReqUnFromDepReAndConvertEval {} {
1041 set depre_list [getDepReList]
1042 set urequn_from_depre_list [getIdentifiedUReqUnFromDepRe]
1043 lassign [getDiffBetweenList $depre_list $urequn_from_depre_list] depre_list
1044 setDepReList $depre_list
1045 foreach urequn_from_depre [lreverse $urequn_from_depre_list] {
1046 changeContextOfModuleEval $urequn_from_depre depun urequn
1047 }
1048 }
1049
1050 proc getUReqUnModuleList {} {
1051 set unloadable_mod_list {}
1052
1053 # add DepRe modules that are UReqUn modules and unset their requirements
1054 # from reloading list (to check them for potential UReqUn modules)
1055 foreach urequn_from_depre [getIdentifiedUReqUnFromDepRe] {
1056 lappend unloadable_mod_list $urequn_from_depre
1057 unsetLoadedReqOfReloadingModule $urequn_from_depre
1058 }
1059
1060 set reloading_req_mod_list [getLoadedReqOfReloadingModuleList]
1061 # useless requirement unload modules are unloadable req of unloaded mods
1062 # treat lastly loaded module first to build unloadable module list
1063 foreach unloading_req_mod [lreverse [getLoadedReqOfUnloadingModuleList]] {
1064 if {$unloading_req_mod ni $reloading_req_mod_list &&\
1065 [isModuleUnloadable $unloading_req_mod $unloadable_mod_list]} {
1066 lappend unloadable_mod_list $unloading_req_mod
1067 }
1068 }
1069 # return result in loaded order
1070 return [lreverse $unloadable_mod_list]
1071 }
1072
1073 proc unloadUReqUnModules {} {
1074 set urequn_list [getUReqUnModuleList]
1075 reportDebug "urequn mod list is '$urequn_list'"
1076
1077 if {[llength $urequn_list]} {
1078 # DepRe: Dependent to Reload (modules optionally dependent or in
1079 # conflict with modname, DepUn or UReqUn modules + modules dependent of
1080 # a module part of this DepRe batch)
1081 set urequn_depre_list [getDependentLoadedModuleList $urequn_list 0 0 1\
1082 0 1 1]
1083 set urequn_depre_list [reloadModuleListUnloadPhase $urequn_depre_list\
1084 {Unload of dependent _MOD_ failed} depre_un]
1085 lprependDepReList $urequn_depre_list
1086
1087 set urequn_list [lreverse $urequn_list]
1088 for {set i 0} {$i < [llength $urequn_list]} {incr i 1} {
1089 set unmod [lindex $urequn_list $i]
1090 if {[cmdModuleUnload urequn match 0 s 0 $unmod]} {
1091 # main unload process continues, but the UReqUn modules that are
1092 # required by unmod (whose unload failed) are withdrawn from
1093 # UReqUn module list
1094 lassign [getDiffBetweenList $urequn_list\
1095 [getRequiredLoadedModuleList [list $unmod]]] urequn_list
1096 }
1097 }
1098 }
1099 }
1100
1101 proc getDepUnModuleList {mod} {
1102 set depun_npo_list [getDependentLoadedModuleList [list $mod] 1 0 1 0]
1103 set depun_list [getDependentLoadedModuleList [list $mod] 1 0 0 0]
1104 # look at both regular dependencies or No Particular Order dependencies:
1105 # use NPO result if situation can be healed with NPO dependencies, which
1106 # will be part of DepRe list to restore the correct loading order for them
1107 if {[llength $depun_npo_list] <= [llength $depun_list]} {
1108 set depun_list $depun_npo_list
1109 }
1110 return $depun_list
1111 }
1112
1113 proc clearDepReList {} {
1114 unset -nocomplain ::g_depReList
1115 }
1116
1117 proc getDepReList {} {
1118 if {[info exists ::g_depReList]} {
1119 return $::g_depReList
1120 }
1121 }
1122
1123 proc setDepReList {mod_list} {
1124 set ::g_depReList $mod_list
1125 }
1126
1127 proc lprependDepReList {mod_list} {
1128 lprepend ::g_depReList {*}$mod_list
1129 }
1130
1131 proc unloadDepUnDepReModules {unload_mod_list reload_mod_list} {
1132 set err_msg_tpl {Unload of dependent _MOD_ failed}
1133 set unload_mod_list [sortModulePerLoadedAndDepOrder [list\
1134 {*}$unload_mod_list {*}$reload_mod_list]]
1135 lprependDepReList $reload_mod_list
1136
1137 foreach unload_mod [lreverse $unload_mod_list] {
1138 if {$unload_mod in $reload_mod_list} {
1139 # an error is raised if an unload eval fails
1140 reloadModuleUnloadPhase $unload_mod $err_msg_tpl depre_un
1141 } else {
1142 if {[cmdModuleUnload depun match 0 s 0 $unload_mod]} {
1143 # stop if one unload fails unless force mode enabled
1144 set err_msg [string map [list _MOD_ [getModuleDesignation loaded\
1145 $unload_mod]] $err_msg_tpl]
1146 knerrorOrWarningIfForced $err_msg
1147 }
1148 }
1149 }
1150 }
1151
1152 proc reloadDepReModules {} {
1153 set err_msg_tpl {Reload of dependent _MOD_ failed}
1154 set depre_list [getDepReList]
1155 reloadModuleListLoadPhase $depre_list $err_msg_tpl depre
1156 }
1157
1158 # Fail unload attempt if module is sticky, unless if forced or reloading
1159 # Also fail unload if mod is super-sticky even if forced, unless reloading
1160 proc failOrSkipUnloadIfSticky {modname modfile} {
1161 # when loaded, tags applies to mod name and version (not with variant)
1162 set is_supersticky_not_reloading [expr {[isModuleTagged $modname\
1163 super-sticky 1 $modfile] && [currentState reloading_supersticky] ne\
1164 $modname}]
1165 set is_sticky_not_reloading [expr {[isModuleTagged $modname sticky 1\
1166 $modfile] && [currentState reloading_sticky] ne $modname &&\
1167 [currentState unloading_sticky] ne $modname}]
1168 set sticky_purge [expr {[getState commandname] eq {purge} ? [getConf\
1169 sticky_purge] : {}}]
1170
1171 if {$is_supersticky_not_reloading || ($is_sticky_not_reloading &&\
1172 ![getState force])} {
1173 set msg [getStickyUnloadMsg [expr {$is_supersticky_not_reloading ?\
1174 {super-sticky} : {sticky}}]]
1175 # no message if sticky_purge is set to silent
1176 switch -- $sticky_purge {
1177 error - {} {knerror $msg}
1178 warning {reportWarning $msg}
1179 }
1180
1181 # skip unload without raising error
1182 return 1
1183 }
1184
1185 # on purge, module required by a retained sticky module is kept loaded,
1186 # which prevails over the forced unload of a sticky module
1187 if {$sticky_purge ne {} && [failOrSkipUnloadIfRequiredBySticky\
1188 $modname]} {
1189 return 1
1190 }
1191
1192 if {$is_sticky_not_reloading && [getState force]} {
1193 reportWarning [getStickyForcedUnloadMsg]
1194 }
1195
1196 return 0
1197 }
1198
1199 proc failOrSkipUnloadIfRequiredBySticky {mod} {
1200 set sticky_tag [getModuleStickyDependentTag $mod]
1201 if {$sticky_tag eq {}} {
1202 return 0
1203 }
1204
1205 set msg [getStickyReqUnloadMsg $sticky_tag]
1206 switch -- [getConf sticky_purge] {
1207 error {knerror $msg}
1208 warning {reportWarning $msg}
1209 }
1210
1211 return 1
1212 }
1213
1214 # ;;; Local Variables:
1215 # ;;; Mode: tcl-mode
1216 # ;;; tcl-indent-level: 3
1217 # ;;; tcl-continued-indent-level: 3
1218 # ;;; indent-tabs-mode: nil
1219 # ;;; End:
1220 # vim:set tabstop=3 shiftwidth=3 expandtab autoindent: