Skeleton repo.
[profile/ivi/murphy.git] / build-aux / gen-linker-script
1 #!/bin/bash
2
3 ###############
4 # Generate a linker script for GNU ld.
5
6 #
7 #
8
9
10
11 error () {
12     echo "error: $*" 1>&2
13 }
14
15 info () {
16     echo "$*" 1>&2
17 }
18
19 usage () {
20     info "usage: $0 [-p <pattern>] [-I <ignore-list>] -o <output> <inputs>"
21     exit ${1:-1}
22 }
23
24 emit () {
25     echo "$*" >> $OUTPUT
26 }
27
28
29 # set up defaults
30 PATTERN="^mrp_"                       # export everything prefixed with mrp_
31 IGNORE="MRP_PRINTF_LIKE"              # ignore these symbols/macros
32 IT=","                                # ignore-list is comma-separated
33 SOURCES=""                            # no default input, must be specified
34 OUTPUT=""                             # no default output, must be specified
35
36 # parse command line
37 while [ -n "${1#-}" ]; do
38     case $1 in
39         -o)
40             if [ -z "$OUTPUT" ]; then
41                 shift
42                 OUTPUT="$1"
43             else
44                 error "Multiple output files requested."
45                 usage
46             fi
47             ;;
48         -p)
49             if [ -z "$PATTERN" ]; then
50                 shift
51                 PATTERN="$1"
52             else
53                 error "Multiple patterns given ($PATTERN, $1)."
54                 usage
55             fi
56             ;;
57         -I)
58             shift
59             IGNORE="$IGNORE$IT$1"
60             IT=","
61             ;;
62         -h)
63             usage 0
64             ;;
65         -*)
66             error "Unknown option '$1'."
67             usage
68             ;;
69         *)
70             SOURCES="$SOURCES $1"
71             ;;
72     esac
73     shift
74 done
75
76 # check that we've got everything mandatory
77 if [ -z "$OUTPUT" ]; then
78     error "No output file specified (use the -o option)."
79     usage
80 fi
81
82 if [ -z "$SOURCES" ]; then
83     error "No input files specified."
84     usage
85 fi
86
87 if [ -z "$PATTERN" ]; then
88     error "Invalid (empty) pattern."
89     usage
90 fi
91
92 if [ -n "$IGNORE" ]; then
93     ignore_opts="-I $IGNORE"
94 else
95     ignore_opts=""
96 fi
97
98 # generate the output
99 info "Generating linker script $OUTPUT..."
100 rm -f $OUTPUT
101 touch $OUTPUT
102
103 emit "{"
104 emit "    global:"
105 ctags $ignore_opts -f - --c-kinds=p $SOURCES | \
106     awk "/$PATTERN/ { print \$1; }" | \
107         sort | \
108             while read sym; do
109                 emit "        $sym;"
110             done
111
112 emit "    local:"
113 emit "        *;"
114 emit "};"