refactor code

Change-Id: Ia2bc49ed8742d79000fd59f7e95fa9b957573c54
diff --git a/.waf-tools/default-compiler-flags.py b/.waf-tools/default-compiler-flags.py
index bdf3b19..759fcc8 100644
--- a/.waf-tools/default-compiler-flags.py
+++ b/.waf-tools/default-compiler-flags.py
@@ -1,54 +1,59 @@
 # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
 
-from waflib import Logs, Configure
+from waflib import Logs, Configure, Utils
 
 def options(opt):
     opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug',
-                   help='''Compile in debugging mode without all optimizations (-O0)''')
-    opt.add_option('--with-c++11', action='store_true', default=False, dest='use_cxx11',
-                   help='''Use C++ 11 features if available in the compiler''')
+                   help='''Compile in debugging mode without optimizations (-O0 or -Og)''')
 
 def configure(conf):
-    areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
-    defaultFlags = []
-
-    if conf.options.use_cxx11:
-        defaultFlags += ['-std=c++0x', '-std=c++11']
+    cxx = conf.env['CXX_NAME'] # CXX_NAME represents generic name of the compiler
+    if cxx == 'gcc':
+        flags = GccFlags()
+    elif cxx == 'clang':
+        flags = ClangFlags()
     else:
-        defaultFlags += ['-std=c++03']
+        flags = CompilerFlags()
+        Logs.warn('The code has not been yet tested with %s compiler' % cxx)
 
-    defaultFlags += ['-pedantic', '-Wall', '-Wno-long-long', '-Wno-unneeded-internal-declaration']
+    areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
 
+    # General flags will alway be applied (e.g., selecting C++11 mode)
+    generalFlags = flags.getGeneralFlags(conf)
+    conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
+    conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
+    conf.env.DEFINES += generalFlags['DEFINES']
+
+    # Debug or optimization CXXFLAGS and LINKFLAGS  will be applied only if the
+    # corresponding environment variables are not set.
+    # DEFINES will be always applied
     if conf.options.debug:
-        conf.define('_DEBUG', 1)
-        defaultFlags += ['-O0',
-                         '-Og', # gcc >= 4.8
-                         '-g3',
-                         '-fcolor-diagnostics', # clang
-                         '-fdiagnostics-color', # gcc >= 4.9
-                         '-Werror',
-                         '-Wno-error=deprecated-register',
-                         '-Wno-error=maybe-uninitialized', # Bug #1615
-                        ]
+        extraFlags = flags.getDebugFlags(conf)
+
         if areCustomCxxflagsPresent:
-            missingFlags = [x for x in defaultFlags if x not in conf.env.CXXFLAGS]
+            missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
             if len(missingFlags) > 0:
                 Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'"
                           % " ".join(conf.env.CXXFLAGS))
                 Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags))
-        else:
-            conf.add_supported_cxxflags(defaultFlags)
     else:
-        defaultFlags += ['-O2', '-g']
-        if not areCustomCxxflagsPresent:
-            conf.add_supported_cxxflags(defaultFlags)
+        extraFlags = flags.getOptimizedFlags(conf)
+
+    if not areCustomCxxflagsPresent:
+        conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
+        conf.add_supported_cxxflags(extraFlags['LINKFLAGS'])
+
+    conf.env.DEFINES += extraFlags['DEFINES']
 
 @Configure.conf
 def add_supported_cxxflags(self, cxxflags):
     """
     Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
     """
-    self.start_msg('Checking allowed flags for c++ compiler')
+    if len(cxxflags) == 0:
+        return
+
+    self.start_msg('Checking supported CXXFLAGS')
 
     supportedFlags = []
     for flag in cxxflags:
@@ -57,3 +62,92 @@
 
     self.end_msg(' '.join(supportedFlags))
     self.env.CXXFLAGS = supportedFlags + self.env.CXXFLAGS
