build: Several updates of the build scripts

- Redesign the way default compile flags are determined.

  This includes a small semantical change with flag processing:
  * there are now "general" compiler and link flags. These are added (if
    supported) even if CXXFLAGS and/or LINKFLAGS environmental variables
    are set.
  * optimization/debug-mode flags that are applied only if CXXFLAGS
    and/or LINKFLAGS environmental variables are not set.

- Fix define constant for sqlite3.

  When pkg-config detection used, it requires `global_define` flag.
  Otherwise define is applied only when the module is included in the
  build target using 'use' statement.

- Remove openssl dependency

  This dependency was redundant for a long time

Change-Id: Icf66fb5300954893cc2d07e022b8a0998ec894e5
Refs: #2209
diff --git a/.waf-tools/default-compiler-flags.py b/.waf-tools/default-compiler-flags.py
index c8b11e0..9316900 100644
--- a/.waf-tools/default-compiler-flags.py
+++ b/.waf-tools/default-compiler-flags.py
@@ -1,51 +1,58 @@
 # -*- 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 optimizations (-O0 or -Og)''')
 
 def configure(conf):
-    areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
-    defaultFlags = ['-std=c++0x', '-std=c++11',
-                    '-stdlib=libc++',   # clang on OSX < 10.9 by default uses gcc's
-                                        # libstdc++, which is not C++11 compatible
-                    '-pedantic', '-Wall']
+    cxx = conf.env['CXX_NAME'] # CXX_NAME represents generic name of the compiler
+    if cxx == 'gcc':
+        flags = GccFlags()
+    elif cxx == 'clang':
+        flags = ClangFlags()
+    else:
+        flags = CompilerFlags()
+        Logs.warn('The code has not been yet tested with %s compiler' % cxx)
 
+    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
-                         '-Wno-error=unneeded-internal-declaration', # Bug #1588
-                        ]
+        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)
 
-    # clang on OSX < 10.9 by default uses gcc's libstdc++, which is not C++11 compatible
-    conf.add_supported_linkflags(['-stdlib=libc++'])
+    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
     """
+    if len(cxxflags) == 0:
+        return
+
     self.start_msg('Checking supported CXXFLAGS')
 
     supportedFlags = []
@@ -61,6 +68,9 @@
     """
     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 = []
@@ -70,3 +80,71 @@
 
     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']
+        return flags
+
+    def getDebugFlags(self, conf):
+        flags = super(GccFlags, self).getDebugFlags(conf)
+        flags['CXXFLAGS'] += ['-Og', # gcc >= 4.8
+                              '-fdiagnostics-color', # gcc >= 4.9
+                              '-Wno-error=deprecated-register',
+                              '-Wno-error=unneeded-internal-declaration', # Bug #1588
+                             ]
+        return flags
+
+class ClangFlags(GccBasicFlags):
+    def getGeneralFlags(self, conf):
+        flags = super(ClangFlags, self).getGeneralFlags(conf)
+        flags['CXXFLAGS'] += ['-std=c++11']
+        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/openssl.py b/.waf-tools/openssl.py
deleted file mode 100644
index a6b6c76..0000000
--- a/.waf-tools/openssl.py
+++ /dev/null
@@ -1,46 +0,0 @@
-#! /usr/bin/env python
-# encoding: utf-8
-
-'''
-
-When using this tool, the wscript will look like:
-
-    def options(opt):
-        opt.tool_options('openssl')
-
-    def configure(conf):
-        conf.load('compiler_cxx openssl')
-        conf.check_openssl()
-
-    def build(bld):
-        bld(source='main.cpp', target='app', use='OPENSSL')
-
-'''
-
-from waflib import Options
-from waflib.Configure import conf
-
-@conf
-def check_openssl(self,*k,**kw):
-    root = k and k[0] or kw.get('path', None) or Options.options.with_openssl
-    mandatory = kw.get('mandatory', True)
-    var = kw.get('uselib_store', 'OPENSSL')
-
-    if root:
-        libcrypto = self.check_cxx(lib=['ssl', 'crypto'],
-                       msg='Checking for OpenSSL library',
-                       define_name='HAVE_%s' % var,
-                       uselib_store=var,
-                       mandatory=mandatory,
-                       includes="%s/include" % root,
-                       libpath="%s/lib" % root)
-    else:
-        libcrypto = self.check_cxx(lib=['ssl', 'crypto'],
-                       msg='Checking for OpenSSL library',
-                       define_name='HAVE_%s' % var,
-                       uselib_store=var,
-                       mandatory=mandatory)
-
-def options(opt):
-    opt.add_option('--with-openssl', type='string', default=None,
-                   dest='with_openssl', help='''Path to OpenSSL''')
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:
diff --git a/docs/INSTALL.rst b/docs/INSTALL.rst
index 7562249..ceb4675 100644
--- a/docs/INSTALL.rst
+++ b/docs/INSTALL.rst
@@ -29,7 +29,6 @@
 ~~~~~~~~~
 
 -  ``python`` >= 2.6
