Fixing compilation portability

+ wscript: now code compiles with apple gcc, clang, and macport's gcc (though not working, but for unrelated reason)
+ adhoc: make it compilable with apple's gcc compiler and replace usage of the deprecated method
+ gui: adding necessary ifdef statement to not add menu item when Sparkle is not available
+ auto-update: with apple's gcc, Sparkle requires additional header include (AppKit)

Change-Id: Ie6137898c3a0ffad5f857bbd4c949c0efbbc89bc
diff --git a/adhoc/adhoc-osx.mm b/adhoc/adhoc-osx.mm
index 3b7da80..9132ad9 100644
--- a/adhoc/adhoc-osx.mm
+++ b/adhoc/adhoc-osx.mm
@@ -36,15 +36,16 @@
 #import <CoreWLAN/CWInterface.h>
 #import <CoreWLAN/CoreWLANTypes.h>
 
+const NSUInteger channel = 11;
+
 bool
 Adhoc::CreateAdhoc ()
 {
-  NSString *networkName = @"NDNdirect";
-  NSNumber *channel = @11;
-  NSString *passphrase = @"NDNhello";
-  NSString *securityMode = @"Open";
+  NSString *networkName = [[NSString alloc] initWithCString:"NDNdirect" encoding:NSASCIIStringEncoding];
+  NSString *passphrase = [[NSString alloc] initWithCString:"NDNhello" encoding:NSASCIIStringEncoding];
+  NSString *securityMode = [[NSString alloc] initWithCString:"Open" encoding:NSASCIIStringEncoding];
 
-  NSArray *airportInterfaces = [CWInterface supportedInterfaces];
+  NSArray *airportInterfaces = [[CWInterface interfaceNames] allObjects];
 
   //Choose the desired interface . the first one will be enought for this example
   NSString *interfaceName =[airportInterfaces objectAtIndex:0];
@@ -58,7 +59,7 @@
 
   NSError *error = nil;
   NSData* data = [networkName dataUsingEncoding:NSUTF8StringEncoding];
-  BOOL created = [airport startIBSSModeWithSSID:data security:kCWIBSSModeSecurityNone channel:(NSUInteger)@11 password:passphrase error:&error];
+  BOOL created = [airport startIBSSModeWithSSID:data security:kCWIBSSModeSecurityNone channel:channel password:passphrase error:&error];
 
   if (!created)
     {
@@ -86,15 +87,10 @@
 void
 Adhoc::DestroyAdhoc ()
 {
-  NSString *networkName = @"NDNdirect";
-  NSNumber *channel = @11;
-  NSString *passphrase = @"NDNhello";
-  NSString *securityMode = @"Open";
-
-  NSArray *airportInterfaces = [CWInterface supportedInterfaces];
+  NSArray *airportInterfaces = [[CWInterface interfaceNames] allObjects];
 
   //Choose the desired interface . the first one will be enought for this example
-  NSString *interfaceName =[airportInterfaces objectAtIndex:0];
+  NSString *interfaceName = [airportInterfaces objectAtIndex:0];
 
   CWInterface *airport = [CWInterface interfaceWithName:interfaceName];
 
diff --git a/gui/chronosharegui.cpp b/gui/chronosharegui.cpp
index 79d7a62..8a30b05 100644
--- a/gui/chronosharegui.cpp
+++ b/gui/chronosharegui.cpp
@@ -279,10 +279,17 @@
   m_trayIconMenu->addSeparator();
   m_trayIconMenu->addAction(m_viewSettings);
   m_trayIconMenu->addAction(m_changeFolder);
+
+#ifdef SPARKLE_SUPPORTED
   m_trayIconMenu->addSeparator();
   m_trayIconMenu->addAction(m_checkForUpdates);
+#endif
+
+#ifdef ADHOC_SUPPORTED
   m_trayIconMenu->addSeparator();
   m_trayIconMenu->addAction(m_wifiAction);
+#endif
+
   m_trayIconMenu->addSeparator();
   m_trayIconMenu->addAction(m_quitProgram);
 
diff --git a/osx/auto-update/sparkle-auto-update.mm b/osx/auto-update/sparkle-auto-update.mm
index 1474775..dda0e79 100644
--- a/osx/auto-update/sparkle-auto-update.mm
+++ b/osx/auto-update/sparkle-auto-update.mm
@@ -21,13 +21,17 @@
 
 #include "sparkle-auto-update.h"
 #import <Foundation/Foundation.h>
+#import <AppKit/AppKit.h>
 #import <Sparkle/Sparkle.h>
-#include <stdio.h>
+
+#include "logging.h"
+
+INIT_LOGGER ("SparkeAutoUpdate");
 
 class SparkleAutoUpdate::Private
 {
-  public:
-    SUUpdater *updater;
+public:
+  SUUpdater *updater;
 };
 
 SparkleAutoUpdate::SparkleAutoUpdate(const QString &updateUrl)
@@ -51,5 +55,5 @@
 {
   //[d->updater checkForUpdatesInBackground];
   [d->updater checkForUpdates : nil];
-  printf("++++++++ checking update ++++++\n");
+  _LOG_DEBUG ("++++++++ checking update +++++");
 }
diff --git a/wscript b/wscript
index 23ec618..d98f134 100644
--- a/wscript
+++ b/wscript
@@ -2,7 +2,7 @@
 VERSION='0.1'
 APPNAME='chronoshare'
 
-from waflib import Build, Logs, Utils, Task, TaskGen
+from waflib import Build, Logs, Utils, Task, TaskGen, Configure
 
 def options(opt):
     opt.add_option('--debug',action='store_true',default=False,dest='debug',help='''debugging mode''')
@@ -18,6 +18,19 @@
 def configure(conf):
     conf.load("compiler_c compiler_cxx")
 
+    if conf.options.debug:
+        conf.define ('_DEBUG', 1)
+        conf.add_supported_cxxflags (cxxflags = ['-O0',
+                                                 '-Wall',
+                                                 '-Wno-unused-variable',
+                                                 '-g3',
+                                                 '-Wno-unused-private-field', # only clang supports
+                                                 '-fcolor-diagnostics',       # only clang supports
+                                                 '-Qunused-arguments'         # only clang supports
+                                                 ])
+    else:
+        conf.add_supported_cxxflags (cxxflags = ['-O3', '-O4'])
+
     # I wish I could use it, but there is some weirdness with boost tests. Give up for now
     # try:
     #     conf.check(features='cxx cxxprogram', cxxflags="-std=c++11")
@@ -37,13 +50,15 @@
 
     if Utils.unversioned_sys_platform () == "darwin":
         conf.check_cxx(framework_name='Foundation', uselib_store='OSX_FOUNDATION', mandatory=False, compile_filename='test.mm')
-        conf.check_cxx(framework_name='CoreWLAN',   uselib_store='OSX_COREWLAN',   define_name='HAVE_COREWLAN', mandatory=False, compile_filename='test.mm')
+        conf.check_cxx(framework_name='AppKit',     uselib_store='OSX_APPKIT',     mandatory=False, compile_filename='test.mm')
+        conf.check_cxx(framework_name='CoreWLAN',   uselib_store='OSX_COREWLAN',   define_name='HAVE_COREWLAN',
+                       use="OSX_FOUNDATION", mandatory=False, compile_filename='test.mm')
 
         if conf.options.autoupdate:
             def check_sparkle(**kwargs):
-              conf.check_cxx (framework_name='Sparkle', header_name="Foundation/Foundation.h",
+              conf.check_cxx (framework_name="Sparkle", header_name=["Foundation/Foundation.h", "AppKit/AppKit.h"],
                               uselib_store='OSX_SPARKLE', define_name='HAVE_SPARKLE', mandatory=True,
-                              compile_filename='test.mm',
+                              compile_filename='test.mm', use="OSX_FOUNDATION OSX_APPKIT",
                               **kwargs
                               )
             try:
@@ -57,27 +72,28 @@
                                   linkflags="-F%s/osx/Frameworks/" % conf.path.abspath())
                     conf.env.HAVE_LOCAL_SPARKLE = 1
                 except:
-                    # Download to local path and retry
-                    Logs.info ("Sparkle framework not found, trying to download it to 'build/'")
-
                     import urllib, subprocess, os, shutil
-                    urllib.urlretrieve ("http://sparkle.andymatuschak.org/files/Sparkle%201.5b6.zip", "build/Sparkle.zip")
-                    if os.path.exists('build/Sparkle.zip'):
-                        try:
-                            subprocess.check_call (['unzip', '-qq', 'build/Sparkle.zip', '-d', 'build/Sparkle'])
-                            os.remove ("build/Sparkle.zip")
-                            if not os.path.exists("osx/Frameworks"):
-                                os.mkdir ("osx/Frameworks")
-                            os.rename ("build/Sparkle/Sparkle.framework", "osx/Frameworks/Sparkle.framework")
-                            shutil.rmtree("build/Sparkle", ignore_errors=True)
+                    if not os.path.exists('osx/Frameworks/Sparkle.framework'):
+                        # Download to local path and retry
+                        Logs.info ("Sparkle framework not found, trying to download it to 'build/'")
 
-                            check_sparkle(cxxflags="-F%s/osx/Frameworks/" % conf.path.abspath(),
-                                          linkflags="-F%s/osx/Frameworks/" % conf.path.abspath())
-                            conf.env.HAVE_LOCAL_SPARKLE = 1
-                        except subprocess.CalledProcessError as e:
-                            conf.fatal("Cannot find Sparkle framework. Auto download failed: '%s' returned %s" % (' '.join(e.cmd), e.returncode))
-                        except:
-                            conf.fatal("Unknown Error happened when auto downloading Sparkle framework")
+                        urllib.urlretrieve ("http://sparkle.andymatuschak.org/files/Sparkle%201.5b6.zip", "build/Sparkle.zip")
+                        if os.path.exists('build/Sparkle.zip'):
+                            try:
+                                subprocess.check_call (['unzip', '-qq', 'build/Sparkle.zip', '-d', 'build/Sparkle'])
+                                os.remove ("build/Sparkle.zip")
+                                if not os.path.exists("osx/Frameworks"):
+                                    os.mkdir ("osx/Frameworks")
+                                os.rename ("build/Sparkle/Sparkle.framework", "osx/Frameworks/Sparkle.framework")
+                                shutil.rmtree("build/Sparkle", ignore_errors=True)
+
+                                check_sparkle(cxxflags="-F%s/osx/Frameworks/" % conf.path.abspath(),
+                                              linkflags="-F%s/osx/Frameworks/" % conf.path.abspath())
+                                conf.env.HAVE_LOCAL_SPARKLE = 1
+                            except subprocess.CalledProcessError as e:
+                                conf.fatal("Cannot find Sparkle framework. Auto download failed: '%s' returned %s" % (' '.join(e.cmd), e.returncode))
+                            except:
+                                conf.fatal("Unknown Error happened when auto downloading Sparkle framework")
 
             if conf.is_defined('HAVE_SPARKLE'):
                 conf.env.HAVE_SPARKLE = 1 # small cheat for wscript
@@ -114,15 +130,6 @@
     conf.check_ccnx (path=conf.options.ccnx_dir)
     conf.define ('CCNX_PATH', conf.env.CCNX_ROOT)
 
-    if conf.options.debug:
-        conf.define ('_DEBUG', 1)
-        conf.env.append_value('CXXFLAGS', ['-O0', '-Wall', '-Wno-unused-variable', '-Wno-unused-private-field', '-g3'])
-    else:
-        conf.env.append_value('CXXFLAGS', ['-O3', '-g'])
-
-    if conf.env["CXX"] == ["clang++"]:
-        conf.env.append_value('CXXFLAGS', ['-fcolor-diagnostics', '-Qunused-arguments'])
-
     if conf.options._test:
         conf.define ('_TESTS', 1)
         conf.env.TEST = 1
@@ -296,3 +303,18 @@
             out.write ('        <file>%s</file>\n' % f.abspath (), 'a')
         out.write('    </qresource>\n</RCC>', 'a')
         return 0
+
+@Configure.conf
+def add_supported_cxxflags(self, cxxflags):
+    """
+    Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
+    """
+    self.start_msg('Checking allowed flags for c++ compiler')
+
+    supportedFlags = []
+    for flag in cxxflags:
+        if self.check_cxx (cxxflags=[flag], mandatory=False):
+            supportedFlags += [flag]
+
+    self.end_msg (' '.join (supportedFlags))
+    self.env.CXXFLAGS += supportedFlags