From cf160bdab6595e9888f23bf9df0cf03613068240 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 5 Jan 2018 05:27:17 +0100 Subject: Get cflags from pkg config dependency --- .gitignore | 1 + backend/ninja/Ninja.cpp | 29 ++++++++++++++++------- backend/ninja/Ninja.hpp | 2 +- include/Linker.hpp | 1 + include/PkgConfig.hpp | 8 +++++++ include/Result.hpp | 8 +++++++ project.conf | 2 +- src/CmakeModule.cpp | 1 + src/PkgConfig.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++- 9 files changed, 102 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 1ea6946..67847e9 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ sibs-build/ sibs.kdev4 build/ cmake/build/ +cmake-build-debug/ diff --git a/backend/ninja/Ninja.cpp b/backend/ninja/Ninja.cpp index 1990926..50cd577 100644 --- a/backend/ninja/Ninja.cpp +++ b/backend/ninja/Ninja.cpp @@ -238,7 +238,7 @@ namespace backend // TODO: First check if pkg-config is installed. If it's not, only check dependencies that exists in the dependencies sub directory. // If pkg-config is installed and dependency is not installed, check in dependencies sub directory. - Result Ninja::getLinkerFlags(const SibsConfig &config, LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, LinkerFlagCallbackFunc dynamicLinkerFlagCallback, GlobalIncludeDirCallbackFunc globalIncludeDirCallback) const + Result Ninja::getLinkerFlags(const SibsConfig &config, LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, LinkerFlagCallbackFunc dynamicLinkerFlagCallback, GlobalIncludeDirCallbackFunc globalIncludeDirCallback, CflagsCallbackFunc cflagsCallbackFunc) const { const vector &dependencies = config.getDependencies(); if(dependencies.empty()) return Result::Ok(true); @@ -268,10 +268,10 @@ namespace backend } } - Result pkgConfigLinkerFlagsResult = PkgConfig::getDynamicLibsLinkerFlags(pkgConfigDependencies); - if (pkgConfigLinkerFlagsResult.isErr()) + Result pkgConfigFlagsResult = PkgConfig::getDynamicLibsFlags(pkgConfigDependencies); + if (!pkgConfigFlagsResult) { - printf("%s, using global lib...\n", pkgConfigLinkerFlagsResult.getErrMsg().c_str()); + printf("%s, using global lib...\n", pkgConfigFlagsResult.getErrMsg().c_str()); globalLibDependencies.reserve(globalLibDependencies.size() + pkgConfigDependencies.size()); for (const Dependency &pkgConfigDependency : pkgConfigDependencies) { @@ -281,8 +281,11 @@ namespace backend } else { - if (!pkgConfigLinkerFlagsResult.unwrap().empty()) - dynamicLinkerFlagCallback(pkgConfigLinkerFlagsResult.unwrap()); + const PkgConfigFlags &pkgConfigFlag = pkgConfigFlagsResult.unwrap(); + if (!pkgConfigFlag.linkerFlags.empty()) + dynamicLinkerFlagCallback(pkgConfigFlag.linkerFlags); + if(!pkgConfigFlag.cflags.empty()) + cflagsCallbackFunc(pkgConfigFlag.cflags); } #else for (const Dependency &dependency : dependencies) @@ -381,8 +384,15 @@ namespace backend allLinkerFlags += " "; allLinkerFlags += linkerFlag; }; + + string cflags; + auto cflagsCallbackFunc = [&cflags](const string &dependencyCflags) + { + cflags += " "; + cflags += dependencyCflags; + }; - Result linkerFlags = getLinkerFlags(config, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallback, globalIncludeDirCallback); + Result linkerFlags = getLinkerFlags(config, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallback, globalIncludeDirCallback, cflagsCallbackFunc); if (linkerFlags.isErr()) return Result::Err(linkerFlags.getErrMsg()); @@ -587,14 +597,15 @@ namespace backend case Compiler::GCC: { // -Werror - result += " '-I" + config.getPackageName() + "@exe' '-I..' -Wall -Wextra -Werror=return-type -fexceptions -fdiagnostics-show-option '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Winvalid-pch' '-Wnon-virtual-dtor' " + optimizationFlags + " '-g'"; + result += " '-I" + config.getPackageName() + "@exe' " + cflags + " '-I..' -Wall -Wextra -Werror=return-type -fexceptions -fdiagnostics-show-option '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Winvalid-pch' '-Wnon-virtual-dtor' " + optimizationFlags + " '-g'"; break; } case Compiler::MSVC: { result += " "; result += optimizationFlags; - result += " /EHsc"; + result += " /EHsc "; + result += cflags; switch (config.getOptimizationLevel()) { case OPT_LEV_DEBUG: diff --git a/backend/ninja/Ninja.hpp b/backend/ninja/Ninja.hpp index eeea1be..fc6842f 100644 --- a/backend/ninja/Ninja.hpp +++ b/backend/ninja/Ninja.hpp @@ -34,7 +34,7 @@ namespace backend bool containsSourceFile(const std::string &filepath) const; bool containsTestSourceDir(const std::string &dir) const; bool containsDependency(const std::string &dependency) const; - sibs::Result getLinkerFlags(const sibs::SibsConfig &config, sibs::LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, sibs::LinkerFlagCallbackFunc dynamicLinkerFlagCallback, sibs::GlobalIncludeDirCallbackFunc globalIncludeDirCallback) const; + sibs::Result getLinkerFlags(const sibs::SibsConfig &config, sibs::LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, sibs::LinkerFlagCallbackFunc dynamicLinkerFlagCallback, sibs::GlobalIncludeDirCallbackFunc globalIncludeDirCallback, sibs::CflagsCallbackFunc cflagsCallbackFunc) const; sibs::Result compile(const _tinydir_char_t *buildFilePath); private: std::vector sourceFiles; diff --git a/include/Linker.hpp b/include/Linker.hpp index 90bebe2..acf31e4 100644 --- a/include/Linker.hpp +++ b/include/Linker.hpp @@ -7,6 +7,7 @@ namespace sibs { using LinkerFlagCallbackFunc = std::function; using GlobalIncludeDirCallbackFunc = std::function; + using CflagsCallbackFunc = std::function; } #endif //SIBS_LINKER_HPP diff --git a/include/PkgConfig.hpp b/include/PkgConfig.hpp index e525843..e41b216 100644 --- a/include/PkgConfig.hpp +++ b/include/PkgConfig.hpp @@ -10,6 +10,12 @@ namespace sibs { + struct PkgConfigFlags + { + std::string linkerFlags; + std::string cflags; + }; + class PkgConfig { public: @@ -17,6 +23,8 @@ namespace sibs static Result validatePackageExists(const std::string &name); static Result validatePackageVersionAtLeast(const std::string &name, const std::string &version); static Result getDynamicLibsLinkerFlags(const std::vector &libs); + static Result getDynamicLibsCflags(const std::vector &libs); + static Result getDynamicLibsFlags(const std::vector &libs); }; } #endif // OS_FAMILY_POSIX diff --git a/include/Result.hpp b/include/Result.hpp index e8f4d12..22cee35 100644 --- a/include/Result.hpp +++ b/include/Result.hpp @@ -16,6 +16,13 @@ namespace sibs result.errorCode = 0; return result; } + + static Result Ok(const T &&value) + { + Result result(value); + result.errorCode = 0; + return result; + } template static Result Err(const Result &other) @@ -57,6 +64,7 @@ namespace sibs operator bool () { return isOk(); } private: Result(const T &_value = T()) : value(_value) {} + Result(const T &&_value) : value(_value) {} private: T value; std::string errMsg; diff --git a/project.conf b/project.conf index 93665a5..2271dbe 100644 --- a/project.conf +++ b/project.conf @@ -5,7 +5,7 @@ version = "0.1.3" authors = ["DEC05EBA <0xdec05eba@gmail.com>"] tests = "tests" platforms = ["linux32", "linux64", "win64"] -ignore_dirs = ["cmake"] +ignore_dirs = ["cmake", "cmake-build-debug", "build"] [dependencies] libcurl = "7.57.0" diff --git a/src/CmakeModule.cpp b/src/CmakeModule.cpp index d82eaf6..8606a42 100644 --- a/src/CmakeModule.cpp +++ b/src/CmakeModule.cpp @@ -78,6 +78,7 @@ namespace sibs } } + // TODO: Use getDynamicLibsFlags instead which includes cflags? Result pkgConfigLinkerFlagsResult = PkgConfig::getDynamicLibsLinkerFlags(pkgConfigDependencies); if (pkgConfigLinkerFlagsResult.isErr()) { diff --git a/src/PkgConfig.cpp b/src/PkgConfig.cpp index f9f8f9b..9a19387 100644 --- a/src/PkgConfig.cpp +++ b/src/PkgConfig.cpp @@ -16,7 +16,7 @@ namespace sibs if(!isspace(input[i])) return input.substr(0, i + 1); } - return input; + return ""; } #if OS_FAMILY == OS_FAMILY_POSIX @@ -143,5 +143,65 @@ namespace sibs return Result::Err(errMsg); } } + + Result PkgConfig::getDynamicLibsCflags(const vector &libs) + { + if(libs.empty()) return Result::Ok(""); + + string args; + for(const Dependency &lib : libs) + { + args += " '"; + args += lib.name; + args += "'"; + } + + string command = "pkg-config --cflags"; + command += args; + Result execResult = exec(command.c_str()); + if(execResult.isErr()) + return Result::Err(execResult.getErrMsg()); + + if(execResult.unwrap().exitCode == 0) + { + execResult.unwrap().execStdout = trimRight(execResult.unwrap().execStdout); + return Result::Ok(execResult.unwrap().execStdout); + } + else if(execResult.unwrap().exitCode == 1) + { + // TODO: This shouldn't happen because we check if each dependency is installed before this, + // but maybe the package is uninstalled somewhere between here... + // Would be better to recheck if each package is installed here again + // to know which package was uninstalled + return Result::Err("Packages not found"); + } + else if(execResult.unwrap().exitCode == 127) + { + return Result::Err("pkg-config is not installed"); + } + else + { + string errMsg = "Failed to get package dynamic lib cflags, Unknown error, exit code: "; + errMsg += to_string(execResult.unwrap().exitCode); + return Result::Err(errMsg); + } + } + + Result PkgConfig::getDynamicLibsFlags(const vector &libs) + { + PkgConfigFlags flags; + + Result linkerFlagsResult = getDynamicLibsLinkerFlags(libs); + if(!linkerFlagsResult) + return Result::Err(linkerFlagsResult); + + Result cflagsResult = getDynamicLibsCflags(libs); + if(!cflagsResult) + return Result::Err(cflagsResult); + + flags.linkerFlags = move(linkerFlagsResult.unwrap()); + flags.cflags = move(cflagsResult.unwrap()); + return Result::Ok(flags); + } } #endif // OS_FAMILY_POSIX -- cgit v1.2.3