diff options
| author | ruki <[email protected]> | 2020-03-26 22:40:34 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-03-26 10:03:24 +0800 |
| commit | 92fce96e6c49ac88824f9266a1a4b5023167b803 (patch) | |
| tree | 85e359643ed4ca841b7b0b38d14c0b2f04cd0923 | |
| parent | 5794b2278e36d1d7b386c8530f9d10b6b2627c37 (diff) | |
improve find_package for brew/cmake
| -rw-r--r-- | xmake/modules/package/manager/brew/find_package.lua | 56 |
1 files changed, 43 insertions, 13 deletions
diff --git a/xmake/modules/package/manager/brew/find_package.lua b/xmake/modules/package/manager/brew/find_package.lua index 06f6711c0..ea3fa09c8 100644 --- a/xmake/modules/package/manager/brew/find_package.lua +++ b/xmake/modules/package/manager/brew/find_package.lua @@ -21,9 +21,32 @@ -- imports import("lib.detect.find_tool") import("lib.detect.find_file") +import("lib.detect.find_path") import("lib.detect.pkg_config") +import("core.project.target") import("package.manager.find_package") +-- get the root directory of the brew packages +function _brew_pkg_rootdir() + local brew_pkg_rootdir = _g.brew_pkg_rootdir + if brew_pkg_rootdir == nil then + local brew = find_tool("brew") + if brew then + brew_pkg_rootdir = try + { + function () + return os.iorunv(brew.program, {"--prefix"}) + end + } or "/usr/local" + end + if brew_pkg_rootdir then + brew_pkg_rootdir = brew_pkg_rootdir:trim() + end + _g.brew_pkg_rootdir = brew_pkg_rootdir or false + end + return brew_pkg_rootdir or nil +end + -- find package from the brew package manager -- -- @param name the package name, e.g. zlib, pcre/libpcre16 @@ -31,23 +54,20 @@ import("package.manager.find_package") -- function main(name, opt) - -- find brew - local brew = find_tool("brew") - if not brew then - return + -- find the prefix directory of brew + local brew_pkg_rootdir = _brew_pkg_rootdir() + if not brew_pkg_rootdir then + return end + brew_pkg_rootdir = path.join(brew_pkg_rootdir, opt.plat == "macosx" and "Cellar" or "opt") -- parse name, e.g. pcre/libpcre16 local nameinfo = name:split('/') local pcname = nameinfo[2] or nameinfo[1] - -- find the prefix directory of brew - local brew_pkg_root = try { function () return os.iorunv(brew.program, {"--prefix"}) end } or "/usr/local" - brew_pkg_root = path.join(brew_pkg_root:trim(), opt.plat == "macosx" and "Cellar" or "opt") - -- find package from pkg-config/*.pc local result = nil - local pcfile = find_file(pcname .. ".pc", path.join(brew_pkg_root, nameinfo[1], "*/lib/pkgconfig")) + local pcfile = find_file(pcname .. ".pc", path.join(brew_pkg_rootdir, nameinfo[1], "*/lib/pkgconfig")) if pcfile then opt.configdirs = path.directory(pcfile) result = find_package("pkg_config::" .. pcname, opt) @@ -64,10 +84,20 @@ function main(name, opt) -- find package from xxx/lib, xxx/include if not result then - local libfile = find_file("*.a", path.join(brew_pkg_root, nameinfo[1], "*", "lib")) - if libfile then - opt.linkdirs = path.directory(libfile) - opt.includedirs = path.join(path.directory(opt.linkdirs), "include") + local pkgdir = find_path("lib", path.join(brew_pkg_rootdir, nameinfo[1], "*")) + if pkgdir then + local links = {} + for _, libfile in ipairs(os.files(path.join(pkgdir, "lib", "*.a"))) do + table.insert(links, target.linkname(path.filename(libfile))) + end + for _, libfile in ipairs(os.files(path.join(pkgdir, "lib", opt.plat == "macosx" and "*.dylib" or "*.so"))) do + if not os.islink(libfile) then + table.insert(links, target.linkname(path.filename(libfile))) + end + end + opt.links = links + opt.linkdirs = path.join(pkgdir, "lib") + opt.includedirs = path.join(pkgdir, "include") result = find_package("system::" .. name, opt) end end |
