1 | #!/bin/sh
|
---|
2 |
|
---|
3 | # There's a bunch of restriction. The script assumes that the structure definition begins with "typedef struct struct_name {",
|
---|
4 | # ends with "struct_name_t;", and that every word in between that ends with ";" is a struct member name with optional
|
---|
5 | # array subscript. Error handling is mostly omitted for simplicity, so any input that does not follow these rules will result
|
---|
6 | # in cryptic errors.
|
---|
7 |
|
---|
8 | input="$1"
|
---|
9 | output="$2"
|
---|
10 | toolsdir="$(readlink -f $(dirname "$0"))"
|
---|
11 | awkscript="$toolsdir/autogen2.awk"
|
---|
12 |
|
---|
13 | # Set default value for $CC.
|
---|
14 | if [ -z "${CC}" ]; then
|
---|
15 | CC=cc
|
---|
16 | fi
|
---|
17 |
|
---|
18 | # If $CC is clang, we need to make sure integrated assembler is not used.
|
---|
19 | if ( ${CC} --version | grep clang > /dev/null ) ; then
|
---|
20 | CFLAGS="${CFLAGS} -no-integrated-as"
|
---|
21 | fi
|
---|
22 |
|
---|
23 | # Tell the compiler to generate makefile dependencies.
|
---|
24 | CFLAGS="${CFLAGS} -MD -MP -MT $output -MF $output.d"
|
---|
25 |
|
---|
26 | # Generate defines
|
---|
27 | defs=`$awkscript -- $input || exit 1`
|
---|
28 |
|
---|
29 | # Generate C file.
|
---|
30 | cat > $output.c <<- EOF
|
---|
31 |
|
---|
32 | #include "`basename $input`"
|
---|
33 |
|
---|
34 | #define DEFINE_MEMBER(ls, us, lm, um) \
|
---|
35 | asm volatile ("/* #define "#us"_OFFSET_"#um" %0 */" :: "i" (__builtin_offsetof(struct ls, lm))); \
|
---|
36 | asm volatile ("/* #define "#us"_SIZE_"#um" %0 */" :: "i" (sizeof((struct ls){}.lm)));
|
---|
37 |
|
---|
38 | #define DEFINE_STRUCT(ls, us) \\
|
---|
39 | asm volatile ("/* #define "#us"_SIZE %0 */" :: "i" (sizeof(struct ls)));
|
---|
40 |
|
---|
41 | void autogen()
|
---|
42 | {
|
---|
43 | $defs
|
---|
44 | }
|
---|
45 |
|
---|
46 | EOF
|
---|
47 |
|
---|
48 | # Turn the C file into assembly.
|
---|
49 | ${CC} ${CFLAGS} -w -S -o $output.s $output.c || exit 1
|
---|
50 |
|
---|
51 | # Process the output.
|
---|
52 |
|
---|
53 | echo "/* Autogenerated file, do not modify. */" > $output
|
---|
54 | echo "#pragma once" >> $output
|
---|
55 | sed -n 's/^.* #define \([^ ]\+\) [^0-9]*\([0-9]\+\) .*/#define \1 \2/p' < $output.s > $output || exit 1 |
---|