bindings/python/ns3modulegen.py
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 4208 ef9ae8962bbb
permissions -rwxr-xr-x
Added tag ns-3.5 for changeset c975274c9707
     1 
     2 LOCAL_MODULES = [
     3     #'my_extra_api_definitions',
     4     ]
     5 
     6 
     7 
     8 import sys
     9 import os
    10 
    11 from pybindgen import FileCodeSink, write_preamble
    12 from pybindgen.module import MultiSectionFactory
    13 import pybindgen.settings
    14 
    15 from ns3modulegen_generated import module_init, register_types, register_methods, register_functions
    16 import ns3modulegen_core_customizations
    17 import callbacks_list
    18 
    19 this_script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
    20 
    21 class ErrorHandler(pybindgen.settings.ErrorHandler):
    22     def handle_error(self, wrapper, exception, traceback_):
    23         try:
    24             stack = wrapper.stack_where_defined
    25         except AttributeError:
    26             print >> sys.stderr, "??:??: %s / %r" % (wrapper, exception)
    27         else:
    28             stack = list(stack)
    29             stack.reverse()
    30             for (filename, line_number, function_name, text) in stack:
    31                 file_dir = os.path.dirname(os.path.abspath(filename))
    32                 if file_dir == this_script_dir:
    33                     print >> sys.stderr, "%s:%i: %r" % (os.path.join("..", "bindings", "python", os.path.basename(filename)),
    34                                                         line_number, exception)
    35                     break
    36         return True
    37 pybindgen.settings.error_handler = ErrorHandler()
    38 
    39 pybindgen.settings.wrapper_registry = pybindgen.settings.StdMapWrapperRegistry
    40 
    41 
    42 class MyMultiSectionFactory(MultiSectionFactory):
    43 
    44     def __init__(self, main_file_name, modules):
    45         super(MyMultiSectionFactory, self).__init__()
    46         self.main_file_name = main_file_name
    47         self.main_sink = FileCodeSink(open(main_file_name, "wt"))
    48         self.header_name = "ns3module.h"
    49         header_file_name = os.path.join(os.path.dirname(self.main_file_name), self.header_name)
    50         self.header_sink = FileCodeSink(open(header_file_name, "wt"))
    51         self.section_sinks = {'__main__': self.main_sink}
    52 
    53         for module in modules:
    54             section_name = 'ns3_module_%s' % module.replace('-', '_')
    55             file_name = os.path.join(os.path.dirname(self.main_file_name), "%s.cc" % section_name)
    56             sink = FileCodeSink(open(file_name, "wt"))
    57             self.section_sinks[section_name] = sink            
    58 
    59     def get_section_code_sink(self, section_name):
    60         return self.section_sinks[section_name]
    61 
    62     def get_main_code_sink(self):
    63         return self.main_sink
    64 
    65     def get_common_header_code_sink(self):
    66         return self.header_sink
    67 
    68     def get_common_header_include(self):
    69         return '"%s"' % self.header_name
    70 
    71     def close(self):
    72         self.header_sink.file.close()
    73         self.main_sink.file.close()
    74         for sink in self.section_sinks.itervalues():
    75             sink.file.close()
    76 
    77 
    78 
    79 def main():
    80     out = MyMultiSectionFactory(sys.argv[1], sys.argv[2:])
    81     root_module = module_init()
    82     root_module.add_include('"everything.h"')
    83 
    84     register_types(root_module)
    85 
    86     ns3modulegen_core_customizations.Simulator_customizations(root_module)
    87     ns3modulegen_core_customizations.CommandLine_customizations(root_module)
    88     ns3modulegen_core_customizations.TypeId_customizations(root_module)
    89     ns3modulegen_core_customizations.add_std_ofstream(root_module)
    90 
    91 
    92     for local_module in LOCAL_MODULES:
    93         mod = __import__(local_module)
    94         mod.register_types(root_module)
    95 
    96     ns3modulegen_core_customizations.generate_callback_classes(root_module.after_forward_declarations,
    97                                                                callbacks_list.callback_classes)
    98 
    99 
   100     register_methods(root_module)
   101 
   102     for local_module in LOCAL_MODULES:
   103         mod = __import__(local_module)
   104         mod.register_methods(root_module)
   105 
   106     ns3modulegen_core_customizations.Object_customizations(root_module)
   107     ns3modulegen_core_customizations.Attribute_customizations(root_module)
   108 
   109     register_functions(root_module)
   110 
   111     for local_module in LOCAL_MODULES:
   112         mod = __import__(local_module)
   113         mod.register_functions(root_module)
   114 
   115     enabled_features = os.environ['NS3_ENABLED_FEATURES'].split(',')
   116 
   117     # if GtkConfigStore support is disabled, disable the class wrapper
   118     if 'GtkConfigStore' not in enabled_features:
   119         try:
   120             root_module.classes.remove(root_module['ns3::GtkConfigStore'])
   121         except KeyError:
   122             pass
   123 
   124     # if no sqlite, the class SqliteDataOutput is disabled
   125     if 'SqliteDataOutput' not in enabled_features:
   126         try:
   127             root_module.classes.remove(root_module['ns3::SqliteDataOutput'])
   128         except KeyError:
   129             pass
   130 
   131     if 'Threading' not in enabled_features:
   132         for clsname in ['SystemThread', 'SystemMutex', 'SystemCondition', 'CriticalSection']:
   133             root_module.classes.remove(root_module['ns3::%s' % clsname])
   134 
   135 
   136     if 'EmuNetDevice' not in enabled_features:
   137         for clsname in ['EmuNetDevice', 'EmuHelper']:
   138             root_module.classes.remove(root_module['ns3::%s' % clsname])
   139 
   140     if 'RealTime' not in enabled_features:
   141         for clsname in ['WallClockSynchronizer', 'RealtimeSimulatorImpl']:
   142             root_module.classes.remove(root_module['ns3::%s' % clsname])
   143         root_module.enums.remove(root_module['ns3::RealtimeSimulatorImpl::SynchronizationMode'])
   144 
   145     if 'TapBridge' not in enabled_features:
   146         for clsname in ['TapBridge', 'TapBridgeHelper']:
   147             root_module.classes.remove(root_module['ns3::%s' % clsname])
   148         root_module.enums.remove(root_module['ns3::TapBridge::Mode'])
   149 
   150     root_module.generate(out, '_ns3')
   151 
   152     out.close()
   153 
   154 if __name__ == '__main__':
   155     if 0:
   156         try:
   157             import cProfile as profile
   158         except ImportError:
   159             main()
   160         else:
   161             print >> sys.stderr, "** running under profiler"
   162             profile.run('main()', 'ns3modulegen.pstat')
   163     else:
   164         main()
   165