| 1 |
#!/bin/sh |
| 2 |
# Copyright (c) 2006-2011 WorkWare Systems http://www.workware.net.au/ |
| 3 |
# All rights reserved |
| 4 |
# vim:se syntax=tcl: |
| 5 |
# \ |
| 6 |
dir=`dirname "$0"`; exec "`$dir/autosetup-find-tclsh`" "$0" "$@" |
| 7 |
|
| 8 |
# Note that the version has a trailing + on unreleased versions |
| 9 |
set autosetup(version) 0.7.1+ |
| 10 |
|
| 11 |
# Can be set to 1 to debug early-init problems |
| 12 |
set autosetup(debug) [expr {"--debug" in $argv}] |
| 13 |
|
| 14 |
################################################################## |
| 15 |
# |
| 16 |
# Main flow of control, option handling |
| 17 |
# |
| 18 |
proc main {argv} { |
| 19 |
global autosetup define |
| 20 |
|
| 21 |
# There are 3 potential directories involved: |
| 22 |
# 1. The directory containing autosetup (this script) |
| 23 |
# 2. The directory containing auto.def |
| 24 |
# 3. The current directory |
| 25 |
|
| 26 |
# From this we need to determine: |
| 27 |
# a. The path to this script (and related support files) |
| 28 |
# b. The path to auto.def |
| 29 |
# c. The build directory, where output files are created |
| 30 |
|
| 31 |
# This is also complicated by the fact that autosetup may |
| 32 |
# have been run via the configure wrapper ([getenv WRAPPER] is set) |
| 33 |
|
| 34 |
# Here are the rules. |
| 35 |
# a. This script is $::argv0 |
| 36 |
# => dir, prog, exe, libdir |
| 37 |
# b. auto.def is in the directory containing the configure wrapper, |
| 38 |
# otherwise it is in the current directory. |
| 39 |
# => srcdir, autodef |
| 40 |
# c. The build directory is the current directory |
| 41 |
# => builddir, [pwd] |
| 42 |
|
| 43 |
# 'misc' is needed before we can do anything, so set a temporary libdir |
| 44 |
# in case this is the development version |
| 45 |
set autosetup(libdir) [file dirname $::argv0]/lib |
| 46 |
use misc |
| 47 |
|
| 48 |
# (a) |
| 49 |
set autosetup(dir) [realdir [file dirname [realpath $::argv0]]] |
| 50 |
set autosetup(prog) [file join $autosetup(dir) [file tail $::argv0]] |
| 51 |
set autosetup(exe) [getenv WRAPPER $autosetup(prog)] |
| 52 |
if {$autosetup(installed)} { |
| 53 |
set autosetup(libdir) $autosetup(dir) |
| 54 |
} else { |
| 55 |
set autosetup(libdir) [file join $autosetup(dir) lib] |
| 56 |
} |
| 57 |
autosetup_add_dep $autosetup(prog) |
| 58 |
|
| 59 |
# (b) |
| 60 |
if {[getenv WRAPPER ""] eq ""} { |
| 61 |
# Invoked directly |
| 62 |
set autosetup(srcdir) [pwd] |
| 63 |
} else { |
| 64 |
# Invoked via the configure wrapper |
| 65 |
set autosetup(srcdir) [file-normalize [file dirname $autosetup(exe)]] |
| 66 |
} |
| 67 |
set autosetup(autodef) [relative-path $autosetup(srcdir)/auto.def] |
| 68 |
|
| 69 |
# (c) |
| 70 |
set autosetup(builddir) [pwd] |
| 71 |
|
| 72 |
set autosetup(argv) $argv |
| 73 |
set autosetup(cmdline) {} |
| 74 |
# options is a list of known options |
| 75 |
set autosetup(options) {} |
| 76 |
# optset is a dictionary of option values set by the user based on getopt |
| 77 |
set autosetup(optset) {} |
| 78 |
# optdefault is a dictionary of default values |
| 79 |
set autosetup(optdefault) {} |
| 80 |
# options-defaults is a dictionary of overrides for default values for options |
| 81 |
set autosetup(options-defaults) {} |
| 82 |
set autosetup(optionhelp) {} |
| 83 |
set autosetup(showhelp) 0 |
| 84 |
|
| 85 |
use util |
| 86 |
|
| 87 |
# Parse options |
| 88 |
use getopt |
| 89 |
|
| 90 |
# At the is point we don't know what is a valid option |
| 91 |
# We simply parse anything that looks like an option |
| 92 |
set autosetup(getopt) [getopt argv] |
| 93 |
|
| 94 |
#"=Core Options:" |
| 95 |
options-add { |
| 96 |
help:=all => "display help and options. Optional: module name, such as --help=system" |
| 97 |
licence license => "display the autosetup license" |
| 98 |
version => "display the version of autosetup" |
| 99 |
ref:=text manual:=text |
| 100 |
reference:=text => "display the autosetup command reference. 'text', 'wiki', 'asciidoc' or 'markdown'" |
| 101 |
debug => "display debugging output as autosetup runs" |
| 102 |
install:=. => "install autosetup to the current or given directory" |
| 103 |
} |
| 104 |
if {$autosetup(installed)} { |
| 105 |
# hidden options so we can produce a nice error |
| 106 |
options-add { |
| 107 |
sysinstall:path |
| 108 |
} |
| 109 |
} else { |
| 110 |
options-add { |
| 111 |
sysinstall:path => "install standalone autosetup to the given directory (e.g.: /usr/local)" |
| 112 |
} |
| 113 |
} |
| 114 |
options-add { |
| 115 |
force init:=help => "create initial auto.def, etc. Use --init=help for known types" |
| 116 |
# Undocumented options |
| 117 |
option-checking=1 |
| 118 |
nopager |
| 119 |
quiet |
| 120 |
timing |
| 121 |
conf: |
| 122 |
} |
| 123 |
|
| 124 |
if {[opt-bool version]} { |
| 125 |
puts $autosetup(version) |
| 126 |
exit 0 |
| 127 |
} |
| 128 |
|
| 129 |
# autosetup --conf=alternate-auto.def |
| 130 |
if {[opt-str conf o]} { |
| 131 |
set autosetup(autodef) $o |
| 132 |
} |
| 133 |
|
| 134 |
# Debugging output (set this early) |
| 135 |
incr autosetup(debug) [opt-bool debug] |
| 136 |
incr autosetup(force) [opt-bool force] |
| 137 |
incr autosetup(msg-quiet) [opt-bool quiet] |
| 138 |
incr autosetup(msg-timing) [opt-bool timing] |
| 139 |
|
| 140 |
# If the local module exists, source it now to allow for |
| 141 |
# project-local customisations |
| 142 |
if {[file exists $autosetup(libdir)/local.tcl]} { |
| 143 |
use local |
| 144 |
} |
| 145 |
|
| 146 |
# Now any auto-load modules |
| 147 |
autosetup_load_auto_modules |
| 148 |
|
| 149 |
if {[opt-str help o]} { |
| 150 |
incr autosetup(showhelp) |
| 151 |
use help |
| 152 |
autosetup_help $o |
| 153 |
} |
| 154 |
|
| 155 |
if {[opt-bool licence license]} { |
| 156 |
use help |
| 157 |
autosetup_show_license |
| 158 |
exit 0 |
| 159 |
} |
| 160 |
|
| 161 |
if {[opt-str {manual ref reference} o]} { |
| 162 |
use help |
| 163 |
autosetup_reference $o |
| 164 |
} |
| 165 |
|
| 166 |
# Allow combining --install and --init |
| 167 |
set earlyexit 0 |
| 168 |
if {[opt-str install o]} { |
| 169 |
use install |
| 170 |
autosetup_install $o |
| 171 |
incr earlyexit |
| 172 |
} |
| 173 |
|
| 174 |
if {[opt-str init o]} { |
| 175 |
use init |
| 176 |
autosetup_init $o |
| 177 |
incr earlyexit |
| 178 |
} |
| 179 |
|
| 180 |
if {$earlyexit} { |
| 181 |
exit 0 |
| 182 |
} |
| 183 |
if {[opt-str sysinstall o]} { |
| 184 |
use install |
| 185 |
autosetup_install $o 1 |
| 186 |
exit 0 |
| 187 |
} |
| 188 |
|
| 189 |
if {![file exists $autosetup(autodef)]} { |
| 190 |
# Check for invalid option first |
| 191 |
options {} |
| 192 |
user-error "No auto.def found in \"$autosetup(srcdir)\" (use [file tail $::autosetup(exe)] --init to create one)" |
| 193 |
} |
| 194 |
|
| 195 |
# Parse extra arguments into autosetup(cmdline) |
| 196 |
foreach arg $argv { |
| 197 |
if {[regexp {([^=]*)=(.*)} $arg -> n v]} { |
| 198 |
dict set autosetup(cmdline) $n $v |
| 199 |
define $n $v |
| 200 |
} else { |
| 201 |
user-error "Unexpected parameter: $arg" |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
autosetup_add_dep $autosetup(autodef) |
| 206 |
|
| 207 |
# Add $argv to CONFIGURE_OPTS |
| 208 |
define-append-argv CONFIGURE_OPTS {*}$autosetup(argv) |
| 209 |
# Set up AUTOREMAKE to reconfigure with the same args |
| 210 |
define-append-argv AUTOREMAKE {*}$autosetup(exe) {*}$autosetup(argv) |
| 211 |
|
| 212 |
# Log how we were invoked |
| 213 |
configlog "Invoked as: [getenv WRAPPER $::argv0] [quote-argv $autosetup(argv)]" |
| 214 |
configlog "Tclsh: [info nameofexecutable]" |
| 215 |
|
| 216 |
# Load auto.def as module "auto.def" |
| 217 |
autosetup_load_module auto.def source $autosetup(autodef) |
| 218 |
|
| 219 |
# Could warn here if options {} was not specified |
| 220 |
|
| 221 |
show-notices |
| 222 |
|
| 223 |
if {$autosetup(debug)} { |
| 224 |
msg-result "Writing all defines to config.log" |
| 225 |
configlog "================ defines ======================" |
| 226 |
foreach n [lsort [array names define]] { |
| 227 |
configlog "define $n $define($n)" |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
exit 0 |
| 232 |
} |
| 233 |
|
| 234 |
# @section Option Handling |
| 235 |
|
| 236 |
# @opt-bool ?-nodefault? option ... |
| 237 |
# |
| 238 |
# Check each of the named, boolean options and if any have been explicitly enabled |
| 239 |
# or disabled by the user, return 1 or 0 accordingly. |
| 240 |
# |
| 241 |
# If the option was specified more than once, the last value wins. |
| 242 |
# e.g. With '--enable-foo --disable-foo', '[opt-bool foo]' will return 0 |
| 243 |
# |
| 244 |
# If no value was specified by the user, returns the default value for the |
| 245 |
# first option. If '-nodefault' is given, this behaviour changes and |
| 246 |
# -1 is returned instead. |
| 247 |
# |
| 248 |
proc opt-bool {args} { |
| 249 |
set nodefault 0 |
| 250 |
if {[lindex $args 0] eq "-nodefault"} { |
| 251 |
set nodefault 1 |
| 252 |
set args [lrange $args 1 end] |
| 253 |
} |
| 254 |
option-check-names {*}$args |
| 255 |
|
| 256 |
foreach opt $args { |
| 257 |
if {[dict exists $::autosetup(optset) $opt]} { |
| 258 |
return [dict get $::autosetup(optset) $opt] |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
if {$nodefault} { |
| 263 |
return -1 |
| 264 |
} |
| 265 |
# Default value is the default for the first option |
| 266 |
return [dict get $::autosetup(optdefault) [lindex $args 0]] |
| 267 |
} |
| 268 |
|
| 269 |
# @opt-val optionlist ?default=""? |
| 270 |
# |
| 271 |
# Returns a list containing all the values given for the non-boolean options in '$optionlist'. |
| 272 |
# There will be one entry in the list for each option given by the user, including if the |
| 273 |
# same option was used multiple times. |
| 274 |
# |
| 275 |
# If no options were set, '$default' is returned (exactly, not as a list). |
| 276 |
# |
| 277 |
# Note: For most use cases, 'opt-str' should be preferred. |
| 278 |
# |
| 279 |
proc opt-val {names {default ""}} { |
| 280 |
option-check-names {*}$names |
| 281 |
|
| 282 |
foreach opt $names { |
| 283 |
if {[dict exists $::autosetup(optset) $opt]} { |
| 284 |
lappend result {*}[dict get $::autosetup(optset) $opt] |
| 285 |
} |
| 286 |
} |
| 287 |
if {[info exists result]} { |
| 288 |
return $result |
| 289 |
} |
| 290 |
return $default |
| 291 |
} |
| 292 |
|
| 293 |
# @opt-str optionlist varname ?default? |
| 294 |
# |
| 295 |
# Sets '$varname' in the callers scope to the value for one of the given options. |
| 296 |
# |
| 297 |
# For the list of options given in '$optionlist', if any value is set for any option, |
| 298 |
# the option value is taken to be the *last* value of the last option (in the order given). |
| 299 |
# |
| 300 |
# If no option was given, and a default was specified with 'options-defaults', |
| 301 |
# that value is used. |
| 302 |
# |
| 303 |
# If no 'options-defaults' value was given and '$default' was given, it is used. |
| 304 |
# |
| 305 |
# If none of the above provided a value, no value is set. |
| 306 |
# |
| 307 |
# The return value depends on whether '$default' was specified. |
| 308 |
# If it was, the option value is returned. |
| 309 |
# If it was not, 1 is returns if a value was set, or 0 if not. |
| 310 |
# |
| 311 |
# Typical usage is as follows: |
| 312 |
# |
| 313 |
## if {[opt-str {myopt altname} o]} { |
| 314 |
## do something with $o |
| 315 |
## } |
| 316 |
# |
| 317 |
# Or: |
| 318 |
## define myname [opt-str {myopt altname} o "/usr/local"] |
| 319 |
# |
| 320 |
proc opt-str {names varname args} { |
| 321 |
global autosetup |
| 322 |
|
| 323 |
option-check-names {*}$names |
| 324 |
upvar $varname value |
| 325 |
|
| 326 |
if {[llength $args]} { |
| 327 |
# A default was given, so always return the string value of the option |
| 328 |
set default [lindex $args 0] |
| 329 |
set retopt 1 |
| 330 |
} else { |
| 331 |
# No default, so return 0 or 1 to indicate if a value was found |
| 332 |
set retopt 0 |
| 333 |
} |
| 334 |
|
| 335 |
foreach opt $names { |
| 336 |
if {[dict exists $::autosetup(optset) $opt]} { |
| 337 |
set result [lindex [dict get $::autosetup(optset) $opt] end] |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
if {![info exists result]} { |
| 342 |
# No user-specified value. Has options-defaults been set? |
| 343 |
foreach opt $names { |
| 344 |
if {[dict exists $::autosetup(optdefault) $opt]} { |
| 345 |
set result [dict get $autosetup(optdefault) $opt] |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
if {[info exists result]} { |
| 351 |
set value $result |
| 352 |
if {$retopt} { |
| 353 |
return $value |
| 354 |
} |
| 355 |
return 1 |
| 356 |
} |
| 357 |
|
| 358 |
if {$retopt} { |
| 359 |
set value $default |
| 360 |
return $value |
| 361 |
} |
| 362 |
|
| 363 |
return 0 |
| 364 |
} |
| 365 |
|
| 366 |
proc option-check-names {args} { |
| 367 |
foreach o $args { |
| 368 |
if {$o ni $::autosetup(options)} { |
| 369 |
autosetup-error "Request for undeclared option --$o" |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
# Parse the option definition in $opts and update |
| 375 |
# ::autosetup(setoptions) and ::autosetup(optionhelp) appropriately |
| 376 |
# |
| 377 |
proc options-add {opts} { |
| 378 |
global autosetup |
| 379 |
|
| 380 |
# First weed out comment lines |
| 381 |
set realopts {} |
| 382 |
foreach line [split $opts \n] { |
| 383 |
if {![string match "#*" [string trimleft $line]]} { |
| 384 |
append realopts $line \n |
| 385 |
} |
| 386 |
} |
| 387 |
set opts $realopts |
| 388 |
|
| 389 |
for {set i 0} {$i < [llength $opts]} {incr i} { |
| 390 |
set opt [lindex $opts $i] |
| 391 |
if {[string match =* $opt]} { |
| 392 |
# This is a special heading |
| 393 |
lappend autosetup(optionhelp) [list $opt $autosetup(module)] |
| 394 |
continue |
| 395 |
} |
| 396 |
unset -nocomplain defaultvalue equal value |
| 397 |
|
| 398 |
#puts "i=$i, opt=$opt" |
| 399 |
regexp {^([^:=]*)(:)?(=)?(.*)$} $opt -> name colon equal value |
| 400 |
if {$name in $autosetup(options)} { |
| 401 |
autosetup-error "Option $name already specified" |
| 402 |
} |
| 403 |
|
| 404 |
#puts "$opt => $name $colon $equal $value" |
| 405 |
|
| 406 |
# Find the corresponding value in the user options |
| 407 |
# and set the default if necessary |
| 408 |
if {[string match "-*" $opt]} { |
| 409 |
# This is a documentation-only option, like "-C <dir>" |
| 410 |
set opthelp $opt |
| 411 |
} elseif {$colon eq ""} { |
| 412 |
# Boolean option |
| 413 |
lappend autosetup(options) $name |
| 414 |
|
| 415 |
# Check for override |
| 416 |
if {[dict exists $autosetup(options-defaults) $name]} { |
| 417 |
# A default was specified with options-defaults, so use it |
| 418 |
set value [dict get $autosetup(options-defaults) $name] |
| 419 |
} |
| 420 |
|
| 421 |
if {$value eq "1"} { |
| 422 |
set opthelp "--disable-$name" |
| 423 |
} else { |
| 424 |
set opthelp "--$name" |
| 425 |
} |
| 426 |
|
| 427 |
# Set the default |
| 428 |
if {$value eq ""} { |
| 429 |
set value 0 |
| 430 |
} |
| 431 |
set defaultvalue $value |
| 432 |
dict set autosetup(optdefault) $name $defaultvalue |
| 433 |
|
| 434 |
if {[dict exists $autosetup(getopt) $name]} { |
| 435 |
# The option was specified by the user. Look at the last value. |
| 436 |
lassign [lindex [dict get $autosetup(getopt) $name] end] type setvalue |
| 437 |
if {$type eq "str"} { |
| 438 |
# Can we convert the value to a boolean? |
| 439 |
if {$setvalue in {1 enabled yes}} { |
| 440 |
set setvalue 1 |
| 441 |
} elseif {$setvalue in {0 disabled no}} { |
| 442 |
set setvalue 0 |
| 443 |
} else { |
| 444 |
user-error "Boolean option $name given as --$name=$setvalue" |
| 445 |
} |
| 446 |
} |
| 447 |
dict set autosetup(optset) $name $setvalue |
| 448 |
#puts "Found boolean option --$name=$setvalue" |
| 449 |
} |
| 450 |
} else { |
| 451 |
# String option. |
| 452 |
lappend autosetup(options) $name |
| 453 |
|
| 454 |
if {$equal ne "="} { |
| 455 |
# Was the option given as "name:value=default"? |
| 456 |
# If so, set $value to the display name and $defaultvalue to the default |
| 457 |
# (This is the preferred way to set a default value for a string option) |
| 458 |
if {[regexp {^([^=]+)=(.*)$} $value -> value defaultvalue]} { |
| 459 |
dict set autosetup(optdefault) $name $defaultvalue |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
# Maybe override the default value |
| 464 |
if {[dict exists $autosetup(options-defaults) $name]} { |
| 465 |
# A default was specified with options-defaults, so use it |
| 466 |
set defaultvalue [dict get $autosetup(options-defaults) $name] |
| 467 |
dict set autosetup(optdefault) $name $defaultvalue |
| 468 |
} elseif {![info exists defaultvalue]} { |
| 469 |
# No default value was given by value=default or options-defaults |
| 470 |
# so use the value as the default when the plain option with no |
| 471 |
# value is given (.e.g. just --opt instead of --opt=value) |
| 472 |
set defaultvalue $value |
| 473 |
} |
| 474 |
|
| 475 |
if {$equal eq "="} { |
| 476 |
# String option with optional value |
| 477 |
set opthelp "--$name?=$value?" |
| 478 |
} else { |
| 479 |
# String option with required value |
| 480 |
set opthelp "--$name=$value" |
| 481 |
} |
| 482 |
|
| 483 |
# Get the values specified by the user |
| 484 |
if {[dict exists $autosetup(getopt) $name]} { |
| 485 |
set listvalue {} |
| 486 |
|
| 487 |
foreach pair [dict get $autosetup(getopt) $name] { |
| 488 |
lassign $pair type setvalue |
| 489 |
if {$type eq "bool" && $setvalue} { |
| 490 |
if {$equal ne "="} { |
| 491 |
user-error "Option --$name requires a value" |
| 492 |
} |
| 493 |
# If given as a boolean, use the default value |
| 494 |
set setvalue $defaultvalue |
| 495 |
} |
| 496 |
lappend listvalue $setvalue |
| 497 |
} |
| 498 |
|
| 499 |
#puts "Found string option --$name=$listvalue" |
| 500 |
dict set autosetup(optset) $name $listvalue |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
# Now create the help for this option if appropriate |
| 505 |
if {[lindex $opts $i+1] eq "=>"} { |
| 506 |
set desc [lindex $opts $i+2] |
| 507 |
if {[info exists defaultvalue]} { |
| 508 |
set desc [string map [list @default@ $defaultvalue] $desc] |
| 509 |
} |
| 510 |
# A multi-line description |
| 511 |
lappend autosetup(optionhelp) [list $opthelp $autosetup(module) $desc] |
| 512 |
incr i 2 |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
# @module-options optionlist |
| 518 |
# |
| 519 |
# Deprecated. Simply use 'options' from within a module. |
| 520 |
proc module-options {opts} { |
| 521 |
options $opts |
| 522 |
} |
| 523 |
|
| 524 |
proc max {a b} { |
| 525 |
expr {$a > $b ? $a : $b} |
| 526 |
} |
| 527 |
|
| 528 |
proc options-wrap-desc {text length firstprefix nextprefix initial} { |
| 529 |
set len $initial |
| 530 |
set space $firstprefix |
| 531 |
foreach word [split $text] { |
| 532 |
set word [string trim $word] |
| 533 |
if {$word == ""} { |
| 534 |
continue |
| 535 |
} |
| 536 |
if {$len && [string length $space$word] + $len >= $length} { |
| 537 |
puts "" |
| 538 |
set len 0 |
| 539 |
set space $nextprefix |
| 540 |
} |
| 541 |
incr len [string length $space$word] |
| 542 |
puts -nonewline $space$word |
| 543 |
set space " " |
| 544 |
} |
| 545 |
if {$len} { |
| 546 |
puts "" |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
# Display options (from $autosetup(optionhelp)) for modules that match |
| 551 |
# glob pattern $what |
| 552 |
proc options-show {what} { |
| 553 |
set local 0 |
| 554 |
# Determine the max option width |
| 555 |
set max 0 |
| 556 |
foreach help $::autosetup(optionhelp) { |
| 557 |
lassign $help opt module desc |
| 558 |
if {![string match $what $module]} { |
| 559 |
continue |
| 560 |
} |
| 561 |
if {[string match =* $opt] || [string match \n* $desc]} { |
| 562 |
continue |
| 563 |
} |
| 564 |
set max [max $max [string length $opt]] |
| 565 |
} |
| 566 |
set indent [string repeat " " [expr {$max+4}]] |
| 567 |
set cols [getenv COLUMNS 80] |
| 568 |
catch { |
| 569 |
lassign [exec stty size] rows cols |
| 570 |
} |
| 571 |
incr cols -1 |
| 572 |
# Now output |
| 573 |
foreach help $::autosetup(optionhelp) { |
| 574 |
lassign $help opt module desc |
| 575 |
if {![string match $what $module]} { |
| 576 |
continue |
| 577 |
} |
| 578 |
if {$local == 0 && $module eq "auto.def"} { |
| 579 |
puts "Local Options:" |
| 580 |
incr local |
| 581 |
} |
| 582 |
if {[string match =* $opt]} { |
| 583 |
# Output a special heading line" |
| 584 |
puts [string range $opt 1 end] |
| 585 |
continue |
| 586 |
} |
| 587 |
puts -nonewline " [format %-${max}s $opt]" |
| 588 |
if {[string match \n* $desc]} { |
| 589 |
# Output a pre-formatted help description as-is |
| 590 |
puts $desc |
| 591 |
} else { |
| 592 |
options-wrap-desc [string trim $desc] $cols " " $indent [expr {$max+2}] |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
# @options optionspec |
| 598 |
# |
| 599 |
# Specifies configuration-time options which may be selected by the user |
| 600 |
# and checked with 'opt-str' and 'opt-bool'. '$optionspec' contains a series |
| 601 |
# of options specifications separated by newlines, as follows: |
| 602 |
# |
| 603 |
# A boolean option is of the form: |
| 604 |
# |
| 605 |
## name[=0|1] => "Description of this boolean option" |
| 606 |
# |
| 607 |
# The default is 'name=0', meaning that the option is disabled by default. |
| 608 |
# If 'name=1' is used to make the option enabled by default, the description should reflect |
| 609 |
# that with text like "Disable support for ...". |
| 610 |
# |
| 611 |
# An argument option (one which takes a parameter) is of one of the following forms: |
| 612 |
# |
| 613 |
## name:value => "Description of this option" |
| 614 |
## name:value=default => "Description of this option with a default value" |
| 615 |
## name:=value => "Description of this option with an optional value" |
| 616 |
# |
| 617 |
# If the 'name:value' form is used, the value must be provided with the option (as '--name=myvalue'). |
| 618 |
# If the 'name:value=default' form is used, the option has the given default value even if not |
| 619 |
# specified by the user. |
| 620 |
# If the 'name:=value' form is used, the value is optional and the given value is used |
| 621 |
# if it is not provided. |
| 622 |
# |
| 623 |
# The description may contain '@default@', in which case it will be replaced with the default |
| 624 |
# value for the option (taking into account defaults specified with 'options-defaults'. |
| 625 |
# |
| 626 |
# Undocumented options are also supported by omitting the '=> description'. |
| 627 |
# These options are not displayed with '--help' and can be useful for internal options or as aliases. |
| 628 |
# |
| 629 |
# For example, '--disable-lfs' is an alias for '--disable=largefile': |
| 630 |
# |
| 631 |
## lfs=1 largefile=1 => "Disable large file support" |
| 632 |
# |
| 633 |
proc options {optlist} { |
| 634 |
global autosetup |
| 635 |
|
| 636 |
options-add $optlist |
| 637 |
|
| 638 |
if {$autosetup(showhelp)} { |
| 639 |
# If --help, stop now to show help |
| 640 |
return -code break |
| 641 |
} |
| 642 |
|
| 643 |
if {$autosetup(module) eq "auto.def"} { |
| 644 |
# Check for invalid options |
| 645 |
if {[opt-bool option-checking]} { |
| 646 |
foreach o [dict keys $::autosetup(getopt)] { |
| 647 |
if {$o ni $::autosetup(options)} { |
| 648 |
user-error "Unknown option --$o" |
| 649 |
} |
| 650 |
} |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
# @options-defaults dictionary |
| 656 |
# |
| 657 |
# Specifies a dictionary of options and a new default value for each of those options. |
| 658 |
# Use before any 'use' statements in 'auto.def' to change the defaults for |
| 659 |
# subsequently included modules. |
| 660 |
proc options-defaults {dict} { |
| 661 |
foreach {n v} $dict { |
| 662 |
dict set ::autosetup(options-defaults) $n $v |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
proc config_guess {} { |
| 667 |
if {[file-isexec $::autosetup(dir)/autosetup-config.guess]} { |
| 668 |
if {[catch {exec-with-stderr sh $::autosetup(dir)/autosetup-config.guess} alias]} { |
| 669 |
user-error $alias |
| 670 |
} |
| 671 |
return $alias |
| 672 |
} else { |
| 673 |
configlog "No autosetup-config.guess, so using uname" |
| 674 |
string tolower [exec uname -p]-unknown-[exec uname -s][exec uname -r] |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
proc config_sub {alias} { |
| 679 |
if {[file-isexec $::autosetup(dir)/autosetup-config.sub]} { |
| 680 |
if {[catch {exec-with-stderr sh $::autosetup(dir)/autosetup-config.sub $alias} alias]} { |
| 681 |
user-error $alias |
| 682 |
} |
| 683 |
} |
| 684 |
return $alias |
| 685 |
} |
| 686 |
|
| 687 |
# @section Variable Definitions (defines) |
| 688 |
|
| 689 |
# @define name ?value=1? |
| 690 |
# |
| 691 |
# Defines the named variable to the given value. |
| 692 |
# These (name, value) pairs represent the results of the configuration check |
| 693 |
# and are available to be subsequently checked, modified and substituted. |
| 694 |
# |
| 695 |
proc define {name {value 1}} { |
| 696 |
set ::define($name) $value |
| 697 |
#dputs "$name <= $value" |
| 698 |
} |
| 699 |
|
| 700 |
# @define-push {name ...} script |
| 701 |
# |
| 702 |
# Save the values of the given defines, evaluation the script, then restore. |
| 703 |
# For example, to avoid updating AS_FLAGS and AS_CXXFLAGS: |
| 704 |
## define-push {AS_CFLAGS AS_CXXFLAGS} { |
| 705 |
## cc-check-flags -Wno-error |
| 706 |
## } |
| 707 |
proc define-push {names script} { |
| 708 |
array set unset {} |
| 709 |
foreach name $names { |
| 710 |
if {[is-defined $name]} { |
| 711 |
set save($name) [get-define $name] |
| 712 |
} else { |
| 713 |
set unset($name) 1 |
| 714 |
} |
| 715 |
} |
| 716 |
uplevel 1 $script |
| 717 |
array set ::define [array get save] |
| 718 |
foreach name [array names unset] { |
| 719 |
unset -nocomplain ::define($name) |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
# @undefine name |
| 724 |
# |
| 725 |
# Undefine the named variable. |
| 726 |
# |
| 727 |
proc undefine {name} { |
| 728 |
unset -nocomplain ::define($name) |
| 729 |
#dputs "$name <= <undef>" |
| 730 |
} |
| 731 |
|
| 732 |
# @define-append name value ... |
| 733 |
# |
| 734 |
# Appends the given value(s) to the given "defined" variable. |
| 735 |
# If the variable is not defined or empty, it is set to '$value'. |
| 736 |
# Otherwise the value is appended, separated by a space. |
| 737 |
# Any extra values are similarly appended. |
| 738 |
# |
| 739 |
# Note that define-append is not designed to add values containing spaces. |
| 740 |
# If values may contain spaces, consider define-append-argv instead. |
| 741 |
# |
| 742 |
proc define-append {name args} { |
| 743 |
if {[get-define $name ""] ne ""} { |
| 744 |
foreach arg $args { |
| 745 |
if {$arg eq ""} { |
| 746 |
continue |
| 747 |
} |
| 748 |
append ::define($name) " " $arg |
| 749 |
} |
| 750 |
} else { |
| 751 |
set ::define($name) [join $args] |
| 752 |
} |
| 753 |
#dputs "$name += [join $args] => $::define($name)" |
| 754 |
} |
| 755 |
|
| 756 |
# @define-append-argv name value ... |
| 757 |
# |
| 758 |
# Similar to define-append except designed to construct shell command |
| 759 |
# lines, including correct handling of parameters with spaces. |
| 760 |
# |
| 761 |
# Each non-empty value is quoted if necessary and then appended to the given variable |
| 762 |
# if it does not already exist. |
| 763 |
# |
| 764 |
proc define-append-argv {name args} { |
| 765 |
set seen {} |
| 766 |
set new {} |
| 767 |
foreach val [list {*}[get-define $name ""] {*}$args] { |
| 768 |
if {$val ne {} && ![dict exists $seen $val]} { |
| 769 |
lappend new [quote-if-needed $val] |
| 770 |
dict set seen $val 1 |
| 771 |
} |
| 772 |
} |
| 773 |
set ::define($name) [join $new " "] |
| 774 |
#dputs "$name += [join $args] => $::define($name)" |
| 775 |
} |
| 776 |
|
| 777 |
# @get-define name ?default=0? |
| 778 |
# |
| 779 |
# Returns the current value of the "defined" variable, or '$default' |
| 780 |
# if not set. |
| 781 |
# |
| 782 |
proc get-define {name {default 0}} { |
| 783 |
if {[info exists ::define($name)]} { |
| 784 |
#dputs "$name => $::define($name)" |
| 785 |
return $::define($name) |
| 786 |
} |
| 787 |
#dputs "$name => $default" |
| 788 |
return $default |
| 789 |
} |
| 790 |
|
| 791 |
# @is-defined name |
| 792 |
# |
| 793 |
# Returns 1 if the given variable is defined. |
| 794 |
# |
| 795 |
proc is-defined {name} { |
| 796 |
info exists ::define($name) |
| 797 |
} |
| 798 |
|
| 799 |
# @is-define-set name |
| 800 |
# |
| 801 |
# Returns 1 if the given variable is defined and is set |
| 802 |
# to a value other than "" or 0 |
| 803 |
# |
| 804 |
proc is-define-set {name} { |
| 805 |
if {[get-define $name] in {0 ""}} { |
| 806 |
return 0 |
| 807 |
} |
| 808 |
return 1 |
| 809 |
} |
| 810 |
|
| 811 |
# @all-defines |
| 812 |
# |
| 813 |
# Returns a dictionary (name, value list) of all defined variables. |
| 814 |
# |
| 815 |
# This is suitable for use with 'dict', 'array set' or 'foreach' |
| 816 |
# and allows for arbitrary processing of the defined variables. |
| 817 |
# |
| 818 |
proc all-defines {} { |
| 819 |
array get ::define |
| 820 |
} |
| 821 |
|
| 822 |
# @section Environment/Helpers |
| 823 |
|
| 824 |
# @get-env name default |
| 825 |
# |
| 826 |
# If '$name' was specified on the command line, return it. |
| 827 |
# Otherwise if '$name' was set in the environment, return it. |
| 828 |
# Otherwise return '$default'. |
| 829 |
# |
| 830 |
proc get-env {name default} { |
| 831 |
if {[dict exists $::autosetup(cmdline) $name]} { |
| 832 |
return [dict get $::autosetup(cmdline) $name] |
| 833 |
} |
| 834 |
getenv $name $default |
| 835 |
} |
| 836 |
|
| 837 |
# @env-is-set name |
| 838 |
# |
| 839 |
# Returns 1 if '$name' was specified on the command line or in the environment. |
| 840 |
# Note that an empty environment variable is not considered to be set. |
| 841 |
# |
| 842 |
proc env-is-set {name} { |
| 843 |
if {[dict exists $::autosetup(cmdline) $name]} { |
| 844 |
return 1 |
| 845 |
} |
| 846 |
if {[getenv $name ""] ne ""} { |
| 847 |
return 1 |
| 848 |
} |
| 849 |
return 0 |
| 850 |
} |
| 851 |
|
| 852 |
# @readfile filename ?default=""? |
| 853 |
# |
| 854 |
# Return the contents of the file, without the trailing newline. |
| 855 |
# If the file doesn't exist or can't be read, returns '$default'. |
| 856 |
# |
| 857 |
proc readfile {filename {default_value ""}} { |
| 858 |
set result $default_value |
| 859 |
catch { |
| 860 |
set f [open $filename] |
| 861 |
set result [read -nonewline $f] |
| 862 |
close $f |
| 863 |
} |
| 864 |
return $result |
| 865 |
} |
| 866 |
|
| 867 |
# @writefile filename value |
| 868 |
# |
| 869 |
# Creates the given file containing '$value'. |
| 870 |
# Does not add an extra newline. |
| 871 |
# |
| 872 |
proc writefile {filename value} { |
| 873 |
set f [open $filename w] |
| 874 |
puts -nonewline $f $value |
| 875 |
close $f |
| 876 |
} |
| 877 |
|
| 878 |
proc quote-if-needed {str} { |
| 879 |
if {[string match {*[\" ]*} $str]} { |
| 880 |
return \"[string map [list \" \\" \\ \\\\] $str]\" |
| 881 |
} |
| 882 |
return $str |
| 883 |
} |
| 884 |
|
| 885 |
proc quote-argv {argv} { |
| 886 |
set args {} |
| 887 |
foreach arg $argv { |
| 888 |
lappend args [quote-if-needed $arg] |
| 889 |
} |
| 890 |
join $args |
| 891 |
} |
| 892 |
|
| 893 |
# @list-non-empty list |
| 894 |
# |
| 895 |
# Returns a copy of the given list with empty elements removed |
| 896 |
proc list-non-empty {list} { |
| 897 |
set result {} |
| 898 |
foreach p $list { |
| 899 |
if {$p ne ""} { |
| 900 |
lappend result $p |
| 901 |
} |
| 902 |
} |
| 903 |
return $result |
| 904 |
} |
| 905 |
|
| 906 |
# @section Paths, Searching |
| 907 |
|
| 908 |
# @find-executable-path name |
| 909 |
# |
| 910 |
# Searches the path for an executable with the given name. |
| 911 |
# Note that the name may include some parameters, e.g. 'cc -mbig-endian', |
| 912 |
# in which case the parameters are ignored. |
| 913 |
# The full path to the executable if found, or "" if not found. |
| 914 |
# Returns 1 if found, or 0 if not. |
| 915 |
# |
| 916 |
proc find-executable-path {name} { |
| 917 |
# Ignore any parameters |
| 918 |
set name [lindex $name 0] |
| 919 |
# The empty string is never a valid executable |
| 920 |
if {$name ne ""} { |
| 921 |
foreach p [split-path] { |
| 922 |
dputs "Looking for $name in $p" |
| 923 |
set exec [file join $p $name] |
| 924 |
if {[file-isexec $exec]} { |
| 925 |
dputs "Found $name -> $exec" |
| 926 |
return $exec |
| 927 |
} |
| 928 |
} |
| 929 |
} |
| 930 |
return {} |
| 931 |
} |
| 932 |
|
| 933 |
# @find-executable name |
| 934 |
# |
| 935 |
# Searches the path for an executable with the given name. |
| 936 |
# Note that the name may include some parameters, e.g. 'cc -mbig-endian', |
| 937 |
# in which case the parameters are ignored. |
| 938 |
# Returns 1 if found, or 0 if not. |
| 939 |
# |
| 940 |
proc find-executable {name} { |
| 941 |
if {[find-executable-path $name] eq {}} { |
| 942 |
return 0 |
| 943 |
} |
| 944 |
return 1 |
| 945 |
} |
| 946 |
|
| 947 |
# @find-an-executable ?-required? name ... |
| 948 |
# |
| 949 |
# Given a list of possible executable names, |
| 950 |
# searches for one of these on the path. |
| 951 |
# |
| 952 |
# Returns the name found, or "" if none found. |
| 953 |
# If the first parameter is '-required', an error is generated |
| 954 |
# if no executable is found. |
| 955 |
# |
| 956 |
proc find-an-executable {args} { |
| 957 |
set required 0 |
| 958 |
if {[lindex $args 0] eq "-required"} { |
| 959 |
set args [lrange $args 1 end] |
| 960 |
incr required |
| 961 |
} |
| 962 |
foreach name $args { |
| 963 |
if {[find-executable $name]} { |
| 964 |
return $name |
| 965 |
} |
| 966 |
} |
| 967 |
if {$required} { |
| 968 |
if {[llength $args] == 1} { |
| 969 |
user-error "failed to find: [join $args]" |
| 970 |
} else { |
| 971 |
user-error "failed to find one of: [join $args]" |
| 972 |
} |
| 973 |
} |
| 974 |
return "" |
| 975 |
} |
| 976 |
|
| 977 |
# @section Logging, Messages and Errors |
| 978 |
|
| 979 |
# @configlog msg |
| 980 |
# |
| 981 |
# Writes the given message to the configuration log, 'config.log'. |
| 982 |
# |
| 983 |
proc configlog {msg} { |
| 984 |
if {![info exists ::autosetup(logfh)]} { |
| 985 |
set ::autosetup(logfh) [open config.log w] |
| 986 |
} |
| 987 |
puts $::autosetup(logfh) $msg |
| 988 |
} |
| 989 |
|
| 990 |
# @msg-checking msg |
| 991 |
# |
| 992 |
# Writes the message with no newline to stdout. |
| 993 |
# |
| 994 |
proc msg-checking {msg} { |
| 995 |
if {$::autosetup(msg-quiet) == 0} { |
| 996 |
maybe-show-timestamp |
| 997 |
puts -nonewline $msg |
| 998 |
set ::autosetup(msg-checking) 1 |
| 999 |
} |
| 1000 |
} |
| 1001 |
|
| 1002 |
# @msg-result msg |
| 1003 |
# |
| 1004 |
# Writes the message to stdout. |
| 1005 |
# |
| 1006 |
proc msg-result {msg} { |
| 1007 |
if {$::autosetup(msg-quiet) == 0} { |
| 1008 |
maybe-show-timestamp |
| 1009 |
puts $msg |
| 1010 |
set ::autosetup(msg-checking) 0 |
| 1011 |
show-notices |
| 1012 |
} |
| 1013 |
} |
| 1014 |
|
| 1015 |
# @msg-quiet command ... |
| 1016 |
# |
| 1017 |
# 'msg-quiet' evaluates it's arguments as a command with output |
| 1018 |
# from 'msg-checking' and 'msg-result' suppressed. |
| 1019 |
# |
| 1020 |
# This is useful if a check needs to run a subcheck which isn't |
| 1021 |
# of interest to the user. |
| 1022 |
proc msg-quiet {args} { |
| 1023 |
incr ::autosetup(msg-quiet) |
| 1024 |
set rc [uplevel 1 $args] |
| 1025 |
incr ::autosetup(msg-quiet) -1 |
| 1026 |
return $rc |
| 1027 |
} |
| 1028 |
|
| 1029 |
# Will be overridden by 'use misc' |
| 1030 |
proc error-stacktrace {msg} { |
| 1031 |
return $msg |
| 1032 |
} |
| 1033 |
|
| 1034 |
proc error-location {msg} { |
| 1035 |
return $msg |
| 1036 |
} |
| 1037 |
|
| 1038 |
################################################################## |
| 1039 |
# |
| 1040 |
# Debugging output |
| 1041 |
# |
| 1042 |
proc dputs {msg} { |
| 1043 |
if {$::autosetup(debug)} { |
| 1044 |
puts $msg |
| 1045 |
} |
| 1046 |
} |
| 1047 |
|
| 1048 |
################################################################## |
| 1049 |
# |
| 1050 |
# User and system warnings and errors |
| 1051 |
# |
| 1052 |
# Usage errors such as wrong command line options |
| 1053 |
|
| 1054 |
# @user-error msg |
| 1055 |
# |
| 1056 |
# Indicate incorrect usage to the user, including if required components |
| 1057 |
# or features are not found. |
| 1058 |
# 'autosetup' exits with a non-zero return code. |
| 1059 |
# |
| 1060 |
proc user-error {msg} { |
| 1061 |
show-notices |
| 1062 |
puts stderr "Error: $msg" |
| 1063 |
puts stderr "Try: '[file tail $::autosetup(exe)] --help' for options" |
| 1064 |
exit 1 |
| 1065 |
} |
| 1066 |
|
| 1067 |
# @user-notice msg |
| 1068 |
# |
| 1069 |
# Output the given message to stderr. |
| 1070 |
# |
| 1071 |
proc user-notice {msg} { |
| 1072 |
lappend ::autosetup(notices) $msg |
| 1073 |
} |
| 1074 |
|
| 1075 |
# Incorrect usage in the auto.def file. Identify the location. |
| 1076 |
proc autosetup-error {msg} { |
| 1077 |
autosetup-full-error [error-location $msg] |
| 1078 |
} |
| 1079 |
|
| 1080 |
# Like autosetup-error, except $msg is the full error message. |
| 1081 |
proc autosetup-full-error {msg} { |
| 1082 |
show-notices |
| 1083 |
puts stderr $msg |
| 1084 |
exit 1 |
| 1085 |
} |
| 1086 |
|
| 1087 |
proc show-notices {} { |
| 1088 |
if {$::autosetup(msg-checking)} { |
| 1089 |
puts "" |
| 1090 |
set ::autosetup(msg-checking) 0 |
| 1091 |
} |
| 1092 |
flush stdout |
| 1093 |
if {[info exists ::autosetup(notices)]} { |
| 1094 |
puts stderr [join $::autosetup(notices) \n] |
| 1095 |
unset ::autosetup(notices) |
| 1096 |
} |
| 1097 |
} |
| 1098 |
|
| 1099 |
proc maybe-show-timestamp {} { |
| 1100 |
if {$::autosetup(msg-timing) && $::autosetup(msg-checking) == 0} { |
| 1101 |
puts -nonewline [format {[%6.2f] } [expr {([clock millis] - $::autosetup(start)) % 10000 / 1000.0}]] |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
# @autosetup-require-version required |
| 1106 |
# |
| 1107 |
# Checks the current version of 'autosetup' against '$required'. |
| 1108 |
# A fatal error is generated if the current version is less than that required. |
| 1109 |
# |
| 1110 |
proc autosetup-require-version {required} { |
| 1111 |
if {[compare-versions $::autosetup(version) $required] < 0} { |
| 1112 |
user-error "autosetup version $required is required, but this is $::autosetup(version)" |
| 1113 |
} |
| 1114 |
} |
| 1115 |
|
| 1116 |
proc autosetup_version {} { |
| 1117 |
return "autosetup v$::autosetup(version)" |
| 1118 |
} |
| 1119 |
|
| 1120 |
################################################################## |
| 1121 |
# |
| 1122 |
# Directory/path handling |
| 1123 |
# |
| 1124 |
|
| 1125 |
proc realdir {dir} { |
| 1126 |
set oldpwd [pwd] |
| 1127 |
cd $dir |
| 1128 |
set pwd [pwd] |
| 1129 |
cd $oldpwd |
| 1130 |
return $pwd |
| 1131 |
} |
| 1132 |
|
| 1133 |
# Follow symlinks until we get to something which is not a symlink |
| 1134 |
proc realpath {path} { |
| 1135 |
while {1} { |
| 1136 |
if {[catch { |
| 1137 |
set path [file readlink $path] |
| 1138 |
}]} { |
| 1139 |
# Not a link |
| 1140 |
break |
| 1141 |
} |
| 1142 |
} |
| 1143 |
return $path |
| 1144 |
} |
| 1145 |
|
| 1146 |
# Convert absolute path, $path into a path relative |
| 1147 |
# to the given directory (or the current dir, if not given). |
| 1148 |
# |
| 1149 |
proc relative-path {path {pwd {}}} { |
| 1150 |
set diff 0 |
| 1151 |
set same 0 |
| 1152 |
set newf {} |
| 1153 |
set prefix {} |
| 1154 |
set path [file-normalize $path] |
| 1155 |
if {$pwd eq ""} { |
| 1156 |
set pwd [pwd] |
| 1157 |
} else { |
| 1158 |
set pwd [file-normalize $pwd] |
| 1159 |
} |
| 1160 |
|
| 1161 |
if {$path eq $pwd} { |
| 1162 |
return . |
| 1163 |
} |
| 1164 |
|
| 1165 |
# Try to make the filename relative to the current dir |
| 1166 |
foreach p [split $pwd /] f [split $path /] { |
| 1167 |
if {$p ne $f} { |
| 1168 |
incr diff |
| 1169 |
} elseif {!$diff} { |
| 1170 |
incr same |
| 1171 |
} |
| 1172 |
if {$diff} { |
| 1173 |
if {$p ne ""} { |
| 1174 |
# Add .. for sibling or parent dir |
| 1175 |
lappend prefix .. |
| 1176 |
} |
| 1177 |
if {$f ne ""} { |
| 1178 |
lappend newf $f |
| 1179 |
} |
| 1180 |
} |
| 1181 |
} |
| 1182 |
if {$same == 1 || [llength $prefix] > 3} { |
| 1183 |
return $path |
| 1184 |
} |
| 1185 |
|
| 1186 |
file join [join $prefix /] [join $newf /] |
| 1187 |
} |
| 1188 |
|
| 1189 |
# Add filename as a dependency to rerun autosetup |
| 1190 |
# The name will be normalised (converted to a full path) |
| 1191 |
# |
| 1192 |
proc autosetup_add_dep {filename} { |
| 1193 |
lappend ::autosetup(deps) [file-normalize $filename] |
| 1194 |
} |
| 1195 |
|
| 1196 |
# @section Modules Support |
| 1197 |
|
| 1198 |
################################################################## |
| 1199 |
# |
| 1200 |
# Library module support |
| 1201 |
# |
| 1202 |
|
| 1203 |
# @use module ... |
| 1204 |
# |
| 1205 |
# Load the given library modules. |
| 1206 |
# e.g. 'use cc cc-shared' |
| 1207 |
# |
| 1208 |
# Note that module 'X' is implemented in either 'autosetup/X.tcl' |
| 1209 |
# or 'autosetup/X/init.tcl' |
| 1210 |
# |
| 1211 |
# The latter form is useful for a complex module which requires additional |
| 1212 |
# support file. In this form, '$::usedir' is set to the module directory |
| 1213 |
# when it is loaded. |
| 1214 |
# |
| 1215 |
proc use {args} { |
| 1216 |
global autosetup libmodule modsource |
| 1217 |
|
| 1218 |
set dirs [list $autosetup(libdir)] |
| 1219 |
if {[info exists autosetup(srcdir)]} { |
| 1220 |
lappend dirs $autosetup(srcdir)/autosetup |
| 1221 |
} |
| 1222 |
foreach m $args { |
| 1223 |
if {[info exists libmodule($m)]} { |
| 1224 |
continue |
| 1225 |
} |
| 1226 |
set libmodule($m) 1 |
| 1227 |
|
| 1228 |
if {[info exists modsource(${m}.tcl)]} { |
| 1229 |
autosetup_load_module $m eval $modsource(${m}.tcl) |
| 1230 |
} else { |
| 1231 |
set locs [list ${m}.tcl ${m}/init.tcl] |
| 1232 |
set found 0 |
| 1233 |
foreach dir $dirs { |
| 1234 |
foreach loc $locs { |
| 1235 |
set source $dir/$loc |
| 1236 |
if {[file exists $source]} { |
| 1237 |
incr found |
| 1238 |
break |
| 1239 |
} |
| 1240 |
} |
| 1241 |
if {$found} { |
| 1242 |
break |
| 1243 |
} |
| 1244 |
} |
| 1245 |
if {$found} { |
| 1246 |
# For the convenience of the "use" source, point to the directory |
| 1247 |
# it is being loaded from |
| 1248 |
set ::usedir [file dirname $source] |
| 1249 |
autosetup_load_module $m source $source |
| 1250 |
autosetup_add_dep $source |
| 1251 |
} else { |
| 1252 |
autosetup-error "use: No such module: $m" |
| 1253 |
} |
| 1254 |
} |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
proc autosetup_load_auto_modules {} { |
| 1259 |
global autosetup modsource |
| 1260 |
# First load any embedded auto modules |
| 1261 |
foreach mod [array names modsource *.auto] { |
| 1262 |
autosetup_load_module $mod eval $modsource($mod) |
| 1263 |
} |
| 1264 |
# Now any external auto modules |
| 1265 |
foreach file [glob -nocomplain $autosetup(libdir)/*.auto $autosetup(libdir)/*/*.auto] { |
| 1266 |
autosetup_load_module [file tail $file] source $file |
| 1267 |
} |
| 1268 |
} |
| 1269 |
|
| 1270 |
# Load module source in the global scope by executing the given command |
| 1271 |
proc autosetup_load_module {module args} { |
| 1272 |
global autosetup |
| 1273 |
set prev $autosetup(module) |
| 1274 |
set autosetup(module) $module |
| 1275 |
|
| 1276 |
if {[catch [list uplevel #0 $args] msg opts] ni {0 2 3}} { |
| 1277 |
autosetup-full-error [error-dump $msg $opts $::autosetup(debug)] |
| 1278 |
} |
| 1279 |
set autosetup(module) $prev |
| 1280 |
} |
| 1281 |
|
| 1282 |
# Initial settings |
| 1283 |
set autosetup(exe) $::argv0 |
| 1284 |
set autosetup(istcl) 1 |
| 1285 |
set autosetup(start) [clock millis] |
| 1286 |
set autosetup(installed) 0 |
| 1287 |
set autosetup(sysinstall) 0 |
| 1288 |
set autosetup(msg-checking) 0 |
| 1289 |
set autosetup(msg-quiet) 0 |
| 1290 |
set autosetup(inittypes) {} |
| 1291 |
set autosetup(module) autosetup |
| 1292 |
|
| 1293 |
# Embedded modules are inserted below here |
| 1294 |
set autosetup(installed) 1 |
| 1295 |
set autosetup(sysinstall) 0 |
| 1296 |
# ----- @module asciidoc-formatting.tcl ----- |
| 1297 |
|
| 1298 |
set modsource(asciidoc-formatting.tcl) { |
| 1299 |
# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/ |
| 1300 |
# All rights reserved |
| 1301 |
|
| 1302 |
# Module which provides text formatting |
| 1303 |
# asciidoc format |
| 1304 |
|
| 1305 |
use formatting |
| 1306 |
|
| 1307 |
proc para {text} { |
| 1308 |
regsub -all "\[ \t\n\]+" [string trim $text] " " |
| 1309 |
} |
| 1310 |
proc title {text} { |
| 1311 |
underline [para $text] = |
| 1312 |
nl |
| 1313 |
} |
| 1314 |
proc p {text} { |
| 1315 |
puts [para $text] |
| 1316 |
nl |
| 1317 |
} |
| 1318 |
proc code {text} { |
| 1319 |
foreach line [parse_code_block $text] { |
| 1320 |
puts " $line" |
| 1321 |
} |
| 1322 |
nl |
| 1323 |
} |
| 1324 |
proc codelines {lines} { |
| 1325 |
foreach line $lines { |
| 1326 |
puts " $line" |
| 1327 |
} |
| 1328 |
nl |
| 1329 |
} |
| 1330 |
proc nl {} { |
| 1331 |
puts "" |
| 1332 |
} |
| 1333 |
proc underline {text char} { |
| 1334 |
regexp "^(\[ \t\]*)(.*)" $text -> indent words |
| 1335 |
puts $text |
| 1336 |
puts $indent[string repeat $char [string length $words]] |
| 1337 |
} |
| 1338 |
proc section {text} { |
| 1339 |
underline "[para $text]" - |
| 1340 |
nl |
| 1341 |
} |
| 1342 |
proc subsection {text} { |
| 1343 |
underline "$text" ~ |
| 1344 |
nl |
| 1345 |
} |
| 1346 |
proc bullet {text} { |
| 1347 |
puts "* [para $text]" |
| 1348 |
} |
| 1349 |
proc indent {text} { |
| 1350 |
puts " :: " |
| 1351 |
puts [para $text] |
| 1352 |
} |
| 1353 |
proc defn {first args} { |
| 1354 |
set sep "" |
| 1355 |
if {$first ne ""} { |
| 1356 |
puts "${first}::" |
| 1357 |
} else { |
| 1358 |
puts " :: " |
| 1359 |
} |
| 1360 |
set defn [string trim [join $args \n]] |
| 1361 |
regsub -all "\n\n" $defn "\n ::\n" defn |
| 1362 |
puts $defn |
| 1363 |
} |
| 1364 |
} |
| 1365 |
|
| 1366 |
# ----- @module formatting.tcl ----- |
| 1367 |
|
| 1368 |
set modsource(formatting.tcl) { |
| 1369 |
# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/ |
| 1370 |
# All rights reserved |
| 1371 |
|
| 1372 |
# Module which provides common text formatting |
| 1373 |
|
| 1374 |
# This is designed for documentation which looks like: |
| 1375 |
# code {...} |
| 1376 |
# or |
| 1377 |
# code { |
| 1378 |
# ... |
| 1379 |
# ... |
| 1380 |
# } |
| 1381 |
# In the second case, we need to work out the indenting |
| 1382 |
# and strip it from all lines but preserve the remaining indenting. |
| 1383 |
# Note that all lines need to be indented with the same initial |
| 1384 |
# spaces/tabs. |
| 1385 |
# |
| 1386 |
# Returns a list of lines with the indenting removed. |
| 1387 |
# |
| 1388 |
proc parse_code_block {text} { |
| 1389 |
# If the text begins with newline, take the following text, |
| 1390 |
# otherwise just return the original |
| 1391 |
if {![regexp "^\n(.*)" $text -> text]} { |
| 1392 |
return [list [string trim $text]] |
| 1393 |
} |
| 1394 |
|
| 1395 |
# And trip spaces off the end |
| 1396 |
set text [string trimright $text] |
| 1397 |
|
| 1398 |
set min 100 |
| 1399 |
# Examine each line to determine the minimum indent |
| 1400 |
foreach line [split $text \n] { |
| 1401 |
if {$line eq ""} { |
| 1402 |
# Ignore empty lines for the indent calculation |
| 1403 |
continue |
| 1404 |
} |
| 1405 |
regexp "^(\[ \t\]*)" $line -> indent |
| 1406 |
set len [string length $indent] |
| 1407 |
if {$len < $min} { |
| 1408 |
set min $len |
| 1409 |
} |
| 1410 |
} |
| 1411 |
|
| 1412 |
# Now make a list of lines with this indent removed |
| 1413 |
set lines {} |
| 1414 |
foreach line [split $text \n] { |
| 1415 |
lappend lines [string range $line $min end] |
| 1416 |
} |
| 1417 |
|
| 1418 |
# Return the result |
| 1419 |
return $lines |
| 1420 |
} |
| 1421 |
} |
| 1422 |
|
| 1423 |
# ----- @module getopt.tcl ----- |
| 1424 |
|
| 1425 |
set modsource(getopt.tcl) { |
| 1426 |
# Copyright (c) 2006 WorkWare Systems http://www.workware.net.au/ |
| 1427 |
# All rights reserved |
| 1428 |
|
| 1429 |
# Simple getopt module |
| 1430 |
|
| 1431 |
# Parse everything out of the argv list which looks like an option |
| 1432 |
# Everything which doesn't look like an option, or is after --, is left unchanged |
| 1433 |
# Understands --enable-xxx as a synonym for --xxx to enable the boolean option xxx. |
| 1434 |
# Understands --disable-xxx to disable the boolean option xxx. |
| 1435 |
# |
| 1436 |
# The returned value is a dictionary keyed by option name |
| 1437 |
# Each value is a list of {type value} ... where type is "bool" or "str". |
| 1438 |
# The value for a boolean option is 0 or 1. The value of a string option is the value given. |
| 1439 |
proc getopt {argvname} { |
| 1440 |
upvar $argvname argv |
| 1441 |
set nargv {} |
| 1442 |
|
| 1443 |
set opts {} |
| 1444 |
|
| 1445 |
for {set i 0} {$i < [llength $argv]} {incr i} { |
| 1446 |
set arg [lindex $argv $i] |
| 1447 |
|
| 1448 |
#dputs arg=$arg |
| 1449 |
|
| 1450 |
if {$arg eq "--"} { |
| 1451 |
# End of options |
| 1452 |
incr i |
| 1453 |
lappend nargv {*}[lrange $argv $i end] |
| 1454 |
break |
| 1455 |
} |
| 1456 |
|
| 1457 |
if {[regexp {^--([^=][^=]+)=(.*)$} $arg -> name value]} { |
| 1458 |
# --name=value |
| 1459 |
dict lappend opts $name [list str $value] |
| 1460 |
} elseif {[regexp {^--(enable-|disable-)?([^=]*)$} $arg -> prefix name]} { |
| 1461 |
if {$prefix in {enable- ""}} { |
| 1462 |
set value 1 |
| 1463 |
} else { |
| 1464 |
set value 0 |
| 1465 |
} |
| 1466 |
dict lappend opts $name [list bool $value] |
| 1467 |
} else { |
| 1468 |
lappend nargv $arg |
| 1469 |
} |
| 1470 |
} |
| 1471 |
|
| 1472 |
#puts "getopt: argv=[join $argv] => [join $nargv]" |
| 1473 |
#array set getopt $opts |
| 1474 |
#parray getopt |
| 1475 |
|
| 1476 |
set argv $nargv |
| 1477 |
|
| 1478 |
return $opts |
| 1479 |
} |
| 1480 |
} |
| 1481 |
|
| 1482 |
# ----- @module help.tcl ----- |
| 1483 |
|
| 1484 |
set modsource(help.tcl) { |
| 1485 |
# Copyright (c) 2010 WorkWare Systems http://workware.net.au/ |
| 1486 |
# All rights reserved |
| 1487 |
|
| 1488 |
# Module which provides usage, help and the command reference |
| 1489 |
|
| 1490 |
proc autosetup_help {what} { |
| 1491 |
use_pager |
| 1492 |
|
| 1493 |
puts "Usage: [file tail $::autosetup(exe)] \[options\] \[settings\]\n" |
| 1494 |
puts "This is [autosetup_version], a build environment \"autoconfigurator\"" |
| 1495 |
puts "See the documentation online at https://msteveb.github.io/autosetup/\n" |
| 1496 |
|
| 1497 |
if {$what in {all local}} { |
| 1498 |
# Need to load auto.def now |
| 1499 |
if {[file exists $::autosetup(autodef)]} { |
| 1500 |
# Load auto.def as module "auto.def" |
| 1501 |
autosetup_load_module auto.def source $::autosetup(autodef) |
| 1502 |
} |
| 1503 |
if {$what eq "all"} { |
| 1504 |
set what * |
| 1505 |
} else { |
| 1506 |
set what auto.def |
| 1507 |
} |
| 1508 |
} else { |
| 1509 |
use $what |
| 1510 |
puts "Options for module $what:" |
| 1511 |
} |
| 1512 |
options-show $what |
| 1513 |
exit 0 |
| 1514 |
} |
| 1515 |
|
| 1516 |
proc autosetup_show_license {} { |
| 1517 |
global modsource autosetup |
| 1518 |
use_pager |
| 1519 |
|
| 1520 |
if {[info exists modsource(LICENSE)]} { |
| 1521 |
puts $modsource(LICENSE) |
| 1522 |
return |
| 1523 |
} |
| 1524 |
foreach dir [list $autosetup(libdir) $autosetup(srcdir)] { |
| 1525 |
set path [file join $dir LICENSE] |
| 1526 |
if {[file exists $path]} { |
| 1527 |
puts [readfile $path] |
| 1528 |
return |
| 1529 |
} |
| 1530 |
} |
| 1531 |
puts "LICENSE not found" |
| 1532 |
} |
| 1533 |
|
| 1534 |
# If not already paged and stdout is a tty, pipe the output through the pager |
| 1535 |
# This is done by reinvoking autosetup with --nopager added |
| 1536 |
proc use_pager {} { |
| 1537 |
if {![opt-bool nopager] && [getenv PAGER ""] ne "" && [isatty? stdin] && [isatty? stdout]} { |
| 1538 |
if {[catch { |
| 1539 |
exec [info nameofexecutable] $::argv0 --nopager {*}$::argv |& {*}[getenv PAGER] >@stdout <@stdin 2>@stderr |
| 1540 |
} msg opts] == 1} { |
| 1541 |
if {[dict get $opts -errorcode] eq "NONE"} { |
| 1542 |
# an internal/exec error |
| 1543 |
puts stderr $msg |
| 1544 |
exit 1 |
| 1545 |
} |
| 1546 |
} |
| 1547 |
exit 0 |
| 1548 |
} |
| 1549 |
} |
| 1550 |
|
| 1551 |
# Outputs the autosetup references in one of several formats |
| 1552 |
proc autosetup_reference {{type text}} { |
| 1553 |
|
| 1554 |
use_pager |
| 1555 |
|
| 1556 |
switch -glob -- $type { |
| 1557 |
wiki {use wiki-formatting} |
| 1558 |
ascii* {use asciidoc-formatting} |
| 1559 |
md - markdown {use markdown-formatting} |
| 1560 |
default {use text-formatting} |
| 1561 |
} |
| 1562 |
|
| 1563 |
title "[autosetup_version] -- Command Reference" |
| 1564 |
|
| 1565 |
section {Introduction} |
| 1566 |
|
| 1567 |
p { |
| 1568 |
See https://msteveb.github.io/autosetup/ for the online documentation for 'autosetup'. |
| 1569 |
This documentation can also be accessed locally with `autosetup --ref`. |
| 1570 |
} |
| 1571 |
|
| 1572 |
p { |
| 1573 |
'autosetup' provides a number of built-in commands which |
| 1574 |
are documented below. These may be used from 'auto.def' to test |
| 1575 |
for features, define variables, create files from templates and |
| 1576 |
other similar actions. |
| 1577 |
} |
| 1578 |
|
| 1579 |
automf_command_reference |
| 1580 |
|
| 1581 |
exit 0 |
| 1582 |
} |
| 1583 |
|
| 1584 |
proc autosetup_output_block {type lines} { |
| 1585 |
if {[llength $lines]} { |
| 1586 |
switch $type { |
| 1587 |
section { |
| 1588 |
section $lines |
| 1589 |
} |
| 1590 |
subsection { |
| 1591 |
subsection $lines |
| 1592 |
} |
| 1593 |
code { |
| 1594 |
codelines $lines |
| 1595 |
} |
| 1596 |
p { |
| 1597 |
p [join $lines] |
| 1598 |
} |
| 1599 |
list { |
| 1600 |
foreach line $lines { |
| 1601 |
bullet $line |
| 1602 |
} |
| 1603 |
nl |
| 1604 |
} |
| 1605 |
} |
| 1606 |
} |
| 1607 |
} |
| 1608 |
|
| 1609 |
# Generate a command reference from inline documentation |
| 1610 |
proc automf_command_reference {} { |
| 1611 |
lappend files $::autosetup(prog) |
| 1612 |
lappend files {*}[lsort [glob -nocomplain $::autosetup(libdir)/*.tcl]] |
| 1613 |
|
| 1614 |
# We want to process all non-module files before module files |
| 1615 |
# and then modules in alphabetical order. |
| 1616 |
# So examine all files and extract docs into doc($modulename) and doc(_core_) |
| 1617 |
# |
| 1618 |
# Each entry is a list of {type data} where $type is one of: section, subsection, code, list, p |
| 1619 |
# and $data is a string for section, subsection or a list of text lines for other types. |
| 1620 |
|
| 1621 |
# XXX: Should commands be in alphabetical order too? Currently they are in file order. |
| 1622 |
|
| 1623 |
set doc(_core_) {} |
| 1624 |
lappend doc(_core_) [list section "Core Commands"] |
| 1625 |
|
| 1626 |
foreach file $files { |
| 1627 |
set modulename [file rootname [file tail $file]] |
| 1628 |
set current _core_ |
| 1629 |
set f [open $file] |
| 1630 |
while {![eof $f]} { |
| 1631 |
set line [gets $f] |
| 1632 |
|
| 1633 |
if {[regexp {^#.*@section (.*)$} $line -> section]} { |
| 1634 |
lappend doc($current) [list section $section] |
| 1635 |
continue |
| 1636 |
} |
| 1637 |
|
| 1638 |
# Find embedded module names |
| 1639 |
if {[regexp {^#.*@module ([^ ]*)} $line -> modulename]} { |
| 1640 |
continue |
| 1641 |
} |
| 1642 |
|
| 1643 |
# Find lines starting with "# @*" and continuing through the remaining comment lines |
| 1644 |
if {![regexp {^# @(.*)} $line -> cmd]} { |
| 1645 |
continue |
| 1646 |
} |
| 1647 |
|
| 1648 |
# Synopsis or command? |
| 1649 |
if {$cmd eq "synopsis:"} { |
| 1650 |
set current $modulename |
| 1651 |
lappend doc($current) [list section "Module: $modulename"] |
| 1652 |
} else { |
| 1653 |
lappend doc($current) [list subsection $cmd] |
| 1654 |
} |
| 1655 |
|
| 1656 |
set lines {} |
| 1657 |
set type p |
| 1658 |
|
| 1659 |
# Now the description |
| 1660 |
while {![eof $f]} { |
| 1661 |
set line [gets $f] |
| 1662 |
|
| 1663 |
if {![regexp {^#(#)? ?(.*)} $line -> hash cmd]} { |
| 1664 |
break |
| 1665 |
} |
| 1666 |
if {$hash eq "#"} { |
| 1667 |
set t code |
| 1668 |
} elseif {[regexp {^- (.*)} $cmd -> cmd]} { |
| 1669 |
set t list |
| 1670 |
} else { |
| 1671 |
set t p |
| 1672 |
} |
| 1673 |
|
| 1674 |
#puts "hash=$hash, oldhash=$oldhash, lines=[llength $lines], cmd=$cmd" |
| 1675 |
|
| 1676 |
if {$t ne $type || $cmd eq ""} { |
| 1677 |
# Finish the current block |
| 1678 |
lappend doc($current) [list $type $lines] |
| 1679 |
set lines {} |
| 1680 |
set type $t |
| 1681 |
} |
| 1682 |
if {$cmd ne ""} { |
| 1683 |
lappend lines $cmd |
| 1684 |
} |
| 1685 |
} |
| 1686 |
|
| 1687 |
lappend doc($current) [list $type $lines] |
| 1688 |
} |
| 1689 |
close $f |
| 1690 |
} |
| 1691 |
|
| 1692 |
# Now format and output the results |
| 1693 |
|
| 1694 |
# _core_ will sort first |
| 1695 |
foreach module [lsort [array names doc]] { |
| 1696 |
foreach item $doc($module) { |
| 1697 |
autosetup_output_block {*}$item |
| 1698 |
} |
| 1699 |
} |
| 1700 |
} |
| 1701 |
} |
| 1702 |
|
| 1703 |
# ----- @module init.tcl ----- |
| 1704 |
|
| 1705 |
set modsource(init.tcl) { |
| 1706 |
# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/ |
| 1707 |
# All rights reserved |
| 1708 |
|
| 1709 |
# Module to help create auto.def and configure |
| 1710 |
|
| 1711 |
proc autosetup_init {type} { |
| 1712 |
set help 0 |
| 1713 |
if {$type in {? help}} { |
| 1714 |
incr help |
| 1715 |
} elseif {![dict exists $::autosetup(inittypes) $type]} { |
| 1716 |
puts "Unknown type, --init=$type" |
| 1717 |
incr help |
| 1718 |
} |
| 1719 |
if {$help} { |
| 1720 |
puts "Use one of the following types (e.g. --init=make)\n" |
| 1721 |
foreach type [lsort [dict keys $::autosetup(inittypes)]] { |
| 1722 |
lassign [dict get $::autosetup(inittypes) $type] desc |
| 1723 |
# XXX: Use the options-show code to wrap the description |
| 1724 |
puts [format "%-10s %s" $type $desc] |
| 1725 |
} |
| 1726 |
return |
| 1727 |
} |
| 1728 |
lassign [dict get $::autosetup(inittypes) $type] desc script |
| 1729 |
|
| 1730 |
puts "Initialising $type: $desc\n" |
| 1731 |
|
| 1732 |
# All initialisations happens in the top level srcdir |
| 1733 |
cd $::autosetup(srcdir) |
| 1734 |
|
| 1735 |
uplevel #0 $script |
| 1736 |
} |
| 1737 |
|
| 1738 |
proc autosetup_add_init_type {type desc script} { |
| 1739 |
dict set ::autosetup(inittypes) $type [list $desc $script] |
| 1740 |
} |
| 1741 |
|
| 1742 |
# This is for in creating build-system init scripts |
| 1743 |
# |
| 1744 |
# If the file doesn't exist, create it containing $contents |
| 1745 |
# If the file does exist, only overwrite if --force is specified. |
| 1746 |
# |
| 1747 |
proc autosetup_check_create {filename contents} { |
| 1748 |
if {[file exists $filename]} { |
| 1749 |
if {!$::autosetup(force)} { |
| 1750 |
puts "I see $filename already exists." |
| 1751 |
return |
| 1752 |
} else { |
| 1753 |
puts "I will overwrite the existing $filename because you used --force." |
| 1754 |
} |
| 1755 |
} else { |
| 1756 |
puts "I don't see $filename, so I will create it." |
| 1757 |
} |
| 1758 |
writefile $filename $contents |
| 1759 |
} |
| 1760 |
} |
| 1761 |
|
| 1762 |
# ----- @module install.tcl ----- |
| 1763 |
|
| 1764 |
set modsource(install.tcl) { |
| 1765 |
# Copyright (c) 2006-2010 WorkWare Systems http://www.workware.net.au/ |
| 1766 |
# All rights reserved |
| 1767 |
|
| 1768 |
# Module which can install autosetup |
| 1769 |
|
| 1770 |
# autosetup(installed)=1 means that autosetup is not running from source |
| 1771 |
# autosetup(sysinstall)=1 means that autosetup is running from a sysinstall version |
| 1772 |
# shared=1 means that we are trying to do a sysinstall. This is only possible from the development source. |
| 1773 |
|
| 1774 |
proc autosetup_install {dir {shared 0}} { |
| 1775 |
global autosetup |
| 1776 |
if {$shared} { |
| 1777 |
if {$autosetup(installed) || $autosetup(sysinstall)} { |
| 1778 |
user-error "Can only --sysinstall from development sources" |
| 1779 |
} |
| 1780 |
} elseif {$autosetup(installed) && !$autosetup(sysinstall)} { |
| 1781 |
user-error "Can't --install from project install" |
| 1782 |
} |
| 1783 |
|
| 1784 |
if {$autosetup(sysinstall)} { |
| 1785 |
# This is the sysinstall version, so install just uses references |
| 1786 |
cd $dir |
| 1787 |
|
| 1788 |
puts "[autosetup_version] creating configure to use system-installed autosetup" |
| 1789 |
autosetup_create_configure 1 |
| 1790 |
puts "Creating autosetup/README.autosetup" |
| 1791 |
file mkdir autosetup |
| 1792 |
autosetup_install_readme autosetup/README.autosetup 1 |
| 1793 |
return |
| 1794 |
} |
| 1795 |
|
| 1796 |
if {[catch { |
| 1797 |
if {$shared} { |
| 1798 |
set target $dir/bin/autosetup |
| 1799 |
set installedas $target |
| 1800 |
} else { |
| 1801 |
if {$dir eq "."} { |
| 1802 |
set installedas autosetup |
| 1803 |
} else { |
| 1804 |
set installedas $dir/autosetup |
| 1805 |
} |
| 1806 |
cd $dir |
| 1807 |
file mkdir autosetup |
| 1808 |
set target autosetup/autosetup |
| 1809 |
} |
| 1810 |
set targetdir [file dirname $target] |
| 1811 |
file mkdir $targetdir |
| 1812 |
|
| 1813 |
set f [open $target w] |
| 1814 |
|
| 1815 |
set publicmodules {} |
| 1816 |
|
| 1817 |
# First the main script, but only up until "CUT HERE" |
| 1818 |
set in [open $autosetup(dir)/autosetup] |
| 1819 |
while {[gets $in buf] >= 0} { |
| 1820 |
if {$buf ne "##-- CUT HERE --##"} { |
| 1821 |
puts $f $buf |
| 1822 |
continue |
| 1823 |
} |
| 1824 |
|
| 1825 |
# Insert the static modules here |
| 1826 |
# i.e. those which don't contain @synopsis: |
| 1827 |
# All modules are inserted if $shared is set |
| 1828 |
puts $f "set autosetup(installed) 1" |
| 1829 |
puts $f "set autosetup(sysinstall) $shared" |
| 1830 |
foreach file [lsort [glob $autosetup(libdir)/*.{tcl,auto}]] { |
| 1831 |
set modname [file tail $file] |
| 1832 |
set ext [file ext $modname] |
| 1833 |
set buf [readfile $file] |
| 1834 |
if {!$shared} { |
| 1835 |
if {$ext eq ".auto" || [string match "*\n# @synopsis:*" $buf]} { |
| 1836 |
lappend publicmodules $file |
| 1837 |
continue |
| 1838 |
} |
| 1839 |
} |
| 1840 |
dputs "install: importing lib/[file tail $file]" |
| 1841 |
puts $f "# ----- @module $modname -----" |
| 1842 |
puts $f "\nset modsource($modname) \{" |
| 1843 |
puts $f $buf |
| 1844 |
puts $f "\}\n" |
| 1845 |
} |
| 1846 |
if {$shared} { |
| 1847 |
foreach {srcname destname} [list $autosetup(libdir)/README.autosetup-lib README.autosetup \ |
| 1848 |
$autosetup(srcdir)/LICENSE LICENSE] { |
| 1849 |
dputs "install: importing $srcname as $destname" |
| 1850 |
puts $f "\nset modsource($destname) \\\n[list [readfile $srcname]\n]\n" |
| 1851 |
} |
| 1852 |
} |
| 1853 |
} |
| 1854 |
close $in |
| 1855 |
close $f |
| 1856 |
catch {exec chmod 755 $target} |
| 1857 |
|
| 1858 |
set installfiles {autosetup-config.guess autosetup-config.sub autosetup-test-tclsh} |
| 1859 |
set removefiles {} |
| 1860 |
|
| 1861 |
if {!$shared} { |
| 1862 |
autosetup_install_readme $targetdir/README.autosetup 0 |
| 1863 |
|
| 1864 |
# Install public modules |
| 1865 |
foreach file $publicmodules { |
| 1866 |
set tail [file tail $file] |
| 1867 |
autosetup_install_file $file $targetdir/$tail |
| 1868 |
} |
| 1869 |
lappend installfiles jimsh0.c autosetup-find-tclsh LICENSE |
| 1870 |
lappend removefiles config.guess config.sub test-tclsh find-tclsh |
| 1871 |
} else { |
| 1872 |
lappend installfiles {sys-find-tclsh autosetup-find-tclsh} |
| 1873 |
} |
| 1874 |
|
| 1875 |
# Install support files |
| 1876 |
foreach fileinfo $installfiles { |
| 1877 |
if {[llength $fileinfo] == 2} { |
| 1878 |
lassign $fileinfo source dest |
| 1879 |
} else { |
| 1880 |
lassign $fileinfo source |
| 1881 |
set dest $source |
| 1882 |
} |
| 1883 |
autosetup_install_file $autosetup(dir)/$source $targetdir/$dest |
| 1884 |
} |
| 1885 |
|
| 1886 |
# Remove obsolete files |
| 1887 |
foreach file $removefiles { |
| 1888 |
if {[file exists $targetdir/$file]} { |
| 1889 |
file delete $targetdir/$file |
| 1890 |
} |
| 1891 |
} |
| 1892 |
} error]} { |
| 1893 |
user-error "Failed to install autosetup: $error" |
| 1894 |
} |
| 1895 |
if {$shared} { |
| 1896 |
set type "system" |
| 1897 |
} else { |
| 1898 |
set type "local" |
| 1899 |
} |
| 1900 |
puts "Installed $type [autosetup_version] to $installedas" |
| 1901 |
|
| 1902 |
if {!$shared} { |
| 1903 |
# Now create 'configure' if necessary |
| 1904 |
autosetup_create_configure 0 |
| 1905 |
} |
| 1906 |
} |
| 1907 |
|
| 1908 |
proc autosetup_create_configure {shared} { |
| 1909 |
if {[file exists configure]} { |
| 1910 |
if {!$::autosetup(force)} { |
| 1911 |
# Could this be an autosetup configure? |
| 1912 |
if {![string match "*\nWRAPPER=*" [readfile configure]]} { |
| 1913 |
puts "I see configure, but not created by autosetup, so I won't overwrite it." |
| 1914 |
puts "Remove it or use --force to overwrite." |
| 1915 |
return |
| 1916 |
} |
| 1917 |
} else { |
| 1918 |
puts "I will overwrite the existing configure because you used --force." |
| 1919 |
} |
| 1920 |
} else { |
| 1921 |
puts "I don't see configure, so I will create it." |
| 1922 |
} |
| 1923 |
if {$shared} { |
| 1924 |
writefile configure \ |
| 1925 |
{#!/bin/sh |
| 1926 |
WRAPPER="$0"; export WRAPPER; "autosetup" "$@" |
| 1927 |
} |
| 1928 |
} else { |
| 1929 |
writefile configure \ |
| 1930 |
{#!/bin/sh |
| 1931 |
dir="`dirname "$0"`/autosetup" |
| 1932 |
#@@INITCHECK@@# |
| 1933 |
WRAPPER="$0"; export WRAPPER; exec "`"$dir/autosetup-find-tclsh"`" "$dir/autosetup" "$@" |
| 1934 |
} |
| 1935 |
} |
| 1936 |
catch {exec chmod 755 configure} |
| 1937 |
} |
| 1938 |
|
| 1939 |
# Append the contents of $file to filehandle $f |
| 1940 |
proc autosetup_install_append {f file} { |
| 1941 |
dputs "install: include $file" |
| 1942 |
set in [open $file] |
| 1943 |
puts $f [read $in] |
| 1944 |
close $in |
| 1945 |
} |
| 1946 |
|
| 1947 |
proc autosetup_install_file {source target} { |
| 1948 |
dputs "install: $source => $target" |
| 1949 |
if {![file exists $source]} { |
| 1950 |
error "Missing installation file '$source'" |
| 1951 |
} |
| 1952 |
writefile $target [readfile $source]\n |
| 1953 |
# If possible, copy the file mode |
| 1954 |
file stat $source stat |
| 1955 |
set mode [format %o [expr {$stat(mode) & 0x1ff}]] |
| 1956 |
catch {exec chmod $mode $target} |
| 1957 |
} |
| 1958 |
|
| 1959 |
proc autosetup_install_readme {target sysinstall} { |
| 1960 |
set readme "README.autosetup created by [autosetup_version]\n\n" |
| 1961 |
if {$sysinstall} { |
| 1962 |
append readme \ |
| 1963 |
{This is the autosetup directory for a system install of autosetup. |
| 1964 |
Loadable modules can be added here. |
| 1965 |
} |
| 1966 |
} else { |
| 1967 |
append readme \ |
| 1968 |
{This is the autosetup directory for a local install of autosetup. |
| 1969 |
It contains autosetup, support files and loadable modules. |
| 1970 |
} |
| 1971 |
} |
| 1972 |
|
| 1973 |
append readme { |
| 1974 |
*.tcl files in this directory are optional modules which |
| 1975 |
can be loaded with the 'use' directive. |
| 1976 |
|
| 1977 |
*.auto files in this directory are auto-loaded. |
| 1978 |
|
| 1979 |
For more information, see https://msteveb.github.io/autosetup/ |
| 1980 |
} |
| 1981 |
dputs "install: autosetup/README.autosetup" |
| 1982 |
writefile $target $readme |
| 1983 |
} |
| 1984 |
} |
| 1985 |
|
| 1986 |
# ----- @module markdown-formatting.tcl ----- |
| 1987 |
|
| 1988 |
set modsource(markdown-formatting.tcl) { |
| 1989 |
# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/ |
| 1990 |
# All rights reserved |
| 1991 |
|
| 1992 |
# Module which provides text formatting |
| 1993 |
# markdown format (kramdown syntax) |
| 1994 |
|
| 1995 |
use formatting |
| 1996 |
|
| 1997 |
proc para {text} { |
| 1998 |
regsub -all "\[ \t\n\]+" [string trim $text] " " text |
| 1999 |
regsub -all {([^a-zA-Z])'([^']*)'} $text {\1**`\2`**} text |
| 2000 |
regsub -all {^'([^']*)'} $text {**`\1`**} text |
| 2001 |
regsub -all {(http[^ \t\n]*)} $text {[\1](\1)} text |
| 2002 |
return $text |
| 2003 |
} |
| 2004 |
proc title {text} { |
| 2005 |
underline [para $text] = |
| 2006 |
nl |
| 2007 |
} |
| 2008 |
proc p {text} { |
| 2009 |
puts [para $text] |
| 2010 |
nl |
| 2011 |
} |
| 2012 |
proc codelines {lines} { |
| 2013 |
puts "~~~~~~~~~~~~" |
| 2014 |
foreach line $lines { |
| 2015 |
puts $line |
| 2016 |
} |
| 2017 |
puts "~~~~~~~~~~~~" |
| 2018 |
nl |
| 2019 |
} |
| 2020 |
proc code {text} { |
| 2021 |
puts "~~~~~~~~~~~~" |
| 2022 |
foreach line [parse_code_block $text] { |
| 2023 |
puts $line |
| 2024 |
} |
| 2025 |
puts "~~~~~~~~~~~~" |
| 2026 |
nl |
| 2027 |
} |
| 2028 |
proc nl {} { |
| 2029 |
puts "" |
| 2030 |
} |
| 2031 |
proc underline {text char} { |
| 2032 |
regexp "^(\[ \t\]*)(.*)" $text -> indent words |
| 2033 |
puts $text |
| 2034 |
puts $indent[string repeat $char [string length $words]] |
| 2035 |
} |
| 2036 |
proc section {text} { |
| 2037 |
underline "[para $text]" - |
| 2038 |
nl |
| 2039 |
} |
| 2040 |
proc subsection {text} { |
| 2041 |
puts "### `$text`" |
| 2042 |
nl |
| 2043 |
} |
| 2044 |
proc bullet {text} { |
| 2045 |
puts "* [para $text]" |
| 2046 |
} |
| 2047 |
proc defn {first args} { |
| 2048 |
puts "^" |
| 2049 |
set defn [string trim [join $args \n]] |
| 2050 |
if {$first ne ""} { |
| 2051 |
puts "**${first}**" |
| 2052 |
puts -nonewline ": " |
| 2053 |
regsub -all "\n\n" $defn "\n: " defn |
| 2054 |
} |
| 2055 |
puts "$defn" |
| 2056 |
} |
| 2057 |
} |
| 2058 |
|
| 2059 |
# ----- @module misc.tcl ----- |
| 2060 |
|
| 2061 |
set modsource(misc.tcl) { |
| 2062 |
# Copyright (c) 2007-2010 WorkWare Systems http://www.workware.net.au/ |
| 2063 |
# All rights reserved |
| 2064 |
|
| 2065 |
# Module containing misc procs useful to modules |
| 2066 |
# Largely for platform compatibility |
| 2067 |
|
| 2068 |
set autosetup(istcl) [info exists ::tcl_library] |
| 2069 |
set autosetup(iswin) [string equal windows $tcl_platform(platform)] |
| 2070 |
|
| 2071 |
if {$autosetup(iswin)} { |
| 2072 |
# mingw/windows separates $PATH with semicolons |
| 2073 |
# and doesn't have an executable bit |
| 2074 |
proc split-path {} { |
| 2075 |
split [getenv PATH .] {;} |
| 2076 |
} |
| 2077 |
proc file-isexec {exec} { |
| 2078 |
# Basic test for windows. We ignore .bat |
| 2079 |
if {[file isfile $exec] || [file isfile $exec.exe]} { |
| 2080 |
return 1 |
| 2081 |
} |
| 2082 |
return 0 |
| 2083 |
} |
| 2084 |
} else { |
| 2085 |
# unix separates $PATH with colons and has and executable bit |
| 2086 |
proc split-path {} { |
| 2087 |
split [getenv PATH .] : |
| 2088 |
} |
| 2089 |
proc file-isexec {exec} { |
| 2090 |
file executable $exec |
| 2091 |
} |
| 2092 |
} |
| 2093 |
|
| 2094 |
# Assume that exec can return stdout and stderr |
| 2095 |
proc exec-with-stderr {args} { |
| 2096 |
exec {*}$args 2>@1 |
| 2097 |
} |
| 2098 |
|
| 2099 |
if {$autosetup(istcl)} { |
| 2100 |
# Tcl doesn't have the env command |
| 2101 |
proc getenv {name args} { |
| 2102 |
if {[info exists ::env($name)]} { |
| 2103 |
return $::env($name) |
| 2104 |
} |
| 2105 |
if {[llength $args]} { |
| 2106 |
return [lindex $args 0] |
| 2107 |
} |
| 2108 |
return -code error "environment variable \"$name\" does not exist" |
| 2109 |
} |
| 2110 |
proc isatty? {channel} { |
| 2111 |
dict exists [fconfigure $channel] -xchar |
| 2112 |
} |
| 2113 |
# Jim-compatible stacktrace using info frame |
| 2114 |
proc stacktrace {} { |
| 2115 |
set stacktrace {} |
| 2116 |
# 2 to skip the current frame |
| 2117 |
for {set i 2} {$i < [info frame]} {incr i} { |
| 2118 |
set frame [info frame -$i] |
| 2119 |
if {[dict exists $frame file]} { |
| 2120 |
# We don't need proc, so use "" |
| 2121 |
lappend stacktrace "" [dict get $frame file] [dict get $frame line] |
| 2122 |
} |
| 2123 |
} |
| 2124 |
return $stacktrace |
| 2125 |
} |
| 2126 |
} else { |
| 2127 |
if {$autosetup(iswin)} { |
| 2128 |
# On Windows, backslash convert all environment variables |
| 2129 |
# (Assume that Tcl does this for us) |
| 2130 |
proc getenv {name args} { |
| 2131 |
string map {\\ /} [env $name {*}$args] |
| 2132 |
} |
| 2133 |
} else { |
| 2134 |
# Jim on unix is simple |
| 2135 |
alias getenv env |
| 2136 |
} |
| 2137 |
proc isatty? {channel} { |
| 2138 |
set tty 0 |
| 2139 |
catch { |
| 2140 |
# isatty is a recent addition to Jim Tcl |
| 2141 |
set tty [$channel isatty] |
| 2142 |
} |
| 2143 |
return $tty |
| 2144 |
} |
| 2145 |
} |
| 2146 |
|
| 2147 |
# In case 'file normalize' doesn't exist |
| 2148 |
# |
| 2149 |
proc file-normalize {path} { |
| 2150 |
if {[catch {file normalize $path} result]} { |
| 2151 |
if {$path eq ""} { |
| 2152 |
return "" |
| 2153 |
} |
| 2154 |
set oldpwd [pwd] |
| 2155 |
if {[file isdir $path]} { |
| 2156 |
cd $path |
| 2157 |
set result [pwd] |
| 2158 |
} else { |
| 2159 |
cd [file dirname $path] |
| 2160 |
set result [file join [pwd] [file tail $path]] |
| 2161 |
} |
| 2162 |
cd $oldpwd |
| 2163 |
} |
| 2164 |
return $result |
| 2165 |
} |
| 2166 |
|
| 2167 |
# If everything is working properly, the only errors which occur |
| 2168 |
# should be generated in user code (e.g. auto.def). |
| 2169 |
# By default, we only want to show the error location in user code. |
| 2170 |
# We use [info frame] to achieve this, but it works differently on Tcl and Jim. |
| 2171 |
# |
| 2172 |
# This is designed to be called for incorrect usage in auto.def, via autosetup-error |
| 2173 |
# |
| 2174 |
proc error-location {msg} { |
| 2175 |
if {$::autosetup(debug)} { |
| 2176 |
return -code error $msg |
| 2177 |
} |
| 2178 |
# Search back through the stack trace for the first error in a .def file |
| 2179 |
foreach {p f l} [stacktrace] { |
| 2180 |
if {[string match *.def $f]} { |
| 2181 |
return "[relative-path $f]:$l: Error: $msg" |
| 2182 |
} |
| 2183 |
#puts "Skipping $f:$l" |
| 2184 |
} |
| 2185 |
return $msg |
| 2186 |
} |
| 2187 |
|
| 2188 |
# If everything is working properly, the only errors which occur |
| 2189 |
# should be generated in user code (e.g. auto.def). |
| 2190 |
# By default, we only want to show the error location in user code. |
| 2191 |
# We use [info frame] to achieve this, but it works differently on Tcl and Jim. |
| 2192 |
# |
| 2193 |
# This is designed to be called for incorrect usage in auto.def, via autosetup-error |
| 2194 |
# |
| 2195 |
proc error-stacktrace {msg} { |
| 2196 |
if {$::autosetup(debug)} { |
| 2197 |
return -code error $msg |
| 2198 |
} |
| 2199 |
# Search back through the stack trace for the first error in a .def file |
| 2200 |
for {set i 1} {$i < [info level]} {incr i} { |
| 2201 |
if {$::autosetup(istcl)} { |
| 2202 |
array set info [info frame -$i] |
| 2203 |
} else { |
| 2204 |
lassign [info frame -$i] info(caller) info(file) info(line) |
| 2205 |
} |
| 2206 |
if {[string match *.def $info(file)]} { |
| 2207 |
return "[relative-path $info(file)]:$info(line): Error: $msg" |
| 2208 |
} |
| 2209 |
#puts "Skipping $info(file):$info(line)" |
| 2210 |
} |
| 2211 |
return $msg |
| 2212 |
} |
| 2213 |
|
| 2214 |
# Given the return from [catch {...} msg opts], returns an appropriate |
| 2215 |
# error message. A nice one for Jim and a less-nice one for Tcl. |
| 2216 |
# If 'fulltrace' is set, a full stack trace is provided. |
| 2217 |
# Otherwise a simple message is provided. |
| 2218 |
# |
| 2219 |
# This is designed for developer errors, e.g. in module code or auto.def code |
| 2220 |
# |
| 2221 |
# |
| 2222 |
proc error-dump {msg opts fulltrace} { |
| 2223 |
if {$::autosetup(istcl)} { |
| 2224 |
if {$fulltrace} { |
| 2225 |
return "Error: [dict get $opts -errorinfo]" |
| 2226 |
} else { |
| 2227 |
return "Error: $msg" |
| 2228 |
} |
| 2229 |
} else { |
| 2230 |
lassign $opts(-errorinfo) p f l |
| 2231 |
if {$f ne ""} { |
| 2232 |
set result "$f:$l: Error: " |
| 2233 |
} |
| 2234 |
append result "$msg\n" |
| 2235 |
if {$fulltrace} { |
| 2236 |
append result [stackdump $opts(-errorinfo)] |
| 2237 |
} |
| 2238 |
|
| 2239 |
# Remove the trailing newline |
| 2240 |
string trim $result |
| 2241 |
} |
| 2242 |
} |
| 2243 |
} |
| 2244 |
|
| 2245 |
# ----- @module text-formatting.tcl ----- |
| 2246 |
|
| 2247 |
set modsource(text-formatting.tcl) { |
| 2248 |
# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/ |
| 2249 |
# All rights reserved |
| 2250 |
|
| 2251 |
# Module which provides text formatting |
| 2252 |
|
| 2253 |
use formatting |
| 2254 |
|
| 2255 |
proc wordwrap {text length {firstprefix ""} {nextprefix ""}} { |
| 2256 |
set len 0 |
| 2257 |
set space $firstprefix |
| 2258 |
|
| 2259 |
foreach word [split $text] { |
| 2260 |
set word [string trim $word] |
| 2261 |
if {$word eq ""} { |
| 2262 |
continue |
| 2263 |
} |
| 2264 |
if {[info exists partial]} { |
| 2265 |
append partial " " $word |
| 2266 |
if {[string first $quote $word] < 0} { |
| 2267 |
# Haven't found end of quoted word |
| 2268 |
continue |
| 2269 |
} |
| 2270 |
# Finished quoted word |
| 2271 |
set word $partial |
| 2272 |
unset partial |
| 2273 |
unset quote |
| 2274 |
} else { |
| 2275 |
set quote [string index $word 0] |
| 2276 |
if {$quote in {' *}} { |
| 2277 |
if {[string first $quote $word 1] < 0} { |
| 2278 |
# Haven't found end of quoted word |
| 2279 |
# Not a whole word. |
| 2280 |
set first [string index $word 0] |
| 2281 |
# Start of quoted word |
| 2282 |
set partial $word |
| 2283 |
continue |
| 2284 |
} |
| 2285 |
} |
| 2286 |
} |
| 2287 |
|
| 2288 |
if {$len && [string length $space$word] + $len >= $length} { |
| 2289 |
puts "" |
| 2290 |
set len 0 |
| 2291 |
set space $nextprefix |
| 2292 |
} |
| 2293 |
incr len [string length $space$word] |
| 2294 |
|
| 2295 |
# Use man-page conventions for highlighting 'quoted' and *quoted* |
| 2296 |
# single words. |
| 2297 |
# Use x^Hx for *bold* and _^Hx for 'underline'. |
| 2298 |
# |
| 2299 |
# less and more will both understand this. |
| 2300 |
# Pipe through 'col -b' to remove them. |
| 2301 |
if {[regexp {^'(.*)'(.*)} $word -> quoted after]} { |
| 2302 |
set quoted [string map {~ " "} $quoted] |
| 2303 |
regsub -all . $quoted "&\b&" quoted |
| 2304 |
set word $quoted$after |
| 2305 |
} elseif {[regexp {^[*](.*)[*](.*)} $word -> quoted after]} { |
| 2306 |
set quoted [string map {~ " "} $quoted] |
| 2307 |
regsub -all . $quoted "_\b&" quoted |
| 2308 |
set word $quoted$after |
| 2309 |
} |
| 2310 |
puts -nonewline $space$word |
| 2311 |
set space " " |
| 2312 |
} |
| 2313 |
if {[info exists partial]} { |
| 2314 |
# Missing end of quote |
| 2315 |
puts -nonewline $space$partial |
| 2316 |
} |
| 2317 |
if {$len} { |
| 2318 |
puts "" |
| 2319 |
} |
| 2320 |
} |
| 2321 |
proc title {text} { |
| 2322 |
underline [string trim $text] = |
| 2323 |
nl |
| 2324 |
} |
| 2325 |
proc p {text} { |
| 2326 |
wordwrap $text 80 |
| 2327 |
nl |
| 2328 |
} |
| 2329 |
proc codelines {lines} { |
| 2330 |
foreach line $lines { |
| 2331 |
puts " $line" |
| 2332 |
} |
| 2333 |
nl |
| 2334 |
} |
| 2335 |
proc nl {} { |
| 2336 |
puts "" |
| 2337 |
} |
| 2338 |
proc underline {text char} { |
| 2339 |
regexp "^(\[ \t\]*)(.*)" $text -> indent words |
| 2340 |
puts $text |
| 2341 |
puts $indent[string repeat $char [string length $words]] |
| 2342 |
} |
| 2343 |
proc section {text} { |
| 2344 |
underline "[string trim $text]" - |
| 2345 |
nl |
| 2346 |
} |
| 2347 |
proc subsection {text} { |
| 2348 |
underline "$text" ~ |
| 2349 |
nl |
| 2350 |
} |
| 2351 |
proc bullet {text} { |
| 2352 |
wordwrap $text 76 " * " " " |
| 2353 |
} |
| 2354 |
proc indent {text} { |
| 2355 |
wordwrap $text 76 " " " " |
| 2356 |
} |
| 2357 |
proc defn {first args} { |
| 2358 |
if {$first ne ""} { |
| 2359 |
underline " $first" ~ |
| 2360 |
} |
| 2361 |
foreach p $args { |
| 2362 |
if {$p ne ""} { |
| 2363 |
indent $p |
| 2364 |
} |
| 2365 |
} |
| 2366 |
} |
| 2367 |
} |
| 2368 |
|
| 2369 |
# ----- @module util.tcl ----- |
| 2370 |
|
| 2371 |
set modsource(util.tcl) { |
| 2372 |
# Copyright (c) 2012 WorkWare Systems http://www.workware.net.au/ |
| 2373 |
# All rights reserved |
| 2374 |
|
| 2375 |
# Module which contains miscellaneous utility functions |
| 2376 |
|
| 2377 |
# @section Utilities |
| 2378 |
|
| 2379 |
# @compare-versions version1 version2 |
| 2380 |
# |
| 2381 |
# Versions are of the form 'a.b.c' (may be any number of numeric components) |
| 2382 |
# |
| 2383 |
# Compares the two versions and returns: |
| 2384 |
## -1 if v1 < v2 |
| 2385 |
## 0 if v1 == v2 |
| 2386 |
## 1 if v1 > v2 |
| 2387 |
# |
| 2388 |
# If one version has fewer components than the other, 0 is substituted to the right. e.g. |
| 2389 |
## 0.2 < 0.3 |
| 2390 |
## 0.2.5 > 0.2 |
| 2391 |
## 1.1 == 1.1.0 |
| 2392 |
# |
| 2393 |
proc compare-versions {v1 v2} { |
| 2394 |
foreach c1 [split $v1 .] c2 [split $v2 .] { |
| 2395 |
if {$c1 eq ""} { |
| 2396 |
set c1 0 |
| 2397 |
} |
| 2398 |
if {$c2 eq ""} { |
| 2399 |
set c2 0 |
| 2400 |
} |
| 2401 |
if {$c1 < $c2} { |
| 2402 |
return -1 |
| 2403 |
} |
| 2404 |
if {$c1 > $c2} { |
| 2405 |
return 1 |
| 2406 |
} |
| 2407 |
} |
| 2408 |
return 0 |
| 2409 |
} |
| 2410 |
|
| 2411 |
# @suffix suf list |
| 2412 |
# |
| 2413 |
# Takes a list and returns a new list with '$suf' appended |
| 2414 |
# to each element |
| 2415 |
# |
| 2416 |
## suffix .c {a b c} => {a.c b.c c.c} |
| 2417 |
# |
| 2418 |
proc suffix {suf list} { |
| 2419 |
set result {} |
| 2420 |
foreach p $list { |
| 2421 |
lappend result $p$suf |
| 2422 |
} |
| 2423 |
return $result |
| 2424 |
} |
| 2425 |
|
| 2426 |
# @prefix pre list |
| 2427 |
# |
| 2428 |
# Takes a list and returns a new list with '$pre' prepended |
| 2429 |
# to each element |
| 2430 |
# |
| 2431 |
## prefix jim- {a.c b.c} => {jim-a.c jim-b.c} |
| 2432 |
# |
| 2433 |
proc prefix {pre list} { |
| 2434 |
set result {} |
| 2435 |
foreach p $list { |
| 2436 |
lappend result $pre$p |
| 2437 |
} |
| 2438 |
return $result |
| 2439 |
} |
| 2440 |
|
| 2441 |
# @lpop list |
| 2442 |
# |
| 2443 |
# Removes the last entry from the given list and returns it. |
| 2444 |
proc lpop {listname} { |
| 2445 |
upvar $listname list |
| 2446 |
set val [lindex $list end] |
| 2447 |
set list [lrange $list 0 end-1] |
| 2448 |
return $val |
| 2449 |
} |
| 2450 |
} |
| 2451 |
|
| 2452 |
# ----- @module wiki-formatting.tcl ----- |
| 2453 |
|
| 2454 |
set modsource(wiki-formatting.tcl) { |
| 2455 |
# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/ |
| 2456 |
# All rights reserved |
| 2457 |
|
| 2458 |
# Module which provides text formatting |
| 2459 |
# wiki.tcl.tk format output |
| 2460 |
|
| 2461 |
use formatting |
| 2462 |
|
| 2463 |
proc joinlines {text} { |
| 2464 |
set lines {} |
| 2465 |
foreach l [split [string trim $text] \n] { |
| 2466 |
lappend lines [string trim $l] |
| 2467 |
} |
| 2468 |
join $lines |
| 2469 |
} |
| 2470 |
proc p {text} { |
| 2471 |
puts [joinlines $text] |
| 2472 |
puts "" |
| 2473 |
} |
| 2474 |
proc title {text} { |
| 2475 |
puts "*** [joinlines $text] ***" |
| 2476 |
puts "" |
| 2477 |
} |
| 2478 |
proc codelines {lines} { |
| 2479 |
puts "======" |
| 2480 |
foreach line $lines { |
| 2481 |
puts " $line" |
| 2482 |
} |
| 2483 |
puts "======" |
| 2484 |
} |
| 2485 |
proc code {text} { |
| 2486 |
puts "======" |
| 2487 |
foreach line [parse_code_block $text] { |
| 2488 |
puts " $line" |
| 2489 |
} |
| 2490 |
puts "======" |
| 2491 |
} |
| 2492 |
proc nl {} { |
| 2493 |
} |
| 2494 |
proc section {text} { |
| 2495 |
puts "'''$text'''" |
| 2496 |
puts "" |
| 2497 |
} |
| 2498 |
proc subsection {text} { |
| 2499 |
puts "''$text''" |
| 2500 |
puts "" |
| 2501 |
} |
| 2502 |
proc bullet {text} { |
| 2503 |
puts " * [joinlines $text]" |
| 2504 |
} |
| 2505 |
proc indent {text} { |
| 2506 |
puts " : [joinlines $text]" |
| 2507 |
} |
| 2508 |
proc defn {first args} { |
| 2509 |
if {$first ne ""} { |
| 2510 |
indent '''$first''' |
| 2511 |
} |
| 2512 |
|
| 2513 |
foreach p $args { |
| 2514 |
p $p |
| 2515 |
} |
| 2516 |
} |
| 2517 |
} |
| 2518 |
|
| 2519 |
|
| 2520 |
################################################################## |
| 2521 |
# |
| 2522 |
# Entry/Exit |
| 2523 |
# |
| 2524 |
if {$autosetup(debug)} { |
| 2525 |
main $argv |
| 2526 |
} |
| 2527 |
if {[catch {main $argv} msg opts] == 1} { |
| 2528 |
show-notices |
| 2529 |
autosetup-full-error [error-dump $msg $opts $autosetup(debug)] |
| 2530 |
if {!$autosetup(debug)} { |
| 2531 |
puts stderr "Try: '[file tail $autosetup(exe)] --debug' for a full stack trace" |
| 2532 |
} |
| 2533 |
exit 1 |
| 2534 |
} |