Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Modules / Platform / AIX / ExportImportList
1 #!/bin/sh
2 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
3 # file Copyright.txt or https://cmake.org/licensing for details.
4
5 # This script is internal to CMake and meant only to be
6 # invoked by CMake-generated build systems on AIX.
7
8 usage='usage: ExportImportList -o <out-file> -c <compiler> [-l <lib>] [-n] [--] <objects>...'
9
10 die() {
11     echo "$@" 1>&2; exit 1
12 }
13
14 # Process command-line arguments.
15 out=''
16 lib=''
17 no_objects=''
18 compiler=''
19 while test "$#" != 0; do
20     case "$1" in
21     -l) shift; lib="$1" ;;
22     -o) shift; out="$1" ;;
23     -n) no_objects='1' ;;
24     -c) shift; compiler="$1" ;;
25     --) shift; break ;;
26     -*) die "$usage" ;;
27     *)  break ;;
28     esac
29     shift
30 done
31 test -n "$out" || die "$usage"
32 # We need the compiler executable to resolve where the ibm-llvm-nm executable is
33 test -n "$compiler" || die "$usage"
34
35 # Build a temporary file that atomically replaces the output later.
36 out_tmp="$out.tmp$$"
37 trap 'rm -f "$out_tmp"' EXIT INT TERM
38 > "$out_tmp"
39
40 # If IPA was enabled and a compiler from the IBMClang family is used, then
41 # the object files contain LLVM bitcode[0] rather than XCOFF objects and so
42 # need to be handled differently.
43 #
44 # [0]: https://www.ibm.com/docs/en/openxl-c-and-cpp-aix/17.1.0?topic=compatibility-link-time-optimization-lto
45 NM="$(dirname "$compiler")/../libexec/ibm-llvm-nm"
46
47 function IsBitcode {
48     # N4 = first 4 bytes, -tx = output in hexadecimal, -An = don't display offset
49     # cut: trim off the preceding whitespace where the offset would be
50     # 4243code is the hexadecimal magic number for LLVM bitcode
51     [ "$(od -N4 -tx -An $1 | cut -d ' ' -f 2)" == "4243c0de" ];
52 }
53
54 # Collect symbols exported from all object files.
55 if test -z "$no_objects"; then
56     for f in "$@"; do
57         if IsBitcode "$f"; then
58             "$NM" "$f" --defined-only --extern-only --just-symbol-name 2>/dev/null
59         else
60             dump -tov -X 32_64 "$f" |
61             awk '
62                 BEGIN {
63                     V["EXPORTED"]=" export"
64                     V["PROTECTED"]=" protected"
65                 }
66                 /^\[[0-9]+\]\tm +[^ ]+ +\.(text|data|bss) +[^ ]+ +(extern|weak) +(EXPORTED|PROTECTED| ) / {
67                     if (!match($NF,/^(\.|__sinit|__sterm|__[0-9]+__)/)) {
68                         print $NF V[$(NF-1)]
69                     }
70                 }
71             '
72         fi
73     done >> "$out_tmp"
74 fi
75
76 # Generate the export/import file.
77 {
78     if test -n "$lib"; then
79         echo "#! $lib"
80     fi
81     sort -u "$out_tmp"
82 } > "$out"