From ab6bac1ee3ba4ebf4dfc8dc1a3ae6783756c48c9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Thu, 28 Oct 2010 20:34:56 +0200
Subject: Added swiften-config.


diff --git a/BuildTools/Copyrighter.py b/BuildTools/Copyrighter.py
index b83c3d6..f59bdaf 100755
--- a/BuildTools/Copyrighter.py
+++ b/BuildTools/Copyrighter.py
@@ -136,7 +136,7 @@ elif sys.argv[1] == "check-all-copyrights" :
   for (path, dirs, files) in os.walk(".") :
     if "3rdParty" in path or ".sconf" in path or "Swift.app" in path :
       continue
-    for filename in [os.path.join(path, file) for file in files if (file.endswith(".cpp") or file.endswith(".h")) and not "ui_" in file and not "moc_" in file and not "qrc_" in file and not "BuildVersion.h" in file and not "Swiften.h" in file ] :
+    for filename in [os.path.join(path, file) for file in files if (file.endswith(".cpp") or file.endswith(".h")) and not "ui_" in file and not "moc_" in file and not "qrc_" in file and not "BuildVersion.h" in file and not "Swiften.h" in file and not "swiften-config.h" in file] :
       ok &= check_copyright(filename) 
   if not ok :
     sys.exit(-1)
@@ -150,7 +150,7 @@ elif sys.argv[1] == "set-all-copyrights" :
   for (path, dirs, files) in os.walk(".") :
     if "3rdParty" in path or ".sconf" in path or "Swift.app" in path :
       continue
-    for filename in [os.path.join(path, file) for file in files if (file.endswith(".cpp") or file.endswith(".h")) and not "ui_" in file and not "moc_" in file and not "qrc_" in file and not "BuildVersion.h" in file] :
+    for filename in [os.path.join(path, file) for file in files if (file.endswith(".cpp") or file.endswith(".h")) and not "ui_" in file and not "moc_" in file and not "qrc_" in file and not "BuildVersion.h" in file and not "swiften-config.h" in file] :
       set_copyright(filename, copyright) 
 else :
   print "Unknown command: " + sys.argv[1]
diff --git a/SwifTools/Application/ApplicationPathProvider.cpp b/SwifTools/Application/ApplicationPathProvider.cpp
index 2c5db7e..8457f88 100644
--- a/SwifTools/Application/ApplicationPathProvider.cpp
+++ b/SwifTools/Application/ApplicationPathProvider.cpp
@@ -9,6 +9,7 @@
 
 #include "SwifTools/Application/ApplicationPathProvider.h"
 #include "Swiften/Base/foreach.h"
+#include "Swiften/Base/Paths.h"
 
 namespace Swift {
 
@@ -40,4 +41,8 @@ boost::filesystem::path ApplicationPathProvider::getResourcePath(const String& r
 	return boost::filesystem::path();
 }
 
+boost::filesystem::path ApplicationPathProvider::getExecutableDir() const {
+	return Paths::getExecutablePath();
+}
+
 }
diff --git a/SwifTools/Application/ApplicationPathProvider.h b/SwifTools/Application/ApplicationPathProvider.h
index 0c2d302..722f1ad 100644
--- a/SwifTools/Application/ApplicationPathProvider.h
+++ b/SwifTools/Application/ApplicationPathProvider.h
@@ -19,7 +19,7 @@ namespace Swift {
 
 			virtual boost::filesystem::path getHomeDir() const = 0;
 			virtual boost::filesystem::path getDataDir() const = 0;
-			virtual boost::filesystem::path getExecutableDir() const = 0;
+			virtual boost::filesystem::path getExecutableDir() const;
 			boost::filesystem::path getProfileDir(const String& profile) const;
 			boost::filesystem::path getResourcePath(const String& resource) const;
 
diff --git a/SwifTools/Application/MacOSXApplicationPathProvider.cpp b/SwifTools/Application/MacOSXApplicationPathProvider.cpp
index eb6c63f..fb6523c 100644
--- a/SwifTools/Application/MacOSXApplicationPathProvider.cpp
+++ b/SwifTools/Application/MacOSXApplicationPathProvider.cpp
@@ -33,17 +33,4 @@ boost::filesystem::path MacOSXApplicationPathProvider::getHomeDir() const {
 	return boost::filesystem::path(getenv("HOME"));
 }
 
-
-boost::filesystem::path MacOSXApplicationPathProvider::getExecutableDir() const {
-	ByteArray path;
-	uint32_t size = 4096;
-	path.resize(size);
-	if (_NSGetExecutablePath(path.getData(), &size) == 0) {
-		return boost::filesystem::path(path.toString().getUTF8Data()).parent_path();
-	}
-	else {
-		return boost::filesystem::path();
-	}
-}
-
 }
