2 # Copyright (c) 2006-2011 WorkWare Systems http://www.workware.net.au/
6 dir
=`dirname "$0"`; exec "`$dir/find-tclsh`" "$0" "$@"
8 set autosetup
(version
) 0.6.5
10 # Can be set to 1 to debug early-init problems
11 set autosetup
(debug
) 0
13 ##################################################################
15 # Main flow of control, option handling
18 global autosetup define
20 # There are 3 potential directories involved:
21 # 1. The directory containing autosetup (this script)
22 # 2. The directory containing auto.def
23 # 3. The current directory
25 # From this we need to determine:
26 # a. The path to this script (and related support files)
27 # b. The path to auto.def
28 # c. The build directory, where output files are created
30 # This is also complicated by the fact that autosetup may
31 # have been run via the configure wrapper ([getenv WRAPPER] is set)
34 # a. This script is $::argv0
35 # => dir, prog, exe, libdir
36 # b. auto.def is in the directory containing the configure wrapper,
37 # otherwise it is in the current directory.
39 # c. The build directory is the current directory
42 # 'misc' is needed before we can do anything, so set a temporary libdir
43 # in case this is the development version
44 set autosetup
(libdir
) [file dirname $
::argv0
]/lib
48 set autosetup
(dir
) [realdir
[file dirname [realpath $
::argv0
]]]
49 set autosetup
(prog
) [file join $autosetup(dir
) [file tail $
::argv0
]]
50 set autosetup
(exe
) [getenv WRAPPER
$autosetup(prog
)]
51 if {$autosetup(installed
)} {
52 set autosetup
(libdir
) $autosetup(dir
)
54 set autosetup
(libdir
) [file join $autosetup(dir
) lib
]
56 autosetup_add_dep
$autosetup(prog
)
59 if {[getenv WRAPPER
""] eq
""} {
61 set autosetup
(srcdir
) [pwd]
63 # Invoked via the configure wrapper
64 set autosetup
(srcdir
) [file dirname $autosetup(exe
)]
66 set autosetup
(autodef
) [relative-path
$autosetup(srcdir
)/auto.def
]
69 set autosetup
(builddir
) [pwd]
71 set autosetup
(argv
) $argv
72 set autosetup
(cmdline
) {}
73 set autosetup
(options
) {}
74 set autosetup
(optionhelp
) {}
75 set autosetup
(showhelp
) 0
80 array
set ::useropts
[getopt argv
]
84 help:=local => "display help and options. Optionally specify a module name, such as --help=system"
85 version
=> "display the version of autosetup"
86 ref
:=text manual
:=text
87 reference
:=text
=> "display the autosetup command reference. 'text', 'wiki', 'asciidoc' or 'markdown'"
88 debug
=> "display debugging output as autosetup runs"
89 install:=.
=> "install autosetup to the current or given directory (in the 'autosetup/' subdirectory)"
90 force init
:=help => "create initial auto.def, etc. Use --init=help for known types"
91 # Undocumented options
100 if {[opt-bool version
]} {
101 puts
$autosetup(version
)
105 # autosetup --conf=alternate-auto.def
106 if {[opt-val conf
] ne
""} {
107 set autosetup
(autodef
) [opt-val conf
]
110 # Debugging output (set this early)
111 incr autosetup
(debug
) [opt-bool debug
]
112 incr autosetup
(force
) [opt-bool force
]
113 incr autosetup
(msg-quiet
) [opt-bool quiet
]
114 incr autosetup
(msg-timing
) [opt-bool timing
]
116 # If the local module exists, source it now to allow for
117 # project-local customisations
118 if {[file exists
$autosetup(libdir
)/local.tcl
]} {
122 # Now any auto-load modules
123 foreach
file [glob
-nocomplain $autosetup(libdir
)/*.auto
$autosetup(libdir
)/*/*.auto
] {
124 automf_load
source $file
127 if {[opt-val
help] ne
""} {
128 incr autosetup
(showhelp
)
130 autosetup_help
[opt-val
help]
133 if {[opt-val
{manual ref reference
}] ne
""} {
135 autosetup_reference
[opt-val
{manual ref reference
}]
138 # Allow combining --install and --init
140 if {[opt-val
install] ne
""} {
142 autosetup_install
[opt-val
install]
146 if {[opt-val init
] ne
""} {
148 autosetup_init
[opt-val init
]
156 if {![file exists
$autosetup(autodef
)]} {
157 # Check for invalid option first
159 user-error
"No auto.def found in \"$autosetup(srcdir)\" (use [file tail $::autosetup(exe)] --init to create one)"
162 # Parse extra arguments into autosetup(cmdline)
164 if {[regexp
{([^
=]*)=(.
*)} $arg -> n v
]} {
165 dict
set autosetup
(cmdline
) $n $v
168 user-error
"Unexpected parameter: $arg"
172 autosetup_add_dep
$autosetup(autodef
)
174 define CONFIGURE_OPTS
""
175 foreach arg
$autosetup(argv
) {
176 define-append CONFIGURE_OPTS
[quote-if-needed
$arg]
178 define AUTOREMAKE
[file-normalize
$autosetup(exe
)]
179 define-append AUTOREMAKE
[get-define CONFIGURE_OPTS
]
182 # Log how we were invoked
183 configlog
"Invoked as: [getenv WRAPPER $::argv0] [quote-argv $autosetup(argv)]"
185 # Note that auto.def is *not* loaded in the global scope
186 source $autosetup(autodef
)
188 # Could warn here if options {} was not specified
192 if {$autosetup(debug
)} {
193 msg-result
"Writing all defines to config.log"
194 configlog
"================ defines ======================"
195 foreach n
[lsort
[array names define
]] {
196 configlog
"define $n $define($n)"
203 # @opt-bool option ...
205 # Check each of the named, boolean options and return 1 if any of them have
206 # been set by the user.
208 proc opt-bool
{args
} {
209 option-check-names
{*}$args
210 opt_bool
::useropts
{*}$args
213 # @opt-val option-list ?default=""?
215 # Returns a list containing all the values given for the non-boolean options in 'option-list'.
216 # There will be one entry in the list for each option given by the user, including if the
217 # same option was used multiple times.
218 # If only a single value is required, use something like:
220 ## lindex [opt-val $names] end
222 # If no options were set, $default is returned (exactly, not as a list).
224 proc opt-val
{names
{default
""}} {
225 option-check-names
{*}$names
226 join [opt_val
::useropts
$names $default]
229 proc option-check-names
{args
} {
231 if {$o ni $
::autosetup
(options
)} {
232 autosetup-error
"Request for undeclared option --$o"
237 # Parse the option definition in $opts and update
238 # ::useropts() and ::autosetup(optionhelp) appropriately
240 proc options-add
{opts
{header
""}} {
241 global useropts autosetup
243 # First weed out comment lines
245 foreach line
[split $opts \n] {
246 if {![string match
"#*" [string trimleft
$line]]} {
247 append realopts
$line \n
252 for {set i
0} {$i < [llength
$opts]} {incr i
} {
253 set opt
[lindex
$opts $i]
254 if {[string match
=* $opt]} {
255 # This is a special heading
256 lappend autosetup
(optionhelp
) $opt ""
261 #puts "i=$i, opt=$opt"
262 regexp
{^
([^
:=]*)(:)?
(=)?
(.
*)$
} $opt -> name colon equal value
263 if {$name in $autosetup(options
)} {
264 autosetup-error
"Option $name already specified"
267 #puts "$opt => $name $colon $equal $value"
269 # Find the corresponding value in the user options
270 # and set the default if necessary
271 if {[string match
"-*" $opt]} {
272 # This is a documentation-only option, like "-C <dir>"
274 } elseif
{$colon eq
""} {
276 lappend autosetup
(options
) $name
278 if {![info exists useropts
($name)]} {
279 set useropts
($name) $value
282 set opthelp
"--disable-$name"
284 set opthelp
"--$name"
288 lappend autosetup
(options
) $name
291 if {[info exists useropts
($name)]} {
292 # If the user specified the option with no value, the value will be "1"
293 # Replace with the default
294 if {$useropts($name) eq
"1"} {
295 set useropts
($name) $value
298 set opthelp
"--$name?=$value?"
300 set opthelp
"--$name=$value"
304 # Now create the help for this option if appropriate
305 if {[lindex
$opts $i+1] eq
"=>"} {
306 set desc
[lindex
$opts $i+2]
307 #string match \n* $desc
309 lappend autosetup
(optionhelp
) $header ""
312 # A multi-line description
313 lappend autosetup
(optionhelp
) $opthelp $desc
319 # @module-options optionlist
321 # Like 'options', but used within a module.
322 proc module-options
{opts
} {
324 if {$
::autosetup
(showhelp
) > 1 && [llength
$opts]} {
325 set header
"Module Options:"
327 options-add
$opts $header
329 if {$
::autosetup
(showhelp
)} {
330 # Ensure that the module isn't executed on --help
331 # We are running under eval or source, so use break
332 # to prevent further execution
333 #return -code break -level 2
339 expr {$a > $b ?
$a : $b}
342 proc options-wrap-desc
{text length firstprefix nextprefix initial
} {
344 set space
$firstprefix
345 foreach word
[split $text] {
346 set word
[string trim
$word]
350 if {$len && [string length
$space$word] + $len >= $length} {
353 set space
$nextprefix
355 incr len
[string length
$space$word]
356 puts
-nonewline $space$word
364 proc options-show
{} {
365 # Determine the max option width
367 foreach
{opt desc
} $
::autosetup
(optionhelp
) {
368 if {[string match
=* $opt] ||
[string match
\n* $desc]} {
371 set max
[max
$max [string length
$opt]]
373 set indent
[string repeat
" " [expr $max+4]]
374 set cols
[getenv COLUMNS
80]
376 lassign
[exec stty size
] rows cols
380 foreach
{opt desc
} $
::autosetup
(optionhelp
) {
381 if {[string match
=* $opt]} {
382 puts
[string range
$opt 1 end
]
385 puts
-nonewline " [format %-${max}s $opt]"
386 if {[string match
\n* $desc]} {
389 options-wrap-desc
[string trim
$desc] $cols " " $indent [expr $max + 2]
394 # @options options-spec
396 # Specifies configuration-time options which may be selected by the user
397 # and checked with opt-val and opt-bool. The format of options-spec follows.
399 # A boolean option is of the form:
401 ## name[=0|1] => "Description of this boolean option"
403 # The default is name=0, meaning that the option is disabled by default.
404 # If name=1 is used to make the option enabled by default, the description should reflect
405 # that with text like "Disable support for ...".
407 # An argument option (one which takes a parameter) is of the form:
409 ## name:[=]value => "Description of this option"
411 # If the name:value form is used, the value must be provided with the option (as --name=myvalue).
412 # If the name:=value form is used, the value is optional and the given value is used as the default
413 # if it is not provided.
415 # Undocumented options are also supported by omitting the "=> description.
416 # These options are not displayed with --help and can be useful for internal options or as aliases.
418 # For example, --disable-lfs is an alias for --disable=largefile:
420 ## lfs=1 largefile=1 => "Disable large file support"
422 proc options
{optlist
} {
423 # Allow options as a list or args
424 options-add
$optlist "Local Options:"
426 if {$
::autosetup
(showhelp
)} {
431 # Check for invalid options
432 if {[opt-bool option-checking
]} {
433 foreach o
[array names
::useropts
] {
434 if {$o ni $
::autosetup
(options
)} {
435 user-error
"Unknown option --$o"
441 proc config_guess
{} {
442 if {[file-isexec $
::autosetup
(dir
)/config.guess
]} {
443 exec-with-stderr sh $
::autosetup
(dir
)/config.guess
444 if {[catch
{exec-with-stderr sh $
::autosetup
(dir
)/config.guess
} alias]} {
449 configlog
"No config.guess, so using uname"
450 string tolower
[exec uname
-p]-unknown-[exec uname
-s][exec uname
-r]
454 proc config_sub
{alias} {
455 if {[file-isexec $
::autosetup
(dir
)/config.sub
]} {
456 if {[catch
{exec-with-stderr sh $
::autosetup
(dir
)/config.sub
$alias} alias]} {
463 # @define name ?value=1?
465 # Defines the named variable to the given value.
466 # These (name, value) pairs represent the results of the configuration check
467 # and are available to be checked, modified and substituted.
469 proc define
{name
{value
1}} {
470 set ::define
($name) $value
471 #dputs "$name <= $value"
476 # Undefine the named variable
478 proc undefine
{name
} {
479 unset -nocomplain ::define
($name)
480 #dputs "$name <= <undef>"
483 # @define-append name value ...
485 # Appends the given value(s) to the given 'defined' variable.
486 # If the variable is not defined or empty, it is set to $value.
487 # Otherwise the value is appended, separated by a space.
488 # Any extra values are similarly appended.
489 # If any value is already contained in the variable (as a substring) it is omitted.
491 proc define-append
{name args
} {
492 if {[get-define
$name ""] ne
""} {
493 # Make a token attempt to avoid duplicates
495 if {[string first
$arg $
::define
($name)] == -1} {
496 append
::define
($name) " " $arg
500 set ::define
($name) [join $args]
502 #dputs "$name += [join $args] => $::define($name)"
505 # @get-define name ?default=0?
507 # Returns the current value of the 'defined' variable, or $default
510 proc get-define
{name
{default
0}} {
511 if {[info exists
::define
($name)]} {
512 #dputs "$name => $::define($name)"
513 return $
::define
($name)
515 #dputs "$name => $default"
521 # Returns 1 if the given variable is defined.
523 proc is-defined
{name
} {
524 info exists
::define
($name)
529 # Returns a dictionary (name value list) of all defined variables.
531 # This is suitable for use with 'dict', 'array set' or 'foreach'
532 # and allows for arbitrary processing of the defined variables.
534 proc all-defines
{} {
539 # @get-env name default
541 # If $name was specified on the command line, return it.
542 # If $name was set in the environment, return it.
543 # Otherwise return $default.
545 proc get-env
{name default
} {
546 if {[dict exists $
::autosetup
(cmdline
) $name]} {
547 return [dict get $
::autosetup
(cmdline
) $name]
549 getenv
$name $default
554 # Returns 1 if the $name was specified on the command line or in the environment.
555 # Note that an empty environment variable is not considered to be set.
557 proc env-is-set
{name
} {
558 if {[dict exists $
::autosetup
(cmdline
) $name]} {
561 if {[getenv
$name ""] ne
""} {
567 # @readfile filename ?default=""?
569 # Return the contents of the file, without the trailing newline.
570 # If the file doesn't exist or can't be read, returns $default.
572 proc readfile
{filename
{default_value
""}} {
573 set result
$default_value
575 set f
[open
$filename]
576 set result
[read -nonewline $f]
582 # @writefile filename value
584 # Creates the given file containing $value.
585 # Does not add an extra newline.
587 proc writefile
{filename value
} {
588 set f
[open
$filename w
]
589 puts
-nonewline $f $value
593 proc quote-if-needed
{str
} {
594 if {[string match
{*[\" ]*} $str]} {
595 return \"[string map
[list
\" \\" \\ \\\\] $str]\"
600 proc quote-argv {argv} {
603 lappend args [quote-if-needed $arg]
610 # Takes a list and returns a new list with $suf appended
613 ## suffix .c {a b c} => {a.c b.c c.c}
615 proc suffix {suf list} {
618 lappend result $p$suf
625 # Takes a list and returns a new list with $pre prepended
628 ## prefix jim- {a.c b.c} => {jim-a.c jim-b.c}
630 proc prefix {pre list} {
633 lappend result $pre$p
638 # @find-executable name
640 # Searches the path for an executable with the given name.
641 # Note that the name may include some parameters, e.g. "cc
-mbig-endian",
642 # in which case the parameters are ignored.
643 # Returns 1 if found, or 0 if not.
645 proc find-executable {name} {
646 # Ignore any parameters
647 set name [lindex $name 0]
649 # The empty string is never a valid executable
652 foreach p [split-path] {
653 dputs "Looking
for $name in $p"
654 set exec [file join $p $name]
655 if {[file-isexec $exec]} {
656 dputs "Found
$name -> $exec"
663 # @find-an-executable ?-required? name ...
665 # Given a list of possible executable names,
666 # searches for one of these on the path.
668 # Returns the name found, or "" if none found.
669 # If the first parameter is '-required', an error is generated
670 # if no executable is found.
672 proc find-an-executable {args} {
674 if {[lindex $args 0] eq "-required"} {
675 set args [lrange $args 1 end]
679 if {[find-executable $name]} {
684 if {[llength $args] == 1} {
685 user-error "failed to
find: [join $args]"
687 user-error "failed to
find one of
: [join $args]"
695 # Writes the given message to the configuration log, config.log
697 proc configlog {msg} {
698 if {![info exists ::autosetup(logfh)]} {
699 set ::autosetup(logfh) [open config.log w]
701 puts $::autosetup(logfh) $msg
706 # Writes the message with no newline to stdout.
708 proc msg-checking {msg} {
709 if {$::autosetup(msg-quiet) == 0} {
712 set ::autosetup(msg-checking) 1
718 # Writes the message to stdout.
720 proc msg-result {msg} {
721 if {$::autosetup(msg-quiet) == 0} {
724 set ::autosetup(msg-checking) 0
729 # @msg-quiet command ...
731 # msg-quiet evaluates it's arguments as a command with output
732 # from msg-checking and msg-result suppressed.
734 # This is useful if a check needs to run a subcheck which isn't
735 # of interest to the user.
736 proc msg-quiet {args} {
737 incr ::autosetup(msg-quiet)
738 set rc [uplevel 1 $args]
739 incr ::autosetup(msg-quiet) -1
743 # Will be overridden by 'use misc'
744 proc error-stacktrace {msg} {
748 proc error-location {msg} {
752 ##################################################################
757 if {$::autosetup(debug)} {
762 ##################################################################
764 # User and system warnings and errors
766 # Usage errors such as wrong command line options
770 # Indicate incorrect usage to the user, including if required components
771 # or features are not found.
772 # autosetup exits with a non-zero return code.
774 proc user-error {msg} {
776 puts stderr "Error
: $msg"
777 puts stderr "Try
: '[file tail $::autosetup(exe)] --help' for options
"
783 # Output the given message to stderr.
785 proc user-notice {msg} {
786 lappend ::autosetup(notices) $msg
789 # Incorrect usage in the auto.def file. Identify the location.
790 proc autosetup-error {msg} {
791 autosetup-full-error [error-location $msg]
794 # Like autosetup-error, except $msg is the full error message.
795 proc autosetup-full-error {msg} {
801 proc show-notices {} {
802 if {$::autosetup(msg-checking)} {
804 set ::autosetup(msg-checking) 0
807 if {[info exists ::autosetup(notices)]} {
808 puts stderr [join $::autosetup(notices) \n]
809 unset ::autosetup(notices)
813 proc maybe-show-timestamp {} {
814 if {$::autosetup(msg-timing) && $::autosetup(msg-checking) == 0} {
815 puts -nonewline [format {[%6.2f] } [expr {([clock millis] - $::autosetup(start)) % 10000 / 1000.0}]]
819 proc autosetup_version {} {
820 return "autosetup v$
::autosetup
(version
)"
823 ##################################################################
825 # Directory/path handling
836 # Follow symlinks until we get to something which is not a symlink
837 proc realpath {path} {
840 set path [file readlink $path]
849 # Convert absolute path, $path into a path relative
850 # to the given directory (or the current dir, if not given).
852 proc relative-path {path {pwd {}}} {
857 set path [file-normalize $path]
861 set pwd [file-normalize $pwd]
868 # Try to make the filename relative to the current dir
869 foreach p [split $pwd /] f [split $path /] {
877 # Add .. for sibling or parent dir
885 if {$same == 1 || [llength $prefix] > 3} {
889 file join [join $prefix /] [join $newf /]
892 # Add filename as a dependency to rerun autosetup
893 # The name will be normalised (converted to a full path)
895 proc autosetup_add_dep {filename} {
896 lappend ::autosetup(deps) [file-normalize $filename]
899 ##################################################################
901 # Library module support
906 # Load the given library modules.
907 # e.g. 'use cc cc-shared'
909 # Note that module 'X' is implemented in either 'autosetup/X.tcl'
910 # or 'autosetup/X/init.tcl'
912 # The latter form is useful for a complex module which requires additional
913 # support file. In this form, '$::usedir' is set to the module directory
918 if {[info exists ::libmodule($m)]} {
921 set ::libmodule($m) 1
922 if {[info exists ::modsource($m)]} {
923 automf_load eval $::modsource($m)
925 set sources [list $::autosetup(libdir)/${m}.tcl $::autosetup(libdir)/${m}/init.tcl]
927 foreach source $sources {
928 if {[file exists $source]} {
934 # For the convenience of the "use
" source, point to the directory
935 # it is being loaded from
936 set ::usedir [file dirname $source]
937 automf_load source $source
938 autosetup_add_dep $source
940 autosetup-error "use
: No such module
: $m"
946 # Load module source in the global scope by executing the given command
947 proc automf_load {args} {
948 if {[catch [list uplevel #0 $args] msg opts] ni {0 2 3}} {
949 autosetup-full-error [error-dump $msg $opts $::autosetup(debug)]
954 set autosetup(exe) $::argv0
955 set autosetup(istcl) 1
956 set autosetup(start) [clock millis]
957 set autosetup(installed) 0
958 set autosetup(msg-checking) 0
959 set autosetup(msg-quiet) 0
961 # Embedded modules are inserted below here
962 set autosetup(installed) 1
963 # ----- module asciidoc-formatting -----
965 set modsource(asciidoc-formatting) {
966 # Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
967 # All rights reserved
969 # Module which provides text formatting
975 regsub -all "\
[ \t\n\
]+" [string trim $text] " "
978 underline [para $text] =
986 foreach line [parse_code_block $text] {
991 proc codelines {lines} {
992 foreach line $lines {
1000 proc underline {text char} {
1001 regexp "^
(\
[ \t\
]*)(.
*)" $text -> indent words
1003 puts $indent[string repeat $char [string length $words]]
1005 proc section {text} {
1006 underline "[para
$text]" -
1009 proc subsection {text} {
1013 proc bullet {text} {
1014 puts "* [para
$text]"
1016 proc indent {text} {
1020 proc defn {first args} {
1027 set defn [string trim [join $args \n]]
1028 regsub -all "\n\n" $defn "\n ::\n" defn
1033 # ----- module formatting -----
1035 set modsource(formatting) {
1036 # Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
1037 # All rights reserved
1039 # Module which provides common text formatting
1041 # This is designed for documenation which looks like:
1048 # In the second case, we need to work out the indenting
1049 # and strip it from all lines but preserve the remaining indenting.
1050 # Note that all lines need to be indented with the same initial
1053 # Returns a list of lines with the indenting removed.
1055 proc parse_code_block {text} {
1056 # If the text begins with newline, take the following text,
1057 # otherwise just return the original
1058 if {![regexp "^
\n(.
*)" $text -> text]} {
1059 return [list [string trim $text]]
1062 # And trip spaces off the end
1063 set text [string trimright $text]
1066 # Examine each line to determine the minimum indent
1067 foreach line [split $text \n] {
1069 # Ignore empty lines for the indent calculation
1072 regexp "^
(\
[ \t\
]*)" $line -> indent
1073 set len [string length $indent]
1079 # Now make a list of lines with this indent removed
1081 foreach line [split $text \n] {
1082 lappend lines [string range $line $min end]
1090 # ----- module getopt -----
1092 set modsource(getopt) {
1093 # Copyright (c) 2006 WorkWare Systems http://www.workware.net.au/
1094 # All rights reserved
1096 # Simple getopt module
1098 # Parse everything out of the argv list which looks like an option
1099 # Knows about --enable-thing and --disable-thing as alternatives for --thing=0 or --thing=1
1100 # Everything which doesn't look like an option, or is after --, is left unchanged
1101 proc getopt {argvname} {
1102 upvar $argvname argv
1105 for {set i 0} {$i < [llength $argv]} {incr i} {
1106 set arg [lindex $argv $i]
1113 lappend nargv {*}[lrange $argv $i end]
1117 if {[regexp {^--([^=][^=]+)=(.*)$} $arg -> name value]} {
1118 lappend opts($name) $value
1119 } elseif {[regexp {^--(enable-|disable-)?([^=]*)$} $arg -> prefix name]} {
1120 if {$prefix eq "disable-
"} {
1125 lappend opts($name) $value
1131 #puts "getopt
: argv
=[join $argv] => [join $nargv]"
1136 return [array get opts]
1139 proc opt_val {optarrayname options {default {}}} {
1140 upvar $optarrayname opts
1144 foreach o $options {
1145 if {[info exists opts($o)]} {
1146 lappend result {*}$opts($o)
1149 if {[llength $result] == 0} {
1155 proc opt_bool {optarrayname args} {
1156 upvar $optarrayname opts
1158 # Support the args being passed as a list
1159 if {[llength $args] == 1} {
1160 set args [lindex $args 0]
1164 if {[info exists opts($o)]} {
1165 if {"1" in $opts($o) || "yes" in $opts($o)} {
1174 # ----- module help -----
1176 set modsource(help) {
1177 # Copyright (c) 2010 WorkWare Systems http://workware.net.au/
1178 # All rights reserved
1180 # Module which provides usage, help and the command reference
1182 proc autosetup_help {what} {
1185 puts "Usage
: [file tail $
::autosetup
(exe
)] \
[options\
] \
[settings\
]\n"
1186 puts "This is
[autosetup_version
], a build environment
\"autoconfigurator
\""
1187 puts "See the documentation online
at http
://msteveb.github.com
/autosetup
/\n"
1189 if {$what eq "local"} {
1190 if {[file exists $::autosetup(autodef)]} {
1191 # This relies on auto.def having a call to 'options'
1192 # which will display options and quit
1193 source $::autosetup(autodef)
1198 incr ::autosetup(showhelp)
1199 if {[catch {use $what}]} {
1200 user-error "Unknown module
: $what"
1208 # If not already paged and stdout is a tty, pipe the output through the pager
1209 # This is done by reinvoking autosetup with --nopager added
1211 if {![opt-bool nopager] && [getenv PAGER ""] ne "" && [isatty? stdin] && [isatty? stdout]} {
1213 exec [info nameofexecutable] $::argv0 --nopager {*}$::argv |& {*}[getenv PAGER] >@stdout <@stdin 2>@stderr
1215 if {[dict get $opts -errorcode] eq "NONE
"} {
1216 # an internal/exec error
1225 # Outputs the autosetup references in one of several formats
1226 proc autosetup_reference {{type text}} {
1230 switch -glob -- $type {
1231 wiki {use wiki-formatting}
1232 ascii* {use asciidoc-formatting}
1233 md - markdown {use markdown-formatting}
1234 default {use text-formatting}
1237 title "[autosetup_version
] -- Command Reference
"
1239 section {Introduction}
1242 See http://msteveb.github.com/autosetup/ for the online documentation for 'autosetup'
1246 'autosetup' provides a number of built-in commands which
1247 are documented below. These may be used from 'auto.def' to test
1248 for features, define variables, create files from templates and
1249 other similar actions.
1252 automf_command_reference
1257 proc autosetup_output_block {type lines} {
1258 if {[llength $lines]} {
1267 foreach line $lines {
1276 # Generate a command reference from inline documentation
1277 proc automf_command_reference {} {
1278 lappend files $::autosetup(prog)
1279 lappend files {*}[lsort [glob -nocomplain $::autosetup(libdir)/*.tcl]]
1281 section "Core Commands
"
1286 foreach file $files {
1291 # Find lines starting with "# @*" and continuing through the remaining comment lines
1292 if {![regexp
{^
# @(.*)} $line -> cmd]} {
1296 # Synopsis or command?
1297 if {$cmd eq
"synopsis:"} {
1298 section
"Module: [file rootname [file tail $file]]"
1306 # Now the description
1310 if {![regexp
{^
#(#)? ?(.*)} $line -> hash cmd]} {
1315 } elseif
{[regexp
{^
- (.
*)} $cmd -> cmd
]} {
1321 #puts "hash=$hash, oldhash=$oldhash, lines=[llength $lines], cmd=$cmd"
1323 if {$t ne
$type ||
$cmd eq
""} {
1324 # Finish the current block
1325 autosetup_output_block
$type $lines
1334 autosetup_output_block
$type $lines
1341 # ----- module init -----
1343 set modsource
(init
) {
1344 # Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
1345 # All rights reserved
1347 # Module to help create auto.def and configure
1349 proc autosetup_init
{type} {
1351 if {$type in {?
help}} {
1353 } elseif
{![dict exists $
::autosetup
(inittypes
) $type]} {
1354 puts
"Unknown type, --init=$type"
1358 puts
"Use one of the following types (e.g. --init=make)\n"
1359 foreach
type [lsort
[dict keys $
::autosetup
(inittypes
)]] {
1360 lassign
[dict get $
::autosetup
(inittypes
) $type] desc
1361 # XXX: Use the options-show code to wrap the description
1362 puts
[format
"%-10s %s" $type $desc]
1366 lassign
[dict get $
::autosetup
(inittypes
) $type] desc
script
1368 puts
"Initialising $type: $desc\n"
1370 # All initialisations happens in the top level srcdir
1371 cd $
::autosetup
(srcdir
)
1376 proc autosetup_add_init_type
{type desc
script} {
1377 dict
set ::autosetup
(inittypes
) $type [list
$desc $script]
1380 # This is for in creating build-system init scripts
1382 # If the file doesn't exist, create it containing $contents
1383 # If the file does exist, only overwrite if --force is specified.
1385 proc autosetup_check_create
{filename contents
} {
1386 if {[file exists
$filename]} {
1387 if {!$
::autosetup
(force
)} {
1388 puts
"I see $filename already exists."
1391 puts
"I will overwrite the existing $filename because you used --force."
1394 puts
"I don't see $filename, so I will create it."
1396 writefile
$filename $contents
1400 # ----- module install -----
1402 set modsource
(install) {
1403 # Copyright (c) 2006-2010 WorkWare Systems http://www.workware.net.au/
1404 # All rights reserved
1406 # Module which can install autosetup
1408 proc autosetup_install
{dir
} {
1411 file mkdir autosetup
1413 set f
[open autosetup
/autosetup w
]
1415 set publicmodules
[glob $
::autosetup
(libdir
)/*.auto
]
1417 # First the main script, but only up until "CUT HERE"
1418 set in [open $
::autosetup
(dir
)/autosetup
]
1419 while {[gets
$in buf
] >= 0} {
1420 if {$buf ne
"##-- CUT HERE --##"} {
1425 # Insert the static modules here
1426 # i.e. those which don't contain @synopsis:
1427 puts
$f "set autosetup(installed) 1"
1428 foreach
file [lsort
[glob $
::autosetup
(libdir
)/*.tcl
]] {
1429 set buf
[readfile
$file]
1430 if {[string match
"*\n# @synopsis:*" $buf]} {
1431 lappend publicmodules
$file
1434 set modname
[file rootname
[file tail $file]]
1435 puts
$f "# ----- module $modname -----"
1436 puts
$f "\nset modsource($modname) \{"
1443 exec chmod 755 autosetup
/autosetup
1445 # Install public modules
1446 foreach
file $publicmodules {
1447 autosetup_install_file
$file autosetup
1450 # Install support files
1451 foreach
file {config.guess config.sub jimsh0.c find-tclsh test-tclsh LICENSE
} {
1452 autosetup_install_file $
::autosetup
(dir
)/$file autosetup
1454 exec chmod 755 autosetup
/config.sub autosetup
/config.guess autosetup
/find-tclsh
1456 writefile autosetup
/README.autosetup \
1457 "This is [autosetup_version]. See http://msteveb.github.com/autosetup/\n"
1460 user-error
"Failed to install autosetup: $error"
1462 puts
"Installed [autosetup_version] to autosetup/"
1464 # Now create 'configure' if necessary
1465 autosetup_create_configure
1468 proc autosetup_create_configure
{} {
1469 if {[file exists configure
]} {
1470 if {!$
::autosetup
(force
)} {
1471 # Could this be an autosetup configure?
1472 if {![string match
"*\nWRAPPER=*" [readfile configure
]]} {
1473 puts
"I see configure, but not created by autosetup, so I won't overwrite it."
1474 puts
"Remove it or use --force to overwrite."
1478 puts
"I will overwrite the existing configure because you used --force."
1481 puts
"I don't see configure, so I will create it."
1483 writefile configure \
1485 dir
="`dirname "$0"`/autosetup"
1486 WRAPPER
="$0"; export WRAPPER
; exec "`$dir/find-tclsh`" "$dir/autosetup" "$@"
1488 catch
{exec chmod 755 configure
}
1491 # Append the contents of $file to filehandle $f
1492 proc autosetup_install_append
{f
file} {
1498 proc autosetup_install_file
{file dir
} {
1499 if {![file exists
$file]} {
1500 error
"Missing installation file '$file'"
1502 writefile
[file join $dir [file tail $file]] [readfile
$file]\n
1505 if {$
::autosetup
(installed
)} {
1506 user-error
"autosetup can only be installed from development source, not from installed copy"
1510 # ----- module markdown-formatting -----
1512 set modsource
(markdown-formatting
) {
1513 # Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
1514 # All rights reserved
1516 # Module which provides text formatting
1517 # markdown format (kramdown syntax)
1522 regsub
-all "\[ \t\n\]+" [string trim
$text] " " text
1523 regsub
-all {([^a-zA-Z
])'([^']*)'} $text {\1**`\2`**} text
1524 regsub -all {^'([^
']*)'} $text {**`\1`**} text
1525 regsub
-all {(http
[^
\t\n]*)} $text {[\
1](\
1)} text
1529 underline
[para
$text] =
1536 proc codelines
{lines
} {
1538 foreach line
$lines {
1546 foreach line
[parse_code_block
$text] {
1555 proc underline
{text char
} {
1556 regexp
"^(\[ \t\]*)(.*)" $text -> indent words
1558 puts
$indent[string repeat
$char [string length
$words]]
1560 proc section
{text
} {
1561 underline
"[para $text]" -
1564 proc subsection
{text
} {
1568 proc bullet
{text
} {
1569 puts
"* [para $text]"
1571 proc defn
{first args
} {
1573 set defn
[string trim
[join $args \n]]
1576 puts
-nonewline ": "
1577 regsub
-all "\n\n" $defn "\n: " defn
1583 # ----- module misc -----
1585 set modsource
(misc
) {
1586 # Copyright (c) 2007-2010 WorkWare Systems http://www.workware.net.au/
1587 # All rights reserved
1589 # Module containing misc procs useful to modules
1590 # Largely for platform compatibility
1592 set autosetup
(istcl
) [info exists
::tcl_library
]
1593 set autosetup
(iswin
) [string equal windows
$tcl_platform(platform
)]
1595 if {$autosetup(iswin
)} {
1596 # mingw/windows separates $PATH with semicolons
1597 # and doesn't have an executable bit
1598 proc split-path
{} {
1599 split [getenv PATH .
] {;}
1601 proc file-isexec
{exec} {
1602 # Basic test for windows. We ignore .bat
1603 if {[file isfile
$exec] ||
[file isfile
$exec.exe
]} {
1609 # unix separates $PATH with colons and has and executable bit
1610 proc split-path
{} {
1611 split [getenv PATH .
] :
1613 proc file-isexec
{exec} {
1614 file executable
$exec
1618 # Assume that exec can return stdout and stderr
1619 proc exec-with-stderr
{args
} {
1623 if {$autosetup(istcl
)} {
1624 # Tcl doesn't have the env command
1625 proc getenv
{name args
} {
1626 if {[info exists
::env
($name)]} {
1627 return $
::env
($name)
1629 if {[llength
$args]} {
1630 return [lindex
$args 0]
1632 return -code error
"environment variable \"$name\" does not exist"
1634 proc isatty?
{channel
} {
1635 dict exists
[fconfigure
$channel] -xchar
1638 if {$autosetup(iswin
)} {
1639 # On Windows, backslash convert all environment variables
1640 # (Assume that Tcl does this for us)
1641 proc getenv
{name args
} {
1642 string map
{\\ /} [env
$name {*}$args]
1645 # Jim on unix is simple
1648 proc isatty?
{channel
} {
1651 # isatty is a recent addition to Jim Tcl
1652 set tty
[$channel isatty
]
1658 # In case 'file normalize' doesn't exist
1660 proc file-normalize
{path
} {
1661 if {[catch
{file normalize
$path} result
]} {
1666 if {[file isdir
$path]} {
1670 cd [file dirname $path]
1671 set result
[file join [pwd] [file tail $path]]
1678 # If everything is working properly, the only errors which occur
1679 # should be generated in user code (e.g. auto.def).
1680 # By default, we only want to show the error location in user code.
1681 # We use [info frame] to achieve this, but it works differently on Tcl and Jim.
1683 # This is designed to be called for incorrect usage in auto.def, via autosetup-error
1685 proc error-location
{msg
} {
1686 if {$
::autosetup
(debug
)} {
1687 return -code error
$msg
1689 # Search back through the stack trace for the first error in a .def file
1690 for {set i
1} {$i < [info level
]} {incr i
} {
1691 if {$
::autosetup
(istcl
)} {
1692 array
set info
[info frame
-$i]
1694 lassign
[info frame
-$i] info
(caller
) info
(file) info
(line
)
1696 if {[string match
*.def
$info(file)]} {
1697 return "[relative-path $info(file)]:$info(line): Error: $msg"
1699 #puts "Skipping $info(file):$info(line)"
1704 # If everything is working properly, the only errors which occur
1705 # should be generated in user code (e.g. auto.def).
1706 # By default, we only want to show the error location in user code.
1707 # We use [info frame] to achieve this, but it works differently on Tcl and Jim.
1709 # This is designed to be called for incorrect usage in auto.def, via autosetup-error
1711 proc error-stacktrace
{msg
} {
1712 if {$
::autosetup
(debug
)} {
1713 return -code error
$msg
1715 # Search back through the stack trace for the first error in a .def file
1716 for {set i
1} {$i < [info level
]} {incr i
} {
1717 if {$
::autosetup
(istcl
)} {
1718 array
set info
[info frame
-$i]
1720 lassign
[info frame
-$i] info
(caller
) info
(file) info
(line
)
1722 if {[string match
*.def
$info(file)]} {
1723 return "[relative-path $info(file)]:$info(line): Error: $msg"
1725 #puts "Skipping $info(file):$info(line)"
1730 # Given the return from [catch {...} msg opts], returns an appropriate
1731 # error message. A nice one for Jim and a less-nice one for Tcl.
1732 # If 'fulltrace' is set, a full stack trace is provided.
1733 # Otherwise a simple message is provided.
1735 # This is designed for developer errors, e.g. in module code or auto.def code
1738 proc error-dump
{msg opts fulltrace
} {
1739 if {$
::autosetup
(istcl
)} {
1741 return "Error: [dict get $opts -errorinfo]"
1743 return "Error: $msg"
1746 lassign
$opts(-errorinfo) p f l
1748 set result
"$f:$l: Error: "
1750 append result
"$msg\n"
1752 append result
[stackdump
$opts(-errorinfo)]
1755 # Remove the trailing newline
1761 # ----- module text-formatting -----
1763 set modsource
(text-formatting
) {
1764 # Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
1765 # All rights reserved
1767 # Module which provides text formatting
1771 proc wordwrap
{text length
{firstprefix
""} {nextprefix
""}} {
1773 set space
$firstprefix
1774 foreach word
[split $text] {
1775 set word
[string trim
$word]
1779 if {$len && [string length
$space$word] + $len >= $length} {
1782 set space
$nextprefix
1784 incr len
[string length
$space$word]
1786 # Use man-page conventions for highlighting 'quoted' and *quoted*
1788 # Use x^Hx for *bold* and _^Hx for 'underline'.
1790 # less and more will both understand this.
1791 # Pipe through 'col -b' to remove them.
1792 if {[regexp
{^
'(.*)'([^a-zA-Z0-9_
]*)$
} $word -> bareword dot
]} {
1793 regsub
-all .
$bareword "_\b&" word
1795 } elseif
{[regexp
{^
[*](.
*)[*]([^a-zA-Z0-9_
]*)$
} $word -> bareword dot
]} {
1796 regsub
-all .
$bareword "&\b&" word
1799 puts
-nonewline $space$word
1807 underline
[string trim
$text] =
1814 proc codelines
{lines
} {
1815 foreach line
$lines {
1823 proc underline
{text char
} {
1824 regexp
"^(\[ \t\]*)(.*)" $text -> indent words
1826 puts
$indent[string repeat
$char [string length
$words]]
1828 proc section
{text
} {
1829 underline
"[string trim $text]" -
1832 proc subsection
{text
} {
1836 proc bullet
{text
} {
1837 wordwrap
$text 76 " * " " "
1839 proc indent
{text
} {
1840 wordwrap
$text 76 " " " "
1842 proc defn
{first args
} {
1844 underline
" $first" ~
1854 # ----- module wiki-formatting -----
1856 set modsource
(wiki-formatting
) {
1857 # Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
1858 # All rights reserved
1860 # Module which provides text formatting
1861 # wiki.tcl.tk format output
1865 proc joinlines
{text
} {
1867 foreach l
[split [string trim
$text] \n] {
1868 lappend lines
[string trim
$l]
1873 puts
[joinlines
$text]
1877 puts
"*** [joinlines $text] ***"
1880 proc codelines
{lines
} {
1882 foreach line
$lines {
1889 foreach line
[parse_code_block
$text] {
1896 proc section
{text
} {
1900 proc subsection
{text
} {
1904 proc bullet
{text
} {
1905 puts
" * [joinlines $text]"
1907 proc indent
{text
} {
1908 puts
" : [joinlines $text]"
1910 proc defn
{first args
} {
1922 ##################################################################
1926 if {$autosetup(debug
)} {
1929 if {[catch
{main
$argv} msg opts
] == 1} {
1931 autosetup-full-error
[error-dump
$msg $opts $
::autosetup
(debug
)]
1932 if {!$autosetup(debug
)} {
1933 puts stderr
"Try: '[file tail $autosetup(exe)] --debug' for a full stack trace"