Makefile
author Oleksandr Gavenko <gavenkoa@gmail.com>
Thu, 04 Aug 2016 15:20:07 +0300
changeset 78 093ee7c2985f
parent 77 df763e1c3fe6
child 79 a0f224c26f08
permissions -rw-r--r--
Generate sitemap.


################################################################
# Standard GNU Makefile settings.

SHELL = /bin/sh
export PATH := /bin:/usr/bin:${PATH}

# Disable built in pattern rules.
MAKEFLAGS += -r
# Disable built in variables.
MAKEFLAGS += -R
# Disable built in suffix rules.
.SUFFIXES:
# Delete target file if command fails.
.DELETE_ON_ERROR:
# Default target.
.DEFAULT_GOAL = all

################################################################
# Platform definition.

host_os := linux
ifneq '' '$(WINDIR)'
  host_os := cygwin
endif
target_os := $(host_os)

################################################################
# Build tool definition/switches.

RST2HTML := rst2html
ifeq '$(host_os)' 'cygwin'
  RST2HTML := rst2html.py
endif

RST_WARNING_FLAGS := --halt warning
RST_RENDER_FLAGS := --strip-comments --embed-stylesheet --no-xml-declaration --math-output=MathJax --initial-header-level=2
RST_FLAGS := $(RST_WARNING_FLAGS) $(RST_RENDER_FLAGS)

################################################################
# Proj dirs/files.

RST_FILES := $(wildcard *.rst)

HTML_DIR := tips-html
RST_HTML_FILES := $(patsubst %.rst,$(HTML_DIR)/%.html,$(RST_FILES))
HTML_FILES := $(RST_HTML_FILES) \
        $(HTML_DIR)/index.html $(HTML_DIR)/iframe.html

CHM_FILES := $(addprefix $(HTML_DIR)/,chm.hhp chm.hhc chm.stp)

DIRS := $(HTML_DIR)

################################################################
# Deploy targets.

WWW_SRV_NAME := tips.defun.work
WWW_SRV_USER := user
HG_SRV_NAME := hg.defun.work
HG_SRV_USER := user
LOCAL_DIR := /srv/www/tips

ifneq '' '$(filter deploy%,$(MAKECMDGOALS))'
  $(shell rm -f $(HTML_DIR)/rst.tmpl)
endif

.PHONY: deploy
deploy: deploy2defun-web deploy2defun-hg deploy2sf-web deploy2sf-hg

# Will be accessible via: http://tips.defun.work/
.PHONY: deploy2defun-web
deploy2defun-web: www
	rsync --delete -avP -e ssh $(HTML_DIR)/ $(WWW_SRV_USER)@$(WWW_SRV_NAME):/srv/www/tips/

.PHONY: deploy2defun-hg
deploy2defun-hg:
	hg push ssh://$(HG_SRV_USER)@$(HG_SRV_NAME)//srv/hg/tips || [ $$? = 1 ]

# Will be accessible via: http://gavenkoa.users.sourceforge.net/tips-html/
.PHONY: deploy2sf-web
deploy2sf-web: www
	rsync --delete -avP -e ssh $(HTML_DIR)/ gavenkoa@frs.sourceforge.net:/home/user-web/g/ga/gavenkoa/htdocs/tips-html/

.PHONY: deploy2sf-hg
deploy2sf-hg:
	hg push ssh://gavenkoa@hg.code.sf.net/u/gavenkoa/tips || [ $$? = 1 ]

.PHONY: deploy2local
deploy2local: html
	rsync --delete -avP $(HTML_DIR)/ $(LOCAL_DIR)

################################################################
# Build targets.

.PHONY: all
all:

.PHONY: www
www: html $(HTML_DIR)/sitemap.xml

.PHONY: html
html: $(HTML_FILES)

$(HTML_DIR)/%.html: %.rst rst.css rst-multi.css $(HTML_DIR)/rst.tmpl $(MAKEFILE_LIST) | $(HTML_DIR)
	$(RST2HTML) $(RST_FLAGS) --stylesheet=rst.css,rst-multi.css --template=$(HTML_DIR)/rst.tmpl $*.rst $@

