wscript
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Wed Jun 18 15:21:08 2008 +0100 (2008-06-18)
changeset 3282 5e57c2f1cfb7
parent 3281 bc61af712d6e
child 3283 9dd59253d39d
child 3284 510fed881852
permissions -rw-r--r--
Add a WAF workaround for the 'Input line is too long.' error in win32/mingw.
gjcarneiro@537
     1
## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
gjc@647
     2
import sys
gjc@672
     3
import shlex
gjcarneiro@695
     4
import shutil
gjc@1217
     5
import types
gjc@1217
     6
import optparse
gjc@1217
     7
import os.path
gjcarneiro@537
     8
gjc@3001
     9
import pproc as subprocess
gjc@3001
    10
gjcarneiro@537
    11
import Params
gjcarneiro@537
    12
import Object
gjc@3001
    13
import ccroot
gjcarneiro@537
    14
gjcarneiro@537
    15
Params.g_autoconfig = 1
gjcarneiro@537
    16
gjc@2881
    17
gjcarneiro@537
    18
# the following two variables are used by the target "waf dist"
gjcarneiro@694
    19
VERSION = file("VERSION").read().strip()
gjcarneiro@694
    20
APPNAME = 'ns'
gjcarneiro@537
    21
gjcarneiro@537
    22
# these variables are mandatory ('/' are converted automatically)
gjcarneiro@537
    23
srcdir = '.'
gjcarneiro@537
    24
blddir = 'build'
gjcarneiro@537
    25
gjc@2881
    26
#
gjc@2881
    27
# The directory in which the tarball of the reference traces lives.  This is
gjc@2881
    28
# used if Mercurial is not on the system.
gjc@2881
    29
#
gjc@2881
    30
REGRESSION_TRACES_URL = "http://www.nsnam.org/releases/"
gjc@2881
    31
gjc@2881
    32
#
gjc@2881
    33
# The name of the tarball to find the reference traces in if there is no
gjc@2881
    34
# mercurial on the system.  It is expected to be created using tar -cjf and
gjc@2881
    35
# will be extracted using tar -xjf
gjc@2881
    36
#
craigdo@3280
    37
REGRESSION_TRACES_TAR_NAME  = "ns-3.1-RC1-ref-traces.tar.bz2"
gjc@2881
    38
gjc@2881
    39
#
gjc@2881
    40
# The path to the Mercurial repository used to find the reference traces if
gjc@2881
    41
# we find "hg" on the system.  We expect that the repository will be named
gjc@2881
    42
# identically to refDirName below
gjc@2881
    43
#
gjc@2881
    44
REGRESSION_TRACES_REPO = "http://code.nsnam.org/"
gjc@2881
    45
gjc@2881
    46
#
gjc@2881
    47
# The local directory name (relative to the 'regression' dir) into
gjc@2881
    48
# which the reference traces will go in either case (net or hg).
gjc@2881
    49
#
craigdo@3281
    50
REGRESSION_TRACES_DIR_NAME = "ns-3-dev-ref-traces"
gjc@2881
    51
gjc@2881
    52
gjc@1531
    53
def dist_hook():
gjc@2886
    54
    import tarfile
gjc@921
    55
    shutil.rmtree("doc/html", True)
gjc@921
    56
    shutil.rmtree("doc/latex", True)
gjcarneiro@537
    57
gjc@2886
    58
    ## Create a tar.bz2 file with the traces
gjc@2886
    59
    traces_dir = os.path.join("regression", "ns-3-dev-ref-traces")
gjc@2886
    60
    if not os.path.isdir(traces_dir):
gjc@2886
    61
        Params.warning("Not creating traces archive: the %s directory does not exist" % traces_dir)
gjc@2886
    62
    else:
gjc@2886
    63
        tar = tarfile.open(os.path.join("..", "ns-%s-ref-traces.tar.bz2" % VERSION), 'w:bz2')
gjc@2886
    64
        tar.add(traces_dir, "ns-3-dev-ref-traces")
gjc@2886
    65
        tar.close()
gjc@2886
    66
        ## Now remove it; we do not ship the traces with the main tarball...
gjc@2886
    67
        shutil.rmtree(traces_dir, True)
gjc@2886
    68
gjc@2886
    69
gjcarneiro@537
    70
def set_options(opt):
gjc@787
    71
gjc@787
    72
    def debug_option_callback(option, opt, value, parser):
gjc@787
    73
        if value == 'debug':
gjc@787
    74
            setattr(parser.values, option.dest, 'ultradebug')
gjc@787
    75
        elif value == 'optimized':
gjc@787
    76
            setattr(parser.values, option.dest, 'optimized')
gjc@787
    77
        else:
gjc@787
    78
            raise optparse.OptionValueError("allowed --debug-level values"
gjc@787
    79
                                            " are debug, optimized.")
gjc@787
    80
gjc@787
    81
    opt.add_option('-d', '--debug-level',
gjc@787
    82
                   action='callback',
gjc@1425
    83
                   type="string", dest='debug_level', default='ultradebug',
gjc@787
    84
                   help=('Specify the debug level, does nothing if CFLAGS is set'
gjc@787
    85
                         ' in the environment. [Allowed Values: debug, optimized].'
gjc@787
    86
                         ' WARNING: this option only has effect '
gjc@787
    87
                         'with the configure command.'),
gjc@787
    88
                   callback=debug_option_callback)
gjc@787
    89
    
gjcarneiro@537
    90
    # options provided by the modules
gjc@642
    91
    opt.tool_options('compiler_cxx')
gjcarneiro@537
    92
craigdo@2846
    93
    opt.add_option('--cwd',
craigdo@2846
    94
                   help=('Set the working directory for a program.'),
craigdo@2846
    95
                   action="store", type="string", default=None,
craigdo@2846
    96
                   dest='cwd_launch')
craigdo@2846
    97
gjcarneiro@537
    98
    opt.add_option('--enable-gcov',
gjc@787
    99
                   help=('Enable code coverage analysis.'
gjc@787
   100
                         ' WARNING: this option only has effect '
gjc@787
   101
                         'with the configure command.'),
gjcarneiro@537
   102
                   action="store_true", default=False,
gjcarneiro@537
   103
                   dest='enable_gcov')
gjcarneiro@537
   104
