make/update-rules.sh

changeset 1158
96c202d45fbb
equal deleted inserted replaced
1157:0a9f4d689c12 1158:96c202d45fbb
1 #!/bin/sh
2
3 # the directory where the executed Makefile is
4 dir="$1"
5
6 # extra flags to add to the compile recipe (if you want to use extra
7 # flags for the dependency resolution, set CFLAGS environment variable)
8 extra_flags="$2"
9
10 # additional source prefix in case the recipes are for a included Makefile
11 src_prefix="$3"
12
13 # the target directory for the object files (default: ../build/$dir/)
14 target="$4"
15
16 # the object file extension to use (default: $(OBJ_EXT) from the config.mk)
17 obj_ext="$5"
18
19
20 if [ -z "$dir" ]; then
21 echo "Usage: $0 <src_dir>"
22 exit 1
23 fi
24
25 if [ -z "$target" ]; then
26 target="../build/$dir/"
27 fi
28
29 if [ -z "$obj_ext" ]; then
30 obj_ext='$(OBJ_EXT)'
31 fi
32
33 if [ -d "$dir" ]; then
34 :
35 else
36 echo "'$dir' is not a directory"
37 exit 1
38 fi
39
40 if [ -z "$CC" ]; then
41 for cc in gcc clang ; do
42 if command -v "$cc" > /dev/null ; then
43 CC="$cc"
44 break
45 fi
46 done
47 fi
48
49 if [ -z "$CC" ]; then
50 echo "No suitable compiler found to generate make rules"
51 exit 1
52 fi
53
54 if command -v sed > /dev/null ; then
55 :
56 else
57 echo "You need the 'sed' program for this script to work."
58 exit 1
59 fi
60
61 # cleanup include paths to only depend on non-system headers
62 cflags=`echo $CFLAGS | sed 's/-I\//-isystem \//g'`
63
64 # switch into the directory of the executed Makefile and generate the recipes
65 cd "$dir"
66
67 mv ${src_prefix}Makefile ${src_prefix}Makefile.old
68 sed '/FORCE:/q' ${src_prefix}Makefile.old > ${src_prefix}Makefile
69 echo >> ${src_prefix}Makefile
70 for file in `ls ${src_prefix}*.c` ; do
71 "$CC" -MT "$target$(basename $file .c)$obj_ext" -MM $cflags "$file"
72 printf '\t$(CC) -o $@ $(CFLAGS) %s -c $<\n\n' "$extra_flags"
73 done >> ${src_prefix}Makefile
74 rm ${src_prefix}Makefile.old

mercurial