+ return trim(str)
+end
+
+-- Get the CFLAGS from pkg-config for the passed libraries.
+function pkg_config_cflags(libs)
+ local env = "PKG_CONFIG_PATH="..libdir.."/pkgconfig:$PKG_CONFIG_PATH"
+ local cmd = env.." pkg-config"
+ return backtick_run(cmd.." --cflags "..libs)
+end
+
+-- Get the LDFLAGS from pkg-config for the passed libraries.
+function pkg_config_ldflags(libs)
+ local env = "PKG_CONFIG_PATH="..libdir.."/pkgconfig:$PKG_CONFIG_PATH"
+ local cmd = env.." pkg-config"
+ return (" "..backtick_run(cmd.." --libs "..libs)):gsub("%s%-l%S*", "")
+end
+
+-- Get the LIBS flags from pkg-config for the passed libraries.
+function pkg_config_libs(libs)
+ local env = "PKG_CONFIG_PATH="..libdir.."/pkgconfig:$PKG_CONFIG_PATH"
+ local cmd = env.." pkg-config"
+ return backtick_run(cmd.." --libs-only-l "..libs)
+end
+
+-- Add a flag to the CFLAGS and CXXFLAGS variables.
+function add_cflag(flag)
+ if CFLAGS == "" then
+ CFLAGS = flag
+ else
+ CFLAGS = string.format("%s %s", CFLAGS, flag)
+ end
+ if CXXFLAGS == "" then
+ CXXFLAGS = flag
+ else
+ CXXFLAGS = string.format("%s %s", CXXFLAGS, flag)
+ end
+end
+
+-- Replace a flag in the CFLAGS and CXXFLAGS variables.
+function set_cflag(flag)
+ local cflag_set, cxxflag_set = false, false
+ CFLAGS = CFLAGS:gsub("%"..flag:sub(1, 2).."%S+", function()
+ cflag_set = true
+ return flag
+ end)
+ CXXFLAGS = CXXFLAGS:gsub("%"..flag:sub(1, 2).."%S+", function()
+ cxxflag_set = true
+ return flag
+ end)
+ if not cflag_set or not cxxflag_set then add_cflag(flag) end
+end
+
+-- Look for a command from a list that can complete successfully (with exit
+-- code zero) given some arguments. Returns nil if none were successful.
+function find_command(commands, args)
+ if not args then args = "" end
+ for i,command in ipairs(commands) do
+ if try_run(command.." "..args) then return command end
+ end
+ return nil