gjc@2866
   105
    opt.add_option('--no-task-lines',
gjc@2866
   106
                   help=("Don't print task lines, i.e. messages saying which tasks are being executed by WAF."
gjc@2866
   107
                         "  Coupled with a single -v will cause WAF to output only the executed commands,"
gjc@2866
   108
                         " just like 'make' does by default."),
gjc@2866
   109
                   action="store_true", default=False,
gjc@2866
   110
                   dest='no_task_lines')
gjc@2866
   111
gjcarneiro@537
   112
    opt.add_option('--lcov-report',
gjcarneiro@537
   113
                   help=('Generate a code coverage report '
gjcarneiro@537
   114
                         '(use this option at build time, not in configure)'),
gjcarneiro@537
   115
                   action="store_true", default=False,
gjcarneiro@537
   116
                   dest='lcov_report')
gjcarneiro@537
   117
gjcarneiro@537
   118
    opt.add_option('--doxygen',
gjcarneiro@537
   119
                   help=('Run doxygen to generate html documentation from source comments'),
gjcarneiro@537
   120
                   action="store_true", default=False,
gjcarneiro@537
   121
                   dest='doxygen')
gjcarneiro@537
   122
gjc@672
   123
    opt.add_option('--run',
gjc@935
   124
                   help=('Run a locally built program; argument can be a program name,'
gjc@935
   125
                         ' or a command starting with the program name.'),
gjc@672
   126
                   type="string", default='', dest='run')
gjc@935
   127
    opt.add_option('--command-template',
gjc@935
   128
                   help=('Template of the command used to run the program given by --run;'
gjc@935
   129
                         ' It should be a shell command string containing %s inside,'
gjc@935
   130
                         ' which will be replaced by the actual program.'),
gjc@935
   131
                   type="string", default=None, dest='command_template')
gjc@2207
   132
    opt.add_option('--valgrind',
gjc@2207
   133
                   help=('Change the default command template to run programs and unit tests with valgrind'),
gjc@2207
   134
                   action="store_true", default=False,
gjc@2207
   135
                   dest='valgrind')
gjc@672
   136
    opt.add_option('--shell',
gjc@672
   137
                   help=('Run a shell with an environment suitably modified to run locally built programs'),
gjc@672
   138
                   action="store_true", default=False,
gjc@672
   139
                   dest='shell')
gjc@672
   140
gjc@2881
   141
    opt.add_option('--regression',
gjc@2881
   142
                   help=("Enable regression testing; only used for the 'check' target"),
gjc@2881
   143
                   default=False, dest='regression', action="store_true")
gjc@2881
   144
    opt.add_option('--regression-generate',
gjc@2884
   145
                   help=("Generate new regression test traces."),
gjc@2881
   146
                   default=False, dest='regression_generate', action="store_true")
gjc@2881
   147
    opt.add_option('--regression-tests',
gjc@2881
   148
                   help=('For regression testing, only run/generate the indicated regression tests, '
gjc@2881
   149
                         'specified as a comma separated list of test names'),
gjc@2881
   150
                   dest='regression_tests', type="string")
gjc@2881
   151
gjcarneiro@537
   152
    # options provided in a script in a subdirectory named "src"
gjcarneiro@537
   153
    opt.sub_options('src')
gjcarneiro@537
   154
gjcarneiro@537
   155
gjcarneiro@537
   156
def configure(conf):
gjc@1531
   157
    conf.check_tool('compiler_cxx')
gjcarneiro@537
   158
gjcarneiro@537
   159
    # create the second environment, set the variant and set its name
gjcarneiro@537
   160
    variant_env = conf.env.copy()
gjc@787
   161
    debug_level = Params.g_options.debug_level.lower()
gjc@787
   162
    if debug_level == 'ultradebug':
gjc@787
   163
        variant_name = 'debug'
gjc@787
   164
    else:
gjc@787
   165
        variant_name = debug_level
gjcarneiro@537
   166
gjc@1532
   167
    variant_env['INCLUDEDIR'] = os.path.join(variant_env['PREFIX'], 'include')
gjc@1532
   168
gjcarneiro@537
   169
    if Params.g_options.enable_gcov:
gjcarneiro@537
   170
        variant_name += '-gcov'
gjcarneiro@537
   171
        variant_env.append_value('CCFLAGS', '-fprofile-arcs')
gjcarneiro@537
   172
        variant_env.append_value('CCFLAGS', '-ftest-coverage')
gjcarneiro@537
   173
        variant_env.append_value('CXXFLAGS', '-fprofile-arcs')
gjcarneiro@537
   174
        variant_env.append_value('CXXFLAGS', '-ftest-coverage')
gjcarneiro@537
   175
        variant_env.append_value('LINKFLAGS', '-fprofile-arcs')
gjcarneiro@537
   176
    
gjcarneiro@537
   177
    conf.env['NS3_ACTIVE_VARIANT'] = variant_name
gjcarneiro@537
   178
    variant_env['NS3_ACTIVE_VARIANT'] = variant_name
gjcarneiro@537
   179
    variant_env.set_variant(variant_name)
gjcarneiro@537
   180
    conf.set_env_name(variant_name, variant_env)
gjcarneiro@537
   181
    conf.setenv(variant_name)
gjcarneiro@537
   182
gjcarneiro@537
   183
    variant_env.append_value('CXXDEFINES', 'RUN_SELF_TESTS')
gjc@786
   184
    
gjc@924
   185
    if (os.path.basename(conf.env['CXX']).startswith("g++")
gjc@924
   186
        and 'CXXFLAGS' not in os.environ):
gjc@1424
   187
        variant_env.append_value('CXXFLAGS', ['-Werror'])
gjc@786
   188
gjcarneiro@537
   189
    if 'debug' in Params.g_options.debug_level.lower():
gjcarneiro@537
   190
        variant_env.append_value('CXXDEFINES', 'NS3_ASSERT_ENABLE')
craigdo@1506
   191
        variant_env.append_value('CXXDEFINES', 'NS3_LOG_ENABLE')
gjcarneiro@537
   192
gjc@923
   193
    ## In optimized builds we still want debugging symbols, e.g. for
gjc@923
   194
    ## profiling, and at least partially usable stack traces.
gjc@924
   195
    if ('optimized' in Params.g_options.debug_level.lower() 
gjc@924
   196
        and 'CXXFLAGS' not in os.environ):
