bindings/python/ns3modulegen.py
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Mon, 08 Sep 2008 12:19:46 +0100
changeset 3648 f912b24ddf2d
parent 3639 8e69ebf086f1
child 3753 a84a48233eb3
permissions -rwxr-xr-x
Detect the pthread.h header file and automatically disable components that cannot build without it.
gjc@3408
     1
gjc@3408
     2
LOCAL_MODULES = [
gjc@3408
     3
    #'my_extra_api_definitions',
gjc@3408
     4
    ]
gjc@3408
     5
gjc@3408
     6
gjc@3408
     7
gjc@3408
     8
import sys
gjc@3408
     9
import os
gjc@3408
    10
gjc@3408
    11
from pybindgen import FileCodeSink, write_preamble
gjc@3408
    12
from pybindgen.module import MultiSectionFactory
gjc@3408
    13
import pybindgen.settings
gjc@3408
    14
gjc@3473
    15
from ns3modulegen_generated import module_init, register_types, register_methods, register_functions
gjc@3408
    16
import ns3modulegen_core_customizations
gjc@3408
    17
import callbacks_list
gjc@3408
    18
gjc@3408
    19
this_script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
gjc@3408
    20
gjc@3408
    21
class ErrorHandler(pybindgen.settings.ErrorHandler):
gjc@3408
    22
    def handle_error(self, wrapper, exception, traceback_):
gjc@3408
    23
        try:
gjc@3408
    24
            stack = wrapper.stack_where_defined
gjc@3408
    25
        except AttributeError:
gjc@3408
    26
            print >> sys.stderr, "??:??: %s / %r" % (wrapper, exception)
gjc@3408
    27
        else:
gjc@3408
    28
            stack = list(stack)
gjc@3408
    29
            stack.reverse()
gjc@3408
    30
            for (filename, line_number, function_name, text) in stack:
gjc@3408
    31
                file_dir = os.path.dirname(os.path.abspath(filename))
gjc@3408
    32
                if file_dir == this_script_dir:
gjc@3408
    33
                    print >> sys.stderr, "%s:%i: %r" % (os.path.join("..", "bindings", "python", os.path.basename(filename)),
gjc@3408
    34
                                                        line_number, exception)
gjc@3408
    35
                    break
gjc@3408
    36
        return True
gjc@3408
    37
pybindgen.settings.error_handler = ErrorHandler()
gjc@3408
    38
gjc@3546
    39
pybindgen.settings.wrapper_registry = pybindgen.settings.StdMapWrapperRegistry
gjc@3546
    40
gjc@3408
    41
gjc@3408
    42
class MyMultiSectionFactory(MultiSectionFactory):
gjc@3408
    43
gjc@3408
    44
    def __init__(self, main_file_name, modules):
gjc@3408
    45
        super(MyMultiSectionFactory, self).__init__()
gjc@3408
    46
        self.main_file_name = main_file_name
gjc@3408
    47
        self.main_sink = FileCodeSink(open(main_file_name, "wt"))
gjc@3408
    48
        self.header_name = "ns3module.h"
gjc@3408
    49
        header_file_name = os.path.join(os.path.dirname(self.main_file_name), self.header_name)
gjc@3408
    50
        self.header_sink = FileCodeSink(open(header_file_name, "wt"))
gjc@3538
    51
        self.section_sinks = {'__main__': self.main_sink}
gjc@3408
    52
gjc@3408
    53
        for module in modules:
gjc@3408
    54
            section_name = 'ns3_module_%s' % module.replace('-', '_')
gjc@3408
    55
            file_name = os.path.join(os.path.dirname(self.main_file_name), "%s.cc" % section_name)
gjc@3408
    56
            sink = FileCodeSink(open(file_name, "wt"))
gjc@3408
    57
            self.section_sinks[section_name] = sink            
gjc@3408
    58
gjc@3408
    59
    def get_section_code_sink(self, section_name):
gjc@3408
    60
        return self.section_sinks[section_name]
gjc@3408
    61
gjc@3408
    62
    def get_main_code_sink(self):
gjc@3408
    63
        return self.main_sink
gjc@3408
    64
gjc@3408
    65
    def get_common_header_code_sink(self):
gjc@3408
    66
        return self.header_sink
gjc@3408
    67
gjc@3408
    68
    def get_common_header_include(self):