$(HTML_DIR)/iframe.html: $(RST_FILES) $(MAKEFILE_LIST) | $(HTML_DIR)
	{ \
echo '<html><head>'; \
echo '<meta charset="utf-8">'; \
echo '<style>'; \
echo 'a { text-decoration: none; color: hsl(240, 100%, 50%); }'; \
echo 'a:hover { opacity: .5; }'; \
echo '</style></head><body>'; \
echo '<ul style="padding-left: 1em;">'; \
for f in $(sort $(RST_FILES)); do \
  n=$${f%.rst}; \
  printf '<li><a target="_parent" href="%s.html">%s</a></li>\n' $$n $$n; \
done; \
echo '</ul>'; \
echo '</body></html>'; \
} >$@

$(HTML_DIR)/rst.tmpl: rst.tmpl $(MAKEFILE_LIST)
	sed -e "s|{date}|$$(date +%F)|" -e "s|{rev}|$$(hg id -i)|"  <$< >$@

$(HTML_DIR)/index.html: $(HTML_DIR)/README.html $(MAKEFILE_LIST) | $(HTML_DIR)
	cp $< $@

.PHONY: chm
chm: html $(CHM_FILES) $(HTML_DIR)/iframe.html
	cd $(HTML_DIR); for file in *.html; do sed -i '/<\?xml.*\?>/d' $$file; done

$(HTML_DIR)/%.stp: %.stp $(MAKEFILE_LIST) | $(HTML_DIR)
	cp $< $@

$(HTML_DIR)/chm.hhp: chm-hhp.sh $(MAKEFILE_LIST) | $(HTML_DIR)
	./chm-hhp.sh >$@

$(HTML_DIR)/chm.hhc: chm-hhc.sh $(MAKEFILE_LIST) | $(HTML_DIR)
	./chm-hhc.sh >$@

.PHONY: single-html
single-html: $(HTML_DIR)/single.html

$(HTML_DIR)/single.html: $(HTML_DIR)/single.rest rst.css rst-single.css $(RST_FILES)
	$(RST2HTML) $(RST_FLAGS) --stylesheet=rst.css,rst-single.css $(HTML_DIR)/single.rest $@

$(HTML_DIR)/single.rest: $(RST_FILES) $(MAKEFILE_LIST)
	{ \
echo ".. contents::"; \
echo "   :local:"; \
echo; \
for f in *.rst; do echo ".. include:: ../$$f"; done; \
} >$@

$(HTML_DIR)/sitemap.xml: $(RST_FILES) $(MAKEFILE_LIST)
	{ \
echo '<?xml version="1.0" encoding="UTF-8"?>'; \
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; \
for f in *.rst iframe; do \
  echo '<url>'; \
  echo "  <loc>http://$(WWW_SRV_NAME)/$${f%.rst}.html</loc>"; \
  echo '  <changefreq>weekly</changefreq>'; \
  echo '</url>'; \
done; \
echo '</urlset>'; \
} >$@

################################################################
# Init targets.

$(DIRS):
	mkdir -p $@

################################################################
# Clean targets.

.PHONY: distclean
distclean: clean

.PHONY: clean
clean:
	rm -r -f $(DIRS)

################################################################
# Helper target.

.PHONY: help
help:
	@echo Supported targets:
	@sed -n -e '/^[[:alnum:]_-]*:/{s=^\(.*\):.*=  \1=;p;}' $(MAKEFILE_LIST)

.PHONY: check-format-policy
check-format-policy:
	\
for f in $(RST_FILES); do \
  if grep '^.. -\*- coding: utf-8; -\*-' $$f >/dev/null; then :; else \
    echo $$f:1:" Has no 'coding: utf-8' directive."; \
  fi; \
  if grep '^.. contents::' $$f >/dev/null; then :; else \
    echo $$f:7:" Has no 'contents::' directive."; \
  fi; \
  if grep '^   :local:' $$f >/dev/null; then :; else \
    echo $$f:7:" Has no ':local:' directive."; \
  fi; \
done