build: make precompiled headers more useful

This can reduce the compilation time of a release
build (without tests) by more than 40%

Refs: #5212
Change-Id: I977aff0c0c7becbfee8a8b55605b81d0c014829b
diff --git a/tools/ndn-autoconfig/main.cpp b/tools/ndn-autoconfig/main.cpp
index 2f8e561..3b30001 100644
--- a/tools/ndn-autoconfig/main.cpp
+++ b/tools/ndn-autoconfig/main.cpp
@@ -29,6 +29,7 @@
 #include <signal.h>
 #include <string.h>
 
+#include <boost/asio/signal_set.hpp>
 #include <boost/exception/diagnostic_information.hpp>
 #include <boost/program_options/options_description.hpp>
 #include <boost/program_options/parsers.hpp>
diff --git a/tools/nfd-autoreg.cpp b/tools/nfd-autoreg.cpp
index 7a72b82..fe079de 100644
--- a/tools/nfd-autoreg.cpp
+++ b/tools/nfd-autoreg.cpp
@@ -33,6 +33,7 @@
 #include <ndn-cxx/net/face-uri.hpp>
 #include <ndn-cxx/security/key-chain.hpp>
 
+#include <boost/asio/signal_set.hpp>
 #include <boost/exception/diagnostic_information.hpp>
 #include <boost/program_options/options_description.hpp>
 #include <boost/program_options/parsers.hpp>
@@ -42,6 +43,9 @@
 
 namespace nfd::tools::autoreg {
 
+using ndn::FaceUri;
+using ndn::Name;
+
 class AutoregServer : boost::noncopyable
 {
 public:
@@ -101,7 +105,7 @@
           .setFaceId(faceId)
           .setOrigin(ndn::nfd::ROUTE_ORIGIN_AUTOREG)
           .setCost(m_cost)
-          .setExpirationPeriod(time::milliseconds::max()),
+          .setExpirationPeriod(ndn::time::milliseconds::max()),
         [=] (auto&&...) { onRegisterCommandSuccess(faceId, prefix); },
         [=] (const auto& response) { onRegisterCommandFailure(faceId, prefix, response); });
     }
diff --git a/tools/nfdc/available-commands.cpp b/tools/nfdc/available-commands.cpp
deleted file mode 100644
index 6df761a..0000000
--- a/tools/nfdc/available-commands.cpp
+++ /dev/null
@@ -1,45 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/*
- * Copyright (c) 2014-2022,  Regents of the University of California,
- *                           Arizona Board of Regents,
- *                           Colorado State University,
- *                           University Pierre & Marie Curie, Sorbonne University,
- *                           Washington University in St. Louis,
- *                           Beijing Institute of Technology,
- *                           The University of Memphis.
- *
- * This file is part of NFD (Named Data Networking Forwarding Daemon).
- * See AUTHORS.md for complete list of NFD authors and contributors.
- *
- * NFD is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE.  See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "available-commands.hpp"
-#include "cs-module.hpp"
-#include "face-module.hpp"
-#include "rib-module.hpp"
-#include "status.hpp"
-#include "strategy-choice-module.hpp"
-
-namespace nfd::tools::nfdc {
-
-void
-registerCommands(CommandParser& parser)
-{
-  registerStatusCommands(parser);
-  FaceModule::registerCommands(parser);
-  RibModule::registerCommands(parser);
-  CsModule::registerCommands(parser);
-  StrategyChoiceModule::registerCommands(parser);
-}
-
-} // namespace nfd::tools::nfdc
diff --git a/tools/nfdc/command-definition.cpp b/tools/nfdc/command-definition.cpp
index 08952fd..c98197e 100644
--- a/tools/nfdc/command-definition.cpp
+++ b/tools/nfdc/command-definition.cpp
@@ -26,6 +26,7 @@
 #include "command-definition.hpp"
 #include "status-report.hpp"
 
+#include <boost/lexical_cast.hpp>
 #include <ndn-cxx/util/logger.hpp>
 
 namespace nfd::tools::nfdc {
diff --git a/tools/nfdc/command-parser.cpp b/tools/nfdc/command-parser.cpp
index 19bb5a2..2dff2ff 100644
--- a/tools/nfdc/command-parser.cpp
+++ b/tools/nfdc/command-parser.cpp
@@ -24,7 +24,12 @@
  */
 
 #include "command-parser.hpp"
-#include "format-helpers.hpp"
+
+#include "cs-module.hpp"
+#include "face-module.hpp"
+#include "rib-module.hpp"
+#include "status.hpp"
+#include "strategy-choice-module.hpp"
 
 #include <ndn-cxx/util/logger.hpp>
 
@@ -127,4 +132,14 @@
   return {def.getNoun(), def.getVerb(), def.parse(tokens, nConsumed), i->second->execute};
 }
 
+void
+registerCommands(CommandParser& parser)
+{
+  registerStatusCommands(parser);
+  FaceModule::registerCommands(parser);
+  RibModule::registerCommands(parser);
+  CsModule::registerCommands(parser);
+  StrategyChoiceModule::registerCommands(parser);
+}
+
 } // namespace nfd::tools::nfdc
diff --git a/tools/nfdc/command-parser.hpp b/tools/nfdc/command-parser.hpp
index e0849b0..52ba292 100644
--- a/tools/nfdc/command-parser.hpp
+++ b/tools/nfdc/command-parser.hpp
@@ -123,6 +123,9 @@
   std::vector<CommandContainer::const_iterator> m_commandOrder;
 };
 
+void
+registerCommands(CommandParser& parser);
+
 } // namespace nfd::tools::nfdc
 
 #endif // NFD_TOOLS_NFDC_COMMAND_PARSER_HPP
