xmake.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package("postgresql")
  2. set_homepage("https://www.postgresql.org/")
  3. set_description("PostgreSQL Database Management System")
  4. on_fetch(function (package, opt)
  5. if opt.system then
  6. import("lib.detect.find_path")
  7. import("lib.detect.find_program")
  8. import("lib.detect.find_library")
  9. -- init search paths
  10. local paths = {}
  11. if package:is_plat("windows") then
  12. local regs = winos.registry_keys("HKEY_LOCAL_MACHINE\\SOFTWARE\\PostgreSQL\\Installations\\postgresql-x64-*")
  13. for _, reg in ipairs(regs) do
  14. table.insert(paths, winos.registry_query(reg .. ";Base Directory"))
  15. end
  16. elseif package:is_plat("macosx") then
  17. for _, path in ipairs(os.dirs("/Library/PostgreSQL/*")) do
  18. table.insert(paths, path)
  19. end
  20. elseif package:is_plat("linux") then
  21. for _, path in ipairs(os.dirs("/usr/lib/postgresql/*")) do
  22. table.insert(paths, path)
  23. end
  24. end
  25. -- find programs
  26. local binfile = find_program("postgres", {paths = os.getenv("PATH")})
  27. if binfile then
  28. local packagedir = path.directory(path.directory(binfile))
  29. table.insert(paths, packagedir)
  30. package:addenv("PATH", path.join(packagedir, "bin"))
  31. end
  32. -- find library
  33. local result = {links = {}, linkdirs = {}, includedirs = {}, libfiles = {}}
  34. local libname = (package:is_plat("windows") and "libpq" or "pq")
  35. local linkinfo = find_library(libname, paths, {suffixes = "lib"})
  36. if linkinfo then
  37. table.insert(result.linkdirs, linkinfo.linkdir)
  38. table.insert(result.links, libname)
  39. if package:is_plat("windows") then
  40. table.insert(result.libfiles, path.join(linkinfo.linkdir, "libpq.lib"))
  41. table.insert(result.libfiles, path.join(linkinfo.linkdir, "libpq.dll"))
  42. end
  43. end
  44. -- find headers
  45. local path = find_path("libpq-fe.h", paths, {suffixes = "include"})
  46. if path then
  47. table.insert(result.includedirs, path)
  48. end
  49. path = find_path("postgres.h", paths, {suffixes = "include/server"})
  50. if path then
  51. table.insert(result.includedirs, path)
  52. end
  53. if #result.includedirs > 0 and #result.linkdirs > 0 then
  54. return result
  55. end
  56. end
  57. end)