summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2018-09-23 21:03:42 +0800
committerruki <[email protected]>2018-09-23 21:03:42 +0800
commitd9d422c8fe2f838052fc2dd92b6801a81a515b34 (patch)
tree760c31d11c480a36ac0378824c270abda8b9d60b
parent6935be8876c358d89b5c5c2d5c0a56f4ea9d1590 (diff)
add envars for package
-rw-r--r--xmake/actions/require/action/install.lua16
-rw-r--r--xmake/actions/require/info.lua3
-rw-r--r--xmake/core/base/os.lua4
-rw-r--r--xmake/core/package/package.lua124
-rw-r--r--xmake/core/platform/environment.lua39
-rw-r--r--xmake/core/sandbox/modules/import/lib/detect/find_package.lua2
-rw-r--r--xmake/rules/wdk/load.lua4
7 files changed, 164 insertions, 28 deletions
diff --git a/xmake/actions/require/action/install.lua b/xmake/actions/require/action/install.lua
index 8339238e4..660256386 100644
--- a/xmake/actions/require/action/install.lua
+++ b/xmake/actions/require/action/install.lua
@@ -32,9 +32,9 @@ import("filter")
-- uninstall package from the prefix directory
function uninstall_prefix(package)
- -- remove the previous prefix files
+ -- remove the previous installed files
local prefixdir = package:prefixdir()
- for _, relativefile in ipairs(package:prefixlist()) do
+ for _, relativefile in ipairs(package:prefixinfo().installed) do
-- trace
vprint("removing %s ..", relativefile)
@@ -51,6 +51,9 @@ function uninstall_prefix(package)
end
end
+ -- unregister this package
+ package:unregister()
+
-- remove the prefix file
os.tryrm(package:prefixfile())
end
@@ -106,8 +109,13 @@ function install_prefix(package)
finally
{
function ()
- -- save the prefix list to file
- io.save(package:prefixfile(), relativefiles)
+ -- save the prefix info to file
+ local prefixinfo = package:prefixinfo()
+ prefixinfo.installed = relativefiles
+ io.save(package:prefixfile(), prefixinfo)
+
+ -- register this package
+ package:register()
end
}
}
diff --git a/xmake/actions/require/info.lua b/xmake/actions/require/info.lua
index 38544171b..6a13d67bc 100644
--- a/xmake/actions/require/info.lua
+++ b/xmake/actions/require/info.lua
@@ -115,6 +115,9 @@ function main(requires)
-- show prefix directory
cprint(" -> ${magenta}prefixdir${clear}: %s", instance:prefixdir())
+ -- show prefix file
+ cprint(" -> ${magenta}prefixfile${clear}: %s", instance:prefixfile())
+
-- show install directory
cprint(" -> ${magenta}installdir${clear}: %s", instance:installdir())
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 3ea8badc7..ac692c445 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -894,7 +894,7 @@ end
function os.setenv(name, ...)
-- get separator
- local seperator = utils.ifelse(os.host() == "windows", ';', ':')
+ local seperator = os.host() == "windows" and ';' or ':'
-- append values
return os._setenv(name, table.concat({...}, seperator))
@@ -904,7 +904,7 @@ end
function os.addenv(name, ...)
-- get separator
- local seperator = utils.ifelse(os.host() == "windows", ';', ':')
+ local seperator = os.host() == "windows" and ';' or ':'
-- append values
return os.setenv(name, table.concat({...}, seperator) .. seperator .. (os.getenv(name) or ""))
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua
index a07bfb375..6f0a6e4d0 100644
--- a/xmake/core/package/package.lua
+++ b/xmake/core/package/package.lua
@@ -75,6 +75,16 @@ function _instance:name()
return self._NAME
end
+-- get the platform of package
+function _instance:plat()
+ return config.get("plat") or os.host()
+end
+
+-- get the architecture of package
+function _instance:arch()
+ return config.get("arch") or os.arch()
+end
+
-- get the repository of this package
function _instance:repo()
return self._REPO
@@ -195,7 +205,7 @@ end
function _instance:prefixdir(...)
-- make the given prefix directory
- local dir = path.join(package.prefixdir(self:from("global")), is_debug and "debug" or "release", config.get("plat") or os.host(), config.get("arch") or os.arch(), ...)
+ local dir = path.join(package.prefixdir(self:from("global"), self:debug(), self:plat(), self:arch()), ...)
-- ensure the prefix directory
if not os.isdir(dir) then
@@ -204,18 +214,53 @@ function _instance:prefixdir(...)
return dir
end
--- get the prefix list
-function _instance:prefixlist()
- if self._PREFIXLIST == nil then
+-- get the prefix info
+function _instance:prefixinfo()
+ if self._PREFIXINFO == nil then
local prefixfile = self:prefixfile()
- self._PREFIXLIST = os.isfile(prefixfile) and io.load(prefixfile) or {}
+ self._PREFIXINFO = os.isfile(prefixfile) and io.load(prefixfile) or {}
end
- return self._PREFIXLIST
+ return self._PREFIXINFO
end
--- get the prefix list file
+-- get the prefix info file
function _instance:prefixfile()
- return path.join(self:prefixdir(".list"), self:name() .. "-" .. (self:version_str() or "") .. ".txt")
+ return path.join(self:prefixdir(".info"), self:name() .. "-" .. (self:version_str() or "") .. ".txt")
+end
+
+-- get environment variables
+function _instance:getenv(name, ...)
+ return self:prefixinfo().envars and self:prefixinfo().envars[name] or nil
+end
+
+-- set environment variables
+function _instance:setenv(name, ...)
+ self:prefixinfo().envars = self:prefixinfo().envars or {}
+ self:prefixinfo().envars[name] = {...}
+end
+
+-- add values to environment variable
+function _instance:addenv(name, ...)
+ self:prefixinfo().envars = self:prefixinfo().envars or {}
+ self:prefixinfo().envars[name] = table.join(self:prefixinfo().envars[name] or {}, ...)
+end
+
+-- register package info in the root prefix info
+function _instance:register()
+
+ -- register the environment variables
+ for name, values in pairs(table.wrap(self:prefixinfo().envars)) do
+ package.addenv(self:from("global"), self:debug(), self:plat(), self:arch(), name, values)
+ end
+end
+
+-- unregister package info from the root prefix info
+function _instance:unregister()
+
+ -- unregister the environment variables
+ for name, values in pairs(table.wrap(self:prefixinfo().envars)) do
+ package.delenv(self:from("global"), self:debug(), self:plat(), self:arch(), name, values)
+ end
end
-- get the downloaded original file
@@ -555,8 +600,67 @@ function package.cachedir()
end
-- get the prefix directory
-function package.prefixdir(is_global)
- return path.join(is_global and global.directory() or config.directory(), "prefix")
+function package.prefixdir(is_global, is_debug, plat, arch)
+ return path.join(is_global and global.directory() or config.directory(), "prefix", is_debug and "debug" or "release", plat or os.host(), arch or os.arch())
+end
+
+-- get the prefix info
+function package.prefixinfo(is_global, is_debug, plat, arch)
+ local prefixfile = package.prefixfile(is_global, is_debug, plat, arch)
+ return os.isfile(prefixfile) and io.load(prefixfile) or {}
+end
+
+-- get the prefix info file
+function package.prefixfile(is_global, is_debug, plat, arch)
+ return path.join(package.prefixdir(is_global, is_global, plat, arch), "info.txt")
+end
+
+-- get environment variables
+function package.getenv(is_global, is_debug, plat, arch, name)
+ local prefixinfo = package.prefixinfo(is_global, is_debug, plat, arch)
+ return prefixinfo.envars and prefixinfo.envars[name] or nil
+end
+
+-- add values to environment variable
+function package.addenv(is_global, is_debug, plat, arch, name, values)
+
+ -- add to the root prefix info
+ local prefixinfo = package.prefixinfo(is_global, is_debug, plat, arch)
+ prefixinfo.envars = prefixinfo.envars or {}
+ prefixinfo.envars[name] = table.join(prefixinfo.envars[name] or {}, values)
+ io.save(package.prefixfile(is_global, is_debug, plat, arch), prefixinfo)
+
+ -- add to the current environment
+ if values then
+ -- PATH? add the prefix root directory
+ if name:lower() == "path" then
+ local prefixdir = package.prefixdir(is_global, is_debug, plat, arch)
+ for _, value in ipairs(values) do
+ os.addenv(name, path.join(prefixdir, value))
+ end
+ else
+ os.addenv(name, unpack(values))
+ end
+ end
+end
+
+-- remove values to environment variable
+function package.delenv(is_global, is_debug, plat, arch, name, values)
+ local prefixinfo = package.prefixinfo(is_global, is_debug, plat, arch)
+ local prefixvalues = prefixinfo.envars and prefixinfo.envars[name] or nil
+ if prefixvalues then
+ local exists = {}
+ for _, value in ipairs(values) do
+ exists[value:trim()] = true
+ end
+ for i = #prefixvalues, 1, -1 do
+ value = prefixvalues[i]:trim()
+ if exists[value] then
+ table.remove(prefixvalues, i)
+ end
+ end
+ io.save(package.prefixfile(is_global, is_debug, plat, arch), prefixinfo)
+ end
end
-- load the package from the system directories
diff --git a/xmake/core/platform/environment.lua b/xmake/core/platform/environment.lua
index 0a549df17..381181004 100644
--- a/xmake/core/platform/environment.lua
+++ b/xmake/core/platform/environment.lua
@@ -35,12 +35,21 @@ local package = require("package/package")
function environment._enter_toolchains()
-- save the toolchains environment
- environment._PATH = os.getenv("PATH")
- environment._LD_LIBRARY_PATH = os.getenv("LD_LIBRARY_PATH")
+ environment._PATH = os.getenv("PATH")
- -- add search binary pathes of packages
- os.addenv("PATH", path.join(package.prefixdir(true), "release", os.host(), os.arch(), "bin")) -- globaldir/release/../bin
- os.addenv("PATH", path.join(package.prefixdir(false), "release", os.host(), os.arch(), "bin")) -- localdir/release/../bin
+ -- add global search binary pathes
+ local globaldir = package.prefixdir(true, false, os.host(), os.arch())
+ for _, dir in ipairs(table.wrap(package.getenv(true, false, os.host(), os.arch(), "PATH"))) do
+ os.addenv("PATH", path.join(globaldir, dir))
+ end
+ os.addenv("PATH", path.join(globaldir, "bin"))
+
+ -- add local search binary pathes
+ local localdir = package.prefixdir(false, false, os.host(), os.arch())
+ for _, dir in ipairs(table.wrap(package.getenv(false, false, os.host(), os.arch(), "PATH"))) do
+ os.addenv("PATH", path.join(localdir, dir))
+ end
+ os.addenv("PATH", path.join(localdir, "bin"))
end
-- leave the toolchains environment
@@ -54,12 +63,23 @@ end
function environment._enter_run()
-- save the running environment
+ environment._PATH = os.getenv("PATH")
environment._LD_LIBRARY_PATH = os.getenv("LD_LIBRARY_PATH")
- -- add search library pathes of packages
- if os.host() ~= "windows" then
- os.addenv("LD_LIBRARY_PATH", path.join(package.prefixdir(true), "release", os.host(), os.arch(), "lib")) -- globaldir/release/../lib
- os.addenv("LD_LIBRARY_PATH", path.join(package.prefixdir(false), "release", os.host(), os.arch(), "lib")) -- localdir/release/../lib
+ -- add global search library pathes of pathes
+ local globaldir = package.prefixdir(true, false, os.host(), os.arch())
+ if os.host() == "windows" then
+ os.addenv("PATH", path.join(globaldir, "lib"))
+ else
+ os.addenv("LD_LIBRARY_PATH", path.join(globaldir, "lib"))
+ end
+
+ -- add local search library pathes of pathes
+ local localdir = package.prefixdir(false, false, os.host(), os.arch())
+ if os.host() == "windows" then
+ os.addenv("PATH", path.join(localdir, "lib"))
+ else
+ os.addenv("LD_LIBRARY_PATH", path.join(localdir, "lib"))
end
end
@@ -67,6 +87,7 @@ end
function environment._leave_run()
-- leave the running environment
+ os.setenv("PATH", environment._PATH)
os.setenv("LD_LIBRARY_PATH", environment._LD_LIBRARY_PATH)
end
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 b0e87e302..57db255b3 100644
--- a/xmake/core/sandbox/modules/import/lib/detect/find_package.lua
+++ b/xmake/core/sandbox/modules/import/lib/detect/find_package.lua
@@ -162,7 +162,7 @@ function sandbox_lib_detect_find_package._find_from_prefixdirs(name, opt)
-- get links and linkdirs
local links = {}
- local prefixlist = prefixfile and io.load(prefixfile) or nil
+ local prefixlist = (prefixfile and io.load(prefixfile) or {}).installed
if prefixlist then
local found = false
for _, line in ipairs(prefixlist) do
diff --git a/xmake/rules/wdk/load.lua b/xmake/rules/wdk/load.lua
index d4cfaab7e..80f59a10c 100644
--- a/xmake/rules/wdk/load.lua
+++ b/xmake/rules/wdk/load.lua
@@ -65,7 +65,7 @@ function driver_kmdf(target)
-- add links
local winver = target:values("wdk.env.winver") or config.get("wdk_winver")
- if os_winver.version(winver) >= os_winver.version("win8") then
+ if winver and os_winver.version(winver) >= os_winver.version("win8") then
target:add("links", "BufferOverflowFastFailK")
else
target:add("links", "BufferOverflowK")
@@ -105,7 +105,7 @@ function driver_wdm(target)
-- add links
local winver = target:values("wdk.env.winver") or config.get("wdk_winver")
- if os_winver.version(winver) >= os_winver.version("win8") then
+ if winver and os_winver.version(winver) >= os_winver.version("win8") then
target:add("links", "BufferOverflowFastFailK")
else
target:add("links", "BufferOverflowK")