Skip to content

Commit 21ae0fa

Browse files
authored
Merge pull request #5543 from xmake-io/package
improve linker for package #5542
2 parents 2f3f1a0 + b7ef860 commit 21ae0fa

File tree

8 files changed

+124
-140
lines changed

8 files changed

+124
-140
lines changed

tests/projects/c++/snippet_runtimes/xmake.lua

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ add_requires("bar")
44
target("foo")
55
set_kind("binary")
66
add_files("src/*.cpp")
7-
87
add_packages("bar")
98

109
on_config(function(target)

xmake/core/package/package.lua

+30-11
Original file line numberDiff line numberDiff line change
@@ -1281,9 +1281,9 @@ function _instance:toolconfig(name)
12811281
end
12821282
end
12831283

1284-
-- get the target compiler
1284+
-- get the package compiler
12851285
function _instance:compiler(sourcekind)
1286-
local compilerinst = self:_memcache():get("compiler")
1286+
local compilerinst = self:_memcache():get2("compiler", sourcekind)
12871287
if not compilerinst then
12881288
if not sourcekind then
12891289
os.raise("please pass sourcekind to the first argument of package:compiler(), e.g. cc, cxx, as")
@@ -1293,11 +1293,28 @@ function _instance:compiler(sourcekind)
12931293
os.raise(errors)
12941294
end
12951295
compilerinst = instance
1296-
self:_memcache():set("compiler", compilerinst)
1296+
self:_memcache():set2("compiler", sourcekind, compilerinst)
12971297
end
12981298
return compilerinst
12991299
end
13001300

1301+
-- get the package linker
1302+
function _instance:linker(targetkind, sourcekinds)
1303+
local linkerinst = self:_memcache():get3("linker", targetkind, sourcekinds)
1304+
if not linkerinst then
1305+
if not sourcekinds then
1306+
os.raise("please pass sourcekinds to the second argument of package:linker(), e.g. cc, cxx, as")
1307+
end
1308+
local instance, errors = linker.load(targetkind, sourcekinds, self)
1309+
if not instance then
1310+
os.raise(errors)
1311+
end
1312+
linkerinst = instance
1313+
self:_memcache():set3("linker", targetkind, sourcekinds, linkerinst)
1314+
end
1315+
return linkerinst
1316+
end
1317+
13011318
-- has the given tool for the current package?
13021319
--
13031320
-- e.g.
@@ -2357,22 +2374,24 @@ function _instance:_generate_build_configs(configs, opt)
23572374
end
23582375
end
23592376
if runtimes then
2377+
-- @note we need to patch package:sourcekinds(), because it wiil be called nf_runtime for gcc/clang
23602378
local sourcekind = opt.sourcekind or "cxx"
2361-
local tool, name = self:tool("ld")
2362-
local linker, errors = linker.load("binary", sourcekind, {target = package})
2363-
if not linker then
2364-
os.raise(errors)
2379+
self.sourcekinds = function (self)
2380+
return sourcekind
23652381
end
2366-
local fake_target = {is_shared = function(_) return false end,
2367-
sourcekinds = function(_) return sourcekind end}
23682382
local compiler = self:compiler(sourcekind)
2369-
local cxflags = compiler:map_flags("runtime", runtimes, {target = fake_target})
2383+
local cxflags = compiler:map_flags("runtime", runtimes, {target = self})
23702384
configs.cxflags = table.wrap(configs.cxflags)
23712385
table.join2(configs.cxflags, cxflags)
23722386

2373-
local ldflags = linker:map_flags("runtime", runtimes, {target = fake_target})
2387+
local ldflags = self:linker("binary", sourcekind):map_flags("runtime", runtimes, {target = self})
23742388
configs.ldflags = table.wrap(configs.ldflags)
23752389
table.join2(configs.ldflags, ldflags)
2390+
2391+
local shflags = self:linker("shared", sourcekind):map_flags("runtime", runtimes, {target = self})
2392+
configs.shflags = table.wrap(configs.shflags)
2393+
table.join2(configs.shflags, shflags)
2394+
self.sourcekinds = nil
23762395
end
23772396
if self:config("lto") then
23782397
local configs_lto = self:_generate_lto_configs(opt.sourcekind or "cxx")

xmake/modules/core/tools/clang.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ function nf_runtime(self, runtime, opt)
296296
if triple_libdir then
297297
maps["c++_shared"] = table.join(maps["c++_shared"], nf_rpathdir(self, triple_libdir))
298298
end
299-
if target:is_shared() and self:is_plat("macosx", "iphoneos", "watchos") then
299+
if target:is_shared() and target.filename and self:is_plat("macosx", "iphoneos", "watchos") then
300300
maps["c++_shared"] = table.join(maps["c++_shared"], "-install_name")
301301
maps["c++_shared"] = table.join(maps["c++_shared"], "@rpath/" .. target:filename())
302302
end

xmake/modules/package/tools/autoconf.lua

+26-41
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121
-- imports
2222
import("core.base.option")
2323
import("core.project.config")
24-
import("core.tool.linker")
25-
import("core.tool.compiler")
2624
import("core.tool.toolchain")
2725
import("core.cache.memcache")
2826
import("lib.detect.find_tool")
27+
import("private.utils.toolchain", {alias = "toolchain_utils"})
2928

3029
-- translate paths
3130
function _translate_paths(paths)
@@ -78,16 +77,6 @@ function _get_msvc_runenvs(package)
7877
return os.joinenvs(_get_msvc(package):runenvs())
7978
end
8079

81-
-- map compiler flags
82-
function _map_compflags(package, langkind, name, values)
83-
return compiler.map_flags(langkind, name, values, {target = package})
84-
end
85-
86-
-- map linker flags
87-
function _map_linkflags(package, targetkind, sourcekinds, name, values)
88-
return linker.map_flags(targetkind, sourcekinds, name, values, {target = package})
89-
end
90-
9180
-- is cross compilation?
9281
function _is_cross_compilation(package)
9382
if not package:is_plat(os.subhost()) then
@@ -199,13 +188,13 @@ function _get_cflags_from_packagedeps(package, opt)
199188
local result = {}
200189
if values then
201190
if values.defines then
202-
table.join2(result, _map_compflags(package, "cxx", "define", values.defines))
191+
table.join2(result, toolchain_utils.map_compflags_for_package(package, "cxx", "define", values.defines))
203192
end
204193
if values.includedirs then
205-
table.join2(result, _translate_paths(_map_compflags(package, "cxx", "includedir", values.includedirs)))
194+
table.join2(result, _translate_paths(toolchain_utils.map_compflags_for_package(package, "cxx", "includedir", values.includedirs)))
206195
end
207196
if values.sysincludedirs then
208-
table.join2(result, _translate_paths(_map_compflags(package, "cxx", "sysincludedir", values.sysincludedirs)))
197+
table.join2(result, _translate_paths(toolchain_utils.map_compflags_for_package(package, "cxx", "sysincludedir", values.sysincludedirs)))
209198
end
210199
end
211200
return result
@@ -230,16 +219,16 @@ function _get_ldflags_from_packagedeps(package, opt)
230219
local result = {}
231220
if values then
232221
if values.linkdirs then
233-
table.join2(result, _translate_paths(_map_linkflags(package, "binary", {"cxx"}, "linkdir", values.linkdirs)))
222+
table.join2(result, _translate_paths(toolchain_utils.map_linkflags_for_package(package, "binary", {"cxx"}, "linkdir", values.linkdirs)))
234223
end
235224
if values.links then
236-
table.join2(result, _map_linkflags(package, "binary", {"cxx"}, "link", values.links))
225+
table.join2(result, toolchain_utils.map_linkflags_for_package(package, "binary", {"cxx"}, "link", values.links))
237226
end
238227
if values.syslinks then
239-
table.join2(result, _translate_paths(_map_linkflags(package, "binary", {"cxx"}, "syslink", values.syslinks)))
228+
table.join2(result, _translate_paths(toolchain_utils.map_linkflags_for_package(package, "binary", {"cxx"}, "syslink", values.syslinks)))
240229
end
241230
if values.frameworks then
242-
table.join2(result, _map_linkflags(package, "binary", {"cxx"}, "framework", values.frameworks))
231+
table.join2(result, toolchain_utils.map_linkflags_for_package(package, "binary", {"cxx"}, "framework", values.frameworks))
243232
end
244233
end
245234
return result
@@ -306,21 +295,21 @@ function buildenvs(package, opt)
306295
table.join2(cxxflags, _get_cflags_from_packagedeps(package, opt))
307296
table.join2(cppflags, _get_cflags_from_packagedeps(package, opt))
308297
table.join2(ldflags, _get_ldflags_from_packagedeps(package, opt))
309-
table.join2(cflags, _map_compflags(package, "c", "define", defines))
310-
table.join2(cflags, _map_compflags(package, "c", "includedir", includedirs))
311-
table.join2(cflags, _map_compflags(package, "c", "sysincludedir", sysincludedirs))
312-
table.join2(asflags, _map_compflags(package, "as", "define", defines))
313-
table.join2(asflags, _map_compflags(package, "as", "includedir", includedirs))
314-
table.join2(asflags, _map_compflags(package, "as", "sysincludedir", sysincludedirs))
315-
table.join2(cxxflags, _map_compflags(package, "cxx", "define", defines))
316-
table.join2(cxxflags, _map_compflags(package, "cxx", "includedir", includedirs))
317-
table.join2(cxxflags, _map_compflags(package, "cxx", "sysincludedir", sysincludedirs))
318-
table.join2(ldflags, _map_linkflags(package, "binary", {"cxx"}, "link", links))
319-
table.join2(ldflags, _map_linkflags(package, "binary", {"cxx"}, "syslink", syslinks))
320-
table.join2(ldflags, _map_linkflags(package, "binary", {"cxx"}, "linkdir", linkdirs))
321-
table.join2(shflags, _map_linkflags(package, "shared", {"cxx"}, "link", links))
322-
table.join2(shflags, _map_linkflags(package, "shared", {"cxx"}, "syslink", syslinks))
323-
table.join2(shflags, _map_linkflags(package, "shared", {"cxx"}, "linkdir", linkdirs))
298+
table.join2(cflags, toolchain_utils.map_compflags_for_package(package, "c", "define", defines))
299+
table.join2(cflags, toolchain_utils.map_compflags_for_package(package, "c", "includedir", includedirs))
300+
table.join2(cflags, toolchain_utils.map_compflags_for_package(package, "c", "sysincludedir", sysincludedirs))
301+
table.join2(asflags, toolchain_utils.map_compflags_for_package(package, "as", "define", defines))
302+
table.join2(asflags, toolchain_utils.map_compflags_for_package(package, "as", "includedir", includedirs))
303+
table.join2(asflags, toolchain_utils.map_compflags_for_package(package, "as", "sysincludedir", sysincludedirs))
304+
table.join2(cxxflags, toolchain_utils.map_compflags_for_package(package, "cxx", "define", defines))
305+
table.join2(cxxflags, toolchain_utils.map_compflags_for_package(package, "cxx", "includedir", includedirs))
306+
table.join2(cxxflags, toolchain_utils.map_compflags_for_package(package, "cxx", "sysincludedir", sysincludedirs))
307+
table.join2(ldflags, toolchain_utils.map_linkflags_for_package(package, "binary", {"cxx"}, "link", links))
308+
table.join2(ldflags, toolchain_utils.map_linkflags_for_package(package, "binary", {"cxx"}, "syslink", syslinks))
309+
table.join2(ldflags, toolchain_utils.map_linkflags_for_package(package, "binary", {"cxx"}, "linkdir", linkdirs))
310+
table.join2(shflags, toolchain_utils.map_linkflags_for_package(package, "shared", {"cxx"}, "link", links))
311+
table.join2(shflags, toolchain_utils.map_linkflags_for_package(package, "shared", {"cxx"}, "syslink", syslinks))
312+
table.join2(shflags, toolchain_utils.map_linkflags_for_package(package, "shared", {"cxx"}, "linkdir", linkdirs))
324313
envs.CC = package:build_getenv("cc")
325314
envs.AS = package:build_getenv("as")
326315
envs.AR = package:build_getenv("ar")
@@ -341,13 +330,9 @@ function buildenvs(package, opt)
341330
end
342331
local runtimes = package:runtimes()
343332
if runtimes then
344-
local fake_target = {is_shared = function(_) return false end,
345-
sourcekinds = function(_) return "cxx" end}
346-
table.join2(cxxflags, _map_compflags(fake_target, "cxx", "runtime", runtimes))
347-
table.join2(ldflags, _map_linkflags(fake_target, "binary", {"cxx"}, "runtime", runtimes))
348-
fake_target = {is_shared = function(_) return true end,
349-
sourcekinds = function(_) return "cxx" end}
350-
table.join2(shflags, _map_linkflags(fake_target, "shared", {"cxx"}, "runtime", runtimes))
333+
table.join2(cxxflags, toolchain_utils.map_compflags_for_package(package, "cxx", "runtime", runtimes))
334+
table.join2(ldflags, toolchain_utils.map_linkflags_for_package(package, "binary", {"cxx"}, "runtime", runtimes))
335+
table.join2(shflags, toolchain_utils.map_linkflags_for_package(package, "shared", {"cxx"}, "runtime", runtimes))
351336
end
352337
if package:config("asan") then
353338
table.join2(cflags, package:_generate_sanitizer_configs("address", "cc").cflags)

0 commit comments

Comments
 (0)