-
Notifications
You must be signed in to change notification settings - Fork 122
/
autosetup
executable file
·2534 lines (2262 loc) · 61.3 KB
/
autosetup
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
#!/bin/sh
# Copyright (c) 2006-2011 WorkWare Systems http://www.workware.net.au/
# All rights reserved
# vim:se syntax=tcl:
# \
dir=`dirname "$0"`; exec "`$dir/autosetup-find-tclsh`" "$0" "$@"
# Note that the version has a trailing + on unreleased versions
set autosetup(version) 0.7.2
# Can be set to 1 to debug early-init problems
set autosetup(debug) [expr {"--debug" in $argv}]
##################################################################
#
# Main flow of control, option handling
#
proc main {argv} {
global autosetup define
# There are 3 potential directories involved:
# 1. The directory containing autosetup (this script)
# 2. The directory containing auto.def
# 3. The current directory
# From this we need to determine:
# a. The path to this script (and related support files)
# b. The path to auto.def
# c. The build directory, where output files are created
# This is also complicated by the fact that autosetup may
# have been run via the configure wrapper ([getenv WRAPPER] is set)
# Here are the rules.
# a. This script is $::argv0
# => dir, prog, exe, libdir
# b. auto.def is in the directory containing the configure wrapper,
# otherwise it is in the current directory.
# => srcdir, autodef
# c. The build directory is the current directory
# => builddir, [pwd]
# 'misc' is needed before we can do anything, so set a temporary libdir
# in case this is the development version
set autosetup(libdir) [file dirname $::argv0]/lib
use misc
# (a)
set autosetup(dir) [realdir [file dirname [realpath $::argv0]]]
set autosetup(prog) [file join $autosetup(dir) [file tail $::argv0]]
set autosetup(exe) [getenv WRAPPER $autosetup(prog)]
if {$autosetup(installed)} {
set autosetup(libdir) $autosetup(dir)
} else {
set autosetup(libdir) [file join $autosetup(dir) lib]
}
autosetup_add_dep $autosetup(prog)
# (b)
if {[getenv WRAPPER ""] eq ""} {
# Invoked directly
set autosetup(srcdir) [pwd]
} else {
# Invoked via the configure wrapper
set autosetup(srcdir) [file-normalize [file dirname $autosetup(exe)]]
}
set autosetup(autodef) [relative-path $autosetup(srcdir)/auto.def]
# (c)
set autosetup(builddir) [pwd]
set autosetup(argv) $argv
set autosetup(cmdline) {}
# options is a list of known options
set autosetup(options) {}
# optset is a dictionary of option values set by the user based on getopt
set autosetup(optset) {}
# optdefault is a dictionary of default values
set autosetup(optdefault) {}
# options-defaults is a dictionary of overrides for default values for options
set autosetup(options-defaults) {}
set autosetup(optionhelp) {}
set autosetup(showhelp) 0
use util
# Parse options
use getopt
# At the is point we don't know what is a valid option
# We simply parse anything that looks like an option
set autosetup(getopt) [getopt argv]
#"=Core Options:"
options-add {
help:=all => "display help and options. Optional: module name, such as --help=system"
licence license => "display the autosetup license"
version => "display the version of autosetup"
ref:=text manual:=text
reference:=text => "display the autosetup command reference. 'text', 'wiki', 'asciidoc' or 'markdown'"
debug => "display debugging output as autosetup runs"
install:=. => "install autosetup to the current or given directory"
}
if {$autosetup(installed)} {
# hidden options so we can produce a nice error
options-add {
sysinstall:path
}
} else {
options-add {
sysinstall:path => "install standalone autosetup to the given directory (e.g.: /usr/local)"
}
}
options-add {
force init:=help => "create initial auto.def, etc. Use --init=help for known types"
# Undocumented options
option-checking=1
nopager
quiet
timing
conf:
}
if {[opt-bool version]} {
puts $autosetup(version)
exit 0
}
# autosetup --conf=alternate-auto.def
if {[opt-str conf o]} {
set autosetup(autodef) $o
}
# Debugging output (set this early)
incr autosetup(debug) [opt-bool debug]
incr autosetup(force) [opt-bool force]
incr autosetup(msg-quiet) [opt-bool quiet]
incr autosetup(msg-timing) [opt-bool timing]
# If the local module exists, source it now to allow for
# project-local customisations
if {[file exists $autosetup(libdir)/local.tcl]} {
use local
}
# Now any auto-load modules
autosetup_load_auto_modules
if {[opt-str help o]} {
incr autosetup(showhelp)
use help
autosetup_help $o
}
if {[opt-bool licence license]} {
use help
autosetup_show_license
exit 0
}
if {[opt-str {manual ref reference} o]} {
use help
autosetup_reference $o
}
# Allow combining --install and --init
set earlyexit 0
if {[opt-str install o]} {
use install
autosetup_install $o
incr earlyexit
}
if {[opt-str init o]} {
use init
autosetup_init $o
incr earlyexit
}
if {$earlyexit} {
exit 0
}
if {[opt-str sysinstall o]} {
use install
autosetup_install $o 1
exit 0
}
if {![file exists $autosetup(autodef)]} {
# Check for invalid option first
options {}
user-error "No auto.def found in \"$autosetup(srcdir)\" (use [file tail $::autosetup(exe)] --init to create one)"
}
# Parse extra arguments into autosetup(cmdline)
foreach arg $argv {
if {[regexp {([^=]*)=(.*)} $arg -> n v]} {
dict set autosetup(cmdline) $n $v
define $n $v
} else {
user-error "Unexpected parameter: $arg"
}
}
autosetup_add_dep $autosetup(autodef)
# Add $argv to CONFIGURE_OPTS
define-append-argv CONFIGURE_OPTS {*}$autosetup(argv)
# Set up AUTOREMAKE to reconfigure with the same args
define-append-argv AUTOREMAKE {*}$autosetup(exe) {*}$autosetup(argv)
# Log how we were invoked
configlog "Invoked as: [getenv WRAPPER $::argv0] [quote-argv $autosetup(argv)]"
configlog "Tclsh: [info nameofexecutable]"
# Load auto.def as module "auto.def"
autosetup_load_module auto.def source $autosetup(autodef)
# Could warn here if options {} was not specified
show-notices
if {$autosetup(debug)} {
msg-result "Writing all defines to config.log"
configlog "================ defines ======================"
foreach n [lsort [array names define]] {
configlog "define $n $define($n)"
}
}
exit 0
}
# @section Option Handling
# @opt-bool ?-nodefault? option ...
#
# Check each of the named, boolean options and if any have been explicitly enabled
# or disabled by the user, return 1 or 0 accordingly.
#
# If the option was specified more than once, the last value wins.
# e.g. With '--enable-foo --disable-foo', '[opt-bool foo]' will return 0
#
# If no value was specified by the user, returns the default value for the
# first option. If '-nodefault' is given, this behaviour changes and
# -1 is returned instead.
#
proc opt-bool {args} {
set nodefault 0
if {[lindex $args 0] eq "-nodefault"} {
set nodefault 1
set args [lrange $args 1 end]
}
option-check-names {*}$args
foreach opt $args {
if {[dict exists $::autosetup(optset) $opt]} {
return [dict get $::autosetup(optset) $opt]
}
}
if {$nodefault} {
return -1
}
# Default value is the default for the first option
return [dict get $::autosetup(optdefault) [lindex $args 0]]
}
# @opt-val optionlist ?default=""?
#
# Returns a list containing all the values given for the non-boolean options in '$optionlist'.
# There will be one entry in the list for each option given by the user, including if the
# same option was used multiple times.
#
# If no options were set, '$default' is returned (exactly, not as a list).
#
# Note: For most use cases, 'opt-str' should be preferred.
#
proc opt-val {names {default ""}} {
option-check-names {*}$names
foreach opt $names {
if {[dict exists $::autosetup(optset) $opt]} {
lappend result {*}[dict get $::autosetup(optset) $opt]
}
}
if {[info exists result]} {
return $result
}
return $default
}
# @opt-str optionlist varname ?default?
#
# Sets '$varname' in the callers scope to the value for one of the given options.
#
# For the list of options given in '$optionlist', if any value is set for any option,
# the option value is taken to be the *last* value of the last option (in the order given).
#
# If no option was given, and a default was specified with 'options-defaults',
# that value is used.
#
# If no 'options-defaults' value was given and '$default' was given, it is used.
#
# If none of the above provided a value, no value is set.
#
# The return value depends on whether '$default' was specified.
# If it was, the option value is returned.
# If it was not, 1 is returns if a value was set, or 0 if not.
#
# Typical usage is as follows:
#
## if {[opt-str {myopt altname} o]} {
## do something with $o
## }
#
# Or:
## define myname [opt-str {myopt altname} o "/usr/local"]
#
proc opt-str {names varname args} {
global autosetup
option-check-names {*}$names
upvar $varname value
if {[llength $args]} {
# A default was given, so always return the string value of the option
set default [lindex $args 0]
set retopt 1
} else {
# No default, so return 0 or 1 to indicate if a value was found
set retopt 0
}
foreach opt $names {
if {[dict exists $::autosetup(optset) $opt]} {
set result [lindex [dict get $::autosetup(optset) $opt] end]
}
}
if {![info exists result]} {
# No user-specified value. Has options-defaults been set?
foreach opt $names {
if {[dict exists $::autosetup(optdefault) $opt]} {
set result [dict get $autosetup(optdefault) $opt]
}
}
}
if {[info exists result]} {
set value $result
if {$retopt} {
return $value
}
return 1
}
if {$retopt} {
set value $default
return $value
}
return 0
}
proc option-check-names {args} {
foreach o $args {
if {$o ni $::autosetup(options)} {
autosetup-error "Request for undeclared option --$o"
}
}
}
# Parse the option definition in $opts and update
# ::autosetup(setoptions) and ::autosetup(optionhelp) appropriately
#
proc options-add {opts} {
global autosetup
# First weed out comment lines
set realopts {}
foreach line [split $opts \n] {
if {![string match "#*" [string trimleft $line]]} {
append realopts $line \n
}
}
set opts $realopts
for {set i 0} {$i < [llength $opts]} {incr i} {
set opt [lindex $opts $i]
if {[string match =* $opt]} {
# This is a special heading
lappend autosetup(optionhelp) [list $opt $autosetup(module)]
continue
}
unset -nocomplain defaultvalue equal value
#puts "i=$i, opt=$opt"
regexp {^([^:=]*)(:)?(=)?(.*)$} $opt -> name colon equal value
if {$name in $autosetup(options)} {
autosetup-error "Option $name already specified"
}
#puts "$opt => $name $colon $equal $value"
# Find the corresponding value in the user options
# and set the default if necessary
if {[string match "-*" $opt]} {
# This is a documentation-only option, like "-C <dir>"
set opthelp $opt
} elseif {$colon eq ""} {
# Boolean option
lappend autosetup(options) $name
# Check for override
if {[dict exists $autosetup(options-defaults) $name]} {
# A default was specified with options-defaults, so use it
set value [dict get $autosetup(options-defaults) $name]
}
if {$value eq "1"} {
set opthelp "--disable-$name"
} else {
set opthelp "--$name"
}
# Set the default
if {$value eq ""} {
set value 0
}
set defaultvalue $value
dict set autosetup(optdefault) $name $defaultvalue
if {[dict exists $autosetup(getopt) $name]} {
# The option was specified by the user. Look at the last value.
lassign [lindex [dict get $autosetup(getopt) $name] end] type setvalue
if {$type eq "str"} {
# Can we convert the value to a boolean?
if {$setvalue in {1 enabled yes}} {
set setvalue 1
} elseif {$setvalue in {0 disabled no}} {
set setvalue 0
} else {
user-error "Boolean option $name given as --$name=$setvalue"
}
}
dict set autosetup(optset) $name $setvalue
#puts "Found boolean option --$name=$setvalue"
}
} else {
# String option.
lappend autosetup(options) $name
if {$equal ne "="} {
# Was the option given as "name:value=default"?
# If so, set $value to the display name and $defaultvalue to the default
# (This is the preferred way to set a default value for a string option)
if {[regexp {^([^=]+)=(.*)$} $value -> value defaultvalue]} {
dict set autosetup(optdefault) $name $defaultvalue
}
}
# Maybe override the default value
if {[dict exists $autosetup(options-defaults) $name]} {
# A default was specified with options-defaults, so use it
set defaultvalue [dict get $autosetup(options-defaults) $name]
dict set autosetup(optdefault) $name $defaultvalue
} elseif {![info exists defaultvalue]} {
# No default value was given by value=default or options-defaults
# so use the value as the default when the plain option with no
# value is given (.e.g. just --opt instead of --opt=value)
set defaultvalue $value
}
if {$equal eq "="} {
# String option with optional value
set opthelp "--$name?=$value?"
} else {
# String option with required value
set opthelp "--$name=$value"
}
# Get the values specified by the user
if {[dict exists $autosetup(getopt) $name]} {
set listvalue {}
foreach pair [dict get $autosetup(getopt) $name] {
lassign $pair type setvalue
if {$type eq "bool" && $setvalue} {
if {$equal ne "="} {
user-error "Option --$name requires a value"
}
# If given as a boolean, use the default value
set setvalue $defaultvalue
}
lappend listvalue $setvalue
}
#puts "Found string option --$name=$listvalue"
dict set autosetup(optset) $name $listvalue
}
}
# Now create the help for this option if appropriate
if {[lindex $opts $i+1] eq "=>"} {
set desc [lindex $opts $i+2]
if {[info exists defaultvalue]} {
set desc [string map [list @default@ $defaultvalue] $desc]
}
# A multi-line description
lappend autosetup(optionhelp) [list $opthelp $autosetup(module) $desc]
incr i 2
}
}
}
# @module-options optionlist
#
# Deprecated. Simply use 'options' from within a module.
proc module-options {opts} {
options $opts
}
proc max {a b} {
expr {$a > $b ? $a : $b}
}
proc options-wrap-desc {text length firstprefix nextprefix initial} {
set len $initial
set space $firstprefix
foreach word [split $text] {
set word [string trim $word]
if {$word == ""} {
continue
}
if {$len && [string length $space$word] + $len >= $length} {
puts ""
set len 0
set space $nextprefix
}
incr len [string length $space$word]
puts -nonewline $space$word
set space " "
}
if {$len} {
puts ""
}
}
# Display options (from $autosetup(optionhelp)) for modules that match
# glob pattern $what
proc options-show {what} {
set local 0
# Determine the max option width
set max 0
foreach help $::autosetup(optionhelp) {
lassign $help opt module desc
if {![string match $what $module]} {
continue
}
if {[string match =* $opt] || [string match \n* $desc]} {
continue
}
set max [max $max [string length $opt]]
}
set indent [string repeat " " [expr {$max+4}]]
set cols [getenv COLUMNS 80]
catch {
lassign [exec stty size] rows cols
}
incr cols -1
# Now output
foreach help $::autosetup(optionhelp) {
lassign $help opt module desc
if {![string match $what $module]} {
continue
}
if {$local == 0 && $module eq "auto.def"} {
puts "Local Options:"
incr local
}
if {[string match =* $opt]} {
# Output a special heading line"
puts [string range $opt 1 end]
continue
}
puts -nonewline " [format %-${max}s $opt]"
if {[string match \n* $desc]} {
# Output a pre-formatted help description as-is
puts $desc
} else {
options-wrap-desc [string trim $desc] $cols " " $indent [expr {$max+2}]
}
}
}
# @options optionspec
#
# Specifies configuration-time options which may be selected by the user
# and checked with 'opt-str' and 'opt-bool'. '$optionspec' contains a series
# of options specifications separated by newlines, as follows:
#
# A boolean option is of the form:
#
## name[=0|1] => "Description of this boolean option"
#
# The default is 'name=0', meaning that the option is disabled by default.
# If 'name=1' is used to make the option enabled by default, the description should reflect
# that with text like "Disable support for ...".
#
# An argument option (one which takes a parameter) is of one of the following forms:
#
## name:value => "Description of this option"
## name:value=default => "Description of this option with a default value"
## name:=value => "Description of this option with an optional value"
#
# If the 'name:value' form is used, the value must be provided with the option (as '--name=myvalue').
# If the 'name:value=default' form is used, the option has the given default value even if not
# specified by the user.
# If the 'name:=value' form is used, the value is optional and the given value is used
# if it is not provided.
#
# The description may contain '@default@', in which case it will be replaced with the default
# value for the option (taking into account defaults specified with 'options-defaults'.
#
# Undocumented options are also supported by omitting the '=> description'.
# These options are not displayed with '--help' and can be useful for internal options or as aliases.
#
# For example, '--disable-lfs' is an alias for '--disable=largefile':
#
## lfs=1 largefile=1 => "Disable large file support"
#
proc options {optlist} {
global autosetup
options-add $optlist
if {$autosetup(showhelp)} {
# If --help, stop now to show help
return -code break
}
if {$autosetup(module) eq "auto.def"} {
# Check for invalid options
if {[opt-bool option-checking]} {
foreach o [dict keys $::autosetup(getopt)] {
if {$o ni $::autosetup(options)} {
user-error "Unknown option --$o"
}
}
}
}
}
# @options-defaults dictionary
#
# Specifies a dictionary of options and a new default value for each of those options.
# Use before any 'use' statements in 'auto.def' to change the defaults for
# subsequently included modules.
proc options-defaults {dict} {
foreach {n v} $dict {
dict set ::autosetup(options-defaults) $n $v
}
}
proc config_guess {} {
if {[file-isexec $::autosetup(dir)/autosetup-config.guess]} {
if {[catch {exec-with-stderr sh $::autosetup(dir)/autosetup-config.guess} alias]} {
user-error $alias
}
return $alias
} else {
configlog "No autosetup-config.guess, so using uname"
string tolower [exec uname -p]-unknown-[exec uname -s][exec uname -r]
}
}
proc config_sub {alias} {
if {[file-isexec $::autosetup(dir)/autosetup-config.sub]} {
if {[catch {exec-with-stderr sh $::autosetup(dir)/autosetup-config.sub $alias} alias]} {
user-error $alias
}
}
return $alias
}
# @section Variable Definitions (defines)
# @define name ?value=1?
#
# Defines the named variable to the given value.
# These (name, value) pairs represent the results of the configuration check
# and are available to be subsequently checked, modified and substituted.
#
proc define {name {value 1}} {
set ::define($name) $value
#dputs "$name <= $value"
}
# @define-push {name ...} script
#
# Save the values of the given defines, evaluation the script, then restore.
# For example, to avoid updating AS_FLAGS and AS_CXXFLAGS:
## define-push {AS_CFLAGS AS_CXXFLAGS} {
## cc-check-flags -Wno-error
## }
proc define-push {names script} {
array set unset {}
foreach name $names {
if {[is-defined $name]} {
set save($name) [get-define $name]
} else {
set unset($name) 1
}
}
uplevel 1 $script
array set ::define [array get save]
foreach name [array names unset] {
unset -nocomplain ::define($name)
}
}
# @undefine name
#
# Undefine the named variable.
#
proc undefine {name} {
unset -nocomplain ::define($name)
#dputs "$name <= <undef>"
}
# @define-append name value ...
#
# Appends the given value(s) to the given "defined" variable.
# If the variable is not defined or empty, it is set to '$value'.
# Otherwise the value is appended, separated by a space.
# Any extra values are similarly appended.
#
# Note that define-append is not designed to add values containing spaces.
# If values may contain spaces, consider define-append-argv instead.
#
proc define-append {name args} {
if {[get-define $name ""] ne ""} {
foreach arg $args {
if {$arg eq ""} {
continue
}
append ::define($name) " " $arg
}
} else {
set ::define($name) [join $args]
}
#dputs "$name += [join $args] => $::define($name)"
}
# @define-append-argv name value ...
#
# Similar to define-append except designed to construct shell command
# lines, including correct handling of parameters with spaces.
#
# Each non-empty value is quoted if necessary and then appended to the given variable
# if it does not already exist.
#
proc define-append-argv {name args} {
set seen {}
set new {}
foreach val [list {*}[get-define $name ""] {*}$args] {
if {$val ne {} && ![dict exists $seen $val]} {
lappend new [quote-if-needed $val]
dict set seen $val 1
}
}
set ::define($name) [join $new " "]
#dputs "$name += [join $args] => $::define($name)"
}
# @get-define name ?default=0?
#
# Returns the current value of the "defined" variable, or '$default'
# if not set.
#
proc get-define {name {default 0}} {
if {[info exists ::define($name)]} {
#dputs "$name => $::define($name)"
return $::define($name)
}
#dputs "$name => $default"
return $default
}
# @is-defined name
#
# Returns 1 if the given variable is defined.
#
proc is-defined {name} {
info exists ::define($name)
}
# @is-define-set name
#
# Returns 1 if the given variable is defined and is set
# to a value other than "" or 0
#
proc is-define-set {name} {
if {[get-define $name] in {0 ""}} {
return 0
}
return 1
}
# @all-defines
#
# Returns a dictionary (name, value list) of all defined variables.
#
# This is suitable for use with 'dict', 'array set' or 'foreach'
# and allows for arbitrary processing of the defined variables.
#
proc all-defines {} {
array get ::define
}
# @section Environment/Helpers
# @get-env name default
#
# If '$name' was specified on the command line, return it.
# Otherwise if '$name' was set in the environment, return it.
# Otherwise return '$default'.
#
proc get-env {name default} {
if {[dict exists $::autosetup(cmdline) $name]} {
return [dict get $::autosetup(cmdline) $name]
}
getenv $name $default
}
# @env-is-set name
#
# Returns 1 if '$name' was specified on the command line or in the environment.
# Note that an empty environment variable is not considered to be set.
#
proc env-is-set {name} {
if {[dict exists $::autosetup(cmdline) $name]} {
return 1
}
if {[getenv $name ""] ne ""} {
return 1
}
return 0
}
# @readfile filename ?default=""?
#
# Return the contents of the file, without the trailing newline.
# If the file doesn't exist or can't be read, returns '$default'.
#
proc readfile {filename {default_value ""}} {
set result $default_value
catch {
set f [open $filename]
set result [read -nonewline $f]
close $f
}
return $result
}
# @writefile filename value
#
# Creates the given file containing '$value'.
# Does not add an extra newline.
#
proc writefile {filename value} {
set f [open $filename w]
puts -nonewline $f $value
close $f
}
proc quote-if-needed {str} {
if {[string match {*[\" ]*} $str]} {
return \"[string map [list \" \\" \\ \\\\] $str]\"
}
return $str
}
proc quote-argv {argv} {
set args {}
foreach arg $argv {
lappend args [quote-if-needed $arg]
}
join $args
}
# @list-non-empty list
#
# Returns a copy of the given list with empty elements removed
proc list-non-empty {list} {
set result {}
foreach p $list {
if {$p ne ""} {
lappend result $p
}
}
return $result
}
# @section Paths, Searching
# @find-executable-path name
#
# Searches the path for an executable with the given name.
# Note that the name may include some parameters, e.g. 'cc -mbig-endian',
# in which case the parameters are ignored.
# The full path to the executable if found, or "" if not found.
# Returns 1 if found, or 0 if not.
#
proc find-executable-path {name} {
# Ignore any parameters
set name [lindex $name 0]
# The empty string is never a valid executable
if {$name ne ""} {
foreach p [split-path] {
dputs "Looking for $name in $p"
set exec [file join $p $name]
if {[file-isexec $exec]} {
dputs "Found $name -> $exec"
return $exec
}
}
}
return {}
}
# @find-executable name
#
# Searches the path for an executable with the given name.
# Note that the name may include some parameters, e.g. 'cc -mbig-endian',
# in which case the parameters are ignored.
# Returns 1 if found, or 0 if not.
#
proc find-executable {name} {
if {[find-executable-path $name] eq {}} {
return 0
}
return 1
}
# @find-an-executable ?-required? name ...
#
# Given a list of possible executable names,
# searches for one of these on the path.
#
# Returns the name found, or "" if none found.
# If the first parameter is '-required', an error is generated
# if no executable is found.
#
proc find-an-executable {args} {
set required 0
if {[lindex $args 0] eq "-required"} {
set args [lrange $args 1 end]
incr required
}
foreach name $args {
if {[find-executable $name]} {
return $name
}
}
if {$required} {
if {[llength $args] == 1} {
user-error "failed to find: [join $args]"
} else {
user-error "failed to find one of: [join $args]"
}
}
return ""
}
# @section Logging, Messages and Errors
# @configlog msg
#
# Writes the given message to the configuration log, 'config.log'.
#
proc configlog {msg} {
if {![info exists ::autosetup(logfh)]} {
set ::autosetup(logfh) [open config.log w]
}
puts $::autosetup(logfh) $msg
}
# @msg-checking msg
#
# Writes the message with no newline to stdout.
#
proc msg-checking {msg} {
if {$::autosetup(msg-quiet) == 0} {
maybe-show-timestamp
puts -nonewline $msg
set ::autosetup(msg-checking) 1
}
}