module(..., package.seeall)

function Configure(name, constructor)
  local fh = assert(io.popen("pkg-config " .. name .. " --cflags --libs"))
  local data = fh:read("*all")
  fh:close()

  local cpppath = {}
  local libpath = {}
  local libs = {}

  for kind, value in data:gmatch("-([ILl])([^ ]+)") do
    if kind == "I" then
      cpppath[#cpppath + 1] = value
    elseif kind == "L" then
      libpath[#libpath + 1] = value
    elseif kind == "l" then
      libs[#libs + 1] = value
    end
  end

  -- We don't have access to ExternalLibrary here - user has to pass it in.
  return constructor({
    Name = name,
    Propagate = {
      Env = {
        CPPPATH = cpppath,
        LIBS    = libs,
        LIBPATH = libpath
      }
    }
  })
end


module(..., package.seeall)

function ConfigureRaw(cmdline, name, constructor)
  local fh = assert(io.popen(cmdline))
  local data = fh:read("*all")
  fh:close()

  local cpppath = {}
  local libpath = {}
  local libs = {}
  local defines = {}

  for kind, value in data:gmatch("-([ILlD])([^ \n\r]+)") do
    if kind == "I" then
      cpppath[#cpppath + 1] = value
    elseif kind == "D" then
      defines[#defines + 1] = value
    elseif kind == "L" then
      libpath[#libpath + 1] = value
    elseif kind == "l" then
      libs[#libs + 1] = value
    end
  end

  -- We don't have access to ExternalLibrary here - user has to pass it in.
  return constructor({
    Name = name,
    Propagate = {
      Env = {
        CPPDEFS = defines,
        CPPPATH = cpppath,
        LIBS    = libs,
        LIBPATH = libpath
      }
    }
  })
end

function Configure(name, ctor)
  return internal_cfg("pkg-config " .. name .. " --cflags --libs", name, ctor)
end

function ConfigureWithTool(tool, name, ctor)
  return internal_cfg(tool .. " --cflags --libs", name, ctor)
end

Allow running other tools than pkg-config.
Allow running other tools than pkg-config.

Useful for e.g. wx-config.
Clean up option handling to ignore newlines and other junk
