Add descriptive error message when pkg-config fails

The pkg_config helper for premake was not checking for errors. When it
failed to run, either when it is not installed or the queried package is
not found, a cryptic error message would be printed:

"Error: .../xenia/third_party/premake-core/src/base/string.lua:36: attempt to index a nil value (upvalue 's')"

Fix this by checking the return status when calling pkg-config and
printing a descriptive error message.
This commit is contained in:
Alex Messier 2022-02-22 23:12:41 -05:00 committed by Rick Gibbed
parent 8d02c5ab21
commit 53320d7ef2
1 changed files with 13 additions and 4 deletions

View File

@ -2,12 +2,21 @@
pkg_config = {} pkg_config = {}
local function pkg_config_call(lib, what)
local result, code = os.outputof("pkg-config --"..what.." "..lib)
if result then
return result
else
error("Failed to run 'pkg-config' for library '"..lib.."'. Are the development files installed?")
end
end
function pkg_config.cflags(lib) function pkg_config.cflags(lib)
if not os.istarget("linux") then if not os.istarget("linux") then
return return
end end
buildoptions({ buildoptions({
({os.outputof("pkg-config --cflags "..lib)})[1], pkg_config_call(lib, "cflags"),
}) })
end end
@ -16,12 +25,12 @@ function pkg_config.lflags(lib)
return return
end end
linkoptions({ linkoptions({
({os.outputof("pkg-config --libs-only-L " ..lib)})[1], pkg_config_call(lib, "libs-only-L"),
({os.outputof("pkg-config --libs-only-other "..lib)})[1], pkg_config_call(lib, "libs-only-other"),
}) })
-- We can't just drop the stdout of the `--libs` command in -- We can't just drop the stdout of the `--libs` command in
-- linkoptions because library order matters -- linkoptions because library order matters
local output = ({os.outputof("pkg-config --libs-only-l "..lib)})[1] local output = pkg_config_call(lib, "libs-only-l")
for k, flag in next, string.explode(output, " ") do for k, flag in next, string.explode(output, " ") do
-- remove "-l" -- remove "-l"
if flag ~= "" then if flag ~= "" then