-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-fallback
More file actions
executable file
·75 lines (69 loc) · 3.52 KB
/
make-fallback
File metadata and controls
executable file
·75 lines (69 loc) · 3.52 KB
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
#!/bin/sh
#
# Copyright (C) 2017-2019 Vincent Sallaberry
# vlib <https://github.com/vsallaberry/vlib>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
############################################################################################
#
# Fallback make script in case 'VAR != cmd" or VAR ?= $(shell cmd) cause syntax error.
# - On recent gnu make, "!=' is understood.
# - On gnu make 3.81 '!=' is not understood but it does NOT cause syntax error.
# - On {open,free,net}bsd $(shell cmd) is not understood but does NOT cause syntax error.
# - On gnu make 3.82, '!=' causes syntax error, then on the moment only case where this
# script is needed.
#
# Use it as you would use 'make',
# eg: ./make-fallback clean, make-fallback CFLAGS=-std=gnu99, ...
# 'make-fallback -C <folder>' is supported but 'make-fallback -f <makefile>' is not.
# By default this wraps 'make', use '$ mymake=gmake ./make-fallback' to use an alternate one.
#
############################################################################################
mydir=$(dirname "$0"); curpwd="`pwd`"; cd "${mydir}"; mydir="`pwd`"; cd "${curpwd}"
mypath="${mydir}/"$(basename "$0")
test -z "${mymake}" && mymake="make"
mytest=uname
# By default the makefile is in current folder, but take care of make -C <folder>
# -C options are concatenated, eg: make -C / -C etc is equivalent to make -C /etc.
mymakefile=
changedir=
for arg in "$@"; do
[ -n "${changedir}" ] && mymakefile="${mymakefile}${arg}" && changedir=
[ "${arg}" = "-C" ] && changedir=yes
done
[ -z "${mymakefile}" ] && mymakefile="${curpwd}/Makefile" || mymakefile="${mymakefile}/Makefile"
# Backup makefile, and try to patch it, preserviing timestamps, make, then restore it.
if tmp=`mktemp /tmp/Makefile.XXXXXXXX` && mv "${mymakefile}" "${tmp}"; then
if ! printf "TMP\t!=${mytest}\nall:\n\t@echo \$(TMP)" | "${mymake}" -f - 2> /dev/null | grep -Eq "^$(${mytest})\$"; then
# make does not understand bsd variable command substitution 'var!=cmd'. remove them.
cat "${tmp}" | sed -e 's/^[a-zA-Z0-9_]*[[:space:]]*!=.*//' > "${mymakefile}"
elif ! printf "TMP\t= \$(shell uname)\nall:\n\t@echo \$(TMP)" | "${mymake}" -f - 2> /dev/null | grep -Eq "^$(${mytest})\$"; then
# make does not understand gnu variable command substitution 'var=$(shell cmd)'. remove them.
cat "${tmp}" | sed -e 's/^[a-zA-Z0-9_]*[[:space:]]*\?=[[:space:]]*\$(shell.*//' > "${mymakefile}"
else
cat "${tmp}" > "${mymakefile}"
fi
touch -r "${tmp}" "${mymakefile}"
# create empty .alldeps.d as some old bsd make do not see it when created by a makefile shell command.
deps=$(dirname "${mymakefile}")/.alldeps.d
[ -e "$deps" ] || printf -- '# alldeps.d\ninclude config.make\n' > "$deps"
"${mymake}" MAKE="${mypath}" "$@"; status=$?
mv "${tmp}" "${mymakefile}"
exit $status
else
echo "error: cannot write in /tmp or on ${mymakefile}."
exit 1
fi