diff --git a/tools/nfdc/face-module.cpp b/tools/nfdc/face-module.cpp
index 08409f7..4d9d3a2 100644
--- a/tools/nfdc/face-module.cpp
+++ b/tools/nfdc/face-module.cpp
@@ -27,6 +27,8 @@
 #include "canonizer.hpp"
 #include "find-face.hpp"
 
+#include <boost/lexical_cast/try_lexical_convert.hpp>
+
 namespace nfd::tools::nfdc {
 
 void
diff --git a/tools/nfdc/main.cpp b/tools/nfdc/main.cpp
index 78da697..b89a3f9 100644
--- a/tools/nfdc/main.cpp
+++ b/tools/nfdc/main.cpp
@@ -23,10 +23,11 @@
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "available-commands.hpp"
-#include "core/version.hpp"
+#include "command-parser.hpp"
 #include "help.hpp"
 
+#include "core/version.hpp"
+
 #include <boost/tokenizer.hpp>
 #include <fstream>
 #include <iostream>
diff --git a/tools/nfdc/available-commands.hpp b/tools/nfdc/nfdc-pch.hpp
similarity index 82%
rename from tools/nfdc/available-commands.hpp
rename to tools/nfdc/nfdc-pch.hpp
index ad6525c..b2e789d 100644
--- a/tools/nfdc/available-commands.hpp
+++ b/tools/nfdc/nfdc-pch.hpp
@@ -23,16 +23,13 @@
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef NFD_TOOLS_NFDC_AVAILABLE_COMMANDS_HPP
-#define NFD_TOOLS_NFDC_AVAILABLE_COMMANDS_HPP
+#ifndef NFD_TOOLS_NFDC_NFDC_PCH_HPP
+#define NFD_TOOLS_NFDC_NFDC_PCH_HPP
 
-#include "command-parser.hpp"
+#include "core/common.hpp"
 
-namespace nfd::tools::nfdc {
+#include <ndn-cxx/mgmt/nfd/controller.hpp>
+#include <ndn-cxx/security/validator-null.hpp>
+#include <ndn-cxx/util/logger.hpp>
 
-void
-registerCommands(CommandParser& parser);
-
-} // namespace nfd::tools::nfdc
-
-#endif // NFD_TOOLS_NFDC_AVAILABLE_COMMANDS_HPP
+#endif // NFD_TOOLS_NFDC_NFDC_PCH_HPP
diff --git a/tools/wscript b/tools/wscript
index 7f4ff7f..f3ee6a8 100644
--- a/tools/wscript
+++ b/tools/wscript
@@ -1,6 +1,6 @@
 # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
 """
-Copyright (c) 2014-2018,  Regents of the University of California,
+Copyright (c) 2014-2022,  Regents of the University of California,
                           Arizona Board of Regents,
                           Colorado State University,
                           University Pierre & Marie Curie, Sorbonne University,
@@ -23,49 +23,55 @@
 NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
 """
 
-top = '..'
-
 from waflib import Context, Utils
 
-def build(bld):
-    commonDeps = 'core-objects NDN_CXX BOOST LIBRESOLV'
+top = '../'
 
+def build(bld):
     # Single object tools:
-    # tools/example-tool.cpp is a self-contained tool with a main() function
-    # and is built as build/bin/example-tool.
-    # These tools cannot be unit-tested.
+    # tools/foo.cpp is a self-contained tool with a main() function
+    # and is built as build/bin/foo. These tools cannot be unit-tested.
     for tool in bld.path.ant_glob('*.cpp'):
         name = tool.change_ext('').path_from(bld.path.get_bld())
         bld.program(name=name,
-                    target='../bin/%s' % name,
+                    target=top + 'bin/%s' % name,
                     source=[tool],
-                    use=commonDeps)
+                    use='core-objects')
 
     # Sub-directory tools:
-    # tools/example-tool/**/*.cpp is compiled and linked into build/bin/example-tool.
-    # tools/example-tool/main.cpp must exist and must contain the main() function.
+    # tools/foo/**/*.cpp are compiled and linked into build/bin/foo.
+    # tools/foo/main.cpp must exist and must contain the main() function.
     # All other objects are collected into 'tools-objects' and can be unit-tested.
     testableObjects = []
     for subdir in bld.path.ant_glob('*', dir=True, src=False):
+        name = subdir.path_from(bld.path)
+        subWscript = subdir.find_node('wscript')
+        if subWscript:
+            # if the subdir has a wscript, delegate to it
+            bld.recurse(name)
+            continue
+
         mainFile = subdir.find_node('main.cpp')
         if mainFile is None:
-            continue # not a C++ tool
+            # not a C++ tool, skip the subdir
+            continue
 
-        name = subdir.path_from(bld.path)
         srcFiles = subdir.ant_glob('**/*.cpp', excl=['main.cpp'])
         srcObjects = ''
         if srcFiles:
             srcObjects = 'tools-%s-objects' % name
             bld.objects(target=srcObjects,
                         source=srcFiles,
-                        use=commonDeps,
+                        features='pch',
+                        headers=subdir.find_node(name + '-pch.hpp'),
+                        use='core-objects LIBRESOLV',
                         includes=name)
             testableObjects.append(srcObjects)
 
         bld.program(name=name,
-                    target='../bin/%s' % name,
+                    target=top + 'bin/%s' % name,
                     source=[mainFile],
-                    use=commonDeps + ' ' + srcObjects,
+                    use='core-objects ' + srcObjects,
                     includes=name)
 
     bld.objects(target='tools-objects',
@@ -78,7 +84,7 @@
         name = script.change_ext('').path_from(bld.path.get_bld())
         bld(features='subst',
             name=name,
-            target='../bin/%s' % name,
+            target=top + 'bin/%s' % name,
             source=[script],
             install_path='${BINDIR}',
             chmod=Utils.O755,