diff options
| author | ruki <[email protected]> | 2017-07-21 10:44:50 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-07-21 10:44:50 +0800 |
| commit | 5bac5ca75bfcbf449007ce5f8a94bdce1b81b0bb (patch) | |
| tree | 0b57e2975d7ffec43e7b8ca504c1402888f01b2e | |
| parent | 84c43d1e02b430d4d207439803202246d459c1b8 (diff) | |
impl find_package from packagedirs
| -rw-r--r-- | xmake/core/project/project.lua | 20 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/lib/detect/find_package.lua | 107 |
2 files changed, 106 insertions, 21 deletions
diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index d2d467d88..fb0999e60 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -180,7 +180,7 @@ function project._api_add_packagedirs(interp, ...) end -- get interpreter -function project._interpreter() +function project.interpreter() -- the interpreter has been initialized? return it directly if project._INTERPRETER then @@ -384,7 +384,7 @@ function project.get(name) if not infos then -- get interpreter - local interp = project._interpreter() + local interp = project.interpreter() assert(interp) -- load infos @@ -402,7 +402,7 @@ end function project._load_targets() -- get interpreter - local interp = project._interpreter() + local interp = project.interpreter() assert(interp) -- enter the project directory @@ -467,7 +467,7 @@ end function project._load_options(disable_filter) -- get interpreter - local interp = project._interpreter() + local interp = project.interpreter() assert(interp) -- enter the project directory @@ -542,7 +542,7 @@ function project.get(name) if not infos then -- get interpreter - local interp = project._interpreter() + local interp = project.interpreter() assert(interp) -- load infos @@ -615,7 +615,7 @@ end function project.tasks() -- get interpreter - local interp = project._interpreter() + local interp = project.interpreter() assert(interp) -- the project file is not found? @@ -635,13 +635,7 @@ end -- get the mtimes function project.mtimes() - - -- get interpreter - local interp = project._interpreter() - assert(interp) - - -- get it - return interp:mtimes() + return project.interpreter():mtimes() end -- get the project menu diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_package.lua b/xmake/core/sandbox/modules/import/lib/detect/find_package.lua index 001bffb20..94529aa32 100644 --- a/xmake/core/sandbox/modules/import/lib/detect/find_package.lua +++ b/xmake/core/sandbox/modules/import/lib/detect/find_package.lua @@ -38,10 +38,97 @@ local import = require("sandbox/modules/import") local cache = require("sandbox/modules/import/lib/detect/cache") local pkg_config = import("lib.detect.pkg_config") --- find package from project package directories -function sandbox_lib_detect_find_package._find_from_project_packagedirs(name, opt) +-- find package from package directories +function sandbox_lib_detect_find_package._find_from_packagedirs(name, opt) - -- TODO + -- get package path (.e.g name.pkg) in the package directories + local packagepath = nil + for _, dir in ipairs(table.wrap(opt.packagedirs)) do + local p = path.join(dir, name .. ".pkg") + if os.isdir(p) then + packagepath = p + break + end + end + if not packagepath then + return + end + + -- get package file (.e.g name.pkg/xmake.lua) + local packagefile = path.join(packagepath, "xmake.lua") + if not os.isfile(packagefile) then + return + end + + -- get interpreter + local interp = project.interpreter() + + -- register filter handler + interp:filter():register("find_package_" .. name, function (variable) + + -- init maps + local maps = + { + arch = opt.arch + , plat = opt.plat + , mode = opt.mode + } + + -- get variable + return maps[variable] + end) + + -- load the package from the the package file + local packageinfos, errors = interp:load(packagefile, "option", true, true) + if not packageinfos then + raise(errors) + end + + -- clear filter handler + interp:filter():register("find_package_" .. name, nil) + + -- get package info + local packageinfo = packageinfos[name] + if not packageinfo then + return + end + + -- get linkdirs + local linkdirs = {} + for _, linkdir in ipairs(table.wrap(packageinfo.linkdirs)) do + table.insert(linkdirs, path.join(packagepath, linkdir)) + end + if #linkdirs == 0 then + return + end + + -- import find_library + local find_library = import("lib.detect.find_library") + + -- find library + local result = nil + for _, link in ipairs(table.wrap(packageinfo.links)) do + local libinfo = find_library(link, linkdirs) + if libinfo then + result = result or {} + result.links = table.join(result.links or {}, libinfo.link) + result.linkdirs = table.join(result.linkdirs or {}, libinfo.linkdir) + end + end + + -- inherit other package info + if result then + result.includedirs = {} + for _, includedir in ipairs(table.wrap(packageinfo.includedirs)) do + table.insert(result.includedirs, path.join(packagepath, includedir)) + end + for _, infoname in ipairs({"defines", "languages", "warnings"}) do + result[infoname] = packageinfo[infoname] + end + end + + -- ok? + return result end -- find package from repositories @@ -62,7 +149,7 @@ end -- find package from pkg-config function sandbox_lib_detect_find_package._find_from_pkg_config(name, opt) - return pkg_config.find(name, opt) + return import("lib.detect.pkg_config").find(name, opt) end -- find package from system directories @@ -90,7 +177,7 @@ function sandbox_lib_detect_find_package._find_from_systemdirs(name, opt) local pkginfo = nil local links = opt.links if not links then - pkginfo = pkg_config.info(name) + pkginfo = import("lib.detect.pkg_config").info(name) if pkginfo then links = pkginfo.links end @@ -125,7 +212,7 @@ function sandbox_lib_detect_find_package._find(name, opt) -- init find scripts local findscripts = { - sandbox_lib_detect_find_package._find_from_project_packagedirs + sandbox_lib_detect_find_package._find_from_packagedirs , sandbox_lib_detect_find_package._find_from_repositories , sandbox_lib_detect_find_package._find_from_modules } @@ -158,7 +245,10 @@ end -- find package -- -- @param name the package name --- @param opt the package options. e.g. {verbose = false, plat = "iphoneos", arch = "arm64", version = "1.0.1", pathes = {"/usr/lib"}, links = {"ssl"}, includes = {"ssl.h"}} +-- @param opt the package options. +-- e.g. { verbose = false, force = false, plat = "iphoneos", arch = "arm64", mode = "debug", version = "1.0.1", + -- pathes = {"/usr/lib"}, links = {"ssl"}, includes = {"ssl.h"} + -- packagedirs = {"/tmp/packages"}} -- -- @return {links = {"ssl", "crypto", "z"}, linkdirs = {"/usr/local/lib"}, includedirs = {"/usr/local/include"}} -- @@ -178,6 +268,7 @@ function sandbox_lib_detect_find_package.main(name, opt) opt = opt or {} opt.plat = opt.plat or config.get("plat") or os.host() opt.arch = opt.arch or config.get("arch") or os.arch() + opt.mode = opt.mode or config.get("mode") -- init cache key local key = "find_package_" .. opt.plat .. "_" .. opt.arch @@ -185,7 +276,7 @@ function sandbox_lib_detect_find_package.main(name, opt) -- attempt to get result from cache first local cacheinfo = cache.load(key) local result = cacheinfo[name] - if result ~= nil then + if result ~= nil and not opt.force then return utils.ifelse(result, result, nil) end |
