123456789101112131415161718192021222324252627282930313233343536373839404142 | #!/bin/sh
# Generate a source file that embeds version info for everything statically
# linked into Tomo binaries, as a named section retrievable with standard
# tools:
# readelf -p .tomo.versions BINARY (ELF)
# segedit BINARY -extract __TEXT __tomo_versions /dev/stdout (Mach-O)
#
# For ELF targets the output is assembly (data directives only, so it works for
# any architecture): the section is flagged SHF_STRINGS ("S") so `readelf -p`
# pretty-prints one NUL-terminated string per line, and SHF_GNU_RETAIN ("R") so
# it survives --gc-sections, which zig cc's linker enables by default. Mach-O
# has no equivalent section flags, so macOS targets generate C instead.
#
# Usage: embed_versions.sh OUTPUT.s|OUTPUT.c VERSIONS_FILE
set -e
out=$1
versions=$2
case "$out" in
*.s)
{
printf '# Generated by scripts/embed_versions.sh. Do not edit.\n'
printf '.pushsection .tomo.versions,"aRS",%%progbits\n'
printf '.globl tomo_versions\ntomo_versions:\n'
tr -d '\r' < "$versions" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/^/.asciz "/' -e 's/$/"/'
printf '.popsection\n'
} > "$out"
;;
*.c)
{
printf '// Generated by scripts/embed_versions.sh. Do not edit.\n'
printf '__attribute__((used, section("__TEXT,__tomo_versions"))) const char tomo_versions[] =\n'
tr -d '\r' < "$versions" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/^/"/' -e 's/$/\\n"/'
printf ';\n'
} > "$out"
;;
*)
echo "embed_versions.sh: output must end in .s (ELF) or .c (Mach-O): $out" >&2
exit 1
;;
esac
|