gjc@923
   197
        for flag in variant_env['CXXFLAGS_DEBUG']:
gjc@923
   198
            ## this probably doesn't work for MSVC
gjc@923
   199
            if flag.startswith('-g'):
gjc@923
   200
                variant_env.append_value('CXXFLAGS', flag)
gjc@923
   201
gjc@1426
   202
    ## in optimized builds, replace -O2 with -O3
gjc@1426
   203
    if 'optimized' in Params.g_options.debug_level.lower():
gjc@1426
   204
        lst = variant_env['CXXFLAGS']
gjc@1426
   205
        for i, flag in enumerate(lst):
gjc@1426
   206
            if flag == '-O2':
gjc@1426
   207
                lst[i] = '-O3'
gjc@1426
   208
gjc@647
   209
    if sys.platform == 'win32':
gjc@786
   210
        if os.path.basename(conf.env['CXX']).startswith("g++"):
gjc@786
   211
            variant_env.append_value("LINKFLAGS", "-Wl,--enable-runtime-pseudo-reloc")
gjc@647
   212
gjcarneiro@537
   213
    conf.sub_config('src')
gjc@1664
   214
    conf.sub_config('utils')
gjcarneiro@537
   215
gjc@1880
   216
    if Params.g_options.enable_modules:
gjc@1880
   217
        conf.env['NS3_ENABLED_MODULES'] = ['ns3-'+mod for mod in
gjc@1880
   218
                                           Params.g_options.enable_modules.split(',')]
gjc@1880
   219
gjc@3189
   220
    ## we cannot run regression tests without diff
gjc@3189
   221
    conf.find_program('diff', var='DIFF')
gjc@3189
   222
gjcarneiro@537
   223
gjc@1217
   224
def create_ns3_program(bld, name, dependencies=('simulator',)):
gjc@1217
   225
    program = bld.create_obj('cpp', 'program')
gjc@3001
   226
    program.is_ns3_program = True
gjc@1217
   227
    program.name = name
gjc@1217
   228
    program.target = program.name
gjc@1220
   229
    program.uselib_local = 'ns3'
gjc@1858
   230
    program.ns3_module_dependencies = ['ns3-'+dep for dep in dependencies]
gjc@1217
   231
    return program
gjc@1217
   232
gjc@3275
   233
def add_scratch_programs(bld):
gjc@3275
   234
    all_modules = [mod[len("ns3-"):] for mod in bld.env()['NS3_MODULES']]
gjc@3275
   235
    for filename in os.listdir("scratch"):
gjc@3275
   236
        if os.path.isdir(os.path.join("scratch", filename)):
gjc@3275
   237
            obj = bld.create_ns3_program(filename, all_modules)
gjc@3275
   238
            obj.path = obj.path.find_dir('scratch')
gjc@3275
   239
            obj.find_sources_in_dirs(filename)
gjc@3275
   240
            obj.target = os.path.join(filename, filename)
gjc@3275
   241
        elif filename.endswith(".cc"):
gjc@3275
   242
            name = filename[:-len(".cc")]
gjc@3275
   243
            obj = bld.create_ns3_program(name, all_modules)
gjc@3275
   244
            obj.source = "scratch/%s" % filename
gjc@3275
   245
            obj.target = "scratch/%s" % name
gjc@1217
   246
gjc@3282
   247
gjc@3282
   248
##
gjc@3282
   249
## This replacement spawn function increases the maximum command line length to 32k
gjc@3282
   250
##
gjc@3282
   251
def _exec_command_interact_win32(s):
gjc@3282
   252
    if Params.g_verbose:
gjc@3282
   253
        print s
gjc@3282
   254
    startupinfo = subprocess.STARTUPINFO()
gjc@3282
   255
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
gjc@3282
   256
    proc = subprocess.Popen(s, shell=False, startupinfo=startupinfo)
gjc@3282
   257
    stat = proc.wait()
gjc@3282
   258
    if stat & 0xff:
gjc@3282
   259
        return stat | 0x80
gjc@3282
   260
    return stat >> 8
gjc@3282
   261
gjc@3282
   262
gjcarneiro@537
   263
def build(bld):
gjc@2866
   264
    if Params.g_options.no_task_lines:
gjc@2866
   265
        import Runner
gjc@2866
   266
        def null_printout(s):
gjc@2866
   267
            pass
gjc@2866
   268
        Runner.printout = null_printout
gjc@2866
   269
gjc@3282
   270
    if sys.platform == 'win32':
gjc@3282
   271
        import Runner
gjc@3282
   272
        Runner.exec_command = _exec_command_interact_win32
gjc@3282
   273
gjc@1307
   274
    Params.g_cwd_launch = Params.g_build.m_curdirnode.abspath()
gjc@1217
   275
    bld.create_ns3_program = types.MethodType(create_ns3_program, bld)
gjcarneiro@537
   276
    variant_name = bld.env_of_name('default')['NS3_ACTIVE_VARIANT']
gjcarneiro@537
   277
    variant_env = bld.env_of_name(variant_name)
gjcarneiro@537
   278
    bld.m_allenvs['default'] = variant_env # switch to the active variant
gjc@762
   279
gjc@915
   280
    if Params.g_options.shell:
gjc@762
   281
        run_shell()
gjc@1016
   282
        raise SystemExit(0)
gjc@1016
   283
gjc@1540
   284
    if Params.g_options.doxygen:
gjc@1540
   285
        doxygen()
gjc@1540
   286
        raise SystemExit(0)
gjc@1540
   287
gjc@1016
   288
    check_shell()
gjc@762
   289
gjc@1717
   290
    if Params.g_options.doxygen:
gjc@1717
   291
        doxygen()
gjc@1717
   292
        raise SystemExit(0)
gjc@1717
   293
gjc@3002
   294
    print "Entering directory `%s'" % os.path.join(Params.g_build.m_curdirnode.abspath(), 'build')
gjcarneiro@537
   295
    # process subfolders from here
gjcarneiro@537
   296
    bld.add_subdirs('src')
craigdo@1502
   297
    bld.add_subdirs('samples utils examples tutorial')
gjcarneiro@537
   298
gjc@3275
   299
    add_scratch_programs(bld)
gjc@3275
   300
gjc@1880
   301
    ## if --enabled-modules option was given, we disable building the
gjc@1880
   302
    ## modules that were not enabled, and programs that depend on
