1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/create-module.py Thu Oct 06 18:03:59 2011 +0100
1.3 @@ -0,0 +1,207 @@
1.4 +#! /usr/bin/env python
1.5 +import sys
1.6 +from optparse import OptionParser
1.7 +import os
1.8 +
1.9 +
1.10 +WSCRIPT_TEMPLATE = '''# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
1.11 +
1.12 +# def options(opt):
1.13 +# pass
1.14 +
1.15 +# def configure(conf):
1.16 +# conf.check_nonfatal(header_name='stdint.h', define_name='HAVE_STDINT_H')
1.17 +
1.18 +def build(bld):
1.19 + module = bld.create_ns3_module(%(MODULE)r, ['core'])
1.20 + module.source = [
1.21 + 'model/%(MODULE)s.cc',
1.22 + 'helper/%(MODULE)s-helper.cc',
1.23 + ]
1.24 +
1.25 + headers = bld.new_task_gen(features=['ns3header'])
1.26 + headers.module = %(MODULE)r
1.27 + headers.source = [
1.28 + 'model/%(MODULE)s.h',
1.29 + 'helper/%(MODULE)s-helper.h',
1.30 + ]
1.31 +
1.32 + if bld.env.ENABLE_EXAMPLES:
1.33 + bld.add_subdirs('examples')
1.34 +
1.35 + # bld.ns3_python_bindings()
1.36 +
1.37 +'''
1.38 +
1.39 +
1.40 +
1.41 +MODEL_CC_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1.42 +
1.43 +#include "%(MODULE)s.h"
1.44 +
1.45 +namespace ns3 {
1.46 +
1.47 +/* ... */
1.48 +
1.49 +
1.50 +}
1.51 +
1.52 +'''
1.53 +
1.54 +
1.55 +
1.56 +MODEL_H_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1.57 +#ifndef %(INCLUDE_GUARD)s
1.58 +#define %(INCLUDE_GUARD)s
1.59 +
1.60 +namespace ns3 {
1.61 +
1.62 +/* ... */
1.63 +
1.64 +}
1.65 +
1.66 +#endif /* %(INCLUDE_GUARD)s */
1.67 +
1.68 +'''
1.69 +
1.70 +
1.71 +
1.72 +HELPER_CC_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1.73 +
1.74 +#include "%(MODULE)s-helper.h"
1.75 +
1.76 +namespace ns3 {
1.77 +
1.78 +/* ... */
1.79 +
1.80 +
1.81 +}
1.82 +
1.83 +'''
1.84 +
1.85 +
1.86 +
1.87 +HELPER_H_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1.88 +#ifndef %(INCLUDE_GUARD)s
1.89 +#define %(INCLUDE_GUARD)s
1.90 +
1.91 +#include "ns3/%(MODULE)s.h"
1.92 +
1.93 +namespace ns3 {
1.94 +
1.95 +/* ... */
1.96 +
1.97 +}
1.98 +
1.99 +#endif /* %(INCLUDE_GUARD)s */
1.100 +
1.101 +'''
1.102 +
1.103 +
1.104 +EXAMPLES_WSCRIPT_TEMPLATE = '''# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
1.105 +
1.106 +def build(bld):
1.107 + obj = bld.create_ns3_program('%(MODULE)s-example', [%(MODULE)r])
1.108 + obj.source = '%(MODULE)s-example.cc'
1.109 +
1.110 +'''
1.111 +
1.112 +EXAMPLE_CC_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1.113 +
1.114 +#include "ns3/core-module.h"
1.115 +#include "ns3/%(MODULE)s-helper.h"
1.116 +
1.117 +using namespace ns3;
1.118 +
1.119 +
1.120 +int
1.121 +main (int argc, char *argv[])
1.122 +{
1.123 + bool verbose = true;
1.124 +
1.125 + CommandLine cmd;
1.126 + cmd.AddValue ("verbose", "Tell application to log if true", verbose);
1.127 +
1.128 + cmd.Parse (argc,argv);
1.129 +
1.130 + /* ... */
1.131 +
1.132 + Simulator::Run ();
1.133 + Simulator::Destroy ();
1.134 + return 0;
1.135 +}
1.136 +
1.137 +
1.138 +'''
1.139 +
1.140 +
1.141 +def main(argv):
1.142 + parser = OptionParser(usage=("Usage: %prog [options] modulename\n"
1.143 + "Utility script to create a basic template for a new ns-3 module"))
1.144 + (options, args) = parser.parse_args()
1.145 + if len(args) != 1:
1.146 + parser.print_help()
1.147 + return 1
1.148 +
1.149 + modname = args[0]
1.150 + assert os.path.sep not in modname
1.151 +
1.152 + moduledir = os.path.join(os.path.dirname(__file__), modname)
1.153 +
1.154 + if os.path.exists(moduledir):
1.155 + print >> sys.stderr, "Module %r already exists" % (modname,)
1.156 + return 2
1.157 +
1.158 + os.mkdir(moduledir)
1.159 + wscript = file(os.path.join(moduledir, "wscript"), "wt")
1.160 + wscript.write(WSCRIPT_TEMPLATE % dict(MODULE=modname))
1.161 + wscript.close()
1.162 +
1.163 +
1.164 + #
1.165 + # model
1.166 + #
1.167 + modeldir = os.path.join(moduledir, "model")
1.168 + os.mkdir(modeldir)
1.169 +
1.170 + model_cc = file(os.path.join(moduledir, "model", "%s.cc" % modname), "wt")
1.171 + model_cc.write(MODEL_CC_TEMPLATE % dict(MODULE=modname))
1.172 + model_cc.close()
1.173 +
1.174 + model_h = file(os.path.join(moduledir, "model", "%s.h" % modname), "wt")
1.175 + model_h.write(MODEL_H_TEMPLATE % dict(MODULE=modname, INCLUDE_GUARD="__%s_H__" % (modname.upper()),))
1.176 + model_h.close()
1.177 +
1.178 +
1.179 +
1.180 + #
1.181 + # helper
1.182 + #
1.183 + helperdir = os.path.join(moduledir, "helper")
1.184 + os.mkdir(helperdir)
1.185 +
1.186 + helper_cc = file(os.path.join(moduledir, "helper", "%s-helper.cc" % modname), "wt")
1.187 + helper_cc.write(HELPER_CC_TEMPLATE % dict(MODULE=modname))
1.188 + helper_cc.close()
1.189 +
1.190 + helper_h = file(os.path.join(moduledir, "helper", "%s-helper.h" % modname), "wt")
1.191 + helper_h.write(HELPER_H_TEMPLATE % dict(MODULE=modname, INCLUDE_GUARD="__%s_HELPER_H__" % (modname.upper()),))
1.192 + helper_h.close()
1.193 +
1.194 +
1.195 + examplesdir = os.path.join(moduledir, "examples")
1.196 + os.mkdir(examplesdir)
1.197 +
1.198 + examples_wscript = file(os.path.join(examplesdir, "wscript"), "wt")
1.199 + examples_wscript.write(EXAMPLES_WSCRIPT_TEMPLATE % dict(MODULE=modname))
1.200 + examples_wscript.close()
1.201 +
1.202 + example_cc = file(os.path.join(moduledir, "examples", "%s-example.cc" % modname), "wt")
1.203 + example_cc.write(EXAMPLE_CC_TEMPLATE % dict(MODULE=modname))
1.204 + example_cc.close()
1.205 +
1.206 +
1.207 + return 0
1.208 +
1.209 +if __name__ == '__main__':
1.210 + sys.exit(main(sys.argv))