1#!/bin/sh 2# 3# Generate generic POSIX compliant Makefiles. 4# 5# This means that there's a lot of unnecessary text (when using BSD or GNU 6# make, as I'm sure there are in other variants), and a lack of modularity, 7# but as long as you follow the criterion set in locate-test, then the 8# end-result for modifying and/or adding tests can be achieved by merely 9# rerunning this script. 10# 11# This script will remain around until (hopefully someday) POSIX make 12# becomes less braindead. 13# 14# See COPYING for more details. 15# 16# Ngie Cooper, June 2010 17# 18 19readonly buildonly_compiler_args="-c" 20 21generate_locate_test_makefile() { 22 23 local maketype=$1; shift 24 25 echo "Generating $maketype Makefiles" 26 27 locate-test --$maketype | sed -e 's,^./,,g' | sort > make-gen.$maketype 28 29 generate_makefiles make-gen.$maketype $* 30 31 rm -f make-gen.$maketype 32} 33 34generate_makefile() { 35 36 local link_libs= 37 local make_rule_prereq_cache= 38 local make_copy_prereq_cache= 39 local prereq_cache= 40 local tests= 41 local targets= 42 43 local makefile=$1 44 local prereq_dir=$2 45 local compiler_args=$3 46 shift 3 47 48 prereq_cache=$* 49 50 test_prefix=$(basename "$prereq_dir") 51 52 # special case for speculative testcases 53 if [ "$test_prefix" = "speculative" ]; then 54 test_prefix=$(basename $(echo "$prereq_dir" | sed s/speculative//)) 55 test_prefix="${test_prefix}_speculative" 56 fi 57 58 # Add all source files to $make_target_prereq_cache. 59 for prereq in $prereq_cache; do 60 # Stuff that needs to be tested. 61 if echo "$prereq" | grep -Eq '\.(run-test|sh)'; then 62 if [ "$tests" != "" ]; then 63 tests="$tests " 64 fi 65 66 tests="$tests${test_prefix}_$prereq" 67 fi 68 69 # Stuff that needs to be compiled. 70 if echo "$prereq" | grep -Eq '\.(run-test|sh|test)'; then 71 if [ "$targets" != "" ]; then 72 targets="$targets " 73 fi 74 75 targets="$targets${test_prefix}_$prereq" 76 fi 77 78 # Cache for generating compile rules. 79 case "$prereq" in 80 *.sh) 81 # Note that the sh scripts are copied later in order to 82 # have the test_prefix as well 83 if [ "$make_copy_prereq_cache" != "" ]; then 84 make_copy_prereq_cache="$make_copy_prereq_cache " 85 fi 86 make_copy_prereq_cache="$make_copy_prereq_cache$prereq" 87 ;; 88 *) 89 if [ "$make_rule_prereq_cache" != "" ]; then 90 make_rule_prereq_cache="$make_rule_prereq_cache " 91 fi 92 make_rule_prereq_cache="$make_rule_prereq_cache$prereq" 93 ;; 94 esac 95 done 96 97 if [ ! -f "$makefile.1" ]; then 98 99 cat > "$makefile.1" <<EOF 100# 101# Automatically generated by `basename "$0"` -- DO NOT EDIT. 102# 103# Restrictions for `basename "$0"` apply to this file. See COPYING for 104# more details. 105# 106# $AUTHORDATE 107# 108 109# Path variables. 110CC := ${COMPILE_PATH} 111top_srcdir?= `echo "$prereq_dir" | awk '{ gsub(/[^\/]+/, "..", $0); print }'` 112subdir= $prereq_cache_dir 113srcdir= \$(top_srcdir)/\$(subdir) 114 115prefix?= $PREFIX 116exec_prefix?= \$(prefix) 117INSTALL_DIR= \$(DESTDIR)/\$(exec_prefix)/\$(subdir) 118LOGFILE?= logfile 119 120# Build variables 121CFLAGS+= -I\$(top_srcdir)/include 122CFLAGS+= --target=arm-liteos-ohos --sysroot=${SYSROOT_PATH} ${ARCH_CFLAGS} 123 124# XXX: for testfrmw.c -- needs to be moved into a library. 125CFLAGS+= -I\$(srcdir) 126 127EOF 128 129 if [ -f "$GLOBAL_BOILERPLATE" ]; then 130 cat >> "$makefile.1" <<EOF 131# Top-level make definitions 132`cat $GLOBAL_BOILERPLATE` 133EOF 134 fi 135 136 cat >> "$makefile.1" <<EOF 137# Submake make definitions. 138EOF 139 140 for var in CFLAGS LDFLAGS LDLIBS; do 141 if [ -f "${TOP_SRCDIR}/$var" ]; then 142 cat >> "$makefile.1" <<EOF 143${var}+= `cat "${prereq_cache_dir}/${var}" 2>/dev/null` 144EOF 145 fi 146 done 147 148 # Whitespace 149 echo "" >> "$makefile.1" 150 151 fi 152 153 if [ ! -z "${tests}" ]; then 154 cat >> "$makefile.2" <<EOF 155INSTALL_TARGETS+= ${tests} 156EOF 157 fi 158 cat >> "$makefile.2" <<EOF 159MAKE_TARGETS+= ${targets} 160 161ifeq (\$V,1) 162VERBOSE=1 163endif 164 165ifndef VERBOSE 166v=@ 167endif 168 169EOF 170 171 if [ ! -f "$makefile.3" ]; then 172 173 cat > "$makefile.3" <<EOF 174all: \$(MAKE_TARGETS) 175 @if [ -d speculative ]; then \$(MAKE) -C speculative all; fi 176 177clean: 178 rm -f \$(MAKE_TARGETS) logfile* run.sh *.core 179 @if [ -d speculative ]; then \$(MAKE) -C speculative clean; fi 180 181install: \$(INSTALL_DIR) run.sh 182 set -e; for file in \$(INSTALL_TARGETS) run.sh; do \\ 183 if [ -f "\$\$file" ] ; then \\ 184 install -m 00755 \$\$file \\ 185 \$(INSTALL_DIR)/\$\$file; \\ 186 fi; \\ 187 done 188 @if [ -d speculative ]; then \$(MAKE) -C speculative install; fi 189 190test: run.sh 191 \$(v)./run.sh 192 193\$(INSTALL_DIR): 194 mkdir -p \$@ 195 196EOF 197 198 fi 199 200 if ! grep -q '^run.sh' "$makefile.3"; then 201 cat >> "$makefile.3" <<EOF 202run.sh: 203 @echo '#!/bin/sh' > \$@ 204 @echo "\$(top_srcdir)/bin/run-tests.sh \$(subdir) \$(INSTALL_TARGETS)" >> \$@ 205 @chmod +x run.sh 206 207EOF 208 fi 209 210 # Only pass along libraries to link if `-c` was not specified in `$compiler_args`. 211 if [ "$compiler_args" = "$buildonly_compiler_args" ]; then 212 link_libs=false 213 else 214 link_libs=true 215 fi 216 217 # Produce _awesome_ target rules for everything that needs it. 218 for prereq in ${make_rule_prereq_cache}; do 219 220 test_name="$prereq" 221 if [ "$suffix" != "" ]; then 222 test_name=`echo "$test_name" | sed -e "s,$suffix,,"` 223 fi 224 225 c_file="$test_name.c" 226 bin_file="${test_prefix}_$prereq" 227 228 case "$suffix" in 229 .run-test) 230 grep -q 'main' "$prereq_dir/$c_file" || echo >&2 "$prereq_dir/$c_file should be test." 231 ;; 232 .test) 233 grep -q 'main' "$prereq_dir/$c_file" && echo >&2 "$prereq_dir/$c_file should be run-test." 234 ;; 235 esac 236 237 COMPILE_STR="\$(CC) $compiler_args \$(CFLAGS) \$(LDFLAGS) -o \$@ \$(srcdir)/$c_file" 238 if $link_libs; then 239 COMPILE_STR="$COMPILE_STR \$(LDLIBS)" 240 fi 241 242 cat >> "$makefile.3" <<EOF 243$bin_file: \$(srcdir)/$c_file 244 \$(v)if $COMPILE_STR > logfile.\$\$\$\$ 2>&1; then \\ 245 cat logfile.\$\$\$\$; \\ 246 echo "\$(subdir)/$test_name compile PASSED"; \\ 247 echo "\$(subdir)/$test_name compile PASSED" >> \$(LOGFILE); \\ 248 else \\ 249 cat logfile.\$\$\$\$; \\ 250 echo "\$(subdir)/$test_name compile FAILED; SKIPPING"; \\ 251 (echo "\$(subdir)/$test_name compile FAILED; SKIPPING"; cat logfile.\$\$\$\$) >> \$(LOGFILE); \\ 252 fi; \\ 253 rm -f logfile.\$\$\$\$ 254 255EOF 256 done 257 258 # Produce copy rules for .sh scripts. 259 for prereq in ${make_copy_prereq_cache}; do 260 src="$prereq" 261 dst="${test_prefix}_$prereq" 262 263 cat >> "$makefile.3" <<EOF 264$dst: \$(srcdir)/$src 265 @cp \$(srcdir)/$src \$@ 266 267EOF 268 done 269} 270 271generate_makefiles() { 272 273 local prereq_cache= 274 275 local make_gen_list=$1 276 local suffix=$2 277 local compiler_args="$3" 278 279 while read filename; do 280 281 prereq_dir=`dirname "$filename"` 282 283 # First run. 284 if [ "$prereq_cache_dir" = "" ] ; then 285 prereq_cache_dir="$prereq_dir" 286 elif [ "$prereq_cache_dir" != "$prereq_dir" ]; then 287 288 generate_makefile "$prereq_cache_dir/Makefile" "$prereq_cache_dir" "$compiler_args" $prereq_cache 289 290 # Prep for the next round.. 291 prereq_cache= 292 prereq_cache_dir="$prereq_dir" 293 294 fi 295 296 # Cache the entries to punt out all of the data at 297 # once for a single Makefile. 298 if [ "$prereq_cache" != "" ] ; then 299 prereq_cache="$prereq_cache " 300 fi 301 prereq_cache="$prereq_cache"`basename "$filename" | sed "s,.c\$,$suffix,g"` 302 303 done < $make_gen_list 304 305 # Dump the last Makefile data cached up. 306 generate_makefile "$prereq_cache_dir/Makefile" $prereq_cache_dir "$compiler_args" $prereq_cache 307 308} 309 310export PATH="$PATH:`dirname "$0"`" 311 312AUTHORDATE=`grep "Ngie Cooper" "$0" | head -n 1 | sed 's,# *,,'` 313PREFIX=$(print-prefix.sh) 314EXEC_PREFIX="${PREFIX}/bin" 315TOP_SRCDIR=${TOP_SRCDIR:=`dirname "$0"`/..} 316 317GLOBAL_BOILERPLATE="${TOP_SRCDIR}/.global_boilerplate" 318 319CONFIG_MK="../../include/mk/config-openposix.mk" 320COMPILE_PATH=$1 321BUILD_ROOT_PATH=$2 322SYSROOT_PATH=$3 323ARCH_CFLAGS=$4 324 325rm -f "$GLOBAL_BOILERPLATE" 326 327for var in CFLAGS LDFLAGS LDLIBS; do 328 if [ -f "$TOP_SRCDIR/$var" ]; then 329 cat >> "$GLOBAL_BOILERPLATE" <<EOF 330$var+= `cat "$TOP_SRCDIR/$var"` 331EOF 332 fi 333done 334 335if [ -f "$CONFIG_MK" ]; then 336 cat "$CONFIG_MK" >> "$GLOBAL_BOILERPLATE" 337fi 338 339# For the generic cases. 340generate_locate_test_makefile buildonly '.test' "$buildonly_compiler_args" 341generate_locate_test_makefile runnable '.run-test' 342generate_locate_test_makefile test-tools '' 343 344rm -f "$GLOBAL_BOILERPLATE" 345 346find . -name Makefile.1 -exec dirname {} \; | while read dir; do 347 if [ -f "$dir/Makefile.2" ]; then 348 cat $dir/Makefile.1 $dir/Makefile.2 $dir/Makefile.3 > $dir/Makefile 349 fi 350 rm $dir/Makefile.1 $dir/Makefile.2 $dir/Makefile.3 351done 352