gjc@1880
   303
    ## disabled modules.
gjc@1880
   304
    env = bld.env()
gjc@1880
   305
gjc@1880
   306
    if Params.g_options.enable_modules:
gjc@1880
   307
        Params.warning("the option --enable-modules is being applied to this build only;"
gjc@1880
   308
                       " to make it permanent it needs to be given to waf configure.")
gjc@1880
   309
        env['NS3_ENABLED_MODULES'] = ['ns3-'+mod for mod in
gjc@1880
   310
                                      Params.g_options.enable_modules.split(',')]
gjc@1880
   311
gjc@1880
   312
    if env['NS3_ENABLED_MODULES']:
gjc@1880
   313
        modules = env['NS3_ENABLED_MODULES']
gjc@1880
   314
        changed = True
gjc@1880
   315
        while changed:
gjc@1880
   316
            changed = False
gjc@1880
   317
            for module in modules:
gjc@1880
   318
                module_obj = Object.name_to_obj(module)
gjc@1880
   319
                if module_obj is None:
gjc@1880
   320
                    raise ValueError("module %s not found" % module)
gjc@1880
   321
                for dep in module_obj.add_objects:
gjc@1880
   322
                    if not dep.startswith('ns3-'):
gjc@1880
   323
                        continue
gjc@1880
   324
                    if dep not in modules:
gjc@1880
   325
                        modules.append(dep)
gjc@1880
   326
                        changed = True
gjc@1880
   327
gjc@1880
   328
        ## remove objects that depend on modules not listed
gjc@1880
   329
        for obj in list(Object.g_allobjs):
gjc@1880
   330
            if hasattr(obj, 'ns3_module_dependencies'):
gjc@1880
   331
                for dep in obj.ns3_module_dependencies:
gjc@1880
   332
                    if dep not in modules:
gjc@1880
   333
                        Object.g_allobjs.remove(obj)
gjc@1880
   334
                        break
gjc@1880
   335
            if obj.name in env['NS3_MODULES'] and obj.name not in modules:
gjc@1880
   336
                Object.g_allobjs.remove(obj)
gjc@1880
   337
gjc@1880
   338
    ## Create a single ns3 library containing all enabled modules
gjc@1880
   339
    lib = bld.create_obj('cpp', 'shlib')
gjc@1880
   340
    lib.name = 'ns3'
gjc@1880
   341
    lib.target = 'ns3'
gjc@1880
   342
    if env['NS3_ENABLED_MODULES']:
gjc@1880
   343
        lib.add_objects = list(modules)
gjc@3014
   344
        lib.uselib_local = list(modules)
gjc@1880
   345
    else:
gjc@1880
   346
        lib.add_objects = list(env['NS3_MODULES'])
gjc@3014
   347
        lib.uselib_local = list(env['NS3_MODULES'])
gjc@1880
   348
gjc@1880
   349
gjc@2207
   350
def get_command_template():
gjc@2207
   351
    if Params.g_options.valgrind:
gjc@2207
   352
        if Params.g_options.command_template:
gjc@2207
   353
            Params.fatal("Options --command-template and --valgrind are conflicting")
gjc@2881
   354
        return "valgrind --leak-check=full %s"
gjc@2207
   355
    else:
gjc@2207
   356
        return (Params.g_options.command_template or '%s')
gjcarneiro@537
   357
gjc@1536
   358
gjcarneiro@537
   359
def shutdown():
gjc@672
   360
    #import UnitTest
gjc@672
   361
    #ut = UnitTest.unit_test()
gjc@672
   362
    #ut.change_to_testfile_dir = True
gjc@672
   363
    #ut.want_to_see_test_output = True
gjc@672
   364
    #ut.want_to_see_test_error = True
gjc@672
   365
    #ut.run()
gjcarneiro@537
   366
    #ut.print_results()
gjc@3189
   367
    env = Params.g_build.env_of_name('default')
gjcarneiro@537
   368
gjc@672
   369
    if Params.g_commands['check']:
gjc@1540
   370
        _run_waf_check()
gjc@672
   371
gjc@2881
   372
    if Params.g_options.regression or Params.g_options.regression_generate:
gjc@3189
   373
        if not env['DIFF']:
gjc@3189
   374
            Params.fatal("Cannot run regression tests: the 'diff' program is not installed.")
gjc@2881
   375
        _dir = os.getcwd()
gjc@2881
   376
        os.chdir("regression")
gjc@2881
   377
        try:
gjc@2881
   378
            run_regression()
gjc@2881
   379
        finally:
gjc@2881
   380
            os.chdir(_dir)
gjc@2881
   381
gjcarneiro@537
   382
    if Params.g_options.lcov_report:
gjc@671
   383
        lcov_report()
gjcarneiro@537
   384
gjc@915
   385
    if Params.g_options.run:
gjc@2207
   386
        run_program(Params.g_options.run, get_command_template())
gjc@935
   387
        raise SystemExit(0)
gjc@935
   388
gjc@1540
   389
def _run_waf_check():
gjc@1540
   390
    ## generate the trace sources list docs
gjc@1540
   391
    env = Params.g_build.env_of_name('default')
gjc@1540
   392
    proc_env = _get_proc_env()
gjc@1858
   393
    try:
gjc@2344
   394
        program_obj = _find_program('print-introspected-doxygen', env)
gjc@1858
   395
    except ValueError: # could happen if print-introspected-doxygen is
gjc@1858
   396
                       # not built because of waf configure
gjc@1858
   397
                       # --enable-modules=xxx
gjc@1858
   398
        pass
gjc@1858
   399
    else:
gjc@3001
   400
        prog = program_obj.path.find_build(ccroot.get_target_name(program_obj)).abspath(env)
gjc@1858
   401
        out = open('doc/introspected-doxygen.h', 'w')
gjc@1858
   402
        if subprocess.Popen([prog], stdout=out, env=proc_env).wait():
gjc@1858
   403
            raise SystemExit(1)
gjc@1858
   404
        out.close()
gjc@1540
   405
gjc@2207
   406
    run_program('run-tests', get_command_template())
gjc@1536
   407
gjc@918
   408
def _find_program(program_name, env):
gjc@918
   409
    launch_dir = os.path.abspath(Params.g_cwd_launch)
gjc@916
   410
    found_programs = []