diff --git a/SwifTools/Application/MacOSXApplicationPathProvider.h b/SwifTools/Application/MacOSXApplicationPathProvider.h
index a914ded..d2613f8 100644
--- a/SwifTools/Application/MacOSXApplicationPathProvider.h
+++ b/SwifTools/Application/MacOSXApplicationPathProvider.h
@@ -15,7 +15,6 @@ namespace Swift {
 
 			virtual boost::filesystem::path getHomeDir() const;
 			boost::filesystem::path getDataDir() const;
-			virtual boost::filesystem::path getExecutableDir() const;
 
 			virtual std::vector<boost::filesystem::path> getResourceDirs() const {
 				return resourceDirs;
diff --git a/SwifTools/Application/UnixApplicationPathProvider.cpp b/SwifTools/Application/UnixApplicationPathProvider.cpp
index caffc50..c5a2782 100644
--- a/SwifTools/Application/UnixApplicationPathProvider.cpp
+++ b/SwifTools/Application/UnixApplicationPathProvider.cpp
@@ -48,17 +48,4 @@ boost::filesystem::path UnixApplicationPathProvider::getDataDir() const {
 	return dataPath;
 }
 
-boost::filesystem::path UnixApplicationPathProvider::getExecutableDir() const {
-	ByteArray path;
-	path.resize(4096);
-	size_t size = readlink("/proc/self/exe", path.getData(), path.getSize());
-	if (size > 0) {
-		path.resize(size);
-		return boost::filesystem::path(path.toString().getUTF8Data()).parent_path();
-	}
-	else {
-		return boost::filesystem::path();
-	}
-}
-
 }
diff --git a/SwifTools/Application/UnixApplicationPathProvider.h b/SwifTools/Application/UnixApplicationPathProvider.h
index 2ef04ea..0c2f643 100644
--- a/SwifTools/Application/UnixApplicationPathProvider.h
+++ b/SwifTools/Application/UnixApplicationPathProvider.h
@@ -21,7 +21,6 @@ namespace Swift {
 
 			virtual boost::filesystem::path getHomeDir() const;
 			boost::filesystem::path getDataDir() const;
-			virtual boost::filesystem::path getExecutableDir() const;
 
 			virtual std::vector<boost::filesystem::path> getResourceDirs() const {
 				return resourceDirs;
diff --git a/SwifTools/Application/WindowsApplicationPathProvider.cpp b/SwifTools/Application/WindowsApplicationPathProvider.cpp
index ed692d7..e19606f 100644
--- a/SwifTools/Application/WindowsApplicationPathProvider.cpp
+++ b/SwifTools/Application/WindowsApplicationPathProvider.cpp
@@ -17,11 +17,4 @@ WindowsApplicationPathProvider::WindowsApplicationPathProvider(const String& nam
 	resourceDirs.push_back(getExecutableDir() / "../resources"); // Development
 }
 
-boost::filesystem::path WindowsApplicationPathProvider::getExecutableDir() const {
-	ByteArray data;
-	data.resize(2048);
-	GetModuleFileName(NULL, data.getData(), data.getSize());
-	return boost::filesystem::path(data.toString().getUTF8Data()).parent_path();
-}
-
 }
diff --git a/SwifTools/Application/WindowsApplicationPathProvider.h b/SwifTools/Application/WindowsApplicationPathProvider.h
index b3341e3..26f7045 100644
--- a/SwifTools/Application/WindowsApplicationPathProvider.h
+++ b/SwifTools/Application/WindowsApplicationPathProvider.h
@@ -27,7 +27,6 @@ namespace Swift {
 				return boost::filesystem::path(homeDirRaw);
 			}
 
-			virtual boost::filesystem::path getExecutableDir() const;
 			virtual std::vector<boost::filesystem::path> getResourceDirs() const {
 				return resourceDirs;
 			}
diff --git a/Swiften/Base/Paths.cpp b/Swiften/Base/Paths.cpp
new file mode 100644
index 0000000..c1dbef2
--- /dev/null
+++ b/Swiften/Base/Paths.cpp
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <Swiften/Base/Paths.h>
+#include <Swiften/Base/Platform.h>
+
+#if defined(SWIFTEN_PLATFORM_MACOSX)
+#include <mach-o/dyld.h>
+#elif defined(SWIFTEN_PLATFORM_LINUX)
+#include <unistd.h>
+#elif defined(SWIFTEN_PLATFORM_WINDOWS)
+#include <windows.h>
+#endif
+
+#include <Swiften/Base/ByteArray.h>
+
+namespace Swift {
+
+boost::filesystem::path Paths::getExecutablePath() {
+#if defined(SWIFTEN_PLATFORM_MACOSX)
+	ByteArray path;
+	uint32_t size = 4096;
+	path.resize(size);
+	if (_NSGetExecutablePath(path.getData(), &size) == 0) {
+		return boost::filesystem::path(path.toString().getUTF8Data()).parent_path();
+	}
+#elif defined(SWIFTEN_PLATFORM_LINUX)
+	ByteArray path;
+	path.resize(4096);
+	size_t size = readlink("/proc/self/exe", path.getData(), path.getSize());
+	if (size > 0) {
+		path.resize(size);
+		return boost::filesystem::path(path.toString().getUTF8Data()).parent_path();
+	}
+#elif defined(SWIFTEN_PLATFORM_WINDOWS)
+	ByteArray data;
+	data.resize(2048);
+	GetModuleFileName(NULL, data.getData(), data.getSize());
+	return boost::filesystem::path(data.toString().getUTF8Data()).parent_path();
+#endif
+	return boost::filesystem::path();
+}
+
+}
diff --git a/Swiften/Base/Paths.h b/Swiften/Base/Paths.h
new file mode 100644
index 0000000..06c6aeb
--- /dev/null
+++ b/Swiften/Base/Paths.h
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <boost/filesystem.hpp>
+
+namespace Swift {
+	class Paths {
+		public:
+			static boost::filesystem::path getExecutablePath();
+	};
+}
diff --git a/Swiften/Base/SConscript b/Swiften/Base/SConscript
index a0984e5..fb58d34 100644
--- a/Swiften/Base/SConscript
+++ b/Swiften/Base/SConscript
@@ -3,6 +3,7 @@ Import("swiften_env")
 objects = swiften_env.StaticObject([
 			"ByteArray.cpp",
 			"Error.cpp",
+			"Paths.cpp",
 			"IDGenerator.cpp",
 			"String.cpp",
 			"sleep.cpp",
diff --git a/Swiften/Config/.gitignore b/Swiften/Config/.gitignore
new file mode 100644
index 0000000..4514b26
--- /dev/null
+++ b/Swiften/Config/.gitignore
@@ -0,0 +1,2 @@
+swiften-config
+swiften-config.h
diff --git a/Swiften/Config/SConscript b/Swiften/Config/SConscript
new file mode 100644
index 0000000..962093a
--- /dev/null
+++ b/Swiften/Config/SConscript
@@ -0,0 +1,33 @@
+Import("env")
+
+def replaceSwiftenPath(input) :
+	return input.replace(env.Dir("#").abspath, "#")
+
+def cStringVariable(env, cVar, sconsVar) :
+	result = "const char* " + cVar + "[] = {\n"
+	# FIXME: Probably not very robust
+	for var in sconsVar.split(" ") :
+		result += "\t\"" + env.subst(var) + "\",\n"
+	result += "\t0\n"
+	result += "};\n"
+	return result
+
+config_flags = ""
+
+swiften_env = env.Clone()
+swiften_env.MergeFlags(swiften_env["SWIFTEN_FLAGS"])
+swiften_env.MergeFlags(swiften_env["SWIFTEN_DEP_FLAGS"])
+
+cppflags = replaceSwiftenPath(" ".join([swiften_env.subst("$_CPPDEFFLAGS"), swiften_env.subst("$_CPPINCFLAGS")]))
+config_flags += cStringVariable(swiften_env, "CPPFLAGS", cppflags)
+
+libflags = replaceSwiftenPath(" ".join([swiften_env.subst("$_LIBDIRFLAGS"), swiften_env.subst("$_LIBFLAGS")]))
+config_flags += cStringVariable(swiften_env, "LIBFLAGS", libflags)
+
+config_env = env.Clone()
+config_env.MergeFlags(config_env["SWIFTEN_FLAGS"])
+config_env.MergeFlags(config_env["BOOST_FLAGS"])
+config_env.WriteVal("swiften-config.h", config_env.Value(config_flags))
+config_env.Program("swiften-config", [
+		"swiften-config.cpp"
+	])
diff --git a/Swiften/Config/swiften-config.cpp b/Swiften/Config/swiften-config.cpp
new file mode 100644
index 0000000..832618d
--- /dev/null
+++ b/Swiften/Config/swiften-config.cpp
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <iostream>
+
+#include <boost/program_options/options_description.hpp>
+#include <boost/program_options/variables_map.hpp>
+#include <boost/program_options.hpp>
+#include <boost/version.hpp>
+
+#include <Swiften/Base/String.h>
+#include <Swiften/Base/foreach.h>
+#include <Swiften/Base/Platform.h>
+#include <Swiften/Base/Paths.h>
+
+#include "swiften-config.h"
+
+using namespace Swift;
+
+void printFlags(const std::vector<String>& flags) {
+	for (size_t i = 0; i < flags.size(); ++i) {
+		if (i > 0) {
+			std::cout << " ";
+		}
+		std::cout << flags[i];
+	}
+	std::cout << std::endl;
+}
+
+int main(int argc, char* argv[]) {
+	boost::program_options::options_description desc;
+	desc.add_options()
+		("help", "Show this help message")
+		("libs", "List the library flags")
+		("cflags", "List the compiler & preprocessor flags")
+	;
+	boost::program_options::variables_map vm;
+	try {
+		boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
+	} catch (boost::program_options::unknown_option option) {
+#if BOOST_VERSION >= 104200
+		std::cout << "Ignoring unknown option " << option.get_option_name() << " but continuing." <<  std::endl;
+#else
+		std::cout << "Error: " << option.what() << " (continuing)" <<  std::endl;
+#endif
+	}
+	boost::program_options::notify(vm);
+
+	if (vm.count("help") > 0) {
+		std::cout << desc << "\n";
+		return 0;
+	}
+
+	// Read in all variables
+	std::vector<String> libs;
+	for (size_t i = 0; LIBFLAGS[i]; ++i) {
+		libs.push_back(LIBFLAGS[i]);
+	}
+	std::vector<String> cflags;
+	for (size_t i = 0; CPPFLAGS[i]; ++i) {
+		cflags.push_back(CPPFLAGS[i]);
+	}
+
+	// Detect whether we're running in-place or not
+	boost::filesystem::path executablePath = Paths::getExecutablePath();
+	boost::filesystem::path topPath = executablePath / ".." / "..";
+	bool inPlace = true;	
+
+	// Replace "#" variables with the correct path
+	for(size_t i = 0; i < libs.size(); ++i) {
+		if (inPlace) {
+			String lib = libs[i];
+			lib.replaceAll('#', topPath.string());
+			libs[i] = lib;
+		}
+		else {
+			// TODO
+		}
+	}
+	for(size_t i = 0; i < cflags.size(); ++i) {
+		if (inPlace) {
+			String cflag = cflags[i];
+			cflag.replaceAll('#', topPath.string());
+			cflags[i] = cflag;
+		}
+		else {
+			// TODO
+		}
+	}
+
+
+	// Print the requested variable
+	if (vm.count("libs") > 0) {
+		printFlags(libs);
+	}
+	else if (vm.count("cflags") > 0) {
+		printFlags(cflags);
+	}
+	return 0;
+}
diff --git a/Swiften/SConscript b/Swiften/SConscript
index 080e88e..11dec59 100644
--- a/Swiften/SConscript
+++ b/Swiften/SConscript
@@ -157,6 +157,7 @@ if env["SCONS_STAGE"] == "build" :
 			"LinkLocal",
 			"StreamManagement",
 			"Component",
+			"Config",
 		])
 	SConscript(test_only = True, dirs = [
 			"QA",
-- 
cgit v0.10.2-6-g49f6