/
blitz_tools.py
390 lines (336 loc) · 13.3 KB
/
blitz_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# $Id$
# $Id$
#
# Copyright 2008 Glencoe Software, Inc. All rights reserved.
# Use is subject to license terms supplied in LICENSE.txt
#
import sys, os, glob, subprocess
import optparse
from SCons.Script.SConscript import *
from SCons.Script import AddOption, GetOption
from SCons.SConf import *
from SCons.Variables import *
#
# Global Directories
#
cwd = os.path.abspath( os.path.dirname( __file__ ) )
top = os.path.abspath( os.path.join( cwd, os.path.pardir, os.path.pardir ) )
blitz_resources = os.path.abspath( os.path.join( top, "components", "blitz", "resources") )
blitz_generated = os.path.abspath( os.path.join( top, "components", "blitz", "generated") )
tools_include = os.path.abspath( os.path.join( top, "components", "tools", "target", "include" ) )
tools_library = os.path.abspath( os.path.join( top, "components", "tools", "target", "lib" ) )
omerocpp_dir = os.path.abspath( os.path.join( top, "components", "tools", "OmeroCpp") )
header = os.path.join( blitz_resources, "header.txt" )
# Relative
resources = os.path.abspath("resources")
generated = os.path.abspath("generated")
# Support ICE_HOME
if os.environ.has_key("ICE_HOME"):
ice_home = os.path.abspath( os.environ["ICE_HOME"] )
print "Using env[ICE_HOME] = %s" % (ice_home)
else:
ice_home = None
if os.environ.has_key("SLICEPATH"):
slicepath = os.path.abspath( os.environ["SLICEPATH"] )
print "Using env[SLICEPATH] = %s" % (slicepath)
else:
slicepath = None
if os.environ.has_key("SLICE2JAVA"):
slice2java = os.environ["SLICE2JAVA"]
print "Using env[SLICE2JAVA] = %s" % (slice2java)
else:
slice2java = None
if os.environ.has_key("SLICE2PY"):
slice2py = os.environ["SLICE2PY"]
print "Using env[SLICE2PY] = %s" % (slice2py)
else:
slice2py = None
if os.environ.has_key("SLICE2CPP"):
slice2cpp = os.environ["SLICE2CPP"]
print "Using env[SLICE2CPP] = %s" % (slice2cpp)
else:
slice2cpp = None
def jdep(DEPMAP, target):
"""
Uses a map named "DEPMAP" to look map from Java file
names which don't exist ("omero/API.ice" -> "omero/API.java")
to real Globs ("omero/api/*.java")
"""
try:
return DEPMAP[target]
except KeyError:
return target
#
# Helpers
#
def common(dir = generated):
"""
Necessary since output for C++ does not include directories.
"""
return ["-I%s" % generated, "-I%s" % resources, "-I%s" % slicepath, "--output-dir=%s" % dir]
def names(dir, ice):
basename = os.path.basename(ice)[:-4]
filename = '%s/%s' % (dir, basename)
return (basename, filename)
def basenames(where, dir):
for ice in glob.glob("%s/%s/*.ice" % (where, dir)):
yield names(dir, ice)
#
# Define calls to slice2java, slice2cpp, and slice2py
#
def make_slice(command):
def slice(target, source, env):
args = command+[str(source[0].get_abspath())]
rv = subprocess.call(args)
if rv != 0:
raise Exception("%s returned %s" % (str(args), str(rv)) )
return slice
def slice_cpp(env, where, dir):
command = [slice2cpp, "--include-dir=%s"%dir] + common( "%s/%s" % (generated, dir) )
if sys.platform == "win32":
command.append("--dll-export")
command.append("OMERO_API")
actions = []
for basename, filename in basenames(where, dir):
c = env.Command(
[filename + '.h', filename + '.cpp'], # target
filename + '.ice', # source
make_slice(command), # command
chdir = where ) # dir
actions.append( c )
return actions
def slice_java(env, where, dir):
command = [slice2java, "--tie"] + common()
actions = []
for basename, filename in basenames(where, dir):
c = env.Command(
jdep(env["DEPMAP"], filename + '.java' ), # target
filename + '.ice', # source
make_slice(command), # command
chdir = where ) # dir
actions.append( c )
return actions
def slice_py(env, where, dir):
prefix = dir.replace("/","_") + "_"
command = [slice2py, "--prefix", "%s" % prefix ] + common()
actions = []
for basename, filename in basenames(where, dir):
c = env.Command(
[prefix + basename + '_ice.py' ], # target
filename + '.ice', # source
make_slice(command), # command
chdir = where ) # dir
actions.append( c )
return actions
#
# Lists which can be used in a cross-product to generate
# all necessary files.
#
methods = [slice_java, slice_cpp, slice_py]
directories = ["omero", "omero/model", "omero/api", "omero/cmd"]
where = [generated, resources]
class OmeroEnvironment(SConsEnvironment):
"""
Wrapper class around a scons environment for properly setting up the
build environment
"""
def __init__(self, **kwargs):
try:
tools = list(kwargs.pop("tools"))
tools.append('packaging')
except KeyError:
tools = ['default', 'packaging']
# omero_quiet is for internal use in order to
# quiet down this instance for re-use.
try:
self.omero_quiet = kwargs.pop("omero_quiet")
except KeyError:
self.omero_quiet = False
try:
AddOption('--release',
dest='release',
type='string',
nargs=1,
action='store',
metavar='RELEASE',
help='Release version [debug (default) or Os]')
AddOption('--arch',
dest='arch',
type='string',
nargs=1,
action='store',
metavar='ARCH',
help='Architecture to build for [x86, x64, or detect (default)]')
except optparse.OptionConflictError, e:
pass # These are global so this has been used twice
# Very odd error: using ENV = os.environ, rather than ENV = dict(os.environ)
# causes *sub*processes to receive a fresh environment with registry values
# for PATH, LIB, etc. *pre*pended to the variables.
SConsEnvironment.__init__(self, ENV = dict(os.environ), tools=tools, **kwargs)
self.Decider('MD5-timestamp')
# Print statements
if not os.environ.has_key("VERBOSE"):
self.Replace(CXXCOMSTR = "Compiling $TARGET")
self.Replace(LINKCOMSTR = "Linking $TARGET")
self.Replace(SHCXXCOMSTR = "Compiling $TARGET")
self.Replace(SHLINKCOMSTR = "Linking $TARGET")
# CXX
if os.environ.has_key("CXX"):
os_cxx = os.environ["CXX"]
if (os_cxx):
print "Overriding env[CXX] (%s) with os.env[CXX] (%s)" % (self["CXX"], os_cxx)
self.Replace(CXX = os_cxx)
# CXXFLAGS
self.AppendUnique(CPPDEFINES=["OMERO_API_EXPORTS","_REENTRANT"])
if self.isdebug():
self.AppendUnique(CPPDEFINES=["DEBUG"])
else:
self.AppendUnique(CPPDEFINES=["NDEBUG"])
if not self.iswin32():
self.Append(CXXFLAGS=self.Split("-Wall -ansi"))
self.Append(CXXFLAGS=self.Split("-Wno-long-long -Wnon-virtual-dtor"))
self.Append(CXXFLAGS=self.Split("-Wno-unused-parameter -Wno-unused-function -Wunused-variable -Wunused-value"))
# self.Append(CXXFLAGS=self.Split("-pedantic -ansi")) Ice fails pedantic due to extra ";"
# self.Append(CXXFLAGS=self.Split("-Wno-long-long -Wctor-dtor-privacy -Wnon-virtual-dtor")) Ice fails the ctor check.
# self.Append(CXXFLAGS=self.Split("-Werror")) http://lists.openmicroscopy.org.uk/pipermail/ome-devel/2010-February/001557.html
if self.isdebug():
self.Append(CXXFLAGS=self.Split("-O0 -g"))
else:
self.Append(CXXFLAGS=self.Split("-Os"))
else:
self.AppendUnique(CPPDEFINES=["WIN32_LEAN_AND_MEAN"])
if self["CC"] == "cl":
self.AppendUnique(CXXFLAGS=self.Split("/bigobj"))
self.AppendUnique(CXXFLAGS=self.Split("/EHsc"))
if self.isdebug():
self.Append(CXXFLAGS=["/Zi","/Od"])
self.AppendUnique(CXXFLAGS = ["/MDd"])
else:
self.Append(CXXFLAGS=["/Os"])
self.AppendUnique(CXXFLAGS = ["/MD"])
# Correcting for registry lookup under WoW64
# Though here the PATH adheres to the values set by
# vcvarsall etc., in subprocesses it revernts (via
# the registry?) to the default values.
self['LINK'] = self.which('link')
self['AR'] = self.which('lib')
self['CC'] = self.which('cl')
self['CXX'] = '$CC'
# Now CC has a non "cl" value. Can no longer use that check.
# Now let user override
if "CXXFLAGS" in os.environ:
self.Append(CXXFLAGS=self.Split(os.environ["CXXFLAGS"]))
#
# LINKFLAGS
#
if self.iswin32():
try:
verbosity = int(os.environ.get("VERBOSE",0))
if verbosity > 1:
# This is VERY verbose
self.AppendUnique(LINKFLAGS = ["/verbose"])
except ValueError:
pass
if self.is64bit():
self.Append(ARFLAGS = ['/MACHINE:X64'])
self.Append(LINKFLAGS = ['/MACHINE:X64'])
if self.is64bit():
self.Append(LINKFLAGS = ['/DEBUG'])
# Now let user override
if "LINKFLAGS" in os.environ:
self.Append(CXXFLAGS=self.Split(os.environ["LINKFLAGS"]))
#
# CPPPATH
#
self.AppendUnique(CPPPATH = [blitz_generated] )
if os.environ.has_key("CPPPATH"):
self.AppendUnique(CPPPATH=os.environ["CPPPATH"].split(os.path.pathsep))
if ice_home:
self.Append(CPPPATH = [os.path.join(ice_home, "include")] )
#
# LIBPATH
#
self.AppendUnique(LIBPATH=omerocpp_dir)
if os.environ.has_key("LIBPATH"):
self.AppendUnique(LIBPATH=os.environ["LIBPATH"].split(os.path.pathsep))
if self.iswin32():
if "LIB" in os.environ:
# Only LIB contains the path to the Windows SDK x64 library when starting
# from the VS2008 x64 command line batch.
self.AppendUnique(LIBPATH=os.environ["LIB"].split(os.path.pathsep))
def isdebug(self):
if hasattr(self, "_isdbg"):
return self._isdbg
RELEASE = GetOption("release")
if RELEASE == "Os":
self._isdbg = False
else:
self._isdbg = True
if RELEASE not in [None, "debug"]:
import warnings
warnings.warn("Unknown release value. Using 'debug'")
if not self.omero_quiet:
print "Debug setting: %s (%s)" % (self._isdbg, RELEASE)
return self._isdbg
def iswin32(self):
if hasattr(self, "_win32"):
return self._win32
self._win32 = self["PLATFORM"] == "win32"
return self._win32
def is64bit(self):
if hasattr(self, "_bit64"):
return self._bit64
ARCH = GetOption("arch")
if ARCH == "x64":
self._bit64 = True
elif ARCH == "x86":
self._bit64 = False
else:
if ARCH not in [None,"detect"]:
import warnings
warnings.warn("Unknown arch value. Using 'detect'")
if self.iswin32():
# Work around for 32bit Windows executables
try:
import win32process
self._bit64 = win32process.IsWow64Process()
except:
import ctypes, sys
i = ctypes.c_int()
kernel32 = ctypes.windll.kernel32
process = kernel32.GetCurrentProcess()
kernel32.IsWow64Process(process, ctypes.byref(i))
self._bit64 = (i.value != 0)
else:
import platform
self._bit64 = platform.architecture()[0] == "64bit"
if not self.omero_quiet:
print "64-Bit build: %s (%s)" % (self._bit64, ARCH)
return self._bit64
def icelibs(self):
if self.iswin32() and self.isdebug():
return ["Iced", "IceUtild", "Glacier2d"]
else:
return ["Ice", "IceUtil", "Glacier2"]
def which(self, exe):
from omero_ext import which
try:
rv = file = which.which(exe)
except which.WhichError:
rv = file = ""
if file:
if self.iswin32():
import win32api
try:
rv = win32api.GetShortPathName(file)
if not os.path.exists(rv):
rv = file
except:
pass
return rv
def fatal_error(self, message):
print "Fatal Error: %s" % message
self.Exit(1)