/[ascend]/trunk/scons/tcl-config.py
ViewVC logotype

Annotation of /trunk/scons/tcl-config.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2437 - (hide annotations) (download) (as text)
Sun Mar 27 17:10:19 2011 UTC (11 years, 4 months ago) by ballan
File MIME type: text/x-python
File size: 4926 byte(s)
fixed exception handling
1 jpye 1946 #! /usr/bin/env python
2    
3 jpye 1948 tclconfigfile = None
4 jpye 1946
5 jpye 1953 import sys, os.path
6     if sys.platform.startswith("win"):
7 jpye 1948 # check for ActiveState Tcl in Windows registry
8     try:
9     import _winreg
10     x=_winreg.ConnectRegistry(None,_winreg.HKEY_LOCAL_MACHINE)
11     y= _winreg.OpenKey(x,r"SOFTWARE\ActiveState\ActiveTcl")
12     _regversion,t = _winreg.QueryValueEx(y,"CurrentVersion")
13     z= _winreg.OpenKey(x,r"SOFTWARE\ActiveState\ActiveTcl\%s" % str(_regversion))
14     _regpath,t = _winreg.QueryValueEx(z,None)
15     _winreg.CloseKey(y)
16     _winreg.CloseKey(z)
17     _winreg.CloseKey(x)
18     # typically, c:\Tcl\lib\tclConfig.sh.
19     _regconfig = os.path.join(_regpath,os.path.join("lib","tclConfig.sh"))
20     if os.path.exists(_regconfig):
21     # if the file exists, good...
22     tclconfigfile = _regconfig
23     except:
24     pass
25 jpye 1946
26 jpye 1948 if tclconfigfile is None:
27     # use a 'tclsh' script to find location of tclConfig.sh
28 ballan 2437 # location of tclConfig.sh is canonical tcl_pkgPath
29 jpye 1948 tclscript = """
30 ballan 2437 #puts stderr "searching for tclConfig.sh ..."
31 jpye 1948 foreach d [concat \
32 ballan 2437 [concat $tcl_library $tcl_pkgPath ] \
33 jpye 1948 $auto_path \
34     [list [file dirname $tcl_library] \
35     [file dirname [lindex $tcl_pkgPath 0]] \
36     [file dirname [file dirname $tcl_library]] \
37     [file dirname [file dirname [lindex $tcl_pkgPath 0]]] \
38     [file dirname [file dirname [file dirname $tcl_library]]] \
39     [file dirname [file dirname [file dirname [lindex $tcl_pkgPath 0]]]]] \
40     ] {
41     if {[file exists [file join $d tclConfig.sh]]} {
42     puts "[file join $d tclConfig.sh]"
43 ballan 2437 #puts stderr "found $d : [file join $d tclConfig.sh]"
44     exit 1
45     } else {
46     #puts stderr "not in $d"
47 jpye 1948 }
48     }
49     """
50 jpye 1946
51 jpye 1948 import subprocess
52     output = subprocess.Popen(["tclsh"], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=tclscript)[0]
53 ballan 2437 # print output
54     # print output.strip()
55     # tcl will not return it if not there; already checked
56 jpye 1950 if os.path.exists(output.strip()):
57 ballan 2437 # print "path exists"
58 jpye 1948 # only report the file if it actually exists...
59     tclconfigfile = output.strip()
60 ballan 2437 # print tclconfigfile
61 jpye 1948
62 ballan 2437 # print tclconfigfile
63 jpye 1948 if tclconfigfile is None:
64 ballan 2437 print >> sys.stderr, "Unable to locate tclConfig.sh."
65     print >> sys.stderr, "If on Linux/Unix and you have tcl & tk installed, check that tclsh is in your path."
66     print >> sys.stderr, "If tclsh is in your path and this still fails, check that tcl-devel or equivalent package is installed."
67     print >> sys.stderr, "If your vendor doesn't provide tcl-devel and tk-devel equivalents, get free ActiveTcl from activestate.com."
68 jpye 1948 sys.exit(1)
69    
70 jpye 1946 # parse the file to determine the names of the variables it contains
71    
72     f = file(tclconfigfile)
73    
74 jpye 1947 # default build environment variables that are assumed to already be defined
75     # note that these are normally given default values in GNU Make, but we're
76     # not inside make, so we might not have these.
77     # FIXME: could try importing these from the environment?
78 jpye 1946 d = {
79 jpye 1949 "CC":"gcc"
80     ,"CFLAGS":""
81     ,"LDFLAGS":""
82     ,"AR":""
83     ,"LIBS":""
84     ,"VERSION":""
85     ,"DBGX":""
86     ,"NODOT_VERSION":"" # required for ActiveState Tcl 8.4 on Windows
87 jpye 1946 }
88    
89 jpye 1947 # regular expression used for variable substitution
90 jpye 1946 import re
91     r = re.compile(r'\$\{([A-Z_0-9]+)\}')
92    
93 jpye 1947 # variable substitution/expansion function
94 jpye 1946 def expand(s,d):
95     m = r.search(s)
96 ballan 2437 # print "MATCHING",s
97 jpye 1946 if m:
98 ballan 2437 # print "MATCH!"
99 jpye 1946 if d.has_key(m.group(1)):
100     return expand(s[:m.start()] + d[m.group(1)] + s[m.end():],d)
101     else:
102     raise RuntimeError("Missing variable '%s'" % m.group(1))
103     return s
104    
105     for l in f:
106     ls = l.strip()
107     if ls == "" or ls[0]=="#":
108     continue
109     k,v = ls.split("=",1)
110     if len(v) >= 2 and v[0] == "'" and v[len(v)-1] == "'":
111     v = v[1:len(v)-1]
112    
113 ballan 2437 try:
114     d[k] = expand(v,d)
115     except RuntimeError, err:
116     # print str(err),"probably unneeded"
117     pass
118 jpye 1946
119     # output the variable that the user requests
120    
121     import getopt, sys
122    
123 jpye 1947 def usage(progname):
124     print "%s [--cflags] [--libs] [--var=TCL_VAR_NAME] [--vars]" % progname
125     print "Output configuration variables for the Tcl script interpreter."
126     print "Options:"
127     print "\t--cflags Compiler flags for C code that uses Tcl"
128     print "\t--libs Linker flags for code that uses Tcl"
129     print "\t--vars List all variables defined in tclConfig.sh"
130     print "\t--var=VARNAME Output the value of a specific variable"
131     print "\nSee http://ascendwiki.cheme.cmu.edu/Tcl-config for more info."
132 jpye 1946
133     try:
134 jpye 1947 opts, args = getopt.getopt(sys.argv[1:], "h",
135 ballan 2437 ["help", "cflags", "libs", "var=","vars"]
136 jpye 1947 )
137 jpye 1946 except getopt.GetoptError, err:
138     print str(err)
139 jpye 1947 usage(sys.argv[0])
140 jpye 1946 sys.exit(2)
141    
142     for o, a in opts:
143     if o == "-h" or o == "--help":
144 jpye 1947 usage(sys.argv[0])
145 jpye 1946 sys.exit()
146     elif o == "--cflags":
147     print d['TCL_INCLUDE_SPEC']
148     elif o == "--libs":
149     print d['TCL_LIB_SPEC']
150 jpye 1947 elif o == "--var":
151     if d.has_key(a):
152     print d[a]
153     else:
154     raise RuntimeError("Unknown variable '%s'" % a)
155     elif o == "--vars":
156 jpye 1949 for k in sorted(d.keys()):
157 jpye 1947 print k
158 jpye 1946 else:
159     assert False, "unhandled option"
160    
161    
162    

Properties

Name Value
svn:executable *

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22