author | Andrey Mazo <mazo@iitp.ru> |
Thu, 15 Oct 2009 14:32:19 +0400 | |
changeset 5423 | 79ff6ad1adbb |
parent 5249 | 85cde7d987ed |
child 5838 | eb5b9d295192 |
permissions | -rwxr-xr-x |
3408 | 1 |
|
2 |
LOCAL_MODULES = [ |
|
3 |
#'my_extra_api_definitions', |
|
4 |
] |
|
5 |
||
6 |
||
7 |
||
8 |
import sys |
|
9 |
import os |
|
10 |
||
5249
85cde7d987ed
Python: allow multiple api definitions directories, one per data model.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4299
diff
changeset
|
11 |
sys.path.insert(0, sys.argv[2]) |
85cde7d987ed
Python: allow multiple api definitions directories, one per data model.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4299
diff
changeset
|
12 |
|
3408 | 13 |
from pybindgen import FileCodeSink, write_preamble |
14 |
from pybindgen.module import MultiSectionFactory |
|
15 |
import pybindgen.settings |
|
16 |
||
3473
6bce86ea4778
Require new PyBindGen; make it work for Python 2.3.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3421
diff
changeset
|
17 |
from ns3modulegen_generated import module_init, register_types, register_methods, register_functions |
3408 | 18 |
import ns3modulegen_core_customizations |
19 |
import callbacks_list |
|
20 |
||
21 |
this_script_dir = os.path.dirname(os.path.abspath(sys.argv[0])) |
|
22 |
||
23 |
class ErrorHandler(pybindgen.settings.ErrorHandler): |
|
24 |
def handle_error(self, wrapper, exception, traceback_): |
|
25 |
try: |
|
26 |
stack = wrapper.stack_where_defined |
|
27 |
except AttributeError: |
|
28 |
print >> sys.stderr, "??:??: %s / %r" % (wrapper, exception) |
|
29 |
else: |
|
30 |
stack = list(stack) |
|
31 |
stack.reverse() |
|
32 |
for (filename, line_number, function_name, text) in stack: |
|
33 |
file_dir = os.path.dirname(os.path.abspath(filename)) |
|
34 |
if file_dir == this_script_dir: |
|
35 |
print >> sys.stderr, "%s:%i: %r" % (os.path.join("..", "bindings", "python", os.path.basename(filename)), |
|
36 |
line_number, exception) |
|
37 |
break |
|
38 |
return True |
|
39 |
pybindgen.settings.error_handler = ErrorHandler() |
|
40 |
||
3546
cecda7126440
New PyBindGen, fixes python wrapper identity issue.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3538
diff
changeset
|
41 |
pybindgen.settings.wrapper_registry = pybindgen.settings.StdMapWrapperRegistry |
cecda7126440
New PyBindGen, fixes python wrapper identity issue.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3538
diff
changeset
|
42 |
|
3408 | 43 |
|
44 |
class MyMultiSectionFactory(MultiSectionFactory): |
|
45 |
||
46 |
def __init__(self, main_file_name, modules): |
|
47 |
super(MyMultiSectionFactory, self).__init__() |
|
48 |
self.main_file_name = main_file_name |
|
49 |
self.main_sink = FileCodeSink(open(main_file_name, "wt")) |
|
50 |
self.header_name = "ns3module.h" |
|
51 |
header_file_name = os.path.join(os.path.dirname(self.main_file_name), self.header_name) |
|
52 |
self.header_sink = FileCodeSink(open(header_file_name, "wt")) |
|
3538
99f49426595a
Python: fix bad parameter passed into pybindgen.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3473
diff
changeset
|
53 |
self.section_sinks = {'__main__': self.main_sink} |
3408 | 54 |
|
55 |
for module in modules: |
|
56 |
section_name = 'ns3_module_%s' % module.replace('-', '_') |
|
57 |
file_name = os.path.join(os.path.dirname(self.main_file_name), "%s.cc" % section_name) |
|
58 |
sink = FileCodeSink(open(file_name, "wt")) |
|
59 |
self.section_sinks[section_name] = sink |
|
60 |
||
61 |
def get_section_code_sink(self, section_name): |
|
62 |
return self.section_sinks[section_name] |
|
63 |
||
64 |
def get_main_code_sink(self): |
|
65 |
return self.main_sink |
|
66 |
||
67 |
def get_common_header_code_sink(self): |
|
68 |
return self.header_sink |
|
69 |
||
70 |
def get_common_header_include(self): |
|
71 |
return '"%s"' % self.header_name |
|
72 |
||
73 |
def close(self): |
|
74 |
self.header_sink.file.close() |
|
75 |
self.main_sink.file.close() |
|
76 |
for sink in self.section_sinks.itervalues(): |
|
77 |
sink.file.close() |
|
78 |
||
79 |
||
80 |
||
81 |
def main(): |
|
5249
85cde7d987ed
Python: allow multiple api definitions directories, one per data model.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4299
diff
changeset
|
82 |
out = MyMultiSectionFactory(sys.argv[1], sys.argv[3:]) |
3408 | 83 |
root_module = module_init() |
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
|
84 |
root_module.add_include('"everything.h"') |
3408 | 85 |
|
86 |
register_types(root_module) |
|
87 |
||
88 |
ns3modulegen_core_customizations.Simulator_customizations(root_module) |
|
89 |
ns3modulegen_core_customizations.CommandLine_customizations(root_module) |
|
3753
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3648
diff
changeset
|
90 |
ns3modulegen_core_customizations.TypeId_customizations(root_module) |
4196
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4105
diff
changeset
|
91 |
ns3modulegen_core_customizations.add_std_ofstream(root_module) |
3408 | 92 |
|
93 |
||
94 |
for local_module in LOCAL_MODULES: |
|
95 |
mod = __import__(local_module) |
|
96 |
mod.register_types(root_module) |
|
97 |
||
98 |
ns3modulegen_core_customizations.generate_callback_classes(root_module.after_forward_declarations, |
|
99 |
callbacks_list.callback_classes) |
|
100 |
||
101 |
||
102 |
register_methods(root_module) |
|
103 |
||
104 |
for local_module in LOCAL_MODULES: |
|
105 |
mod = __import__(local_module) |
|
106 |
mod.register_methods(root_module) |
|
107 |
||
108 |
ns3modulegen_core_customizations.Object_customizations(root_module) |
|
3421
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3420
diff
changeset
|
109 |
ns3modulegen_core_customizations.Attribute_customizations(root_module) |
3408 | 110 |
|
111 |
register_functions(root_module) |
|
112 |
||
113 |
for local_module in LOCAL_MODULES: |
|
114 |
mod = __import__(local_module) |
|
115 |
mod.register_functions(root_module) |
|
116 |
||
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:
3582
diff
changeset
|
117 |
enabled_features = os.environ['NS3_ENABLED_FEATURES'].split(',') |
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:
3409
diff
changeset
|
118 |
|
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:
3409
diff
changeset
|
119 |
# if GtkConfigStore support is disabled, disable the class wrapper |
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:
3582
diff
changeset
|
120 |
if 'GtkConfigStore' not in enabled_features: |
3582
96fa2a7b5f88
catch KeyError exception to avoid failing when sqlite or gtk are not installed
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3575
diff
changeset
|
121 |
try: |
96fa2a7b5f88
catch KeyError exception to avoid failing when sqlite or gtk are not installed
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3575
diff
changeset
|
122 |
root_module.classes.remove(root_module['ns3::GtkConfigStore']) |
96fa2a7b5f88
catch KeyError exception to avoid failing when sqlite or gtk are not installed
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3575
diff
changeset
|
123 |
except KeyError: |
96fa2a7b5f88
catch KeyError exception to avoid failing when sqlite or gtk are not installed
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3575
diff
changeset
|
124 |
pass |
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:
3409
diff
changeset
|
125 |
|
3575
1f5d9b97a1a2
Fix compilation of Python bindings when libsqlite3 is not available (and so the class SqliteDataOutput is omitted).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3546
diff
changeset
|
126 |
# if no sqlite, the class SqliteDataOutput is disabled |
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:
3582
diff
changeset
|
127 |
if 'SqliteDataOutput' not in enabled_features: |
3582
96fa2a7b5f88
catch KeyError exception to avoid failing when sqlite or gtk are not installed
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3575
diff
changeset
|
128 |
try: |
96fa2a7b5f88
catch KeyError exception to avoid failing when sqlite or gtk are not installed
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3575
diff
changeset
|
129 |
root_module.classes.remove(root_module['ns3::SqliteDataOutput']) |
96fa2a7b5f88
catch KeyError exception to avoid failing when sqlite or gtk are not installed
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3575
diff
changeset
|
130 |
except KeyError: |
96fa2a7b5f88
catch KeyError exception to avoid failing when sqlite or gtk are not installed
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3575
diff
changeset
|
131 |
pass |
3575
1f5d9b97a1a2
Fix compilation of Python bindings when libsqlite3 is not available (and so the class SqliteDataOutput is omitted).
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3546
diff
changeset
|
132 |
|
3648
f912b24ddf2d
Detect the pthread.h header file and automatically disable components that cannot build without it.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3639
diff
changeset
|
133 |
if 'Threading' not in enabled_features: |
f912b24ddf2d
Detect the pthread.h header file and automatically disable components that cannot build without it.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3639
diff
changeset
|
134 |
for clsname in ['SystemThread', 'SystemMutex', 'SystemCondition', 'CriticalSection']: |
f912b24ddf2d
Detect the pthread.h header file and automatically disable components that cannot build without it.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3639
diff
changeset
|
135 |
root_module.classes.remove(root_module['ns3::%s' % clsname]) |
f912b24ddf2d
Detect the pthread.h header file and automatically disable components that cannot build without it.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3639
diff
changeset
|
136 |
|
3916
a1e72a0595a6
Disable EmuNetDevice Python bindings if the netdevice is not compiled in (bug #413)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
137 |
|
a1e72a0595a6
Disable EmuNetDevice Python bindings if the netdevice is not compiled in (bug #413)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
138 |
if 'EmuNetDevice' not in enabled_features: |
a1e72a0595a6
Disable EmuNetDevice Python bindings if the netdevice is not compiled in (bug #413)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
139 |
for clsname in ['EmuNetDevice', 'EmuHelper']: |
a1e72a0595a6
Disable EmuNetDevice Python bindings if the netdevice is not compiled in (bug #413)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
140 |
root_module.classes.remove(root_module['ns3::%s' % clsname]) |
a1e72a0595a6
Disable EmuNetDevice Python bindings if the netdevice is not compiled in (bug #413)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3753
diff
changeset
|
141 |
|
3648
f912b24ddf2d
Detect the pthread.h header file and automatically disable components that cannot build without it.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3639
diff
changeset
|
142 |
if 'RealTime' not in enabled_features: |
4090
3dda1049f0d8
RealtimeEventLock is gone
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3916
diff
changeset
|
143 |
for clsname in ['WallClockSynchronizer', 'RealtimeSimulatorImpl']: |
3648
f912b24ddf2d
Detect the pthread.h header file and automatically disable components that cannot build without it.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3639
diff
changeset
|
144 |
root_module.classes.remove(root_module['ns3::%s' % clsname]) |
f912b24ddf2d
Detect the pthread.h header file and automatically disable components that cannot build without it.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3639
diff
changeset
|
145 |
root_module.enums.remove(root_module['ns3::RealtimeSimulatorImpl::SynchronizationMode']) |
f912b24ddf2d
Detect the pthread.h header file and automatically disable components that cannot build without it.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3639
diff
changeset
|
146 |
|
4208
ef9ae8962bbb
Fix build of Python bindings on systems with no support for TabBridge
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4196
diff
changeset
|
147 |
if 'TapBridge' not in enabled_features: |
ef9ae8962bbb
Fix build of Python bindings on systems with no support for TabBridge
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4196
diff
changeset
|
148 |
for clsname in ['TapBridge', 'TapBridgeHelper']: |
ef9ae8962bbb
Fix build of Python bindings on systems with no support for TabBridge
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4196
diff
changeset
|
149 |
root_module.classes.remove(root_module['ns3::%s' % clsname]) |
4299
834a7ac30481
fix mac osx python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4208
diff
changeset
|
150 |
root_module.enums.remove(root_module['ns3::TapBridge::Mode']) |
4208
ef9ae8962bbb
Fix build of Python bindings on systems with no support for TabBridge
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4196
diff
changeset
|
151 |
|
3408 | 152 |
root_module.generate(out, '_ns3') |
153 |
||
154 |
out.close() |
|
155 |
||
156 |
if __name__ == '__main__': |
|
4105
ce8be572c4d4
Make it easy to turn on profiling of pybindgen
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4090
diff
changeset
|
157 |
if 0: |
ce8be572c4d4
Make it easy to turn on profiling of pybindgen
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4090
diff
changeset
|
158 |
try: |
ce8be572c4d4
Make it easy to turn on profiling of pybindgen
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4090
diff
changeset
|
159 |
import cProfile as profile |
ce8be572c4d4
Make it easy to turn on profiling of pybindgen
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4090
diff
changeset
|
160 |
except ImportError: |
ce8be572c4d4
Make it easy to turn on profiling of pybindgen
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4090
diff
changeset
|
161 |
main() |
ce8be572c4d4
Make it easy to turn on profiling of pybindgen
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4090
diff
changeset
|
162 |
else: |
ce8be572c4d4
Make it easy to turn on profiling of pybindgen
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4090
diff
changeset
|
163 |
print >> sys.stderr, "** running under profiler" |
ce8be572c4d4
Make it easy to turn on profiling of pybindgen
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4090
diff
changeset
|
164 |
profile.run('main()', 'ns3modulegen.pstat') |
ce8be572c4d4
Make it easy to turn on profiling of pybindgen
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4090
diff
changeset
|
165 |
else: |
ce8be572c4d4
Make it easy to turn on profiling of pybindgen
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4090
diff
changeset
|
166 |
main() |
3408 | 167 |