build-sys: no need to install native-messages.h.
[profile/ivi/speech-recognition.git] / build-aux / gen-linkedin-loader
1 #!/bin/bash
2
3 ######################################################################
4 # Generate helper code for making builtin DSO plugins available.
5 #
6 # Sometimes it is necessary to split a plugin to several source
7 # files. Usually this is the case when the functionality provided
8 # by the plugin is complex enough to require subdividing the plugin
9 # into internal modules for the best maintainability.
10 #
11 # In these cases we cannot avoid making several functions and
12 # variables globally available within the plugin so we cannot just
13 # limit the scope of visibility by making them static. Still, we do
14 # want to avoid conflicts between these symbols of different plugins.
15 # To accomplish this we compile and link such plugins into shared
16 # libraries (with our normal symbol visibility conventions) and then
17 # link the murphy daemon against them.
18 #
19 # Since none of the code linked into murphy proper references
20 # any of the symbols in any of these plugins without any further
21 # special arrangements these plugins wouldn't be demand-loaded
22 # and consequently would be unavailable when the daemon starts up.
23 #
24 # To overcome this every such plugin gets automatically augmented
25 # by a plugin-specifig globally visible symbol and the murphy daemon
26 # gets augmented by a symbol that references these plugin-specific
27 # symbols. This forces the dynamic loader to load the plugins and
28 # the normal builtin-plugin autoregistration mechanism takes care of
29 # the rest.
30 #
31 # A bit too spaceship-ish, I admit... but nevertheless necessary
32 # unless we want to give up the idea of builtin/linkedin plugins...
33 #
34
35
36 error () {
37     echo "error: $*" 1>&2
38 }
39
40 info () {
41     echo "$*" 1>&2
42 }
43
44 warning () {
45     echo "warning: $*" 1>&2
46 }
47
48 usage () {
49     info "usage: $0 -p <plugin> -o <output-file>, or"
50     info "usage: $0 -o <output-file> -d <plugin-list>"
51     exit ${1:-1}
52 }
53
54 emit () {
55     echo "$*" >> $OUTPUT
56 }
57
58 emit_plugin_loader () {
59     case $OUTPUT in
60         *.c) emit "int mrp_linkedin_plugin_${1//-/_}_symbol = 0;"
61              ;;
62         *.h) emit "extern int mrp_linkedin_plugin_${1//-/_}_symbol;"
63              ;;
64     esac
65 }
66
67 emit_murphy_loader () {
68     local _p
69
70     for _p in $*; do
71         emit "#include \"linkedin-$_p-loader.h\""
72     done
73     emit ""
74     emit "int mrp_load_linkedin_plugins(void)"
75     emit "{"
76     emit "    return \\"
77     for _p in $*; do
78         emit "        mrp_linkedin_plugin_${_p//-/_}_symbol + \\"
79     done
80     emit "        0;"
81     emit "}"
82 }
83
84
85 OUTPUT=""
86 PLUGIN=""
87 PLUGIN_LIST=""
88 daemon=no
89
90 #echo "*** $0 $* ***"
91
92 # parse command line
93 while [ -n "${1#-}" ]; do
94     case $1 in
95         -o)
96             if [ -z "$OUTPUT" ]; then
97                 shift
98                 OUTPUT="$1"
99             else
100                 error "Multiple output files requested."
101                 usage
102             fi
103             ;;
104         -p)
105             if [ -z "$PLUGIN" -a "$daemon" = "no" ]; then
106                 shift
107                 PLUGIN="$1"
108             else
109                 if [ -n "$PLUGIN" ]; then
110                     error "Multiple builtin plugins specified."
111                 else
112                     error "Both builtin plugin and plugin list specified."
113                 fi
114                 usage
115             fi
116             ;;
117
118         -h)
119             usage 0
120             ;;
121         -q)
122             QUIET="yes"
123             ;;
124         -d)
125             daemon=yes
126             ;;
127
128         -*)
129             error "Unknown option '$1'."
130             usage
131             ;;
132
133         *)
134             if [ "$daemon" = "yes" ]; then
135                 PLUGIN_LIST="$PLUGIN_LIST $1"
136             else
137                 error "Unexpected argument '$1'."
138                 usage
139             fi
140             ;;
141     esac
142     shift
143 done
144
145 # check that we've got everything mandatory
146 if [ -z "$OUTPUT" ]; then
147     error "No output file specified (use the -o option)."
148     usage
149 fi
150
151 if [ -z "$PLUGIN" -a "$daemon" = "no" ]; then
152     error "Neither builtin plugin nor plugin list is specified."
153     usage
154 fi
155
156 # generate the output
157 rm -f $OUTPUT
158 touch $OUTPUT
159 if [ "$daemon" = "no" ]; then
160     emit_plugin_loader $PLUGIN
161 else
162     emit_murphy_loader $PLUGIN_LIST
163 fi