cleanup
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 26 Mar 2011 11:02:49 +0100
changeset 4 2254dd4d8e9e
parent 3 6c512aedca40
child 5 f4c070e26a79
cleanup
.hgignore
bake.py
bake/ModuleBuild.py
bake/Utils.py
examples/ns3/bakeconf.xml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Sat Mar 26 11:02:49 2011 +0100
@@ -0,0 +1,4 @@
+~$
+^examples/.*/source/.*
+bakefile.xml$
+.pyc$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bake.py	Sat Mar 26 11:02:49 2011 +0100
@@ -0,0 +1,8 @@
+#!/usr/bin/env python
+
+import sys
+import bake
+
+b = bake.Bake()
+
+b.main(sys.argv)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bake/ModuleBuild.py	Sat Mar 26 11:02:49 2011 +0100
@@ -0,0 +1,110 @@
+import Utils
+import os
+from Utils import ModuleAttributeBase
+
+class ModuleBuild(ModuleAttributeBase):
+    def __init__(self):
+        ModuleAttributeBase.__init__(self)
+    @classmethod
+    def create(cls, name):
+        for subclass in ModuleBuild.__subclasses__():
+            if subclass.name() == name:
+                return subclass()
+        return None
+    def build(self, logger, srcdir, blddir, installdir):
+        raise NotImplemented()
+    def clean(self, logger, srcdir, blddir):
+        raise NotImplemented()
+
+class NoneModuleBuild(ModuleBuild):
+    def __init__(self):
+        ModuleBuild.__init__(self)
+    @classmethod
+    def name(cls):
+        return 'none'
+    def build(self, logger, srcdir, blddir, installdir):
+        pass
+    def clean(self, logger, srcdir, blddir):
+        pass
+
+class Cmake(ModuleBuild):
+    def __init__(self):
+        ModuleBuild.__init__(self)
+        self.add_attribute('CC',       '', 'C compiler to use')
+        self.add_attribute('CXX',      '', 'C++ compiler to use')
+        self.add_attribute('CFLAGS',   '', 'Flags to use for C compiler')
+        self.add_attribute('CXXFLAGS', '', 'Flags to use for C++ compiler')
+        self.add_attribute('LDFLAGS',  '', 'Flags to use for Linker')
+    @classmethod
+    def name(cls):
+        return 'cmake'
+    def build(self, logger, srcdir, blddir, installdir):
+        variables = []
+        if self.attribute('CC').value != '':
+            variables.append('-DCMAKE_C_COMPILER=' + self.attribute('CC').value)
+        if self.attribute('CFLAGS').value != '':
+            variables.append('-DCMAKE_CFLAGS=' + self.attribute('CFLAGS').value)
+        if self.attribute('CXX').value != '':
+            variables.append('-DCMAKE_CXX_COMPILER=' + self.attribute('CXX').value)
+        if self.attribute('CXXFLAGS').value != '':
+            variables.append('-DCMAKE_CXXFLAGS=' + self.attribute('CXXFLAGS').value)
+        if self.attribute('LDFLAGS').value != '':
+            variables.append('-DCMAKE_EXE_LINKER_FLAGS=' + self.attribute('LDFLAGS').value)
+        Utils.run_command(['cmake', srcdir, '-DCMAKE_INSTALL_PREFIX=' + installdir] + variables, 
+                          logger,
+                          directory=blddir)
+        Utils.run_command(['make'], logger, directory = blddir)
+        Utils.run_command(['make', 'doc'], logger, directory = blddir)
+        Utils.run_command(['make', 'install'], logger, directory = blddir)
+    def clean(self, logger, srcdir, blddir):
+        pass
+
+
+class Autotools(ModuleBuild):
+    def __init__(self):
+        ModuleBuild.__init__(self)
+        self.add_attribute('CC',       '', 'C compiler to use')
+        self.add_attribute('CXX',      '', 'C++ compiler to use')
+        self.add_attribute('CFLAGS',   '', 'Flags to use for C compiler')
+        self.add_attribute('CXXFLAGS', '', 'Flags to use for C++ compiler')
+        self.add_attribute('LDFLAGS',  '', 'Flags to use for Linker')
+        self.add_attribute('blddir',   'srcdir', 
+                           'Does this module support building in blddir != srcdir ? '
+                           'Defaults to blddir == srcdir. ')
+        self.add_attribute('maintainer', 'no', 'Maintainer mode ?')
+    @classmethod
+    def name(cls):
+        return 'autotools'
+    def build(self, logger, srcdir, blddir, installdir):
+        if self.attribute('maintainer').value != 'no':
+            Utils.run_command(['autoreconf', '--install'], logger,
+                        directory = srcdir)
+        if self.attribute('blddir').value == 'srcdir':
+            blddir = srcdir
+        Utils.run_command([os.path.join(srcdir, 'configure'),
+                           '--prefix=' + installdir, 
+                           'CC=' + self.attribute('CC').value,
+                           'CXX=' + self.attribute('CXX').value,
+                           'CFLAGS=' + self.attribute('CFLAGS').value,
+                           'CXXFLAGS=' + self.attribute('CXXFLAGS').value,
+                           'LDFLAGS=' + self.attribute('LDFLAGS').value], 
+                          logger,
+                          directory = blddir)
+        Utils.run_command(['make'], logger, directory = blddir)
+        Utils.run_command(['make', 'install'], logger, directory = blddir)
+
+    def clean(self, logger, srcdir, blddir):
+        if self.attribute('blddir').value  == 'srcdir':
+            blddir = srcdir
+        if not os.path.isfile(os.path.join(blddir,'Makefile')):
+            return
+        if self.attribute('maintainer').value != 'no':
+            Utils.run_command(['make', '-k', 'maintainerclean'], logger, directory = blddir)
+        else:
+            Utils.run_command(['make', '-k', 'distclean'], logger, directory = blddir)
+        try:
+            os.remove(os.path.join(blddir, 'config.cache'))
+        except OSError:
+            pass
+            
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bake/Utils.py	Sat Mar 26 11:02:49 2011 +0100
@@ -0,0 +1,56 @@
+import subprocess
+from Exceptions import TaskError
+
+def run_command(args, logger, directory = None):
+    logger.write(str(args) + ' dir=' + str(directory) + '\n')
+    popen = subprocess.Popen(args,
+                             stdout = logger,
+                             stderr = logger,
+                             cwd = directory)
+    retcode = popen.wait()
+    if retcode != 0:
+        raise TaskError('Subprocess failed with error %d: %s' % (retcode, str(args)))
+
+def print_backtrace():
+    import sys
+    import traceback
+    trace = ""
+    exception = ""
+    exc_list = traceback.format_exception_only (sys.exc_type, sys.exc_value)
+    for entry in exc_list:
+        exception += entry
+    tb_list = traceback.format_tb(sys.exc_info()[2])
+    for entry in tb_list:
+        trace += entry
+    sys.stderr.write("%s\n%s" % (exception, trace))
+
+class ModuleAttribute:
+    def __init__(self, name, value, help, mandatory):
+        self._name = name
+        self.value = value
+        self._help = help
+        self._mandatory = mandatory
+    @property
+    def name(self):
+        return self._name
+    @property
+    def help(self):
+        return self._help
+    @property
+    def is_mandatory(self):
+        return self._mandatory
+
+class ModuleAttributeBase(object):
+    def __init__(self):
+        self._attributes = dict()
+    def add_attribute(self, name, value, help, mandatory = False):
+        assert not self._attributes.has_key(name)
+        self._attributes[name] = ModuleAttribute(name, value, help, mandatory)
+    def attributes(self):
+        return self._attributes.values()
+    def attribute(self, name):
+        if not self._attributes.has_key(name):
+            return None
+        else:
+            return self._attributes[name]
+
--- a/examples/ns3/bakeconf.xml	Sat Mar 26 10:46:08 2011 +0100
+++ b/examples/ns3/bakeconf.xml	Sat Mar 26 11:02:49 2011 +0100
@@ -1,38 +1,49 @@
- <modules>
+<modules>
 
   <module name="iproute2" version="2.6.33">
