build: Reorganizing wscript into a set smaller function-specific scripts

This commit also fixes #1401 (path to sqlite3 can be explicitly
specified and the script will try to detect sqlite3 without the use of
pkg-config)

Change-Id: Ic91ec968410705b19d2df443523026c4e0e95c6b
diff --git a/wscript b/wscript
index b920afc..142f03a 100644
--- a/wscript
+++ b/wscript
@@ -1,20 +1,17 @@
 # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
-#
-# Copyright (c) 2014, Regents of the University of California
-#
-# GPL 3.0 license, see the COPYING.md file for more information
 
 VERSION = '0.4.0'
 APPNAME = "ndn-cpp-dev"
 PACKAGE_BUGREPORT = "http://redmine.named-data.net/projects/ndn-cpp-dev"
 PACKAGE_URL = "https://github.com/named-data/ndn-cpp-dev"
 
-from waflib import Logs, Utils, Task, TaskGen
-from waflib.Tools import c_preproc
+from waflib import Logs, Utils
 
 def options(opt):
-    opt.load('compiler_cxx gnu_dirs c_osx')
-    opt.load('boost doxygen openssl cryptopp coverage default-compiler-flags',
+    opt.load(['compiler_cxx', 'gnu_dirs', 'c_osx'])
+    opt.load(['default-compiler-flags', 'coverage', 'osx-security', 'pch',
+              'boost', 'openssl', 'cryptopp', 'sqlite3',
+              'doxygen', 'sphinx_build'],
              tooldir=['.waf-tools'])
 
     opt = opt.add_option_group('Library Options')
@@ -34,65 +31,30 @@
                    help='''Disable filesystem locking in sqlite3 database '''
                         '''(use unix-dot locking mechanism instead). '''
                         '''This option may be necessary if home directory is hosted on NFS.''')
-    opt.add_option('--with-pch', action='store_true', default=False, dest='with_pch',
-                   help='''Try to use precompiled header to speed up compilation '''
-                        '''(only gcc and clang)''')
     opt.add_option('--without-osx-keychain', action='store_false', default=True,
                    dest='with_osx_keychain',
                    help='''On Darwin, do not use OSX keychain as a default TPM''')
 
 def configure(conf):
-    conf.load("compiler_cxx boost gnu_dirs c_osx openssl cryptopp")
-    try: conf.load("doxygen")
-    except: pass
+    conf.load(['compiler_cxx', 'gnu_dirs', 'c_osx',
+               'default-compiler-flags', 'osx-security', 'pch',
+               'boost', 'openssl', 'cryptopp', 'sqlite3',
+               'doxygen', 'sphinx_build'])
 
-    if conf.options.with_tests:
-        conf.env['WITH_TESTS'] = True
+    conf.env['WITH_TESTS'] = conf.options.with_tests
+    conf.env['WITH_TOOLS'] = conf.options.with_tools
 
-    if conf.options.with_tools:
-        conf.env['WITH_TOOLS'] = True
+    conf.check_osx_security(mandatory=False)
 
-    conf.check_openssl()
-
-    conf.load('default-compiler-flags')
-
-    if Utils.unversioned_sys_platform() == "darwin":
-        try:
-            codeFragment='''
-#include <CoreFoundation/CoreFoundation.h>
-#include <Security/Security.h>
-#include <Security/SecRandom.h>
-#include <CoreServices/CoreServices.h>
-#include <Security/SecDigestTransform.h>
-
-int main(int argc, char **argv) {
-    (void)argc; (void)argv;
-    return 0;
-}
-'''
-            conf.check_cxx(framework_name='CoreFoundation', uselib_store='OSX_COREFOUNDATION',
-                           mandatory=True)
-            conf.check_cxx(framework_name='CoreServices', uselib_store='OSX_CORESERVICES',
-                           mandatory=True)
-            conf.check_cxx(framework_name='Security', uselib_store='OSX_SECURITY',
-                           define_name='HAVE_SECURITY', use="OSX_COREFOUNDATION",
-                           fragment=codeFragment, mandatory=True)
-            conf.define('HAVE_OSX_SECURITY', 1)
-            conf.env['HAVE_OSX_SECURITY'] = True
-        except:
-            Logs.warn("Compiling on OSX, but CoreFoundation, CoreServices, or Security framework is not functional.")
-            Logs.warn("The frameworks are known to work only with Apple-specific compilers: llvm-gcc-4.2 or clang")
-
-    conf.check_cfg(package='sqlite3', args=['--cflags', '--libs'], uselib_store='SQLITE3',
-                   mandatory=True)
+    conf.check_openssl(mandatory=True)
+    conf.check_sqlite3(mandatory=True)
+    conf.check_cryptopp(mandatory=True)
 
     if conf.options.log4cxx:
         conf.check_cfg(package='liblog4cxx', args=['--cflags', '--libs'], uselib_store='LOG4CXX',
                        mandatory=True)
         conf.define("HAVE_LOG4CXX", 1)
 
