forked from ibex-team/ibex-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibexutils.py
190 lines (153 loc) · 6.12 KB
/
ibexutils.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
import os, tarfile, functools, sys, shutil, copy
from waflib import Logs, Errors, Utils
from waflib.Configure import conf, ConfigurationContext
sys.path.append(os.path.abspath ("3rd"))
import patch
# not @Configure.conf because, the function is also called by 'options'
def get_dirlist (node):
folders = node.ant_glob('*',dir=True,src=False)
return [ os.path.basename(str(f)) for f in folders ]
@conf
def setting_define (conf, key, val, key_format = "_IBEX_%s_", **kwargs):
conf.setenv ("setting")
conf.define (key_format % key, val, **kwargs)
conf.setenv("")
@conf
def apply_all_relevant_patches (conf, name):
patch_ant_glob = "3rd/%s.all.all*patch" % name
patch_ant_glob += " 3rd/%s.%s.all*patch" % (name, sys.platform)
patch_ant_glob += " 3rd/%s.all.%s*patch" % (name, conf.env.CC_NAME)
patch_ant_glob += " 3rd/%s.%s.%s*patch" % (name,sys.platform,conf.env.CC_NAME)
for p in conf.path.ant_glob (patch_ant_glob):
conf.apply_patch (p.abspath())
@conf
def write_setting_header (conf, **kwargs):
conf.setenv ("setting")
conf.write_config_header (**kwargs)
bak = conf.env.cfg_files
conf.setenv("")
conf.env.append_unique ("cfg_files", bak)
conf.all_envs.pop("setting", None)
def archive_name_without_suffix (archive):
suffixes = [".tar.gz", ".tgz", ".tar" ]
if any(archive.endswith (suffix) for suffix in suffixes):
for suffix in suffixes:
if archive.endswith (suffix):
return archive[:-len(suffix)]
else:
conf.fatal ("Cannot handle archive %s (based on its suffix)" % archive)
@conf
def apply_patch (conf, patch_abspath):
conf.msg ("Applying patch", os.path.basename (patch_abspath))
p = patch.fromfile (patch_abspath)
if not p.apply (root = conf.bldnode.make_node ("3rd").abspath()):
conf.fatal ("Cannot apply patch %s" % patch_abspath)
@conf
def extract_archive (conf, archive_path, name, destnode):
# path is the destination folder where the file will be extracted
path = os.path.join (destnode.abspath (), name)
conf.start_msg("Extracting %s" % os.path.basename(archive_path))
if os.path.isdir (path): # if output directory already exists, remove it
shutil.rmtree (path, ignore_errors = True)
# extract the sources
os.makedirs (path)
t = tarfile.open (archive_path)
t.extractall (destnode.abspath ())
t.close()
conf.end_msg("done")
return path
# convert path from windows format to format compatible with mingw.
# Ex: C:\path/to/dir becomes /c/path/to/dir
def convert_path_win2msys (path):
path = os.path.abspath (path)
drv, path = os.path.splitdrive (path)
return "/%s%s" % (drv[0], path.replace("\\", "/"))
def escape_backslash_on_win32 (s):
if Utils.is_win32:
return s.replace ("\\", "\\\\")
else:
return s
@conf
def path_pc_prefix (conf, path):
path = path.replace (conf.env.PREFIX, "${prefix}")
return escape_backslash_on_win32 (path)
@conf
def path_pc (conf, path):
path = path.replace (conf.env.INCDIR, "${includedir}")
path = path.replace (conf.env.LIBDIR, "${libdir}")
return escape_backslash_on_win32 (path)
@conf
def configure_3rd_party_with_autotools (conf, archive_name,
without_configure=False, without_make_install=False, conf_args = ""):
name = archive_name_without_suffix (archive_name)
Logs.pprint ("BLUE", "Starting installation of %s"%name)
conf.to_log ((" Starting installation of %s " % name).center (80, "="))
archive_path = os.path.join (conf.path.abspath (), "3rd", archive_name)
destnode = conf.bldnode.make_node ("3rd")
# Install everything in build directory, in '3rd' subdirectory (the 'lib' and
# 'include' directory can be copied in conf.env.PREFIX when ./waf install is
# called, if needed)
incdir = destnode.find_or_declare ("include").abspath()
libdir = destnode.find_or_declare ("lib").abspath()
srcdir = conf.extract_archive (archive_path, name, destnode)
conf.find_program ("make")
# Apply patches
conf.apply_all_relevant_patches (name)
# always build static library, even if ibex is built as a shared library.
conf_args += " --enable-static --disable-shared"
if conf.env.ENABLE_SHARED:
cflags = os.getenv("CFLAGS", "")
cxxflags = os.getenv("CXXFLAGS", "")
os.environ["CFLAGS"] = cflags + " ".join(conf.env.CFLAGS_cshlib)
os.environ["CXXFLAGS"] = cxxflags+" "+" ".join(conf.env.CXXFLAGS_cxxshlib)
if Utils.is_win32:
conf_args += " --prefix=%s" % convert_path_win2msys (destnode.abspath ())
conf.find_program ("sh")
cmd_conf = [conf.env.SH, "-c", "./configure %s"%conf_args]
cmd_rmconfig = [conf.env.SH, "-c", "rm config.sub config.guess"]
cmd_reconf = [conf.env.SH, "-c", "autoreconf -i"]
else:
conf_args += " --prefix=%s" % destnode.abspath ()
cmd_conf = "./configure %s" % (conf_args)
cmd_make = conf.env.MAKE + ["-j%d"%conf.options.jobs]
cmd_install = conf.env.MAKE + ["install"]
stages = []
if not without_configure:
if Utils.is_win32:
stages += [ (cmd_rmconfig, cmd_rmconfig[2]), (cmd_reconf, "autoreconf") ]
stages += [ (cmd_conf, "configure") ]
stages += [ (cmd_make, "make") ]
if not without_make_install:
stages += [ (cmd_install, "install") ]
for cmd, stage in stages:
conf.start_msg("Calling %s" % stage)
try:
out = conf.cmd_and_log (cmd, cwd=srcdir, env=os.environ)
conf.end_msg("done")
except Errors.WafError as e:
conf.end_msg("failed", color="RED")
print e
conf.fatal ("failed to %s %s (%s)" % (stage, name, cmd))
conf.to_log ((" Installation of %s: done " % name).center (80, "="))
if conf.env.ENABLE_SHARED:
os.environ["CFLAGS"] = cflags
os.environ["CXXFLAGS"] = cxxflags
return srcdir, incdir, libdir
# Add verbose wrapper around pre_recurse and post_recurse methods of
# ConfigurationContext class, in order to a a more verbose output.
def verbose_pre_recurse (f):
def fun (ctx, node):
if Logs.verbose:
Logs.pprint ("BLUE", "Starting configure from ./%s" % node.srcpath ())
f (ctx, node)
fun.__name__ = f.__name__
return fun
ConfigurationContext.pre_recurse = verbose_pre_recurse (ConfigurationContext.pre_recurse)
def verbose_post_recurse (f):
def fun (ctx, node):
if Logs.verbose:
Logs.pprint ("BLUE", "Leaving configure from ./%s" % node.srcpath ())
f (ctx, node)
fun.__name__ = f.__name__
return fun
ConfigurationContext.post_recurse = verbose_post_recurse (ConfigurationContext.post_recurse)