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 --- src/CmakeModule.cpp | 1 + src/PkgConfig.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) (limited to 'src') 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