repo-fixing: fixup #5.
[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 warning () {
20     echo "warning: $*" 1>&2
21 }
22
23 usage () {
24     info "usage: $0 [-p <pattern>] [-I <ignore-list>] -o <output> <inputs>"
25     exit ${1:-1}
26 }
27
28 emit () {
29     echo "$*" >> $OUTPUT
30 }
31
32
33 # set up defaults
34 PATTERN="^mrp_|^_mrp_"                # export everything prefixed with mrp_
35 IGNORE="MRP_PRINTF_LIKE,MRP_NULLTERM" # ignore these symbols/macros
36 IT=","                                # ignore-list is comma-separated
37 SOURCES=""                            # no default input, must be specified
38 OUTPUT=""                             # no default output, must be specified
39
40 # parse command line
41 while [ -n "${1#-}" ]; do
42     case $1 in
43         -o)
44             if [ -z "$OUTPUT" ]; then
45                 shift
46                 OUTPUT="$1"
47             else
48                 error "Multiple output files requested."
49                 usage
50             fi
51             ;;
52         -p)
53             shift;
54             PATTERN="$1"
55             ;;
56         -I)
57             shift
58             IGNORE="$IGNORE$IT$1"
59             IT=","
60             ;;
61         -h)
62             usage 0
63             ;;
64         -q)
65             QUIET="yes"
66             ;;
67         -*)
68             error "Unknown option '$1'."
69             usage
70             ;;
71         *)
72             SOURCES="$SOURCES $1"
73             ;;
74     esac
75     shift
76 done
77
78 # check that we've got everything mandatory
79 if [ -z "$OUTPUT" ]; then
80     error "No output file specified (use the -o option)."
81     usage
82 fi
83
84 if [ -z "$SOURCES" ]; then
85     warning "No input files, generating local-only linker script."
86     emit "{"
87     emit "    local:"
88     emit "        *;"
89     emit "};"
90     exit 0
91 fi
92
93 if [ -z "$PATTERN" ]; then
94     PATTERN="^mrp_"
95 fi
96
97 if [ -n "$IGNORE" ]; then
98     ignore_opts="-I $IGNORE"
99 else
100     ignore_opts=""
101 fi
102
103 # generate the output
104 [ -n "$QUIET" ] || info "Generating linker script $OUTPUT..."
105 rm -f $OUTPUT
106 touch $OUTPUT
107
108 emit "{"
109 emit "    global:"
110 ctags $ignore_opts -f - --c-kinds=px $SOURCES | \
111     awk "/$PATTERN/ { print \$1; }" | \
112         sort | \
113             while read sym; do
114                 emit "        $sym;"
115             done
116
117 emit "    local:"
118 emit "        *;"
119 emit "};"