diff options
| author | ruki <[email protected]> | 2020-05-14 00:00:11 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-05-13 18:01:07 +0800 |
| commit | 8730d4e1aa726168336d4f0ed69a32c251b02a43 (patch) | |
| tree | a8bd034520b4f105a6e2e39f5ed4a6cc2ff2737f | |
| parent | 519e890061d919ae5ebd174a5a38f844e57cdab6 (diff) | |
improve project.version
| -rw-r--r-- | xmake/core/project/project.lua | 387 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/project/project.lua | 8 |
2 files changed, 199 insertions, 196 deletions
diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index bc73dc55b..56e001c55 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -153,197 +153,6 @@ function project._api_add_platformdirs(interp, ...) platform.add_directories(...) end --- get interpreter -function project.interpreter() - - -- the interpreter has been initialized? return it directly - if project._INTERPRETER then - return project._INTERPRETER - end - - -- init interpreter - local interp = interpreter.new() - assert(interp) - - -- set root directory - interp:rootdir_set(project.directory()) - - -- set root scope - interp:rootscope_set("target") - - -- define apis for rule - interp:api_define(rule.apis()) - - -- define apis for task - interp:api_define(task.apis()) - - -- define apis for target - interp:api_define(target.apis()) - - -- define apis for option - interp:api_define(option.apis()) - - -- define apis for package - interp:api_define(package.apis()) - - -- define apis for language - interp:api_define(language.apis()) - - -- define apis for project - interp:api_define - { - values = - { - -- set_xxx - "set_project" - , "set_modes" -- TODO deprecated - , "set_description" - -- add_xxx - , "add_requires" - , "add_repositories" - } - , pathes = - { - -- add_xxx - "add_packagedirs" - } - , keyvalues = - { - "set_config" - } - , custom = - { - -- is_xxx - {"is_os", project._api_is_os } - , {"is_kind", project._api_is_kind } - , {"is_arch", project._api_is_arch } - , {"is_host", project._api_is_host } - , {"is_subhost", project._api_is_subhost } - , {"is_mode", project._api_is_mode } - , {"is_plat", project._api_is_plat } - , {"is_config", project._api_is_config } - -- get_xxx - , {"get_config", project._api_get_config } - -- has_xxx - , {"has_config", project._api_has_config } - , {"has_package", project._api_has_package } - -- add_xxx - , {"add_moduledirs", project._api_add_moduledirs } - , {"add_plugindirs", project._api_add_plugindirs } - , {"add_platformdirs", project._api_add_platformdirs } - } - } - - -- register api: deprecated - deprecated_project.api_register(interp) - - -- set filter - interp:filter():register("project", function (variable) - - -- check - assert(variable) - - -- hack buildir first - if variable == "buildir" then - return config.buildir() - end - - -- attempt to get it directly from the configure - local result = config.get(variable) - if not result or type(result) ~= "string" then - - -- init maps - local maps = - { - os = platform.os() - , host = os.host() - , subhost = os.subhost() - , prefix = "$(prefix)" - , tmpdir = function () return os.tmpdir() end - , curdir = function () return os.curdir() end - , scriptdir = function () return sandbox_os.scriptdir() end - , globaldir = global.directory() - , configdir = config.directory() - , projectdir = project.directory() - , programdir = os.programdir() - } - - -- map it - result = maps[variable] - if type(result) == "function" then - result = result() - end - - -- attempt to get it from the platform tools, e.g. cc, cxx, ld .. - -- because these values may not exist in config cache when call `config.get()`, we need check and get it. - -- - if not result then - result = platform.tool(variable) - end - end - - -- ok? - return result - end) - - -- save interpreter - project._INTERPRETER = interp - - -- ok? - return interp -end - --- get the root project file -function project.rootfile() - return os.projectfile() -end - --- get all loaded project files with subfiles (xmake.lua) -function project.allfiles() - local rcfile = project.rcfile() - if rcfile and os.isfile(rcfile) then - return table.join(project.interpreter():scriptfiles(), rcfile) - else - return project.interpreter():scriptfiles() - end -end - --- get the global rcfile: ~/.xmakerc.lua -function project.rcfile() - local xmakerc = project._XMAKE_RCFILE - if xmakerc == nil then - xmakerc = "/etc/xmakerc.lua" - if not os.isfile(xmakerc) then - xmakerc = "~/.xmakerc.lua" - if not os.isfile(xmakerc) then - xmakerc = path.join(global.directory(), "xmakerc.lua") - end - end - project._XMAKE_RCFILE = xmakerc - end - return xmakerc -end - --- get the project directory -function project.directory() - return os.projectdir() -end - --- get the filelock of the whole project directory -function project.filelock() - local filelock = project._FILELOCK - if filelock == nil then - filelock = io.openlock(path.join(config.directory(), "project.lock")) - project._FILELOCK = filelock - end - return filelock -end - --- get the project info from the given name -function project.get(name) - return project._INFO and project._INFO:get(name) or nil -end - -- load the project file function project._load(force, disable_filter) @@ -786,6 +595,202 @@ function project._load_packages() return project._load_scope("package", true, false) end +-- get interpreter +function project.interpreter() + + -- the interpreter has been initialized? return it directly + if project._INTERPRETER then + return project._INTERPRETER + end + + -- init interpreter + local interp = interpreter.new() + assert(interp) + + -- set root directory + interp:rootdir_set(project.directory()) + + -- set root scope + interp:rootscope_set("target") + + -- define apis for rule + interp:api_define(rule.apis()) + + -- define apis for task + interp:api_define(task.apis()) + + -- define apis for target + interp:api_define(target.apis()) + + -- define apis for option + interp:api_define(option.apis()) + + -- define apis for package + interp:api_define(package.apis()) + + -- define apis for language + interp:api_define(language.apis()) + + -- define apis for project + interp:api_define + { + values = + { + -- set_xxx + "set_project" + , "set_modes" -- TODO deprecated + , "set_description" + -- add_xxx + , "add_requires" + , "add_repositories" + } + , pathes = + { + -- add_xxx + "add_packagedirs" + } + , keyvalues = + { + "set_config" + } + , custom = + { + -- is_xxx + {"is_os", project._api_is_os } + , {"is_kind", project._api_is_kind } + , {"is_arch", project._api_is_arch } + , {"is_host", project._api_is_host } + , {"is_subhost", project._api_is_subhost } + , {"is_mode", project._api_is_mode } + , {"is_plat", project._api_is_plat } + , {"is_config", project._api_is_config } + -- get_xxx + , {"get_config", project._api_get_config } + -- has_xxx + , {"has_config", project._api_has_config } + , {"has_package", project._api_has_package } + -- add_xxx + , {"add_moduledirs", project._api_add_moduledirs } + , {"add_plugindirs", project._api_add_plugindirs } + , {"add_platformdirs", project._api_add_platformdirs } + } + } + + -- register api: deprecated + deprecated_project.api_register(interp) + + -- set filter + interp:filter():register("project", function (variable) + + -- check + assert(variable) + + -- hack buildir first + if variable == "buildir" then + return config.buildir() + end + + -- attempt to get it directly from the configure + local result = config.get(variable) + if not result or type(result) ~= "string" then + + -- init maps + local maps = + { + os = platform.os() + , host = os.host() + , subhost = os.subhost() + , prefix = "$(prefix)" + , tmpdir = function () return os.tmpdir() end + , curdir = function () return os.curdir() end + , scriptdir = function () return sandbox_os.scriptdir() end + , globaldir = global.directory() + , configdir = config.directory() + , projectdir = project.directory() + , programdir = os.programdir() + } + + -- map it + result = maps[variable] + if type(result) == "function" then + result = result() + end + + -- attempt to get it from the platform tools, e.g. cc, cxx, ld .. + -- because these values may not exist in config cache when call `config.get()`, we need check and get it. + -- + if not result then + result = platform.tool(variable) + end + end + + -- ok? + return result + end) + + -- save interpreter + project._INTERPRETER = interp + + -- ok? + return interp +end + +-- get the root project file +function project.rootfile() + return os.projectfile() +end + +-- get all loaded project files with subfiles (xmake.lua) +function project.allfiles() + local rcfile = project.rcfile() + if rcfile and os.isfile(rcfile) then + return table.join(project.interpreter():scriptfiles(), rcfile) + else + return project.interpreter():scriptfiles() + end +end + +-- get the global rcfile: ~/.xmakerc.lua +function project.rcfile() + local xmakerc = project._XMAKE_RCFILE + if xmakerc == nil then + xmakerc = "/etc/xmakerc.lua" + if not os.isfile(xmakerc) then + xmakerc = "~/.xmakerc.lua" + if not os.isfile(xmakerc) then + xmakerc = path.join(global.directory(), "xmakerc.lua") + end + end + project._XMAKE_RCFILE = xmakerc + end + return xmakerc +end + +-- get the project directory +function project.directory() + return os.projectdir() +end + +-- get the filelock of the whole project directory +function project.filelock() + local filelock = project._FILELOCK + if filelock == nil then + filelock = io.openlock(path.join(config.directory(), "project.lock")) + project._FILELOCK = filelock + end + return filelock +end + +-- get the project info from the given name +function project.get(name) + return project._INFO and project._INFO:get(name) or nil +end + +-- get the project version, the root version of the target scope +function project.version() + return project.get("target.version") +end + -- clear project cache to reload targets and options function project.clear() diff --git a/xmake/core/sandbox/modules/import/core/project/project.lua b/xmake/core/sandbox/modules/import/core/project/project.lua index 8f36af51e..9da23006c 100644 --- a/xmake/core/sandbox/modules/import/core/project/project.lua +++ b/xmake/core/sandbox/modules/import/core/project/project.lua @@ -35,6 +35,9 @@ local environment = require("platform/environment") local package = require("package/package") local import = require("sandbox/modules/import") +-- export some readonly interfaces +sandbox_core_project.version = project.version + -- load project function sandbox_core_project.load() -- deprecated @@ -215,11 +218,6 @@ function sandbox_core_project.name() return name end --- get the project version, the root version of the target scope -function sandbox_core_project.version() - return project.get("target.version") -end - -- get the project modes function sandbox_core_project.modes() local modes = project.get("modes") or {} |
