Update. o/

- “help” virtual target added to Makefiles.
	  That target also prints the current Makefile configuration,
	  so that users can make sure they’re exporting the right
	  variables.
	- Some fixes and minor new variables to help making tests or
	  re-use automated rules, although those could be done in a
	  completely clean way, and it’s presently not being done…
	- Better dependencies tracking with C-like binaries.
	- Maybe something else. Those were old modifications that I just
	  pushed.
master
Luka Vandervelden 2015-04-05 11:00:54 +02:00
parent 7be1efdd03
commit dd63cd87e1
4 changed files with 204 additions and 17 deletions

131
Makefile Normal file
View File

@ -0,0 +1,131 @@
PACKAGE = 'build_zsh'
VERSION = '0.2'
PREFIX := /usr/local
BINDIR := $(PREFIX)/bin
LIBDIR := $(PREFIX)/lib
SHAREDIR := $(PREFIX)/share
INCLUDEDIR := $(PREFIX)/include
CC := cc
CFLAGS :=
LDFLAGS :=
Q := @
all: build.zsh
build.zsh:
build.zsh.install: build.zsh
@echo ' [IN] $(BINDIR)/build.zsh'
$(Q)mkdir -p '$(DESTDIR)$(BINDIR)/build.zsh'
$(Q)install -m0755 build.zsh $(DESTDIR)$(BINDIR)/build.zsh
build.zsh.clean:
build.zsh.uninstall:
@echo ' [RM] $(BINDIR)/build.zsh'
$(Q)rm -f '$(DESTDIR)$(BINDIR)/build.zsh'
$(DESTDIR)$(PREFIX):
@echo ' [DIR] $(PREFIX)'
$(Q)mkdir -p $(DESTDIR)$(PREFIX)
$(DESTDIR)$(BINDIR):
@echo ' [DIR] $(BINDIR)'
$(Q)mkdir -p $(DESTDIR)$(BINDIR)
$(DESTDIR)$(LIBDIR):
@echo ' [DIR] $(LIBDIR)'
$(Q)mkdir -p $(DESTDIR)$(LIBDIR)
$(DESTDIR)$(SHAREDIR):
@echo ' [DIR] $(SHAREDIR)'
$(Q)mkdir -p $(DESTDIR)$(SHAREDIR)
$(DESTDIR)$(INCLUDEDIR):
@echo ' [DIR] $(INCLUDEDIR)'
$(Q)mkdir -p $(DESTDIR)$(INCLUDEDIR)
install: subdirs.install build.zsh.install
@:
subdirs.install:
uninstall: subdirs.uninstall build.zsh.uninstall
@:
subdirs.uninstall:
test: all subdirs subdirs.test
@:
subdirs.test:
clean: build.zsh.clean
distclean: clean
dist: dist-gz dist-xz dist-bz2
$(Q)rm -- $(PACKAGE)-$(VERSION)
distdir:
$(Q)rm -rf -- $(PACKAGE)-$(VERSION)
$(Q)ln -s -- . $(PACKAGE)-$(VERSION)
dist-gz: $(PACKAGE)-$(VERSION).tar.gz
$(PACKAGE)-$(VERSION).tar.gz: distdir
@echo ' [TAR] $(PACKAGE)-$(VERSION).tar.gz'
$(Q)tar czf $(PACKAGE)-$(VERSION).tar.gz \
$(PACKAGE)-$(VERSION)/build/binary.zsh \
$(PACKAGE)-$(VERSION)/build/script.zsh \
$(PACKAGE)-$(VERSION)/project.zsh \
$(PACKAGE)-$(VERSION)/Makefile
dist-xz: $(PACKAGE)-$(VERSION).tar.xz
$(PACKAGE)-$(VERSION).tar.xz: distdir
@echo ' [TAR] $(PACKAGE)-$(VERSION).tar.xz'
$(Q)tar cJf $(PACKAGE)-$(VERSION).tar.xz \
$(PACKAGE)-$(VERSION)/build/binary.zsh \
$(PACKAGE)-$(VERSION)/build/script.zsh \
$(PACKAGE)-$(VERSION)/project.zsh \
$(PACKAGE)-$(VERSION)/Makefile
dist-bz2: $(PACKAGE)-$(VERSION).tar.bz2
$(PACKAGE)-$(VERSION).tar.bz2: distdir
@echo ' [TAR] $(PACKAGE)-$(VERSION).tar.bz2'
$(Q)tar cjf $(PACKAGE)-$(VERSION).tar.bz2 \
$(PACKAGE)-$(VERSION)/build/binary.zsh \
$(PACKAGE)-$(VERSION)/build/script.zsh \
$(PACKAGE)-$(VERSION)/project.zsh \
$(PACKAGE)-$(VERSION)/Makefile
help:
@echo ' :: build_zsh-0.2'
@echo ''
@echo 'Generic targets:'
@echo ' - help Prints this help message.'
@echo ' - all Builds all targets.'
@echo ' - dist Creates tarballs of the files of the project.'
@echo ' - install Installs the project.'
@echo ' - clean Removes compiled files.'
@echo ' - uninstall Deinstalls the project.'
@echo ''
@echo 'CLI-modifiable variables:'
@echo ' - CC ${CC}'
@echo ' - CFLAGS ${CFLAGS}'
@echo ' - LDFLAGS ${LDFLAGS}'
@echo ' - DESTDIR ${DESTDIR}'
@echo ' - PREFIX ${PREFIX}'
@echo ' - BINDIR ${BINDIR}'
@echo ' - LIBDIR ${LIBDIR}'
@echo ' - SHAREDIR ${SHAREDIR}'
@echo ' - INCLUDEDIR ${INCLUDEDIR}'
@echo ''
@echo 'Project targets: '
@echo ' - build.zsh script'
@echo ''
@echo 'Makefile options:'
@echo ' - gnu: true'
@echo ' - colors: true'
@echo ''
@echo 'Rebuild the Makefile with:'
@echo ' zsh ./build.zsh -c -g'
.PHONY: all subdirs clean distclean dist install uninstall help

