Makefile
author Oleksandr Gavenko <gavenkoa@gmail.com>
Tue, 09 May 2023 16:49:07 +0300
changeset 91 cbb01f06b349
parent 88 dcb712a32f02
permissions -rw-r--r--
Fixed spelling.


################################################################
# 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.

BUILD_DIR := dist
SITE_DIR := $(BUILD_DIR)/site

RST_FILES := $(wildcard *.rst)

RST_HTML_FILES := $(patsubst %.rst,$(SITE_DIR)/%.html,$(RST_FILES))
HTML_FILES := $(RST_HTML_FILES)

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

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

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

.PHONY: deploy
deploy: deploy2defun

.PHONY: deploy2defun
deploy2defun: deploy2defun-web deploy2defun-hg

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

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

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

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

.PHONY: all
all:

.PHONY: www
www: html $(SITE_DIR)/sitemap.xml $(SITE_DIR)/robots.txt

.PHONY: html
html: $(HTML_FILES)

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

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

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

# $(HTML_DIR)/single.html: $(HTML_DIR)/single.rest www/rst.css www/rst-single.css $(RST_FILES)
# 	$(RST2HTML) $(RST_FLAGS) --stylesheet=www/rst.css,www/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; \
# } >$@

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

$(SITE_DIR)/robots.txt: www/robots.txt $(MAKEFILE_LIST) | $(SITE_DIR)
	cp $< $@

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

$(BUILD_DIR) $(SITE_DIR) $(LOCAL_DIR):
	mkdir -p $@

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

.PHONY: distclean
distclean: clean
	rm -r -f $(LOCAL_DIR)

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

################################################################
# 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