gjc@3408
    69
        return '"%s"' % self.header_name
gjc@3408
    70
gjc@3408
    71
    def close(self):
gjc@3408
    72
        self.header_sink.file.close()
gjc@3408
    73
        self.main_sink.file.close()
gjc@3408
    74
        for sink in self.section_sinks.itervalues():
gjc@3408
    75
            sink.file.close()
gjc@3408
    76
gjc@3408
    77
gjc@3408
    78
gjc@3408
    79
def main():
gjc@3408
    80
    out = MyMultiSectionFactory(sys.argv[1], sys.argv[2:])
gjc@3408
    81
    root_module = module_init()
gjc@3409
    82
    root_module.add_include('"everything.h"')
gjc@3408
    83
gjc@3408
    84
    register_types(root_module)
gjc@3408
    85
gjc@3408
    86
    ns3modulegen_core_customizations.Simulator_customizations(root_module)
gjc@3408
    87
    ns3modulegen_core_customizations.CommandLine_customizations(root_module)
gjc@3408
    88
gjc@3408
    89
gjc@3408
    90
    for local_module in LOCAL_MODULES:
gjc@3408
    91
        mod = __import__(local_module)
gjc@3408
    92
        mod.register_types(root_module)
gjc@3408
    93
gjc@3408
    94
    ns3modulegen_core_customizations.generate_callback_classes(root_module.after_forward_declarations,
gjc@3408
    95
                                                               callbacks_list.callback_classes)
gjc@3408
    96
gjc@3408
    97
gjc@3408
    98
    register_methods(root_module)
gjc@3408
    99
gjc@3408
   100
    for local_module in LOCAL_MODULES:
gjc@3408
   101
        mod = __import__(local_module)
gjc@3408
   102
        mod.register_methods(root_module)
gjc@3408
   103
gjc@3408
   104
    ns3modulegen_core_customizations.Object_customizations(root_module)
gjc@3421
   105
    ns3modulegen_core_customizations.Attribute_customizations(root_module)
gjc@3408
   106
gjc@3408
   107
    register_functions(root_module)
gjc@3408
   108
gjc@3408
   109
    for local_module in LOCAL_MODULES:
gjc@3408
   110
        mod = __import__(local_module)
gjc@3408
   111
        mod.register_functions(root_module)
gjc@3408
   112
gjc@3639
   113
    enabled_features = os.environ['NS3_ENABLED_FEATURES'].split(',')
gjc@3420
   114
gjc@3420
   115
    # if GtkConfigStore support is disabled, disable the class wrapper
gjc@3639
   116
    if 'GtkConfigStore' not in enabled_features:
mathieu@3582
   117
        try:
mathieu@3582
   118
            root_module.classes.remove(root_module['ns3::GtkConfigStore'])
mathieu@3582
   119
        except KeyError:
mathieu@3582
   120
            pass
gjc@3420
   121
gjc@3575
   122
    # if no sqlite, the class SqliteDataOutput is disabled
gjc@3639
   123
    if 'SqliteDataOutput' not in enabled_features:
mathieu@3582
   124
        try:
mathieu@3582
   125
            root_module.classes.remove(root_module['ns3::SqliteDataOutput'])
mathieu@3582
   126
        except KeyError:
mathieu@3582
   127
            pass
gjc@3575
   128
gjc@3648
   129
    if 'Threading' not in enabled_features:
gjc@3648
   130
        for clsname in ['SystemThread', 'SystemMutex', 'SystemCondition', 'CriticalSection']:
gjc@3648
   131
            root_module.classes.remove(root_module['ns3::%s' % clsname])
gjc@3648
   132
gjc@3648
   133
    if 'RealTime' not in enabled_features:
gjc@3648
   134
        for clsname in ['WallClockSynchronizer', 'RealtimeSimulatorImpl', 'RealtimeEventLock']:
gjc@3648
   135
            root_module.classes.remove(root_module['ns3::%s' % clsname])
gjc@3648
   136
        root_module.enums.remove(root_module['ns3::RealtimeSimulatorImpl::SynchronizationMode'])
gjc@3648
   137
gjc@3408
   138
    root_module.generate(out, '_ns3')
gjc@3408
   139
gjc@3408
   140
    out.close()
gjc@3408
   141
gjc@3408
   142
if __name__ == '__main__':
gjc@3408
   143
    main()
gjc@3408
   144