build: Detecting if UNIX socket is available and disabling it if it is not

Change-Id: Iacdffe36223c085136a4fe09587ce304a3029853
diff --git a/.waf-tools/unix-socket.py b/.waf-tools/unix-socket.py
new file mode 100644
index 0000000..916fb42
--- /dev/null
+++ b/.waf-tools/unix-socket.py
@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+# encoding: utf-8
+
+BOOST_ASIO_HAS_LOCAL_SOCKETS_CHECK = '''
+#include <iostream>
+#include <boost/asio.hpp>
+int main() {
+#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
+    std::cout << "yes";
+#else
+    std::cout << "no";
+#endif
+    return 0;
+}
+'''
+
+def configure(conf):
+    boost_asio_present = conf.check_cxx(msg='Checking if UNIX socket is supported',
+                                        fragment=BOOST_ASIO_HAS_LOCAL_SOCKETS_CHECK,
+                                        use='BOOST NDN_CPP RT',
+                                        execute=True, define_ret=True)
+    if boost_asio_present == "yes":
+        conf.define('HAVE_UNIX_SOCKETS', 1)
+        conf.env['HAVE_UNIX_SOCKETS'] = True
diff --git a/daemon/face/unix-stream-face.hpp b/daemon/face/unix-stream-face.hpp
index c04bec2..cc97c21 100644
--- a/daemon/face/unix-stream-face.hpp
+++ b/daemon/face/unix-stream-face.hpp
@@ -9,6 +9,10 @@
 
 #include "stream-face.hpp"
 
+#ifndef HAVE_UNIX_SOCKETS
+#error "Cannot include this file when UNIX sockets are not available"
+#endif
+
 namespace nfd
 {
 
diff --git a/daemon/main.cpp b/daemon/main.cpp
index 8da8012..226fbe6 100644
--- a/daemon/main.cpp
+++ b/daemon/main.cpp
@@ -10,7 +10,10 @@
 #include "mgmt/internal-face.hpp"
 #include "mgmt/fib-manager.hpp"
 #include "face/tcp-channel-factory.hpp"
+
+#ifdef HAVE_UNIX_SOCKETS
 #include "face/unix-stream-channel-factory.hpp"
+#endif
 
 namespace nfd {
 
@@ -42,8 +45,12 @@
 static TcpChannelFactory* g_tcpFactory;
 static shared_ptr<TcpChannel> g_tcpChannel;
 static shared_ptr<InternalFace> g_internalFace;
+
+#ifdef HAVE_UNIX_SOCKETS
 static UnixStreamChannelFactory* g_unixFactory;
 static shared_ptr<UnixStreamChannel> g_unixChannel;
+#endif
+
 
 void
 usage(char* programName)
@@ -51,12 +58,16 @@
   printf(
     "%s --help\n\tshow this help and exit\n"
     "%s [--tcp-listen \"0.0.0.0:6363\"] "
-        "[--unix-listen \"/var/run/nfd.sock\"] "
-        "[--tcp-connect \"192.0.2.1:6363\" "
+#ifdef HAVE_UNIX_SOCKETS
+       "[--unix-listen \"/var/run/nfd.sock\"] "
+#endif
+       "[--tcp-connect \"192.0.2.1:6363\" "
             "[--prefix </example>]]\n"
       "\trun forwarding daemon\n"
       "\t--tcp-listen <ip:port>: listen on IP and port\n"
+#ifdef HAVE_UNIX_SOCKETS
       "\t--unix-listen <unix-socket-path>: listen on Unix socket\n"
+#endif
       "\t--tcp-connect <ip:port>: connect to IP and port (can occur multiple times)\n"
       "\t--prefix <NDN name>: add this face as nexthop to FIB entry "
         "(must appear after --tcp-connect, can occur multiple times)\n"
@@ -174,6 +185,7 @@
   }
 }
 
+#ifdef HAVE_UNIX_SOCKETS
 void
 initializeUnix()
 {
@@ -184,6 +196,7 @@
     bind(&onFaceEstablish, _1, static_cast<std::vector<Name>*>(0)),
     &onFaceError);
 }
+#endif
 
 void
 initializeMgmt()
@@ -214,7 +227,9 @@
   
   g_forwarder = new Forwarder(g_ioService);
   initializeTcp();
+#ifdef HAVE_UNIX_SOCKETS
   initializeUnix();
+#endif
   initializeMgmt();
 
   /// \todo Add signal processing to gracefully terminate the app
diff --git a/wscript b/wscript
index f0b04ac..3562e45 100644
--- a/wscript
+++ b/wscript
@@ -5,7 +5,7 @@
 
 def options(opt):
     opt.load('compiler_cxx')
-    opt.load('boost doxygen coverage', tooldir=['.waf-tools'])
+    opt.load('boost doxygen coverage unix-socket', tooldir=['.waf-tools'])
 
     nfdopt = opt.add_option_group('NFD Options')
     nfdopt.add_option('--debug',action='store_true',default=False,dest='debug',help='''Compile library debugging mode without all optimizations (-O0)''')
@@ -55,6 +55,8 @@
     if int(boost_version[0]) < 1 or int(boost_version[1]) < 42:
         Logs.error ("Minumum required boost version is 1.42")
         return
+
+    conf.load('unix-socket')
     
     conf.check_cxx(lib='rt', uselib_store='RT', define_name='HAVE_RT', mandatory=False)
 
@@ -63,13 +65,19 @@
     conf.write_config_header('daemon/config.hpp')
 
 def build(bld):
-    bld(target = "nfd-objects",
+    nfd_objects = bld(
+        target = "nfd-objects",
         features = "cxx",
-        source = bld.path.ant_glob(['daemon/**/*.cpp'], excl=['daemon/main.cpp']),
+        source = bld.path.ant_glob(['daemon/**/*.cpp'],
+                                   excl=['daemon/**/unix-*.cpp', 'daemon/main.cpp']),
         use = 'BOOST NDN_CPP RT',
         includes = [".", "daemon"],
         )
 
+    if bld.env['HAVE_UNIX_SOCKETS']:
+        nfd_objects.source += bld.path.ant_glob(['daemon/**/unix-*.cpp'],
+                                                excl=['daemon/main.cpp'])
+
     bld(target = "nfd",
         features = "cxx cxxprogram",
         source = 'daemon/main.cpp',
@@ -79,15 +87,19 @@
     
     # Unit tests
     if bld.env['WITH_TESTS']:
-      unittests = bld.program (
-          target="unit-tests",
-          features = "cxx cxxprogram",
-          source = bld.path.ant_glob(['tests/**/*.cpp']),
-          use = 'nfd-objects',
-          includes = [".", "daemon"],
-          install_prefix = None,
+        unit_tests = unittests = bld.program (
+            target="unit-tests",
+            features = "cxx cxxprogram",
+            source = bld.path.ant_glob(['tests/**/*.cpp'],
+                                       excl=['tests/**/unix-*.cpp']),
+            use = 'nfd-objects',
+            includes = [".", "daemon"],
+            install_prefix = None,
           )
 
+        if bld.env['HAVE_UNIX_SOCKETS']:
+            unit_tests.source += bld.path.ant_glob(['tests/**/unix-*.cpp'])
+
 @Configure.conf
 def add_supported_cxxflags(self, cxxflags):
     """