Changeset 97796


Ignore:
Timestamp:
Sep 15, 2012, 10:37:14 PM (7 years ago)
Author:
cal@…
Message:

active_variants: add debug code, add convenience wrapper require_active_variants

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/dports/_resources/port1.0/group/active_variants-1.0.tcl

    r97795 r97796  
    5555#    (default is empty list, see description of $required for values that can be
    5656#    interpreted as list by Tcl)
     57#
     58#
     59# In situations where you know that a version of the port is active (e.g., when
     60# checking in pre-configure of a port and the checked port is a dependency),
     61# this can be simplified to:
     62# if {[active_variants $name $required $forbidden]} {
     63#   # code to be run if $name is active with all from $required and none from
     64#   # $forbidden
     65# } else {
     66#   # code to be run if $name is active, but either not with all variants in
     67#   # $required or any variant in $forbidden
     68# }
     69#
     70# If all you want to do is bail out when the condition isn't fulfilled, there's
     71# a convience wrapper available. If the condition isn't met it will print an
     72# error message and exit. This will also error out, if the port $name isn't
     73# active, so you should probably not be using this before configure phase.
     74#
     75# require_active_variants $name $required $forbidden
     76#
    5777
    5878proc active_variants {name required {forbidden {}}} {
     
    85105        # we're interested in the field at offset 3.
    86106        set variants [lindex $installed 3]
     107        ui_debug "$name is installed with the following variants: $variants"
     108        ui_debug "  required: $required, forbidden: $forbidden"
    87109
    88110        # split by "+" into the separate variant names
     
    92114        foreach required_variant $required {
    93115                if {![_variant_in_variant_list $required_variant $variant_list]} {
     116                        ui_debug "  rejected, because required variant $required_variant is missing"
    94117                        return 0
    95118                }
     
    99122        foreach forbidden_variant $forbidden {
    100123                if {[_variant_in_variant_list $forbidden_variant $variant_list]} {
     124                        ui_debug "  rejected, because forbidden variant $forbidden_variant is present"
    101125                        return 0
    102126                }
     127        }
     128
     129        ui_debug "  accepted"
     130        return 1
     131}
     132
     133proc require_active_variants {name required {forbidden {}}} {
     134        if {[catch {set result [active_variants $name $required $forbidden]}] != 0} {
     135                error "$name is required, but not active."
     136        }
     137        if {!$result} {
     138                set str_required ""
     139                if {[llength $required] > 0} {
     140                        set str_required "with +[join $required +]"
     141                }
     142                set str_forbidden ""
     143                if {[llength $forbidden] > 0} {
     144                        set str_forbidden "without +[join $forbidden +]"
     145                }
     146                set str_combine ""
     147                if {$str_required != "" && $str_forbidden != ""} {
     148                        set str_combine " and "
     149                }
     150                error "$name must be installed ${str_required}${str_combine}${str_forbidden}."
    103151        }
    104152}
Note: See TracChangeset for help on using the changeset viewer.