src/wscript
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 4541 e943b94bf523
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
gjcarneiro@537
     1
## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
gjcarneiro@537
     2
gjc@761
     3
import os, os.path
gjcarneiro@693
     4
import shutil
gjc@1217
     5
import types
gjc@4064
     6
import warnings
gjcarneiro@693
     7
gjc@4064
     8
import TaskGen
gjc@4064
     9
import Task
gjc@4064
    10
import Options
gjc@4064
    11
import Build
gjc@4326
    12
import Utils
gjcarneiro@693
    13
gjc@969
    14
all_modules = (
gjcarneiro@600
    15
    'core',
gjcarneiro@600
    16
    'common',
gjcarneiro@600
    17
    'simulator',
gjc@1878
    18
    'contrib',
gjcarneiro@600
    19
    'node',
mathieu@3260
    20
    'internet-stack',
emmanuelle@972
    21
    'devices/point-to-point',
craigdo@1272
    22
    'devices/csma',
craigdo@3854
    23
    'devices/emu',
craigdo@3826
    24
    'devices/bridge',
craigdo@4163
    25
    'devices/tap-bridge',
gjc@4541
    26
    'devices/virtual-net-device',
craigdo@1496
    27
    'applications/onoff',
craigdo@1496
    28
    'applications/packet-sink',
craigdo@1496
    29
    'applications/udp-echo',
gjc@1716
    30
    'routing/olsr',
tomh@1120
    31
    'routing/global-routing',
tomh@4573
    32
    'routing/static-routing',
tomh@4573
    33
    'routing/list-routing',
mathieu@968
    34
    'mobility',
mathieu@1882
    35
    'devices/wifi',
mathieu@2603
    36
    'helper',
tjkopena@3570
    37
    'contrib/stats',
craigdo@3821
    38
    'applications/v4ping',
gjc@969
    39
    )
gjcarneiro@600
    40
gjcarneiro@537
    41
def set_options(opt):
gjcarneiro@537
    42
    opt.sub_options('simulator')
gjc@955
    43
gjc@955
    44
    opt.add_option('--enable-rpath',
gjc@955
    45
                   help=("Link programs with rpath"
gjc@955
    46
                         " (normally not needed, see "
gjc@955
    47
                         " --run and --shell; moreover, only works in some"
gjc@955
    48
                         " specific platforms, such as Linux and Solaris)"),
gjc@955
    49
                   action="store_true", dest='enable_rpath', default=False)
craigdo@4164
    50
    
gjc@1858
    51
    opt.add_option('--enable-modules',
gjc@1858
    52
                   help=("Build only these modules (and dependencies)"),
gjc@1858
    53
                   dest='enable_modules')
gjc@1858
    54
gjcarneiro@537
    55
def configure(conf):
gjcarneiro@537
    56
    conf.sub_config('core')
gjcarneiro@537
    57
    conf.sub_config('simulator')
craigdo@3854
    58
    conf.sub_config('devices/emu')
guangyu@4470
    59
    conf.sub_config('devices/wifi')
craigdo@4163
    60
    conf.sub_config('devices/tap-bridge')
mathieu@3063
    61
    conf.sub_config('contrib')
fw@3579
    62
    conf.sub_config('internet-stack')
gjcarneiro@537
    63
gjc@4064
    64
    blddir = os.path.abspath(os.path.join(conf.blddir, conf.env.variant()))
mathieu@3498
    65
    conf.env.append_value('NS3_MODULE_PATH', blddir)
gjc@4064
    66
    if Options.options.enable_rpath:
gjc@1306
    67
        conf.env.append_value('RPATH', '-Wl,-rpath=%s' % (os.path.join(blddir),))
gjc@761
    68
gjc@928
    69
    ## Used to link the 'run-tests' program with all of ns-3 code
gjc@928
    70
    conf.env['NS3_MODULES'] = ['ns3-' + module.split('/')[-1] for module in all_modules]
gjc@928
    71
gjc@1858
    72
gjc@1217
    73
def create_ns3_module(bld, name, dependencies=()):
gjc@4368
    74
    module = bld.new_task_gen('cxx')
gjc@1217
    75
    module.name = 'ns3-' + name
gjc@1217
    76
    module.target = module.name
gjc@1220
    77
    module.add_objects = ['ns3-' + dep for dep in dependencies]
gjc@2611
    78
    module.module_deps = list(dependencies)
mathieu@4392
    79
    if not module.env['ENABLE_STATIC_NS3']:
mathieu@4392
    80
        module.env.append_value('CXXFLAGS', module.env['shlib_CXXFLAGS'])
mathieu@4395
    81
    elif module.env['CXX_NAME'] == 'gcc' and \
mathieu@4395
    82
            os.uname()[4] == 'x86_64' and \
mathieu@4395
    83
            module.env['ENABLE_PYTHON_BINDINGS']:
