2 ###########################################################################
4 # Window Maker window manager
6 # Copyright (c) 2014 Christophe CURIS
7 # Copyright (c) 2014 Window Maker Team
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with this program; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 ###########################################################################
25 # generate-mapfile-from-header.sh:
26 # from a list of C header files, extract the name of the functions and
27 # global variables that are considered public for a library, and generate
28 # a 'version script' for ld with that list, so that all other symbols
29 # will be considered local.
31 # The goal is that the symbol that are not part of the public API should
32 # not be visible to the user because it can be a source of problems (from
33 # name clash to evolutivity issues).
35 ###########################################################################
37 # Please note that this script is writen in sh+awk on purpose: this script
38 # is gonna be run on the machine of the person who is trying to compile
39 # WindowMaker, and as such we cannot be sure to find any scripting language
40 # in a known version and that works (python/ruby/tcl/perl/php/you-name-it).
42 # So for portability, we stick to the same sh+awk constraint as Autotools
43 # to limit the problem, see for example:
44 # http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Portable-Shell.html
46 ###########################################################################
48 # Report an error on stderr and exit with status 1 to tell make could not work
54 # print help and exit with success status
56 echo "$0: generate a script for ld to keep only the symbols from C header"
57 echo "Usage: $0 [options...] input_file[s]"
58 echo "valid options are:"
59 echo " -l : add symbol's definition line number for debug"
60 echo " -n name : name to use for the library"
61 echo " -v version : set the library version for ld"
65 # Extract command line arguments
66 while [ $# -gt 0 ]; do
69 debug_info
=' /* " symbol_type " at " FILENAME ":" NR " */'
74 echo "$1" |
grep '^[A-Za-z][A-Za-z_0-9]*$' > /dev
/null || arg_error
"invalid name \"$1\" for a library"
80 # Version may be 'x:y:z', we keep only 'x'
81 version
="`echo "$1" | sed -e 's,:.*$,,' `"
82 # the version should only be a number
83 echo "$version" |
grep '^[1-9][0-9]*$' > /dev
/null || \
84 arg_error
"version \"$1\" is not valid"
87 -h|
-help|
--help) print_help
;;
88 -*) arg_error
"unknow option '$1'" ;;
91 [ -r "$1" ] || arg_error
"source file \"$1\" is not readable"
92 input_files
="$input_files $1"
98 # Check consistency of command-line
99 [ "x$input_files" = "x" ] && arg_error
"no source file given"
102 if [ -z "$libname" ]; then
103 # build the library name from the first header name, remove path stuff, extension and invalid characters
104 libname
="`echo "$input_files" | sed -e 's,^ ,,;s, .*$,,;s,^.*/\([^/]*\)$,\1,;s,\.[^.]*$,,;s,[^A-Za-z_0-9],_,g;s,^_*,,' `"
107 # Parse the input file and find the function declarations; extract the names
108 # and place them in the 'global' section so that ld will keep the symbols;
109 # generate the rest of the script so that other symbols will not be kept.
112 print "/* Generated version-script for ld */";
114 print "'"$libname$version"'";
120 # Ignore macro definition because their content could be mis-interpreted
122 if (getline == 0) { break; }
128 # Remove comments to avoid mis-detection
129 pos = index($0, "/*");
130 comment = substr($0, pos + 2);
131 $0 = substr($0, 1, pos - 1);
133 pos = index(comment, "*/");
134 if (pos > 0) { break; }
137 $0 = $0 substr(comment, pos + 2);
143 # Remove everything from the ";"
144 sub(/;.*$/, "", line);
146 # Check if it is a global variable
147 nb = split(line, words, /[\t *]/);
150 for (i = 1; i < nb; i++) {
151 # If the word looks valid, do not abort
152 if (words[i] ~ /^[A-Za-z_0-9]*$/) { continue; }
154 # Treat the case were the variable is an array, and there was space(s) after the name
155 if (words[i] ~ /^\[/) { nb = i - 1; break; }
157 # If we are here, the word is not valid; we stop processing the line here but we do
158 # not use "next" so the line may match function prototype handler below
164 # Remove array size, if any
165 sub(/\[.*$/, "", words[nb]);
167 if (words[nb] ~ /^[A-Za-z][A-Za-z_0-9]*$/) {
168 symbol_type = "variable";
169 print " " words[nb] ";'"$debug_info"'";
176 # Skip type definition because they could be mis-interpreted when it
177 # defines a prototype for a callback function
180 # Count the number of brace pairs to skip the content of the definition
181 nb = split($0, dummy_array, /\{/);
182 nb_braces = nb_braces + nb;
184 nb = split($0, dummy_array, /\}/);
185 nb_braces = nb_braces - nb;
187 # Stop when we have out count of pair of braces and there is the final ";"
188 if ((nb_braces == 0) && (dummy_array[nb] ~ /;/)) { break; }
197 /[\t ]\**[A-Z_a-z][A-Z_a-z0-9]*[\t ]*\(/ {
198 # Get everything until the end of the definition, to avoid mis-interpreting
199 # arguments and attributes on next lines
201 pos = index($0, ";");
202 if (pos != 0) { break; }
208 # Remove the argument list
209 sub(/[ \t]*\(.*$/, "");
211 # Trim possible indentation at beginning of line
214 # If it does not start by a keyword, it is probably not a valid function
215 if (!/^[A-Z_a-z]/) { next; }
217 nb = split($0, words, /[\t *]/);
218 for (i = 1; i < nb; i++) {
219 # Do not consider "static" because it is used for functions to be inlined
220 if (words[i] == "static") { next; }
222 # Allow empty keyword, that were just "*"
223 if (words[i] == "") { continue; }
225 # If it does not match, it is unlikely to be a function
226 if (words[i] !~ /^[A-Z_a-z][A-Z_a-z0-9]*$/) { next; }
229 # If we arrived so far, everything looks valid and the last argument of
230 # the array contains the name of the function
231 symbol_type = "function";
232 print " " words[nb] ";'"$debug_info"'";