gjc@672
   411
    for obj in Object.g_allobjs:
gjc@3001
   412
        if not getattr(obj, 'is_ns3_program', False):
gjc@916
   413
            continue
gjc@918
   414
gjc@918
   415
        ## filter out programs not in the subtree starting at the launch dir
gjc@918
   416
        if not (obj.path.abspath().startswith(launch_dir)
gjc@918
   417
                or obj.path.abspath(env).startswith(launch_dir)):
gjc@918
   418
            continue
gjc@918
   419
        
gjc@916
   420
        found_programs.append(obj.target)
gjc@672
   421
        if obj.target == program_name:
gjc@672
   422
            return obj
gjc@917
   423
    raise ValueError("program '%s' not found; available programs are: %r"
gjc@916
   424
                     % (program_name, found_programs))
gjc@672
   425
gjc@1536
   426
def _get_proc_env(os_env=None):
gjc@672
   427
    env = Params.g_build.env_of_name('default')
gjc@672
   428
    if sys.platform == 'linux2':
gjc@672
   429
        pathvar = 'LD_LIBRARY_PATH'
gjc@672
   430
    elif sys.platform == 'darwin':
gjc@672
   431
        pathvar = 'DYLD_LIBRARY_PATH'
gjc@672
   432
    elif sys.platform == 'win32':
gjc@672
   433
        pathvar = 'PATH'
gjc@785
   434
    elif sys.platform == 'cygwin':
gjc@785
   435
        pathvar = 'PATH'
gjc@672
   436
    else:
gjc@672
   437
        Params.warning(("Don't know how to configure "
gjc@672
   438
                        "dynamic library path for the platform '%s'") % (sys.platform,))
gjc@672
   439
        pathvar = None
gjc@672
   440
gjc@1016
   441
    proc_env = dict(os.environ)
gjc@1016
   442
    if os_env is not None:
gjc@1016
   443
        proc_env.update(os_env)
gjc@1016
   444
gjc@672
   445
    if pathvar is not None:
gjc@1016
   446
        if pathvar in proc_env:
gjc@1016
   447
            proc_env[pathvar] = os.pathsep.join(list(env['NS3_MODULE_PATH']) + [proc_env[pathvar]])
gjc@672
   448
        else:
gjc@1016
   449
            proc_env[pathvar] = os.pathsep.join(list(env['NS3_MODULE_PATH']))
gjc@1536
   450
    return proc_env
gjc@672
   451
gjc@1536
   452
def _run_argv(argv, os_env=None):
gjc@1536
   453
    proc_env = _get_proc_env(os_env)
gjc@1016
   454
    retval = subprocess.Popen(argv, env=proc_env).wait()
gjc@672
   455
    if retval:
gjc@935
   456
        Params.fatal("Command %s exited with code %i" % (argv, retval))
gjc@672
   457
gjc@672
   458
gjc@935
   459
def run_program(program_string, command_template=None):
gjc@935
   460
    """
gjc@935
   461
    if command_template is not None, then program_string == program
gjc@935
   462
    name and argv is given by command_template with %s replaced by the
gjc@935
   463
    full path to the program.  Else, program_string is interpreted as
gjc@935
   464
    a shell command with first name being the program name.
gjc@935
   465
    """
gjc@672
   466
    env = Params.g_build.env_of_name('default')
gjc@672
   467
gjc@2345
   468
    if command_template in (None, '%s'):
gjc@935
   469
        argv = shlex.split(program_string)
gjc@935
   470
        program_name = argv[0]
gjc@672
   471
gjc@935
   472
        try:
gjc@935
   473
            program_obj = _find_program(program_name, env)
gjc@935
   474
        except ValueError, ex:
gjc@935
   475
            Params.fatal(str(ex))
gjc@672
   476
gjc@935
   477
        try:
gjc@3001
   478
            program_node = program_obj.path.find_build(ccroot.get_target_name(program_obj))
gjc@935
   479
        except AttributeError:
gjc@935
   480
            Params.fatal("%s does not appear to be a program" % (program_name,))
gjc@935
   481
gjc@935
   482
        execvec = [program_node.abspath(env)] + argv[1:]
gjc@935
   483
gjc@935
   484
    else:
gjc@935
   485
gjc@935
   486
        program_name = program_string
gjc@935
   487
        try:
gjc@935
   488
            program_obj = _find_program(program_name, env)
gjc@935
   489
        except ValueError, ex:
gjc@935
   490
            Params.fatal(str(ex))
gjc@935
   491
        try:
gjc@3001
   492
            program_node = program_obj.path.find_build(ccroot.get_target_name(program_obj))
gjc@935
   493
        except AttributeError:
gjc@935
   494
            Params.fatal("%s does not appear to be a program" % (program_name,))
gjc@935
   495
gjc@935
   496
        execvec = shlex.split(command_template % (program_node.abspath(env),))
gjc@935
   497
gjc@919
   498
    former_cwd = os.getcwd()
craigdo@2846
   499
    if (Params.g_options.cwd_launch):
craigdo@2846
   500
        os.chdir(Params.g_options.cwd_launch)
craigdo@2846
   501
    else:
craigdo@2846
   502
        os.chdir(Params.g_cwd_launch)
gjc@919
   503
    try:
gjc@935
   504
        retval = _run_argv(execvec)
gjc@919
   505
    finally:
gjc@919
   506
        os.chdir(former_cwd)
gjc@672
   507
gjc@935
   508
    return retval
gjc@935
   509
gjc@1016
   510
def check_shell():
gjc@1016
   511
    if 'NS3_MODULE_PATH' not in os.environ:
gjc@1016
   512
        return
gjc@1016
   513
    env = Params.g_build.env_of_name('default')
gjc@1016
   514
    correct_modpath = os.pathsep.join(env['NS3_MODULE_PATH'])
gjc@1016
   515
    found_modpath = os.environ['NS3_MODULE_PATH']
gjc@1016
   516
    if found_modpath != correct_modpath:
gjc@1016
   517
        msg = ("Detected shell (waf --shell) with incorrect configuration\n"
gjc@1016
   518
               "=========================================================\n"
gjc@1016
   519
               "Possible reasons for this problem:\n"
gjc@1016
   520
               "  1. You switched to another ns-3 tree from inside this shell\n"
gjc@1016
   521
               "  2. You switched ns-3 debug level (waf configure --debug)\n"
gjc@1016
   522
               "  3. You modified the list of built ns-3 modules\n"
gjc@1016
   523
               "You should correct this situation before running any program.  Possible solutions:\n"
gjc@1016
   524
               "  1. Exit this shell, and start a new one\n"
gjc@1016
   525
               "  2. Run a new nested shell")
