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/.waf-tools/boost.py b/.waf-tools/boost.py
index 39b5446..8c36b34 100644
--- a/.waf-tools/boost.py
+++ b/.waf-tools/boost.py
@@ -53,8 +53,11 @@
 from waflib import Utils, Logs, Errors
 from waflib.Configure import conf
 
-BOOST_LIBS = ['/usr/lib', '/usr/local/lib', '/opt/local/lib', '/sw/lib', '/lib', '/usr/lib/x86_64-linux-gnu', '/usr/lib/i386-linux-gnu', '/usr/local/ndn/lib']
-BOOST_INCLUDES = ['/usr/include', '/usr/local/include', '/opt/local/include', '/sw/include', '/usr/local/ndn/include']
+BOOST_LIBS = ['/usr/lib', '/usr/local/lib', '/opt/local/lib', '/sw/lib', '/lib',
+              '/usr/lib/x86_64-linux-gnu', '/usr/lib/i386-linux-gnu',
+              '/usr/local/ndn/lib', '/opt/ndn/lib']
+BOOST_INCLUDES = ['/usr/include', '/usr/local/include', '/opt/local/include', '/sw/include',
+                  '/usr/local/ndn/include', '/opt/ndn/include']
 BOOST_VERSION_FILE = 'boost/version.hpp'
 BOOST_VERSION_CODE = '''
 #include <iostream>
diff --git a/.waf-tools/coverage.py b/.waf-tools/coverage.py
index eac7608..8045aa8 100644
--- a/.waf-tools/coverage.py
+++ b/.waf-tools/coverage.py
@@ -3,7 +3,7 @@
 from waflib import TaskGen
 
 def options(opt):
-    opt.add_option('--with-coverage',action='store_true',default=False,dest='with_coverage',
+    opt.add_option('--with-coverage', action='store_true', default=False, dest='with_coverage',
                    help='''Set compiler flags for gcc to enable code coverage information''')
 
 def configure(conf):
diff --git a/.waf-tools/cryptopp.py b/.waf-tools/cryptopp.py
index ab3f7f7..4293aab 100644
--- a/.waf-tools/cryptopp.py
+++ b/.waf-tools/cryptopp.py
@@ -6,10 +6,11 @@
 When using this tool, the wscript will look like:
 
     def options(opt):
-        opt.tool_options('cryptopp', tooldir=["waf-tools"])
+        opt.load('compiler_cxx cryptopp')
 
     def configure(conf):
         conf.load('compiler_cxx cryptopp')
+        conf.check_cryptopp()
 
     def build(bld):
         bld(source='main.cpp', target='app', use='CRYPTOPP')
@@ -22,12 +23,12 @@
 import re
 from waflib import Utils,Logs,Errors
 from waflib.Configure import conf
-CRYPTOPP_DIR = ['/usr', '/usr/local', '/opt/local', '/sw']
+CRYPTOPP_DIR = ['/usr', '/usr/local', '/opt/local', '/sw', '/usr/local/ndn', '/opt/ndn']
 CRYPTOPP_VERSION_FILE = 'config.h'
 
 def options(opt):
-    opt.add_option('--cryptopp', type='string', default='', dest='cryptopp_dir',
-                   help='''Path to where CryptoPP is installed, e.g. /opt/local''')
+    opt.add_option('--with-cryptopp', type='string', default=None, dest='cryptopp_dir',
+                   help='''Path to where CryptoPP is installed, e.g., /usr/local''')
 
 @conf
 def __cryptopp_get_version_file(self, dir):
@@ -39,7 +40,7 @@
 
 @conf
 def __cryptopp_find_root_and_version_file(self, *k, **kw):
-    root = k and k[0]or kw.get('path', None)
+    root = k and k[0] or kw.get('path', self.options.cryptopp_dir)
 
     file = self.__cryptopp_get_version_file(root)
     if root and file:
@@ -52,7 +53,7 @@
     if root:
         self.fatal('CryptoPP not found in %s' % root)
     else:
-        self.fatal('CryptoPP not found, please provide a --cryptopp argument (see help)')
+        self.fatal('CryptoPP not found, please provide a --with=cryptopp=PATH argument (see help)')
 
 @conf
 def check_cryptopp(self, *k, **kw):
@@ -60,6 +61,8 @@
         self.fatal('Load a c++ compiler first, e.g., conf.load("compiler_cxx")')
 
     var = kw.get('uselib_store','CRYPTOPP')
+    mandatory = kw.get('mandatory', True)
+
     self.start_msg('Checking Crypto++ lib')
     (root, file) = self.__cryptopp_find_root_and_version_file(*k, **kw)
 
@@ -81,5 +84,5 @@
                          lib='cryptopp',
                          cxxflags="-I%s/include" % root,
                          linkflags="-L%s/lib" % root,
-                         mandatory=True,
+                         mandatory=mandatory,
                          uselib_store=var)
diff --git a/.waf-tools/default-compiler-flags.py b/.waf-tools/default-compiler-flags.py
index 9f7843e..21060a2 100644
--- a/.waf-tools/default-compiler-flags.py
+++ b/.waf-tools/default-compiler-flags.py
@@ -1,8 +1,4 @@
 # -*- 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
 
 from waflib import Logs, Configure
 
diff --git a/.waf-tools/openssl.py b/.waf-tools/openssl.py
index eb85462..e2ba7d0 100644
--- a/.waf-tools/openssl.py
+++ b/.waf-tools/openssl.py
@@ -9,8 +9,7 @@
         opt.tool_options('openssl')
 
     def configure(conf):
-        conf.load('compiler_c openssl')
-
+        conf.load('compiler_cxx openssl')
         conf.check_openssl()
 
     def build(bld):
@@ -23,21 +22,10 @@
 
 @conf
 def check_openssl(self,*k,**kw):
-    root = k and k[0] or kw.get('path',None) or Options.options.with_openssl
+    root = k and k[0] or kw.get('path', None) or Options.options.with_openssl
     mandatory = kw.get('mandatory', True)
-    var = kw.get('var', 'OPENSSL')
+    var = kw.get('uselib_store', 'OPENSSL')
 
-    CODE = """
-#include <openssl/crypto.h>
-#include <stdio.h>
-
-int main(int argc, char **argv) {
-    (void)argc;
-    printf("%s", argv[0]);
-
-    return 0;
-}
-"""
     if root:
         libcrypto = self.check_cxx(lib=['ssl', 'crypto'],
                        msg='Checking for OpenSSL library',
@@ -45,16 +33,14 @@
                        uselib_store=var,
                        mandatory=mandatory,
                        cxxflags="-I%s/include" % root,
-                       linkflags="-L%s/lib" % root,
-                       fragment=CODE)
+                       linkflags="-L%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,
-                       fragment=CODE)
+                       mandatory=mandatory)
 
 def options(opt):
-    opt.add_option('--with-openssl', type='string', default='',
+    opt.add_option('--with-openssl', type='string', default=None,
                    dest='with_openssl', help='''Path to OpenSSL''')
diff --git a/.waf-tools/osx-security.py b/.waf-tools/osx-security.py
new file mode 100644
index 0000000..9bcb3a8
--- /dev/null
+++ b/.waf-tools/osx-security.py
@@ -0,0 +1,36 @@
+#! /usr/bin/env python
+# encoding: utf-8
+
+from waflib import Logs, Utils
+from waflib.Configure import conf
+
+OSX_SECURITY_CODE='''
+#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
+def check_osx_security(conf, *k, **kw):
+    if Utils.unversioned_sys_platform() == "darwin":
+        try:
+            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=OSX_SECURITY_CODE, 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")
diff --git a/.waf-tools/pch.py b/.waf-tools/pch.py
new file mode 100644
index 0000000..b4f8f7f
--- /dev/null
+++ b/.waf-tools/pch.py
@@ -0,0 +1,38 @@
+#! /usr/bin/env python
+# encoding: utf-8
+
+from waflib import Logs, Utils, Task, TaskGen
+from waflib.Tools import c_preproc
+
+def options(opt):
+    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)''')
+
+def configure(conf):
+    conf.env['WITH_PCH'] = conf.options.with_pch
+
+
+@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']
diff --git a/.waf-tools/sphinx_build.py b/.waf-tools/sphinx_build.py
index 53cc431..32fd78a 100644
--- a/.waf-tools/sphinx_build.py
+++ b/.waf-tools/sphinx_build.py
@@ -1,119 +1,77 @@
 #!/usr/bin/env python
 # encoding: utf-8
-# Hans-Martin von Gaudecker, 2012
 
-"""
-Create Sphinx documentation. Currently only LaTeX and HTML are supported.
-
-The source file **must** be the conf.py file used by Sphinx. Everything
-else has defaults, passing in the parameters is optional.
-
-Usage for getting both html and pdf docs:
-
-    def build(ctx):
-        ctx(features='sphinx', source='docs/conf.py')
-        ctx(features='sphinx', source='docs/conf.py', buildername='latex')
-
-    def sphinx(ctx):
-        ctx(features='sphinx', source='docs/conf.py')
-        ctx(features='sphinx', source='docs/conf.py', buildername='latex')
-
-Optional parameters and their defaults:
-
-    * buildername: html
-    * srcdir: confdir (the directory where conf.py lives)
-    * outdir: confdir/buildername (in the build directory tree)
-    * doctreedir: outdir/.doctrees
-    * type: pdflatex (only applies to 'latex' builder)
-
-"""
-
+# inspired by code by Hans-Martin von Gaudecker, 2012
 
 import os
-from waflib import Task, TaskGen, Errors, Logs, Build
+from waflib import Node, Task, TaskGen, Errors, Logs, Build, Utils
 
-class RunSphinxBuild(Task.Task):
-	def scan(self):
-		"""Use Sphinx' internal environment to find the dependencies."""
-		s = self.sphinx_instance
-		msg, dummy, iterator = s.env.update(s.config, s.srcdir, s.doctreedir, s)
-		s.info(msg)
-		dep_nodes = []
-		for docname in s.builder.status_iterator(iterator, "reading sources... "):
-			filename = docname + s.config.source_suffix
-			dep_nodes.append(self.srcdir.find_node(filename))
-		for dep in s.env.dependencies.values():
-			# Need the 'str' call because Sphinx might return Unicode strings.
-			[dep_nodes.append(self.srcdir.find_node(str(d))) for d in dep]
-		return (dep_nodes, [])
+class sphinx_build(Task.Task):
+    color = 'BLUE'
+    run_str = '${SPHINX_BUILD} -q -b ${BUILDERNAME} -d ${DOCTREEDIR} ${SRCDIR} ${OUTDIR}'
 
-	def run(self):
-		"""Run the Sphinx build."""
-		self.sphinx_instance.build(force_all=False, filenames=None)
-		return None
+    def __str__(self):
+        env = self.env
+        src_str = ' '.join([a.nice_path()for a in self.inputs])
+        tgt_str = ' '.join([a.nice_path()for a in self.outputs])
+        if self.outputs: sep = ' -> '
+        else: sep = ''
+        return'%s [%s]: %s%s%s\n'%(self.__class__.__name__.replace('_task',''),
+                                   self.env['BUILDERNAME'], src_str, sep, tgt_str)
 
-	def post_run(self):
-		"""Add everything found in the output directory tree as an output.
-		Not elegant, but pragmatic."""
-		for n in self.outdir.ant_glob("**", quiet=True, remove=False):
-			if n not in self.outputs: self.set_outputs(n)
-		super(RunSphinxBuild, self).post_run()
-
-
-def _get_main_targets(tg, s):
-	"""Return some easy targets known from the Sphinx build environment **s.env**."""
-	out_dir = tg.bld.root.find_node(s.outdir)
-	tgt_nodes = []
-	if s.builder.name == "latex":
-		for tgt_info in s.env.config.latex_documents:
-			tgt_nodes.append(out_dir.find_or_declare(tgt_info[1]))
-	elif s.builder.name == "html":
-		suffix = getattr(s.env.config, "html_file_suffix", ".html")
-		tgt_name = s.env.config.master_doc + suffix
-		tgt_nodes.append(out_dir.find_or_declare(tgt_name))
-	else:
-		raise Errors.WafError("Sphinx builder not implemented: %s" % s.builder.name)
-	return tgt_nodes
-
+@TaskGen.extension('.py', '.rst')
+def sig_hook(self, node):
+    node.sig=Utils.h_file(node.abspath())
 
 @TaskGen.feature("sphinx")
 @TaskGen.before_method("process_source")
-def apply_sphinx(tg):
-	"""Set up the task generator with a Sphinx instance and create a task."""
+def apply_sphinx(self):
+    """Set up the task generator with a Sphinx instance and create a task."""
 
-        from sphinx.application import Sphinx
+    inputs = []
+    for i in Utils.to_list(self.source):
+        if not isinstance(i, Node.Node):
+            node = self.path.find_node(node)
+        else:
+            node = i
+        if not node:
+            raise ValueError('[%s] file not found' % i)
+        inputs.append(node)
 
-	# Put together the configuration based on defaults and tg attributes.
-	conf = tg.path.find_node(tg.source)
-	confdir = conf.parent.abspath()
-	buildername = getattr(tg, "buildername", "html")
-	srcdir = getattr(tg, "srcdir", confdir)
-	outdir = tg.path.find_or_declare (getattr(tg, "outdir", os.path.join(conf.parent.get_bld().abspath(), buildername))).abspath ()
+    task = self.create_task('sphinx_build', inputs)
 
-	doctreedir = getattr(tg, "doctreedir", os.path.join(outdir, ".doctrees"))
+    conf = self.path.find_node(self.config)
+    task.inputs.append(conf)
 
-	# Set up the Sphinx instance.
-	s = Sphinx (srcdir, confdir, outdir, doctreedir, buildername, status=None)
+    confdir = conf.parent.abspath()
+    buildername = getattr(self, "builder", "html")
+    srcdir = getattr(self, "srcdir", confdir)
+    outdir = self.path.find_or_declare(getattr(self, "outdir", buildername)).get_bld()
+    doctreedir = getattr(self, "doctreedir", os.path.join(outdir.abspath(), ".doctrees"))
 
-	# Get the main targets of the Sphinx build.
-	tgt_nodes = _get_main_targets(tg, s)
+    task.env['BUILDERNAME'] = buildername
+    task.env['SRCDIR'] = srcdir
+    task.env['DOCTREEDIR'] = doctreedir
+    task.env['OUTDIR'] = outdir.abspath()
 
-	# Create the task and set the required attributes.
-	task = tg.create_task("RunSphinxBuild", src=conf, tgt=tgt_nodes)
-	task.srcdir = tg.bld.root.find_node(s.srcdir)
-	task.outdir = tg.bld.root.find_node(s.outdir)
-	task.sphinx_instance = s
+    import imp
+    confData = imp.load_source('sphinx_conf', conf.abspath())
 
-	# Build pdf if we have the LaTeX builder, allow for building with xelatex.
-	if s.builder.name == "latex":
-		compile_type = getattr(tg, "type", "pdflatex")
-		tg.bld(features="tex", type=compile_type, source=tgt_nodes, name="sphinx_pdf", prompt=0)
+    if buildername == "man":
+        for i in confData.man_pages:
+            target = outdir.find_or_declare('%s.%d' % (i[1], i[4]))
+            task.outputs.append(target)
 
-	# Bypass the execution of process_source by setting the source to an empty list
-	tg.source = []
+            if self.install_path:
+                self.bld.install_files("%s/man%d/" % (self.install_path, i[4]), target)
+    else:
+        task.outputs.append(outdir)
+
+def configure(conf):
+    conf.find_program('sphinx-build', var='SPHINX_BUILD', mandatory=False)
 
 # sphinx docs
 from waflib.Build import BuildContext
-class sphinx (BuildContext):
+class sphinx(BuildContext):
     cmd = "sphinx"
     fun = "sphinx"
diff --git a/.waf-tools/sqlite3.py b/.waf-tools/sqlite3.py
new file mode 100644
index 0000000..4eabeaa
--- /dev/null
+++ b/.waf-tools/sqlite3.py
@@ -0,0 +1,36 @@
+#! /usr/bin/env python
+# encoding: utf-8
+
+from waflib import Options
+from waflib.Configure import conf
+
+def options(opt):
+    opt.add_option('--with-sqlite3', type='string', default=None,
+                   dest='with_sqlite3', help='''Path to SQLite3, e.g., /usr/local''')
+
+@conf
+def check_sqlite3(self, *k, **kw):
+    root = k and k[0] or kw.get('path', None) or Options.options.with_sqlite3
+    mandatory = kw.get('mandatory', True)
+    var = kw.get('uselib_store', 'SQLITE3')
+
+    if root:
+        self.check_cxx(lib='sqlite3',
+                       msg='Checking for SQLite3 library',
+                       define_name='HAVE_%s' % var,
+                       uselib_store=var,
+                       mandatory=mandatory,
+                       cxxflags="-I%s/include" % root,
+                       linkflags="-L%s/lib" % root)
+    else:
+        try:
+            self.check_cfg(package='sqlite3',
+                           args=['--cflags', '--libs'],
+                           uselib_store='SQLITE3',
+                           mandatory=True)
+        except:
+            self.check_cxx(lib='sqlite3',
+                           msg='Checking for SQLite3 library',
+                           define_name='HAVE_%s' % var,
+                           uselib_store=var,
+                           mandatory=mandatory)
diff --git a/src/common.hpp b/src/common.hpp
index 79f6c1c..b6de7b9 100644
--- a/src/common.hpp
+++ b/src/common.hpp
@@ -8,7 +8,7 @@
 #ifndef NDN_COMMON_HPP
 #define NDN_COMMON_HPP
 
-#include "ndn-cpp-config.h"
+#include "ndn-cpp-config.hpp"
 #include <stdint.h>
 // TODO: Is stddef.h portable?
 #include <stddef.h>
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")