author | Gustavo J. A. M. Carneiro <gjc@inescporto.pt> |
Sat, 06 Sep 2008 19:24:32 +0100 | |
changeset 3639 | 8e69ebf086f1 |
parent 3637 | a6d8bf62a61d |
child 3647 | cd704871a4ed |
permissions | -rw-r--r-- |
3408 | 1 |
## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
2 |
||
3 |
import re |
|
4 |
import Params |
|
5 |
import Configure |
|
6 |
import Object |
|
7 |
import Action |
|
8 |
import os |
|
9 |
import Task |
|
10 |
import pproc as subprocess |
|
11 |
from Params import fatal, warning |
|
12 |
import shutil |
|
3619
a97d3ed04035
Disable Python support on the CygWin platform, at least until/if the problems are fixed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3612
diff
changeset
|
13 |
import sys |
3408 | 14 |
|
15 |
## Adjust python path to look for our local copy of pybindgen |
|
16 |
LOCAL_PYBINDGEN_PATH = os.path.join(os.getcwd(), "bindings", "python", "pybindgen") |
|
17 |
#PYBINDGEN_BRANCH = 'lp:pybindgen' |
|
18 |
PYBINDGEN_BRANCH = 'https://launchpad.net/pybindgen' |
|
19 |
if os.environ.get('PYTHONPATH', ''): |
|
20 |
os.environ['PYTHONPATH'] = LOCAL_PYBINDGEN_PATH + os.pathsep + os.environ.get('PYTHONPATH') |
|
21 |
else: |
|
22 |
os.environ['PYTHONPATH'] = LOCAL_PYBINDGEN_PATH |
|
23 |
||
24 |
## https://launchpad.net/pybindgen/ |
|
3624
422ce0c7bd20
New pybindgen, should fix bug #308.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3619
diff
changeset
|
25 |
REQUIRED_PYBINDGEN_VERSION = (0, 9, 0, 577) |
3408 | 26 |
REQUIRED_PYGCCXML_VERSION = (0, 9, 5) |
27 |
||
28 |
||
29 |
def set_options(opt): |
|
30 |
opt.tool_options('python') |
|
3637
a6d8bf62a61d
For better consistency, option --nsc becomes --enable-nsc, --python-disable becomes --disable-python.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3625
diff
changeset
|
31 |
opt.add_option('--disable-python', |
3408 | 32 |
help=("Don't build Python bindings."), |
33 |
action="store_true", default=False, |
|
34 |
dest='python_disable') |
|
35 |
opt.add_option('--python-scan', |
|
36 |
help=("Rescan Python bindings. Needs working GCCXML / pygccxml environment."), |
|
37 |
action="store_true", default=False, |
|
38 |
dest='python_scan') |
|
39 |
opt.add_option('--pybindgen-checkout', |
|
40 |
help=("During configure, force checkout of pybingen inside ns-3, " |
|
41 |
"instead of using the system installed version."), |
|
42 |
action="store_true", default=False, |
|
43 |
dest='pybindgen_checkout') |
|
44 |
||
45 |
def fetch_pybindgen(conf): |
|
46 |
""" |
|
47 |
Fetches pybindgen from launchpad as bindings/python/pybindgen. |
|
48 |
Returns True if successful, False it not. |
|
49 |
""" |
|
50 |
bzr = conf.find_program("bzr") |
|
51 |
if not bzr: |
|
52 |
warning("the program 'bzr' is needed in order to fetch pybindgen") |
|
53 |
return False |
|
54 |
if len(REQUIRED_PYBINDGEN_VERSION) == 4: |
|
55 |
rev = "-rrevno:%i" % REQUIRED_PYBINDGEN_VERSION[3] |
|
56 |
else: |
|
57 |
rev = "-rtag:%s" % '.'.join([str(x) for x in REQUIRED_PYBINDGEN_VERSION]) |
|
58 |
if os.path.exists(LOCAL_PYBINDGEN_PATH): |
|
59 |
print "Trying to update pybindgen; this will fail if no network connection is available." |
|
60 |
||
61 |
cmd = [bzr, "pull", rev, PYBINDGEN_BRANCH] |
|
62 |
print " => ", ' '.join(cmd) |
|
63 |
if subprocess.Popen(cmd, cwd=LOCAL_PYBINDGEN_PATH).wait(): |
|
64 |
return False |
|
65 |
print "Update was successful." |
|
66 |
else: |
|
67 |
print "Trying to fetch pybindgen; this will fail if no network connection is available." |
|
68 |
cmd = [bzr, "checkout", rev, PYBINDGEN_BRANCH, LOCAL_PYBINDGEN_PATH] |
|
69 |
print " => ", ' '.join(cmd) |
|
70 |
if subprocess.Popen(cmd).wait(): |
|
71 |
return False |
|
72 |
print "Fetch was successful." |
|
73 |
||
74 |
## generate a fake version.py file in pybindgen it's safer this |
|
75 |
## way, since the normal version generation process requires |
|
76 |
## bazaar python bindings, which may not be available. |
|
77 |
vfile = open(os.path.join(LOCAL_PYBINDGEN_PATH, "pybindgen", "version.py"), "wt") |
|
78 |
vfile.write(""" |
|
79 |
# (fake version generated by ns-3) |
|
80 |
__version__ = %r |
|
81 |
""" % list(REQUIRED_PYBINDGEN_VERSION)) |
|
82 |
vfile.close() |
|
83 |
||
84 |
return True |
|
85 |
||
86 |
||
87 |
def configure(conf): |
|
88 |
conf.env['ENABLE_PYTHON_BINDINGS'] = False |
|
89 |
if Params.g_options.python_disable: |
|
3625
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
90 |
conf.report_optional_feature("python", "Python Bindings", False, |
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
91 |
"disabled by user request") |
3408 | 92 |
return |
93 |
||
94 |
conf.check_tool('misc') |
|
95 |
||
3619
a97d3ed04035
Disable Python support on the CygWin platform, at least until/if the problems are fixed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3612
diff
changeset
|
96 |
if sys.platform == 'cygwin': |
3625
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
97 |
conf.report_optional_feature("python", "Python Bindings", False, |
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
98 |
"unsupported platform 'cygwin'") |
3619
a97d3ed04035
Disable Python support on the CygWin platform, at least until/if the problems are fixed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3612
diff
changeset
|
99 |
warning("Python is not supported in CygWin environment. Try MingW instead.") |
a97d3ed04035
Disable Python support on the CygWin platform, at least until/if the problems are fixed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3612
diff
changeset
|
100 |
return |
a97d3ed04035
Disable Python support on the CygWin platform, at least until/if the problems are fixed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3612
diff
changeset
|
101 |
|
3408 | 102 |
## Check for Python |
103 |
try: |
|
104 |
conf.check_tool('python') |
|
3473
6bce86ea4778
Require new PyBindGen; make it work for Python 2.3.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3468
diff
changeset
|
105 |
conf.check_python_version((2,3)) |
3408 | 106 |
conf.check_python_headers() |
3625
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
107 |
except Configure.ConfigurationError, ex: |
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
108 |
conf.report_optional_feature("python", "Python Bindings", False, str(ex)) |
3408 | 109 |
return |
110 |
||
111 |
## Check for pybindgen |
|
112 |
if Params.g_options.pybindgen_checkout: |
|
113 |
fetch_pybindgen(conf) |
|
114 |
||
115 |
try: |
|
116 |
conf.check_python_module('pybindgen') |
|
117 |
except Configure.ConfigurationError: |
|
118 |
warning("pybindgen missing") |
|
119 |
if not fetch_pybindgen(conf): |
|
3625
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
120 |
conf.report_optional_feature("python", "Python Bindings", False, |
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
121 |
"PyBindGen missing and could not be retrieved") |
3408 | 122 |
return |
123 |
else: |
|
124 |
out = subprocess.Popen([conf.env['PYTHON'], "-c", |
|
125 |
"import pybindgen.version; " |
|
126 |
"print '.'.join([str(x) for x in pybindgen.version.__version__])"], |
|
127 |
stdout=subprocess.PIPE).communicate()[0] |
|
128 |
pybindgen_version_str = out.strip() |
|
129 |
pybindgen_version = tuple([int(x) for x in pybindgen_version_str.split('.')]) |
|
130 |
conf.check_message('pybindgen', 'version', |
|
131 |
(pybindgen_version >= REQUIRED_PYBINDGEN_VERSION), |
|
132 |
pybindgen_version_str) |
|
133 |
if not (pybindgen_version >= REQUIRED_PYBINDGEN_VERSION): |
|
134 |
warning("pybindgen (found %s) is too old (need %s)" % |
|
135 |
(pybindgen_version_str, |
|
136 |
'.'.join([str(x) for x in REQUIRED_PYBINDGEN_VERSION]))) |
|
137 |
if not fetch_pybindgen(conf): |
|
3625
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
138 |
conf.report_optional_feature("python", "Python Bindings", False, |
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
139 |
"PyBindGen too old and newer version could not be retrieved") |
3408 | 140 |
return |
141 |
||
142 |
## If all has gone well, we finally enable the Python bindings |
|
143 |
conf.env['ENABLE_PYTHON_BINDINGS'] = True |
|
3625
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
144 |
conf.report_optional_feature("python", "Python Bindings", True, None) |
3408 | 145 |
|
146 |
## Check for pygccxml |
|
147 |
try: |
|
148 |
conf.check_python_module('pygccxml') |
|
149 |
except Configure.ConfigurationError: |
|
3625
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
150 |
conf.report_optional_feature("pygccxml", "Python API Scanning Support", False, |
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
151 |
"Missing 'pygccxml' Python module") |
3408 | 152 |
return |
153 |
||
154 |
out = subprocess.Popen([conf.env['PYTHON'], "-c", |
|
155 |
"import pygccxml; print pygccxml.__version__"], |
|
156 |
stdout=subprocess.PIPE).communicate()[0] |
|
157 |
pygccxml_version_str = out.strip() |
|
158 |
pygccxml_version = tuple([int(x) for x in pygccxml_version_str.split('.')]) |
|
159 |
conf.check_message('pygccxml', 'version', |
|
160 |
(pygccxml_version >= REQUIRED_PYGCCXML_VERSION), |
|
161 |
pygccxml_version_str) |
|
162 |
if not (pygccxml_version >= REQUIRED_PYGCCXML_VERSION): |
|
163 |
warning("pygccxml (found %s) is too old (need %s) => " |
|
164 |
"automatic scanning of API definitions will not be possible" % |
|
165 |
(pygccxml_version_str, |
|
166 |
'.'.join([str(x) for x in REQUIRED_PYGCCXML_VERSION]))) |
|
3625
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
167 |
conf.report_optional_feature("pygccxml", "Python API Scanning Support", False, |
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
168 |
"pygccxml too old") |
3408 | 169 |
return |
170 |
||
171 |
||
172 |
## Check gccxml version |
|
173 |
gccxml = conf.find_program('gccxml', var='GCCXML') |
|
174 |
if not gccxml: |
|
175 |
warning("gccxml missing; automatic scanning of API definitions will not be possible") |
|
3625
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
176 |
conf.report_optional_feature("pygccxml", "Python API Scanning Support", False, |
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
177 |
"gccxml missing") |
3408 | 178 |
return |
179 |
||
180 |
gccxml_version_line = os.popen(gccxml + " --version").readline().strip() |
|
181 |
m = re.match( "^GCC-XML version (\d\.\d(\.\d)?)$", gccxml_version_line) |
|
182 |
gccxml_version = m.group(1) |
|
183 |
gccxml_version_ok = ([int(s) for s in gccxml_version.split('.')] >= [0, 9]) |
|
184 |
conf.check_message('gccxml', 'version', True, gccxml_version) |
|
185 |
if not gccxml_version_ok: |
|
186 |
warning("gccxml too old, need version >= 0.9; automatic scanning of API definitions will not be possible") |
|
3625
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
187 |
conf.report_optional_feature("pygccxml", "Python API Scanning Support", False, |
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
188 |
"gccxml too old") |
3408 | 189 |
return |
190 |
||
191 |
## If we reached |
|
192 |
conf.env['ENABLE_PYTHON_SCANNING'] = True |
|
3625
30afad8324d5
Add a summary of optional features at the end of the configuration stage.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3624
diff
changeset
|
193 |
conf.report_optional_feature("pygccxml", "Python API Scanning Support", True, None) |
3408 | 194 |
|
195 |
||
196 |
prio_headers = { |
|
197 |
-2: ( |
|
198 |
"string.h", # work around http://www.gccxml.org/Bug/view.php?id=6682 |
|
199 |
), |
|
200 |
-1: ( |
|
201 |
"propagation-delay-model.h", |
|
202 |
"propagation-loss-model.h", |
|
203 |
"net-device.h", |
|
204 |
) |
|
205 |
} |
|
206 |
||
207 |
def get_header_prio(header): |
|
208 |
for prio, headers in prio_headers.iteritems(): |
|
209 |
if header in headers: |
|
210 |
return prio |
|
211 |
return 1 |
|
212 |
||
3485
26de1421d000
When generating the everything.h header file for Python, handle ns3 headers inside subdirectories properly.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3474
diff
changeset
|
213 |
|
26de1421d000
When generating the everything.h header file for Python, handle ns3 headers inside subdirectories properly.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3474
diff
changeset
|
214 |
def calc_header_include(path): |
26de1421d000
When generating the everything.h header file for Python, handle ns3 headers inside subdirectories properly.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3474
diff
changeset
|
215 |
(head, tail) = os.path.split (path) |
26de1421d000
When generating the everything.h header file for Python, handle ns3 headers inside subdirectories properly.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3474
diff
changeset
|
216 |
if tail == 'ns3': |
26de1421d000
When generating the everything.h header file for Python, handle ns3 headers inside subdirectories properly.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3474
diff
changeset
|
217 |
return '' |
26de1421d000
When generating the everything.h header file for Python, handle ns3 headers inside subdirectories properly.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3474
diff
changeset
|
218 |
else: |
26de1421d000
When generating the everything.h header file for Python, handle ns3 headers inside subdirectories properly.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3474
diff
changeset
|
219 |
return os.path.join (calc_header_include (head), tail) |
26de1421d000
When generating the everything.h header file for Python, handle ns3 headers inside subdirectories properly.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3474
diff
changeset
|
220 |
|
26de1421d000
When generating the everything.h header file for Python, handle ns3 headers inside subdirectories properly.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3474
diff
changeset
|
221 |
|
3408 | 222 |
def gen_ns3_metaheader(task): |
223 |
assert len(task.m_outputs) == 1 |
|
3485
26de1421d000
When generating the everything.h header file for Python, handle ns3 headers inside subdirectories properly.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3474
diff
changeset
|
224 |
|
26de1421d000
When generating the everything.h header file for Python, handle ns3 headers inside subdirectories properly.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3474
diff
changeset
|
225 |
header_files = [calc_header_include(node.abspath(task.m_env)) for node in task.m_inputs] |
3408 | 226 |
outfile = file(task.m_outputs[0].bldpath(task.m_env), "w") |
227 |
||
228 |
def sort_func(h1, h2): |
|
229 |
return cmp((get_header_prio(h1), h1), (get_header_prio(h1), h2)) |
|
230 |
||
231 |
header_files.sort(sort_func) |
|
232 |
||
233 |
for header in header_files: |
|
234 |
print >> outfile, "#include \"ns3/%s\"" % (header,) |
|
235 |
||
236 |
print >> outfile, """ |
|
237 |
namespace ns3 { |
|
238 |
static inline Ptr<Object> |
|
239 |
__dummy_function_to_force_template_instantiation (Ptr<Object> obj, TypeId typeId) |
|
240 |
{ |
|
241 |
return obj->GetObject<Object> (typeId); |
|
242 |
} |
|
243 |
||
244 |
} |
|
245 |
""" |
|
246 |
outfile.close() |
|
247 |
return 0 |
|
248 |
||
249 |
||
3541
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
250 |
|
3408 | 251 |
class all_ns3_headers_taskgen(Object.task_gen): |
252 |
"""Generates a 'everything.h' header file that includes some/all public ns3 headers. |
|
253 |
This single header file is to be parsed only once by gccxml, for greater efficiency. |
|
254 |
""" |
|
255 |
def __init__(self, *features): |
|
256 |
Object.task_gen.__init__(self, *features) |
|
3409
94ac3e381075
The 'everything.h' header file is only used for Python bindings and should be generated into bindings/python/, not ns3/.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3408
diff
changeset
|
257 |
self.inst_var = 0#'INCLUDEDIR' |
94ac3e381075
The 'everything.h' header file is only used for Python bindings and should be generated into bindings/python/, not ns3/.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3408
diff
changeset
|
258 |
#self.inst_dir = 'ns3' |
3408 | 259 |
|
260 |
def apply(self): |
|
261 |
## get all of the ns3 headers |
|
262 |
ns3_dir_node = Params.g_build.m_srcnode.find_dir("ns3") |
|
263 |
all_headers_inputs = [] |
|
264 |
||
265 |
for filename in self.to_list(self.source): |
|
266 |
src_node = ns3_dir_node.find_build(filename) |
|
267 |
if src_node is None: |
|
268 |
Params.fatal("source ns3 header file %s not found" % (filename,)) |
|
269 |
all_headers_inputs.append(src_node) |
|
270 |
||
271 |
## if self.source was empty, include all ns3 headers in enabled modules |
|
272 |
if not all_headers_inputs: |
|
273 |
for ns3headers in Object.g_allobjs: |
|
274 |
if type(ns3headers).__name__ == 'ns3header_taskgen': # XXX: find less hackish way to compare |
|
275 |
## skip headers not part of enabled modules |
|
276 |
if self.env['NS3_ENABLED_MODULES']: |
|
277 |
if ("ns3-%s" % ns3headers.module) not in self.env['NS3_ENABLED_MODULES']: |
|
278 |
continue |
|
279 |
||
280 |
for source in ns3headers.to_list(ns3headers.source): |
|
3485
26de1421d000
When generating the everything.h header file for Python, handle ns3 headers inside subdirectories properly.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3474
diff
changeset
|
281 |
#source = os.path.basename(source) |
26de1421d000
When generating the everything.h header file for Python, handle ns3 headers inside subdirectories properly.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3474
diff
changeset
|
282 |
node = ns3_dir_node.find_build(source) |
3408 | 283 |
if node is None: |
284 |
fatal("missing header file %s" % (source,)) |
|
285 |
all_headers_inputs.append(node) |
|
286 |
assert all_headers_inputs |
|
3409
94ac3e381075
The 'everything.h' header file is only used for Python bindings and should be generated into bindings/python/, not ns3/.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3408
diff
changeset
|
287 |
all_headers_outputs = [self.path.find_build("everything.h")] |
3408 | 288 |
task = self.create_task('gen-ns3-metaheader', self.env, 4) |
289 |
task.set_inputs(all_headers_inputs) |
|
290 |
task.set_outputs(all_headers_outputs) |
|
291 |
||
292 |
def install(self): |
|
293 |
pass |
|
294 |
||
295 |
||
296 |
def get_modules_and_headers(): |
|
297 |
""" |
|
298 |
Gets a dict of |
|
299 |
module_name => ([module_dep1, module_dep2, ...], [module_header1, module_header2, ...]) |
|
300 |
tuples, one for each module. |
|
301 |
""" |
|
302 |
||
303 |
retval = {} |
|
304 |
for module in Object.g_allobjs: |
|
305 |
if not module.name.startswith('ns3-'): |
|
306 |
continue |
|
307 |
module_name = module.name[4:] # strip the ns3- prefix |
|
308 |
## find the headers object for this module |
|
309 |
headers = [] |
|
310 |
for ns3headers in Object.g_allobjs: |
|
311 |
if type(ns3headers).__name__ != 'ns3header_taskgen': # XXX: find less hackish way to compare |
|
312 |
continue |
|
313 |
if ns3headers.module != module_name: |
|
314 |
continue |
|
315 |
for source in ns3headers.to_list(ns3headers.source): |
|
316 |
headers.append(source) |
|
317 |
retval[module_name] = (list(module.module_deps), headers) |
|
318 |
return retval |
|
319 |
||
320 |
||
3541
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
321 |
|
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
322 |
class PythonScanTask(Task.TaskBase): |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
323 |
"""Uses gccxml to scan the file 'everything.h' and extract API definitions. |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
324 |
""" |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
325 |
def __init__(self, curdirnode, env): |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
326 |
self.m_display = 'python-scan' |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
327 |
self.prio = 5 # everything.h has prio 4 |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
328 |
super(PythonScanTask, self).__init__() |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
329 |
self.curdirnode = curdirnode |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
330 |
self.env = env |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
331 |
|
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
332 |
def run(self): |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
333 |
#print "Rescanning the python bindings..." |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
334 |
argv = [ |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
335 |
self.env['PYTHON'], |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
336 |
os.path.join(self.curdirnode.abspath(), 'ns3modulescan.py'), # scanning script |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
337 |
self.curdirnode.find_dir('../..').abspath(self.env), # include path (where the ns3 include dir is) |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
338 |
self.curdirnode.find_build('everything.h').abspath(self.env), |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
339 |
os.path.join(self.curdirnode.abspath(), 'ns3modulegen_generated.py'), # output file |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
340 |
] |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
341 |
scan = subprocess.Popen(argv, stdin=subprocess.PIPE) |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
342 |
scan.stdin.write(repr(get_modules_and_headers())) |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
343 |
scan.stdin.close() |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
344 |
if scan.wait(): |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
345 |
raise SystemExit(1) |
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
346 |
|
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
347 |
|
3408 | 348 |
def build(bld): |
349 |
if Params.g_options.python_disable: |
|
350 |
return |
|
351 |
||
352 |
env = bld.env_of_name('default') |
|
3591
f548d13ffd4e
Fix build when ns3_module_*__local.py files are used.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3575
diff
changeset
|
353 |
curdir = bld.m_curdirnode.abspath() |
f548d13ffd4e
Fix build when ns3_module_*__local.py files are used.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3575
diff
changeset
|
354 |
|
3408 | 355 |
#Object.register('all-ns3-headers', AllNs3Headers) |
356 |
Action.Action('gen-ns3-metaheader', func=gen_ns3_metaheader, color='BLUE') |
|
357 |
||
3486
818a6bbef490
Only generate everything.h if python bindings are enabled. Based on patch by Mathieu Lacage. Closes #260.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3485
diff
changeset
|
358 |
if env['ENABLE_PYTHON_BINDINGS']: |
818a6bbef490
Only generate everything.h if python bindings are enabled. Based on patch by Mathieu Lacage. Closes #260.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3485
diff
changeset
|
359 |
obj = bld.create_obj('all_ns3_headers') |
3408 | 360 |
|
361 |
if Params.g_options.python_scan: |
|
362 |
if not env['ENABLE_PYTHON_SCANNING']: |
|
363 |
Params.fatal("Cannot re-scan python bindings: (py)gccxml not available") |
|
3541
15fe83e15ff5
Don't scan python bindings until the everything.h file to be scanned is generated. Closes #288.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3540
diff
changeset
|
364 |
PythonScanTask(bld.m_curdirnode, env) |
3408 | 365 |
|
3424
fd8ae9ea848b
Again, make Python depend only on scanned modules (last fix was incomplete).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3423
diff
changeset
|
366 |
## Get a list of scanned modules; the set of scanned modules |
fd8ae9ea848b
Again, make Python depend only on scanned modules (last fix was incomplete).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3423
diff
changeset
|
367 |
## may be smaller than the set of all modules, in case a new |
fd8ae9ea848b
Again, make Python depend only on scanned modules (last fix was incomplete).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3423
diff
changeset
|
368 |
## ns3 module is being developed which wasn't scanned yet. |
fd8ae9ea848b
Again, make Python depend only on scanned modules (last fix was incomplete).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3423
diff
changeset
|
369 |
scanned_modules = [] |
3591
f548d13ffd4e
Fix build when ns3_module_*__local.py files are used.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3575
diff
changeset
|
370 |
for filename in os.listdir(curdir): |
3424
fd8ae9ea848b
Again, make Python depend only on scanned modules (last fix was incomplete).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3423
diff
changeset
|
371 |
m = re.match(r"^ns3_module_(.+)\.py$", filename) |
fd8ae9ea848b
Again, make Python depend only on scanned modules (last fix was incomplete).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3423
diff
changeset
|
372 |
if m is None: |
fd8ae9ea848b
Again, make Python depend only on scanned modules (last fix was incomplete).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3423
diff
changeset
|
373 |
continue |
3591
f548d13ffd4e
Fix build when ns3_module_*__local.py files are used.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3575
diff
changeset
|
374 |
name = m.group(1) |
f548d13ffd4e
Fix build when ns3_module_*__local.py files are used.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3575
diff
changeset
|
375 |
if name.endswith("__local"): |
f548d13ffd4e
Fix build when ns3_module_*__local.py files are used.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3575
diff
changeset
|
376 |
continue |
f548d13ffd4e
Fix build when ns3_module_*__local.py files are used.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3575
diff
changeset
|
377 |
scanned_modules.append(name) |
3424
fd8ae9ea848b
Again, make Python depend only on scanned modules (last fix was incomplete).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3423
diff
changeset
|
378 |
|
3408 | 379 |
if env['ENABLE_PYTHON_BINDINGS']: |
380 |
bindgen = bld.create_obj('command-output') |
|
381 |
bindgen.name = 'pybindgen' |
|
382 |
bindgen.command = env['PYTHON'] |
|
383 |
bindgen.command_is_external = True |
|
3411
eca46ea15a04
Redirect the stderr of pybindgen to a log file (ns3modulegen.log).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3409
diff
changeset
|
384 |
bindgen.stderr = 'ns3modulegen.log' |
3408 | 385 |
bindgen.argv = [ |
386 |
bindgen.input_file("ns3modulegen.py"), |
|
387 |
bindgen.output_file("ns3module.cc"), |
|
388 |
] |
|
389 |
bindgen.argv.extend(get_modules_and_headers().iterkeys()) |
|
3409
94ac3e381075
The 'everything.h' header file is only used for Python bindings and should be generated into bindings/python/, not ns3/.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3408
diff
changeset
|
390 |
bindgen.hidden_inputs = ['everything.h', |
3408 | 391 |
'ns3modulegen_generated.py', |
392 |
'ns3modulegen_core_customizations.py'] |
|
3423
f84261098ab0
Make Python bindings depend only on NS-3 modules that have been API-scanned, not all modules.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3421
diff
changeset
|
393 |
|
f84261098ab0
Make Python bindings depend only on NS-3 modules that have been API-scanned, not all modules.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3421
diff
changeset
|
394 |
for module in scanned_modules: |
f84261098ab0
Make Python bindings depend only on NS-3 modules that have been API-scanned, not all modules.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3421
diff
changeset
|
395 |
bindgen.hidden_inputs.append("ns3_module_%s.py" % module) |
3591
f548d13ffd4e
Fix build when ns3_module_*__local.py files are used.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3575
diff
changeset
|
396 |
local = "ns3_module_%s__local.py" % module |
f548d13ffd4e
Fix build when ns3_module_*__local.py files are used.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3575
diff
changeset
|
397 |
if os.path.exists(os.path.join(curdir, local)): |
f548d13ffd4e
Fix build when ns3_module_*__local.py files are used.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3575
diff
changeset
|
398 |
bindgen.hidden_inputs.append(local) |
3423
f84261098ab0
Make Python bindings depend only on NS-3 modules that have been API-scanned, not all modules.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3421
diff
changeset
|
399 |
|
3408 | 400 |
bindgen.hidden_outputs = ['ns3module.h'] |
3423
f84261098ab0
Make Python bindings depend only on NS-3 modules that have been API-scanned, not all modules.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3421
diff
changeset
|
401 |
for module in scanned_modules: |
f84261098ab0
Make Python bindings depend only on NS-3 modules that have been API-scanned, not all modules.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3421
diff
changeset
|
402 |
bindgen.hidden_outputs.append("ns3_module_%s.cc" % module) |
f84261098ab0
Make Python bindings depend only on NS-3 modules that have been API-scanned, not all modules.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3421
diff
changeset
|
403 |
|
3408 | 404 |
bindgen.prio = 50 |
405 |
||
3420
390db659644b
If GtkConfigStore support is disabled, disable the python bindings for it, or else compilation fails.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3417
diff
changeset
|
406 |
bindgen.os_env = dict(os.environ) |
3639
8e69ebf086f1
Use the information provided by conf.report_optional_feature() to enable/disable python bindings for optional APIs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3637
diff
changeset
|
407 |
features = [] |
8e69ebf086f1
Use the information provided by conf.report_optional_feature() to enable/disable python bindings for optional APIs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3637
diff
changeset
|
408 |
for (name, caption, was_enabled, reason_not_enabled) in env['NS3_OPTIONAL_FEATURES']: |
8e69ebf086f1
Use the information provided by conf.report_optional_feature() to enable/disable python bindings for optional APIs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3637
diff
changeset
|
409 |
if was_enabled: |
8e69ebf086f1
Use the information provided by conf.report_optional_feature() to enable/disable python bindings for optional APIs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3637
diff
changeset
|
410 |
features.append(name) |
8e69ebf086f1
Use the information provided by conf.report_optional_feature() to enable/disable python bindings for optional APIs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3637
diff
changeset
|
411 |
bindgen.os_env['NS3_ENABLED_FEATURES'] = ','.join(features) |
3420
390db659644b
If GtkConfigStore support is disabled, disable the python bindings for it, or else compilation fails.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3417
diff
changeset
|
412 |
|
3408 | 413 |
|
414 |
## we build python bindings if either we have the tools to |
|
415 |
## generate them or if the pregenerated source file is already |
|
416 |
## present in the source dir. |
|
417 |
if env['ENABLE_PYTHON_BINDINGS'] \ |
|
418 |
or os.path.exists(os.path.join(bld.m_curdirnode.abspath(), 'ns3module.cc')): |
|
419 |
pymod = bld.create_obj('cpp', 'shlib', 'pyext') |
|
420 |
pymod.source = ['ns3module.cc', 'ns3module_helpers.cc'] |
|
421 |
pymod.includes = '.' |
|
3424
fd8ae9ea848b
Again, make Python depend only on scanned modules (last fix was incomplete).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3423
diff
changeset
|
422 |
for module in scanned_modules: |
fd8ae9ea848b
Again, make Python depend only on scanned modules (last fix was incomplete).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3423
diff
changeset
|
423 |
pymod.source.append("ns3_module_%s.cc" % module) |
3408 | 424 |
pymod.target = 'ns3/_ns3' |
425 |
pymod.name = 'ns3module' |
|
426 |
pymod.uselib_local = "ns3" |
|
3417
06df0787254b
Make sure we don't trigger deprecation warnings while compiling the python bindings.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
427 |
pymod.env.append_value('CXXDEFINES', ['NS_DEPRECATED=""', 'NS3_DEPRECATED_H']) |
3408 | 428 |
|
429 |
# copy the __init__.py file to the build dir waf can't handle |
|
430 |
# this, it's against waf's principles to have build dir files |
|
431 |
# with the same name as source dir files, apparently. |
|
432 |
dirnode = bld.m_curdirnode.find_dir('ns3') |
|
433 |
src = os.path.join(dirnode.abspath(), '__init__.py') |
|
434 |
dst = os.path.join(dirnode.abspath(env), '__init__.py') |
|
435 |
try: |
|
436 |
need_copy = os.stat(src).st_mtime > os.stat(dst).st_mtime |
|
437 |
except OSError: |
|
438 |
need_copy = True |
|
439 |
if need_copy: |
|
440 |
try: |
|
441 |
os.mkdir(os.path.dirname(dst)) |
|
442 |
except OSError: |
|
443 |
pass |
|
444 |
print "%r -> %r" % (src, dst) |
|
445 |
shutil.copy2(src, dst) |