1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
local pkg_config_command = "pkg-config"
if os.getenv("PKG_CONFIG") then
pkg_config_command = os.getenv("PKG_CONFIG")
end
local pkg_config_path = ""
if os.getenv("PKG_CONFIG_PATH") then
pkg_config_path = os.getenv("PKG_CONFIG_PATH")
end
local static_link_libs = false
local function os_capture(cmd)
return io.popen(cmd, 'r'):read('*a'):gsub("\n", " ")
end
local function parse_pkg_config_includes(lib, alternative_cmd, alternative_flags)
local result
if not alternative_cmd then
result = os_capture("PKG_CONFIG_PATH=" .. pkg_config_path .. " " .. pkg_config_command .. " --cflags " .. lib)
else
if not alternative_flags then
result = os_capture(alternative_cmd.." --cflags")
else
result = os_capture(alternative_cmd.." "..alternative_flags)
end
end
-- Small trick: delete the space after -include so that we can detect
-- which files have to be force-included without difficulty.
result = result:gsub("%-include +(%g+)", "-include%1")
result = result:gsub("%-isystem +(%g+)", "-isystem%1")
local dirs = {}
local files = {}
local options = {}
for w in string.gmatch(result, "[^' ']+") do
if string.sub(w,1,2) == "-I" then
table.insert(dirs, string.sub(w,3))
elseif string.sub(w,1,8) == "-isystem" then
table.insert(dirs, string.sub(w,9))
elseif string.sub(w,1,8) == "-include" then
table.insert(files, string.sub(w,9))
else
table.insert(options, w)
end
end
return dirs, files, options
end
local function parse_pkg_config_links(lib, alternative_cmd, alternative_flags)
local result
if not alternative_cmd then
local static = static_link_libs and " --static " or ""
result = os_capture("PKG_CONFIG_PATH=" .. pkg_config_path .. " " .. pkg_config_command .. " --libs " .. static .. lib)
else
if not alternative_flags then
result = os_capture(alternative_cmd.." --libs")
else
result = os_capture(alternative_cmd.." "..alternative_flags)
end
end
-- On OSX, wx-config outputs "-framework foo" instead of "-Wl,-framework,foo"
-- which doesn't fare well with the splitting into libs, libdirs and options
-- we perform afterwards.
result = result:gsub("%-framework +(%g+)", "-Wl,-framework,%1")
local libs = {}
local dirs = {}
local options = {}
for w in string.gmatch(result, "[^' ']+") do
if string.sub(w,1,2) == "-l" then
table.insert(libs, string.sub(w,3))
elseif string.sub(w,1,2) == "-L" then
table.insert(dirs, string.sub(w,3))
else
table.insert(options, w)
end
end
return dirs, libs, options
end
-- ----------------------------------------------------------------------------
-- Public API
-- ----------------------------------------------------------------------------
local m = {}
m._VERSION = "2.0.0"
--- Append path to PKG_CONFIG_PATH
-- @param path string Path to append to PKG_CONFIG_PATH
function m.add_pkg_config_path(path)
if pkg_config_path == "" then
pkg_config_path = path
else
pkg_config_path = pkg_config_path .. ":" .. path
end
end
--- Set whether to link libraries statically.
-- @param value boolean True for static linking, false otherwise.
function m.set_static_link_libs(value)
static_link_libs = value
end
--- Add include directories and build options for a library.
-- @param lib string The library name.
-- @param alternative_cmd string|nil Optional alternative command.
-- @param alternative_flags string|nil Optional alternative flags.
function m.add_includes(lib, alternative_cmd, alternative_flags)
local dirs, files, options = parse_pkg_config_includes(lib, alternative_cmd, alternative_flags)
externalincludedirs(dirs)
forceincludes(files)
buildoptions(options)
end
--- Add include directories after the default ones (used for overrides).
-- @param lib string The library name.
-- @param alternative_cmd string|nil Optional alternative command.
-- @param alternative_flags string|nil Optional alternative flags.
function m.add_includes_after(lib, alternative_cmd, alternative_flags)
local dirs, files, options = parse_pkg_config_includes(lib, alternative_cmd, alternative_flags)
includedirsafter(dirs)
forceincludes(files)
buildoptions(options)
end
--- Add library directories, libraries, and link options for a library.
-- @param lib string The library name.
-- @param alternative_cmd string|nil Optional alternative command.
-- @param alternative_flags string|nil Optional alternative flags.
function m.add_links(lib, alternative_cmd, alternative_flags)
local dirs, libs, options = parse_pkg_config_links(lib, alternative_cmd, alternative_flags)
libdirs(dirs)
links(libs)
linkoptions(options)
end
return m
|