Branch
Hash :
7b089321
Author :
Date :
2025-01-01T09:24:36
maint: run 'make update-copyright'
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#!/bin/sh
# Wrapper for compilers on macOS
# that lose debug information in the final link of an executable.
scriptversion=2024-05-20.15; # UTC
# Copyright (C) 2024-2025 Free Software Foundation, Inc.
#
# This file is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Written by Bruno Haible <bruno@clisp.org>, 2024.
# The problem:
# 1) On macOS, debug information for executables is not stored in the
# executable itself, but in a separate file: If the executable is in
# ${DIR}/${BASENAME}
# the debug info is in
# ${DIR}/${BASENAME}.dSYM/Contents/Resources/DWARF/${BASENAME}
# 2) The Apple compilers on macOS, in the linking step that creates an
# executable from object files, do not create this debug info.
# An extra command
# dsymutil ${DIR}/${BASENAME}
# is necessary in order to create it.
#
# See
# https://stackoverflow.com/questions/32297349/why-does-a-2-stage-command-line-build-with-clang-not-generate-a-dsym-directory
# https://stackoverflow.com/questions/10044697/where-how-does-apples-gcc-store-dwarf-inside-an-executable/12827463#12827463
# https://wiki.dwarfstd.org/Apple%27s_%22Lazy%22_DWARF_Scheme.md
# The solution:
# Make
# macos-compile clang [OPTION...] FILE...
# behave like
# clang [OPTION...] FILE...
# does on other platforms, namely to create the debug info during linking
# of the executable (instead of leaving it for later).
# Note: 1) is also a problem for the 'install' command that installs an
# executable in a public location. This is not handled here.
# func_usage
# outputs to stdout the --help usage message.
func_usage ()
{
echo "\
Usage: macos-compile COMPILER [OPTION...] FILE...
Invokes COMPILER [OPTION...] FILE... and handles debug information.
Report bugs to <bug-gnulib@gnu.org>."
}
# Handle --help and --version.
case "$1" in
--help)
func_usage
exit $?
;;
--version)
echo "macos-compile (GNU gnulib) $scriptversion
Copyright (C) 2024-2025 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Bruno Haible."
exit $?
;;
esac
# Set to true for the linking step that creates an executable.
linking=true
# The executable created by the command.
output_file='a.out'
output_file_is_next=false
# Whether debug information is requested.
debugging=false
# Process the command-line options.
first=true
for arg
do
if $first; then
first=false
set -- "$arg"
else
if $output_file_is_next; then
output_file="$arg"
output_file_is_next=false
else
case "$arg" in
-c | -E | -S | -fdriver-only | --precompile )
linking=false
;;
-o )
output_file_is_next=true
;;
-g* )
debugging=true
;;
esac
fi
set -- "$@" "$arg"
fi
done
# Execute the command and the $output_file.dSYM workaround.
if $linking; then
rm -rf "$output_file.dSYM"
fi
"$@" || exit $?
if $linking; then
# Do this dsymutil invocation also when not $debugging.
# The linking step is supposed to copy and link the debug information from
# the previous steps always.
# Example: $CC -g -O2 -c foo.c && $CC foo.o
dsymutil "$output_file" || exit $?
fi
exit 0