source: trunk/base/src/port1.0/portutil.tcl @ 136619

Last change on this file since 136619 was 136619, checked in by raimue@…, 4 years ago

base: reject variant names with invalid characters

https://lists.macosforge.org/pipermail/macports-dev/2015-May/030581.html

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 115.0 KB
Line 
1# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4
2# $Id: portutil.tcl 136619 2015-05-23 14:20:06Z raimue@macports.org $
3#
4# Copyright (c) 2002-2003 Apple Inc.
5# Copyright (c) 2004 Robert Shaw <rshaw@opendarwin.org>
6# Copyright (c) 2006-2007 Markus W. Weissmann <mww@macports.org>
7# Copyright (c) 2004-2014 The MacPorts Project
8# All rights reserved.
9#
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions
12# are met:
13# 1. Redistributions of source code must retain the above copyright
14#    notice, this list of conditions and the following disclaimer.
15# 2. Redistributions in binary form must reproduce the above copyright
16#    notice, this list of conditions and the following disclaimer in the
17#    documentation and/or other materials provided with the distribution.
18# 3. Neither the name of Apple Inc. nor the names of its contributors
19#    may be used to endorse or promote products derived from this software
20#    without specific prior written permission.
21#
22# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32# POSSIBILITY OF SUCH DAMAGE.
33#
34
35package provide portutil 1.0
36package require Pextlib 1.0
37package require macports_dlist 1.0
38package require macports_util 1.0
39package require msgcat
40package require porttrace 1.0
41
42global targets target_uniqid all_variants
43
44set targets [list]
45set target_uniqid 0
46
47set all_variants [list]
48
49########### External High Level Procedures ###########
50
51namespace eval options {
52}
53
54# option
55# This is an accessor for Portfile options.  Targets may use
56# this in the same style as the standard Tcl "set" procedure.
57#   option  - the name of the option to read or write
58#       not called 'name' because this would fail if its value was 'name'...
59#   value - an optional value to assign to the option
60
61proc option {option args} {
62    # XXX: right now we just transparently use globals
63    # eventually this will need to bridge the options between
64    # the Portfile's interpreter and the target's interpreters.
65    global $option
66    if {[llength $args] > 0} {
67        ui_debug "setting option $option to $args"
68        set $option [lindex $args 0]
69    }
70    return [set $option]
71}
72
73# exists
74# This is an accessor for Portfile options.  Targets may use
75# this procedure to test for the existence of a Portfile option.
76#   option - the name of the option to test for existence
77
78proc exists {option} {
79    # XXX: right now we just transparently use globals
80    # eventually this will need to bridge the options between
81    # the Portfile's interpreter and the target's interpreters.
82    global $option
83    return [info exists $option]
84}
85
86##
87# Handle an option
88#
89# @param option name of the option
90# @param args arguments
91proc handle_option {option args} {
92    global $option user_options option_procs
93
94    if {![info exists user_options($option)]} {
95        set $option $args
96    }
97}
98
99##
100# Handle option-append
101#
102# @param option name of the option
103# @param args arguments
104proc handle_option-append {option args} {
105    global $option user_options option_procs
106
107    if {![info exists user_options($option)]} {
108        if {[info exists $option]} {
109            set $option [concat [set $option] $args]
110        } else {
111            set $option $args
112        }
113    }
114}
115
116##
117# Handle option-prepend
118#
119# @param option name of the option
120# @param args arguments
121proc handle_option-prepend {option args} {
122    global $option user_options option_procs
123
124    if {![info exists user_options($option)]} {
125        if {[info exists $option]} {
126            set $option [concat $args [set $option]]
127        } else {
128            set $option $args
129        }
130    }
131}
132
133##
134# Handle option-delete
135#
136# @param option name of the option
137# @param args arguments
138proc handle_option-delete {option args} {
139    global $option user_options option_procs
140
141    if {![info exists user_options($option)] && [info exists $option]} {
142        set temp [set $option]
143        foreach val $args {
144            set temp [ldelete $temp $val]
145        }
146        set $option $temp
147    }
148}
149
150##
151# Handle option-strsed
152#
153# @param option name of the option
154# @param args arguments
155proc handle_option-strsed {option args} {
156    global $option user_options option_procs
157
158    if {![info exists user_options($option)] && [info exists $option]} {
159        set temp [set $option]
160        foreach val $args {
161            set temp [strsed $temp $val]
162        }
163        set $option $temp
164    }
165}
166
167##
168# Handle option-replace
169#
170# @param option name of the option
171# @param args arguments
172proc handle_option-replace {option args} {
173    global $option user_options option_procs deprecated_options
174
175    # Deprecate -replace with only one argument, for backwards compatibility call -strsed
176    # XXX: Remove this in 2.2.0
177    if {[llength $args] == 1} {
178        if {![info exists deprecated_options(${option}-replace)]} {
179            set deprecated_options(${option}-replace) [list ${option}-strsed 0]
180        }
181        set refcount [lindex $deprecated_options(${option}-replace) 1]
182        lset deprecated_options(${option}-replace) 1 [expr {$refcount + 1}]
183        return [handle_option-strsed $option {*}$args]
184    }
185
186    if {![info exists user_options($option)] && [info exists $option]} {
187        foreach {old new} $args {
188            set index [lsearch -exact [set $option] $old]
189            if {$index == -1} {
190                continue
191            }
192            set $option [lreplace [set $option] $index $index $new]
193        }
194    }
195}
196
197# options
198# Exports options in an array as externally callable procedures
199# Thus, "options name date" would create procedures named "name"
200# and "date" that set global variables "name" and "date", respectively
201# When an option is modified in any way, options::$option is called,
202# if it exists
203# Arguments: <list of options>
204proc options {args} {
205    foreach option $args {
206        interp alias {} $option {} handle_option $option
207        interp alias {} $option-append {} handle_option-append $option
208        interp alias {} $option-prepend {} handle_option-prepend $option
209        interp alias {} $option-delete {} handle_option-delete $option
210        interp alias {} $option-strsed {} handle_option-strsed $option
211        interp alias {} $option-replace {} handle_option-replace $option
212    }
213}
214
215##
216# Export options into PortInfo
217#
218# @param option the name of the option
219# @param action set or delete
220# @param value the value to be set, defaults to an empty string
221proc options::export {option action {value ""}} {
222    global $option PortInfo
223    switch $action {
224        set {
225            set PortInfo($option) $value
226        }
227        delete {
228            unset -nocomplain PortInfo($option)
229        }
230    }
231}
232
233##
234# Export multiple options
235#
236# @param args list of ports to be exported
237proc options_export {args} {
238    foreach option $args {
239        option_proc $option options::export
240    }
241}
242
243##
244# Print a warning for deprecated options
245#
246# @param option deprecated option
247# @param action read/set
248# @param value ignored
249proc handle_deprecated_option {option action {value ""}} {
250    global subport $option deprecated_options
251    set newoption [lindex $deprecated_options($option) 0]
252    set refcount  [lindex $deprecated_options($option) 1]
253    global $newoption
254
255    if {$newoption eq ""} {
256        ui_warn "Port $subport using deprecated option \"$option\"."
257        return
258    }
259
260    # Increment reference counter
261    lset deprecated_options($option) 1 [expr {$refcount + 1}]
262
263    if {$action ne "read"} {
264        $newoption [set $option]
265    } else {
266        $option [set $newoption]
267    }
268}
269
270##
271# Get the name of the array containing the deprecated options
272# Thin layer avoiding to share global variables without notice
273proc get_deprecated_options {} {
274    return "deprecated_options"
275}
276
277##
278# Mark an option as deprecated
279# If it is set or accessed, it will be mapped to the new option
280#
281# @param option name of the option
282# @param newoption name of a superseding option
283proc option_deprecate {option {newoption ""} } {
284    global deprecated_options
285    # If a new option is specified, default the option to $newoption
286    set deprecated_options($option) [list $newoption 0]
287    # Create a normal option for compatibility
288    options $option
289    # Register a proc for handling the deprecation
290    option_proc $option handle_deprecated_option
291}
292
293##
294# Registers a proc to be called when an option is changed
295#
296# @param option the name of the option
297# @param args name of procs
298proc option_proc {option args} {
299    global option_procs $option
300    if {[info exists option_procs($option)]} {
301        set option_procs($option) [concat $option_procs($option) $args]
302        # we're already tracing
303    } else {
304        set option_procs($option) $args
305        trace add variable $option {read write unset} option_proc_trace
306    }
307}
308
309# option_proc_trace
310# trace handler for option reads. Calls option procedures with correct arguments.
311proc option_proc_trace {optionName index op} {
312    global option_procs
313    upvar $optionName $optionName
314    switch $op {
315        write {
316            foreach p $option_procs($optionName) {
317                $p $optionName set [set $optionName]
318            }
319        }
320        read {
321            foreach p $option_procs($optionName) {
322                $p $optionName read
323            }
324        }
325        unset {
326            foreach p $option_procs($optionName) {
327                if {[catch {$p $optionName delete} result]} {
328                    ui_debug "error during unset trace ($p): $result\n$::errorInfo"
329                }
330            }
331            trace add variable $optionName {read write unset} option_proc_trace
332        }
333    }
334}
335
336# commands
337# Accepts a list of arguments, of which several options are created
338# and used to form a standard set of command options.
339proc commands {args} {
340    foreach option $args {
341        options use_${option} ${option}.dir ${option}.pre_args ${option}.args ${option}.post_args ${option}.env ${option}.nice ${option}.type ${option}.cmd
342    }
343}
344
345# Given a command name, assemble a command string
346# composed of the command options.
347proc command_string {command} {
348    global ${command}.dir ${command}.pre_args ${command}.args ${command}.post_args ${command}.cmd
349
350    if {[info exists ${command}.dir]} {
351        append cmdstring "cd \"[set ${command}.dir]\" &&"
352    }
353
354    if {[info exists ${command}.cmd]} {
355        foreach string [set ${command}.cmd] {
356            append cmdstring " $string"
357        }
358    } else {
359        append cmdstring " ${command}"
360    }
361
362    foreach var "${command}.pre_args ${command}.args ${command}.post_args" {
363        if {[info exists $var]} {
364            foreach string [set ${var}] {
365                append cmdstring " ${string}"
366            }
367        }
368    }
369
370    ui_debug "Assembled command: '$cmdstring'"
371    return $cmdstring
372}
373
374# Given a command name, execute it with the options.
375# command_exec command [-notty] [-varprefix variable_prefix] [command_prefix [command_suffix]]
376# command           name of the command
377# variable_prefix   name of the variable prefix to use (defaults to command)
378# command_prefix    additional command prefix (typically pipe command)
379# command_suffix    additional command suffix (typically redirection)
380proc command_exec {command args} {
381    set varprefix "${command}"
382    set notty ""
383    set command_prefix ""
384    set command_suffix ""
385
386    if {[llength $args] > 0} {
387        if {[lindex $args 0] eq "-notty"} {
388            set notty "-notty"
389            set args [lrange $args 1 end]
390        }
391
392        if {[lindex $args 0] eq "-varprefix"} {
393            set varprefix [lindex $args 1]
394            set args [lrange $args 2 end]
395        }
396
397        if {[llength $args] > 0} {
398            set command_prefix [lindex $args 0]
399            if {[llength $args] > 1} {
400                set command_suffix [lindex $args 1]
401            }
402        }
403    }
404
405    set dir [option ${varprefix}.dir]
406    if {![file exists ${dir}]} {
407        ui_debug "Creating ${varprefix} directory: ${dir}"
408        file mkdir ${dir}
409    }
410
411    global ${varprefix}.env ${varprefix}.env_array ${varprefix}.nice env macosx_version
412
413    # Set the environment.
414    # If the array doesn't exist, we create it with the value
415    # coming from ${varprefix}.env
416    # Otherwise, it means the caller actually played with the environment
417    # array already (e.g. configure flags).
418    if {![array exists ${varprefix}.env_array]} {
419        parse_environment ${varprefix}
420    }
421    if {[option macosx_deployment_target] ne ""} {
422        set ${varprefix}.env_array(MACOSX_DEPLOYMENT_TARGET) [option macosx_deployment_target]
423    }
424    set ${varprefix}.env_array(CC_PRINT_OPTIONS) "YES"
425    set ${varprefix}.env_array(CC_PRINT_OPTIONS_FILE) [file join [option workpath] ".CC_PRINT_OPTIONS"]
426    if {[option compiler.cpath] ne ""} {
427        set ${varprefix}.env_array(CPATH) [join [option compiler.cpath] :]
428    }
429    if {[option compiler.library_path] ne ""} {
430        set ${varprefix}.env_array(LIBRARY_PATH) [join [option compiler.library_path] :]
431    }
432
433    # Debug that.
434    ui_debug "Environment: [environment_array_to_string ${varprefix}.env_array]"
435
436    # Prepare nice value change
437    set nice ""
438    if {[info exists ${varprefix}.nice] && [set ${varprefix}.nice] ne ""} {
439        set nice "-nice [set ${varprefix}.nice]"
440    }
441
442    # Get the command string.
443    set cmdstring [command_string ${command}]
444
445    # Call this command.
446    # TODO: move that to the system native call?
447    # Save the environment.
448    array set saved_env [array get env]
449    # Set the overriden variables from the portfile.
450    array set env [array get ${varprefix}.env_array]
451    # Call the command.
452    set fullcmdstring "$command_prefix $cmdstring $command_suffix"
453    ui_debug "Executing command line: $fullcmdstring"
454    set code [catch {system {*}$notty {*}$nice $fullcmdstring} result]
455    # Save variables in order to re-throw the same error code.
456    set errcode $::errorCode
457    set errinfo $::errorInfo
458
459    # Unset the command array until next time.
460    array unset ${varprefix}.env_array
461
462    # Restore the environment.
463    array unset env *
464    if {$macosx_version eq "10.5"} {
465        unsetenv *
466    }
467    array set env [array get saved_env]
468
469    # Return as if system had been called directly.
470    return -code $code -errorcode $errcode -errorinfo $errinfo $result
471}
472
473# default
474# Sets a variable to the supplied default if it does not exist,
475# and adds a variable trace. The variable traces allows for delayed
476# variable and command expansion in the variable's default value.
477proc default {option val} {
478    global $option option_defaults
479    if {[info exists option_defaults($option)]} {
480        ui_debug "Re-registering default for $option"
481        # remove the old trace
482        trace vdelete $option rwu default_check
483    } else {
484        # If option is already set and we did not set it
485        # do not reset the value
486        if {[info exists $option]} {
487            return
488        }
489    }
490    set option_defaults($option) $val
491    set $option $val
492    trace variable $option rwu default_check
493}
494
495# default_check
496# trace handler to provide delayed variable & command expansion
497# for default variable values
498proc default_check {optionName index op} {
499    global option_defaults $optionName
500    switch $op {
501        w {
502            unset option_defaults($optionName)
503            trace vdelete $optionName rwu default_check
504            return
505        }
506        r {
507            upvar $optionName option
508            uplevel #0 set $optionName $option_defaults($optionName)
509            return
510        }
511        u {
512            unset option_defaults($optionName)
513            trace vdelete $optionName rwu default_check
514            return
515        }
516    }
517}
518
519##
520# Filter options which take strings removing indent to ease Portfile writing
521proc handle_option_string {option action args} {
522    global $option
523
524    switch $action {
525        set {
526            set args [join $args]
527
528            set fulllist {}
529            # args is a list of strings/list
530            foreach arg $args {
531                # Strip trailing empty lines
532                if {[string index $arg 0] eq "\n"} {
533                    set arg [string range $arg 1 end]
534                }
535                if {[string index $arg end] eq "\n"} {
536                    set arg [string range $arg 0 end-1]
537                }
538
539                # Determine indent level
540                set indent ""
541                for {set i 0} {$i < [string length $arg]} {incr i} {
542                    set c [string index $arg $i]
543                    if {$c ne " " && $c ne "\t"} {
544                        break
545                    }
546                    append indent $c
547                }
548                # Remove indent on first line
549                set arg [string replace $arg 0 [expr {$i - 1}]]
550                # Remove indent on each other line
551                set arg [string map "\"\n$indent\" \"\n\"" $arg]
552
553                lappend fulllist $arg
554            }
555
556            set $option $fulllist
557        }
558    }
559}
560
561# variant <provides> [<provides> ...] [requires <requires> [<requires>]]
562# Portfile level procedure to provide support for declaring variants
563proc variant {args} {
564    global all_variants PortInfo porturl
565
566    # Each key in PortInfo(vinfo) maps to an array which contains the
567    # following keys:
568    #   * conflicts
569    #   * description
570    #   * is_default: This key exists iff the variant is a default variant.
571    #   * requires
572    if {![info exists PortInfo(vinfo)]} {
573        set PortInfo(vinfo) {}
574    }
575    array set vinfo $PortInfo(vinfo)
576
577    set len [llength $args]
578    if {$len < 2} {
579        return -code error "Malformed variant specification"
580    }
581    set code [lindex $args end]
582    set args [lrange $args 0 [expr {$len - 2}]]
583
584    set ditem [variant_new "temp-variant"]
585
586    # mode indicates what the arg is interpreted as.
587    # possible mode keywords are: requires, conflicts, provides
588    # The default mode is provides.  Arguments are added to the
589    # most recently specified mode (left to right).
590    set mode "provides"
591    foreach arg $args {
592        switch -exact $arg {
593            description -
594            provides -
595            requires -
596            conflicts { set mode $arg }
597            default { ditem_append $ditem $mode $arg }
598        }
599    }
600    ditem_key $ditem name "[join [ditem_key $ditem provides] -]"
601
602    if {![regexp {^[A-Za-z0-9_]+$} [ditem_key $ditem provides]]} {
603        set name [ditem_key $ditem provides] 
604        ditem_delete $ditem
605        return -code error "Variant name $name contains invalid characters"
606    }
607
608    # make a user procedure named variant-blah-blah
609    # we will call this procedure during variant-run
610    makeuserproc variant-[ditem_key $ditem name] $code
611
612    # Export provided variant to PortInfo
613    # (don't list it twice if the variant was already defined, which can happen
614    # with universal or group code).
615    set variant_provides [ditem_key $ditem provides]
616    if {[variant_exists $variant_provides]} {
617        # This variant was already defined. Remove it from the dlist.
618        variant_remove_ditem $variant_provides
619    } else {
620        # Create an array to contain the variant's information.
621        if {![info exists vinfo($variant_provides)]} {
622            set vinfo($variant_provides) {}
623        }
624        array set variant $vinfo($variant_provides)
625
626        # Set conflicts.
627        set vconflicts [join [lsort [ditem_key $ditem conflicts]]]
628        if {$vconflicts ne ""} {
629            array set variant [list conflicts $vconflicts]
630        }
631
632        lappend PortInfo(variants) $variant_provides
633        set vdesc [join [ditem_key $ditem description]]
634
635        # read global variant description, if none given
636        if {$vdesc eq ""} {
637            set vdesc [variant_desc $porturl $variant_provides]
638        }
639
640        # Set description.
641        if {$vdesc ne ""} {
642            array set variant [list description $vdesc]
643        }
644
645        # Set requires.
646        set vrequires [join [lsort [ditem_key $ditem requires]]]
647        if {$vrequires ne ""} {
648            array set variant [list requires $vrequires]
649        }
650    }
651
652    # Add the variant (back) to PortInfo(vinfo).
653    array set vinfo [list $variant_provides [array get variant]]
654    set PortInfo(vinfo) [array get vinfo]
655
656    if {[variant_isset $variant_provides]} {
657        # set variants that this one requires
658        foreach req [ditem_key $ditem requires] {
659            variant_set $req
660        }
661    }
662
663    # Finally append the ditem to the dlist.
664    lappend all_variants $ditem
665}
666
667# variant_isset name
668# Returns 1 if variant name selected, otherwise 0
669proc variant_isset {name} {
670    global variations
671
672    if {[info exists variations($name)] && $variations($name) eq "+"} {
673        return 1
674    }
675    return 0
676}
677
678# variant_set name
679# Sets variant to run for current portfile
680proc variant_set {name} {
681    global variations
682    set variations($name) +
683}
684
685# variant_remove_ditem name
686# Remove variant name's ditem from the all_variants dlist
687proc variant_remove_ditem {name} {
688    global all_variants
689    set item_index 0
690    foreach variant_item $all_variants {
691        set item_provides [ditem_key $variant_item provides]
692        if {$item_provides == $name} {
693            set all_variants [lreplace $all_variants $item_index $item_index]
694            break
695        }
696
697        incr item_index
698    }
699}
700
701# variant_exists name
702# determine if a variant exists.
703proc variant_exists {name} {
704    global PortInfo
705    if {[info exists PortInfo(variants)] &&
706      [lsearch -exact $PortInfo(variants) $name] >= 0} {
707        return 1
708    }
709
710    return 0
711}
712
713##
714# Load the global description file for a port tree
715#
716# @param descfile path to the descriptions file
717proc load_variant_desc_file {descfile} {
718    global variant_descs_global
719
720    if {![info exists variant_descs_global($descfile)]} {
721        set variant_descs_global($descfile) yes
722
723        if {[file exists $descfile]} {
724            ui_debug "Reading variant descriptions from $descfile"
725
726            if {[catch {set fd [open $descfile r]} err]} {
727                ui_warn "Could not open global variant description file: $err"
728                return ""
729            }
730            set lineno 0
731            while {[gets $fd line] >= 0} {
732                incr lineno
733                set name [lindex $line 0]
734                set desc [lindex $line 1]
735                if {$name ne "" && $desc ne ""} {
736                    set variant_descs_global(${descfile}_$name) $desc
737                } else {
738                    ui_warn "Invalid variant description in $descfile at line $lineno"
739                }
740            }
741            close $fd
742        }
743    }
744}
745
746##
747# Get description for a variant from global descriptions file
748#
749# @param porturl url to a port
750# @param variant name
751# @return description from descriptions file or an empty string
752proc variant_desc {porturl variant} {
753    global variant_descs_global
754
755    set descfile [getportresourcepath $porturl "port1.0/variant_descriptions.conf" no]
756    load_variant_desc_file $descfile
757
758    if {[info exists variant_descs_global(${descfile}_${variant})]} {
759        return $variant_descs_global(${descfile}_${variant})
760    } else {
761        set descfile [getdefaultportresourcepath "port1.0/variant_descriptions.conf"]
762        load_variant_desc_file $descfile
763
764        if {[info exists variant_descs_global(${descfile}_${variant})]} {
765            return $variant_descs_global(${descfile}_${variant})
766        }
767
768        return ""
769    }
770}
771
772# platform [<os> [<release>]] [<arch>]
773# Portfile level procedure to provide support for declaring platform-specifics
774# Basically, just a fancy 'if', so that Portfiles' platform declarations can
775# be more readable, and support arch and version specifics
776proc platform {args} {
777    global os.platform os.subplatform os.arch os.major
778
779    set len [llength $args]
780    if {$len < 2} {
781        return -code error "Malformed platform specification"
782    }
783    set code [lindex $args end]
784    set os [lindex $args 0]
785    set args [lrange $args 1 [expr {$len - 2}]]
786
787    foreach arg $args {
788        if {[regexp {(^[0-9]+$)} $arg match result]} {
789            set release $result
790        } elseif {[regexp {([a-zA-Z0-9]*)} $arg match result]} {
791            set arch $result
792        }
793    }
794
795    set match 0
796    # 'os' could be a platform or an arch when it's alone
797    if {$len == 2 && ($os == ${os.platform} || $os == ${os.subplatform} || $os == ${os.arch})} {
798        set match 1
799    } elseif {($os == ${os.platform} || $os == ${os.subplatform})
800              && (![info exists release] || ${os.major} == $release)
801              && (![info exists arch] || ${os.arch} == $arch)} {
802        set match 1
803    }
804
805    # Execute the code if this platform matches the platform we're on
806    if {$match} {
807        uplevel 1 $code
808    }
809}
810
811# Portfiles may define more than one port.
812# This executes the given code in 'body' if we were opened as the specified
813# subport, and also adds it to the list of subports that are defined.
814proc subport {subname body} {
815    global subport name PortInfo
816    if {$subport eq $name && $subname ne $name && 
817        (![info exists PortInfo(subports)] || [lsearch -exact $PortInfo(subports) $subname] == -1)} {
818        lappend PortInfo(subports) $subname
819    }
820    if {[string equal -nocase $subname $subport]} {
821        set PortInfo(name) $subname
822        uplevel 1 $body
823    }
824}
825
826########### Environment utility functions ###########
827
828# Parse the environment string of a command, storing the values into the
829# associated environment array.
830proc parse_environment {command} {
831    global ${command}.env ${command}.env_array
832
833    if {[info exists ${command}.env]} {
834        # Flatten the environment string.
835        set the_environment [join [set ${command}.env]]
836
837        while {[regexp "^(?: *)(\[^= \]+)=(\"|'|)(\[^\"'\]*?)\\2(?: +|$)(.*)$" ${the_environment} matchVar key delimiter value remaining]} {
838            set the_environment ${remaining}
839            set ${command}.env_array(${key}) ${value}
840        }
841    } else {
842        array set ${command}.env_array {}
843    }
844}
845
846# Append one or more items to the key in the parsed environment.
847proc append_to_environment_value {command key args} {
848    upvar #0 ${command}.env_array($key) env_key
849    foreach value $args {
850        if {$value eq {}} {
851            continue
852        }
853        # Parse out any delimiters. Is this even necessary anymore?
854        regexp {^(['"])(.*)\1$} $value -> delim value
855
856        append env_key " $value"
857    }
858    catch {set env_key [string trimleft $env_key]}
859}
860
861# Return a string representation of the specified environment, for
862# debugging purposes.
863proc environment_array_to_string {environment_array} {
864    upvar 1 ${environment_array} env_array
865    foreach {key value} [array get env_array] {
866        lappend env_list $key='$value'
867    }
868    return "\n[join [lsort $env_list] "\n"]"
869}
870
871########### Distname utility functions ###########
872
873# Given a distribution file name, return the appended tag
874# Example: getdisttag distfile.tar.gz:tag1 returns "tag1"
875# / isn't included in the regexp, thus allowing port specification in URLs.
876proc getdisttag {name} {
877    if {[regexp {.+:([0-9A-Za-z_-]+)$} $name match tag]} {
878        return $tag
879    } else {
880        return ""
881    }
882}
883
884# Given a distribution file name, return the name without an attached tag
885# Example : getdistname distfile.tar.gz:tag1 returns "distfile.tar.gz"
886# / isn't included in the regexp, thus allowing port specification in URLs.
887proc getdistname {name} {
888    regexp {(.+):[0-9A-Za-z_-]+$} $name match name
889    return $name
890}
891
892########### Misc Utility Functions ###########
893
894# tbool (testbool)
895# If the variable exists in the calling procedure's namespace
896# and is set to "yes", return 1. Otherwise, return 0
897proc tbool {key} {
898    upvar $key $key
899    if {[info exists $key]} {
900        if {[string equal -nocase [set $key] "yes"]} {
901            return 1
902        }
903    }
904    return 0
905}
906
907# ldelete
908# Deletes a value from the supplied list
909proc ldelete {list value} {
910    set ix [lsearch -exact $list $value]
911    if {$ix >= 0} {
912        return [lreplace $list $ix $ix]
913    }
914    return $list
915}
916
917# reinplace
918# Provides "sed in place" functionality
919proc reinplace {args}  {
920    global env workpath worksrcpath macosx_version
921    set extended 0
922    set suppress 0
923    # once a macports version has been released, add the rest of the
924    # code from https://trac.macports.org/ticket/15514
925    set quiet 0
926    set oldlocale_exists 0
927    set oldlocale ""
928    set locale ""
929    set dir ${worksrcpath}
930    while 1 {
931        set arg [lindex $args 0]
932        if {[string index $arg 0] eq "-"} {
933            set args [lrange $args 1 end]
934            switch -- [string range $arg 1 end] {
935                locale {
936                    set oldlocale_exists [info exists env(LC_CTYPE)]
937                    if {$oldlocale_exists} {
938                        set oldlocale $env(LC_CTYPE)
939                    }
940                    set locale [lindex $args 0]
941                    set args [lrange $args 1 end]
942                }
943                E {
944                    set extended 1
945                }
946                n {
947                    set suppress 1
948                }
949                q {
950                    set quiet 1
951                }
952                W {
953                    set dir [lindex $args 0]
954                    set args [lrange $args 1 end]
955                }
956                - {
957                    break
958                }
959                default {
960                    error "reinplace: unknown flag '$arg'"
961                }
962            }
963        } else {
964            break
965        }
966    }
967    if {[llength $args] < 2} {
968        error "reinplace ?-E? ?-n? ?-q? ?-W dir? pattern file ..."
969    }
970    set pattern [lindex $args 0]
971    set files [lrange $args 1 end]
972
973    if {[file isdirectory ${workpath}/.tmp]} {
974        set tempdir ${workpath}/.tmp
975    } else {
976        set tempdir /tmp
977    }
978
979    foreach file $files {
980        global UI_PREFIX
981
982        # if $file is an absolute path already, file join will just return the
983        # absolute path, otherwise it is $dir/$file
984        set file [file join $dir $file]
985
986        if {[catch {set tmpfile [mkstemp "${tempdir}/[file tail $file].sed.XXXXXXXX"]} error]} {
987            global errorInfo
988            ui_debug "$errorInfo"
989            ui_error "reinplace: $error"
990            return -code error "reinplace failed"
991        } else {
992            # Extract the Tcl Channel number
993            set tmpfd [lindex $tmpfile 0]
994            # Set tmpfile to only the file name
995            set tmpfile [join [lrange $tmpfile 1 end]]
996        }
997
998        set cmdline {}
999        lappend cmdline $portutil::autoconf::sed_command
1000        if {$extended} {
1001            if {$portutil::autoconf::sed_ext_flag eq "N/A"} {
1002                ui_debug "sed extended regexp not available"
1003                return -code error "reinplace sed(1) too old"
1004            }
1005            lappend cmdline $portutil::autoconf::sed_ext_flag
1006        }
1007        if {$suppress} {
1008            lappend cmdline -n
1009        }
1010        lappend cmdline $pattern "<$file" ">@$tmpfd"
1011        if {$locale ne ""} {
1012            set env(LC_CTYPE) $locale
1013        }
1014        ui_info "$UI_PREFIX [format [msgcat::mc "Patching %s: %s"] [file tail $file] $pattern]"
1015        ui_debug "Executing reinplace: $cmdline"
1016        if {[catch {exec -ignorestderr -- {*}$cmdline} error]} {
1017            global errorInfo
1018            ui_debug "$errorInfo"
1019            ui_error "reinplace: $error"
1020            file delete "$tmpfile"
1021            if {$locale ne ""} {
1022                if {$oldlocale_exists} {
1023                    set env(LC_CTYPE) $oldlocale
1024                } else {
1025                    unset env(LC_CTYPE)
1026                    if {$macosx_version eq "10.5"} {
1027                        unsetenv LC_CTYPE
1028                    }
1029                }
1030            }
1031            close $tmpfd
1032            return -code error "reinplace sed(1) failed"
1033        }
1034
1035        if {$locale ne ""} {
1036            if {$oldlocale_exists} {
1037                set env(LC_CTYPE) $oldlocale
1038            } else {
1039                unset env(LC_CTYPE)
1040                if {$macosx_version eq "10.5"} {
1041                    unsetenv LC_CTYPE
1042                }
1043            }
1044        }
1045        close $tmpfd
1046
1047        set attributes [file attributes $file]
1048        # start gsoc08-privileges
1049        chownAsRoot $file
1050        # end gsoc08-privileges
1051
1052        # We need to overwrite this file
1053        if {[catch {file attributes $file -permissions u+w} error]} {
1054            global errorInfo
1055            ui_debug "$errorInfo"
1056            ui_error "reinplace: $error"
1057            file delete "$tmpfile"
1058            return -code error "reinplace permissions failed"
1059        }
1060
1061        if {[catch {file copy -force $tmpfile $file} error]} {
1062            global errorInfo
1063            ui_debug "$errorInfo"
1064            ui_error "reinplace: $error"
1065            file delete "$tmpfile"
1066            return -code error "reinplace copy failed"
1067        }
1068
1069        fileAttrsAsRoot $file $attributes
1070
1071        file delete "$tmpfile"
1072    }
1073    return
1074}
1075
1076# delete
1077# Wrapper for file delete -force
1078proc delete {args} {
1079    file delete -force -- {*}$args
1080}
1081
1082# touch
1083# mimics the BSD touch command
1084proc touch {args} {
1085    while {[string match "-*" [lindex $args 0]]} {
1086        set arg [string range [lindex $args 0] 1 end]
1087        set args [lrange $args 1 end]
1088        switch -- $arg {
1089            a -
1090            c -
1091            m {set options($arg) yes}
1092            r -
1093            t {
1094                set narg [lindex $args 0]
1095                set args [lrange $args 1 end]
1096                if {$narg eq ""} {
1097                    return -code error "touch: option requires an argument -- $arg"
1098                }
1099                set options($arg) $narg
1100                set options(rt) $arg ;# later option overrides earlier
1101            }
1102            - break
1103            default {return -code error "touch: illegal option -- $arg"}
1104        }
1105    }
1106
1107    # parse the r/t options
1108    if {[info exists options(rt)]} {
1109        if {$options(rt) eq "r"} {
1110            # -r
1111            # get atime/mtime from the file
1112            if {[file exists $options(r)]} {
1113                set atime [file atime $options(r)]
1114                set mtime [file mtime $options(r)]
1115            } else {
1116                return -code error "touch: $options(r): No such file or directory"
1117            }
1118        } else {
1119            # -t
1120            # parse the time specification
1121            # turn it into a CCyymmdd hhmmss
1122            set timespec {^(?:(\d\d)?(\d\d))?(\d\d)(\d\d)(\d\d)(\d\d)(?:\.(\d\d))?$}
1123            if {[regexp $timespec $options(t) {} CC YY MM DD hh mm SS]} {
1124                if {$YY eq ""} {
1125                    set year [clock format [clock seconds] -format %Y]
1126                } elseif {$CC eq ""} {
1127                    if {$YY >= 69 && $YY <= 99} {
1128                        set year 19$YY
1129                    } else {
1130                        set year 20$YY
1131                    }
1132                } else {
1133                    set year $CC$YY
1134                }
1135                if {$SS eq ""} {
1136                    set SS 00
1137                }
1138                set atime [clock scan "$year$MM$DD $hh$mm$SS"]
1139                set mtime $atime
1140            } else {
1141                return -code error \
1142                    {touch: out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]}
1143            }
1144        }
1145    } else {
1146        set atime [clock seconds]
1147        set mtime [clock seconds]
1148    }
1149
1150    # do we have any files to process?
1151    if {[llength $args] == 0} {
1152        # print usage
1153        return -code error {usage: touch [-a] [-c] [-m] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...}
1154    }
1155
1156    foreach file $args {
1157        if {![file exists $file]} {
1158            if {[info exists options(c)]} {
1159                continue
1160            } else {
1161                close [open $file w]
1162            }
1163        }
1164
1165        if {[info exists options(a)] || ![info exists options(m)]} {
1166            file atime $file $atime
1167        }
1168        if {[info exists options(m)] || ![info exists options(a)]} {
1169            file mtime $file $mtime
1170        }
1171    }
1172    return
1173}
1174
1175# copy
1176# Wrapper for file copy
1177proc copy {args} {
1178    file copy {*}$args
1179}
1180
1181# move
1182# Wrapper for file rename that handles case-only renames
1183proc move {args} {
1184    set options {}
1185    while {[string match "-*" [lindex $args 0]]} {
1186        set arg [string range [lindex $args 0] 1 end]
1187        set args [lreplace $args 0 0]
1188        switch -- $arg {
1189            force {append options -$arg}
1190            - break
1191            default {return -code error "move: illegal option -- $arg"}
1192        }
1193    }
1194    if {[llength $args] == 2} {
1195        set oldname [lindex $args 0]
1196        set newname [lindex $args 1]
1197        if {[string equal -nocase $oldname $newname] && $oldname ne $newname} {
1198            # case-only rename
1199            set tempdir [mkdtemp ${oldname}-XXXXXXXX]
1200            set tempname $tempdir/[file tail $oldname]
1201            file rename $options -- $oldname $tempname
1202            file rename $options -- $tempname $newname
1203            delete $tempdir
1204            return
1205        }
1206    }
1207    file rename {*}$options -- {*}$args
1208}
1209
1210# ln
1211# Mimics the BSD ln implementation
1212# ln [-f] [-h] [-s] [-v] source_file [target_file]
1213# ln [-f] [-h] [-s] [-v] source_file ... target_dir
1214proc ln {args} {
1215    while {[string match "-*" [lindex $args 0]]} {
1216        set arg [string range [lindex $args 0] 1 end]
1217        if {[string length $arg] > 1} {
1218            set remainder -[string range $arg 1 end]
1219            set arg [string range $arg 0 0]
1220            set args [lreplace $args 0 0 $remainder]
1221        } else {
1222            set args [lreplace $args 0 0]
1223        }
1224        switch -- $arg {
1225            f -
1226            h -
1227            s -
1228            v {set options($arg) yes}
1229            - break
1230            default {return -code error "ln: illegal option -- $arg"}
1231        }
1232    }
1233
1234    if {[llength $args] == 0} {
1235        return -code error [join {{usage: ln [-f] [-h] [-s] [-v] source_file [target_file]}
1236                                  {       ln [-f] [-h] [-s] [-v] file ... directory}} "\n"]
1237    } elseif {[llength $args] == 1} {
1238        set files $args
1239        set target ./
1240    } else {
1241        set files [lrange $args 0 [expr {[llength $args] - 2}]]
1242        set target [lindex $args end]
1243    }
1244
1245    foreach file $files {
1246        if {[file isdirectory $file] && ![info exists options(s)]} {
1247            return -code error "ln: $file: Is a directory"
1248        }
1249
1250        if {[file isdirectory $target] && ([file type $target] ne "link" || ![info exists options(h)])} {
1251            set linktarget [file join $target [file tail $file]]
1252        } else {
1253            set linktarget $target
1254        }
1255
1256        if {![catch {file type $linktarget}]} {
1257            if {[info exists options(f)]} {
1258                file delete $linktarget
1259            } else {
1260                return -code error "ln: $linktarget: File exists"
1261            }
1262        }
1263
1264        if {[llength $files] > 2} {
1265            if {![file exists $linktarget]} {
1266                return -code error "ln: $linktarget: No such file or directory"
1267            } elseif {![file isdirectory $target]} {
1268                # this error isn't strictly what BSD ln gives, but I think it's more useful
1269                return -code error "ln: $target: Not a directory"
1270            }
1271        }
1272
1273        if {[info exists options(v)]} {
1274            ui_notice "ln: $linktarget -> $file"
1275        }
1276        if {[info exists options(s)]} {
1277            symlink $file $linktarget
1278        } else {
1279            file link -hard $linktarget $file
1280        }
1281    }
1282    return
1283}
1284
1285# makeuserproc
1286# This procedure re-writes the user-defined custom target to include
1287# all the globals in its scope.  This is undeniably ugly, but I haven't
1288# thought of any other way to do this.
1289proc makeuserproc {name body} {
1290    append modified_body {global {*}[info globals]} \n $body
1291    proc $name {} $modified_body
1292}
1293
1294# backup
1295# Operates on universal_filelist, creates universal_archlist
1296# Save single-architecture files, a temporary location, preserving the original
1297# directory structure.
1298
1299proc backup {arch} {
1300    global universal_archlist universal_filelist workpath
1301    lappend universal_archlist ${arch}
1302    foreach file ${universal_filelist} {
1303        set filedir [file dirname $file]
1304        xinstall -d ${workpath}/${arch}/${filedir}
1305        xinstall ${file} ${workpath}/${arch}/${filedir}
1306    }
1307}
1308
1309# lipo
1310# Operates on universal_filelist, universal_archlist.
1311# Run lipo(1) on a list of single-arch files.
1312
1313proc lipo {} {
1314    global universal_archlist universal_filelist workpath
1315    foreach file ${universal_filelist} {
1316        xinstall -d [file dirname $file]
1317        file delete ${file}
1318        set lipoSources ""
1319        foreach arch $universal_archlist {
1320            append lipoSources "-arch ${arch} ${workpath}/${arch}/${file} "
1321        }
1322        system "[findBinary lipo $portutil::autoconf::lipo_path] ${lipoSources}-create -output ${file}"
1323    }
1324}
1325
1326
1327# unobscure maintainer addresses as used in Portfiles
1328# We allow two obscured forms:
1329#   (1) User name only with no domain:
1330#           foo implies foo@macports.org
1331#   (2) Mangled name:
1332#           subdomain.tld:username implies username@subdomain.tld
1333#
1334proc unobscure_maintainers { list } {
1335    set result {}
1336    foreach m $list {
1337        if {[string first "@" $m] < 0} {
1338            if {[string first ":" $m] >= 0} {
1339                set m [regsub -- "(.*):(.*)" $m "\\2@\\1"]
1340            } else {
1341                set m "$m@macports.org"
1342            }
1343        }
1344        lappend result $m
1345    }
1346    return $result
1347}
1348
1349
1350
1351
1352########### Internal Dependency Manipulation Procedures ###########
1353global ports_dry_last_skipped
1354set ports_dry_last_skipped ""
1355
1356proc target_run {ditem} {
1357    global target_state_fd workpath portpath ports_trace PortInfo ports_dryrun \
1358           ports_dry_last_skipped worksrcpath prefix subport env portdbpath \
1359           macosx_version
1360    set portname $subport
1361    set result 0
1362    set skipped 0
1363    set procedure [ditem_key $ditem procedure]
1364    set savedhome [file join $portdbpath home]
1365    set env(HOME) "${workpath}/.home"
1366    set env(TMPDIR) "${workpath}/.tmp"
1367    # targets to run even in dry-run mode (these should have their own dry-run checks)
1368    set dryrun_allow_targets {org.macports.uninstall}
1369
1370    if {[ditem_key $ditem state] ne "no"} {
1371        set target_state_fd [open_statefile]
1372    }
1373
1374    if {$procedure ne ""} {
1375        set targetname [ditem_key $ditem name]
1376        set target [ditem_key $ditem provides]
1377        portsandbox::set_profile $target
1378        global ${target}.asroot
1379        if { [tbool ${target}.asroot] } {
1380            elevateToRoot $targetname
1381        }
1382
1383        if {[ditem_contains $ditem init]} {
1384            set result [catch {[ditem_key $ditem init] $targetname} errstr]
1385            # Save variables in order to re-throw the same error code.
1386            set errcode $::errorCode
1387            set errinfo $::errorInfo
1388        }
1389
1390        if {$result == 0} {
1391            # Skip the step if required and explain why through ui_debug.
1392            # check if the step was already done (as mentioned in the state file)
1393            if {[ditem_key $ditem state] ne "no"
1394                    && [check_statefile target $targetname $target_state_fd]} {
1395                ui_debug "Skipping completed $targetname ($portname)"
1396                set skipped 1
1397            }
1398
1399            # Of course, if this is a dry run, don't do the task:
1400            if {[info exists ports_dryrun] && $ports_dryrun eq "yes" && [lsearch -exact $dryrun_allow_targets $targetname] == -1} {
1401                # only one message per portname
1402                if {$portname != $ports_dry_last_skipped} {
1403                    ui_notice "For $portname: skipping $targetname (dry run)"
1404                    set ports_dry_last_skipped $portname
1405                } else {
1406                    ui_info "    .. and skipping $targetname"
1407                }
1408                set skipped 1
1409            }
1410
1411            # otherwise execute the task.
1412            if {$skipped == 0} {
1413                # cd somewhere readable in tracemode to avoid error, e.g. with
1414                # find. Make sure to use a path that also exists when executing
1415                # Portfiles from registry, i.e., _not_ $workpath.
1416                set oldpwd [pwd]
1417                _cd $portdbpath
1418                # change current phase shown in log
1419                set_phase $target
1420
1421                # Execute pre-run procedure
1422                if {[ditem_contains $ditem prerun]} {
1423                    set result [catch {[ditem_key $ditem prerun] $targetname} errstr]
1424                    # Save variables in order to re-throw the same error code.
1425                    set errcode $::errorCode
1426                    set errinfo $::errorInfo
1427                }
1428
1429                #start tracelib
1430                if {($result ==0
1431                  && [info exists ports_trace]
1432                  && $ports_trace eq "yes"
1433                  && $target ne "clean"
1434                  && $target ne "uninstall")} {
1435                    # uninstall will open a portfile from registry and call
1436                    # deactivate and uninstall there; if we enable trace mode
1437                    # for the first level the two trace threads will conflict
1438                    # and cause a deadlock.
1439                    porttrace::trace_start $workpath
1440
1441                    # Enable the fence to prevent any creation/modification
1442                    # outside the sandbox.
1443                    if {$target ne "activate"
1444                      && $target ne "deactivate"
1445                      && $target ne "archive"
1446                      && $target ne "install"} {
1447                        porttrace::trace_enable_fence
1448                    }
1449
1450                    # collect deps
1451                    set depends {}
1452                    set deptypes {}
1453
1454                    # Determine deptypes to look for based on target
1455                    switch $target {
1456                        fetch       -
1457                        checksum    { set deptypes "depends_fetch" }
1458                        extract     -
1459                        patch       { set deptypes "depends_fetch depends_extract" }
1460                        configure   -
1461                        build       { set deptypes "depends_fetch depends_extract depends_lib depends_build" }
1462
1463                        test        -
1464                        destroot    -
1465                        dmg         -
1466                        pkg         -
1467                        portpkg     -
1468                        mpkg        -
1469                        mdmg        -
1470                        ""          { set deptypes "depends_fetch depends_extract depends_lib depends_build depends_run" }
1471
1472                        # install may be run given an archive, which means
1473                        # depends_fetch, _extract, _build dependencies have
1474                        # never been installed
1475                        activate    -
1476                        install     { set deptypes "depends_lib depends_run" }
1477                    }
1478
1479                    # Gather the dependencies for deptypes
1480                    foreach deptype $deptypes {
1481                        # Add to the list of dependencies if the option exists and isn't empty.
1482                        if {[info exists PortInfo($deptype)] && $PortInfo($deptype) ne ""} {
1483                            set depends [concat $depends $PortInfo($deptype)]
1484                        }
1485                    }
1486
1487                    # Recursively collect all dependencies from registry for tracing
1488                    set deplist {}
1489                    foreach depspec $depends {
1490                        # Resolve dependencies to actual ports
1491                        set name [_get_dep_port $depspec]
1492
1493                        # If portname is empty, the dependency is already satisfied by other means,
1494                        # for example a bin: dependency on a file not installed by MacPorts
1495                        if {$name ne ""} {
1496                            if {[lsearch -exact $deplist $name] == -1} {
1497                                lappend deplist $name
1498                                set deplist [recursive_collect_deps $name $deplist]
1499                            }
1500                        }
1501                    }
1502
1503                    # Add ccache port for access to ${prefix}/bin/ccache binary
1504                    if {[option configure.ccache]} {
1505                        lappend deplist ccache
1506                    }
1507
1508                    ui_debug "Tracemode will respect recursively collected port dependencies: [lsort $deplist]"
1509
1510                    if {[llength $deptypes] > 0} {tracelib setdeps $deplist}
1511                }
1512
1513                if {$result == 0} {
1514                    foreach pre [ditem_key $ditem pre] {
1515                        ui_debug "Executing $pre"
1516                        set result [catch {$pre $targetname} errstr]
1517                        # Save variables in order to re-throw the same error code.
1518                        set errcode $::errorCode
1519                        set errinfo $::errorInfo
1520                        if {$result != 0} { break }
1521                    }
1522                }
1523
1524                if {$result == 0} {
1525                    ui_debug "Executing $targetname ($portname)"
1526                    set result [catch {$procedure $targetname} errstr]
1527                    # Save variables in order to re-throw the same error code.
1528                    set errcode $::errorCode
1529                    set errinfo $::errorInfo
1530                }
1531
1532                if {$result == 0} {
1533                    foreach post [ditem_key $ditem post] {
1534                        ui_debug "Executing $post"
1535                        set result [catch {$post $targetname} errstr]
1536                        # Save variables in order to re-throw the same error code.
1537                        set errcode $::errorCode
1538                        set errinfo $::errorInfo
1539                        if {$result != 0} { break }
1540                    }
1541                }
1542
1543                # Check dependencies & file creations outside workpath.
1544                if {[info exists ports_trace]
1545                  && $ports_trace eq "yes"
1546                  && $target ne "clean"
1547                  && $target ne "uninstall"} {
1548
1549                    tracelib closesocket
1550
1551                    porttrace::trace_check_violations
1552
1553                    # End of trace.
1554                    porttrace::trace_stop
1555                }
1556
1557                # Execute post-run procedure
1558                if {[ditem_contains $ditem postrun] && $result == 0} {
1559                    set postrun [ditem_key $ditem postrun]
1560                    ui_debug "Executing $postrun"
1561                    set result [catch {$postrun $targetname} errstr]
1562                    # Save variables in order to re-throw the same error code.
1563                    set errcode $::errorCode
1564                    set errinfo $::errorInfo
1565                }
1566
1567                # $oldpwd is deleted while uninstalling a port, changing back
1568                # _will_ fail
1569                catch {_cd $oldpwd}
1570            }
1571        }
1572        if {[exists copy_log_files]} {
1573            set log_files [option copy_log_files]
1574            set log_dir [getportlogpath $portpath $subport]
1575            file mkdir $log_dir
1576 
1577            foreach log_file $log_files {
1578                set from "$worksrcpath/$log_file"
1579                if {[file exists $from]} {
1580                    file copy -force $from $log_dir
1581                }
1582            }
1583        }
1584        if {$result == 0} {
1585            # Only write to state file if:
1586            # - we indeed performed this step.
1587            # - this step is not to always be performed
1588            # - this step must be written to file
1589            if {$skipped == 0
1590                && [ditem_key $ditem runtype] ne "always"
1591                && [ditem_key $ditem state] ne "no"} {
1592                write_statefile target $targetname $target_state_fd
1593            }
1594        } else {
1595            if {$errstr ne {}} {
1596                ui_error "Failed to $target $portname: $errstr"
1597            } else {
1598                ui_error "Failed to $target $portname."
1599            }
1600            ui_debug "Error code: $errcode"
1601            ui_debug "Backtrace: $errinfo"
1602            set result 1
1603        }
1604
1605    } else {
1606        ui_info "Warning: $targetname does not have a registered procedure"
1607        set result 1
1608    }
1609
1610    if {[ditem_key $ditem state] ne "no"} {
1611        close $target_state_fd
1612    }
1613
1614    set env(HOME) $savedhome
1615    if {[info exists env(TMPDIR)]} {
1616        unset env(TMPDIR)
1617        if {$macosx_version eq "10.5"} {
1618            unsetenv TMPDIR
1619        }
1620    }
1621
1622    return $result
1623}
1624
1625# recursive dependency search for portname
1626proc recursive_collect_deps {portname {depsfound {}}} \
1627{
1628    # Get the active port from the registry
1629    # There can be only one port active at a time, so take the first result only
1630    set regentry [lindex [registry_active $portname] 0]
1631    # Get port dependencies from the registry
1632    set deplist [registry_list_depends [lindex $regentry 0] [lindex $regentry 1] [lindex $regentry 2] [lindex $regentry 3]]
1633
1634    foreach item $deplist {
1635        set name [lindex $item 0]
1636        if {[lsearch -exact $depsfound $name] == -1} {
1637            lappend depsfound $name
1638            set depsfound [recursive_collect_deps $name $depsfound]
1639        }
1640    }
1641
1642    return $depsfound
1643}
1644
1645
1646proc eval_targets {target} {
1647    global targets target_state_fd subport version revision portvariants epoch ports_dryrun user_options
1648    set dlist $targets
1649
1650    # the statefile will likely be autocleaned away after install,
1651    # so special-case already-completed install and activate
1652    if {[registry_exists $subport $version $revision $portvariants]} {
1653        if {$target eq "install"} {
1654            ui_debug "Skipping $target ($subport) since this port is already installed"
1655            return 0
1656        } elseif {$target eq "activate"} {
1657            set regref [registry_open $subport $version $revision $portvariants ""]
1658            if {[registry_prop_retr $regref active] != 0} {
1659                # Something to close the registry entry may be called here, if it existed.
1660                ui_debug "Skipping $target ($subport @${version}_${revision}${portvariants}) since this port is already active"
1661                return 0
1662            } else {
1663                # run the activate target but ignore its (completed) dependencies
1664                set result [target_run [lindex [dlist_search $dlist provides $target] 0]]
1665                if {[getuid] == 0 && [geteuid] != 0} {
1666                    seteuid 0; setegid 0
1667                }
1668                return $result
1669            }
1670        }
1671    }
1672
1673    # Select the subset of targets under $target
1674    if {$target ne ""} {
1675        set matches [dlist_search $dlist provides $target]
1676
1677        if {[llength $matches] > 0} {
1678            set dlist [dlist_append_dependents $dlist [lindex $matches 0] [list]]
1679            # Special-case 'all'
1680        } elseif {$target ne "all"} {
1681            ui_error "unknown target: $target"
1682            return 1
1683        }
1684    }
1685
1686    set dlist [dlist_eval $dlist "" target_run]
1687
1688    if {[getuid] == 0 && [geteuid] != 0} {
1689        seteuid 0; setegid 0
1690    }
1691
1692    if {[llength $dlist] > 0} {
1693        # somebody broke!
1694        # The phase that failed should have already printed error info; don't
1695        # print useless stuff cluttering the *real* info of the error message.
1696
1697        #set errstring "Warning: targets not executed for $subport:"
1698        #foreach ditem $dlist {
1699        #    append errstring " [ditem_key $ditem name]"
1700        #}
1701        #ui_info $errstring
1702        set result 1
1703    } else {
1704        set result 0
1705    }
1706
1707    return $result
1708}
1709
1710# open_statefile
1711# open file to store name of completed targets
1712proc open_statefile {args} {
1713    global workpath worksymlink place_worksymlink subport portpath ports_ignore_different ports_dryrun \
1714           usealtworkpath altprefix env applications_dir subbuildpath
1715
1716    if {$usealtworkpath} {
1717         ui_warn_once "privileges" "MacPorts running without privileges.\
1718                You may be unable to complete certain actions (e.g. install)."
1719
1720        set newsourcepath "${altprefix}${portpath}"
1721   
1722        # copy Portfile (and patch files) if not there already
1723        # note to maintainers/devs: the original portfile in /opt/local is ALWAYS the one that will be
1724        #    read by macports. The copying of the portfile is done to preserve the symlink provided
1725        #    historically by macports from the portfile directory to the work directory.
1726        #    It is NOT read by MacPorts.
1727        if {![file exists ${newsourcepath}/Portfile] && ![tbool ports_dryrun]} {
1728            file mkdir $newsourcepath
1729            ui_debug "$newsourcepath created"
1730            ui_debug "Going to copy: ${portpath}/Portfile"
1731            file copy ${portpath}/Portfile $newsourcepath
1732            if {[file exists ${portpath}/files] } {
1733                ui_debug "Going to copy: ${portpath}/files"
1734                file copy ${portpath}/files $newsourcepath
1735            }
1736        }
1737    }
1738
1739    if {![tbool ports_dryrun]} {
1740        set need_chown 0
1741        if {![file isdirectory $workpath/.home]} {
1742            file mkdir $workpath/.home
1743            set need_chown 1
1744        }
1745        if {![file isdirectory $workpath/.tmp]} {
1746            file mkdir $workpath/.tmp
1747            set need_chown 1
1748        }
1749        if {$need_chown} {
1750            chownAsRoot $subbuildpath
1751        }
1752        # Create a symlink to the workpath for port authors
1753        if {[tbool place_worksymlink] && ![file isdirectory $worksymlink]} {
1754            ui_debug "Attempting ln -sf $workpath $worksymlink"
1755            ln -sf $workpath $worksymlink
1756        }
1757    }
1758
1759    # de-escalate privileges if MacPorts was started with sudo
1760    dropPrivileges
1761
1762    # flock Portfile
1763    set statefile [file join $workpath .macports.${subport}.state]
1764    set fresh_build yes
1765    set checksum_portfile [sha256 file ${portpath}/Portfile]
1766    if {[file exists $statefile]} {
1767        set fresh_build no
1768        if {![file writable $statefile] && ![tbool ports_dryrun]} {
1769            return -code error "$statefile is not writable - check permission on port directory"
1770        }
1771        if {[file mtime ${portpath}/Portfile] > [clock seconds]} {
1772            return -code error "Portfile is from the future - check date and time of your system"
1773        }
1774        if {![tbool ports_ignore_different]} {
1775            # start by assuming the statefile is current
1776            set portfile_changed no
1777
1778            # open the statefile, determine the statefile version
1779            set readfd [open $statefile r]
1780            set statefile_version 1
1781            if {[get_statefile_value "version" $readfd result] != 0} {
1782                set statefile_version $result
1783            }
1784
1785            # check for outdated statefiles depending on what version the
1786            # statefile is; we explicitly support older statefiles here, because
1787            # all previously built archives would be invalidated (e.g., when
1788            # using mpkg) if we didn't
1789            switch $statefile_version {
1790                1 {
1791                    # statefile version 1
1792                    # this statefile doesn't have a checksum, fall back to
1793                    # comparing the Portfile modification date with the
1794                    # statefile modification date
1795                    if {[file mtime $statefile] < [file mtime ${portpath}/Portfile]} {
1796                        ui_debug "Statefile has version 1 and is older than Portfile"
1797                        set portfile_changed yes
1798                    }
1799                }
1800                2 {
1801                    # statefile version 2
1802                    # this statefile has a sha256 checksum of the Portfile in
1803                    # the "checksum" key
1804                    set checksum_statefile ""
1805                    if {[get_statefile_value "checksum" $readfd checksum_statefile] == 0} {
1806                        ui_warn "Statefile has version 2 but didn't contain a checksum"
1807                        set portfile_changed yes
1808                    } else {
1809                        if {$checksum_portfile != $checksum_statefile} {
1810                            ui_debug "Checksum recorded in statefile '$checksum_statefile' differs from Portfile checksum '$checksum_portfile'"
1811                            set portfile_changed yes
1812                        }
1813                    }
1814                }
1815                default {
1816                    ui_warn "Unsupported statefile version '$statefile_version'"
1817                    ui_warn "Please run 'port selfupdate' to update to the latest version of MacPorts"
1818                }
1819            }
1820            if {[tbool portfile_changed]} {
1821                if {![tbool ports_dryrun]} {
1822                    ui_notice "Portfile changed since last build; discarding previous state."
1823                    chownAsRoot $subbuildpath
1824                    delete $workpath
1825                    file mkdir $workpath
1826                    set fresh_build yes
1827                } else {
1828                    ui_notice "Portfile changed since last build but not discarding previous state (dry run)"
1829                }
1830            }
1831            close $readfd
1832        }
1833    } elseif {[tbool ports_dryrun]} {
1834        set statefile /dev/null
1835    }
1836
1837    set fd [open $statefile a+]
1838    if {![tbool ports_dryrun]} {
1839        if {[catch {flock $fd -exclusive -noblock} result]} {
1840            if {"$result" == "EAGAIN"} {
1841                ui_notice "Waiting for lock on $statefile"
1842                flock $fd -exclusive
1843            } elseif {"$result" == "EOPNOTSUPP"} {
1844                # Locking not supported, just return
1845                return $fd
1846            } else {
1847                return -code error "$result obtaining lock on $statefile"
1848            }
1849        }
1850    }
1851    if {[tbool fresh_build]} {
1852        write_statefile "version" 2 $fd
1853        write_statefile "checksum" $checksum_portfile $fd
1854    }
1855    return $fd
1856}
1857
1858# get_statefile_value
1859# Check for a given $class in the statefile $fd and write the first match to
1860# $result, if any. Returns 1 if a line matched, 0 otherwise
1861proc get_statefile_value {class fd result} {
1862    upvar $result upresult
1863    seek $fd 0
1864    while {[gets $fd line] >= 0} {
1865        if {[regexp "$class: (.*)" $line match value]} {
1866            set upresult $value
1867            return 1
1868        }
1869    }
1870    return 0
1871}
1872
1873# check_statefile
1874# Check completed/selected state of target/variant $name
1875proc check_statefile {class name fd} {
1876    seek $fd 0
1877    while {[gets $fd line] >= 0} {
1878        if {$line == "$class: $name"} {
1879            return 1
1880        }
1881    }
1882    return 0
1883}
1884
1885# write_statefile
1886# Set target $name completed in the state file
1887proc write_statefile {class name fd} {
1888    if {[check_statefile $class $name $fd]} {
1889        return 0
1890    }
1891    seek $fd 0 end
1892    puts $fd "$class: $name"
1893    flush $fd
1894}
1895
1896# Change the value of an existing statefile key
1897# caller must call open_statefile after this
1898proc update_statefile {class name path} {
1899    set fd [open $path r]
1900    while {[gets $fd line] >= 0} {
1901        if {[lindex $line 0] != "${class}:"} {
1902            lappend lines $line
1903        }
1904    }
1905    close $fd
1906    # truncate
1907    set fd [open $path w]
1908    puts $fd "$class: $name"
1909    foreach line $lines {
1910        puts $fd $line
1911    }
1912    close $fd
1913}
1914
1915##
1916# Check that recorded selection of variants match the current selection
1917#
1918# @param variations input array name of new variants
1919# @param oldvariations output array name of old variants
1920# @param fd file descriptor of the state file
1921# @return 0 if variants match, 1 otherwise
1922proc check_statefile_variants {variations oldvariations fd} {
1923    upvar $variations upvariations
1924    upvar $oldvariations upoldvariations
1925
1926    array set upoldvariations {}
1927
1928    set variants_found no
1929    set targets_found no
1930    seek $fd 0
1931    while {[gets $fd line] >= 0} {
1932        if {[regexp "variant: (.*)" $line match name]} {
1933            set upoldvariations([string range $name 1 end]) [string range $name 0 0]
1934            set variants_found yes
1935        }
1936        if {[regexp "target: .*" $line]} {
1937            set targets_found yes
1938        }
1939    }
1940
1941    if {![tbool variants_found] && ![tbool targets_found]} {
1942        # Statefile is "empty", skipping further tests
1943        return 0
1944    }
1945
1946    set mismatch 0
1947    if {[array size upoldvariations] != [array size upvariations]} {
1948        set mismatch 1
1949    } else {
1950        foreach key [array names upvariations *] {
1951            if {![info exists upoldvariations($key)] || $upvariations($key) != $upoldvariations($key)} {
1952                set mismatch 1
1953                break
1954            }
1955        }
1956    }
1957
1958    return $mismatch
1959}
1960
1961########### Port Variants ###########
1962
1963# Each variant which provides a subset of the requested variations
1964# will be chosen.  Returns a list of the selected variants.
1965proc choose_variants {dlist variations} {
1966    upvar $variations upvariations
1967
1968    set selected [list]
1969    set negated [list]
1970
1971    foreach ditem $dlist {
1972        # Enumerate through the provides, tallying the pros and cons.
1973        set pros 0
1974        set cons 0
1975        set ignored 0
1976        foreach flavor [ditem_key $ditem provides] {
1977            if {[info exists upvariations($flavor)]} {
1978                if {$upvariations($flavor) eq "+"} {
1979                    incr pros
1980                } elseif {$upvariations($flavor) eq "-"} {
1981                    incr cons
1982                }
1983            } else {
1984                incr ignored
1985            }
1986        }
1987
1988        if {$cons > 0} {
1989            lappend negated $ditem
1990        }
1991        if {$pros > 0 && $ignored == 0} {
1992            lappend selected $ditem
1993        }
1994    }
1995    return [list $selected $negated]
1996}
1997
1998proc variant_run {ditem} {
1999    set name [ditem_key $ditem name]
2000    ui_debug "Executing variant $name provides [ditem_key $ditem provides]"
2001
2002    # test for conflicting variants
2003    foreach v [ditem_key $ditem conflicts] {
2004        if {[variant_isset $v]} {
2005            ui_error "[option name]: Variant $name conflicts with $v"
2006            return 1
2007        }
2008    }
2009
2010    # execute proc with same name as variant.
2011    if {[catch "variant-${name}" result]} {
2012        global errorInfo
2013        ui_debug "$errorInfo"
2014        ui_error "[option name]: Error executing $name: $result"
2015        return 1
2016    }
2017    return 0
2018}
2019
2020# Given a list of variant specifications, return a canonical string form
2021# for the registry.
2022    # The strategy is as follows: regardless of how some collection of variants
2023    # was turned on or off, a particular instance of the port is uniquely
2024    # characterized by the set of variants that are *on*. Thus, record those
2025    # variants in a string in a standard order as +var1+var2 etc.
2026    # Can also do the same for -variants, for recording the negated list.
2027proc canonicalize_variants {variants {sign "+"}} {
2028    array set vara $variants
2029    set result ""
2030    set vlist [lsort -ascii [array names vara]]
2031    foreach v $vlist {
2032        if {$vara($v) == $sign} {
2033            append result "${sign}${v}"
2034        }
2035    }
2036    return $result
2037}
2038
2039proc eval_variants {variations} {
2040    global all_variants ports_force PortInfo requested_variations portvariants negated_variants
2041    set dlist $all_variants
2042    upvar $variations upvariations
2043    set chosen [choose_variants $dlist upvariations]
2044    set negated [lindex $chosen 1]
2045    set chosen [lindex $chosen 0]
2046    set portname [option subport]
2047
2048    # Check to make sure the requested variations are available with this
2049    # port, if one is not, warn the user and remove the variant from the
2050    # array.
2051    # Save the originally requested set in requested_variations.
2052    array set requested_variations [array get upvariations]
2053    foreach key [array names upvariations *] {
2054        if {![info exists PortInfo(variants)] ||
2055            [lsearch $PortInfo(variants) $key] == -1} {
2056            ui_debug "Requested variant $upvariations($key)$key is not provided by port $portname."
2057            array unset upvariations $key
2058        }
2059    }
2060
2061    # now that we've selected variants, change all provides [a b c] to [a-b-c]
2062    # this will eliminate ambiguity between item a, b, and a-b while fulfilling requirements.
2063    #foreach obj $dlist {
2064    #    $obj set provides [list [join [$obj get provides] -]]
2065    #}
2066
2067    set newlist [list]
2068    foreach variant $chosen {
2069        set newlist [dlist_append_dependents $dlist $variant $newlist]
2070    }
2071
2072    set dlist [dlist_eval $newlist "" variant_run]
2073    if {[llength $dlist] > 0} {
2074        return 1
2075    }
2076
2077    # Now compute the true active array of variants. Note we do not
2078    # change upvariations any further, since that represents the
2079    # requested list of variations; but the registry for consistency
2080    # must encode the actual list of variants evaluated, however that
2081    # came to pass (dependencies, defaults, etc.) While we're at it,
2082    # it's convenient to check for inconsistent requests for
2083    # variations, namely foo +requirer -required where the 'requirer'
2084    # variant requires the 'required' one.
2085    set activevariants [list]
2086    foreach dvar $newlist {
2087        set thevar [ditem_key $dvar provides]
2088        if {[info exists upvariations($thevar)] && $upvariations($thevar) eq "-"} {
2089            set chosenlist ""
2090            foreach choice $chosen {
2091                lappend chosenlist +[ditem_key $choice provides]
2092            }
2093            ui_error "Inconsistent variant specification: $portname variant +$thevar is required by at least one of $chosenlist, but specified -$thevar"
2094            return 1
2095        }
2096        lappend activevariants $thevar "+"
2097    }
2098
2099    # Record a canonical variant string, used e.g. in accessing the registry
2100    set portvariants [canonicalize_variants $activevariants]
2101
2102    # Make this important information visible in PortInfo
2103    set PortInfo(active_variants) $activevariants
2104    set PortInfo(canonical_active_variants) $portvariants
2105
2106    # now set the negated variants
2107    set negated_list [list]
2108    foreach dvar $negated {
2109        set thevar [ditem_key $dvar provides]
2110        lappend negated_list $thevar "-"
2111    }
2112    set negated_variants [canonicalize_variants $negated_list "-"]
2113
2114    return 0
2115}
2116
2117proc check_variants {target} {
2118    global targets ports_force ports_dryrun PortInfo
2119    set result 0
2120    array set variations $PortInfo(active_variants)
2121
2122    # Make sure the variations match those stored in the statefile.
2123    # If they don't match, print an error indicating a 'port clean'
2124    # should be performed.
2125    # - Skip this test if the target indicated target_state no
2126    # - Skip this test if the statefile is empty.
2127    # - Skip this test if ports_force was specified.
2128
2129    # Assume we do not need the statefile
2130    set statereq 0
2131    set ditems [dlist_search $targets provides $target]
2132    if {[llength $ditems] > 0} {
2133        set ditems [dlist_append_dependents $targets [lindex $ditems 0] [list]]
2134    }
2135    foreach d $ditems {
2136        if {[ditem_key $d state] ne "no"} {
2137            # At least one matching target requires the state file
2138            set statereq 1
2139            break
2140        }
2141    }
2142    if { $statereq &&
2143        !([info exists ports_force] && $ports_force eq "yes")} {
2144
2145        set state_fd [open_statefile]
2146
2147        array set oldvariations {}
2148        if {[check_statefile_variants variations oldvariations $state_fd]} {
2149            ui_error "Requested variants \"[canonicalize_variants [array get variations]]\" do not match original selection \"[canonicalize_variants [array get oldvariations]]\"."
2150            ui_error "Please use the same variants again, perform 'port clean [option subport]' or specify the force option (-f)."
2151            set result 1
2152        } elseif {!([info exists ports_dryrun] && $ports_dryrun eq "yes")} {
2153            # Write variations out to the statefile
2154            foreach key [array names variations *] {
2155                write_statefile variant $variations($key)$key $state_fd
2156            }
2157        }
2158
2159        close $state_fd
2160    }
2161
2162    return $result
2163}
2164
2165# add the default universal variant if appropriate
2166proc universal_setup {args} {
2167    if {[variant_exists universal]} {
2168        ui_debug "universal variant already exists, so not adding the default one"
2169    } elseif {[exists universal_variant] && ![option universal_variant]} {
2170        ui_debug "universal_variant is false, so not adding the default universal variant"
2171    } elseif {[exists use_xmkmf] && [option use_xmkmf]} {
2172        ui_debug "using xmkmf, so not adding the default universal variant"
2173    } elseif {![exists os.universal_supported] || ![option os.universal_supported]} {
2174        ui_debug "OS doesn't support universal builds, so not adding the default universal variant"
2175    } elseif {[llength [option supported_archs]] == 1} {
2176        ui_debug "only one arch supported, so not adding the default universal variant"
2177    } else {
2178        ui_debug "adding the default universal variant"
2179        variant universal {}
2180    }
2181}
2182
2183# Target class definition.
2184
2185# constructor for target object
2186proc target_new {name procedure} {
2187    global targets
2188    set ditem [ditem_create]
2189
2190    ditem_key $ditem name $name
2191    ditem_key $ditem procedure $procedure
2192
2193    lappend targets $ditem
2194
2195    return $ditem
2196}
2197
2198proc target_provides {ditem args} {
2199    global targets
2200    # Register the pre-/post- hooks for use in Portfile.
2201    # Portfile syntax: pre-fetch { puts "hello world" }
2202    # User-code exceptions are caught and returned as a result of the target.
2203    # Thus if the user code breaks, dependent targets will not execute.
2204    foreach target $args {
2205        set origproc [ditem_key $ditem procedure]
2206        set ident [ditem_key $ditem name]
2207        if {[info commands $target] eq ""} {
2208            proc $target {code} "
2209                variable proc_index
2210                set proc_index \[llength \[ditem_key $ditem proc\]\]
2211                ditem_key $ditem procedure proc-${ident}-${target}-\${proc_index}
2212                proc proc-${ident}-${target}-\${proc_index} {name} \"
2213                    if {\\\[catch userproc-${ident}-${target}-\${proc_index} result\\\]} {
2214                        return -code error \\\$result
2215                    } else {
2216                        return 0
2217                    }
2218                \"
2219                proc do-$target {} { $origproc $target }
2220                makeuserproc userproc-${ident}-${target}-\${proc_index} \$code
2221            "
2222        }
2223        proc pre-$target {code} "
2224            variable proc_index
2225            set proc_index \[llength \[ditem_key $ditem pre\]\]
2226            ditem_append $ditem pre proc-pre-${ident}-${target}-\${proc_index}
2227            proc proc-pre-${ident}-${target}-\${proc_index} {name} \"
2228                if {\\\[catch userproc-pre-${ident}-${target}-\${proc_index} result\\\]} {
2229                    return -code error \\\$result
2230                } else {
2231                    return 0
2232                }
2233            \"
2234            makeuserproc userproc-pre-${ident}-${target}-\${proc_index} \$code
2235        "
2236        proc post-$target {code} "
2237            variable proc_index
2238            set proc_index \[llength \[ditem_key $ditem post\]\]
2239            ditem_append $ditem post proc-post-${ident}-${target}-\${proc_index}
2240            proc proc-post-${ident}-${target}-\${proc_index} {name} \"
2241                if {\\\[catch userproc-post-${ident}-${target}-\${proc_index} result\\\]} {
2242                    return -code error \\\$result
2243                } else {
2244                    return 0
2245                }
2246            \"
2247            makeuserproc userproc-post-${ident}-${target}-\${proc_index} \$code
2248        "
2249    }
2250    ditem_append $ditem provides {*}$args
2251}
2252
2253proc target_requires {ditem args} {
2254    ditem_append $ditem requires {*}$args
2255}
2256
2257proc target_uses {ditem args} {
2258    ditem_append $ditem uses {*}$args
2259}
2260
2261proc target_deplist {ditem args} {
2262    ditem_append $ditem deplist {*}$args
2263}
2264
2265proc target_prerun {ditem args} {
2266    ditem_append $ditem prerun {*}$args
2267}
2268
2269proc target_postrun {ditem args} {
2270    ditem_append $ditem postrun {*}$args
2271}
2272
2273proc target_runtype {ditem args} {
2274    ditem_append $ditem runtype {*}$args
2275}
2276
2277proc target_state {ditem args} {
2278    ditem_append $ditem state {*}$args
2279}
2280
2281proc target_init {ditem args} {
2282    ditem_append $ditem init {*}$args
2283}
2284
2285##### variant class #####
2286
2287# constructor for variant objects
2288proc variant_new {name} {
2289    set ditem [ditem_create]
2290    ditem_key $ditem name $name
2291    return $ditem
2292}
2293
2294proc handle_default_variants {option action {value ""}} {
2295    global PortInfo variations
2296    switch -regex $action {
2297        set|append {
2298            # Retrieve the information associated with each variant.
2299            if {![info exists PortInfo(vinfo)]} {
2300                set PortInfo(vinfo) {}
2301            }
2302            array set vinfo $PortInfo(vinfo)
2303            foreach v $value {
2304                if {[regexp {([-+])([-A-Za-z0-9_]+)} $v whole val variant] && ![info exists variations($variant)]} {
2305                    # Retrieve the information associated with this variant.
2306                    if {![info exists vinfo($variant)]} {
2307                        set vinfo($variant) {}
2308                    }
2309                    array unset info
2310                    array set info $vinfo($variant)
2311                    # Set is_default and update vinfo.
2312                    set info(is_default) $val
2313                    array set vinfo [list $variant [array get info]]
2314
2315                    set variations($variant) $val
2316                }
2317            }
2318            # Update PortInfo(vinfo).
2319            set PortInfo(vinfo) [array get vinfo]
2320        }
2321        delete {
2322            # xxx
2323        }
2324    }
2325}
2326
2327# create all users/groups listed in the add_users option
2328# format: [username [option=value ...] ...]
2329proc handle_add_users {} {
2330    set cur ""
2331    foreach val [option add_users] {
2332        if {[string match "*=*" $val] && $cur ne ""} {
2333            set split_arg [split $val =]
2334            if {[lindex $split_arg 0] eq "group"} {
2335                set groupname [lindex $split_arg 1]
2336                addgroup $groupname
2337                lappend args($cur) gid=[existsgroup $groupname]
2338            } else {
2339                lappend args($cur) $val
2340            }
2341        } else {
2342            set cur $val
2343        }
2344    }
2345    foreach username [array names args] {
2346        adduser $username {*}$args($username)
2347    }
2348}
2349
2350proc adduser {name args} {
2351    global os.platform
2352
2353    if {[getuid] != 0} {
2354        ui_warn "adduser only works when running as root."
2355        ui_warn "The requested user '$name' was not created."
2356        return
2357    } elseif {[geteuid] != 0} {
2358        seteuid 0; setegid 0
2359        set escalated 1
2360    }
2361
2362    set passwd {*}
2363    set uid [nextuid]
2364    set gid [existsgroup nogroup]
2365    set realname ${name}
2366    set home /var/empty
2367    set shell /usr/bin/false
2368
2369    foreach arg $args {
2370        if {[regexp {([a-z]*)=(.*)} $arg match key val]} {
2371            set $key $val
2372        }
2373    }
2374
2375    if {[existsuser ${name}] != -1 || [existsuser ${uid}] != -1} {
2376        return
2377    }
2378
2379    if {${os.platform} eq "darwin"} {
2380        set dscl [findBinary dscl $portutil::autoconf::dscl_path]
2381        set failed? 0
2382        try {
2383            exec -ignorestderr $dscl . -create /Users/${name} UniqueID ${uid}
2384
2385            # These are implicitly added on Mac OSX Lion.  AuthenticationAuthority
2386            # causes the user to be visible in the Users & Groups Preference Pane,
2387            # and the others are just noise, so delete them.
2388            # https://trac.macports.org/ticket/30168
2389            exec -ignorestderr $dscl . -delete /Users/${name} AuthenticationAuthority
2390            exec -ignorestderr $dscl . -delete /Users/${name} PasswordPolicyOptions
2391            exec -ignorestderr $dscl . -delete /Users/${name} dsAttrTypeNative:KerberosKeys
2392            exec -ignorestderr $dscl . -delete /Users/${name} dsAttrTypeNative:ShadowHashData
2393
2394            exec -ignorestderr $dscl . -create /Users/${name} RealName ${realname}
2395            exec -ignorestderr $dscl . -create /Users/${name} Password ${passwd}
2396            exec -ignorestderr $dscl . -create /Users/${name} PrimaryGroupID ${gid}
2397            exec -ignorestderr $dscl . -create /Users/${name} NFSHomeDirectory ${home}
2398            exec -ignorestderr $dscl . -create /Users/${name} UserShell ${shell}
2399        } catch {{CHILDKILLED *} eCode eMessage} {
2400            # the foreachs are a simple workaround for Tcl 8.4, which doesn't
2401            # seem to have lassign
2402            foreach {- pid sigName msg} $eCode {
2403                ui_error "dscl($pid) was killed by $sigName: $msg"
2404                ui_debug "dscl printed: $eMessage"
2405            }
2406
2407            set failed? 1
2408        } catch {{CHILDSTATUS *} eCode eMessage} {
2409            foreach {- pid code} $eCode {
2410                ui_error "dscl($pid) termined with an exit status of $code"
2411                ui_debug "dscl printed: $eMessage"
2412            }
2413           
2414            set failed? 1
2415        } catch {{POSIX *} eCode eMessage} {
2416            foreach {- errName msg} {
2417                ui_error "failed to execute $dscl: $errName: $msg"
2418                ui_debug "dscl printed: $eMessage"
2419            }
2420
2421            set failed? 1
2422        } finally {
2423            if {${failed?}} {
2424                # creating the user properly failed and we're bailing out
2425                # anyway, try to delete the half-created user to revert to the
2426                # state before the error
2427                ui_debug "Attempting to clean up failed creation of user $name"
2428                try {
2429                    exec -ignorestderr $dscl . -delete /Users/${name}
2430                } catch {{CHILDKILLED *} eCode eMessage} {
2431                    foreach {- pid sigName msg} {
2432                        ui_warn "dscl($pid) was killed by $sigName: $msg while trying to clean up failed creation of user $name."
2433                        ui_debug "dscl printed: $eMessage"
2434                    }
2435                } catch {{CHILDSTATUS *} eCode eMessage} {
2436                    # ignoring childstatus failure, because that probably means
2437                    # the first call failed and the user wasn't even created
2438                } catch {{POSIX *} eCode eMessage} {
2439                    foreach {- errName msg} {
2440                        ui_warn "failed to execute $dscl: $errName: $msg while trying to clean up failed creation of user $name."
2441                        ui_debug "dscl printed: $eMessage"
2442                    }
2443                }
2444
2445                # and raise an error to abort
2446                error "dscl failed to create required user $name."
2447            }
2448        }
2449    } else {
2450        # XXX adduser is only available for darwin, add more support here
2451        ui_warn "adduser is not implemented on ${os.platform}."
2452        ui_warn "The requested user '$name' was not created."
2453    }
2454
2455    if {[info exists escalated]} {
2456        dropPrivileges
2457    }
2458}
2459
2460proc addgroup {name args} {
2461    global os.platform
2462
2463    if {[getuid] != 0} {
2464        ui_warn "addgroup only works when running as root."
2465        ui_warn "The requested group '$name' was not created."
2466        return
2467    } elseif {[geteuid] != 0} {
2468        seteuid 0; setegid 0
2469        set escalated 1
2470    }
2471
2472    set gid [nextgid]
2473    set realname ${name}
2474    set passwd {*}
2475    set users ""
2476
2477    foreach arg $args {
2478        if {[regexp {([a-z]*)=(.*)} $arg match key val]} {
2479            set $key $val
2480        }
2481    }
2482
2483    if {[existsgroup ${name}] != -1 || [existsgroup ${gid}] != -1} {
2484        return
2485    }
2486
2487    if {${os.platform} eq "darwin"} {
2488        set dscl [findBinary dscl $portutil::autoconf::dscl_path]
2489        set failed? 0
2490        try {
2491            exec -ignorestderr $dscl . -create /Groups/${name} Password ${passwd}
2492            exec -ignorestderr $dscl . -create /Groups/${name} RealName ${realname}
2493            exec -ignorestderr $dscl . -create /Groups/${name} PrimaryGroupID ${gid}
2494            if {${users} ne ""} {
2495                exec -ignorestderr $dscl . -create /Groups/${name} GroupMembership ${users}
2496            }
2497        } catch {{CHILDKILLED *} eCode eMessage} {
2498            # the foreachs are a simple workaround for Tcl 8.4, which doesn't
2499            # seem to have lassign
2500            foreach {- pid sigName msg} $eCode {
2501                ui_error "dscl($pid) was killed by $sigName: $msg"
2502                ui_debug "dscl printed: $eMessage"
2503            }
2504
2505            set failed? 1
2506        } catch {{CHILDSTATUS *} eCode eMessage} {
2507            foreach {- pid code} $eCode {
2508                ui_error "dscl($pid) termined with an exit status of $code"
2509                ui_debug "dscl printed: $eMessage"
2510            }
2511           
2512            set failed? 1
2513        } catch {{POSIX *} eCode eMessage} {
2514            foreach {- errName msg} {
2515                ui_error "failed to execute $dscl: $errName: $msg"
2516                ui_debug "dscl printed: $eMessage"
2517            }
2518
2519            set failed? 1
2520        } finally {
2521            if {${failed?}} {
2522                # creating the user properly failed and we're bailing out
2523                # anyway, try to delete the half-created user to revert to the
2524                # state before the error
2525                ui_debug "Attempting to clean up failed creation of group $name"
2526                try {
2527                    exec -ignorestderr $dscl . -delete /Groups/${name}
2528                } catch {{CHILDKILLED *} eCode eMessage} {
2529                    foreach {- pid sigName msg} {
2530                        ui_warn "dscl($pid) was killed by $sigName: $msg while trying to clean up failed creation of group $name."
2531                        ui_debug "dscl printed: $eMessage"
2532                    }
2533                } catch {{CHILDSTATUS *} eCode eMessage} {
2534                    # ignoring childstatus failure, because that probably means
2535                    # the first call failed and the user wasn't even created
2536                } catch {{POSIX *} eCode eMessage} {
2537                    foreach {- errName msg} {
2538                        ui_warn "failed to execute $dscl: $errName: $msg while trying to clean up failed creation of group $name."
2539                        ui_debug "dscl printed: $eMessage"
2540                    }
2541                }
2542
2543                # and raise an error to abort
2544                error "dscl failed to create required group $name."
2545            }
2546        }
2547    } else {
2548        # XXX addgroup is only available for darwin, add more support here
2549        ui_warn "addgroup is not implemented on ${os.platform}."
2550        ui_warn "The requested group was not created."
2551    }
2552
2553    if {[info exists escalated]} {
2554        dropPrivileges
2555    }
2556}
2557
2558# proc to calculate size of a directory
2559# moved here from portpkg.tcl
2560proc dirSize {dir} {
2561    set size    0;
2562    foreach file [readdir $dir] {
2563        if {[file type [file join $dir $file]] == "link" } {
2564            continue
2565        }
2566        if {[file isdirectory [file join $dir $file]]} {
2567            incr size [dirSize [file join $dir $file]]
2568        } else {
2569            incr size [file size [file join $dir $file]];
2570        }
2571    }
2572    return $size;
2573}
2574
2575# Set the UI prefix to something standard (so it can be grepped for in output)
2576proc set_ui_prefix {} {
2577    global UI_PREFIX env
2578    if {[info exists env(UI_PREFIX)]} {
2579        set UI_PREFIX $env(UI_PREFIX)
2580    } else {
2581        set UI_PREFIX "---> "
2582    }
2583}
2584
2585# Use a specified group/version.
2586proc PortGroup {group version} {
2587    global porturl PortInfo _portgroup_search_dirs
2588
2589    lappend PortInfo(portgroups) [list $group $version]
2590
2591    if {[info exists _portgroup_search_dirs]} {
2592        foreach dir $_portgroup_search_dirs {
2593            set groupFile ${dir}/${group}-${version}.tcl
2594            if {[file exists $groupFile]} {
2595                uplevel "source $groupFile"
2596                ui_debug "Sourcing PortGroup $group $version from $groupFile"
2597                return
2598            }
2599        }
2600    }
2601
2602    set groupFile [getportresourcepath $porturl "port1.0/group/${group}-${version}.tcl"]
2603
2604    if {[file exists $groupFile]} {
2605        uplevel "source $groupFile"
2606        ui_debug "Sourcing PortGroup $group $version from $groupFile"
2607    } else {
2608        ui_warn "PortGroup ${group} ${version} could not be located. ${group}-${version}.tcl does not exist."
2609    }
2610}
2611
2612# return filename of the archive for this port
2613proc get_portimage_name {} {
2614    global portdbpath subport version revision portvariants os.platform os.major portarchivetype
2615    set ret "${subport}-${version}_${revision}${portvariants}.${os.platform}_${os.major}.[join [get_canonical_archs] -].${portarchivetype}"
2616    # should really look up NAME_MAX here, but it's 255 for all OS X so far
2617    # (leave 10 chars for an extension like .rmd160 on the sig file)
2618    if {[string length $ret] > 245 && ${portvariants} != ""} {
2619        # try hashing the variants
2620        set ret "${subport}-${version}_${revision}+[rmd160 string ${portvariants}].${os.platform}_${os.major}.[join [get_canonical_archs] -].${portarchivetype}"
2621    }
2622    if {[string length $ret] > 245} {
2623        error "filename too long: $ret"
2624    }
2625    return $ret
2626}
2627
2628# return path where a newly created image/archive for this port will be stored
2629proc get_portimage_path {} {
2630    global portdbpath subport
2631    return [file normalize [file join ${portdbpath} software ${subport} [get_portimage_name]]]
2632}
2633
2634# return list of archive types that we can extract
2635proc supportedArchiveTypes {} {
2636    global supported_archive_types
2637    if {![info exists supported_archive_types]} {
2638        set supported_archive_types {}
2639        foreach type {tbz2 tbz tgz tar txz tlz xar zip cpgz cpio} {
2640            if {[catch {archiveTypeIsSupported $type}] == 0} {
2641                lappend supported_archive_types $type
2642            }
2643        }
2644    }
2645    return $supported_archive_types
2646}
2647
2648# return path to a downloaded or installed archive for this port
2649proc find_portarchive_path {} {
2650    global portdbpath subport version revision portvariants force_archive_refresh
2651    set installed 0
2652    if {[registry_exists $subport $version $revision $portvariants]} {
2653        set installed 1
2654    }
2655    set archiverootname [file rootname [get_portimage_name]]
2656    foreach unarchive.type [supportedArchiveTypes] {
2657        set fullarchivename "${archiverootname}.${unarchive.type}"
2658        if {$installed && ![tbool force_archive_refresh]} {
2659            set fullarchivepath [file join $portdbpath software $subport $fullarchivename]
2660        } else {
2661            set fullarchivepath [file join $portdbpath incoming/verified $fullarchivename]
2662        }
2663        if {[file isfile $fullarchivepath]} {
2664            return $fullarchivepath
2665        }
2666    }
2667    return ""
2668}
2669
2670# check if archive type is supported by current system
2671# returns an error code if it is not
2672proc archiveTypeIsSupported {type} {
2673    global os.platform os.version
2674    set errmsg ""
2675    switch -regex $type {
2676        cp(io|gz) {
2677            set pax "pax"
2678            if {[catch {set pax [findBinary $pax ${portutil::autoconf::pax_path}]} errmsg] == 0} {
2679                if {[regexp {z$} $type]} {
2680                    set gzip "gzip"
2681                    if {[catch {set gzip [findBinary $gzip ${portutil::autoconf::gzip_path}]} errmsg] == 0} {
2682                        return 0
2683                    }
2684                } else {
2685                    return 0
2686                }
2687            }
2688        }
2689        t(ar|bz|lz|xz|gz) {
2690            set tar "tar"
2691            if {[catch {set tar [findBinary $tar ${portutil::autoconf::tar_path}]} errmsg] == 0} {
2692                if {[regexp {z2?$} $type]} {
2693                    if {[regexp {bz2?$} $type]} {
2694                        set gzip "bzip2"
2695                    } elseif {[regexp {lz$} $type]} {
2696                        set gzip "lzma"
2697                    } elseif {[regexp {xz$} $type]} {
2698                        set gzip "xz"
2699                    } else {
2700                        set gzip "gzip"
2701                    }
2702                    if {[info exists portutil::autoconf::${gzip}_path]} {
2703                        set hint [set portutil::autoconf::${gzip}_path]
2704                    } else {
2705                        set hint ""
2706                    }
2707                    if {[catch {set gzip [findBinary $gzip $hint]} errmsg] == 0} {
2708                        return 0
2709                    }
2710                } else {
2711                    return 0
2712                }
2713            }
2714        }
2715        xar {
2716            set xar "xar"
2717            if {[catch {set xar [findBinary $xar ${portutil::autoconf::xar_path}]} errmsg] == 0} {
2718                return 0
2719            }
2720        }
2721        zip {
2722            set zip "zip"
2723            if {[catch {set zip [findBinary $zip ${portutil::autoconf::zip_path}]} errmsg] == 0} {
2724                set unzip "unzip"
2725                if {[catch {set unzip [findBinary $unzip ${portutil::autoconf::unzip_path}]} errmsg] == 0} {
2726                    return 0
2727                }
2728            }
2729        }
2730        default {
2731            return -code error [format [msgcat::mc "Invalid port archive type '%s' specified!"] $type]
2732        }
2733    }
2734    return -code error [format [msgcat::mc "Unsupported port archive type '%s': %s"] $type $errmsg]
2735}
2736
2737# return the specified piece of metadata from the +CONTENTS file in the given archive
2738proc extract_archive_metadata {archive_location archive_type metadata_type} {
2739    set qflag ${portutil::autoconf::tar_q}
2740    set raw_contents ""
2741
2742    switch -- $archive_type {
2743        xar -
2744        cpgz -
2745        cpio {
2746            set twostep 1
2747            global workpath
2748            if {[file isdirectory ${workpath}/.tmp]} {
2749                set tempdir [mkdtemp ${workpath}/.tmp/portarchiveXXXXXXXX]
2750            } else {
2751                set tempdir [mkdtemp /tmp/portarchiveXXXXXXXX]
2752            }
2753        }
2754    }
2755
2756    switch -- $archive_type {
2757        tbz -
2758        tbz2 {
2759            set raw_contents [exec -ignorestderr [findBinary tar ${portutil::autoconf::tar_path}] -xOj${qflag}f $archive_location ./+CONTENTS]
2760        }
2761        tgz {
2762            set raw_contents [exec -ignorestderr [findBinary tar ${portutil::autoconf::tar_path}] -xOz${qflag}f $archive_location ./+CONTENTS]
2763        }
2764        tar {
2765            set raw_contents [exec -ignorestderr [findBinary tar ${portutil::autoconf::tar_path}] -xO${qflag}f $archive_location ./+CONTENTS]
2766        }
2767        txz {
2768            set raw_contents [exec -ignorestderr [findBinary tar ${portutil::autoconf::tar_path}] -xO${qflag}f $archive_location --use-compress-program [findBinary xz ""] ./+CONTENTS]
2769        }
2770        tlz {
2771            set raw_contents [exec -ignorestderr [findBinary tar ${portutil::autoconf::tar_path}] -xO${qflag}f $archive_location --use-compress-program [findBinary lzma ""] ./+CONTENTS]
2772        }
2773        xar {
2774            system -W ${tempdir} "[findBinary xar ${portutil::autoconf::xar_path}] -xf $archive_location +CONTENTS"
2775        }
2776        zip {
2777            set raw_contents [exec -ignorestderr [findBinary unzip ${portutil::autoconf::unzip_path}] -p $archive_location +CONTENTS]
2778        }
2779        cpgz {
2780            system -W ${tempdir} "[findBinary pax ${portutil::autoconf::pax_path}] -rzf $archive_location +CONTENTS"
2781        }
2782        cpio {
2783            system -W ${tempdir} "[findBinary pax ${portutil::autoconf::pax_path}] -rf $archive_location +CONTENTS"
2784        }
2785    }
2786    if {[info exists twostep]} {
2787        set fd [open "${tempdir}/+CONTENTS"]
2788        set raw_contents [read $fd]
2789        close $fd
2790        file delete -force $tempdir
2791    }
2792    if {$metadata_type eq "contents"} {
2793        set contents {}
2794        set ignore 0
2795        set sep [file separator]
2796        foreach line [split $raw_contents \n] {
2797            if {$ignore} {
2798                set ignore 0
2799                continue
2800            }
2801            if {[string index $line 0] != "@"} {
2802                lappend contents "${sep}${line}"
2803            } elseif {$line == "@ignore"} {
2804                set ignore 1
2805            }
2806        }
2807        return $contents
2808    } elseif {$metadata_type eq "portname"} {
2809        foreach line [split $raw_contents \n] {
2810            if {[lindex $line 0] == "@portname"} {
2811                return [lindex $line 1]
2812            }
2813        }
2814        return ""
2815    } else {
2816        return -code error "unknown metadata_type: $metadata_type"
2817    }
2818}
2819
2820#
2821# merge function for universal builds
2822#
2823
2824# private function
2825# merge_lipo base-path target-path relative-path architectures
2826# e.g. 'merge_lipo ${workpath}/pre-dest ${destroot} ${prefix}/bin/pstree i386 ppc
2827# will merge binary files with lipo which have to be in the same (relative) path
2828proc merge_lipo {base target file archs} {
2829    set exec-lipo [list [findBinary lipo $portutil::autoconf::lipo_path]]
2830    foreach arch ${archs} {
2831        lappend exec-lipo -arch ${arch} ${base}/${arch}${file}
2832    }
2833    exec {*}${exec-lipo} -create -output ${target}${file}
2834}
2835
2836# private function
2837# merge C/C++/.. files
2838# either just copy (if equivalent) or add CPP directive for differences
2839# should work for C++, C, Obj-C, Obj-C++ files and headers
2840proc merge_cpp {base target file archs} {
2841    merge_file $base $target $file $archs
2842    # TODO -- instead of just calling merge_file:
2843    # check if different
2844    #   no: copy
2845    #   yes: merge with #elif defined(__i386__) (__x86_64__, __ppc__, __ppc64__)
2846}
2847
2848# private function
2849# merge_file base-path target-path relative-path architectures
2850# e.g. 'merge_file ${workpath}/pre-dest ${destroot} ${prefix}/share/man/man1/port.1 i386 ppc
2851# will test equivalence of files and copy them if they are the same (for the different architectures)
2852proc merge_file {base target file archs} {
2853    set basearch [lindex ${archs} 0]
2854    ui_debug "ba: '${basearch}' ('${archs}')"
2855    foreach arch [lrange ${archs} 1 end] {
2856        # checking for differences; TODO: error more gracefully on non-equal files
2857        exec [findBinary diff $portutil::autoconf::diff_path] "-q" "${base}/${basearch}${file}" "${base}/${arch}${file}"
2858    }
2859    ui_debug "ba: '${basearch}'"
2860    file copy "${base}/${basearch}${file}" "${target}${file}"
2861}
2862
2863# merges multiple "single-arch" destroots into the final destroot
2864# 'base' is the path where the different directories (one for each arch) are
2865# e.g. call 'merge ${workpath}/pre-dest' with having a destroot in ${workpath}/pre-dest/i386 and ${workpath}/pre-dest/ppc64 -- single arch -- each
2866proc merge {base} {
2867    global destroot configure.universal_archs
2868
2869    # test which architectures are available, set one as base-architecture
2870    set archs ""
2871    set base_arch ""
2872    foreach arch ${configure.universal_archs} {
2873        if {[file exists "${base}/${arch}"]} {
2874            set archs [concat ${archs} ${arch}]
2875            set base_arch ${arch}
2876        }
2877    }
2878    if {"" == ${base_arch}} {
2879        return -code error [format [msgcat::mc "Cannot merge because directory '%s' contains no architecture directories."] ${base}]
2880    }
2881    ui_debug "merging architectures ${archs}, base_arch is ${base_arch}"
2882
2883    # traverse the base-architecture directory
2884    set basepath "${base}/${base_arch}"
2885    fs-traverse file "${basepath}" {
2886        set fpath [string range "${file}" [string length "${basepath}"] [string length "${file}"]]
2887        if {${fpath} != ""} {
2888            # determine the type (dir/file/link)
2889            switch [file type ${basepath}${fpath}] {
2890                directory {
2891                    # just create directories
2892                    ui_debug "mrg: directory ${fpath}"
2893                    file mkdir "${destroot}${fpath}"
2894                }
2895                link {
2896                    # copy symlinks, TODO: check if targets match!
2897                    ui_debug "mrg: symlink ${fpath}"
2898                    file copy "${basepath}${fpath}" "${destroot}${fpath}"
2899                }
2900                default {
2901                    set filetype [exec [findBinary file $portutil::autoconf::file_path] "-b" "${basepath}${fpath}"]
2902                    switch -regexp ${filetype} {
2903                        Mach-O.* {
2904                            merge_lipo "${base}" "${destroot}" "${fpath}" "${archs}"
2905                        }
2906                        current\ ar\ archive {
2907                            merge_lipo "${base}" "${destroot}" "${fpath}" "${archs}"
2908                        }
2909                        ASCII\ C\ program\ text {
2910                            merge_cpp "${base}" "${destroot}" "${fpath}" "${archs}"
2911                        }
2912                        default {
2913                            ui_debug "unknown file type: ${filetype}"
2914                            merge_file "${base}" "${destroot}" "${fpath}" "${archs}"
2915                        }
2916                    }
2917                }
2918            }
2919        }
2920    }
2921}
2922
2923##
2924# Escape a string for safe use in regular expressions
2925#
2926# @param str the string to be quoted
2927# @return the escaped string
2928proc quotemeta {str} {
2929    regsub -all {(\W)} $str {\\\1} str
2930    return $str
2931}
2932
2933##
2934# Recusively chown the given file or directory to the specified user.
2935#
2936# @param path the file/directory to be chowned
2937# @param user the user to chown file to
2938proc chown {path user} {
2939    lchown $path $user
2940
2941    if {[file isdirectory $path]} {
2942        fs-traverse myfile ${path} {
2943            lchown $myfile $user
2944        }
2945    }
2946
2947}
2948
2949##
2950# Recusively chown the given file or directory to $macportsuser, using root privileges.
2951#
2952# @param path the file/directory to be chowned
2953proc chownAsRoot {path} {
2954    global euid egid macportsuser
2955
2956    if { [getuid] == 0 } {
2957        if {[geteuid] != 0} {
2958            # if started with sudo but have dropped the privileges
2959            seteuid $euid
2960            setegid $egid
2961            ui_debug "euid/egid changed to: [geteuid]/[getegid]"
2962            chown  ${path} ${macportsuser}
2963            ui_debug "chowned $path to $macportsuser"
2964            setegid [uname_to_gid "$macportsuser"]
2965            seteuid [name_to_uid "$macportsuser"]
2966            ui_debug "euid/egid changed to: [geteuid]/[getegid]"
2967        } else {
2968            # if started with sudo but have elevated back to root already
2969            chown  ${path} ${macportsuser}
2970        }
2971    }
2972}
2973
2974##
2975# Change attributes of file while running as root
2976#
2977# @param file the file in question
2978# @param attributes the attributes for the file
2979proc fileAttrsAsRoot {file attributes} {
2980    global euid egid macportsuser
2981    if {[getuid] == 0} {
2982        if {[geteuid] != 0} {
2983            # Started as root, but not root now
2984            seteuid $euid
2985            setegid $egid
2986            ui_debug "euid/egid changed to: [geteuid]/[getegid]"
2987            ui_debug "setting attributes on $file"
2988            file attributes $file {*}$attributes
2989            setegid [uname_to_gid "$macportsuser"]
2990            seteuid [name_to_uid "$macportsuser"]
2991            ui_debug "euid/egid changed to: [geteuid]/[getegid]"
2992        } else {
2993            file attributes $file {*}$attributes
2994        }
2995    } else {
2996        # not root, so can't set owner/group
2997        set permissions [lindex $attributes [expr {[lsearch $attributes "-permissions"] + 1}]]
2998        file attributes $file -permissions $permissions
2999    }
3000}
3001
3002##
3003# Elevate privileges back to root.
3004#
3005# @param action the action for which privileges are being elevated
3006proc elevateToRoot {action} {
3007    global euid egid macportsuser
3008
3009    if { [getuid] == 0 && [geteuid] != 0 } {
3010    # if started with sudo but have dropped the privileges
3011        ui_debug "Can't run $action on this port without elevated privileges. Escalating privileges back to root."
3012        seteuid $euid
3013        setegid $egid
3014        ui_debug "euid changed to: [geteuid]. egid changed to: [getegid]."
3015    } elseif { [getuid] != 0 } {
3016        return -code error "MacPorts requires root privileges for this action"
3017    }
3018}
3019
3020##
3021# de-escalate privileges from root to those of $macportsuser.
3022#
3023proc dropPrivileges {} {
3024    global euid egid macportsuser workpath
3025    if { [geteuid] == 0 } {
3026        if { [catch {
3027                if {[name_to_uid "$macportsuser"] != 0} {
3028                    ui_debug "changing euid/egid - current euid: $euid - current egid: $egid"
3029
3030                    #seteuid [name_to_uid [file attributes $workpath -owner]]
3031                    #setegid [name_to_gid [file attributes $workpath -group]]
3032
3033                    setegid [uname_to_gid "$macportsuser"]
3034                    seteuid [name_to_uid "$macportsuser"]
3035                    ui_debug "egid changed to: [getegid]"
3036                    ui_debug "euid changed to: [geteuid]"
3037                }
3038            }]
3039        } {
3040            ui_debug "$::errorInfo"
3041            ui_error "Failed to de-escalate privileges."
3042        }
3043    } else {
3044        ui_debug "Privilege de-escalation not attempted as not running as root."
3045    }
3046}
3047
3048proc validate_macportsuser {} {
3049    global macportsuser
3050    if {[getuid] == 0 && $macportsuser ne "root" && 
3051        ([existsuser $macportsuser] == -1 || [existsgroup $macportsuser] == -1)} {
3052        ui_warn "configured user/group $macportsuser does not exist, will build as root"
3053        set macportsuser "root"
3054    }
3055}
3056
3057# dependency analysis helpers
3058
3059### _libtest is private; subject to change without notice
3060# XXX - Architecture specific
3061# XXX - Rely on information from internal defines in cctools/dyld:
3062# define DEFAULT_FALLBACK_FRAMEWORK_PATH
3063# /Library/Frameworks:/Library/Frameworks:/Network/Library/Frameworks:/System/Library/Frameworks
3064# define DEFAULT_FALLBACK_LIBRARY_PATH /lib:/usr/local/lib:/lib:/usr/lib
3065#   -- Since /usr/local is bad, using /lib:/usr/lib only.
3066# Environment variables DYLD_FRAMEWORK_PATH, DYLD_LIBRARY_PATH,
3067# DYLD_FALLBACK_FRAMEWORK_PATH, and DYLD_FALLBACK_LIBRARY_PATH take precedence
3068
3069proc _libtest {depspec {return_match 0}} {
3070    global env prefix frameworks_dir os.platform
3071    set depline [lindex [split $depspec :] 1]
3072
3073    if {[info exists env(DYLD_FRAMEWORK_PATH)]} {
3074        lappend search_path $env(DYLD_FRAMEWORK_PATH)
3075    } else {
3076        lappend search_path ${frameworks_dir} /Library/Frameworks /Network/Library/Frameworks /System/Library/Frameworks
3077    }
3078    if {[info exists env(DYLD_FALLBACK_FRAMEWORK_PATH)]} {
3079        lappend search_path $env(DYLD_FALLBACK_FRAMEWORK_PATH)
3080    }
3081    if {[info exists env(DYLD_LIBRARY_PATH)]} {
3082        lappend search_path $env(DYLD_LIBRARY_PATH)
3083    }
3084    lappend search_path /lib /usr/lib ${prefix}/lib
3085    if {[info exists env(DYLD_FALLBACK_LIBRARY_PATH)]} {
3086        lappend search_path $env(DYLD_FALLBACK_LIBRARY_PATH)
3087    }
3088
3089    set i [string first . $depline]
3090    if {$i < 0} {set i [string length $depline]}
3091    set depname [string range $depline 0 [expr {$i - 1}]]
3092    set depversion [string range $depline $i end]
3093    regsub {\.} $depversion {\.} depversion
3094    if {${os.platform} == "darwin"} {
3095        set depregex \^${depname}${depversion}\\.dylib\$
3096    } else {
3097        set depregex \^${depname}\\.so${depversion}\$
3098    }
3099
3100    return [_mportsearchpath $depregex $search_path 0 $return_match]
3101}
3102
3103### _bintest is private; subject to change without notice
3104
3105proc _bintest {depspec {return_match 0}} {
3106    global env prefix
3107    set depregex [lindex [split $depspec :] 1]
3108
3109    set search_path [split $env(PATH) :]
3110
3111    set depregex \^$depregex\$
3112
3113    return [_mportsearchpath $depregex $search_path 1 $return_match]
3114}
3115
3116### _pathtest is private; subject to change without notice
3117
3118proc _pathtest {depspec {return_match 0}} {
3119    global env prefix
3120    set depregex [lindex [split $depspec :] 1]
3121
3122    # separate directory from regex
3123    set fullname $depregex
3124
3125    regexp {^(.*)/(.*?)$} "$fullname" match search_path depregex
3126
3127    if {[string index $search_path 0] ne "/"} {
3128        # Prepend prefix if not an absolute path
3129        set search_path "${prefix}/${search_path}"
3130    }
3131
3132    set depregex \^$depregex\$
3133
3134    return [_mportsearchpath $depregex $search_path 0 $return_match]
3135}
3136
3137# returns the name of the port that will actually be satisfying $depspec
3138proc _get_dep_port {depspec} {
3139    set speclist [split $depspec :]
3140    set portname [lindex $speclist end]
3141    set res [_portnameactive $portname]
3142    if {$res != 0} {
3143        return $portname
3144    }
3145   
3146    set depfile ""
3147    switch [lindex $speclist 0] {
3148        bin {
3149            set depfile [_bintest $depspec 1]
3150        }
3151        lib {
3152            set depfile [_libtest $depspec 1]
3153        }
3154        path {
3155            set depfile [_pathtest $depspec 1]
3156        }
3157    }
3158    if {$depfile eq ""} {
3159        return $portname
3160    } else {
3161        set theport [registry_file_registered $depfile]
3162        if {$theport != 0} {
3163            return $theport
3164        } else {
3165            return ""
3166        }
3167    }
3168}
3169
3170# returns the list of archs that the port is targeting
3171proc get_canonical_archs {} {
3172    global supported_archs os.arch configure.build_arch configure.universal_archs
3173    if {$supported_archs eq "noarch"} {
3174        return "noarch"
3175    } elseif {[variant_exists universal] && [variant_isset universal]} {
3176        return [lsort -ascii ${configure.universal_archs}]
3177    } elseif {${configure.build_arch} != ""} {
3178        return ${configure.build_arch}
3179    } else {
3180        return ${os.arch}
3181    }
3182}
3183
3184# returns the flags that should be passed to the compiler to choose arch(s)
3185proc get_canonical_archflags {{tool cc}} {
3186    if {![variant_exists universal] || ![variant_isset universal]} {
3187        if {[catch {option configure.${tool}_archflags} flags]} {
3188            return -code error "archflags do not exist for tool '$tool'"
3189        }
3190    } else {
3191        if {$tool eq "cc"} {
3192            set tool c
3193        }
3194        if {[catch {option configure.universal_${tool}flags} flags]} {
3195            return -code error "universal archflags do not exist for tool '$tool'"
3196        }
3197    }
3198    return $flags
3199}
3200
3201# check that the selected archs are supported
3202proc check_supported_archs {} {
3203    global supported_archs build_arch universal_archs configure.build_arch configure.universal_archs subport
3204    if {$supported_archs eq "noarch"} {
3205        return 0
3206    } elseif {[variant_exists universal] && [variant_isset universal]} {
3207        if {[llength ${configure.universal_archs}] > 1 || $universal_archs == ${configure.universal_archs}} {
3208            return 0
3209        } else {
3210            ui_error "$subport cannot be installed for the configured universal_archs '$universal_archs' because it only supports the arch(s) '$supported_archs'."
3211            return 1
3212        }
3213    } elseif {$build_arch eq "" || ${configure.build_arch} != ""} {
3214        return 0
3215    }
3216    ui_error "$subport cannot be installed for the configured build_arch '$build_arch' because it only supports the arch(s) '$supported_archs'."
3217    return 1
3218}
3219
3220# check if the installed xcode version is new enough
3221proc _check_xcode_version {} {
3222    global os.subplatform macosx_version xcodeversion
3223
3224    if {${os.subplatform} eq "macosx"} {
3225        switch $macosx_version {
3226            10.4 {
3227                set min 2.0
3228                set ok 2.4.1
3229                set rec 2.5
3230            }
3231            10.5 {
3232                set min 3.0
3233                set ok 3.1
3234                set rec 3.1.4
3235            }
3236            10.6 {
3237                set min 3.2
3238                set ok 3.2
3239                set rec 3.2.6
3240            }
3241            10.7 {
3242                set min 4.1
3243                set ok 4.1
3244                set rec 4.6.3
3245            }
3246            10.8 {
3247                set min 4.4
3248                set ok 4.4
3249                set rec 4.6.3
3250            }
3251            10.9 {
3252                set min 5.0.1
3253                set ok 5.0.1
3254                set rec 6.0.1
3255            }
3256            default {
3257                set min 6.0.1
3258                set ok 6.0.1
3259                set rec 6.1
3260            }
3261        }
3262        if {$xcodeversion eq "none"} {
3263            ui_warn "Xcode does not appear to be installed; most ports will likely fail to build."
3264            if {[file exists "/Applications/Install Xcode.app"]} {
3265                ui_warn "You downloaded Xcode from the Mac App Store but didn't install it. Run \"Install Xcode\" in the /Applications folder."
3266            }
3267        } elseif {[vercmp $xcodeversion $min] < 0} {
3268            ui_error "The installed version of Xcode (${xcodeversion}) is too old to use on the installed OS version. Version $rec or later is recommended on Mac OS X ${macosx_version}."
3269            return 1
3270        } elseif {[vercmp $xcodeversion $ok] < 0} {
3271            ui_warn "The installed version of Xcode (${xcodeversion}) is known to cause problems. Version $rec or later is recommended on Mac OS X ${macosx_version}."
3272        }
3273
3274        # Xcode 4.3 and above requires the command-line utilities package to be installed.
3275        if {[vercmp $xcodeversion 4.3] >= 0 || ($xcodeversion eq "none" && [file exists "/Applications/Xcode.app"])} {
3276            if {[vercmp $macosx_version 10.9] >= 0} {
3277                # on Mavericks, /usr/bin/make might always installed as a shim into the command line tools installer.
3278                # Let's check for /Library/Developer/CommandLineTools, installed by the
3279                # com.apple.pkg.CLTools_Executables package.
3280                set cltpath "/Library/Developer/CommandLineTools"
3281            } else {
3282                set cltpath "/"
3283            }
3284
3285            # Check whether /usr/include and /usr/bin/make exist and tell users to install the command line tools, if they don't
3286            if {   ![file isdirectory [file join $cltpath usr include]]
3287                || ![file executable  [file join $cltpath usr bin make]]} {
3288                ui_warn "The Xcode Command Line Tools don't appear to be installed; most ports will likely fail to build."
3289                if {[vercmp $macosx_version 10.9] >= 0} {
3290                    ui_warn "Install them by running `xcode-select --install'."
3291                } else {
3292                    ui_warn "You can install them from Xcode's Preferences in the Downloads section."
3293                    ui_warn "See http://guide.macports.org/chunked/installing.xcode.html#installing.xcode.lion.43 for more information."
3294                }
3295            }
3296
3297            # Check whether users have agreed to the Xcode license agreement
3298            catch {exec [findBinary xcrun $portutil::autoconf::xcrun_path] clang 2>@1} output
3299            set output [join [lrange [split $output "\n"] 0 end-1] "\n"]
3300            if {[string match -nocase "*license*" $output]} {
3301                ui_error "It seems you have not accepted the Xcode license; most ports will fail to build."
3302                ui_error "Agree to the license by opening Xcode or running `sudo xcodebuild -license'."
3303                return 1
3304            }
3305        }
3306    }
3307    return 0
3308}
3309
3310# check if we can unarchive this port
3311proc _archive_available {} {
3312    global ports_source_only porturl portutil::archive_available_result
3313
3314    if {[info exists archive_available_result]} {
3315        return $archive_available_result
3316    }
3317
3318    if {[tbool ports_source_only]} {
3319        set archive_available_result 0
3320        return 0
3321    }
3322
3323    if {[find_portarchive_path] ne ""} {
3324        set archive_available_result 1
3325        return 1
3326    }
3327
3328    set archiverootname [file rootname [get_portimage_name]]
3329    if {[file rootname [file tail $porturl]] eq $archiverootname && [file extension $porturl] ne ""} {
3330        set archive_available_result 1
3331        return 1
3332    }
3333
3334    # check if there's an archive available on the server
3335    global archive_sites
3336    set mirrors macports_archives
3337    if {[lsearch $archive_sites macports_archives::*] == -1} {
3338        set mirrors [lindex [split [lindex $archive_sites 0] :] 0]
3339    }
3340    if {$mirrors == {}} {
3341        set archive_available_result 0
3342        return 0
3343    }
3344    set archivetype $portfetch::mirror_sites::archive_type($mirrors)
3345    set archivename "${archiverootname}.${archivetype}"
3346    # grab first site, should conventionally be the master mirror
3347    set sites_entry [lindex $portfetch::mirror_sites::sites($mirrors) 0]
3348    # look for and strip off any tag, which will start with the first colon after the
3349    # first slash after the ://
3350    set lastcolon [string last : $sites_entry]
3351    set aftersep [expr {[string first : $sites_entry] + 3}]
3352    set firstslash [string first / $sites_entry $aftersep]
3353    if {$firstslash != -1 && $firstslash < $lastcolon} {
3354        incr lastcolon -1
3355        set site [string range $sites_entry 0 $lastcolon]
3356    } else {
3357        set site $sites_entry
3358    }
3359    if {[string index $site end] ne "/"} {
3360        append site "/[option archive.subdir]"
3361    } else {
3362        append site [option archive.subdir]
3363    }
3364    set url [portfetch::assemble_url $site $archivename]
3365    if {![catch {curl getsize $url} result]} {
3366        set archive_available_result 1
3367        return 1
3368    }
3369
3370    set archive_available_result 0
3371    return 0
3372}
Note: See TracBrowser for help on using the repository browser.