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/modules | |
| parent | 5708a5c03d35f8761c3d6d07bbb3f6149a879f23 (diff) | |
| parent | bec05e2418d6c2055cd729b7b4e0de7210091473 (diff) | |
Merge pull request #2523 from xmake-io/lto
Add better LTO support
Diffstat (limited to 'xmake/modules')
| -rw-r--r-- | xmake/modules/package/tools/autoconf.lua | 53 | ||||
| -rw-r--r-- | xmake/modules/package/tools/cmake.lua | 12 | ||||
| -rw-r--r-- | xmake/modules/package/tools/xmake.lua | 3 | ||||
| -rw-r--r-- | xmake/modules/private/action/require/impl/package.lua | 4 |
4 files changed, 57 insertions, 15 deletions
diff --git a/xmake/modules/package/tools/autoconf.lua b/xmake/modules/package/tools/autoconf.lua index 7e3c72f55..77797e93a 100644 --- a/xmake/modules/package/tools/autoconf.lua +++ b/xmake/modules/package/tools/autoconf.lua @@ -155,12 +155,14 @@ end function buildenvs(package, opt) opt = opt or {} local envs = {} - local cppflags = {} + local cross = false + local cflags, cxxflags, cppflags, asflags, ldflags, shflags, arflags if package:is_plat(os.subhost()) and not package:config("toolchains") then - local cflags = table.join(table.wrap(package:config("cxflags")), package:config("cflags")) - local cxxflags = table.join(table.wrap(package:config("cxflags")), package:config("cxxflags")) - local asflags = table.copy(table.wrap(package:config("asflags"))) - local ldflags = table.copy(table.wrap(package:config("ldflags"))) + cppflags = {} + cflags = table.join(table.wrap(package:config("cxflags")), package:config("cflags")) + cxxflags = table.join(table.wrap(package:config("cxflags")), package:config("cxxflags")) + asflags = table.copy(table.wrap(package:config("asflags"))) + ldflags = table.copy(table.wrap(package:config("ldflags"))) if package:is_plat("linux") and package:is_arch("i386") then table.insert(cflags, "-m32") table.insert(cxxflags, "-m32") @@ -184,12 +186,14 @@ function buildenvs(package, opt) envs.ASFLAGS = table.concat(asflags, ' ') envs.LDFLAGS = table.concat(ldflags, ' ') else - local cflags = table.join(table.wrap(package:build_getenv("cxflags")), package:build_getenv("cflags")) - local cxxflags = table.join(table.wrap(package:build_getenv("cxflags")), package:build_getenv("cxxflags")) - local asflags = table.copy(table.wrap(package:build_getenv("asflags"))) - local ldflags = table.copy(table.wrap(package:build_getenv("ldflags"))) - local shflags = table.copy(table.wrap(package:build_getenv("shflags"))) - local arflags = table.copy(table.wrap(package:build_getenv("arflags"))) + cross = true + cppflags = {} + cflags = table.join(table.wrap(package:build_getenv("cxflags")), package:build_getenv("cflags")) + cxxflags = table.join(table.wrap(package:build_getenv("cxflags")), package:build_getenv("cxxflags")) + asflags = table.copy(table.wrap(package:build_getenv("asflags"))) + ldflags = table.copy(table.wrap(package:build_getenv("ldflags"))) + shflags = table.copy(table.wrap(package:build_getenv("shflags"))) + arflags = table.copy(table.wrap(package:build_getenv("arflags"))) local defines = package:build_getenv("defines") local includedirs = package:build_getenv("includedirs") local sysincludedirs = package:build_getenv("sysincludedirs") @@ -231,13 +235,32 @@ function buildenvs(package, opt) envs.LDSHARED = package:build_getenv("sh") envs.CPP = package:build_getenv("cpp") envs.RANLIB = package:build_getenv("ranlib") - envs.CFLAGS = table.concat(cflags, ' ') - envs.CXXFLAGS = table.concat(cxxflags, ' ') - envs.CPPFLAGS = table.concat(cppflags, ' ') - envs.ASFLAGS = table.concat(asflags, ' ') + end + if package:is_plat("linux") and package:config("pic") ~= false then + table.insert(cflags, "-fPIC") + table.insert(cxxflags, "-fPIC") + end + if package:config("lto") then + table.join2(cflags, package:_generate_lto_configs("cc").cflags) + table.join2(cxxflags, package:_generate_lto_configs("cxx").cxxflags) + table.join2(ldflags, package:_generate_lto_configs().ldflags) + end + envs.CFLAGS = table.concat(cflags, ' ') + envs.CXXFLAGS = table.concat(cxxflags, ' ') + envs.CPPFLAGS = table.concat(cppflags, ' ') + envs.ASFLAGS = table.concat(asflags, ' ') + if arflags then envs.ARFLAGS = table.concat(arflags, ' ') + end + if ldflags then envs.LDFLAGS = table.concat(ldflags, ' ') + end + if shflags then envs.SHFLAGS = table.concat(shflags, ' ') + end + + -- cross-compilation? pass the full build environments + if cross then if package:is_plat("mingw") then -- fix linker error, @see https://github.com/xmake-io/xmake/issues/574 -- libtool: line 1855: lib: command not found diff --git a/xmake/modules/package/tools/cmake.lua b/xmake/modules/package/tools/cmake.lua index 00f0c937e..dc227b89b 100644 --- a/xmake/modules/package/tools/cmake.lua +++ b/xmake/modules/package/tools/cmake.lua @@ -137,6 +137,9 @@ function _get_cflags(package, opt) if opt.cxflags then table.join2(result, opt.cxflags) end + if package:config("lto") then + table.join2(result, package:_generate_lto_configs("cc").cflags) + end table.join2(result, _get_cflags_from_packagedeps(package, opt)) if #result > 0 then return os.args(result) @@ -162,6 +165,9 @@ function _get_cxxflags(package, opt) if opt.cxflags then table.join2(result, opt.cxflags) end + if package:config("lto") then + table.join2(result, package:_generate_lto_configs("cxx").cxxflags) + end table.join2(result, _get_cflags_from_packagedeps(package, opt)) if #result > 0 then return os.args(result) @@ -197,6 +203,9 @@ function _get_ldflags(package, opt) table.join2(result, _map_linkflags(package, "binary", {"cxx"}, "syslink", package:build_getenv("syslinks"))) table.join2(result, _map_linkflags(package, "binary", {"cxx"}, "linkdir", package:build_getenv("linkdirs"))) end + if package:config("lto") then + table.join2(result, package:_generate_lto_configs().ldflags) + end table.join2(result, _get_ldflags_from_packagedeps(package, opt)) if opt.ldflags then table.join2(result, opt.ldflags) @@ -216,6 +225,9 @@ function _get_shflags(package, opt) table.join2(result, _map_linkflags(package, "shared", {"cxx"}, "syslink", package:build_getenv("syslinks"))) table.join2(result, _map_linkflags(package, "shared", {"cxx"}, "linkdir", package:build_getenv("linkdirs"))) end + if package:config("lto") then + table.join2(result, package:_generate_lto_configs().shflags) + end table.join2(result, _get_ldflags_from_packagedeps(package, opt)) if opt.shflags then table.join2(result, opt.shflags) diff --git a/xmake/modules/package/tools/xmake.lua b/xmake/modules/package/tools/xmake.lua index 6e98adc7b..b871afcd2 100644 --- a/xmake/modules/package/tools/xmake.lua +++ b/xmake/modules/package/tools/xmake.lua @@ -94,6 +94,9 @@ function _get_configs(package, configs) end end end + if package:config("lto") then + table.insert(configs, "--policies=build.optimization.lto") + end if not package:is_plat("windows", "mingw") and package:config("pic") ~= false then table.insert(cxflags, "-fPIC") end diff --git a/xmake/modules/private/action/require/impl/package.lua b/xmake/modules/private/action/require/impl/package.lua index ca11a10d3..cf330f308 100644 --- a/xmake/modules/private/action/require/impl/package.lua +++ b/xmake/modules/private/action/require/impl/package.lua @@ -310,6 +310,9 @@ function _add_package_configurations(package) if package:extraconf("configs", "pic", "default") == nil then package:add("configs", "pic", {builtin = true, description = "Enable the position independent code.", default = true, type = "boolean"}) end + if package:extraconf("configs", "lto", "default") == nil then + package:add("configs", "lto", {builtin = true, description = "Enable the link-time build optimization.", type = "boolean"}) + end if package:extraconf("configs", "vs_runtime", "default") == nil then package:add("configs", "vs_runtime", {builtin = true, description = "Set vs compiler runtime.", values = {"MT", "MTd", "MD", "MDd"}}) end @@ -474,6 +477,7 @@ function _init_requireinfo(requireinfo, package, opt) if project.policy("package.inherit_external_configs") then requireinfo.configs.vs_runtime = requireinfo.configs.vs_runtime or get_config("vs_runtime") end + requireinfo.configs.lto = requireinfo.configs.lto or project.policy("build.optimization.lto") end end end |
