author | Gustavo J. A. M. Carneiro <gjc@inescporto.pt> |
Sun, 12 Oct 2008 18:04:10 +0100 | |
changeset 3743 | d0cf214d050c |
parent 3537 | ab1673dcdf58 |
child 3753 | a84a48233eb3 |
permissions | -rw-r--r-- |
3408 | 1 |
#! /usr/bin/env python |
2 |
||
3 |
import sys |
|
4 |
import os.path |
|
5 |
||
6 |
import pybindgen.settings |
|
7 |
from pybindgen.gccxmlparser import ModuleParser, PygenClassifier, PygenSection, WrapperWarning |
|
8 |
from pybindgen.typehandlers.codesink import FileCodeSink |
|
9 |
from pygccxml.declarations import templates |
|
10 |
from pygccxml.declarations.class_declaration import class_t |
|
11 |
from pygccxml.declarations.calldef import free_function_t, member_function_t, constructor_t |
|
12 |
||
13 |
||
14 |
## we need the smart pointer type transformation to be active even |
|
15 |
## during gccxml scanning. |
|
16 |
import ns3modulegen_core_customizations |
|
17 |
||
18 |
||
19 |
## silence gccxmlparser errors; we only want error handling in the |
|
20 |
## generated python script, not while scanning. |
|
21 |
class ErrorHandler(pybindgen.settings.ErrorHandler): |
|
22 |
def handle_error(self, dummy_wrapper, dummy_exception, dummy_traceback_): |
|
23 |
return True |
|
24 |
pybindgen.settings.error_handler = ErrorHandler() |
|
25 |
import warnings |
|
26 |
warnings.filterwarnings(category=WrapperWarning, action='ignore') |
|
27 |
||
28 |
type_annotations = { |
|
29 |
'::ns3::RefCountBase': { |
|
30 |
'incref_method': 'Ref', |
|
31 |
'decref_method': 'Unref', |
|
32 |
'peekref_method': 'GetReferenceCount', |
|
33 |
'automatic_type_narrowing': 'true', |
|
34 |
}, |
|
35 |
'::ns3::Object': { |
|
36 |
'incref_method': 'Ref', |
|
37 |
'decref_method': 'Unref', |
|
38 |
'peekref_method': 'GetReferenceCount', |
|
39 |
'automatic_type_narrowing': 'true', |
|
40 |
}, |
|
41 |
'::ns3::Packet': { |
|
42 |
'incref_method': 'Ref', |
|
43 |
'decref_method': 'Unref', |
|
44 |
'peekref_method': 'GetReferenceCount', |
|
45 |
}, |
|
46 |
'::ns3::CallbackImplBase': { |
|
47 |
'incref_method': 'Ref', |
|
48 |
'decref_method': 'Unref', |
|
49 |
'peekref_method': 'GetReferenceCount', |
|
50 |
}, |
|
51 |
'::ns3::PacketMetadata': { |
|
52 |
'ignore': None, |
|
53 |
}, |
|
54 |
'::ns3::AttributeChecker': { |
|
55 |
'automatic_type_narrowing': 'true', |
|
56 |
'allow_subclassing': 'false', |
|
57 |
}, |
|
58 |
'::ns3::AttributeValue': { |
|
59 |
'automatic_type_narrowing': 'true', |
|
60 |
'allow_subclassing': 'false', |
|
61 |
}, |
|
62 |
'ns3::RandomVariable::RandomVariable(ns3::RandomVariableBase const & variable) [constructor]': { |
|
63 |
'ignore': None, |
|
64 |
}, |
|
65 |
'ns3::RandomVariableBase * ns3::RandomVariable::Peek() const [member function]': { |
|
66 |
'ignore': None, |
|
67 |
}, |
|
68 |
'void ns3::RandomVariable::GetSeed(uint32_t * seed) const [member function]': { |
|
69 |
'params': {'seed':{'direction':'out', |
|
70 |
'array_length':'6'}} |
|
71 |
}, |
|
72 |
'bool ns3::TypeId::LookupAttributeByName(std::string name, ns3::TypeId::AttributeInfo * info) const [member function]': { |
|
73 |
'params': {'info':{'transfer_ownership': 'false'}} |
|
74 |
}, |
|
75 |
'static bool ns3::TypeId::LookupByNameFailSafe(std::string name, ns3::TypeId * tid) [member function]': { |
|
76 |
'params': {'tid': {'transfer_ownership': 'false'}} |
|
77 |
}, |
|
78 |
'bool ns3::TraceSourceAccessor::ConnectWithoutContext(ns3::ObjectBase * obj, ns3::CallbackBase const & cb) const [member function]': { |
|
79 |
'params': {'obj': {'transfer_ownership':'false'}} |
|
80 |
}, |
|
81 |
'bool ns3::TraceSourceAccessor::Connect(ns3::ObjectBase * obj, std::string context, ns3::CallbackBase const & cb) const [member function]': { |
|
82 |
'params': {'obj': {'transfer_ownership':'false'}} |
|
83 |
}, |
|
84 |
'bool ns3::TraceSourceAccessor::DisconnectWithoutContext(ns3::ObjectBase * obj, ns3::CallbackBase const & cb) const [member function]': { |
|
85 |
'params': {'obj': {'transfer_ownership':'false'}} |
|
86 |
}, |
|
87 |
'bool ns3::TraceSourceAccessor::Disconnect(ns3::ObjectBase * obj, std::string context, ns3::CallbackBase const & cb) const [member function]': { |
|
88 |
'params': {'obj': {'transfer_ownership':'false'}} |
|
89 |
}, |
|
90 |
'bool ns3::AttributeAccessor::Set(ns3::ObjectBase * object, ns3::AttributeValue const & value) const [member function]': { |
|
91 |
'params': {'object': {'transfer_ownership':'false'}} |
|
92 |
}, |
|
93 |
'ns3::EmpiricalVariable::EmpiricalVariable(ns3::RandomVariableBase const & variable) [constructor]': { |
|
94 |
'ignore': None |
|
95 |
}, |
|
96 |
'static ns3::AttributeList * ns3::AttributeList::GetGlobal() [member function]': { |
|
97 |
'caller_owns_return': 'false' |
|
98 |
}, |
|
99 |
'void ns3::CommandLine::Parse(int argc, char * * argv) const [member function]': { |
|
100 |
'ignore': None # manually wrapped |
|
101 |
}, |
|
102 |
'extern void ns3::PythonCompleteConstruct(ns3::Ptr<ns3::Object> object, ns3::TypeId typeId, ns3::AttributeList const & attributes) [free function]': { |
|
103 |
'ignore': None # used transparently by, should not be wrapped |
|
104 |
}, |
|
105 |
} |
|
106 |
||
107 |
def get_ns3_relative_path(path): |
|
108 |
l = [] |
|
109 |
head = path |
|
110 |
while head: |
|
111 |
head, tail = os.path.split(head) |
|
112 |
if tail == 'ns3': |
|
113 |
return os.path.join(*l) |
|
114 |
l.insert(0, tail) |
|
115 |
raise AssertionError("is the path %r inside ns3?!" % path) |
|
116 |
||
117 |
||
118 |
def pre_scan_hook(dummy_module_parser, |
|
119 |
pygccxml_definition, |
|
120 |
global_annotations, |
|
121 |
parameter_annotations): |
|
122 |
ns3_header = get_ns3_relative_path(pygccxml_definition.location.file_name) |
|
123 |
||
124 |
## Note: we don't include line numbers in the comments because |
|
125 |
## those numbers are very likely to change frequently, which would |
|
126 |
## cause needless changes, since the generated python files are |
|
127 |
## kept under version control. |
|
128 |
||
129 |
#global_annotations['pygen_comment'] = "%s:%i: %s" % \ |
|
130 |
# (ns3_header, pygccxml_definition.location.line, pygccxml_definition) |
|
131 |
global_annotations['pygen_comment'] = "%s: %s" % \ |
|
132 |
(ns3_header, pygccxml_definition) |
|
133 |
||
134 |
||
135 |
## handle ns3::Object::GetObject (left to its own devices, |
|
136 |
## pybindgen will generate a mangled name containing the template |
|
137 |
## argument type name). |
|
138 |
if isinstance(pygccxml_definition, member_function_t) \ |
|
139 |
and pygccxml_definition.parent.name == 'Object' \ |
|
140 |
and pygccxml_definition.name == 'GetObject': |
|
141 |
template_args = templates.args(pygccxml_definition.demangled_name) |
|
142 |
if template_args == ['ns3::Object']: |
|
143 |
global_annotations['template_instance_names'] = 'ns3::Object=>GetObject' |
|
144 |
||
145 |
## Don't wrap Simulator::Schedule* (manually wrapped) |
|
146 |
if isinstance(pygccxml_definition, member_function_t) \ |
|
147 |
and pygccxml_definition.parent.name == 'Simulator' \ |
|
148 |
and pygccxml_definition.name.startswith('Schedule'): |
|
149 |
global_annotations['ignore'] = None |
|
150 |
||
3511
eb17407147c3
Python: re-scan API; unblock python threads during Simulator::Run; new pybindgen brings improved thread safety for virtual methods.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3484
diff
changeset
|
151 |
# unblock python threads for Simulator::Run |
eb17407147c3
Python: re-scan API; unblock python threads during Simulator::Run; new pybindgen brings improved thread safety for virtual methods.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3484
diff
changeset
|
152 |
if isinstance(pygccxml_definition, member_function_t) \ |
eb17407147c3
Python: re-scan API; unblock python threads during Simulator::Run; new pybindgen brings improved thread safety for virtual methods.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3484
diff
changeset
|
153 |
and pygccxml_definition.parent.name == 'Simulator' \ |
eb17407147c3
Python: re-scan API; unblock python threads during Simulator::Run; new pybindgen brings improved thread safety for virtual methods.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3484
diff
changeset
|
154 |
and pygccxml_definition.name == 'Run': |
eb17407147c3
Python: re-scan API; unblock python threads during Simulator::Run; new pybindgen brings improved thread safety for virtual methods.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3484
diff
changeset
|
155 |
global_annotations['unblock_threads'] = True |
eb17407147c3
Python: re-scan API; unblock python threads during Simulator::Run; new pybindgen brings improved thread safety for virtual methods.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3484
diff
changeset
|
156 |
|
eb17407147c3
Python: re-scan API; unblock python threads during Simulator::Run; new pybindgen brings improved thread safety for virtual methods.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3484
diff
changeset
|
157 |
|
3408 | 158 |
## classes |
159 |
if isinstance(pygccxml_definition, class_t): |
|
160 |
# no need for helper classes to allow subclassing in Python, I think... |
|
161 |
if pygccxml_definition.name.endswith('Helper'): |
|
162 |
global_annotations['allow_subclassing'] = 'false' |
|
163 |
||
164 |
if pygccxml_definition.decl_string.startswith('::ns3::Callback<'): |
|
165 |
# manually handled in ns3modulegen_core_customizations.py |
|
166 |
global_annotations['ignore'] = None |
|
167 |
return |
|
168 |
||
3537
ab1673dcdf58
Python: ignore TracedCallback classes for the time being: they were never usable and were triggering pybindgen generate code compilation problems.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3511
diff
changeset
|
169 |
if pygccxml_definition.decl_string.startswith('::ns3::TracedCallback<'): |
ab1673dcdf58
Python: ignore TracedCallback classes for the time being: they were never usable and were triggering pybindgen generate code compilation problems.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3511
diff
changeset
|
170 |
global_annotations['ignore'] = None |
ab1673dcdf58
Python: ignore TracedCallback classes for the time being: they were never usable and were triggering pybindgen generate code compilation problems.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3511
diff
changeset
|
171 |
return |
ab1673dcdf58
Python: ignore TracedCallback classes for the time being: they were never usable and were triggering pybindgen generate code compilation problems.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3511
diff
changeset
|
172 |
|
3408 | 173 |
if pygccxml_definition.decl_string.startswith('::ns3::Ptr<'): |
174 |
# handled by pybindgen "type transformation" |
|
175 |
global_annotations['ignore'] = None |
|
176 |
return |
|
177 |
||
178 |
# table driven class customization |
|
179 |
try: |
|
180 |
annotations = type_annotations[pygccxml_definition.decl_string] |
|
181 |
except KeyError: |
|
182 |
pass |
|
183 |
else: |
|
184 |
global_annotations.update(annotations) |
|
185 |
||
186 |
## free functions |
|
187 |
if isinstance(pygccxml_definition, free_function_t): |
|
188 |
if pygccxml_definition.name == 'PeekPointer': |
|
189 |
global_annotations['ignore'] = None |
|
190 |
return |
|
191 |
||
192 |
## table driven methods/constructors/functions customization |
|
193 |
if isinstance(pygccxml_definition, (free_function_t, member_function_t, constructor_t)): |
|
194 |
try: |
|
195 |
annotations = type_annotations[str(pygccxml_definition)] |
|
196 |
except KeyError: |
|
197 |
pass |
|
198 |
else: |
|
199 |
for key,value in annotations.items(): |
|
200 |
if key == 'params': |
|
201 |
parameter_annotations.update (value) |
|
202 |
del annotations['params'] |
|
203 |
global_annotations.update(annotations) |
|
204 |
||
205 |
||
206 |
# def post_scan_hook(dummy_module_parser, dummy_pygccxml_definition, pybindgen_wrapper): |
|
207 |
# ## classes |
|
208 |
# if isinstance(pybindgen_wrapper, CppClass): |
|
209 |
# if pybindgen_wrapper.name.endswith('Checker'): |
|
210 |
# print >> sys.stderr, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", pybindgen_wrapper |
|
211 |
# #pybindgen_wrapper.set_instance_creation_function(AttributeChecker_instance_creation_function) |
|
212 |
||
213 |
||
214 |
def scan_callback_classes(module_parser, callback_classes_file): |
|
215 |
callback_classes_file.write("callback_classes = [\n") |
|
216 |
for cls in module_parser.module_namespace.classes(function=module_parser.location_filter, |
|
217 |
recursive=False): |
|
218 |
if not cls.name.startswith("Callback<"): |
|
219 |
continue |
|
220 |
assert templates.is_instantiation(cls.decl_string), "%s is not a template instantiation" % cls |
|
221 |
dummy_cls_name, template_parameters = templates.split(cls.decl_string) |
|
222 |
callback_classes_file.write(" %r,\n" % template_parameters) |
|
223 |
callback_classes_file.write("]\n") |
|
224 |
||
225 |
||
226 |
class MyPygenClassifier(PygenClassifier): |
|
227 |
def __init__(self, headers_map): |
|
228 |
self.headers_map = headers_map |
|
229 |
||
230 |
def classify(self, pygccxml_definition): |
|
231 |
name = os.path.basename(pygccxml_definition.location.file_name) |
|
232 |
try: |
|
233 |
return self.headers_map[name] |
|
234 |
except KeyError: |
|
235 |
return '__main__' |
|
236 |
||
237 |
||
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
|
238 |
def ns3_module_scan(top_builddir, pygen_file_name, everything_h): |
3408 | 239 |
|
240 |
ns3_modules = eval(sys.stdin.read()) |
|
241 |
||
242 |
## do a topological sort on the modules graph |
|
243 |
from topsort import topsort |
|
244 |
graph = [] |
|
245 |
for ns3_module_name, (ns3_module_deps, dummy) in ns3_modules.iteritems(): |
|
246 |
for dep in ns3_module_deps: |
|
247 |
graph.append((dep, ns3_module_name)) |
|
248 |
sorted_ns3_modules = topsort(graph) |
|
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
|
249 |
#print >> sys.stderr, "******* topological sort: ", sorted_ns3_modules |
3408 | 250 |
|
251 |
sections = [PygenSection('__main__', FileCodeSink(open(pygen_file_name, "wt")))] |
|
252 |
headers_map = {} # header_name -> section_name |
|
253 |
for ns3_module in sorted_ns3_modules: |
|
254 |
section_name = "ns3_module_%s" % ns3_module.replace('-', '_') |
|
255 |
file_name = os.path.join(os.path.dirname(pygen_file_name), "%s.py" % section_name) |
|
256 |
sections.append(PygenSection(section_name, FileCodeSink(open(file_name, "wt")), |
|
257 |
section_name + "__local")) |
|
258 |
for header in ns3_modules[ns3_module][1]: |
|
259 |
headers_map[header] = section_name |
|
260 |
||
261 |
module_parser = ModuleParser('ns3', 'ns3') |
|
262 |
||
263 |
module_parser.add_pre_scan_hook(pre_scan_hook) |
|
264 |
#module_parser.add_post_scan_hook(post_scan_hook) |
|
3474
dac9146297c8
Define NS3_ASSERT_ENABLE and NS3_LOG_ENABLE, to make sure we pick up all definitions (such as the logging APIs)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3467
diff
changeset
|
265 |
|
dac9146297c8
Define NS3_ASSERT_ENABLE and NS3_LOG_ENABLE, to make sure we pick up all definitions (such as the logging APIs)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3467
diff
changeset
|
266 |
gccxml_options = dict( |
dac9146297c8
Define NS3_ASSERT_ENABLE and NS3_LOG_ENABLE, to make sure we pick up all definitions (such as the logging APIs)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3467
diff
changeset
|
267 |
include_paths=[top_builddir], |
dac9146297c8
Define NS3_ASSERT_ENABLE and NS3_LOG_ENABLE, to make sure we pick up all definitions (such as the logging APIs)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3467
diff
changeset
|
268 |
define_symbols={ |
3484
ebdeeb7cd463
When python scanning API, remove again the NS3_LOG_ENABLE define; it is no longer needed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3474
diff
changeset
|
269 |
#'NS3_ASSERT_ENABLE': None, |
ebdeeb7cd463
When python scanning API, remove again the NS3_LOG_ENABLE define; it is no longer needed.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3474
diff
changeset
|
270 |
#'NS3_LOG_ENABLE': None, |
3474
dac9146297c8
Define NS3_ASSERT_ENABLE and NS3_LOG_ENABLE, to make sure we pick up all definitions (such as the logging APIs)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3467
diff
changeset
|
271 |
} |
dac9146297c8
Define NS3_ASSERT_ENABLE and NS3_LOG_ENABLE, to make sure we pick up all definitions (such as the logging APIs)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3467
diff
changeset
|
272 |
) |
dac9146297c8
Define NS3_ASSERT_ENABLE and NS3_LOG_ENABLE, to make sure we pick up all definitions (such as the logging APIs)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3467
diff
changeset
|
273 |
|
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
|
274 |
module_parser.parse_init([everything_h], |
3474
dac9146297c8
Define NS3_ASSERT_ENABLE and NS3_LOG_ENABLE, to make sure we pick up all definitions (such as the logging APIs)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3467
diff
changeset
|
275 |
None, whitelist_paths=[top_builddir, os.path.dirname(everything_h)], |
3408 | 276 |
#includes=['"ns3/everything.h"'], |
277 |
pygen_sink=sections, |
|
3474
dac9146297c8
Define NS3_ASSERT_ENABLE and NS3_LOG_ENABLE, to make sure we pick up all definitions (such as the logging APIs)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3467
diff
changeset
|
278 |
pygen_classifier=MyPygenClassifier(headers_map), |
dac9146297c8
Define NS3_ASSERT_ENABLE and NS3_LOG_ENABLE, to make sure we pick up all definitions (such as the logging APIs)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3467
diff
changeset
|
279 |
gccxml_options=gccxml_options) |
3408 | 280 |
module_parser.scan_types() |
281 |
||
282 |
callback_classes_file = open(os.path.join(os.path.dirname(pygen_file_name), "callbacks_list.py"), "wt") |
|
283 |
scan_callback_classes(module_parser, callback_classes_file) |
|
284 |
callback_classes_file.close() |
|
285 |
||
286 |
||
287 |
module_parser.scan_methods() |
|
288 |
module_parser.scan_functions() |
|
289 |
module_parser.parse_finalize() |
|
290 |
||
291 |
for section in sections: |
|
292 |
section.code_sink.file.close() |
|
293 |
||
294 |
||
295 |
||
296 |
if __name__ == '__main__': |
|
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
|
297 |
ns3_module_scan(sys.argv[1], sys.argv[3], sys.argv[2]) |
3408 | 298 |