diff options
| author | ruki <[email protected]> | 2019-02-18 22:34:04 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-02-18 10:09:01 +0800 |
| commit | eb0674bb59e5d0b682bec1ade12ee976bcf658e5 (patch) | |
| tree | 707e0c6ab4c487790e7efd6e92bb71ad8f13649d | |
| parent | 061366a4d5fdff013903ac2b6ddede59e69fdd1a (diff) | |
improve target
| -rw-r--r-- | xmake/core/project/target.lua | 387 |
1 files changed, 193 insertions, 194 deletions
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 979befbb7..951287848 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -23,7 +23,8 @@ -- -- define module -local target = target or {} +local target = target or {} +local _instance = _instance or {} -- load modules local os = require("base/os") @@ -44,137 +45,17 @@ local language = require("language/language") local sandbox = require("sandbox/sandbox") local sandbox_module = require("sandbox/modules/import/core/sandbox/module") --- get target apis -function target.apis() - - return - { - values = - { - -- target.set_xxx - "target.set_kind" - , "target.set_strip" - , "target.set_rules" - , "target.set_version" - , "target.set_enabled" - , "target.set_default" - , "target.set_options" - , "target.set_symbols" - , "target.set_filename" - , "target.set_basename" - , "target.set_warnings" - , "target.set_optimize" - , "target.set_languages" - -- target.add_xxx - , "target.add_deps" - , "target.add_rules" - , "target.add_options" - , "target.add_packages" - , "target.add_imports" - , "target.add_languages" - , "target.add_vectorexts" - } - , keyvalues = - { - -- target.set_xxx - "target.set_values" - , "target.set_configvar" - -- target.add_xxx - , "target.add_values" - } - , pathes = - { - -- target.set_xxx - "target.set_targetdir" - , "target.set_objectdir" - , "target.set_dependir" - , "target.set_configdir" - , "target.set_installdir" - -- target.add_xxx - , "target.add_files" - , "target.add_configfiles" - , "target.add_installfiles" - -- target.del_xxx - , "target.del_files" - } - , dictionary = - { - -- target.set_xxx - "target.set_tools" - , "target.add_tools" - } - , script = - { - -- target.on_xxx - "target.on_run" - , "target.on_load" - , "target.on_build" - , "target.on_build_file" - , "target.on_build_files" - , "target.on_clean" - , "target.on_package" - , "target.on_install" - , "target.on_uninstall" - -- target.before_xxx - , "target.before_run" - , "target.before_build" - , "target.before_build_file" - , "target.before_build_files" - , "target.before_clean" - , "target.before_package" - , "target.before_install" - , "target.before_uninstall" - -- target.after_xxx - , "target.after_run" - , "target.after_build" - , "target.after_build_file" - , "target.after_build_files" - , "target.after_clean" - , "target.after_package" - , "target.after_install" - , "target.after_uninstall" - } - } -end - --- get the filename from the given target name and kind -function target.filename(targetname, targetkind, targetformat) - - -- check - assert(targetname and targetkind) - - -- make filename by format - local format = targetformat or platform.format(targetkind) - return format and (format:gsub("%$%(name%)", targetname)) or targetname -end - --- get the link name of the target file -function target.linkname(filename) - local linkname, count = filename:gsub(target.filename("__pattern__", "static"):gsub("%.", "%%."):gsub("__pattern__", "(.+)") .. "$", "%1") - if count == 0 then - linkname, count = filename:gsub(target.filename("__pattern__", "shared"):gsub("%.", "%%."):gsub("__pattern__", "(.+)") .. "$", "%1") - end - return count > 0 and linkname or nil -end - -- new a target instance -function target.new(name, info, project) - - -- init a target instance - local instance = table.inherit(target) - assert(instance) - - -- save name and info - instance._NAME = name - instance._INFO = info +function _instance.new(name, info, project) + local instance = table.inherit(_instance) + instance._NAME = name + instance._INFO = info instance._PROJECT = project - - -- ok? return instance end -- load rule, move cache to target -function target:_load_rule(ruleinst, suffix) +function _instance:_load_rule(ruleinst, suffix) -- init cache local key = ruleinst:name() .. (suffix and ("_" .. suffix) or "") @@ -202,7 +83,7 @@ function target:_load_rule(ruleinst, suffix) end -- load rules -function target:_load_rules(suffix) +function _instance:_load_rules(suffix) for _, r in pairs(self:orderules()) do local ok, errors = self:_load_rule(r, suffix) if not ok then @@ -213,7 +94,7 @@ function target:_load_rules(suffix) end -- do load target and rules -function target:_load() +function _instance:_load() -- do before_load with target rules local ok, errors = self:_load_rules("before") @@ -247,7 +128,7 @@ function target:_load() end -- get the copied files -function target:_copiedfiles(filetype, outputdir, pathfilter) +function _instance:_copiedfiles(filetype, outputdir, pathfilter) -- no copied files? local copiedfiles = self:get(filetype) @@ -328,44 +209,44 @@ function target:_copiedfiles(filetype, outputdir, pathfilter) end -- get the target info -function target:get(name) +function _instance:get(name) return self._INFO:get(name) end -- set the value to the target info -function target:set(name, ...) +function _instance:set(name, ...) self._INFO:apival_set(name, ...) end -- add the value to the target info -function target:add(name, ...) +function _instance:add(name, ...) self._INFO:apival_add(name, ...) end -- remove the value to the target info -function target:del(name, ...) +function _instance:del(name, ...) self._INFO:apival_del(name, ...) end -- get user private data -function target:data(name) +function _instance:data(name) return self._DATA and self._DATA[name] or nil end -- set user private data -function target:data_set(name, data) +function _instance:data_set(name, data) self._DATA = self._DATA or {} self._DATA[name] = data end -- add user private data -function target:data_add(name, data) +function _instance:data_add(name, data) self._DATA = self._DATA or {} self._DATA[name] = table.unwrap(table.join(self._DATA[name] or {}, data)) end -- get values -function target:values(name, sourcefile) +function _instance:values(name, sourcefile) -- get values from the source file first local values = {} @@ -390,32 +271,32 @@ function target:values(name, sourcefile) end -- set values -function target:values_set(name, ...) +function _instance:values_set(name, ...) self:set("values." .. name, ...) end -- add values -function target:values_add(name, ...) +function _instance:values_add(name, ...) self:add("values." .. name, ...) end -- get the target info -function target:info() +function _instance:info() return self._INFO:info() end -- get the type: option -function target:type() +function _instance:type() return "target" end -- get the target name -function target:name() +function _instance:name() return self._NAME end -- get the target version -function target:version() +function _instance:version() -- get version and build version local version = self:get("version") @@ -439,7 +320,7 @@ function target:version() end -- get the base name of target file -function target:basename() +function _instance:basename() local filename = self:get("filename") if filename then return path.basename(filename) @@ -448,7 +329,7 @@ function target:basename() end -- get the target linker -function target:linker() +function _instance:linker() -- get it from cache first if self._LINKER then @@ -469,22 +350,22 @@ function target:linker() end -- make linking command for this target -function target:linkcmd(objectfiles) +function _instance:linkcmd(objectfiles) return self:linker():linkcmd(objectfiles or self:objectfiles(), self:targetfile(), {target = self}) end -- make linking arguments for this target -function target:linkargv(objectfiles) +function _instance:linkargv(objectfiles) return self:linker():linkargv(objectfiles or self:objectfiles(), self:targetfile(), {target = self}) end -- make link flags for the given target -function target:linkflags() +function _instance:linkflags() return self:linker():linkflags({target = self}) end -- get the given dependent target -function target:dep(name) +function _instance:dep(name) local deps = self:deps() if deps then return deps[name] @@ -492,17 +373,17 @@ function target:dep(name) end -- get target deps -function target:deps() +function _instance:deps() return self._DEPS end -- get target ordered deps -function target:orderdeps() +function _instance:orderdeps() return self._ORDERDEPS end -- get the given dependent config -function target:depconfig(name) +function _instance:depconfig(name) -- get deps config -- @@ -520,24 +401,24 @@ function target:depconfig(name) end -- get target rules -function target:rules() +function _instance:rules() return self._RULES end -- get target ordered rules -function target:orderules() +function _instance:orderules() return self._ORDERULES end -- get target rule from the given rule name -function target:rule(name) +function _instance:rule(name) if self._RULES then return self._RULES[name] end end -- is phony target? -function target:isphony() +function _instance:isphony() -- get target kind local targetkind = self:targetkind() @@ -547,12 +428,12 @@ function target:isphony() end -- get the enabled option -function target:opt(name) +function _instance:opt(name) return self:opts()[name] end -- get the enabled options -function target:opts() +function _instance:opts() -- attempt to get it from cache first if self._OPTS_ENABLED then @@ -570,7 +451,7 @@ function target:opts() end -- get the enabled ordered options -function target:orderopts() +function _instance:orderopts() -- attempt to get it from cache first if self._ORDEROPTS_ENABLED then @@ -603,12 +484,12 @@ function target:orderopts() end -- get the enabled package -function target:pkg(name) +function _instance:pkg(name) return self:pkgs()[name] end -- get the enabled packages -function target:pkgs() +function _instance:pkgs() -- attempt to get it from cache first if self._PKGS_ENABLED then @@ -626,7 +507,7 @@ function target:pkgs() end -- get the required packages -function target:orderpkgs() +function _instance:orderpkgs() if not self._ORDERPKGS_ENABLED then local packages = {} for _, pkg in ipairs(self._PACKAGES) do @@ -640,7 +521,7 @@ function target:orderpkgs() end -- get the config info of the given package -function target:pkgconfig(pkgname) +function _instance:pkgconfig(pkgname) local extra_packages = self:get("__extra_packages") if extra_packages then return extra_packages[pkgname] @@ -648,7 +529,7 @@ function target:pkgconfig(pkgname) end -- get the object files directory -function target:objectdir(onlyroot) +function _instance:objectdir(onlyroot) -- the object directory local objectdir = self:get("objectdir") @@ -683,7 +564,7 @@ function target:objectdir(onlyroot) end -- get the dependent files directory -function target:dependir() +function _instance:dependir() -- init the dependent directory local dependir = self:get("dependir") @@ -694,12 +575,12 @@ function target:dependir() end -- get the target kind -function target:targetkind() +function _instance:targetkind() return self:get("kind") or "phony" end -- get the target directory -function target:targetdir(onlyroot) +function _instance:targetdir(onlyroot) -- the target directory local targetdir = self:get("targetdir") @@ -734,7 +615,7 @@ function target:targetdir(onlyroot) end -- get the target file -function target:targetfile() +function _instance:targetfile() -- the target directory local targetdir = self:targetdir() @@ -756,7 +637,7 @@ function target:targetfile() end -- get the symbol file -function target:symbolfile() +function _instance:symbolfile() -- the target directory local targetdir = self:targetdir() or config.buildir() @@ -771,22 +652,22 @@ function target:symbolfile() end -- get the script directory of xmake.lua -function target:scriptdir() +function _instance:scriptdir() return self:get("__scriptdir") end -- TODO get header directory (deprecated) -function target:headerdir() +function _instance:headerdir() return self:get("headerdir") or config.buildir() end -- get configuration output directory -function target:configdir() +function _instance:configdir() return self:get("configdir") or config.buildir() end -- get install directory -function target:installdir() +function _instance:installdir() -- get it from the cache local installdir = self._INSTALLDIR @@ -814,7 +695,7 @@ function target:installdir() end -- get rules of the source file -function target:filerules(sourcefile) +function _instance:filerules(sourcefile) -- add rules from file config local rules = {} @@ -852,7 +733,7 @@ function target:filerules(sourcefile) end -- get the config info of the given source file -function target:fileconfig(sourcefile) +function _instance:fileconfig(sourcefile) -- get files config local filesconfig = self._FILESCONFIG @@ -887,7 +768,7 @@ function target:fileconfig(sourcefile) end -- set the config info to the given source file -function target:fileconfig_set(sourcefile, info) +function _instance:fileconfig_set(sourcefile, info) -- get files config local filesconfig = self._FILESCONFIG or {} @@ -900,7 +781,7 @@ function target:fileconfig_set(sourcefile, info) end -- get the source files -function target:sourcefiles() +function _instance:sourcefiles() -- cached? return it directly if self._SOURCEFILES then @@ -991,7 +872,7 @@ function target:sourcefiles() end -- get object file from source file -function target:objectfile(sourcefile) +function _instance:objectfile(sourcefile) -- translate: [lib]xxx*.[a|lib] => xxx/*.[o|obj] object file sourcefile = sourcefile:gsub(target.filename("([%%w%%-_]+)", "static"):gsub("%.", "%%.") .. "$", "%1/*") @@ -1020,7 +901,7 @@ function target:objectfile(sourcefile) end -- get the object files -function target:objectfiles() +function _instance:objectfiles() -- get source batches local sourcebatches, modified = self:sourcebatches() @@ -1060,7 +941,7 @@ function target:objectfiles() end -- TODO get the header files, get("headers") (deprecated) -function target:headers(outputdir) +function _instance:headers(outputdir) return self:headerfiles(outputdir, true) end @@ -1069,7 +950,7 @@ end -- default: get("headers") + get("headerfiles") -- only_deprecated: get("headers") -- -function target:headerfiles(outputdir, only_deprecated) +function _instance:headerfiles(outputdir, only_deprecated) -- get header files? local headers = self:get("headers") -- TODO deprecated @@ -1150,7 +1031,7 @@ function target:headerfiles(outputdir, only_deprecated) end -- get the configuration files -function target:configfiles(outputdir) +function _instance:configfiles(outputdir) return self:_copiedfiles("configfiles", outputdir or self:configdir(), function (dstpath, fileinfo) if dstpath:endswith(".in") then dstpath = dstpath:sub(1, -4) @@ -1160,12 +1041,12 @@ function target:configfiles(outputdir) end -- get the install files -function target:installfiles(outputdir) +function _instance:installfiles(outputdir) return self:_copiedfiles("installfiles", outputdir or self:installdir()) end -- get depend file from object file -function target:dependfile(objectfile) +function _instance:dependfile(objectfile) -- get the dependent original file and directory, @note relative to the root directory local originfile = objectfile and objectfile or self:targetfile(true) @@ -1189,7 +1070,7 @@ function target:dependfile(objectfile) end -- get the dependent include files -function target:dependfiles() +function _instance:dependfiles() -- get source batches local sourcebatches, modified = self:sourcebatches() @@ -1218,7 +1099,7 @@ end -- -- .e.g cc cxx mm mxx as ... -- -function target:sourcekinds() +function _instance:sourcekinds() -- cached? return it directly if self._SOURCEKINDS then @@ -1247,12 +1128,12 @@ function target:sourcekinds() end -- get source count -function target:sourcecount() +function _instance:sourcecount() return #self:sourcefiles() end -- get source batches -function target:sourcebatches() +function _instance:sourcebatches() -- get source files local sourcefiles, modified = self:sourcefiles() @@ -1372,7 +1253,7 @@ function target:sourcebatches() end -- get xxx_script -function target:script(name, generic) +function _instance:script(name, generic) -- get script local script = self:get(name) @@ -1423,7 +1304,7 @@ function target:script(name, generic) end -- TODO get the config header version (deprecated) -function target:configversion() +function _instance:configversion() -- get the config version and build version local version = nil @@ -1447,7 +1328,7 @@ function target:configversion() end -- get the config header prefix -function target:configprefix() +function _instance:configprefix() -- get the config prefix local configprefix = nil @@ -1465,7 +1346,7 @@ function target:configprefix() end -- get the config header files (deprecated) -function target:configheader(outputdir) +function _instance:configheader(outputdir) -- get config header local configheader = self:get("config_header") or self:get("config_h") @@ -1505,7 +1386,7 @@ end -- -- @param langkind c/cxx -- -function target:pcheaderfile(langkind) +function _instance:pcheaderfile(langkind) return self:get("p" .. langkind .. "header") end @@ -1513,7 +1394,7 @@ end -- -- @param langkind c/cxx -- -function target:pcoutputfile(langkind) +function _instance:pcoutputfile(langkind) -- init cache self._PCOUTPUTFILES = self._PCOUTPUTFILES or {} @@ -1544,5 +1425,123 @@ function target:pcoutputfile(langkind) end end +-- get target apis +function target.apis() + + return + { + values = + { + -- target.set_xxx + "target.set_kind" + , "target.set_strip" + , "target.set_rules" + , "target.set_version" + , "target.set_enabled" + , "target.set_default" + , "target.set_options" + , "target.set_symbols" + , "target.set_filename" + , "target.set_basename" + , "target.set_warnings" + , "target.set_optimize" + , "target.set_languages" + -- target.add_xxx + , "target.add_deps" + , "target.add_rules" + , "target.add_options" + , "target.add_packages" + , "target.add_imports" + , "target.add_languages" + , "target.add_vectorexts" + } + , keyvalues = + { + -- target.set_xxx + "target.set_values" + , "target.set_configvar" + -- target.add_xxx + , "target.add_values" + } + , pathes = + { + -- target.set_xxx + "target.set_targetdir" + , "target.set_objectdir" + , "target.set_dependir" + , "target.set_configdir" + , "target.set_installdir" + -- target.add_xxx + , "target.add_files" + , "target.add_configfiles" + , "target.add_installfiles" + -- target.del_xxx + , "target.del_files" + } + , dictionary = + { + -- target.set_xxx + "target.set_tools" + , "target.add_tools" + } + , script = + { + -- target.on_xxx + "target.on_run" + , "target.on_load" + , "target.on_build" + , "target.on_build_file" + , "target.on_build_files" + , "target.on_clean" + , "target.on_package" + , "target.on_install" + , "target.on_uninstall" + -- target.before_xxx + , "target.before_run" + , "target.before_build" + , "target.before_build_file" + , "target.before_build_files" + , "target.before_clean" + , "target.before_package" + , "target.before_install" + , "target.before_uninstall" + -- target.after_xxx + , "target.after_run" + , "target.after_build" + , "target.after_build_file" + , "target.after_build_files" + , "target.after_clean" + , "target.after_package" + , "target.after_install" + , "target.after_uninstall" + } + } +end + +-- get the filename from the given target name and kind +function target.filename(targetname, targetkind, targetformat) + + -- check + assert(targetname and targetkind) + + -- make filename by format + local format = targetformat or platform.format(targetkind) + return format and (format:gsub("%$%(name%)", targetname)) or targetname +end + +-- get the link name of the target file +function target.linkname(filename) + local linkname, count = filename:gsub(target.filename("__pattern__", "static"):gsub("%.", "%%."):gsub("__pattern__", "(.+)") .. "$", "%1") + if count == 0 then + linkname, count = filename:gsub(target.filename("__pattern__", "shared"):gsub("%.", "%%."):gsub("__pattern__", "(.+)") .. "$", "%1") + end + return count > 0 and linkname or nil +end + +-- new a target instance +function target.new(...) + return _instance.new(...) +end + -- return module return target |
