diff options
| author | ruki <[email protected]> | 2022-07-02 20:32:53 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-07-02 20:32:53 +0800 |
| commit | 0e77badced0a9e0c9e8aec10fb6c4f324616078c (patch) | |
| tree | 47d6fe4a8adb8864ceddd896a127fb3e9d889465 /xmake/core/package/package.lua | |
| parent | 5708a5c03d35f8761c3d6d07bbb3f6149a879f23 (diff) | |
| parent | bec05e2418d6c2055cd729b7b4e0de7210091473 (diff) | |
Merge pull request #2523 from xmake-io/lto
Add better LTO support
Diffstat (limited to 'xmake/core/package/package.lua')
| -rw-r--r-- | xmake/core/package/package.lua | 60 |
1 files changed, 51 insertions, 9 deletions
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 0de198b90..9dad30ab1 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -1690,8 +1690,41 @@ function _instance:resourcedir(name) end end +-- generate lto configs +function _instance:_generate_lto_configs(sourcekind) + + -- add cflags + local configs = {} + if sourcekind then + local _, cc = self:tool(sourcekind) + local cflag = sourcekind == "cxx" and "cxxflags" or "cflags" + if cc == "cl" then + configs[cflag] = "-GL" + elseif cc == "clang" or cc == "clangxx" then + configs[cflag] = "-flto=thin" + elseif cc == "gcc" or cc == "gxx" then + configs[cflag] = "-flto" + end + end + + -- add ldflags and shflags + local _, ld = self:tool("ld") + if ld == "link" then + configs.ldflags = "-LTCG" + configs.shflags = "-LTCG" + elseif ld == "clang" or ld == "clangxx" then + configs.ldflags = "-flto=thin" + configs.shflags = "-flto=thin" + elseif ld == "gcc" or ld == "gxx" then + configs.ldflags = "-flto" + configs.shflags = "-flto" + end + return configs +end + -- generate building configs for has_xxx/check_xxx -function _instance:_generate_build_configs(configs) +function _instance:_generate_build_configs(configs, opt) + opt = opt or {} configs = table.join(self:fetch_linkdeps(), configs) if self:is_plat("windows") then local ld = self:build_getenv("ld") @@ -1705,6 +1738,15 @@ function _instance:_generate_build_configs(configs) end end end + if self:config("lto") then + local configs_lto = self:_generate_lto_configs(opt.sourcekind or "cxx") + if configs_lto then + for k, v in pairs(configs_lto) do + configs[k] = table.wrap(configs[k] or {}) + table.join2(configs[k], v) + end + end + end if configs and (configs.ldflags or configs.shflags) then configs.force = {ldflags = configs.ldflags, shflags = configs.shflags} configs.ldflags = nil @@ -1723,7 +1765,7 @@ end function _instance:has_cfuncs(funcs, opt) opt = opt or {} opt.target = self - opt.configs = self:_generate_build_configs(opt.configs) + opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "cc"}) return sandbox_module.import("lib.detect.has_cfuncs", {anonymous = true})(funcs, opt) end @@ -1737,7 +1779,7 @@ end function _instance:has_cxxfuncs(funcs, opt) opt = opt or {} opt.target = self - opt.configs = self:_generate_build_configs(opt.configs) + opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "cxx"}) return sandbox_module.import("lib.detect.has_cxxfuncs", {anonymous = true})(funcs, opt) end @@ -1751,7 +1793,7 @@ end function _instance:has_ctypes(types, opt) opt = opt or {} opt.target = self - opt.configs = self:_generate_build_configs(opt.configs) + opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "cc"}) return sandbox_module.import("lib.detect.has_ctypes", {anonymous = true})(types, opt) end @@ -1765,7 +1807,7 @@ end function _instance:has_cxxtypes(types, opt) opt = opt or {} opt.target = self - opt.configs = self:_generate_build_configs(opt.configs) + opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "cxx"}) return sandbox_module.import("lib.detect.has_cxxtypes", {anonymous = true})(types, opt) end @@ -1779,7 +1821,7 @@ end function _instance:has_cincludes(includes, opt) opt = opt or {} opt.target = self - opt.configs = self:_generate_build_configs(opt.configs) + opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "cc"}) return sandbox_module.import("lib.detect.has_cincludes", {anonymous = true})(includes, opt) end @@ -1793,7 +1835,7 @@ end function _instance:has_cxxincludes(includes, opt) opt = opt or {} opt.target = self - opt.configs = self:_generate_build_configs(opt.configs) + opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "cxx"}) return sandbox_module.import("lib.detect.has_cxxincludes", {anonymous = true})(includes, opt) end @@ -1807,7 +1849,7 @@ end function _instance:check_csnippets(snippets, opt) opt = opt or {} opt.target = self - opt.configs = self:_generate_build_configs(opt.configs) + opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "cc"}) return sandbox_module.import("lib.detect.check_csnippets", {anonymous = true})(snippets, opt) end @@ -1821,7 +1863,7 @@ end function _instance:check_cxxsnippets(snippets, opt) opt = opt or {} opt.target = self - opt.configs = self:_generate_build_configs(opt.configs) + opt.configs = self:_generate_build_configs(opt.configs, {sourcekind = "cxx"}) return sandbox_module.import("lib.detect.check_cxxsnippets", {anonymous = true})(snippets, opt) end |