+
+@Configure.conf
+def add_supported_linkflags(self, linkflags):
+    """
+    Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
+    """
+    if len(linkflags) == 0:
+        return
+
+    self.start_msg('Checking supported LINKFLAGS')
+
+    supportedFlags = []
+    for flag in linkflags:
+        if self.check_cxx(linkflags=['-Werror', flag], mandatory=False):
+            supportedFlags += [flag]
+
+    self.end_msg(' '.join(supportedFlags))
+    self.env.LINKFLAGS = supportedFlags + self.env.LINKFLAGS
+
+
+class CompilerFlags(object):
+    def getGeneralFlags(self, conf):
+        """Get dict {'CXXFLAGS':[...], LINKFLAGS:[...], DEFINES:[...]} that are always needed"""
+        return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
+
+    def getDebugFlags(self, conf):
+        """Get tuple {CXXFLAGS, LINKFLAGS, DEFINES} that are needed in debug mode"""
+        return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
+
+    def getOptimizedFlags(self, conf):
+        """Get tuple {CXXFLAGS, LINKFLAGS, DEFINES} that are needed in optimized mode"""
+        return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
+
+class GccBasicFlags(CompilerFlags):
+    """This class defines base flags that work for gcc and clang compiler"""
+    def getDebugFlags(self, conf):
+        flags = super(GccBasicFlags, self).getDebugFlags(conf)
+        flags['CXXFLAGS'] += ['-pedantic', '-Wall',
+                              '-O0',
+                              '-g3',
+                              '-Werror',
+                              '-Wno-error=maybe-uninitialized', # Bug #1615
+                             ]
+        return flags
+
+    def getOptimizedFlags(self, conf):
+        flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
+        flags['CXXFLAGS'] += ['-pedantic', '-Wall', '-O2', '-g']
+        return flags
+
+class GccFlags(GccBasicFlags):
+    def getGeneralFlags(self, conf):
+        flags = super(GccFlags, self).getGeneralFlags(conf)
+        version = tuple(int(i) for i in conf.env['CC_VERSION'])
+        if version < (4, 6, 0):
+            conf.fatal('The version of gcc you are using (%s) is too old.\n' %
+                       '.'.join(conf.env['CC_VERSION']) +
+                       'The minimum supported gcc version is 4.6.0.')
+        elif version < (4, 7, 0):
+            flags['CXXFLAGS'] += ['-std=c++0x']
+        else:
+            flags['CXXFLAGS'] += ['-std=c++11']
+        if version < (4, 8, 0):
+            flags['DEFINES'] += ['_GLIBCXX_USE_NANOSLEEP'] # Bug #2499
+        return flags
+
+    def getDebugFlags(self, conf):
+        flags = super(GccFlags, self).getDebugFlags(conf)
+        flags['CXXFLAGS'] += ['-Og', # gcc >= 4.8
+                              '-fdiagnostics-color', # gcc >= 4.9
+                             ]
+        return flags
+
+class ClangFlags(GccBasicFlags):
+    def getGeneralFlags(self, conf):
+        flags = super(ClangFlags, self).getGeneralFlags(conf)
+        flags['CXXFLAGS'] += ['-std=c++11',
+                              '-Wno-error=unneeded-internal-declaration', # Bug #1588
+                              '-Wno-error=deprecated-register',
+                              ]
+        if Utils.unversioned_sys_platform() == "darwin":
+            flags['CXXFLAGS'] += ['-stdlib=libc++']
+            flags['LINKFLAGS'] += ['-stdlib=libc++']
+        return flags
+
+    def getDebugFlags(self, conf):
+        flags = super(ClangFlags, self).getDebugFlags(conf)
+        flags['CXXFLAGS'] += ['-fcolor-diagnostics']
+        return flags
diff --git a/.waf-tools/sqlite3.py b/.waf-tools/sqlite3.py
index c47ae6f..3d4e46e 100644
--- a/.waf-tools/sqlite3.py
+++ b/.waf-tools/sqlite3.py
@@ -1,7 +1,7 @@
 #! /usr/bin/env python
 # encoding: utf-8
 
-from waflib import Options
+from waflib import Options, Logs
 from waflib.Configure import conf
 
 def options(opt):
@@ -26,6 +26,8 @@
         try:
             self.check_cfg(package='sqlite3',
                            args=['--cflags', '--libs'],
+                           global_define=True,
+                           define_name='HAVE_%s' % var,
                            uselib_store='SQLITE3',
                            mandatory=True)
         except: