summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-07-17 23:40:30 +0800
committerruki <[email protected]>2017-07-17 23:40:30 +0800
commit2a8458eb016b682f815a26b90ab65abd9e67b37a (patch)
tree7aabde3aadf09029138566027b1b0a0c1b4fb4e7
parent7cac00bb9cace801c6db9758d2a01d2711738926 (diff)
add includes api instead of add_subdirs and add_subfiles
-rw-r--r--CHANGELOG.md2
-rw-r--r--xmake/core/base/interpreter.lua142
-rw-r--r--xmake/core/project/deprecated/project.lua4
-rw-r--r--xmake/core/project/project.lua4
4 files changed, 122 insertions, 30 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9c1011af7..26fdc1bdd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,7 @@
* [#132](https://github.com/tboox/xmake/issues/132): Add `add_frameworkdirs` api
* Add `lib.detect.has_xxx` and `lib.detect.find_xxx` apis.
* Add `add_moduledirs` api
+* Add `includes` api instead of `add_subdirs` and `add_subfiles`
### Changes
@@ -325,6 +326,7 @@
* [#132](https://github.com/tboox/xmake/issues/132): 添加`add_frameworkdirs`接口
* 添加`lib.detect.has_xxx`和`lib.detect.find_xxx`接口
* 添加`add_moduledirs`接口在工程中定义和加载扩展模块
+* 添加`includes`接口替换`add_subdirs`和`add_subfiles`
### 改进
diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua
index c23fa8457..e6d3e683a 100644
--- a/xmake/core/base/interpreter.lua
+++ b/xmake/core/base/interpreter.lua
@@ -351,7 +351,7 @@ function interpreter:_api_translate_pathes(...)
end
-- the builtin api: add_subdirs() or add_subfiles()
-function interpreter:_api_builtin_add_subdirfiles(isdirs, ...)
+function interpreter:_api_builtin_add_subdirfiles(...)
-- check
assert(self and self._PRIVATE and self._PRIVATE._ROOTDIR and self._PRIVATE._MTIMES)
@@ -370,8 +370,10 @@ function interpreter:_api_builtin_add_subdirfiles(isdirs, ...)
-- match all subpathes
local subpathes_matched = {}
for _, subpath in ipairs(subpathes) do
- local files = os.match(subpath, isdirs)
- if files then table.join2(subpathes_matched, files) end
+ local files = os.match(subpath, not subpath:endswith(".lua"))
+ if files then
+ table.join2(subpathes_matched, files)
+ end
end
-- done
@@ -380,7 +382,7 @@ function interpreter:_api_builtin_add_subdirfiles(isdirs, ...)
-- the file path
local file = subpath
- if isdirs then
+ if not subpath:endswith(".lua") then
file = path.join(subpath, path.filename(curfile))
end
@@ -695,8 +697,9 @@ function interpreter.new()
end})
-- register the builtin interfaces
- instance:api_register(nil, "add_subdirs", interpreter.api_builtin_add_subdirs)
- instance:api_register(nil, "add_subfiles", interpreter.api_builtin_add_subfiles)
+ instance:api_register(nil, "includes", interpreter.api_builtin_includes)
+ instance:api_register(nil, "add_subdirs", interpreter.api_builtin_includes)
+ instance:api_register(nil, "add_subfiles", interpreter.api_builtin_includes)
instance:api_register(nil, "set_xmakever", interpreter.api_builtin_set_xmakever)
-- load builtin module files
@@ -1389,26 +1392,6 @@ function interpreter:api_define(apis)
end
end
--- the builtin api: add_subdirs()
-function interpreter:api_builtin_add_subdirs(...)
-
- -- check
- assert(self)
-
- -- done
- return self:_api_builtin_add_subdirfiles(true, ...)
-end
-
--- the builtin api: add_subfiles()
-function interpreter:api_builtin_add_subfiles(...)
-
- -- check
- assert(self)
-
- -- done
- return self:_api_builtin_add_subdirfiles(false, ...)
-end
-
-- the builtin api: set_xmakever()
function interpreter:api_builtin_set_xmakever(minver)
@@ -1438,6 +1421,113 @@ function interpreter:api_builtin_set_xmakever(minver)
end
end
+-- the builtin api: includes()
+function interpreter:api_builtin_includes(...)
+
+ -- check
+ assert(self and self._PRIVATE and self._PRIVATE._ROOTDIR and self._PRIVATE._MTIMES)
+
+ -- the current file
+ local curfile = self._PRIVATE._CURFILE
+ assert(curfile)
+
+ -- the scopes
+ local scopes = self._PRIVATE._SCOPES
+ assert(scopes)
+
+ -- get all subpathes
+ local subpathes = table.join(...)
+
+ -- match all subpathes
+ local subpathes_matched = {}
+ for _, subpath in ipairs(subpathes) do
+ local files = os.match(subpath, not subpath:endswith(".lua"))
+ if files then
+ table.join2(subpathes_matched, files)
+ end
+ end
+
+ -- done
+ for _, subpath in ipairs(subpathes_matched) do
+ if subpath and type(subpath) == "string" then
+
+ -- the file path
+ local file = subpath
+ if not subpath:endswith(".lua") then
+ file = path.join(subpath, path.filename(curfile))
+ end
+
+ -- get the absolute file path
+ if not path.is_absolute(file) then
+ file = path.absolute(file)
+ end
+
+ -- update the current file
+ self._PRIVATE._CURFILE = file
+
+ -- load the file script
+ local script, errors = loadfile(file)
+ if script then
+
+ -- bind public scope
+ setfenv(script, self._PUBLIC)
+
+ -- save the previous root scope
+ local root_prev = scopes._ROOT
+
+ -- save the previous scope
+ local scope_prev = scopes._CURRENT
+
+ -- save the previous scope kind
+ local scope_kind_prev = scopes._CURRENT_KIND
+
+ -- clear the current root scope
+ scopes._ROOT = nil
+
+ -- clear the current scope, force to enter root scope
+ scopes._CURRENT = nil
+
+ -- save the current directory
+ local olddir = os.curdir()
+
+ -- enter the script directory
+ os.cd(path.directory(file))
+
+ -- done interpreter
+ local ok, errors = xpcall(script, interpreter._traceback)
+ if not ok then
+ os.raise(errors)
+ end
+
+ -- leave the script directory
+ os.cd(olddir)
+
+ -- restore the previous scope kind
+ scopes._CURRENT_KIND = scope_kind_prev
+
+ -- restore the previous scope
+ scopes._CURRENT = scope_prev
+
+ -- fetch the root values in root scopes first
+ interpreter._fetch_root_scope(scopes._ROOT)
+
+ -- restore the previous root scope and merge current root scope
+ -- it will override the previous values if the current values are override mode
+ -- so we priority use the values in subdirs scope
+ scopes._ROOT = interpreter._merge_root_scope(scopes._ROOT, root_prev, true)
+
+ -- get mtime of the file
+ self._PRIVATE._MTIMES[path.relative(file, self._PRIVATE._ROOTDIR)] = os.mtime(file)
+ else
+ os.raise(errors)
+ end
+ end
+ end
+
+ -- restore the current file
+ self._PRIVATE._CURFILE = curfile
+end
+
-- get api function
function interpreter:api_func(apiname)
diff --git a/xmake/core/project/deprecated/project.lua b/xmake/core/project/deprecated/project.lua
index 4636a35e9..1b86b45e3 100644
--- a/xmake/core/project/deprecated/project.lua
+++ b/xmake/core/project/deprecated/project.lua
@@ -47,14 +47,14 @@ function deprecated_project._api_add_pkgdirs(interp, ...)
end
-- add all packages
- interp:api_builtin_add_subdirs(pkgdirs)
+ interp:api_builtin_includes(pkgdirs)
end
-- load the given packages
function deprecated_project._api_add_pkgs(interp, ...)
-- add all packages
- interp:api_builtin_add_subdirs(...)
+ interp:api_builtin_includes(...)
end
-- load all packages from the given directories
diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua
index d31322345..bc5e7ab25 100644
--- a/xmake/core/project/project.lua
+++ b/xmake/core/project/project.lua
@@ -163,7 +163,7 @@ function project._api_add_plugindirs(interp, ...)
end
-- add all plugins
- interp:api_builtin_add_subdirs(plugindirs)
+ interp:api_builtin_includes(plugindirs)
end
-- add package directories and load all packages from the given directories
@@ -177,7 +177,7 @@ function project._api_add_packagedirs(interp, ...)
end
-- add all packages
- interp:api_builtin_add_subdirs(pkgdirs)
+ interp:api_builtin_includes(pkgdirs)
end
-- get interpreter