View File

@ -5,6 +5,8 @@
# (stuff *will* break if you add non-C things as targets)
# - Clean some more (or a lot). I mean, this script could even be reused if
# it were cleaner, more readable and somewhat more documented.
# - Using subdirs creates trouble. Really. Dont do it unless its for
# completely separate, independent sub-projects.
#
# WARNINGS and LIMITATIONS:
# - Using a relative path in DESTDIR= *will* fail.
@ -108,12 +110,6 @@ function get_distfiles {
fi
done
for target in ${targets[@]}; do
if exists "${type[$target]}.distfiles"; then
"${type[$target]}.distfiles"
fi
done
for dir in ${subdirs[@]}; do
(
unset dist sources depends subdirs
@ -221,9 +217,11 @@ function main {
write "Q := @"
write
write -n "all: "
(( ${#targets[@]} > 0 )) && write -n " ${targets[@]}"
(( ${#subdirs[@]} > 0 )) && write -n " subdirs"
if [[ -z "${all}" ]] || (( ${#all[@]} == 0 )); then
all=(${targets[@]} $((( ${#subdirs[@]} > 0 )) && echo subdirs))
fi
write -n "all: ${all[@]}"
if $gnu; then
write "\n"
@ -328,6 +326,9 @@ function main {
write -n " ${target}.test"
fi
done
for target in ${tests[@]}; do
write -n " ${target}"
done
write "\n\t@:\n"
write "subdirs.test:"
@ -393,6 +394,52 @@ function main {
done
fi
write "help:"
if [[ -n "$package" ]]; then
write " @echo '${fg_bold[white]} :: $package-$version${reset_color}'"
write " @echo ''"
fi
write " @echo '${fg_bold[white]}Generic targets:${reset_color}'"
typeset -la help
help=(
help "Prints this help message."
all "Builds all targets."
dist "Creates tarballs of the files of the project."
install "Installs the project."
clean "Removes compiled files."
uninstall "Deinstalls the project."
)
for rule message in ${help[@]}; do
printf " @echo '${reset_color} - ${fg_bold[green]}%-14s${fg[white]}$message${reset_color}'\n" \
"$rule" >> $Makefile
done
write " @echo ''"
write " @echo '${fg_bold[white]}CLI-modifiable variables:${reset_color}'"
for VAR in CC CFLAGS LDFLAGS DESTDIR; do
printf " @echo ' - ${fg_bold[blue]}%-14s${fg[white]}\${$VAR}${reset_color}'\n" "$VAR" >> $Makefile
done
for VAR _ in ${prefixes}; do
printf " @echo ' - ${fg_bold[blue]}%-14s${fg[white]}\${$VAR}${reset_color}'\n" "$VAR" >> $Makefile
done
write " @echo ''"
write " @echo '${fg_bold[white]}Project targets: ${reset_color}'"
for T in ${targets[@]}; do
printf " @echo ' - ${fg_bold[yellow]}%-14s${fg[white]}${type[$T]}${reset_color}'\n" "$T" >> $Makefile
done
write " @echo ''"
write " @echo '${fg_bold[white]}Makefile options:${reset_color}'"
printf " @echo ' - %-14s$gnu'\n" "gnu:" >> $Makefile
printf " @echo ' - %-14s$colors'\n" "colors:" >> $Makefile
write " @echo ''"
write " @echo '${fg_bold[white]}Rebuild the Makefile with:${reset_color}'"
write " @echo ' zsh ./build.zsh$($colors && echo -n " -c")$($gnu && echo -n " -g")'"
for i in ${subdirs[@]}; do
(
cd $i
@ -403,7 +450,7 @@ function main {
)
done
write ".PHONY: all subdirs clean distclean dist install uninstall"
write ".PHONY: all subdirs clean distclean dist install uninstall help"
write
}
@ -411,10 +458,12 @@ export Makefile=Makefile
export MAKE='$(MAKE)'
export Q='$(Q)'
export gnu=false
export colors=false
while (($# > 0)); do
case "$1" in
-c|--colors)
export colors=true
autoload -U colors
colors
;;

View File

@ -6,21 +6,27 @@ function binary.build {
done
write " ${depends[$target]}"
write "\t@echo '$(LD ${target})'"
write "\t$Q\$(CC) -o ${target} \$(LDFLAGS) ${ldflags[$target]} ${src[@]//.c/.o}"
write -n "\t$Q\$(CC) -o ${target} \$(LDFLAGS)"
write -n " ${src[@]//.c/.o}"
write " ${ldflags[$target]}"
write
for i in ${src[@]}; do
local dirname="$(dirname "$i")"
write -n "${i%.c}.o: ${i}"
for h in *.h; do
if grep -q "#include \"$h\"" $i; then
write -n " $h"
fi
sed '/^#include "/!d;s/^#include "//;s/"$//' $i | while read h; do
h="$dirname/$h"
write -n " $h"
done
write
write "\t@echo '$(CC ${i%.c}.o)'"
write "\t$Q\$(CC) \$(CFLAGS) ${cflags[$target]} -c ${i}"
write -n "\t$Q\$(CC) \$(CFLAGS) ${cflags[$target]} -c ${i} "
write -n " ${cflags[$target]}"
write " -o ${i%.c}.o"
write
done
}
@ -29,6 +35,7 @@ function binary.install {
local install="${install[$target]:-\$(BINDIR)}"
write "${target}.install: ${target}"
write "\t@echo '$(IN "${install}/${target}")'"
write "\t${Q}mkdir -p '\$(DESTDIR)${install}/${target}'"
write "\t${Q}install -m0755 ${target} \$(DESTDIR)${install}/${target}"
write
}

View File

@ -1,6 +1,6 @@
package=build_zsh
version=0.1
version=0.2
targets=(build.zsh)
type[build.zsh]=script