Import is mainly used to import xmake's extension class library and some user-defined class library modules. It is generally used in user-defined script (on_build, on_run..), plug-in development, template development, platform extension, user-defined task tasks, and other places.
The import mechanism is as follows:
- Import from the current script directory first
- Then import from the extension class library
Imported syntax rules:
Class library path rules based on., for example:
Import core extension module
import("core.base.option") import("core.project") import("core.project.task") import("core") function main() --Get Parameter Options print(option.get("version")) --Running tasks and plug-ins task.run("hello") project.task.run("hello") core.project.task.run("hello") end
Import custom modules in the current directory:
Directory structure:
plugin - xmake.lua - main.lua - modules - hello1.lua - hello2.lua
Import modules in main.lua
import("modules.hello1") import("modules.hello2")
After importing, you can directly use all public interfaces. Private interfaces are marked with the _ prefix, indicating that they will not be exported or called externally..
In addition to the current directory, we can also import class libraries in other specified directories, such as:
import("hello3", {rootdir = "/home/xxx/modules"})
To prevent naming conflicts, you can also specify aliases after importing:
import("core.platform.platform", {alias = "p"}) function main() --In this way, we can use p to call the plats interface of the platform module and get the list of all the platforms supported by xmake table.dump(p.plats()) end
Import can not only import the class library, but also support importing as inheritance, so as to realize the inheritance relationship between modules
import("xxx.xxx", {inherit = true})
In this way, instead of importing the reference of the module, all the public interfaces of the imported module will be merged with the interfaces of the current module to achieve inheritance between modules