Opened 7 years ago

Last modified 7 years ago

#54366 new enhancement

2 variant convenience procedures

Reported by: RJVB (René Bertin) Owned by:
Priority: Normal Milestone:
Component: base Version:
Keywords: Cc:
Port:

Description

Here are 2 convenience procedures for working with (mutually exclusive) variants which I think might be useful to include in "base". They're very new, so constructive feedback is welcome either way:

proc which_variant {args} {
    set vlist {}
    foreach v [join ${args}] {
        if {[variant_isset ${v}]} {
            set vlist [lappend vlist ${v}]
        }
    }
    return ${vlist}
}

proc switch_variant {args} {
    set code [lindex $args end]
    set variant [which_variant [lrange ${args} 0 end-1]]
    switch ${variant} ${code}
}

Example use:

    variant mariadb55 conflicts mysql56 mysql57 description {use MariaDB v5.5} {}
    variant mysql56 conflicts mariadb55 mysql57 description {use MySQL v5.6} {}
    variant mysql57 conflicts mariadb55 mysql56 description {use MySQL v5.7} {}
    # handle the depspec and set a default variant if none is requested
    switch_variant mysql56 mysql57 {
        "mysql56" -
        "mysql57" {
            depends_lib-append port:${variant}
        }
        default {
            default_variants +mariadb55
            depends_lib-append port:mariadb55
        }
    }

Or

    variant mariadb55 conflicts mysql56 mysql57 description {use MariaDB v5.5} {}
    variant mysql56 conflicts mariadb55 mysql57 description {use MySQL v5.6} {}
    variant mysql57 conflicts mariadb55 mysql56 description {use MySQL v5.7} {}
    switch_variant mysql56 mysql57 {
        "mysql56" -
        "mysql57" {
            global qt5_dependency
            require_active_variants ${qt5_dependency} ${variant}
        }
        default {
            global qt5_dependency
            default_variants +mariadb55
            require_active_variants ${qt5_dependency} mariadb55
        }
    }

The 2nd example probably justifies the procedures better, probably, because I don't think the code in question can reliably be put directly in a variant foo ... { code } expression but needs to be evaluated "inline" during regular Portfile execution. It would be nice if there were a way to execute the switch statement in the uplevel, or if that's not feasible, a way to inject a list of global variable declarations (switch_variant [-global list] variants codeblock?)

Change History (1)

comment:1 Changed 7 years ago by RJVB (René Bertin)

Oops, that must be

    switch_variant mysql56 mysql57 {
        "mysql56" -
        "mysql57" {
            depends_lib-append port:${variant}
        }
        default {
            default_variants +mariadb55
            depends_lib-append port:mariadb
        }
    }

and if you prefer putting the depspecs in the variant declarations and only take care of setting a default variant with a simple expression:

switch_variant var1 var2 ... varN {
    "" {
        # empty variant means none is requested by the user, specify a suitable default
        default_variants +var?
    }
}
Last edited 7 years ago by RJVB (René Bertin) (previous) (diff)
Note: See TracTickets for help on using tickets.