mathieu@4395
    84
        # enable that flag for static builds only on x86-64 platforms
mathieu@4395
    85
        # when gcc is present and only when we want python bindings
mathieu@4395
    86
        # (it's more efficient to not use this option if we can avoid it)
mathieu@4395
    87
        module.env.append_value('CXXFLAGS', '-mcmodel=large')
mathieu@4395
    88
        
gjc@2611
    89
    module.env.append_value('CXXDEFINES', "NS3_MODULE_COMPILATION")
gjc@1217
    90
    return module
gjc@4064
    91
gjc@4064
    92
def create_obj(bld, *args):
gjc@4064
    93
    warnings.warn("(in %s) Use bld.new_task_gen(...) now, instead of bld.create_obj(...)" % str(bld.path),
gjc@4064
    94
                  DeprecationWarning, stacklevel=2)
gjc@4064
    95
    return bld.new_task_gen(*args)
gjc@955
    96
gjcarneiro@537
    97
def build(bld):
gjc@1217
    98
    bld.create_ns3_module = types.MethodType(create_ns3_module, bld)
gjc@4064
    99
    bld.create_obj = types.MethodType(create_obj, bld)
gjcarneiro@600
   100
    
gjc@969
   101
    bld.add_subdirs(list(all_modules))
gjcarneiro@693
   102
gjc@2611
   103
    for module in all_modules:
gjc@4064
   104
        modheader = bld.new_task_gen('ns3moduleheader')
gjc@2611
   105
        modheader.module = module.split('/')[-1]
gjc@2611
   106
gjc@1220
   107
gjc@4064
   108
class ns3header_taskgen(TaskGen.task_gen):
gjcarneiro@693
   109
    """A set of NS-3 header files"""
gjc@4064
   110
    COLOR = 'BLUE'
gjc@4064
   111
    def __init__(self, *args, **kwargs):
gjc@4064
   112
        super(ns3header_taskgen, self).__init__(*args, **kwargs)
gjc@4064
   113
        self.install_path = None
gjc@1877
   114
        self.sub_dir = None # if not None, header files will be published as ns3/sub_dir/file.h
gjc@2609
   115
        self.module = None # module name
gjc@2611
   116
gjcarneiro@693
   117
    def apply(self):
gjc@2609
   118
        if self.module is None:
gjc@4064
   119
            raise Utils.WafError("'module' missing on ns3headers object %s" % self)
gjc@4326
   120
        ns3_dir_node = self.bld.path.find_dir("ns3")
gjc@1877
   121
        if self.sub_dir is not None:
gjc@1877
   122
            ns3_dir_node = ns3_dir_node.find_dir(self.sub_dir)
gjcarneiro@693
   123
        for filename in self.to_list(self.source):
gjc@4064
   124
            src_node = self.path.find_resource(filename)
gjcarneiro@693
   125
            if src_node is None:
gjc@4064
   126
                raise Utils.WafError("source ns3 header file %s not found" % (filename,))
gjc@4064
   127
            dst_node = ns3_dir_node.find_or_declare(os.path.basename(filename))
gjcarneiro@693
   128
            assert dst_node is not None
gjc@4064
   129
            task = self.create_task('ns3header', self.env)
gjc@954
   130
            task.set_inputs([src_node])
gjc@954
   131
            task.set_outputs([dst_node])
gjcarneiro@693
   132
gjc@4064
   133
class ns3header_task(Task.Task):
gjc@4066
   134
    before = 'cc cxx gen_ns3_module_header_task'
gjc@4064
   135
    color = 'BLUE'
gjc@4064
   136
    def run(self):
gjc@4064
   137
        assert len(self.inputs) == len(self.outputs)
gjc@4064
   138
        inputs = [node.srcpath(self.env) for node in self.inputs]
gjc@4064
   139
        outputs = [node.bldpath(self.env) for node in self.outputs]
gjc@4064
   140
        for src, dst in zip(inputs, outputs):
gjc@4064
   141
            try:
gjc@4064
   142
                os.chmod(dst, 0600)
gjc@4064
   143
            except OSError:
gjc@4064
   144
                pass
gjc@4064
   145
            shutil.copy2(src, dst)
gjc@4064
   146
            ## make the headers in builddir read-only, to prevent
gjc@4064
   147
            ## accidental modification
gjc@4064
   148
            os.chmod(dst, 0400)
gjc@4064
   149
        return 0
gjcarneiro@693
   150
gjc@2611
   151
gjc@2611
   152
gjc@4064
   153
class gen_ns3_module_header_task(Task.Task):
gjc@4064
   154
    before = 'cc cxx'
gjc@4066
   155
    after = 'ns3header_task'
gjc@4064
   156
    color = 'BLUE'
gjc@4064
   157
    def run(self):
gjc@4064
   158
        assert len(self.outputs) == 1
