p7utils/Makefile

244 lines
7.8 KiB
Makefile
Executable File

#!/usr/bin/make -f
#******************************************************************************#
# Include variables and message subsystem #
#******************************************************************************#
include Makefile.vars Makefile.msg
# Check if we're a git repository
ISGIT := $(shell test -e .git && echo y)
#******************************************************************************#
# General targets #
#******************************************************************************#
# Make it all
all: all-bins
# Mostly clean
mostlyclean: mostlyclean-bins
mclean: mostlyclean
# Clean it all
clean: clean-bins clean-doc
fclean: clean
# Clean it entirely
mrproper: clean
$(call rmsg,Removing configuration.)
$(call qcmd,$(RM) Makefile.cfg)
$(call qcmd,$(RM) $(NAME)-*)
# Remake it all
re: clean all
# Install it all
install: install-bins
# Uninstall it all
uninstall: uninstall-bins
# Reinstall it all
reinstall: uninstall install
# Make dist
dist: mrproper $(if $(ISGIT),reinit-gitmodules)
$(call bcmd,mkdir,$(NAME)-$(VERSION),\
$(MD) .dist)
$(call bcmd,cp,* $(NAME)-$(VERSION),\
cp -R * .dist)
$(call qcmd,\
mv .dist $(NAME)-$(VERSION))
$(call bcmd,tarball,$(NAME)-$(VERSION),\
tar czf $(NAME)-$(VERSION).tar.gz \
--exclude .git $(NAME)-$(VERSION))
$(call qcmd,$(RM) -r $(NAME)-$(VERSION))
.PHONY: all mostlyclean mclean clean fclean mrproper re
.PHONY: dist install uninstall reinstall
#******************************************************************************#
# Git submodules #
#******************************************************************************#
# Main rule.
reinit-gitmodules:
$(call qcmd,git submodule deinit -- \
$(shell grep path .gitmodules | sed 's/.*= //'))
$(call qcmd,git submodule update --init --recursive)
# Initialize one module
ifeq ($(ISGIT),y)
define check-gitmodule
-$(call qcmd,git submodule update --init --quiet $1)
endef
else
define check-gitmodule
@true
endef
endif
.PHONY: reinit-gitmodules
#******************************************************************************#
# Configuration (version) checking dependencies #
#******************************************************************************#
# Define the dependencies.
CHECKCFG := $(if $(shell test -f Makefile.cfg || echo y),check-config, \
$(if $(shell [ "$(VERSION)" = "$(CONFIG_VERSION)" ] || echo y), \
check-config-version))
# Define the rules.
check-config:
@printf "\033[1;31mNo configuration file found!\n"
@printf "You should configure before re-running this target.\033[0m\n"
@false
check-config-version:
@printf "\033[1;31mConfiguration version is incorrect!\n"
@printf "You should re-configure before re-running this target.\033[0m\n"
@false
.PHONY: check-config check-config-version
#******************************************************************************#
# Information getting from the Makefile variables #
#******************************************************************************#
# Get the project name.
getname:
@echo "$(NAME)"
# Get the project version.
getversion:
@echo "$(VERSION)"
# Get the project author.
getmaintainer:
@echo "$(MAINTAINER_NAME) <$(MAINTAINER_MAIL)>"
.PHONY: getname getmaintainer getversion
#******************************************************************************#
# Binaries-specific targets #
#******************************************************************************#
# Make the binaries.
all-bins: $(CHECKCFG) $(BINARIES:%=all-%)
# Make a binary object directory.
$(BINARIES:%=$(OBJDIR)/%):
$(call bcmd,mkdir,$@,$(MD) $@)
# Make an object out of a source file/directory.
define make-binaryobj-rule
ifeq ($(shell test -f $(SRCDIR)/$1/$2.c && echo y),y)
# - Out of a C source file
$(OBJDIR)/$1/$2.o: $(SRCDIR)/$1/$2.c | $(OBJDIR)/$1
$(call bcmd,cc,$$@,$(CC) -c -o $$@ $$< $(CFLAGS_$1))
else
# - Out of an update.exe project
$(SRCDIR)/$1/$2.exe/$2.exe.bin:| $(SRCDIR)/$1/$2.exe
$(call check-gitmodule,$(SRCDIR)/$1/$2.exe)
$(if $(shell test -f $(SRCDIR)/$1/$2.exe/configure \
&& test -x $(SRCDIR)/$1/$2.exe/configure && echo y), \
$(call qcmd,cd $(SRCDIR)/$1/$2.exe && ./configure 1>/dev/null))
$(call bcmd,make,$2.exe,$(MAKE) -C $(SRCDIR)/$1/$2.exe $2.exe.bin \
| sed -e 's/^/ /')
$(OBJDIR)/$1/$2.o: $(SRCDIR)/$1/$2.exe/$2.exe.bin | $(OBJDIR)/$1
$(call bcmd,ld -r,$$@,cd $(SRCDIR)/$1/$2.exe && \
$(LDR) -o ../../../$$@ -b binary $2.exe.bin)
endif
endef
$(foreach bin,$(BINARIES),\
$(foreach obj,$(SRC_$(bin)),\
$(eval $(call make-binaryobj-rule,$(bin),$(obj)))))
# Make a binary
define make-binary-rules
$1$(if $(FOR_WINDOWS),.exe): $(SRC_$1:%=$(OBJDIR)/$1/%.o) | $(OBJDIR)/$1
$(call bcmd,ld,$$@,$(LD) -o $$@ $$^ $(LDFLAGS_$1))
all-$1 all-$1.exe: $(CHECKCFG) $1$(if $(FOR_WINDOWS),.exe) all-doc-$1
clean-$1 clean-$1.exe:
$(call rmsg,Removing $1 and its objects directory.)
$(call qcmd,$(RM) -r $(OBJDIR)/$1)
$(call qcmd,$(RM) $1$(if $(FOR_WINDOWS),.exe))
re-$1 re-$1.exe: clean-$1 all-$1
endef
$(foreach bin,$(BINARIES),\
$(eval $(call make-binary-rules,$(bin))))
# Remove object files.
mostlyclean-bins:
$(call rmsg,Removing objects directory.)
$(call qcmd,$(RM) -r $(OBJDIR))
mclean-bins: mostlyclean-bins
# Clean and remove binaries.
clean-bins: mostlyclean-bins
$(call rmsg,Removing binaries.)
$(call qcmd,$(RM) $(BINARIES:%=%*))
# Remake binaries
re-bins: clean-bins all-bins
# Install a binary
define make-installbinary-rule
install-$1 install-$1.exe: $(CHECKCFG) \
all-$1 $(if $(INSTALL_MANPAGES),install-doc-$1)
$(call imsg,Installing $1$(if $(FOR_WINDOWS),.exe).)
$(call qcmd,$(INSTALL) -m 755 -d "$(IBINDIR)")
$(call qcmd,$(INSTALL) -m 755 -t "$(IBINDIR)" $1$(if $(FOR_WINDOWS),.exe))
uninstall-$1 uninstall-$1.exe: $(CHECKCFG) uninstall-doc-$1
$(call rmsg,Uninstalling $1)
$(call qcmd,$(RM) "$(IBINDIR)/$1"{,.exe})
endef
$(foreach bin,$(BINARIES),\
$(eval $(call make-installbinary-rule,$(bin))))
# Install binaries
install-bins: $(BINARIES:%=install-%)
# Uninstall binaries
uninstall-bins: $(BINARIES:%=uninstall-%)
.PHONY: all-bins mostlyclean-bins mclean-bins clean-bins re-bins
.PHONY: install-bins uninstall-bins
.PHONY: $(foreach b,$(BINARIES),all-$(b) all-$(b).exe \
clean-$(b) clean-$(b).exe re-$(b) re-$(b).exe \
install-$(b) install-$(b).exe uninstall-$(b) uninstall-$(b).exe)
#******************************************************************************#
# Documentation-related #
#******************************************************************************#
# Make manpages directories
$(MANDIR)/man1:
$(call bcmd,mkdir,$@,$(MD) $@)
# Make-A-Manpage
define make-manpage-rule
$(MANDIR)/man1/$1.%: $(DOCDIR)/$1.1.txt | $(MANDIR)/man1
$(call bcmd,a2x,$$<,$(A2X) -f manpage -D $$| $$< 2>/dev/null)
all-doc-$1: $(MANDIR)/man1/$1.1
endef
$(foreach bin,$(BINARIES),\
$(eval $(call make-manpage-rule,$(bin))))
# Remove all manpages
clean-doc:
$(call rmsg,Removing manpages directory.)
$(call qcmd,$(RM) -r $(MANDIR))
# Install a manpage.
define make-install-manpage-rule
install-doc-$1: $(MANDIR)/man1/$1.1
$(call imsg,Installing $1 manpage)
$(call qcmd,$(INSTALL) -m 755 -d "$(IMANDIR)/man1")
$(call qcmd,$(INSTALL) -m 644 -t "$(IMANDIR)/man1" \
$(MANDIR)/man1/$1.1)
$(call qcmd,$(GZIP) "$(IMANDIR)/man1/$1.1")
uninstall-doc-$1:
$(call rmsg,Removing $1 manpage)
$(call qcmd,$(RM) "$(IMANDIR)/man1/$1.1*")
endef
$(foreach bin,$(BINARIES), \
$(eval $(call make-install-manpage-rule,$(bin))))
.PHONY: $(foreach bin,$(BINARIES),all-doc-$(bin)) clean-doc
.PHONY: $(foreach bin,$(BINARIES),install-doc-$(bin) uninstall-doc-$(bin))
# End of file.