package.tcl 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. # package.tcl --
  2. #
  3. # utility procs formerly in init.tcl which can be loaded on demand
  4. # for package management.
  5. #
  6. # Copyright (c) 1991-1993 The Regents of the University of California.
  7. # Copyright (c) 1994-1998 Sun Microsystems, Inc.
  8. #
  9. # See the file "license.terms" for information on usage and redistribution
  10. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11. #
  12. namespace eval tcl::Pkg {}
  13. # ::tcl::Pkg::CompareExtension --
  14. #
  15. # Used internally by pkg_mkIndex to compare the extension of a file to a given
  16. # extension. On Windows, it uses a case-insensitive comparison because the
  17. # file system can be file insensitive.
  18. #
  19. # Arguments:
  20. # fileName name of a file whose extension is compared
  21. # ext (optional) The extension to compare against; you must
  22. # provide the starting dot.
  23. # Defaults to [info sharedlibextension]
  24. #
  25. # Results:
  26. # Returns 1 if the extension matches, 0 otherwise
  27. proc tcl::Pkg::CompareExtension {fileName {ext {}}} {
  28. global tcl_platform
  29. if {$ext eq ""} {set ext [info sharedlibextension]}
  30. if {$tcl_platform(platform) eq "windows"} {
  31. return [string equal -nocase [file extension $fileName] $ext]
  32. } else {
  33. # Some unices add trailing numbers after the .so, so
  34. # we could have something like '.so.1.2'.
  35. set root $fileName
  36. while {1} {
  37. set currExt [file extension $root]
  38. if {$currExt eq $ext} {
  39. return 1
  40. }
  41. # The current extension does not match; if it is not a numeric
  42. # value, quit, as we are only looking to ignore version number
  43. # extensions. Otherwise we might return 1 in this case:
  44. # tcl::Pkg::CompareExtension foo.so.bar .so
  45. # which should not match.
  46. if {![string is integer -strict [string range $currExt 1 end]]} {
  47. return 0
  48. }
  49. set root [file rootname $root]
  50. }
  51. }
  52. }
  53. # pkg_mkIndex --
  54. # This procedure creates a package index in a given directory. The package
  55. # index consists of a "pkgIndex.tcl" file whose contents are a Tcl script that
  56. # sets up package information with "package require" commands. The commands
  57. # describe all of the packages defined by the files given as arguments.
  58. #
  59. # Arguments:
  60. # -direct (optional) If this flag is present, the generated
  61. # code in pkgMkIndex.tcl will cause the package to be
  62. # loaded when "package require" is executed, rather
  63. # than lazily when the first reference to an exported
  64. # procedure in the package is made.
  65. # -verbose (optional) Verbose output; the name of each file that
  66. # was successfully rocessed is printed out. Additionally,
  67. # if processing of a file failed a message is printed.
  68. # -load pat (optional) Preload any packages whose names match
  69. # the pattern. Used to handle DLLs that depend on
  70. # other packages during their Init procedure.
  71. # dir - Name of the directory in which to create the index.
  72. # args - Any number of additional arguments, each giving
  73. # a glob pattern that matches the names of one or
  74. # more shared libraries or Tcl script files in
  75. # dir.
  76. proc pkg_mkIndex {args} {
  77. set usage {"pkg_mkIndex ?-direct? ?-lazy? ?-load pattern? ?-verbose? ?--? dir ?pattern ...?"}
  78. set argCount [llength $args]
  79. if {$argCount < 1} {
  80. return -code error "wrong # args: should be\n$usage"
  81. }
  82. set more ""
  83. set direct 1
  84. set doVerbose 0
  85. set loadPat ""
  86. for {set idx 0} {$idx < $argCount} {incr idx} {
  87. set flag [lindex $args $idx]
  88. switch -glob -- $flag {
  89. -- {
  90. # done with the flags
  91. incr idx
  92. break
  93. }
  94. -verbose {
  95. set doVerbose 1
  96. }
  97. -lazy {
  98. set direct 0
  99. append more " -lazy"
  100. }
  101. -direct {
  102. append more " -direct"
  103. }
  104. -load {
  105. incr idx
  106. set loadPat [lindex $args $idx]
  107. append more " -load $loadPat"
  108. }
  109. -* {
  110. return -code error "unknown flag $flag: should be\n$usage"
  111. }
  112. default {
  113. # done with the flags
  114. break
  115. }
  116. }
  117. }
  118. set dir [lindex $args $idx]
  119. set patternList [lrange $args [expr {$idx + 1}] end]
  120. if {![llength $patternList]} {
  121. set patternList [list "*.tcl" "*[info sharedlibextension]"]
  122. }
  123. try {
  124. set fileList [glob -directory $dir -tails -types {r f} -- \
  125. {*}$patternList]
  126. } on error {msg opt} {
  127. return -options $opt $msg
  128. }
  129. foreach file $fileList {
  130. # For each file, figure out what commands and packages it provides.
  131. # To do this, create a child interpreter, load the file into the
  132. # interpreter, and get a list of the new commands and packages that
  133. # are defined.
  134. if {$file eq "pkgIndex.tcl"} {
  135. continue
  136. }
  137. set c [interp create]
  138. # Load into the child any packages currently loaded in the parent
  139. # interpreter that match the -load pattern.
  140. if {$loadPat ne ""} {
  141. if {$doVerbose} {
  142. tclLog "currently loaded packages: '[info loaded]'"
  143. tclLog "trying to load all packages matching $loadPat"
  144. }
  145. if {![llength [info loaded]]} {
  146. tclLog "warning: no packages are currently loaded, nothing"
  147. tclLog "can possibly match '$loadPat'"
  148. }
  149. }
  150. foreach pkg [info loaded] {
  151. if {![string match -nocase $loadPat [lindex $pkg 1]]} {
  152. continue
  153. }
  154. if {$doVerbose} {
  155. tclLog "package [lindex $pkg 1] matches '$loadPat'"
  156. }
  157. try {
  158. load [lindex $pkg 0] [lindex $pkg 1] $c
  159. } on error err {
  160. if {$doVerbose} {
  161. tclLog "warning: load [lindex $pkg 0]\
  162. [lindex $pkg 1]\nfailed with: $err"
  163. }
  164. } on ok {} {
  165. if {$doVerbose} {
  166. tclLog "loaded [lindex $pkg 0] [lindex $pkg 1]"
  167. }
  168. }
  169. if {[lindex $pkg 1] eq "Tk"} {
  170. # Withdraw . if Tk was loaded, to avoid showing a window.
  171. $c eval [list wm withdraw .]
  172. }
  173. }
  174. $c eval {
  175. # Stub out the package command so packages can require other
  176. # packages.
  177. rename package __package_orig
  178. proc package {what args} {
  179. switch -- $what {
  180. require {
  181. return; # Ignore transitive requires
  182. }
  183. default {
  184. __package_orig $what {*}$args
  185. }
  186. }
  187. }
  188. proc tclPkgUnknown args {}
  189. package unknown tclPkgUnknown
  190. # Stub out the unknown command so package can call into each other
  191. # during their initialilzation.
  192. proc unknown {args} {}
  193. # Stub out the auto_import mechanism
  194. proc auto_import {args} {}
  195. # reserve the ::tcl namespace for support procs and temporary
  196. # variables. This might make it awkward to generate a
  197. # pkgIndex.tcl file for the ::tcl namespace.
  198. namespace eval ::tcl {
  199. variable dir ;# Current directory being processed
  200. variable file ;# Current file being processed
  201. variable direct ;# -direct flag value
  202. variable x ;# Loop variable
  203. variable debug ;# For debugging
  204. variable type ;# "load" or "source", for -direct
  205. variable namespaces ;# Existing namespaces (e.g., ::tcl)
  206. variable packages ;# Existing packages (e.g., Tcl)
  207. variable origCmds ;# Existing commands
  208. variable newCmds ;# Newly created commands
  209. variable newPkgs {} ;# Newly created packages
  210. }
  211. }
  212. $c eval [list set ::tcl::dir $dir]
  213. $c eval [list set ::tcl::file $file]
  214. $c eval [list set ::tcl::direct $direct]
  215. # Download needed procedures into the child because we've just deleted
  216. # the unknown procedure. This doesn't handle procedures with default
  217. # arguments.
  218. foreach p {::tcl::Pkg::CompareExtension} {
  219. $c eval [list namespace eval [namespace qualifiers $p] {}]
  220. $c eval [list proc $p [info args $p] [info body $p]]
  221. }
  222. try {
  223. $c eval {
  224. set ::tcl::debug "loading or sourcing"
  225. # we need to track command defined by each package even in the
  226. # -direct case, because they are needed internally by the
  227. # "partial pkgIndex.tcl" step above.
  228. proc ::tcl::GetAllNamespaces {{root ::}} {
  229. set list $root
  230. foreach ns [namespace children $root] {
  231. lappend list {*}[::tcl::GetAllNamespaces $ns]
  232. }
  233. return $list
  234. }
  235. # init the list of existing namespaces, packages, commands
  236. foreach ::tcl::x [::tcl::GetAllNamespaces] {
  237. set ::tcl::namespaces($::tcl::x) 1
  238. }
  239. foreach ::tcl::x [package names] {
  240. if {[package provide $::tcl::x] ne ""} {
  241. set ::tcl::packages($::tcl::x) 1
  242. }
  243. }
  244. set ::tcl::origCmds [info commands]
  245. # Try to load the file if it has the shared library extension,
  246. # otherwise source it. It's important not to try to load
  247. # files that aren't shared libraries, because on some systems
  248. # (like SunOS) the loader will abort the whole application
  249. # when it gets an error.
  250. if {[::tcl::Pkg::CompareExtension $::tcl::file [info sharedlibextension]]} {
  251. # The "file join ." command below is necessary. Without
  252. # it, if the file name has no \'s and we're on UNIX, the
  253. # load command will invoke the LD_LIBRARY_PATH search
  254. # mechanism, which could cause the wrong file to be used.
  255. set ::tcl::debug loading
  256. load [file join $::tcl::dir $::tcl::file]
  257. set ::tcl::type load
  258. } else {
  259. set ::tcl::debug sourcing
  260. source [file join $::tcl::dir $::tcl::file]
  261. set ::tcl::type source
  262. }
  263. # As a performance optimization, if we are creating direct
  264. # load packages, don't bother figuring out the set of commands
  265. # created by the new packages. We only need that list for
  266. # setting up the autoloading used in the non-direct case.
  267. if {!$::tcl::direct} {
  268. # See what new namespaces appeared, and import commands
  269. # from them. Only exported commands go into the index.
  270. foreach ::tcl::x [::tcl::GetAllNamespaces] {
  271. if {![info exists ::tcl::namespaces($::tcl::x)]} {
  272. namespace import -force ${::tcl::x}::*
  273. }
  274. # Figure out what commands appeared
  275. foreach ::tcl::x [info commands] {
  276. set ::tcl::newCmds($::tcl::x) 1
  277. }
  278. foreach ::tcl::x $::tcl::origCmds {
  279. unset -nocomplain ::tcl::newCmds($::tcl::x)
  280. }
  281. foreach ::tcl::x [array names ::tcl::newCmds] {
  282. # determine which namespace a command comes from
  283. set ::tcl::abs [namespace origin $::tcl::x]
  284. # special case so that global names have no
  285. # leading ::, this is required by the unknown
  286. # command
  287. set ::tcl::abs \
  288. [lindex [auto_qualify $::tcl::abs ::] 0]
  289. if {$::tcl::x ne $::tcl::abs} {
  290. # Name changed during qualification
  291. set ::tcl::newCmds($::tcl::abs) 1
  292. unset ::tcl::newCmds($::tcl::x)
  293. }
  294. }
  295. }
  296. }
  297. # Look through the packages that appeared, and if there is a
  298. # version provided, then record it
  299. foreach ::tcl::x [package names] {
  300. if {[package provide $::tcl::x] ne ""
  301. && ![info exists ::tcl::packages($::tcl::x)]} {
  302. lappend ::tcl::newPkgs \
  303. [list $::tcl::x [package provide $::tcl::x]]
  304. }
  305. }
  306. }
  307. } on error msg {
  308. set what [$c eval set ::tcl::debug]
  309. if {$doVerbose} {
  310. tclLog "warning: error while $what $file: $msg"
  311. }
  312. } on ok {} {
  313. set what [$c eval set ::tcl::debug]
  314. if {$doVerbose} {
  315. tclLog "successful $what of $file"
  316. }
  317. set type [$c eval set ::tcl::type]
  318. set cmds [lsort [$c eval array names ::tcl::newCmds]]
  319. set pkgs [$c eval set ::tcl::newPkgs]
  320. if {$doVerbose} {
  321. if {!$direct} {
  322. tclLog "commands provided were $cmds"
  323. }
  324. tclLog "packages provided were $pkgs"
  325. }
  326. if {[llength $pkgs] > 1} {
  327. tclLog "warning: \"$file\" provides more than one package ($pkgs)"
  328. }
  329. foreach pkg $pkgs {
  330. # cmds is empty/not used in the direct case
  331. lappend files($pkg) [list $file $type $cmds]
  332. }
  333. if {$doVerbose} {
  334. tclLog "processed $file"
  335. }
  336. }
  337. interp delete $c
  338. }
  339. append index "# Tcl package index file, version 1.1\n"
  340. append index "# This file is generated by the \"pkg_mkIndex$more\" command\n"
  341. append index "# and sourced either when an application starts up or\n"
  342. append index "# by a \"package unknown\" script. It invokes the\n"
  343. append index "# \"package ifneeded\" command to set up package-related\n"
  344. append index "# information so that packages will be loaded automatically\n"
  345. append index "# in response to \"package require\" commands. When this\n"
  346. append index "# script is sourced, the variable \$dir must contain the\n"
  347. append index "# full path name of this file's directory.\n"
  348. foreach pkg [lsort [array names files]] {
  349. set cmd {}
  350. lassign $pkg name version
  351. lappend cmd ::tcl::Pkg::Create -name $name -version $version
  352. foreach spec [lsort -index 0 $files($pkg)] {
  353. foreach {file type procs} $spec {
  354. if {$direct} {
  355. set procs {}
  356. }
  357. lappend cmd "-$type" [list $file $procs]
  358. }
  359. }
  360. append index "\n[eval $cmd]"
  361. }
  362. set f [open [file join $dir pkgIndex.tcl] w]
  363. puts $f $index
  364. close $f
  365. }
  366. # tclPkgSetup --
  367. # This is a utility procedure use by pkgIndex.tcl files. It is invoked as
  368. # part of a "package ifneeded" script. It calls "package provide" to indicate
  369. # that a package is available, then sets entries in the auto_index array so
  370. # that the package's files will be auto-loaded when the commands are used.
  371. #
  372. # Arguments:
  373. # dir - Directory containing all the files for this package.
  374. # pkg - Name of the package (no version number).
  375. # version - Version number for the package, such as 2.1.3.
  376. # files - List of files that constitute the package. Each
  377. # element is a sub-list with three elements. The first
  378. # is the name of a file relative to $dir, the second is
  379. # "load" or "source", indicating whether the file is a
  380. # loadable binary or a script to source, and the third
  381. # is a list of commands defined by this file.
  382. proc tclPkgSetup {dir pkg version files} {
  383. global auto_index
  384. package provide $pkg $version
  385. foreach fileInfo $files {
  386. set f [lindex $fileInfo 0]
  387. set type [lindex $fileInfo 1]
  388. foreach cmd [lindex $fileInfo 2] {
  389. if {$type eq "load"} {
  390. set auto_index($cmd) [list load [file join $dir $f] $pkg]
  391. } else {
  392. set auto_index($cmd) [list source [file join $dir $f]]
  393. }
  394. }
  395. }
  396. }
  397. # tclPkgUnknown --
  398. # This procedure provides the default for the "package unknown" function. It
  399. # is invoked when a package that's needed can't be found. It scans the
  400. # auto_path directories and their immediate children looking for pkgIndex.tcl
  401. # files and sources any such files that are found to setup the package
  402. # database. As it searches, it will recognize changes to the auto_path and
  403. # scan any new directories.
  404. #
  405. # Arguments:
  406. # name - Name of desired package. Not used.
  407. # version - Version of desired package. Not used.
  408. # exact - Either "-exact" or omitted. Not used.
  409. proc tclPkgUnknown {name args} {
  410. global auto_path env
  411. if {![info exists auto_path]} {
  412. return
  413. }
  414. # Cache the auto_path, because it may change while we run through the
  415. # first set of pkgIndex.tcl files
  416. set old_path [set use_path $auto_path]
  417. while {[llength $use_path]} {
  418. set dir [lindex $use_path end]
  419. # Make sure we only scan each directory one time.
  420. if {[info exists tclSeenPath($dir)]} {
  421. set use_path [lrange $use_path 0 end-1]
  422. continue
  423. }
  424. set tclSeenPath($dir) 1
  425. # Get the pkgIndex.tcl files in subdirectories of auto_path directories.
  426. # - Safe Base interpreters have a restricted "glob" command that
  427. # works in this case.
  428. # - The "catch" was essential when there was no safe glob and every
  429. # call in a safe interp failed; it is retained only for corner
  430. # cases in which the eventual call to glob returns an error.
  431. catch {
  432. foreach file [glob -directory $dir -join -nocomplain \
  433. * pkgIndex.tcl] {
  434. set dir [file dirname $file]
  435. if {![info exists procdDirs($dir)]} {
  436. try {
  437. source $file
  438. } trap {POSIX EACCES} {} {
  439. # $file was not readable; silently ignore
  440. continue
  441. } on error msg {
  442. tclLog "error reading package index file $file: $msg"
  443. } on ok {} {
  444. set procdDirs($dir) 1
  445. }
  446. }
  447. }
  448. }
  449. set dir [lindex $use_path end]
  450. if {![info exists procdDirs($dir)]} {
  451. set file [file join $dir pkgIndex.tcl]
  452. # safe interps usually don't have "file exists",
  453. if {([interp issafe] || [file exists $file])} {
  454. try {
  455. source $file
  456. } trap {POSIX EACCES} {} {
  457. # $file was not readable; silently ignore
  458. continue
  459. } on error msg {
  460. tclLog "error reading package index file $file: $msg"
  461. } on ok {} {
  462. set procdDirs($dir) 1
  463. }
  464. }
  465. }
  466. set use_path [lrange $use_path 0 end-1]
  467. # Check whether any of the index scripts we [source]d above set a new
  468. # value for $::auto_path. If so, then find any new directories on the
  469. # $::auto_path, and lappend them to the $use_path we are working from.
  470. # This gives index scripts the (arguably unwise) power to expand the
  471. # index script search path while the search is in progress.
  472. set index 0
  473. if {[llength $old_path] == [llength $auto_path]} {
  474. foreach dir $auto_path old $old_path {
  475. if {$dir ne $old} {
  476. # This entry in $::auto_path has changed.
  477. break
  478. }
  479. incr index
  480. }
  481. }
  482. # $index now points to the first element of $auto_path that has
  483. # changed, or the beginning if $auto_path has changed length Scan the
  484. # new elements of $auto_path for directories to add to $use_path.
  485. # Don't add directories we've already seen, or ones already on the
  486. # $use_path.
  487. foreach dir [lrange $auto_path $index end] {
  488. if {![info exists tclSeenPath($dir)] && ($dir ni $use_path)} {
  489. lappend use_path $dir
  490. }
  491. }
  492. set old_path $auto_path
  493. }
  494. }
  495. # tcl::MacOSXPkgUnknown --
  496. # This procedure extends the "package unknown" function for MacOSX. It scans
  497. # the Resources/Scripts directories of the immediate children of the auto_path
  498. # directories for pkgIndex files.
  499. #
  500. # Arguments:
  501. # original - original [package unknown] procedure
  502. # name - Name of desired package. Not used.
  503. # version - Version of desired package. Not used.
  504. # exact - Either "-exact" or omitted. Not used.
  505. proc tcl::MacOSXPkgUnknown {original name args} {
  506. # First do the cross-platform default search
  507. uplevel 1 $original [linsert $args 0 $name]
  508. # Now do MacOSX specific searching
  509. global auto_path
  510. if {![info exists auto_path]} {
  511. return
  512. }
  513. # Cache the auto_path, because it may change while we run through the
  514. # first set of pkgIndex.tcl files
  515. set old_path [set use_path $auto_path]
  516. while {[llength $use_path]} {
  517. set dir [lindex $use_path end]
  518. # Make sure we only scan each directory one time.
  519. if {[info exists tclSeenPath($dir)]} {
  520. set use_path [lrange $use_path 0 end-1]
  521. continue
  522. }
  523. set tclSeenPath($dir) 1
  524. # get the pkgIndex files out of the subdirectories
  525. # Safe interpreters do not use tcl::MacOSXPkgUnknown - see init.tcl.
  526. foreach file [glob -directory $dir -join -nocomplain \
  527. * Resources Scripts pkgIndex.tcl] {
  528. set dir [file dirname $file]
  529. if {![info exists procdDirs($dir)]} {
  530. try {
  531. source $file
  532. } trap {POSIX EACCES} {} {
  533. # $file was not readable; silently ignore
  534. continue
  535. } on error msg {
  536. tclLog "error reading package index file $file: $msg"
  537. } on ok {} {
  538. set procdDirs($dir) 1
  539. }
  540. }
  541. }
  542. set use_path [lrange $use_path 0 end-1]
  543. # Check whether any of the index scripts we [source]d above set a new
  544. # value for $::auto_path. If so, then find any new directories on the
  545. # $::auto_path, and lappend them to the $use_path we are working from.
  546. # This gives index scripts the (arguably unwise) power to expand the
  547. # index script search path while the search is in progress.
  548. set index 0
  549. if {[llength $old_path] == [llength $auto_path]} {
  550. foreach dir $auto_path old $old_path {
  551. if {$dir ne $old} {
  552. # This entry in $::auto_path has changed.
  553. break
  554. }
  555. incr index
  556. }
  557. }
  558. # $index now points to the first element of $auto_path that has
  559. # changed, or the beginning if $auto_path has changed length Scan the
  560. # new elements of $auto_path for directories to add to $use_path.
  561. # Don't add directories we've already seen, or ones already on the
  562. # $use_path.
  563. foreach dir [lrange $auto_path $index end] {
  564. if {![info exists tclSeenPath($dir)] && ($dir ni $use_path)} {
  565. lappend use_path $dir
  566. }
  567. }
  568. set old_path $auto_path
  569. }
  570. }
  571. # ::tcl::Pkg::Create --
  572. #
  573. # Given a package specification generate a "package ifneeded" statement
  574. # for the package, suitable for inclusion in a pkgIndex.tcl file.
  575. #
  576. # Arguments:
  577. # args arguments used by the Create function:
  578. # -name packageName
  579. # -version packageVersion
  580. # -load {filename ?{procs}?}
  581. # ...
  582. # -source {filename ?{procs}?}
  583. # ...
  584. #
  585. # Any number of -load and -source parameters may be
  586. # specified, so long as there is at least one -load or
  587. # -source parameter. If the procs component of a module
  588. # specifier is left off, that module will be set up for
  589. # direct loading; otherwise, it will be set up for lazy
  590. # loading. If both -source and -load are specified, the
  591. # -load'ed files will be loaded first, followed by the
  592. # -source'd files.
  593. #
  594. # Results:
  595. # An appropriate "package ifneeded" statement for the package.
  596. proc ::tcl::Pkg::Create {args} {
  597. append err(usage) "[lindex [info level 0] 0] "
  598. append err(usage) "-name packageName -version packageVersion"
  599. append err(usage) "?-load {filename ?{procs}?}? ... "
  600. append err(usage) "?-source {filename ?{procs}?}? ..."
  601. set err(wrongNumArgs) "wrong # args: should be \"$err(usage)\""
  602. set err(valueMissing) "value for \"%s\" missing: should be \"$err(usage)\""
  603. set err(unknownOpt) "unknown option \"%s\": should be \"$err(usage)\""
  604. set err(noLoadOrSource) "at least one of -load and -source must be given"
  605. # process arguments
  606. set len [llength $args]
  607. if {$len < 6} {
  608. error $err(wrongNumArgs)
  609. }
  610. # Initialize parameters
  611. array set opts {-name {} -version {} -source {} -load {}}
  612. # process parameters
  613. for {set i 0} {$i < $len} {incr i} {
  614. set flag [lindex $args $i]
  615. incr i
  616. switch -glob -- $flag {
  617. "-name" -
  618. "-version" {
  619. if {$i >= $len} {
  620. error [format $err(valueMissing) $flag]
  621. }
  622. set opts($flag) [lindex $args $i]
  623. }
  624. "-source" -
  625. "-load" {
  626. if {$i >= $len} {
  627. error [format $err(valueMissing) $flag]
  628. }
  629. lappend opts($flag) [lindex $args $i]
  630. }
  631. default {
  632. error [format $err(unknownOpt) [lindex $args $i]]
  633. }
  634. }
  635. }
  636. # Validate the parameters
  637. if {![llength $opts(-name)]} {
  638. error [format $err(valueMissing) "-name"]
  639. }
  640. if {![llength $opts(-version)]} {
  641. error [format $err(valueMissing) "-version"]
  642. }
  643. if {!([llength $opts(-source)] || [llength $opts(-load)])} {
  644. error $err(noLoadOrSource)
  645. }
  646. # OK, now everything is good. Generate the package ifneeded statment.
  647. set cmdline "package ifneeded $opts(-name) $opts(-version) "
  648. set cmdList {}
  649. set lazyFileList {}
  650. # Handle -load and -source specs
  651. foreach key {load source} {
  652. foreach filespec $opts(-$key) {
  653. lassign $filespec filename proclist
  654. if { [llength $proclist] == 0 } {
  655. set cmd "\[list $key \[file join \$dir [list $filename]\]\]"
  656. lappend cmdList $cmd
  657. } else {
  658. lappend lazyFileList [list $filename $key $proclist]
  659. }
  660. }
  661. }
  662. if {[llength $lazyFileList]} {
  663. lappend cmdList "\[list tclPkgSetup \$dir $opts(-name)\
  664. $opts(-version) [list $lazyFileList]\]"
  665. }
  666. append cmdline [join $cmdList "\\n"]
  667. return $cmdline
  668. }
  669. interp alias {} ::pkg::create {} ::tcl::Pkg::Create