diff options
| author | ruki <[email protected]> | 2026-08-09 17:57:20 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2026-08-09 18:28:59 +0800 |
| commit | 7caece0cb3ee8ab207130edc17421d604610e686 (patch) | |
| tree | 0c29dff7e79f0b455f707b9dec7de6ea1cabe2f8 /xmake/actions/create/template.lua | |
| parent | cf62f410738a7489fe052d183bcf616ecf5f7067 (diff) | |
improve addon to import includes, rules and toolchains
Diffstat (limited to 'xmake/actions/create/template.lua')
| -rw-r--r-- | xmake/actions/create/template.lua | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/xmake/actions/create/template.lua b/xmake/actions/create/template.lua index 778028ce8..ff8f9aefa 100644 --- a/xmake/actions/create/template.lua +++ b/xmake/actions/create/template.lua @@ -120,6 +120,26 @@ end function templatedir(lang, templateid) assert(lang) assert(templateid) + + -- the qualified template of an addon, e.g. @addon/basic-templates/verilator.console + -- + -- @note the template ids are not namespaced, we only need it to disambiguate the conflicts + -- + if templateid:startswith("@addon/") then + local templatesdir, id, _, errors = addon.resolve_reference(templateid, "/", "templates") + if not templatesdir then + os.raise(errors) + end + local subdir = _templateid_subdir(id) + if subdir then + local dir = path.join(templatesdir, lang, subdir) + if os.isfile(path.join(dir, "xmake.lua")) then + return dir + end + end + return + end + for _, rootdir in ipairs(rootdirs()) do local subdir = _templateid_subdir(templateid) if subdir then @@ -191,11 +211,21 @@ function languages() return results end +-- get the reference of the given template, e.g. @addon/basic-templates/verilator.console +function _template_reference(rootinfo, templateid) + if rootinfo.kind == "addon" then + return string.format("@addon/%s/%s", rootinfo.name, templateid) + end + return path.join(rootinfo.dir, templateid) +end + -- get all templates for the given language function templates(lang) assert(lang) local found = hashset.new() - for _, rootdir in ipairs(rootdirs()) do + local providers = {} + for _, rootinfo in ipairs(rootinfos()) do + local rootdir = rootinfo.dir local templateroot = path.join(rootdir, lang) local configfiles = os.files(path.join(templateroot, "**", "xmake.lua")) if configfiles then @@ -219,7 +249,17 @@ function templates(lang) end if ok then table.insert(accepted, item.dir) - found:insert((item.relpath:gsub("[/\\]", "."))) + local templateid = (item.relpath:gsub("[/\\]", ".")) + -- the templates are not namespaced, so we need to report the conflicts of the addons, + -- otherwise we do not know which template will be used + local provider = providers[templateid] + if provider and (provider.kind == "addon" or rootinfo.kind == "addon") then + utils.warning("template(%s/%s) conflicts, we will use the first one!\n -> %s\n -> %s\nplease use the qualified template id to disambiguate them.", + lang, templateid, _template_reference(provider, templateid), _template_reference(rootinfo, templateid)) + else + providers[templateid] = rootinfo + found:insert(templateid) + end end end end |
