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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

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