gjc@1016
   526
        Params.fatal(msg)
gjc@1016
   527
gjc@672
   528
gjc@672
   529
def run_shell():
gjc@672
   530
    if sys.platform == 'win32':
gjc@672
   531
        shell = os.environ.get("COMSPEC", "cmd.exe")
gjc@672
   532
    else:
gjc@672
   533
        shell = os.environ.get("SHELL", "/bin/sh")
gjc@1016
   534
gjc@1016
   535
    env = Params.g_build.env_of_name('default')
gjc@1016
   536
    _run_argv([shell], {'NS3_MODULE_PATH': os.pathsep.join(env['NS3_MODULE_PATH'])})
gjc@672
   537
gjc@671
   538
def doxygen():
mathieu@1855
   539
    if not os.path.exists('doc/introspected-doxygen.h'):
mathieu@1855
   540
        Params.warning("doc/introspected-doxygen.h does not exist; run waf check to generate it.")
gjc@1536
   541
gjc@1536
   542
    ## run doxygen
gjc@671
   543
    doxygen_config = os.path.join('doc', 'doxygen.conf')
gjc@671
   544
    if subprocess.Popen(['doxygen', doxygen_config]).wait():
gjc@671
   545
        raise SystemExit(1)
gjc@671
   546
gjc@671
   547
def lcov_report():
gjc@671
   548
    env = Params.g_build.env_of_name('default')
gjc@671
   549
    variant_name = env['NS3_ACTIVE_VARIANT']
gjc@671
   550
gjc@671
   551
    if 'gcov' not in variant_name:
gjc@671
   552
        Params.fatal("project not configured for code coverage;"
gjc@671
   553
                     " reconfigure with --enable-gcov")
gjc@671
   554
gjc@671
   555
    os.chdir(blddir)
gjc@671
   556
    try:
gjc@671
   557
        lcov_report_dir = os.path.join(variant_name, 'lcov-report')
gjc@671
   558
        create_dir_command = "rm -rf " + lcov_report_dir
gjc@671
   559
        create_dir_command += " && mkdir " + lcov_report_dir + ";"
gjc@671
   560
gjc@671
   561
        if subprocess.Popen(create_dir_command, shell=True).wait():
gjcarneiro@537
   562
            raise SystemExit(1)
gjcarneiro@537
   563
gjc@671
   564
        info_file = os.path.join(lcov_report_dir, variant_name + '.info')
gjc@671
   565
        lcov_command = "../utils/lcov/lcov -c -d . -o " + info_file
gjc@671
   566
        lcov_command += " --source-dirs=" + os.getcwd()
gjc@671
   567
        lcov_command += ":" + os.path.join(
gjc@671
   568
            os.getcwd(), variant_name, 'include')
gjc@671
   569
        if subprocess.Popen(lcov_command, shell=True).wait():
gjc@671
   570
            raise SystemExit(1)
gjc@671
   571
gjc@671
   572
        genhtml_command = "../utils/lcov/genhtml -o " + lcov_report_dir
gjc@671
   573
        genhtml_command += " " + info_file
gjc@671
   574
        if subprocess.Popen(genhtml_command, shell=True).wait():
gjc@671
   575
            raise SystemExit(1)
gjc@671
   576
    finally:
gjc@671
   577
        os.chdir("..")
gjc@671
   578
gjc@2622
   579
gjc@2622
   580
gjc@2622
   581
gjc@2622
   582
##
gjc@2622
   583
## The default WAF DistDir implementation is rather slow, because it
gjc@2622
   584
## first copies everything and only later removes unwanted files and
gjc@2622
   585
## directories; this means that it needless copies the full build dir
gjc@2622
   586
## and the .hg repository tree.  Here we provide a replacement DistDir
gjc@2622
   587
## implementation that is more efficient.
gjc@2622
   588
##
gjc@2622
   589
import Scripting
gjc@2622
   590
from Scripting import g_dist_exts, g_excludes, BLDDIR
gjc@2622
   591
import Utils
gjc@2622
   592
import os
gjc@2622
   593
gjc@2622
   594
def copytree(src, dst, symlinks=False, excludes=(), build_dir=None):
gjc@2622
   595
    """Recursively copy a directory tree using copy2().
gjc@2622
   596
gjc@2622
   597
    The destination directory must not already exist.
gjc@2622
   598
    If exception(s) occur, an Error is raised with a list of reasons.
gjc@2622
   599
gjc@2622
   600
    If the optional symlinks flag is true, symbolic links in the
gjc@2622
   601
    source tree result in symbolic links in the destination tree; if
gjc@2622
   602
    it is false, the contents of the files pointed to by symbolic
gjc@2622
   603
    links are copied.
gjc@2622
   604
gjc@2622
   605
    XXX Consider this example code rather than the ultimate tool.
gjc@2622
   606
gjc@2622
   607
    Note: this is a modified version of shutil.copytree from python
gjc@2622
   608
    2.5.2 library; modified for WAF purposes to exclude dot dirs and
gjc@2622
   609
    another list of files.
gjc@2622
   610
    """
gjc@2622
   611
    names = os.listdir(src)
gjc@2622
   612
    os.makedirs(dst)
gjc@2622
   613
    errors = []
gjc@2622
   614
    for name in names:
gjc@2622
   615
        srcname = os.path.join(src, name)
gjc@2622
   616
        dstname = os.path.join(dst, name)
gjc@2622
   617
        try:
gjc@2622
   618
            if symlinks and os.path.islink(srcname):
gjc@2622
   619
                linkto = os.readlink(srcname)
gjc@2622
   620
                os.symlink(linkto, dstname)
gjc@2622
   621
            elif os.path.isdir(srcname):
gjc@2622
   622
                if name in excludes:
gjc@2622
   623
                    continue
gjc@2622
   624
                elif name.startswith('.') or name.startswith(',,') or name.startswith('++'):
gjc@2622
   625
                    continue
gjc@2622
   626
                elif name == build_dir:
gjc@2622
   627
                    continue
