src/create-module.py
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Thu Oct 06 18:03:59 2011 +0100 (7 months ago)
changeset 7557 81ca9a7671bb
child 7646 0c1ed7ef4ee7
permissions -rwxr-xr-x
Rescan API of spectrum module
gjc@7497
     1
#! /usr/bin/env python
gjc@7497
     2
import sys
gjc@7497
     3
from optparse import OptionParser
gjc@7497
     4
import os
gjc@7497
     5
gjc@7497
     6
gjc@7497
     7
WSCRIPT_TEMPLATE = '''# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
gjc@7497
     8
gjc@7497
     9
# def options(opt):
gjc@7497
    10
#     pass
gjc@7497
    11
gjc@7497
    12
# def configure(conf):
gjc@7497
    13
#     conf.check_nonfatal(header_name='stdint.h', define_name='HAVE_STDINT_H')
gjc@7497
    14
gjc@7497
    15
def build(bld):
gjc@7497
    16
    module = bld.create_ns3_module(%(MODULE)r, ['core'])
gjc@7497
    17
    module.source = [
gjc@7497
    18
        'model/%(MODULE)s.cc',
gjc@7497
    19
        'helper/%(MODULE)s-helper.cc',
gjc@7497
    20
        ]
gjc@7497
    21
gjc@7497
    22
    headers = bld.new_task_gen(features=['ns3header'])
gjc@7497
    23
    headers.module = %(MODULE)r
gjc@7497
    24
    headers.source = [
gjc@7497
    25
        'model/%(MODULE)s.h',
gjc@7497
    26
        'helper/%(MODULE)s-helper.h',
gjc@7497
    27
        ]
gjc@7497
    28
gjc@7497
    29
    if bld.env.ENABLE_EXAMPLES:
gjc@7497
    30
        bld.add_subdirs('examples')
gjc@7497
    31
gjc@7497
    32
    # bld.ns3_python_bindings()
gjc@7497
    33
gjc@7497
    34
'''
gjc@7497
    35
gjc@7497
    36
gjc@7497
    37
gjc@7497
    38
MODEL_CC_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
gjc@7497
    39
gjc@7497
    40
#include "%(MODULE)s.h"
gjc@7497
    41
gjc@7497
    42
namespace ns3 {
gjc@7497
    43
gjc@7497
    44
/* ... */
gjc@7497
    45
gjc@7497
    46
gjc@7497
    47
}
gjc@7497
    48
gjc@7497
    49
'''
gjc@7497
    50
gjc@7497
    51
gjc@7497
    52
gjc@7497
    53
MODEL_H_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
gjc@7497
    54
#ifndef %(INCLUDE_GUARD)s
gjc@7497
    55
#define %(INCLUDE_GUARD)s
gjc@7497
    56
gjc@7497
    57
namespace ns3 {
gjc@7497
    58
gjc@7497
    59
/* ... */
gjc@7497
    60
gjc@7497
    61
}
gjc@7497
    62
gjc@7497
    63
#endif /* %(INCLUDE_GUARD)s */
gjc@7497
    64
gjc@7497
    65
'''
gjc@7497
    66
gjc@7497
    67
gjc@7497
    68
gjc@7497
    69
HELPER_CC_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
gjc@7497
    70
gjc@7497
    71
#include "%(MODULE)s-helper.h"
gjc@7497
    72
gjc@7497
    73
namespace ns3 {
gjc@7497
    74
gjc@7497
    75
/* ... */
gjc@7497
    76
gjc@7497
    77
gjc@7497
    78
}
gjc@7497
    79
gjc@7497
    80
'''
gjc@7497
    81
gjc@7497
    82
gjc@7497
    83
gjc@7497
    84
HELPER_H_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
gjc@7497
    85
#ifndef %(INCLUDE_GUARD)s
gjc@7497
    86
#define %(INCLUDE_GUARD)s
gjc@7497
    87
gjc@7497
    88
#include "ns3/%(MODULE)s.h"
gjc@7497
    89
gjc@7497
    90
namespace ns3 {
gjc@7497
    91
gjc@7497
    92
/* ... */
gjc@7497
    93
gjc@7497
    94
}
gjc@7497
    95
gjc@7497
    96
#endif /* %(INCLUDE_GUARD)s */
gjc@7497
    97
gjc@7497
    98
'''
gjc@7497
    99
gjc@7497
   100
gjc@7497
   101
EXAMPLES_WSCRIPT_TEMPLATE = '''# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
gjc@7497
   102
gjc@7497
   103
def build(bld):
gjc@7497
   104
    obj = bld.create_ns3_program('%(MODULE)s-example', [%(MODULE)r])
gjc@7497
   105
    obj.source = '%(MODULE)s-example.cc'
gjc@7497
   106
gjc@7497
   107
'''
gjc@7497
   108
gjc@7497
   109
EXAMPLE_CC_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
gjc@7497
   110
gjc@7497
   111
#include "ns3/core-module.h"
gjc@7497
   112
#include "ns3/%(MODULE)s-helper.h"
gjc@7497
   113
gjc@7497
   114
using namespace ns3;
gjc@7497
   115
gjc@7497
   116
gjc@7497
   117
