From 4bb8c7fb366dffe90ef91b5d8b1acfcd35a0369d Mon Sep 17 00:00:00 2001 From: Foe Date: Thu, 15 Aug 2024 23:21:01 +0200 Subject: [PATCH] Make PostgreSQL premake script version-agnostic (#4) --- Dependencies/libpqxx/Libpqxx.lua | 73 ++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/Dependencies/libpqxx/Libpqxx.lua b/Dependencies/libpqxx/Libpqxx.lua index 3a16c86..b196fe0 100644 --- a/Dependencies/libpqxx/Libpqxx.lua +++ b/Dependencies/libpqxx/Libpqxx.lua @@ -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", {}) @@ -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) \ No newline at end of file + Solution.Util.SetLinks({ dep.Name, foundLibs }) +end)