Version 3 of PkgIndexUtil.tcl - determining a platform name

Updated 2003-10-02 11:42:58

Michael Lenaghan wrote to the starkit mailing list:

Like many other people here I've built a little system for automating Starpack/Starkit construction. Like many other people here I ran into the problem of loading platform-specific libraries. Since I wanted to do "cross-compiling" of Starpacks/Starkits, I also ran into the problem of knowing various extensions on different platforms.

I borrowed and modifed some things that I saw in other kits and on the wiki. In case it's helpful I'll post here my approach. (Note that some changes are subtle. For example, I changed the "Power Macintosh" glob to Power*, because according to another page on the wiki some PowerPC platforms report as "Power PC" and those should also get switched to "ppc".)

Finally: I did modify some of the stuff I saw, and someone here may spot something I did that doesn't make sense on platform X. Oh, in fact that's pretty much a given... :-)


 # File:      PkgIndexUtil.tcl
 # Author:    Michael Lenaghan
 # Copyright: Public Domain
 # Created:   2003/09/30

 # Based on an idea from the wiki @ http://mini.net/tcl/8522

 # Add this line to the top of a pkgIndex.tcl file to use this file:
 #
 #     source [file join [file dirname [info script]] "pkgIndexUtil.tcl"]
 #
 # Then add [platform osmachine] and [platform oslibextension] where
 # required in the load statements.

 proc platform {args} {
     if {[llength $args] > 0} {
         switch -- [lindex $args 0] {
             machine {
                 global tcl_platform
                 switch -glob -- $tcl_platform(machine) {
                     intel -
                     i*86* {
                         return x86
                     }
                     Power* {
                         return ppc
                     }
                     sun4* {
                         return sparc
                     }
                     9000* {
                         return 9000
                     }
                     default {
                         return [regsub -all {[ /]} \
                             [string tolower $tcl_platform(machine)] "-"]
                     }
                 }
             }
             os {
                 global tcl_platform
                 switch -glob -- [lindex $tcl_platform(os) 0] {
                     Win* {
                         return windows
                     }
                     Sun* {
                         return solaris
                     }
                     default {
                         return [regsub -all {[ /]} \
                             [string tolower $tcl_platform(os)] "-"]
                     }
                 }
             }
             osmachine {
                 return "[platform os]-[platform machine]"
             }
             osbinextension {
                 if {[llength $args] == 1} {
                     set os [platform os]
                 } else {
                     set os [lindex $args 1]
                 }
                 switch -glob -- $os {
                     win* {
                         return ".exe"
                     }
                     default {
                         return ".bin"
                     }
                 }
             }
             oslibextension {
                 if {[llength $args] == 1} {
                     set os [platform os]
                 } else {
                     set os [lindex $args 1]
                 }
                 switch -glob -- $os {
                     darwin* {
                         return ".dylib"
                     }
                     win* {
                         return ".dll"
                     }
                     default {
                         return ".so"
                     }
                 }
             }
             default {
                 error "bad option \"[lindex $args 0]\": must be machine, 
 os, osmachine, osbinextension, or oslibextension."
             }
         }
     } else {
         error "wrong # args: should be \"platform option ?arg arg ...?\""
     }
 }

Here's an example of use, from the first couple of lines in my XOTcl pkgIndex.tcl file:

 source [file join [file dirname [info script]] "pkgIndexUtil.tcl"]

 package ifneeded XOTcl 1.0 [list load [file join $dir [platform osmachine] libxotcl1.0[platform oslibextension]] XOTcl]


Category Command | Category Deployment | Category Porting