int 
gjc@7497
   118
main (int argc, char *argv[])
gjc@7497
   119
{
gjc@7497
   120
  bool verbose = true;
gjc@7497
   121
gjc@7497
   122
  CommandLine cmd;
gjc@7497
   123
  cmd.AddValue ("verbose", "Tell application to log if true", verbose);
gjc@7497
   124
gjc@7497
   125
  cmd.Parse (argc,argv);
gjc@7497
   126
gjc@7497
   127
  /* ... */
gjc@7497
   128
gjc@7497
   129
  Simulator::Run ();
gjc@7497
   130
  Simulator::Destroy ();
gjc@7497
   131
  return 0;
gjc@7497
   132
}
gjc@7497
   133
gjc@7497
   134
gjc@7497
   135
'''
gjc@7497
   136
gjc@7497
   137
gjc@7497
   138
def main(argv):
gjc@7497
   139
    parser = OptionParser(usage=("Usage: %prog [options] modulename\n"
gjc@7497
   140
                                 "Utility script to create a basic template for a new ns-3 module"))
gjc@7497
   141
    (options, args) = parser.parse_args()
gjc@7497
   142
    if len(args) != 1:
gjc@7497
   143
        parser.print_help()
gjc@7497
   144
        return 1
gjc@7497
   145
gjc@7497
   146
    modname = args[0]
gjc@7497
   147
    assert os.path.sep not in modname
gjc@7497
   148
gjc@7497
   149
    moduledir = os.path.join(os.path.dirname(__file__), modname)
gjc@7497
   150
gjc@7497
   151
    if os.path.exists(moduledir):
gjc@7497
   152
        print >> sys.stderr, "Module %r already exists" % (modname,)
gjc@7497
   153
        return 2
gjc@7497
   154
gjc@7497
   155
    os.mkdir(moduledir)
gjc@7497
   156
    wscript = file(os.path.join(moduledir, "wscript"), "wt")
gjc@7497
   157
    wscript.write(WSCRIPT_TEMPLATE % dict(MODULE=modname))
gjc@7497
   158
    wscript.close()
gjc@7497
   159
gjc@7497
   160
gjc@7497
   161
    #
gjc@7497
   162
    # model
gjc@7497
   163
    # 
gjc@7497
   164
    modeldir = os.path.join(moduledir, "model")
gjc@7497
   165
    os.mkdir(modeldir)
gjc@7497
   166
gjc@7497
   167
    model_cc = file(os.path.join(moduledir, "model", "%s.cc" % modname), "wt")
gjc@7497
   168
    model_cc.write(MODEL_CC_TEMPLATE % dict(MODULE=modname))
gjc@7497
   169
    model_cc.close()
gjc@7497
   170
gjc@7497
   171
    model_h = file(os.path.join(moduledir, "model", "%s.h" % modname), "wt")
gjc@7497
   172
    model_h.write(MODEL_H_TEMPLATE % dict(MODULE=modname, INCLUDE_GUARD="__%s_H__" % (modname.upper()),))
gjc@7497
   173
    model_h.close()
gjc@7497
   174
gjc@7497
   175
gjc@7497
   176
gjc@7497
   177
    #
gjc@7497
   178
    # helper
gjc@7497
   179
    # 
gjc@7497
   180
    helperdir = os.path.join(moduledir, "helper")
gjc@7497
   181
    os.mkdir(helperdir)
gjc@7497
   182
gjc@7497
   183
    helper_cc = file(os.path.join(moduledir, "helper", "%s-helper.cc" % modname), "wt")
gjc@7497
   184
    helper_cc.write(HELPER_CC_TEMPLATE % dict(MODULE=modname))
gjc@7497
   185
    helper_cc.close()
gjc@7497
   186
gjc@7497
   187
    helper_h = file(os.path.join(moduledir, "helper", "%s-helper.h" % modname), "wt")
gjc@7497
   188
    helper_h.write(HELPER_H_TEMPLATE % dict(MODULE=modname, INCLUDE_GUARD="__%s_HELPER_H__" % (modname.upper()),))
gjc@7497
   189
    helper_h.close()
gjc@7497
   190
gjc@7497
   191
gjc@7497
   192
    examplesdir = os.path.join(moduledir, "examples")
gjc@7497
   193
    os.mkdir(examplesdir)
gjc@7497
   194
gjc@7497
   195
    examples_wscript = file(os.path.join(examplesdir, "wscript"), "wt")
gjc@7497
   196
    examples_wscript.write(EXAMPLES_WSCRIPT_TEMPLATE % dict(MODULE=modname))
gjc@7497
   197
    examples_wscript.close()
gjc@7497
   198
gjc@7497
   199
    example_cc = file(os.path.join(moduledir, "examples", "%s-example.cc" % modname), "wt")
gjc@7497
   200
    example_cc.write(EXAMPLE_CC_TEMPLATE % dict(MODULE=modname))
gjc@7497
   201
    example_cc.close()
gjc@7497
   202
gjc@7497
   203
gjc@7497
   204
    return 0
gjc@7497
   205
gjc@7497
   206
if __name__ == '__main__':
gjc@7497
   207
    sys.exit(main(sys.argv))