| 1 |
# Create an application package. |
| 2 |
|
| 3 |
# https://www.scons.org/wiki/SubstInFileBuilder |
| 4 |
|
| 5 |
import re |
| 6 |
from SCons.Script import * |
| 7 |
from SCons.Builder import * |
| 8 |
|
| 9 |
def TOOL_SUBST(env): |
| 10 |
"""Adds SubstInFile builder, which substitutes the keys->values of SUBST_DICT |
| 11 |
from the source to the target. |
| 12 |
The values of SUBST_DICT first have any construction variables expanded |
| 13 |
(its keys are not expanded). |
| 14 |
If a value of SUBST_DICT is a python callable function, it is called and |
| 15 |
the result is expanded as the value. |
| 16 |
If there's more than one source and more than one target, each target gets |
| 17 |
substituted from the corresponding source. |
| 18 |
""" |
| 19 |
env.Append(TOOLS = 'SUBST') |
| 20 |
def do_subst_in_file(targetfile, sourcefile, dict): |
| 21 |
"""Replace all instances of the keys of dict with their values. |
| 22 |
For example, if dict is {'%VERSION%': '1.2345', '%BASE%': 'MyProg'}, |
| 23 |
then all instances of %VERSION% in the file will be replaced with 1.2345 etc. |
| 24 |
""" |
| 25 |
try: |
| 26 |
f = open(sourcefile, 'rb') |
| 27 |
contents = f.read() |
| 28 |
f.close() |
| 29 |
except: |
| 30 |
raise SCons.Errors.UserError("Can't read source file %s" % (sourcefile,)) |
| 31 |
for (k,v) in dict.items(): |
| 32 |
try: |
| 33 |
contents = contents.decode("UTF-8") |
| 34 |
except (UnicodeDecodeError, AttributeError): |
| 35 |
pass |
| 36 |
contents = re.sub(k, v, contents) |
| 37 |
try: |
| 38 |
f = open(targetfile, 'wt') |
| 39 |
f.write(contents) |
| 40 |
f.close() |
| 41 |
except Exception as e: |
| 42 |
print("Text:", str(e)) |
| 43 |
raise SCons.Errors.UserError("Can't write target file %s" % (targetfile,)) |
| 44 |
return 0 # success |
| 45 |
|
| 46 |
def subst_in_file(target, source, env): |
| 47 |
if 'SUBST_DICT' not in env: |
| 48 |
raise SCons.Errors.UserError("SubstInFile requires SUBST_DICT to be set.") |
| 49 |
d = dict(env['SUBST_DICT']) # copy it |
| 50 |
for (k,v) in d.items(): |
| 51 |
if callable(v): |
| 52 |
d[k] = env.subst(v()) |
| 53 |
elif SCons.Util.is_String(v): |
| 54 |
d[k]=env.subst(v) |
| 55 |
else: |
| 56 |
raise SCons.Errors.UserError("SubstInFile: key %s: %r must be a string or callable" % (k, v)) |
| 57 |
for (t,s) in zip(target, source): |
| 58 |
return do_subst_in_file(str(t), str(s), d) |
| 59 |
|
| 60 |
def subst_in_file_string(target, source, env): |
| 61 |
"""This is what gets printed on the console.""" |
| 62 |
# return '\n'.join(['Substituting vars from %s into %s'%(str(s), str(t)) |
| 63 |
# for (t,s) in zip(target, source)]) |
| 64 |
|
| 65 |
def subst_emitter(target, source, env): |
| 66 |
"""Add dependency from substituted SUBST_DICT to target. |
| 67 |
Returns original target, source tuple unchanged. |
| 68 |
""" |
| 69 |
d = env['SUBST_DICT'].copy() # copy it |
| 70 |
for (k,v) in d.items(): |
| 71 |
if callable(v): |
| 72 |
d[k] = env.subst(v()) |
| 73 |
elif SCons.Util.is_String(v): |
| 74 |
d[k]=env.subst(v) |
| 75 |
env.Depends(target, SCons.Node.Python.Value(d)) |
| 76 |
return target, source |
| 77 |
|
| 78 |
subst_action=SCons.Action.Action(subst_in_file, subst_in_file_string) |
| 79 |
env['BUILDERS']['SubstInFile'] = env.Builder(action=subst_action, emitter=subst_emitter) |
| 80 |
|
| 81 |
# https://www.scons.org/wiki/MacOSX (modified to suit) |
| 82 |
|
| 83 |
from SCons.Defaults import SharedCheck, ProgScan |
| 84 |
from SCons.Script.SConscript import SConsEnvironment |
| 85 |
import sys |
| 86 |
import os |
| 87 |
import SCons.Node |
| 88 |
import SCons.Util |
| 89 |
import string |
| 90 |
|
| 91 |
def TOOL_BUNDLE(env): |
| 92 |
"""defines env.LinkBundle() for linking bundles on Darwin/OSX, and |
| 93 |
env.MakeBundle() for installing a bundle into its dir. |
| 94 |
A bundle has this structure: (filenames are case SENSITIVE) |
| 95 |
sapphire.bundle/ |
| 96 |
Contents/ |
| 97 |
Info.plist (an XML key->value database; defined by BUNDLE_INFO_PLIST) |
| 98 |
PkgInfo (trivially short; defined by value of BUNDLE_PKGINFO) |
| 99 |
MacOS/ |
| 100 |
executable (the executable or shared lib, linked with Bundle()) |
| 101 |
Resources/ |
| 102 |
""" |
| 103 |
if 'BUNDLE' in env['TOOLS']: return |
| 104 |
#if tools_verbose: |
| 105 |
print(" running tool: TOOL_BUNDLE") |
| 106 |
env.Append(TOOLS = 'BUNDLE') |
| 107 |
# This is like the regular linker, but uses different vars. |
| 108 |
# XXX: NOTE: this may be out of date now, scons 0.96.91 has some bundle linker stuff built in. |
| 109 |
# Check the docs before using this. |
| 110 |
"""LinkBundle = env.Builder(action=[SharedCheck, "$BUNDLECOM"], |
| 111 |
emitter="$SHLIBEMITTER", |
| 112 |
prefix = '$BUNDLEPREFIX', |
| 113 |
suffix = '$BUNDLESUFFIX', |
| 114 |
target_scanner = ProgScan, |
| 115 |
src_suffix = '$BUNDLESUFFIX', |
| 116 |
src_builder = 'SharedObject') |
| 117 |
env['BUILDERS']['LinkBundle'] = LinkBundle""" |
| 118 |
env['BUNDLEEMITTER'] = None |
| 119 |
env['BUNDLEPREFIX'] = '' |
| 120 |
env['BUNDLESUFFIX'] = '' |
| 121 |
env['BUNDLEDIRSUFFIX'] = '.bundle' |
| 122 |
#env['FRAMEWORKS'] = ['-framework Carbon', '-framework System'] |
| 123 |
env['BUNDLE'] = '$SHLINK' |
| 124 |
env['BUNDLEFLAGS'] = ' -bundle' |
| 125 |
env['BUNDLECOM'] = '$BUNDLE $BUNDLEFLAGS -o ${TARGET} $SOURCES $_LIBDIRFLAGS $_LIBFLAGS $FRAMEWORKS' |
| 126 |
# This requires some other tools: |
| 127 |
TOOL_SUBST(env) |
| 128 |
# Common type codes are BNDL for generic bundle and APPL for application. |
| 129 |
def MakeBundle(env, bundledir, app, |
| 130 |
key, info_plist, |
| 131 |
typecode='BNDL', creator='SapP', |
| 132 |
icon_file='#macosx-install/sapphire-icon.icns', |
| 133 |
subst_dict=None, |
| 134 |
resources=[]): |
| 135 |
"""Install a bundle into its dir, in the proper format""" |
| 136 |
# Substitute construction vars: |
| 137 |
for a in [bundledir, key, info_plist, icon_file, typecode, creator]: |
| 138 |
a = env.subst(a) |
| 139 |
if SCons.Util.is_List(app): |
| 140 |
app = app[0] |
| 141 |
if SCons.Util.is_String(app): |
| 142 |
app = env.subst(app) |
| 143 |
appbase = os.path.basename(app) |
| 144 |
else: |
| 145 |
appbase = os.path.basename(str(app)) |
| 146 |
if not ('.' in bundledir): |
| 147 |
bundledir += '.$BUNDLEDIRSUFFIX' |
| 148 |
bundledir = env.subst(bundledir) # substitute again |
| 149 |
suffix=bundledir[bundledir.rfind('.'):] |
| 150 |
if (suffix=='.app' and typecode != 'APPL' or |
| 151 |
suffix!='.app' and typecode == 'APPL'): |
| 152 |
raise Error("MakeBundle: inconsistent dir suffix %s and type code %s: app bundles should end with .app and type code APPL." % (suffix, typecode)) |
| 153 |
if subst_dict is None: |
| 154 |
subst_dict={'%SHORTVERSION%': '$VERSION_NUM', |
| 155 |
'%LONGVERSION%': '$VERSION_NAME', |
| 156 |
'%YEAR%': '$COMPILE_YEAR', |
| 157 |
'%BUNDLE_EXECUTABLE%': appbase, |
| 158 |
'%ICONFILE%': os.path.basename(icon_file), |
| 159 |
'%CREATOR%': creator, |
| 160 |
'%TYPE%': typecode, |
| 161 |
'%BUNDLE_KEY%': key} |
| 162 |
bundledir = env.Dir(bundledir) |
| 163 |
appfile = env.Install(bundledir.Dir('Contents/MacOS'), app) |
| 164 |
f = env.SubstInFile(bundledir.File('Contents/Info.plist'), info_plist, |
| 165 |
SUBST_DICT=subst_dict) |
| 166 |
env.Depends(f, env.Value(key + creator + typecode + env['VERSION_NUM'] + env['VERSION_NAME'])) |
| 167 |
env.Textfile(target=bundledir.File('Contents/PkgInfo'), |
| 168 |
source=env.Value(typecode+creator)) |
| 169 |
resources.append(icon_file) |
| 170 |
resource_directory = bundledir.Dir('Contents/Resources') |
| 171 |
for r in resources: |
| 172 |
if SCons.Util.is_List(r): |
| 173 |
env.InstallAs(resource_directory.File(r[1]), r[0]) |
| 174 |
else: |
| 175 |
env.Install(resource_directory, r) |
| 176 |
return bundledir, appfile |
| 177 |
# This is not a regular Builder; it's a wrapper function. |
| 178 |
# So just make it available as a method of Environment. |
| 179 |
SConsEnvironment.MakeBundle = MakeBundle |