gjc@4064
   159
        header_files = [os.path.basename(node.abspath(self.env)) for node in self.inputs]
gjc@4064
   160
        outfile = file(self.outputs[0].bldpath(self.env), "w")
gjc@4064
   161
        header_files.sort()
gjc@2611
   162
gjc@4064
   163
        print >> outfile, """
gjc@2611
   164
#ifdef NS3_MODULE_COMPILATION
gjc@2611
   165
# error "Do not include ns3 module aggregator headers from other modules; these are meant only for end user scripts."
gjc@2611
   166
#endif
gjc@2611
   167
gjc@2611
   168
#ifndef NS3_MODULE_%s
gjc@4064
   169
    """ % (self.module.upper().replace('-', '_'),)
gjc@2611
   170
gjc@4064
   171
    #     if self.module_deps:
gjc@4064
   172
    #         print >> outfile, "// Module dependencies:"
gjc@4064
   173
    #     for dep in self.module_deps:
gjc@4064
   174
    #         print >> outfile, "#include \"%s-module.h\"" % dep
gjc@2611
   175
gjc@4064
   176
        print >> outfile
gjc@4064
   177
        print >> outfile, "// Module headers:"
gjc@4064
   178
        for header in header_files:
gjc@4064
   179
            print >> outfile, "#include \"%s\"" % (header,)
gjc@2611
   180
gjc@4064
   181
        print >> outfile, "#endif"
gjc@2611
   182
gjc@4064
   183
        outfile.close()
gjc@4064
   184
        return 0
gjc@2611
   185
gjc@4126
   186
    def sig_explicit_deps(self):
gjc@4326
   187
        m = Utils.md5()
gjc@4126
   188
        m.update('\n'.join([node.abspath(self.env) for node in self.inputs]))
gjc@4126
   189
        return m.digest()
gjc@4126
   190
gjc@4126
   191
    def unique_id(self):
gjc@4126
   192
        try:
gjc@4126
   193
            return self.uid
gjc@4126
   194
        except AttributeError:
gjc@4126
   195
            "this is not a real hot zone, but we want to avoid surprizes here"
gjc@4326
   196
            m = Utils.md5()
gjc@4126
   197
            m.update("ns-3-module-header-%s" % self.module)
gjc@4126
   198
            self.uid = m.digest()
gjc@4126
   199
            return self.uid
gjc@4126
   200
gjc@2611
   201
gjc@4064
   202
class ns3moduleheader_taskgen(TaskGen.task_gen):
gjc@2611
   203
    """
gjc@2611
   204
    Generates a 'ns3/foo-module.h' header file that includes all
gjc@2611
   205
    public ns3 headers of a certain module.
gjc@2611
   206
    """
gjc@4064
   207
    COLOR = 'BLUE'
gjc@4064
   208
    def __init__(self, *args, **kwargs):
gjc@4064
   209
        super(ns3moduleheader_taskgen, self).__init__(*args, **kwargs)
gjc@2611
   210
gjc@2611
   211
    def apply(self):
gjc@2611
   212
        ## get all of the ns3 headers
gjc@4326
   213
        ns3_dir_node = self.bld.path.find_dir("ns3")
gjc@2611
   214
        all_headers_inputs = []
gjc@4326
   215
        for ns3headers in self.bld.all_task_gen:
gjc@3001
   216
            if isinstance(ns3headers, ns3header_taskgen):
gjc@2611
   217
                if ns3headers.module != self.module:
gjc@2611
   218
                    continue
gjc@2611
   219
                for source in ns3headers.to_list(ns3headers.source):
gjc@2611
   220
                    source = os.path.basename(source)
gjc@4064
   221
                    node = ns3_dir_node.find_or_declare(os.path.basename(source))
gjc@2611
   222
                    if node is None:
gjc@2611
   223
                        fatal("missing header file %s" % (source,))
gjc@2611
   224
                    all_headers_inputs.append(node)
gjc@4326
   225
        if not all_headers_inputs:
gjc@4326
   226
            raise Utils.WscriptError("error finding headers for module %s" % self.module)
gjc@2611
   227
        assert all_headers_inputs
gjc@4326
   228
        module_obj = self.bld.name_to_obj("ns3-" + self.module, self.env)
gjc@2611
   229
        assert module_obj is not None
gjc@4064
   230
        all_headers_outputs = [ns3_dir_node.find_or_declare("%s-module.h" % self.module)]
gjc@4064
   231
        task = self.create_task('gen_ns3_module_header', self.env)
gjc@2611
   232
        task.set_inputs(all_headers_inputs)
gjc@2611
   233
        task.set_outputs(all_headers_outputs)
gjc@2611
   234
        task.module = self.module
gjc@2611
   235
        task.module_deps = module_obj.module_deps
gjc@2611
   236
gjc@2611
   237
    def install(self):
gjc@2611
   238
        pass