-    <source type="archive" 
-     url="http://devresources.linuxfoundation.org/dev/iproute2/download/iproute2-2.6.33.tar.bz2"/>
-    <build type="xxx"/>
+    <source type="archive">
+     <attribute name="url" value="http://devresources.linuxfoundation.org/dev/iproute2/download/iproute2-2.6.33.tar.bz2"/>
+    </source>
+    <build type="none"/>
   </module>
 
   <module name="net-next-2.6">
     <!-- url="git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next-2.6.git" -->
-    <source type="git"
-     url="file:///home/mathieu/code/ns-3-linux/net-next-2.6"
-     version="fed66381d65a35198639f564365e61a7f256bf79"/>
-    <build type="xxx"/>
+    <source type="git">
+      <attribute name="url" value="file:///home/mathieu/code/ns-3-linux/net-next-2.6"/>
+      <attribute name="revision" value="fed66381d65a35198639f564365e61a7f256bf79"/>
+    </source>
+    <build type="none"/>
   </module>
 
   <module name="ns-3-linux" version="0.1">
-    <source type="mercurial" url="http://code.nsnam.org/mathieu/ns-3-linux"/>
+    <source type="mercurial">
+      <attribute name="url" value="http://code.nsnam.org/mathieu/ns-3-linux"/>
+    </source>
     <depends_on name="net-next-2.6"/>
     <depends_on name="iproute2"/>
-    <build type="xxx"/>
+    <build type="none"/>
   </module>
 
   <module name="gdb" version="cvs">
-    <source type="cvs" root=":pserver:anoncvs:anoncvs@sourceware.org:/cvs/src" 
-            module="gdb" checkout_directory="src"/>
+    <source type="cvs">
+      <attribute name="root" value=":pserver:anoncvs:anoncvs@sourceware.org:/cvs/src"/>
+      <attribute name="module" value="gdb"/>
+      <attribute name="checkout_directory" value="src"/>
+    </source>
     <!-- <build type="autotools" blddir="srcdir"/> -->
-    <build type="autotools" blddir="any"/>
+    <build type="autotools">
+      <attribute name="blddir" value="any"/>
+    </build>
   </module>
 
   <module name="ns-3-dce" version="0.1">
-    <source type="mercurial" url="http://code.nsnam.org/mathieu/ns-3-dce"/>
+    <source type="mercurial">
+      <attribute name="url" value="http://code.nsnam.org/mathieu/ns-3-dce"/>
+    </source>
     <depends_on name="ns-3-linux" optional="True"/>
     <depends_on name="gdb" optional="True"/>
-    <build type="xxx"/>
+    <build type="none"/>
   </module>
 
 </modules>