-
-
Notifications
You must be signed in to change notification settings - Fork 236
/
portutil.tcl
3357 lines (3001 loc) · 114 KB
/
portutil.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- 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
# $Id$
#
# Copyright (c) 2002-2003 Apple Inc.
# Copyright (c) 2004 Robert Shaw <rshaw@opendarwin.org>
# Copyright (c) 2006-2007 Markus W. Weissmann <mww@macports.org>
# Copyright (c) 2004-2013 The MacPorts Project
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of Apple Inc. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
package provide portutil 1.0
package require Pextlib 1.0
package require macports_dlist 1.0
package require macports_util 1.0
package require msgcat
package require porttrace 1.0
global targets target_uniqid all_variants
set targets [list]
set target_uniqid 0
set all_variants [list]
########### External High Level Procedures ###########
namespace eval options {
}
# option
# This is an accessor for Portfile options. Targets may use
# this in the same style as the standard Tcl "set" procedure.
# option - the name of the option to read or write
# not called 'name' because this would fail if its value was 'name'...
# value - an optional value to assign to the option
proc option {option args} {
# XXX: right now we just transparently use globals
# eventually this will need to bridge the options between
# the Portfile's interpreter and the target's interpreters.
global $option
if {[llength $args] > 0} {
ui_debug "setting option $option to $args"
set $option [lindex $args 0]
}
return [set $option]
}
# exists
# This is an accessor for Portfile options. Targets may use
# this procedure to test for the existence of a Portfile option.
# option - the name of the option to test for existence
proc exists {option} {
# XXX: right now we just transparently use globals
# eventually this will need to bridge the options between
# the Portfile's interpreter and the target's interpreters.
global $option
return [info exists $option]
}
##
# Handle an option
#
# @param option name of the option
# @param args arguments
proc handle_option {option args} {
global $option user_options option_procs
if {![info exists user_options($option)]} {
set $option $args
}
}
##
# Handle option-append
#
# @param option name of the option
# @param args arguments
proc handle_option-append {option args} {
global $option user_options option_procs
if {![info exists user_options($option)]} {
if {[info exists $option]} {
set $option [concat [set $option] $args]
} else {
set $option $args
}
}
}
##
# Handle option-prepend
#
# @param option name of the option
# @param args arguments
proc handle_option-prepend {option args} {
global $option user_options option_procs
if {![info exists user_options($option)]} {
if {[info exists $option]} {
set $option [concat $args [set $option]]
} else {
set $option $args
}
}
}
##
# Handle option-delete
#
# @param option name of the option
# @param args arguments
proc handle_option-delete {option args} {
global $option user_options option_procs
if {![info exists user_options($option)] && [info exists $option]} {
set temp [set $option]
foreach val $args {
set temp [ldelete $temp $val]
}
set $option $temp
}
}
##
# Handle option-strsed
#
# @param option name of the option
# @param args arguments
proc handle_option-strsed {option args} {
global $option user_options option_procs
if {![info exists user_options($option)] && [info exists $option]} {
set temp [set $option]
foreach val $args {
set temp [strsed $temp $val]
}
set $option $temp
}
}
##
# Handle option-replace
#
# @param option name of the option
# @param args arguments
proc handle_option-replace {option args} {
global $option user_options option_procs deprecated_options
# Deprecate -replace with only one argument, for backwards compatibility call -strsed
# XXX: Remove this in 2.2.0
if {[llength $args] == 1} {
if {![info exists deprecated_options(${option}-replace)]} {
set deprecated_options(${option}-replace) [list ${option}-strsed 0]
}
set refcount [lindex $deprecated_options(${option}-replace) 1]
lset deprecated_options(${option}-replace) 1 [expr {$refcount + 1}]
return [eval handle_option-strsed $option $args]
}
if {![info exists user_options($option)] && [info exists $option]} {
foreach {old new} $args {
set index [lsearch -exact [set $option] $old]
if {$index == -1} {
continue
}
set $option [lreplace [set $option] $index $index $new]
}
}
}
# options
# Exports options in an array as externally callable procedures
# Thus, "options name date" would create procedures named "name"
# and "date" that set global variables "name" and "date", respectively
# When an option is modified in any way, options::$option is called,
# if it exists
# Arguments: <list of options>
proc options {args} {
foreach option $args {
interp alias {} $option {} handle_option $option
interp alias {} $option-append {} handle_option-append $option
interp alias {} $option-prepend {} handle_option-prepend $option
interp alias {} $option-delete {} handle_option-delete $option
interp alias {} $option-strsed {} handle_option-strsed $option
interp alias {} $option-replace {} handle_option-replace $option
}
}
##
# Export options into PortInfo
#
# @param option the name of the option
# @param action set or delete
# @param value the value to be set, defaults to an empty string
proc options::export {option action {value ""}} {
global $option PortInfo
switch $action {
set {
set PortInfo($option) $value
}
delete {
unset -nocomplain PortInfo($option)
}
}
}
##
# Export multiple options
#
# @param args list of ports to be exported
proc options_export {args} {
foreach option $args {
option_proc $option options::export
}
}
##
# Print a warning for deprecated options
#
# @param option deprecated option
# @param action read/set
# @param value ignored
proc handle_deprecated_option {option action {value ""}} {
global subport $option deprecated_options
set newoption [lindex $deprecated_options($option) 0]
set refcount [lindex $deprecated_options($option) 1]
global $newoption
if {$newoption eq ""} {
ui_warn "Port $subport using deprecated option \"$option\"."
return
}
# Increment reference counter
lset deprecated_options($option) 1 [expr {$refcount + 1}]
if {$action ne "read"} {
$newoption [set $option]
} else {
$option [set $newoption]
}
}
##
# Get the name of the array containing the deprecated options
# Thin layer avoiding to share global variables without notice
proc get_deprecated_options {} {
return "deprecated_options"
}
##
# Mark an option as deprecated
# If it is set or accessed, it will be mapped to the new option
#
# @param option name of the option
# @param newoption name of a superseding option
proc option_deprecate {option {newoption ""} } {
global deprecated_options
# If a new option is specified, default the option to $newoption
set deprecated_options($option) [list $newoption 0]
# Create a normal option for compatibility
options $option
# Register a proc for handling the deprecation
option_proc $option handle_deprecated_option
}
##
# Registers a proc to be called when an option is changed
#
# @param option the name of the option
# @param args name of procs
proc option_proc {option args} {
global option_procs $option
if {[info exists option_procs($option)]} {
set option_procs($option) [concat $option_procs($option) $args]
# we're already tracing
} else {
set option_procs($option) $args
trace add variable $option {read write unset} option_proc_trace
}
}
# option_proc_trace
# trace handler for option reads. Calls option procedures with correct arguments.
proc option_proc_trace {optionName index op} {
global option_procs
upvar $optionName $optionName
switch $op {
write {
foreach p $option_procs($optionName) {
$p $optionName set [set $optionName]
}
}
read {
foreach p $option_procs($optionName) {
$p $optionName read
}
}
unset {
foreach p $option_procs($optionName) {
if {[catch {$p $optionName delete} result]} {
ui_debug "error during unset trace ($p): $result\n$::errorInfo"
}
}
trace add variable $optionName {read write unset} option_proc_trace
}
}
}
# commands
# Accepts a list of arguments, of which several options are created
# and used to form a standard set of command options.
proc commands {args} {
foreach option $args {
options use_${option} ${option}.dir ${option}.pre_args ${option}.args ${option}.post_args ${option}.env ${option}.nice ${option}.type ${option}.cmd
}
}
# Given a command name, assemble a command string
# composed of the command options.
proc command_string {command} {
global ${command}.dir ${command}.pre_args ${command}.args ${command}.post_args ${command}.cmd
if {[info exists ${command}.dir]} {
append cmdstring "cd \"[set ${command}.dir]\" &&"
}
if {[info exists ${command}.cmd]} {
foreach string [set ${command}.cmd] {
append cmdstring " $string"
}
} else {
append cmdstring " ${command}"
}
foreach var "${command}.pre_args ${command}.args ${command}.post_args" {
if {[info exists $var]} {
foreach string [set ${var}] {
append cmdstring " ${string}"
}
}
}
ui_debug "Assembled command: '$cmdstring'"
return $cmdstring
}
# Given a command name, execute it with the options.
# command_exec command [-notty] [-varprefix variable_prefix] [command_prefix [command_suffix]]
# command name of the command
# variable_prefix name of the variable prefix to use (defaults to command)
# command_prefix additional command prefix (typically pipe command)
# command_suffix additional command suffix (typically redirection)
proc command_exec {command args} {
set varprefix "${command}"
set notty ""
set command_prefix ""
set command_suffix ""
if {[llength $args] > 0} {
if {[lindex $args 0] eq "-notty"} {
set notty "-notty"
set args [lrange $args 1 end]
}
if {[lindex $args 0] eq "-varprefix"} {
set varprefix [lindex $args 1]
set args [lrange $args 2 end]
}
if {[llength $args] > 0} {
set command_prefix [lindex $args 0]
if {[llength $args] > 1} {
set command_suffix [lindex $args 1]
}
}
}
set dir [option ${varprefix}.dir]
if {![file exists ${dir}]} {
ui_debug "Creating ${varprefix} directory: ${dir}"
file mkdir ${dir}
}
global ${varprefix}.env ${varprefix}.env_array ${varprefix}.nice env macosx_version
# Set the environment.
# If the array doesn't exist, we create it with the value
# coming from ${varprefix}.env
# Otherwise, it means the caller actually played with the environment
# array already (e.g. configure flags).
if {![array exists ${varprefix}.env_array]} {
parse_environment ${varprefix}
}
if {[option macosx_deployment_target] ne ""} {
set ${varprefix}.env_array(MACOSX_DEPLOYMENT_TARGET) [option macosx_deployment_target]
}
set ${varprefix}.env_array(CC_PRINT_OPTIONS) "YES"
set ${varprefix}.env_array(CC_PRINT_OPTIONS_FILE) [file join [option workpath] ".CC_PRINT_OPTIONS"]
if {[option compiler.cpath] ne ""} {
set ${varprefix}.env_array(CPATH) [join [option compiler.cpath] :]
}
if {[option compiler.library_path] ne ""} {
set ${varprefix}.env_array(LIBRARY_PATH) [join [option compiler.library_path] :]
}
# Debug that.
ui_debug "Environment: [environment_array_to_string ${varprefix}.env_array]"
# Prepare nice value change
set nice ""
if {[info exists ${varprefix}.nice] && [set ${varprefix}.nice] ne ""} {
set nice "-nice [set ${varprefix}.nice]"
}
# Get the command string.
set cmdstring [command_string ${command}]
# Call this command.
# TODO: move that to the system native call?
# Save the environment.
array set saved_env [array get env]
# Set the overriden variables from the portfile.
array set env [array get ${varprefix}.env_array]
# Call the command.
set fullcmdstring "$command_prefix $cmdstring $command_suffix"
ui_debug "Executing command line: $fullcmdstring"
set code [catch {eval system $notty $nice \$fullcmdstring} result]
# Save variables in order to re-throw the same error code.
set errcode $::errorCode
set errinfo $::errorInfo
# Unset the command array until next time.
array unset ${varprefix}.env_array
# Restore the environment.
array unset env *
if {$macosx_version eq "10.5"} {
unsetenv *
}
array set env [array get saved_env]
# Return as if system had been called directly.
return -code $code -errorcode $errcode -errorinfo $errinfo $result
}
# default
# Sets a variable to the supplied default if it does not exist,
# and adds a variable trace. The variable traces allows for delayed
# variable and command expansion in the variable's default value.
proc default {option val} {
global $option option_defaults
if {[info exists option_defaults($option)]} {
ui_debug "Re-registering default for $option"
# remove the old trace
trace vdelete $option rwu default_check
} else {
# If option is already set and we did not set it
# do not reset the value
if {[info exists $option]} {
return
}
}
set option_defaults($option) $val
set $option $val
trace variable $option rwu default_check
}
# default_check
# trace handler to provide delayed variable & command expansion
# for default variable values
proc default_check {optionName index op} {
global option_defaults $optionName
switch $op {
w {
unset option_defaults($optionName)
trace vdelete $optionName rwu default_check
return
}
r {
upvar $optionName option
uplevel #0 set $optionName $option_defaults($optionName)
return
}
u {
unset option_defaults($optionName)
trace vdelete $optionName rwu default_check
return
}
}
}
##
# Filter options which take strings removing indent to ease Portfile writing
proc handle_option_string {option action args} {
global $option
switch $action {
set {
set args [join $args]
set fulllist {}
# args is a list of strings/list
foreach arg $args {
# Strip trailing empty lines
if {[string index $arg 0] eq "\n"} {
set arg [string range $arg 1 end]
}
if {[string index $arg end] eq "\n"} {
set arg [string range $arg 0 end-1]
}
# Determine indent level
set indent ""
for {set i 0} {$i < [string length $arg]} {incr i} {
set c [string index $arg $i]
if {$c ne " " && $c ne "\t"} {
break
}
append indent $c
}
# Remove indent on first line
set arg [string replace $arg 0 [expr {$i - 1}]]
# Remove indent on each other line
set arg [string map "\"\n$indent\" \"\n\"" $arg]
lappend fulllist $arg
}
set $option $fulllist
}
}
}
# variant <provides> [<provides> ...] [requires <requires> [<requires>]]
# Portfile level procedure to provide support for declaring variants
proc variant {args} {
global all_variants PortInfo porturl
# Each key in PortInfo(vinfo) maps to an array which contains the
# following keys:
# * conflicts
# * description: This key's mapping is duplicated in
# PortInfo(variant_desc) for backward compatibility
# reasons (specifically 1.7.0's format of PortIndex).
# * is_default: This key exists iff the variant is a default variant.
# * requires
if {![info exists PortInfo(vinfo)]} {
set PortInfo(vinfo) {}
}
array set vinfo $PortInfo(vinfo)
set len [llength $args]
if {$len < 2} {
return -code error "Malformed variant specification"
}
set code [lindex $args end]
set args [lrange $args 0 [expr {$len - 2}]]
set ditem [variant_new "temp-variant"]
# mode indicates what the arg is interpreted as.
# possible mode keywords are: requires, conflicts, provides
# The default mode is provides. Arguments are added to the
# most recently specified mode (left to right).
set mode "provides"
foreach arg $args {
switch -exact $arg {
description -
provides -
requires -
conflicts { set mode $arg }
default { ditem_append $ditem $mode $arg }
}
}
ditem_key $ditem name "[join [ditem_key $ditem provides] -]"
# make a user procedure named variant-blah-blah
# we will call this procedure during variant-run
makeuserproc "variant-[ditem_key $ditem name]" \{$code\}
# Export provided variant to PortInfo
# (don't list it twice if the variant was already defined, which can happen
# with universal or group code).
set variant_provides [ditem_key $ditem provides]
if {[variant_exists $variant_provides]} {
# This variant was already defined. Remove it from the dlist.
variant_remove_ditem $variant_provides
} else {
# Create an array to contain the variant's information.
if {![info exists vinfo($variant_provides)]} {
set vinfo($variant_provides) {}
}
array set variant $vinfo($variant_provides)
# Set conflicts.
set vconflicts [join [lsort [ditem_key $ditem conflicts]]]
if {$vconflicts ne ""} {
array set variant [list conflicts $vconflicts]
}
lappend PortInfo(variants) $variant_provides
set vdesc [join [ditem_key $ditem description]]
# read global variant description, if none given
if {$vdesc eq ""} {
set vdesc [variant_desc $porturl $variant_provides]
}
# Set description.
if {$vdesc ne ""} {
array set variant [list description $vdesc]
# XXX: The following line should be removed after 1.8.0 is
# released.
lappend PortInfo(variant_desc) $variant_provides $vdesc
}
# Set requires.
set vrequires [join [lsort [ditem_key $ditem requires]]]
if {$vrequires ne ""} {
array set variant [list requires $vrequires]
}
}
# Add the variant (back) to PortInfo(vinfo).
array set vinfo [list $variant_provides [array get variant]]
set PortInfo(vinfo) [array get vinfo]
if {[variant_isset $variant_provides]} {
# set variants that this one requires
foreach req [ditem_key $ditem requires] {
variant_set $req
}
}
# Finally append the ditem to the dlist.
lappend all_variants $ditem
}
# variant_isset name
# Returns 1 if variant name selected, otherwise 0
proc variant_isset {name} {
global variations
if {[info exists variations($name)] && $variations($name) eq "+"} {
return 1
}
return 0
}
# variant_set name
# Sets variant to run for current portfile
proc variant_set {name} {
global variations
set variations($name) +
}
# variant_remove_ditem name
# Remove variant name's ditem from the all_variants dlist
proc variant_remove_ditem {name} {
global all_variants
set item_index 0
foreach variant_item $all_variants {
set item_provides [ditem_key $variant_item provides]
if {$item_provides == $name} {
set all_variants [lreplace $all_variants $item_index $item_index]
break
}
incr item_index
}
}
# variant_exists name
# determine if a variant exists.
proc variant_exists {name} {
global PortInfo
if {[info exists PortInfo(variants)] &&
[lsearch -exact $PortInfo(variants) $name] >= 0} {
return 1
}
return 0
}
##
# Load the global description file for a port tree
#
# @param descfile path to the descriptions file
proc load_variant_desc_file {descfile} {
global variant_descs_global
if {![info exists variant_descs_global($descfile)]} {
set variant_descs_global($descfile) yes
if {[file exists $descfile]} {
ui_debug "Reading variant descriptions from $descfile"
if {[catch {set fd [open $descfile r]} err]} {
ui_warn "Could not open global variant description file: $err"
return ""
}
set lineno 0
while {[gets $fd line] >= 0} {
incr lineno
set name [lindex $line 0]
set desc [lindex $line 1]
if {$name ne "" && $desc ne ""} {
set variant_descs_global(${descfile}_$name) $desc
} else {
ui_warn "Invalid variant description in $descfile at line $lineno"
}
}
close $fd
}
}
}
##
# Get description for a variant from global descriptions file
#
# @param porturl url to a port
# @param variant name
# @return description from descriptions file or an empty string
proc variant_desc {porturl variant} {
global variant_descs_global
set descfile [getportresourcepath $porturl "port1.0/variant_descriptions.conf" no]
load_variant_desc_file $descfile
if {[info exists variant_descs_global(${descfile}_${variant})]} {
return $variant_descs_global(${descfile}_${variant})
} else {
set descfile [getdefaultportresourcepath "port1.0/variant_descriptions.conf"]
load_variant_desc_file $descfile
if {[info exists variant_descs_global(${descfile}_${variant})]} {
return $variant_descs_global(${descfile}_${variant})
}
return ""
}
}
# platform [<os> [<release>]] [<arch>]
# Portfile level procedure to provide support for declaring platform-specifics
# Basically, just a fancy 'if', so that Portfiles' platform declarations can
# be more readable, and support arch and version specifics
proc platform {args} {
global os.platform os.subplatform os.arch os.major
set len [llength $args]
if {$len < 2} {
return -code error "Malformed platform specification"
}
set code [lindex $args end]
set os [lindex $args 0]
set args [lrange $args 1 [expr {$len - 2}]]
foreach arg $args {
if {[regexp {(^[0-9]+$)} $arg match result]} {
set release $result
} elseif {[regexp {([a-zA-Z0-9]*)} $arg match result]} {
set arch $result
}
}
set match 0
# 'os' could be a platform or an arch when it's alone
if {$len == 2 && ($os == ${os.platform} || $os == ${os.subplatform} || $os == ${os.arch})} {
set match 1
} elseif {($os == ${os.platform} || $os == ${os.subplatform})
&& (![info exists release] || ${os.major} == $release)
&& (![info exists arch] || ${os.arch} == $arch)} {
set match 1
}
# Execute the code if this platform matches the platform we're on
if {$match} {
uplevel 1 $code
}
}
# Portfiles may define more than one port.
# This executes the given code in 'body' if we were opened as the specified
# subport, and also adds it to the list of subports that are defined.
proc subport {subname body} {
global subport name PortInfo
if {$subport == $name && $subname != $name &&
(![info exists PortInfo(subports)] || [lsearch -exact $PortInfo(subports) $subname] == -1)} {
lappend PortInfo(subports) $subname
}
if {[string equal -nocase $subname $subport]} {
set PortInfo(name) $subname
uplevel 1 $body
}
}
########### Environment utility functions ###########
# Parse the environment string of a command, storing the values into the
# associated environment array.
proc parse_environment {command} {
global ${command}.env ${command}.env_array
if {[info exists ${command}.env]} {
# Flatten the environment string.
set the_environment [join [set ${command}.env]]
while {[regexp "^(?: *)(\[^= \]+)=(\"|'|)(\[^\"'\]*?)\\2(?: +|$)(.*)$" ${the_environment} matchVar key delimiter value remaining]} {
set the_environment ${remaining}
set ${command}.env_array(${key}) ${value}
}
} else {
array set ${command}.env_array {}
}
}
# Append one or more items to the key in the parsed environment.
proc append_to_environment_value {command key args} {
upvar #0 ${command}.env_array($key) env_key
foreach value $args {
if {$value eq {}} {
continue
}
# Parse out any delimiters. Is this even necessary anymore?
regexp {^(['"])(.*)\1$} $value -> delim value
append env_key " $value"
}
catch {set env_key [string trimleft $env_key]}
}
# Return a string representation of the specified environment, for
# debugging purposes.
proc environment_array_to_string {environment_array} {
upvar 1 ${environment_array} env_array
foreach {key value} [array get env_array] {
lappend env_list $key='$value'
}
return "\n[join [lsort $env_list] "\n"]"
}
########### Distname utility functions ###########
# Given a distribution file name, return the appended tag
# Example: getdisttag distfile.tar.gz:tag1 returns "tag1"
# / isn't included in the regexp, thus allowing port specification in URLs.
proc getdisttag {name} {
if {[regexp {.+:([0-9A-Za-z_-]+)$} $name match tag]} {
return $tag
} else {
return ""
}
}
# Given a distribution file name, return the name without an attached tag
# Example : getdistname distfile.tar.gz:tag1 returns "distfile.tar.gz"
# / isn't included in the regexp, thus allowing port specification in URLs.
proc getdistname {name} {
regexp {(.+):[0-9A-Za-z_-]+$} $name match name
return $name
}
########### Misc Utility Functions ###########
# tbool (testbool)
# If the variable exists in the calling procedure's namespace
# and is set to "yes", return 1. Otherwise, return 0
proc tbool {key} {
upvar $key $key
if {[info exists $key]} {
if {[string equal -nocase [set $key] "yes"]} {
return 1
}
}
return 0
}
# ldelete
# Deletes a value from the supplied list
proc ldelete {list value} {
set ix [lsearch -exact $list $value]
if {$ix >= 0} {
return [lreplace $list $ix $ix]
}
return $list
}
# reinplace
# Provides "sed in place" functionality
proc reinplace {args} {
global env workpath worksrcpath macosx_version
set extended 0
set suppress 0
set oldlocale_exists 0
set oldlocale ""
set locale ""
set dir ${worksrcpath}
while 1 {
set arg [lindex $args 0]
if {[string index $arg 0] eq "-"} {
set args [lrange $args 1 end]
switch -- [string range $arg 1 end] {
locale {
set oldlocale_exists [info exists env(LC_CTYPE)]
if {$oldlocale_exists} {
set oldlocale $env(LC_CTYPE)
}
set locale [lindex $args 0]
set args [lrange $args 1 end]
}
E {
set extended 1
}
n {
set suppress 1
}
W {
set dir [lindex $args 0]
set args [lrange $args 1 end]
}
- {
break
}
default {
error "reinplace: unknown flag '$arg'"
}
}
} else {
break
}
}
if {[llength $args] < 2} {
error "reinplace ?-E? ?-n? ?-W dir? pattern file ..."
}
set pattern [lindex $args 0]
set files [lrange $args 1 end]
if {[file isdirectory ${workpath}/.tmp]} {
set tempdir ${workpath}/.tmp
} else {
set tempdir /tmp
}
foreach file $files {
global UI_PREFIX
# if $file is an absolute path already, file join will just return the
# absolute path, otherwise it is $dir/$file
set file [file join $dir $file]
if {[catch {set tmpfile [mkstemp "${tempdir}/[file tail $file].sed.XXXXXXXX"]} error]} {
global errorInfo
ui_debug "$errorInfo"
ui_error "reinplace: $error"
return -code error "reinplace failed"
} else {
# Extract the Tcl Channel number
set tmpfd [lindex $tmpfile 0]
# Set tmpfile to only the file name
set tmpfile [join [lrange $tmpfile 1 end]]
}
set cmdline $portutil::autoconf::sed_command
if {$extended} {
if {$portutil::autoconf::sed_ext_flag eq "N/A"} {
ui_debug "sed extended regexp not available"
return -code error "reinplace sed(1) too old"
}
lappend cmdline $portutil::autoconf::sed_ext_flag
}
if {$suppress} {
lappend cmdline -n