#!/usr/bin/make -f
#******************************************************************************#
#                                                                              #
#   Makefile                                                                   #
#   | Project : librtc                                                         #
#                                                                              #
#   By: thomas <thomas@touhey.fr>                                              #
#   Last updated: 2016/05/23 00:26:44                                          #
#                                                                              #
#******************************************************************************#
# 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, see <http://www.gnu.org/licenses/>.        #
#******************************************************************************#
# INCLUDE VARS AND CONFIGURATION
include $(CURDIR)/Makefile.cfg
include $(CURDIR)/Makefile.vars

# DEDUCED VARS
ALLINC = $(INCPUB) $(INCINT)

# TARGETS
## General
## - Make everything means make the library (default)
all: lib$(NAME).a

## Library-related
## - Make the object directory
$(OBJDIR):
	$(MD) $@

## - Make an object file out of a C source file
$(OBJDIR)/%.o: $(SRCDIR)/%.c $(ALLINC:%=$(INCDIR)/%.h) | $(OBJDIR)
	$(CC) -c -o $@ $< $(CFLAGS)

## - Make the library
lib$(NAME).a: $(SRC:%=$(OBJDIR)/%.o)
	$(AR) rc $@ $^
	$(RANLIB) $@

## - Clean the object files
clean:
	$(RM) $(SRC:%=$(OBJDIR)/%.o)

## - Clean the object files and the binary
fclean: clean
	$(RM) lib$(NAME).a

## - Remake the library
re: fclean all

## - Install an include file
define make-installinc-rule
$(IINCDIR)/$1.h: $(INCDIR)/$1.h
	$(INST) -D -m 644 $$< $$@
endef
$(foreach inc,$(INCPUB), \
$(eval $(call make-installinc-rule,$(inc))))

## - Install the library
$(ILIBDIR)/lib$(NAME).a: ./lib$(NAME).a
	$(INST) -D -m 644 $< $@

## - Install the project
install: $(ILIBDIR)/lib$(NAME).a $(foreach inc,$(INCPUB),$(IINCDIR)/$(inc).h)

## Documentation-related
## - Make man directories
define make-mandir-rule
$(MANDIR)/man$1:
	$(MD) $$@
endef
$(foreach section,$(MAN_SECTS), \
$(eval $(call make-mandir-rule,$(section))))

## - Make-A-Manpage
define make-manpage-rule
$(MANDIR)/man$1/$2.$1: $(DOCDIR)/$2.$1.txt | $(MANDIR)/man$1
	$(A2X) -f manpage -D $$| $$<
endef
$(foreach section,$(MAN_SECTS), \
$(foreach page,$(MAN_$(section)), \
$(eval $(call make-manpage-rule,$(section),$(page)))))

## - Make all manpages
all-doc: $(foreach section,$(MAN_SECTS),$(MAN_$(section):%=$(MANDIR)/man$(section)/%.$(section)))

## - Clean all manpages
clean-doc:
	$(RM) -R $(MAN_SECTS:%=$(MANDIR)/man%)

## - Install a manpage
define make-installmanpage-rule
install-$1-$2:| $(MANDIR)/man$1/$2.$1
	$(INST) -D -m 644 $(MANDIR)/man$1/$2.$1 $(IMANDIR)/man$1/$2.$1
	$(GZIP) $(IMANDIR)/man$1/$2.$1
endef
$(foreach section, $(MAN_SECTS), \
$(foreach page,$(MAN_$(section)), \
$(eval $(call make-installmanpage-rule,$(section),$(page)))))

## - Install manpages
install-doc: $(foreach s,$(MAN_SECTS),$(MAN_$(s):%=install-$(s)-%))

## Doz rulz are phunny
.PHONY: all clean fclean re install
.PHONY: all-doc $(foreach s,$(MAN_SECTS),$(MAN_$(s):%=install-$(s)-%)) install-doc

# END OF FILE
