1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author ruki
-- @file config.lua
--
-- imports
import("core.tool.compiler")
import("core.project.project")
import("lib.detect.find_file")
-- get liblto_plugin.so path of gcc
function _get_gcc_liblto_plugin_path(target, gcc)
local cachekey = "lto_plugin_" .. gcc
local plugin_path = target:data(cachekey)
if plugin_path == nil then
local outdata = try { function() return os.iorunv(gcc, {"-print-prog-name=lto-wrapper"}) end }
if outdata then
local lto_plugindir = path.directory(outdata:trim())
if os.isdir(lto_plugindir) then
if is_host("windows") then
plugin_path = find_file("liblto_plugin*.dll", lto_plugindir)
else
plugin_path = find_file("liblto_plugin.so", lto_plugindir)
end
end
end
target:data_set(cachekey, plugin_path or false)
end
return plugin_path or nil
end
-- add lto optimization
function _add_lto_optimization(target, sourcekind)
-- add cflags
local _, cc = target:tool(sourcekind)
local cflag = sourcekind == "cxx" and "cxxflags" or "cflags"
if cc == "cl" then
target:add(cflag, "-GL")
elseif cc == "clang" or cc == "clangxx" then
target:add(cflag, "-flto=thin")
elseif cc == "gcc" or cc == "gxx" then
target:add(cflag, "-flto")
end
-- add ldflags and shflags
local program, ld = target:tool("ld")
if ld == "link" then
target:add("ldflags", "-LTCG")
target:add("shflags", "-LTCG")
elseif ld == "clang" or ld == "clangxx" then
target:add("ldflags", "-flto=thin")
target:add("shflags", "-flto=thin")
elseif ld == "gcc" or ld == "gxx" then
target:add("ldflags", "-flto")
target:add("shflags", "-flto")
-- @see https://github.com/xmake-io/xmake/issues/5015
-- add lto_plugin.so to ar
local lto_plugin = _get_gcc_liblto_plugin_path(target, program)
if lto_plugin then
target:add("arflags", {"--plugin", lto_plugin}, {force = true, expand = false})
end
-- to use the link-time optimizer, -flto and optimization options should be specified at compile time and during the final link.
-- @see https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
local optimize = target:get("optimize")
if optimize then
local optimize_flags = compiler.map_flags(sourcekind == "cc" and "c" or "cxx", "optimize", optimize)
target:add("ldflags", optimize_flags)
target:add("shflags", optimize_flags)
end
end
end
function main(target, sourcekind)
if target:policy("build.optimization.lto") or
project.policy("build.optimization.lto") then
_add_lto_optimization(target, sourcekind)
end
end
|