diff options
| author | ruki <[email protected]> | 2024-03-13 23:30:29 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2024-03-13 23:30:29 +0800 |
| commit | 0f77719ed8570dd5630b7212c5198788f396c54e (patch) | |
| tree | 8a26aa47e0bdb899793732ffa7e30de2ea5d2218 | |
| parent | 487a1047fab0914b755ea7a87859203a971ce3f2 (diff) | |
improve to extract symbols #4684
| -rw-r--r-- | xmake/rules/utils/symbols/extract/xmake.lua | 118 |
1 files changed, 63 insertions, 55 deletions
diff --git a/xmake/rules/utils/symbols/extract/xmake.lua b/xmake/rules/utils/symbols/extract/xmake.lua index 222634688..c59adc058 100644 --- a/xmake/rules/utils/symbols/extract/xmake.lua +++ b/xmake/rules/utils/symbols/extract/xmake.lua @@ -27,9 +27,8 @@ rule("utils.symbols.extract") -- need generate symbols? local strip = target:get("strip") - local targetkind = target:kind() if target:get("symbols") == "debug" and (strip == "all" or strip == "debug") - and (targetkind == "binary" or targetkind == "shared") and platform.tool("strip") then -- only for strip command + and (target:is_binary() or target:is_shared()) and target:tool("strip") then -- only for strip command target:data_set("utils.symbols.extract", true) target:set("strip", "none") -- disable strip in link stage, because we need to run separate strip commands target:data_set("strip.origin", strip) @@ -55,74 +54,83 @@ rule("utils.symbols.extract") return end - -- get dsymutil - local dsymutil - if is_plat("macosx", "iphoneos", "watchos") then + -- get dsymutil and objcopy + local dsymutil, objcopy + if target:is_plat("macosx", "iphoneos", "watchos") then dsymutil = target:tool("dsymutil") if not dsymutil then return end + else + objcopy = target:tool("objcopy") end - -- need re-generate this symbol file? - local symbolfile = target:symbolfile() + -- @note we use dependfile(targetfile) as sourcefile/mtime instead of targetfile to ensure it's mtime less than mtime(symbolfile), because targetfile will be changed after stripping local targetfile = target:targetfile() - local dependfile = target:dependfile(symbolfile) - local dependinfo = target:is_rebuilt() and {} or (depend.load(dependfile) or {}) - if not depend.is_changed(dependinfo, {lastmtime = os.mtime(dependfile)}) then - return - end - - -- trace progress info - progress.show(opt.progress, "${color.build.target}generating.$(mode) %s", path.filename(symbolfile)) - - -- we remove the previous symbol file to ensure that it will be re-generated and it's mtime will be changed. + local symbolfile = target:symbolfile() local dryrun = option.get("dry-run") - if not dryrun then - os.tryrm(symbolfile) - end + depend.on_changed(function () - -- generate symbols file - if dsymutil then - local dsymutil_argv = {} - local arch = get_config("arch") - if arch then - table.insert(dsymutil_argv, "-arch") - table.insert(dsymutil_argv, arch) + -- trace progress info + progress.show(opt.progress, "${color.build.target}generating.$(mode) %s", path.filename(symbolfile)) + + -- we remove the previous symbol file to ensure that it will be re-generated and it's mtime will be changed. + if not dryrun then + os.tryrm(symbolfile) end - table.insert(dsymutil_argv, targetfile) - table.insert(dsymutil_argv, "-o") - table.insert(dsymutil_argv, symbolfile) - os.vrunv(dsymutil, dsymutil_argv, {dryrun = dryrun}) - elseif not dryrun then - os.vcp(targetfile, symbolfile) - end - -- strip it - local strip_argv = {} - if is_plat("macosx", "iphoneos", "watchos") then - -- do not support `-s`, we can only strip debug symbols - local arch = get_config("arch") - if arch then - table.insert(strip_argv, "-arch") - table.insert(strip_argv, arch) + -- generate symbols file + if dsymutil then + local dsymutil_argv = {} + local arch = target:arch() + if arch then + table.insert(dsymutil_argv, "-arch") + table.insert(dsymutil_argv, arch) + end + table.insert(dsymutil_argv, targetfile) + table.insert(dsymutil_argv, "-o") + table.insert(dsymutil_argv, symbolfile) + os.vrunv(dsymutil, dsymutil_argv, {dryrun = dryrun}) + else + -- @see https://github.com/xmake-io/xmake/issues/4684 + if objcopy then + os.vrunv(objcopy, {"--only-keep-debug", targetfile, symbolfile}, {dryrun = dryrun}) + elseif not dryrun then + os.vcp(targetfile, symbolfile) + end end - table.insert(strip_argv, "-S") - else - -- -s/--strip-all for gnu strip - local strip = target:data("strip.origin") - if strip == "debug" then + + -- strip it + local strip_argv = {} + if target:is_plat("macosx", "iphoneos", "watchos") then + -- do not support `-s`, we can only strip debug symbols + local arch = target:arch() + if arch then + table.insert(strip_argv, "-arch") + table.insert(strip_argv, arch) + end table.insert(strip_argv, "-S") else - table.insert(strip_argv, "-s") + -- -s/--strip-all for gnu strip + local strip = target:data("strip.origin") + if strip == "debug" then + table.insert(strip_argv, "-S") + else + table.insert(strip_argv, "-s") + end end - end - table.insert(strip_argv, targetfile) - os.vrunv(strip, strip_argv, {dryrun = dryrun}) + table.insert(strip_argv, targetfile) + os.vrunv(strip, strip_argv, {dryrun = dryrun}) - -- update files and values to the dependent file - -- @note we use dependfile(targetfile) as sourcefile/mtime instead of targetfile to ensure it's mtime less than mtime(symbolfile), because targetfile will be changed after stripping - dependinfo.files = {target:dependfile(targetfile)} - depend.save(dependinfo, dependfile) + -- attach symbolfile to targetfile + if not target:is_plat("macosx", "iphoneos", "watchos") and objcopy then + -- @see https://github.com/xmake-io/xmake/issues/4684 + os.vrunv(objcopy, {"--add-gnu-debuglink=" .. symbolfile, targetfile}, {dryrun = dryrun}) + end + + end, {dependfile = target:dependfile(symbolfile), + files = target:dependfile(targetfile), + changed = target:is_rebuilt(), + dryrun = dryrun}) end) |
