-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscbuilder
executable file
·607 lines (531 loc) · 20.3 KB
/
scbuilder
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#!/usr/bin/env ruby
#
# File: scbuilder
# Contents: Build script for SuperCollider plugins
# Authors: Stefan Kersten <sk AT k-hornz DOT de>
# nescivi AT gmail DOT com
#
# This is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2, or (at your option) any later version.
#
# This software is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., 675
# Mass Ave, Cambridge, MA 02139, USA.
#
# ======================================================================
# Usage:
#
# This script compiles `simple' SuperCollider plugins, i.e. plugins that
# consist of only one source file. Source files may be C++ implementations or
# a Faust[1] DSP specification.
#
# Source file paths are passed either in a file (specified with the
# PLUGIN_SOURCES option) or via standard input.
#
# E.g., when source file paths are in a file called sources.txt:
#
# $ sc-build-plugins PLUGIN_SOURCES=sources.txt
#
# Alternatively, source file paths can be passed via standard input:
#
# $ my-command | sc-build-plugins
#
# ======================================================================
# ======================================================================
# Bootstrap (ruby): Execute scons with content after __END__
require 'tempfile'
Tempfile.open("r") { |f|
f << DATA.read
f.flush
system("scons", "-f", f.path, *ARGV)
}
__END__
# ======================================================================
# NOTE: Please use an indentation level of 4 spaces, i.e. no mixing of
# tabs and spaces.
# ======================================================================
# ======================================================================
# setup
# ======================================================================
EnsureSConsVersion(0,96)
EnsurePythonVersion(2,3)
SConsignFile()
# ======================================================================
# imports
# ======================================================================
import glob
import os
import re
import sys
import subprocess
import types
import tarfile
# ======================================================================
# constants
# ======================================================================
PACKAGE = 'SuperCollider'
def short_cpu_name(cpu):
if cpu == 'Power Macintosh':
cpu = 'ppc'
return cpu.lower()
PLATFORM = os.uname()[0].lower()
CPU = short_cpu_name(os.uname()[4])
if PLATFORM == 'darwin':
PLATFORM_SYMBOL = 'SC_DARWIN'
PLUGIN_EXT = '.scx'
DEFAULT_PREFIX = '/'
elif PLATFORM == 'freebsd':
PLATFORM_SYMBOL = 'SC_FREEBSD'
PLUGIN_EXT = '.so'
DEFAULT_PREFIX = '/usr/local'
elif PLATFORM == 'linux':
PLATFORM_SYMBOL = 'SC_LINUX'
PLUGIN_EXT = '.so'
DEFAULT_PREFIX = '/usr/local'
elif PLATFORM == 'windows':
PLATFORM_SYMBOL = 'SC_WIN32'
PLUGIN_EXT = '.scx'
DEFAULT_PREFIX = '/'
else:
print 'Unknown platform: %s' % PLATFORM
Exit(1)
if CPU == 'ppc':
DEFAULT_OPT_ARCH = '7450'
elif CPU in [ 'i586', 'i686' ]:
# FIXME: better detection
DEFAULT_OPT_ARCH = CPU
else:
DEFAULT_OPT_ARCH = None
# ======================================================================
# util
# ======================================================================
def make_os_env(*keys):
env = os.environ
res = {}
for key in keys:
if env.has_key(key):
res[key] = env[key]
return res
def CheckPKGConfig(context, version):
context.Message( 'Checking for pkg-config... ' )
ret = context.TryAction('pkg-config --atleast-pkgconfig-version=%s' % version)[0]
context.Result( ret )
return ret
def CheckPKG(context, name):
context.Message('Checking for %s... ' % name)
ret = context.TryAction('pkg-config --exists \'%s\'' % name)[0]
res = None
if ret:
res = Environment(ENV = make_os_env('PATH', 'PKG_CONFIG_PATH'))
res.ParseConfig('pkg-config --cflags --libs \'%s\'' % name)
res['PKGCONFIG'] = name
context.Result(ret)
return (ret, res)
def get_new_pkg_env():
return Environment(ENV = make_os_env('PATH', 'PKG_CONFIG_PATH'))
def merge_lib_info(env, *others):
for other in others:
env.AppendUnique(CCFLAGS = other.get('CCFLAGS', []))
env.AppendUnique(CPPDEFINES = other.get('CPPDEFINES', []))
env.AppendUnique(CPPPATH = other.get('CPPPATH', []))
env.AppendUnique(CXXFLAGS = other.get('CXXFLAGS', []))
env.AppendUnique(LIBS = other.get('LIBS', []))
env.AppendUnique(LIBPATH = other.get('LIBPATH', []))
env['LINKFLAGS'] = env['LINKFLAGS'] + other.get('LINKFLAGS', "")
def flatten_dir(dir):
res = []
for root, dirs, files in os.walk(dir):
if 'CVS' in dirs: dirs.remove('CVS')
if '.svn' in dirs: dirs.remove('.svn')
for f in files:
res.append(os.path.join(root, f))
return res
def install_dir(env, src_dir, dst_dir, filter_re, strip_levels=0):
nodes = []
for root, dirs, files in os.walk(src_dir):
src_paths = []
dst_paths = []
if 'CVS' in dirs: dirs.remove('CVS')
if '.svn' in dirs: dirs.remove('.svn')
for d in dirs[:]:
if filter_re.match(d):
src_paths += flatten_dir(os.path.join(root, d))
dirs.remove(d)
for f in files:
if filter_re.match(f):
src_paths.append(os.path.join(root, f))
dst_paths += map(
lambda f:
os.path.join(
dst_dir,
*f.split(os.path.sep)[strip_levels:]),
src_paths)
nodes += env.InstallAs(dst_paths, src_paths)
return nodes
def is_installing():
pat = re.compile('^install.*$')
for x in COMMAND_LINE_TARGETS:
if pat.match(x): return True
return False
def bin_dir(prefix):
return os.path.join(prefix, 'bin')
def lib_dir(prefix):
return os.path.join(prefix, 'lib')
def pkg_data_dir(prefix, *args):
if PLATFORM == 'darwin':
base = os.path.join(prefix, 'Library/Application Support')
else:
base = os.path.join(prefix, 'share')
return os.path.join(base, PACKAGE, *args)
def pkg_doc_dir(prefix, *args):
if PLATFORM == 'darwin':
base = os.path.join(prefix, 'Library/Documentation')
else:
base = os.path.join(prefix, 'share', 'doc')
return os.path.join(base, PACKAGE, *args)
def pkg_include_dir(prefix, *args):
return os.path.join(prefix, 'include', PACKAGE, *args)
def pkg_lib_dir(prefix, *args):
return os.path.join(lib_dir(prefix), PACKAGE, *args)
def pkg_classlib_dir(prefix, *args):
return pkg_data_dir(prefix, 'SCClassLibrary', *args)
def pkg_extension_dir(prefix, *args):
return pkg_data_dir(prefix, 'Extensions', *args)
def make_opt_flags(env):
flags = [
"-O3",
## "-fomit-frame-pointer", # can behave strangely for sclang
"-ffast-math",
"-fstrength-reduce"
]
arch = env.get('OPT_ARCH')
if arch:
if CPU == 'ppc':
flags.extend([ "-mcpu=%s" % (arch,) ])
else:
flags.extend([ "-march=%s" % (arch,) ])
if CPU == 'ppc':
flags.extend([ "-fsigned-char", "-mhard-float",
## "-mpowerpc-gpopt", # crashes sqrt
"-mpowerpc-gfxopt"
])
return flags
# ======================================================================
# Faust builder
# ======================================================================
def faustInitEnvironment(env):
dsp = Builder(
action = 'faust -a supercollider.cpp -o $TARGET $SOURCE',
suffix = '.cpp',
src_suffix = '.dsp')
xml = Builder(
action = ['faust -o /dev/null -xml $SOURCE', Move('$TARGET', '${SOURCE}.xml')],
suffix = '.dsp.xml',
src_suffix = '.dsp')
sc = Builder(
action = '$FAUST2SC --prefix="$FAUST2SC_PREFIX" -o $TARGET $SOURCE',
suffix = '.sc',
src_suffix = '.dsp.xml')
env.Append(BUILDERS = { 'Faust' : dsp,
'FaustXML' : xml,
'FaustSC' : sc })
# ======================================================================
# Command line options
# ======================================================================
opts = Options('scache.conf', ARGUMENTS)
opts.AddOptions(
BoolOption('ALTIVEC',
'Build with Altivec support', 1),
PathOption('BUILD_DIR',
'Directory to build temporary files in', '.',
PathOption.PathIsDirCreate),
BoolOption('BUILD_SC', 'Build SuperCollider class files', 1),
BoolOption('BUILD_XML', 'Build Faust XML files', 1),
('CC', 'C compiler executable'),
('CCFLAGS', 'C compiler flags'),
('CXX', 'C++ compiler executable'),
('CXXFLAGS', 'C++ compiler flags'),
BoolOption('CROSSCOMPILE',
'Crosscompile for another platform (does not do SSE support check)', 0),
BoolOption('DEBUG',
'Build with debugging information', 0),
PathOption('DESTDIR',
'Intermediate installation prefix for packaging', '/'),
PathOption('FAUST2SC',
'Path to faust2sc script', 'faust2sc',
PathOption.PathAccept),
( 'FAUST2SC_PREFIX',
'Prefix for SC classes generated from Faust definitions', ''),
PathOption('PREFIX',
'Installation prefix', DEFAULT_PREFIX),
PathOption('INSTALLDIR',
'Installation directory', '',
PathOption.PathAccept),
BoolOption('SSE',
'Build with SSE support', 1),
( 'OPT_ARCH',
'Architecture to optimize for', DEFAULT_OPT_ARCH),
PathOption('PLUGIN_SOURCES',
'File containing plugin sources (C++ or Faust)', None,
PathOption.PathAccept),
PathOption('SC_SOURCE_DIR',
'SuperCollider source directory', '../supercollider',
PathOption.PathAccept),
)
if PLATFORM == 'darwin':
opts.AddOptions(
BoolOption('UNIVERSAL',
'Build universal binaries (see UNIVERSAL_ARCHS)', 1),
ListOption('UNIVERSAL_ARCHS',
'Architectures to build for',
'all', ['ppc', 'i386'])
)
# ======================================================================
# basic environment
# ======================================================================
env = Environment(options = opts,
ENV = make_os_env('PATH', 'PKG_CONFIG_PATH'),
PACKAGE = PACKAGE,
URL = 'http://supercollider.sourceforge.net')
env.Append(PATH = ['/usr/local/bin', '/usr/bin', '/bin'])
faustInitEnvironment(env)
# checks for DISTCC and CCACHE as used in modern linux-distros:
if os.path.exists('/usr/lib/distcc/bin'):
os.environ['PATH'] = '/usr/lib/distcc/bin:' + os.environ['PATH']
env['ENV']['DISTCC_HOSTS'] = os.environ['DISTCC_HOSTS']
if os.path.exists('/usr/lib/ccache/bin'):
os.environ['PATH'] = '/usr/lib/ccache/bin:' + os.environ['PATH']
env['ENV']['CCACHE_DIR'] = os.environ['CCACHE_DIR']
env['ENV']['PATH'] = os.environ['PATH']
env['ENV']['HOME'] = os.environ['HOME']
# ======================================================================
# installation directories
# ======================================================================
FINAL_PREFIX = '$PREFIX'
INSTALL_PREFIX = os.path.join('$DESTDIR', '$PREFIX')
# ======================================================================
# Work out the SuperCollider version (this effects the sourcecode layout)
# (thanks Jonny Stutters)
# ======================================================================
print "Attempting to detect SuperCollider version..."
version_classvar = None
try:
version_file = open(env['SC_SOURCE_DIR'] + "/VERSION", 'r')
version_string = version_file.readline().rstrip()
version_classvar = version_file.readline().rstrip()
version_file.close()
except IOError:
sc_version = 3.4
print "Failed to detect SuperCollider version, assuming it's < 3.5"
if version_classvar:
pattern = "^(classvar scVersionMajor=)([0-9]*)(, scVersionMinor=)([0-9]*)(.*)"
r = re.search(pattern, version_classvar)
sc_version = 0.0
sc_version = float(r.group(2))
sc_version += float(r.group(4)) * 0.1
print "It looks like you have version {0}".format(sc_version)
if sc_version >= 3.5:
INCLUDEDIR = 'include'
else:
INCLUDEDIR = 'Headers'
# ======================================================================
# configuration
# ======================================================================
def make_conf(env):
return Configure(env, custom_tests = { 'CheckPKGConfig' : CheckPKGConfig,
'CheckPKG' : CheckPKG })
def isDefaultBuild():
return not env.GetOption('clean') and not 'debian' in COMMAND_LINE_TARGETS
conf = make_conf(env)
# libraries
libraries = { }
features = { }
if isDefaultBuild():
if not conf.CheckPKGConfig('0'):
print 'pkg-config not found.'
Exit(1)
# SC includes and flags
success, libraries['scplugin'] = conf.CheckPKG('libscplugin')
if not success:
success, libraries['scplugin'] = conf.CheckPKG('libscsynth')
if success:
libraries['scplugin']['LIBS'] = []
else:
src_dir = env['SC_SOURCE_DIR']
if os.path.isdir(src_dir):
src_dir = os.path.join(src_dir, INCLUDEDIR)
libraries['scplugin'] = Environment(
CPPDEFINES = [PLATFORM_SYMBOL],
CPPPATH = [os.path.join(src_dir, 'common'),
os.path.join(src_dir, 'plugin_interface'),
os.path.join(src_dir, 'server')])
else:
print "Please specify the SC_SOURCE_DIR option."
Exit(1)
else:
libraries['scplugin'] = Environment()
# only _one_ Configure context can be alive at a time
env = conf.Finish()
# altivec
if env['ALTIVEC']:
if PLATFORM == 'darwin':
altivec_flags = [ '-faltivec' ]
else:
altivec_flags = [ '-maltivec', '-mabi=altivec' ]
libraries['altivec'] = env.Copy()
libraries['altivec'].Append(
CCFLAGS = altivec_flags,
CPPDEFINES = [('SC_MEMORY_ALIGNMENT', 16)])
altiConf = Configure(libraries['altivec'])
features['altivec'] = altiConf.CheckCHeader('altivec.h')
altiConf.Finish()
else:
features['altivec'] = False
# sse
if env['SSE']:
libraries['sse'] = env.Copy()
libraries['sse'].Append(
CCFLAGS = ['-msse', '-mfpmath=sse'],
CPPDEFINES = [('SC_MEMORY_ALIGNMENT', 16)])
sseConf = Configure(libraries['sse'])
hasSSEHeader = sseConf.CheckCHeader('xmmintrin.h')
if env['CROSSCOMPILE']:
build_host_supports_sse = True
print 'NOTICE: cross compiling for another platform: assuming SSE support'
else:
build_host_supports_sse = False
if CPU != 'ppc':
if PLATFORM == 'freebsd':
machine_info = os.popen ("sysctl -a hw.instruction_sse").read()[:-1]
x86_flags = 'no'
if "1" in [x for x in machine_info]:
build_host_supports_sse = True
x86_flags = 'sse'
elif PLATFORM != 'darwin':
flag_line = os.popen ("cat /proc/cpuinfo | grep '^flags'").read()[:-1]
x86_flags = flag_line.split (": ")[1:][0].split ()
else:
machine_info = os.popen ("sysctl -a machdep.cpu").read()[:-1]
x86_flags = machine_info.split()
if "sse" in [x.lower() for x in x86_flags]:
build_host_supports_sse = True
print 'NOTICE: CPU has SSE support'
else:
print 'NOTICE: CPU does not have SSE support'
features['sse'] = hasSSEHeader and build_host_supports_sse
sseConf.Finish()
else:
features['sse'] = False
opts.Save('scache.conf', env)
Help(opts.GenerateHelpText(env))
# defines and compiler flags
env.Append(
CPPDEFINES = [ '_REENTRANT' ],
CCFLAGS = [ '-Wno-unknown-pragmas' ],
CXXFLAGS = [ '-Wno-deprecated' ]
)
# debugging flags
if env['DEBUG']:
env.Append(CCFLAGS = '-g')
else:
env.Append(
CCFLAGS = make_opt_flags(env),
CPPDEFINES = ['NDEBUG'])
# platform specific
if PLATFORM == 'darwin':
env.Append(
CCFLAGS = '-fvisibility=hidden'
)
# vectorization
if features['altivec']:
merge_lib_info(env, libraries['altivec'])
elif features['sse']:
merge_lib_info(env, libraries['sse'])
else:
env.AppendUnique(CPPDEFINES = [('SC_MEMORY_ALIGNMENT', 1)])
# ======================================================================
# Source/plugins
# ======================================================================
pluginEnv = env.Copy(
SHLIBPREFIX = '',
SHLIBSUFFIX = PLUGIN_EXT)
if PLATFORM == 'darwin':
pluginEnv['SHLINKFLAGS'] = '$LINKFLAGS -bundle'
merge_lib_info(pluginEnv, libraries['scplugin'])
def get_sources(env):
if env.has_key('PLUGIN_SOURCES') and env['PLUGIN_SOURCES']:
fd = open(env['PLUGIN_SOURCES'], "r")
sources = fd.readlines()
fd.close()
else:
print "Reading PLUGIN_SOURCES from <stdin>"
sources = sys.stdin.readlines()
return map(lambda x: x.strip(), sources)
def make_build_file(env, path, ext=""):
newpath = os.path.splitext(os.path.basename(path))[0] + ext
return os.path.join(env['BUILD_DIR'], newpath)
def make_plugin_target(env, path):
name, ext = os.path.splitext(path)
if ext == ".dsp":
src = env.Faust(make_build_file(env, path), path)
else:
src = path
return env.SharedLibrary(make_build_file(env, path), src)
def make_xml_target(env, path):
name, ext = os.path.splitext(path)
if ext == '.dsp':
if env['BUILD_SC']:
xml = env.FaustXML(make_build_file(env, path), path)
sc = env.FaustSC(make_build_file(env, path), xml)
if env['BUILD_XML']:
return [sc, xml]
else:
return sc
elif env['BUILD_XML']:
return env.FaustXML(make_build_file(env, path), path)
return []
sources = get_sources(env)
plugins = map(lambda x: make_plugin_target(pluginEnv, x), sources)
xml = map(lambda x: make_xml_target(pluginEnv, x), sources)
# ======================================================================
# installation
# ======================================================================
env.Alias('install', env.Install(
os.path.join(pkg_extension_dir(INSTALL_PREFIX), env['INSTALLDIR']),
plugins + xml))
# ======================================================================
# cleanup
# ======================================================================
env.Clean('scrub',
Split('config.log scache.conf .sconf_temp .sconsign.dblite'))
# ======================================================================
# configuration summary
# ======================================================================
def yesorno(p):
if p: return 'yes'
else: return 'no'
print '------------------------------------------------------------------------'
print ' ALTIVEC: %s' % yesorno(features['altivec'])
print ' BUILD_SC: %s' % yesorno(env['BUILD_SC'])
print ' BUILD_XML: %s' % yesorno(env['BUILD_XML'])
print ' FAUST2SC: %s' % env['FAUST2SC']
if env['INSTALLDIR']:
print ' INSTALLDIR: %s' % env['INSTALLDIR']
print ' DEBUG: %s' % yesorno(env['DEBUG'])
if env.has_key('PLUGIN_SOURCES'):
print ' PLUGIN_SOURCES: %s' % env['PLUGIN_SOURCES']
else:
print ' PLUGIN_SOURCES: %s' % 'stdin'
print ' PREFIX: %s' % env['PREFIX']
print ' SC_SOURCE_DIR %s' % env['SC_SOURCE_DIR']
print ' SSE: %s' % yesorno(features['sse'])
print ' CROSSCOMPILE: %s' % yesorno(env['CROSSCOMPILE'])
print '------------------------------------------------------------------------'
# ======================================================================
# EOF