Skip to content

Commit

Permalink
Make PostgreSQL premake script version-agnostic (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Foereaper authored Aug 15, 2024
1 parent a49d005 commit 4bb8c7f
Showing 1 changed file with 50 additions and 23 deletions.
73 changes: 50 additions & 23 deletions Dependencies/libpqxx/Libpqxx.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
local postgresRoot = ""
-- TODO: should probably not hardcode postgres dir for Linux but oh well
if os.target() == "windows" then
local osEnvName = "POSTGRES_ROOT"
postgresRoot = os.getenv(osEnvName)

if not postgresRoot then
Solution.Util.PrintError("Failed to find System Environment Variable '" .. osEnvName .. ". Please ensure Postgres is installed and that " .. osEnvName .. " have been properly configured")
local function getPostgresInfo()
local includeDirs = {}
local libDirs = {}
local libs = {}

if os.target() == "windows" then
local osEnvName = "POSTGRES_ROOT"
local postgresRoot = os.getenv(osEnvName)

if not postgresRoot then
Solution.Util.PrintError("Failed to find System Environment Variable '" .. osEnvName .. "'. Please ensure Postgres is installed and that " .. osEnvName .. " has been properly configured.")
else
print("Found PostgreSQL at: " .. postgresRoot)
table.insert(includeDirs, postgresRoot .. "/include")
table.insert(libs, postgresRoot .. "/lib/libpq.lib")
end
else
-- Fetch PostgreSQL include, lib paths and version using pg_config on Linux
local pgInclude = io.popen("pg_config --includedir"):read("*a"):gsub("\n", "")
local pgIncludeServer = io.popen("pg_config --includedir-server"):read("*a"):gsub("\n", "")
local pgLibDir = io.popen("pg_config --libdir"):read("*a"):gsub("\n", "")
local postgresVersion = io.popen("pg_config --version"):read("*a"):gsub("\n", "")

if pgInclude == "" or pgLibDir == "" then
Solution.Util.PrintError("Failed to find PostgreSQL installation using 'pg_config'. Please ensure PostgreSQL is installed.")
else
print("PostgreSQL version: " .. postgresVersion)
print("PostgreSQL include dir: " .. pgInclude)
table.insert(includeDirs, pgInclude)
print("PostgreSQL include-server dir: " .. pgIncludeServer)
table.insert(includeDirs, pgIncludeServer)
print("PostgreSQL lib dir: " .. pgLibDir)
table.insert(libDirs, pgLibDir)
table.insert(libs, "pq")
end
end

return includeDirs, libDirs, libs
end

local dep = Solution.Util.CreateDepTable("Libpqxx", {})
Expand All @@ -19,30 +48,28 @@ Solution.Util.CreateStaticLib(dep.Name, Solution.Projects.Current.BinDir, dep.De

local baseDir = dep.Path .. "/Libpqxx"
local sourceDir = baseDir .. "/src"
local pqinclude = iif(os.istarget("windows"), postgresRoot .. "/include", { "/usr/include/postgresql", "/usr/include/postgresql/14/server" })

if os.target() == "linux" then
Solution.Util.SetLibDirs({ "/usr/lib/x86_64-linux-gnu", "/usr/lib/postgresql/14/lib" })
end

local includeDirs = { baseDir .. "/include", pqinclude }

-- Include directories setup
local files =
{
sourceDir .. "/**.cxx",
}

Solution.Util.SetFiles(files)

-- Get libraries and includes from system
local foundIncludeDirs, foundLibDirs, foundLibs = getPostgresInfo()
local includeDirs = { baseDir .. "/include", foundIncludeDirs }
Solution.Util.SetIncludes(includeDirs)
Solution.Util.SetDefines(defines)

local libPath = iif(os.istarget("windows"), postgresRoot .. "/lib/libpq.lib", "pq")
Solution.Util.SetLinks(libPath)

-- Library link setup
Solution.Util.SetLibDirs(foundLibDirs)
Solution.Util.SetLinks(foundLibs)
end)

Solution.Util.CreateDep(dep.NameLow, dep.Dependencies, function()
local baseDir = dep.Path .. "/Libpqxx"

local _, _, foundLibs = getPostgresInfo()
Solution.Util.SetIncludes({baseDir .. "/include"})
local libPath = iif(os.istarget("windows"), postgresRoot .. "/lib/libpq.lib", "pq")
Solution.Util.SetLinks({ dep.Name, libPath })
end)
Solution.Util.SetLinks({ dep.Name, foundLibs })
end)

0 comments on commit 4bb8c7f

Please sign in to comment.