summaryrefslogtreecommitdiff
path: root/scripts/genmake
blob: 79a89175aae3e39711d0546b32bfe703bfe2ee12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/sh -e

# ./scripts/deplist system | ./scripts/genmake [0|1]

##
# This script reads from stdin and generates
# a Makefile in the repository's root.
#
# The first argument to this script is a value {0,1}
# indicating whether to generate real rules or fake
# (simulation) rules. Fake rules might be 'sleep 1'
# or can be user-defined.
#
# NOTE: out-of-tree builds are not supported.
#

HERE="$(dirname $(readlink -f ${0}))";
DEST="${HERE}"/../Makefile;
TEMP="$(mktemp)";

##
# Build recipes.
#
# FIXME: is $(@D) POSIX or $(%/built=%) required?
#
rule_real=$(cat <<"EOF"
	@cd $(@D) && abuild -r
EOF
);
rule_fake=$(cat <<"EOF"
	@echo $(@D)
	@sleep 1
EOF
);

##
# Read deplist from stdin, synthesize Makefile targets.
#
awk '{ $(NF+1)=$1; print $0 }' `# duplicate first column to last` \
    | sed > "${TEMP}" -E `# use extended regex, write to temp file ` \
    -e 's/ /: /' `# append colon to first column` \
    -e '/: /s@( |$)@/built @2g' `# append '/built' to all dependencies` \
    -e 's/ $//g' `# trim trailing spaces` \
    ;

##
# Internal; convenience only.
#
list=$(grep : "${TEMP}" \
    | cut -d: -f1 \
    | xargs \
);

##
# Create (or truncate) the output Makefile.
#
# Default target builds everything.
#
printf "all: %s\n" "${list}" > "${DEST}";

##
# Append generic target build recipe.
#
rule=;
case ${1} in
    0) rule="${rule_real}"; ;;
    1) rule="${rule_fake}"; ;;
    *) printf "E: Invalid mode '%s'\n" "${1}"; exit 1; ;;
esac
cat >> "${DEST}" <<EOF
%/built: %/APKBUILD
${rule}
	@touch \$@
clean:
	@find \$(CURDIR) -type f -name built -print -delete
distclean:
	@rm -fv \
		\$(CURDIR)/Makefile \
		\$(CURDIR)/scripts/.index \
		\$(CURDIR)/scripts/sgrep \
		\$(CURDIR)/scripts/tsort \
		;
EOF

##
# Append target rules to output Makefile.
#
cat >> "${DEST}" "${TEMP}";

##
# Clean up.
#
rm "${TEMP}";