gjc@2622
   628
                else:
gjc@2622
   629
                    ## build_dir is not passed into the recursive
gjc@2622
   630
                    ## copytree, but that is intentional; it is a
gjc@2622
   631
                    ## directory name valid only at the top level.
gjc@2622
   632
                    copytree(srcname, dstname, symlinks, excludes)
gjc@2622
   633
            else:
gjc@2622
   634
                ends = name.endswith
gjc@2622
   635
                to_remove = False
gjc@2622
   636
                if name.startswith('.') or name.startswith('++'):
gjc@2622
   637
                    to_remove = True
gjc@2622
   638
                else:
gjc@2622
   639
                    for x in g_dist_exts:
gjc@2622
   640
                        if ends(x):
gjc@2622
   641
                            to_remove = True
gjc@2622
   642
                            break
gjc@2622
   643
                if not to_remove:
gjc@2622
   644
                    shutil.copy2(srcname, dstname)
gjc@2622
   645
            # XXX What about devices, sockets etc.?
gjc@2622
   646
        except (IOError, os.error), why:
gjc@2622
   647
            errors.append((srcname, dstname, str(why)))
gjc@2622
   648
        # catch the Error from the recursive copytree so that we can
gjc@2622
   649
        # continue with other files
gjc@2622
   650
        except shutil.Error, err:
gjc@2622
   651
            errors.extend(err.args[0])
gjc@2622
   652
    try:
gjc@2622
   653
        shutil.copystat(src, dst)
gjc@2622
   654
    except WindowsError:
gjc@2622
   655
        # can't copy file access times on Windows
gjc@2622
   656
        pass
gjc@2622
   657
    except OSError, why:
gjc@2622
   658
        errors.extend((src, dst, str(why)))
gjc@2622
   659
    if errors:
gjc@2622
   660
        raise shutil.Error, errors
gjc@2622
   661
gjc@2622
   662
gjc@2622
   663
def DistDir(appname, version):
gjc@2622
   664
    "make a distribution directory with all the sources in it"
gjc@2622
   665
    import shutil
gjc@2622
   666
gjc@2622
   667
    # Our temporary folder where to put our files
gjc@2622
   668
    TMPFOLDER=appname+'-'+version
gjc@2622
   669
gjc@2622
   670
    # Remove an old package directory
gjc@2622
   671
    if os.path.exists(TMPFOLDER): shutil.rmtree(TMPFOLDER)
gjc@2622
   672
gjc@2622
   673
    global g_dist_exts, g_excludes
gjc@2622
   674
gjc@2622
   675
    # Remove the Build dir
gjc@2622
   676
    build_dir = getattr(Utils.g_module, BLDDIR, None)
gjc@2622
   677
gjc@2622
   678
    # Copy everything into the new folder
gjc@2622
   679
    copytree('.', TMPFOLDER, excludes=g_excludes, build_dir=build_dir)
gjc@2622
   680
gjc@2622
   681
    # TODO undocumented hook
gjc@2622
   682
    dist_hook = getattr(Utils.g_module, 'dist_hook', None)
gjc@2622
   683
    if dist_hook:
gjc@2622
   684
        os.chdir(TMPFOLDER)
gjc@2622
   685
        try:
gjc@2622
   686
            dist_hook()
gjc@2622
   687
        finally:
gjc@2622
   688
            # go back to the root directory
gjc@2622
   689
            os.chdir('..')
gjc@2622
   690
    return TMPFOLDER
gjc@2622
   691
gjc@2622
   692
Scripting.DistDir = DistDir
gjc@2881
   693
gjc@2881
   694
gjc@2881
   695
gjc@2881
   696
gjc@2881
   697
### Regression testing
gjc@2881
   698
class Regression(object):
gjc@2881
   699
    def __init__(self, testdir):
gjc@2881
   700
        self.testdir = testdir
gjc@3189
   701
        env = Params.g_build.env_of_name('default')
gjc@3189
   702
        self.diff = env['DIFF']
gjc@2881
   703
gjc@2881
   704
    def run_test(self, verbose, generate, refDirName, testName):
gjc@2881
   705
        refTestDirName = os.path.join(refDirName, (testName + ".ref"))
gjc@2881
   706
gjc@2881
   707
        if not os.path.exists(refDirName):
gjc@2881
   708
            print"No reference trace repository"
gjc@2881
   709
            return 1
gjc@2881
   710
gjc@2881
   711
        if generate:
gjc@2881
   712
            if not os.path.exists(refTestDirName):
gjc@2881
   713
                print "creating new " + refTestDirName
gjc@2881
   714
                os.mkdir(refTestDirName)
gjc@2881
   715
gjc@2881
   716
            #os.system("./waf --cwd regression/" + refTestDirName +
gjc@2881
   717
            #    " --run " + testName + " > /dev/null 2>&1")
gjc@2881
   718
            Params.g_options.cwd_launch = refTestDirName
gjc@2881
   719
            run_program(testName)
gjc@2881
   720
gjc@2881
   721
            print "Remember to commit " + refTestDirName
gjc@2881
   722
            return 0
gjc@2881
   723
        else:
gjc@2881
   724
            if not os.path.exists(refTestDirName):
gjc@2881
   725
                print "Cannot locate reference traces"
gjc@2881
   726
                return 1
gjc@2881
   727
gjc@2881
   728
            shutil.rmtree("traces");
gjc@2881
   729
            os.mkdir("traces")
gjc@2881
   730
gjc@2881
   731
            #os.system("./waf --cwd regression/traces --run " +
gjc@2881
   732
            #  testName + " > /dev/null 2>&1")
gjc@2881
   733
            Params.g_options.cwd_launch = "traces"
gjc@2881
   734
            run_program(testName, command_template=get_command_template())
gjc@2881
   735
gjc@2881
   736
            if verbose:
gjc@2882
   737
                #diffCmd = "diff traces " + refTestDirName + " | head"
gjc@3189
   738
                diffCmd = subprocess.Popen([self.diff, "traces", refTestDirName],
gjc@2884
   739
                                           stderr=subprocess.PIPE, stdout=subprocess.PIPE)
gjc@2884
   740
                headCmd = subprocess.Popen("head", stdin=diffCmd.stdout)
gjc@2884
   741
                rc2 = headCmd.wait()
gjc@2884
   742
                diffCmd.stdout.close()