-    conf.check_cryptopp(path=conf.options.cryptopp_dir, mandatory=True)
-
     if conf.options.use_cxx11:
         conf.check(msg='Checking for type std::shared_ptr',
                    type_name="std::shared_ptr<int>", header_name="memory",
@@ -122,8 +84,6 @@
     if not conf.options.with_sqlite_locking:
         conf.define('DISABLE_SQLITE3_FS_LOCKING', 1)
 
-    conf.env['WITH_PCH'] = conf.options.with_pch
-
     if conf.env['HAVE_OSX_SECURITY']:
         conf.env['WITH_OSX_KEYCHAIN'] = conf.options.with_osx_keychain
         if conf.options.with_osx_keychain:
@@ -131,11 +91,12 @@
     else:
         conf.env['WITH_OSX_KEYCHAIN'] = False
 
-    conf.load("coverage")
+    # Loading "late" to prevent tests to be compiled with profiling flags
+    conf.load('coverage')
 
     conf.define('SYSCONFDIR', conf.env['SYSCONFDIR'])
 
-    conf.write_config_header('src/ndn-cpp-config.h', define_prefix='NDN_CPP_')
+    conf.write_config_header('src/ndn-cpp-config.hpp', define_prefix='NDN_CPP_')
 
 def build(bld):
     libndn_cpp = bld(
@@ -219,7 +180,7 @@
                       relative_trick=True, cwd=bld.path.find_node('src'))
 
     bld.install_files("%s/ndn-cpp-dev" % bld.env['INCLUDEDIR'],
-                      bld.path.find_resource('src/ndn-cpp-config.h'))
+                      bld.path.find_resource('src/ndn-cpp-config.hpp'))
 
     bld.install_files("${SYSCONFDIR}/ndn", "client.conf.sample")
 
@@ -230,33 +191,9 @@
         doxyfile='docs/doxygen.conf')
 
 def sphinx(bld):
-    bld.load('sphinx_build', tooldir=['waf-tools'])
-
+    if not bld.env.SPHINX_BUILD:
+        bld.fatal("ERROR: cannot build documentation (`sphinx-build' is not found in $PATH)")
     bld(features="sphinx",
-        outdir="doc/html",
-        source="doc/source/conf.py")
-
-
-@TaskGen.feature('cxx')
-@TaskGen.before('process_source')
-def process_pch(self):
-    if getattr(self, 'pch', ''):
-        # for now support only gcc-compatible things
-        if self.env['COMPILER_CXX'] == 'g++':
-            nodes = self.to_nodes(self.pch, path=self.path)
-            for x in nodes:
-                z = self.create_task('gchx', x, x.change_ext('.hpp.gch'))
-                z.orig_self = self
-
-class gchx(Task.Task):
-    run_str = '${CXX} -x c++-header ${CXXFLAGS} ${FRAMEWORKPATH_ST:FRAMEWORKPATH} ' + \
-                '${CPPPATH_ST:INCPATHS} ${DEFINES_ST:DEFINES} ' + \
-                '${CXX_SRC_F}${SRC} ${CXX_TGT_F}${TGT}'
-    scan    = c_preproc.scan
-    ext_out = ['.hpp']
-    color   = 'BLUE'
-
-    def post_run(self):
-        super(gchx, self).post_run()
-        self.orig_self.env['CXXFLAGS'] = ['-include', self.inputs[0].relpath()] + \
-                                         self.env['CXXFLAGS']
+        outdir="docs/html",
+        source=bld.path.ant_glob("docs/**/*.rst"),
+        config="docs/source/conf.py")