--  ``libcrypto``
 -  ``libsqlite3``
 -  ``libcrypto++``
 -  ``pkg-config``
@@ -54,7 +53,7 @@
    In a terminal, enter::
 
        sudo apt-get install build-essential
-       sudo apt-get install libssl-dev libsqlite3-dev libcrypto++-dev
+       sudo apt-get install libsqlite3-dev libcrypto++-dev
 
        # For Ubuntu 12.04
        sudo apt-get install libboost1.48-all-dev
@@ -67,7 +66,7 @@
    In a terminal, enter::
 
        sudo yum install gcc-g++ git
-       sudo yum install openssl-devel sqlite-devel cryptopp-devel boost-devel
+       sudo yum install sqlite-devel cryptopp-devel boost-devel
 
 Optional:
 ~~~~~~~~~
diff --git a/tests/unit-tests/ndebug.cpp b/tests/unit-tests/ndebug.cpp
index 8b9598c..e3e4569 100644
--- a/tests/unit-tests/ndebug.cpp
+++ b/tests/unit-tests/ndebug.cpp
@@ -30,7 +30,7 @@
 
 BOOST_AUTO_TEST_CASE(AssertFalse)
 {
-#ifndef NDN_CXX__DEBUG
+#ifndef _DEBUG
   // in release builds, assertion shouldn't execute
   BOOST_ASSERT(false);
 #endif
@@ -40,7 +40,7 @@
 {
   int a = 1;
   BOOST_ASSERT((a = 2) > 0);
-#ifdef NDN_CXX__DEBUG
+#ifdef _DEBUG
   BOOST_CHECK_EQUAL(a, 2);
 #else
   BOOST_CHECK_EQUAL(a, 1);
diff --git a/wscript b/wscript
index ddb2cf9..45dbef1 100644
--- a/wscript
+++ b/wscript
@@ -12,7 +12,7 @@
 def options(opt):
     opt.load(['compiler_cxx', 'gnu_dirs', 'c_osx'])
     opt.load(['default-compiler-flags', 'coverage', 'osx-security', 'pch',
-              'boost', 'openssl', 'cryptopp', 'sqlite3',
+              'boost', 'cryptopp', 'sqlite3',
               'doxygen', 'sphinx_build', 'type_traits', 'compiler-features'],
              tooldir=['.waf-tools'])
 
@@ -39,7 +39,7 @@
 def configure(conf):
     conf.load(['compiler_cxx', 'gnu_dirs', 'c_osx',
                'default-compiler-flags', 'osx-security', 'pch',
-               'boost', 'openssl', 'cryptopp', 'sqlite3',
+               'boost', 'cryptopp', 'sqlite3',
                'doxygen', 'sphinx_build', 'type_traits', 'compiler-features'])
 
     conf.env['WITH_TESTS'] = conf.options.with_tests
@@ -55,7 +55,6 @@
 
     conf.check_osx_security(mandatory=False)
 
-    conf.check_openssl(mandatory=True)
     conf.check_sqlite3(mandatory=True)
     conf.check_cryptopp(mandatory=True, use='PTHREAD')
 
@@ -89,12 +88,6 @@
 
     conf.write_config_header('src/ndn-cxx-config.hpp', define_prefix='NDN_CXX_')
 
-    # disable assertions in release builds
-    # This must appear after write_config_header, because otherwise all projects
-    # using a ndn-cxx release build would be compiled without assertions.
-    if not conf.options.debug:
-        conf.define('NDEBUG', 1)
-
 def build(bld):
     version(bld)
 
@@ -121,7 +114,7 @@
         source=bld.path.ant_glob('src/**/*.cpp',
                                  excl=['src/**/*-osx.cpp', 'src/**/*-sqlite3.cpp']),
         headers='src/common-pch.hpp',
-        use='version BOOST OPENSSL CRYPTOPP SQLITE3 RT PIC PTHREAD',
+        use='version BOOST CRYPTOPP SQLITE3 RT PIC PTHREAD',
         includes=". src",
         export_includes="src",
         install_path='${LIBDIR}',