gjc@2882
   743
                rc1 = diffCmd.wait()
gjc@2882
   744
                rc = rc1 or rc2
gjc@2881
   745
            else:
gjc@3189
   746
                diffCmd = self.diff +" traces " + refTestDirName + \
gjc@2881
   747
                    " > /dev/null 2>&1"
gjc@2882
   748
                rc = os.system(diffCmd)
gjc@2881
   749
            if rc:
gjc@2881
   750
                print "----------"
gjc@2881
   751
                print "Traces differ in test: test-" + testName
gjc@2884
   752
                print "Reference traces in directory: regression/" + refTestDirName
gjc@2881
   753
                print "Traces in directory: traces"
gjc@2881
   754
                print "Rerun regression test as: " + \
gjc@2884
   755
                    "\"./waf --regression --regression-tests=test-" + testName + "\""
mathieu@3178
   756
                print "Then do \"diff -u regression/" + refTestDirName + " regression/traces" \
gjc@2881
   757
                    "\" for details"
gjc@2881
   758
                print "----------"
gjc@2881
   759
            return rc
gjc@2881
   760
gjc@2881
   761
def _find_tests(testdir):
gjc@2881
   762
    """Return a list of test modules in the test directory
gjc@2881
   763
gjc@2881
   764
    Arguments:
gjc@2881
   765
    testdir -- the directory to look in for tests
gjc@2881
   766
    """
gjc@2881
   767
    names = os.listdir(testdir)
gjc@2881
   768
    tests = []
gjc@2881
   769
    for name in names:
gjc@2881
   770
        if name[:5] == "test-" and name[-3:] == ".py":
gjc@2881
   771
            testname = name[:-3]
gjc@2881
   772
            tests.append(testname)
gjc@2881
   773
    tests.sort()
gjc@2881
   774
    return tests
gjc@2881
   775
gjc@2881
   776
def run_regression():
gjc@2881
   777
    """Execute regression tests."""
gjc@2881
   778
gjc@2881
   779
    testdir = "tests"
gjc@2881
   780
    if not os.path.exists(testdir):
gjc@2881
   781
        print "Tests directory does not exist"
gjc@2881
   782
        sys.exit(3)
gjc@2881
   783
    
gjc@2881
   784
    sys.path.append(testdir)
gjc@2881
   785
    sys.modules['tracediff'] = Regression(testdir)
gjc@2881
   786
gjc@2881
   787
    if Params.g_options.regression_tests:
gjc@2881
   788
        tests = Params.g_options.regression_tests.split(',')
gjc@2881
   789
    else:
gjc@2881
   790
        tests = _find_tests(testdir)
gjc@2881
   791
gjc@2881
   792
    print "========== Running Regression Tests =========="
gjc@2881
   793
gjc@2881
   794
    if os.system("hg version > /dev/null 2>&1") == 0:
gjc@2881
   795
        print "Synchronizing reference traces using Mercurial."
gjc@2881
   796
        if not os.path.exists(REGRESSION_TRACES_DIR_NAME):
gjc@2881
   797
            os.system("hg clone " + REGRESSION_TRACES_REPO + REGRESSION_TRACES_DIR_NAME + " > /dev/null 2>&1")
gjc@2881
   798
        else:
gjc@2881
   799
            _dir = os.getcwd()
gjc@2881
   800
            os.chdir(REGRESSION_TRACES_DIR_NAME)
gjc@2881
   801
            try:
gjc@2990
   802
                result = os.system("hg -q pull %s && hg -q update" % (REGRESSION_TRACES_REPO + REGRESSION_TRACES_DIR_NAME))
gjc@2881
   803
            finally:
gjc@2881
   804
                os.chdir("..")
gjc@2990
   805
            if result:
gjc@2990
   806
                Params.fatal("Synchronizing reference traces using Mercurial failed.")
gjc@2881
   807
    else:
gjc@2881
   808
        print "Synchronizing reference traces from web."
gjc@2881
   809
        urllib.urlretrieve(REGRESSION_TRACES_URL + REGRESSION_TRACES_TAR_NAME, REGRESSION_TRACES_TAR_NAME)
gjc@2881
   810
        os.system("tar -xjf %s" % (REGRESSION_TRACES_TAR_NAME,))
gjc@2881
   811
gjc@2881
   812
    print "Done."
gjc@2881
   813
gjc@2881
   814
    if not os.path.exists(REGRESSION_TRACES_DIR_NAME):
gjc@2881
   815
        print "Reference traces directory does not exist"
gjc@2881
   816
        return 3
gjc@2881
   817
    
gjc@2881
   818
    bad = []
gjc@2881
   819
gjc@2881
   820
    for test in tests:
gjc@2881
   821
        result = _run_regression_test(test)
gjc@2881
   822
        if result == 0:
gjc@2881
   823
            if Params.g_options.regression_generate:
gjc@2881
   824
                print "GENERATE " + test
gjc@2881
   825
            else:
gjc@2881
   826
                print "PASS " + test
gjc@2881
   827
        else:
gjc@2881
   828
            bad.append(test)
gjc@2881
   829
            print "FAIL " + test
gjc@2881
   830
gjc@2881
   831
    return len(bad) > 0
gjc@2881
   832
gjc@2881
   833
gjc@2881
   834
def _run_regression_test(test):
gjc@2881
   835
    """Run a single test.
gjc@2881
   836
gjc@2881
   837
    Arguments:
gjc@2881
   838
    test -- the name of the test
gjc@2881
   839
    """
gjc@2881
   840
    if os.path.exists("traces"):
gjc@2881
   841
        files = os.listdir("traces")
gjc@2881
   842
        for file in files:
gjc@2881
   843
            if file == '.' or file == '..':
gjc@2881
   844
                continue
gjc@2881
   845
            path = "traces" + os.sep + file
gjc@2881
   846
            os.remove(path)
gjc@2881
   847
    else:
gjc@2881
   848
        os.mkdir("traces")
gjc@2881
   849
    
gjc@2881
   850
    mod = __import__(test, globals(), locals(), [])
gjc@2881
   851
    return mod.run(verbose=(Params.g_options.verbose > 0),
gjc@2881
   852
                   generate=Params.g_options.regression_generate,
gjc@2881
   853
                   refDirName=REGRESSION_TRACES_DIR_NAME)