Makefile
author Oleksandr Gavenko <gavenkoa@gmail.com>
Fri, 13 Jan 2012 00:04:09 +0200
changeset 115 1c6ffaecdf50
parent 112 e48a802ce83b
child 118 c9ce7b6ae3bc
permissions -rw-r--r--
Fix sftp command usage.

# To build dictionary you need to install:
#
#    $ sudo apt-get install dictfmt
#
# Available target:
#
#   all         build dictionaries
#   install     install dictionaries, so they been available in stardict, may require root permission
#   uninstall   remove dictionaries, they been unavailable in stardict, may require root permission
#   install-local install dictionaries in user HOME dir, so they been available in stardict for that user
#   uninstall-local remove dictionaries from user HOME directory
#   install-local-symlink create symlink from installed dir to user HOME dir, so dictionaries been available in stardict
#   uninstall-local-symlink remove symlink from user HOME directory

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:
# Default target.
.DEFAULT_GOAL = help

################################################################
# Version extracting/generation.

# Prevent from deploying and distributing with wrong version.
ifneq '' '$(filter deploy%,$(MAKECMDGOALS))'
  ifeq '' '$(MAKE_RESTARTS)'
    $(info $(shell rm VERSION))
  endif
endif

# Here are vmajor and vminor. Look README section "Versioning rules."
-include VERSION

VERSION:
	ver=$$(hg log -r . --template '{latesttag}'); \
ver=$${ver#t}; \
major=$${ver%.*}; \
minor=$${ver#*.}; \
printf "vmajor=%s\nvminor=%s\n" $$major $$minor >VERSION
	# is_release=`hg log --template '{latesttagdistance}'`

################################################################
# Determine platform/environment.

host_os = unix
ifneq '' '$(COMSPEC)'
  ifneq '' '$(WINDIR)'
    # Probably under Windows.
    host_os = windows
    ifneq '' '$(wildcard /etc/setup/*cygwin*)'
      # Probably under Cygwin.
      host_os = cygwin
    endif
  endif
endif

################################################################
# Build tools definition/switches.

RST_WARNING_FLAGS := --halt warning
RST_FLAGS := --strip-comments
RST_FLAGS += $(RST_WARNING_FLAGS)

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

################################################################
# Install paths.

ifeq '$(origin prefix)' 'undefined'
  ifeq '$(shell id -u)' '0'
    prefix = /usr/local
  else
    prefix = $(HOME)/usr
  endif
endif
datarootdir = $(prefix)/share
datadir = $(datarootdir)/dictd

################################################################
# Project dirs/files.

BUILD_SCRIPT := $(firstword $(MAKEFILE_LIST))

C5_FILES := $(wildcard *.dict-c5)
DICT_FILES := $(C5_FILES:.dict-c5=.dict)
DICTDZ_FILES := $(C5_FILES:.dict-c5=.dict.dz)
INDEX_FILES := $(C5_FILES:.dict-c5=.index)

RST_FILES := $(wildcard *.rst)
HTML_FILES := $(RST_FILES:.rst=.html)

name = gadict
fullpkgname = $(name)-$(vmajor).$(vminor)

DIST_DIR = $(fullpkgname)
DISTSRC_DIR = $(fullpkgname)_src

DIST_FILES = $(DICTDZ_FILES) $(INDEX_FILES) $(HTML_FILES) $(RST_FILES) VERSION

DISTSRC_HELPER_FILES = VERSION Makefile rst.css .dir-locals.el
DISTSRC_FILES = $(HTML_FILES) $(RST_FILES) $(DISTSRC_HELPER_FILES)

DIST_TARBALLS = $(DIST_DIR).tar.gz $(DIST_DIR).tar.bz2
DISTSRC_TARBALLS = $(DISTSRC_DIR).tar.gz $(DISTSRC_DIR).tar.bz2

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

.PHONY: deploy
deploy: deploy2sf

SFTP_PATH = gavenkoa,$(name)@frs.sourceforge.net

# First time you deploy to SourceForge (sf) you need manually login to:
#   $ sftp $(SFTP_PATH)
# as it may require interactive input for accepting server public key.
# Next time any action fully automated.

.PHONY: deploy2sf
deploy2sf: deploy2sf-doc deploy2sf-release deploy2sf-src

# Will be accessed via http://$(name).sourceforge.net
.PHONY: deploy2sf-doc
deploy2sf-doc: $(HTML_FILES)
	( echo 'cd htdocs'; \
for f in $(HTML_FILES); do \
  echo "put $$f"; \
  echo "chmod 644 $$f"; \
done; \
echo 'quit'; \
) | sftp -b - $(SFTP_PATH)

.PHONY: deploy2sf-release
deploy2sf-release: $(DIST_TARBALLS) $(DISTSRC_TARBALLS)
	( echo 'cd /home/frs/project/g/ga/gadict'; \
echo "put CHANGES.rst"; \
echo "chmod 644 CHANGES.rst"; \
echo "put  README.rst"; \
echo "chmod 644 README.rst"; \
echo "mkdir v$(vmajor).$(vminor)"; \
echo "cd v$(vmajor).$(vminor)"; \
for f in $^; do \
	echo "put $$f"; \
	echo "chmod 644 $$f"; \
done; \
echo 'quit'; \
) | sftp -b - $(SFTP_PATH)

.PHONY: deploy2sf-src
deploy2sf-src:
	hg push ssh://gavenkoa@hg.code.sf.net/p/$(name)/code

################################################################
# Dist targets.

.PHONY: dist
dist: dist-dictd dist-src

.PHONY: dist-dictd
dist-dictd: $(DIST_TARBALLS)

.PHONY: dist-src
dist-src: $(DISTSRC_TARBALLS)

%.tar.gz: %
	tar zcf $*.tar.gz $*

%.tar.bz2: %
	tar jcf $*.tar.bz2 $*

$(DIST_DIR): $(DIST_FILES)
	rm -rf $@
	mkdir $@
	cp $(DIST_FILES) $@

$(DISTSRC_DIR): $(DISTSRC_FILES)
	rm -rf $@
	mkdir $@
	cp $(DISTSRC_FILES) $@

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

.PHONY: all
all: dist

.PHONY: dist
dist: $(DICTDZ_FILES) $(INDEX_FILES)

#  --case-sensitive
%.dict %.index: %.dict-c5
	sed '/^#/d' $< | dictfmt  -c5 \
	--headword-separator '; ' --break-headwords \
	--utf8 --allchars \
	-u "`sed -n '\|http://[[:print:]]\+/|{s=^.*\(http://\)=\1=;p;q;}' $<`" \
	-s "`sed -n '/^ABOUT: /{s=ABOUT: ==;p;q;}' $<`" \
	$*

%.dict.dz: %.dict
	dictzip -c $< >$@

################################################################
# Install/uninstall targets.

.PHONY: install
install: dist
	mkdir -p $(datadir)
	for f in $(DICTDZ_FILES) $(INDEX_FILES); do \
		install -m 644 $$f $(datadir); \
	done

.PHONY: uninstall
uninstall:
	for f in $(DICTDZ_FILES) $(INDEX_FILES); do \
		rm -f $(datadir)/$$f; \
	done

################################################################
# Documentation targets.

.PHONY: html
html: $(HTML_FILES)

$(HTML_FILES): %.html: %.rst rst.css VERSION.rst $(BUILD_SCRIPT)
	$(RST2HTML) $(RST2HTML_FLAGS) --stylesheet=rst.css $*.rst $@

VERSION.rst: VERSION $(BUILD_SCRIPT)
	{ \
	echo 'Version: '$(vmajor).$(vminor); \
	echo; \
	echo 'Date: '`date +%F`; \
	} >$@

################################################################
# Helpers targets.

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

.PHONY: stat
stat:
	total=0; \
	for dic in *.dict-c5; do \
		cnt=`grep '^_____' $$dic | wc -l`; \
		echo $$dic 'has  ' $$cnt words.; \
		total=$$(($$total + $$cnt)); \
	done; \
	echo Total words count is $$total.

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

.PHONY: distclean
distclean: clean
	rm -f VERSION

.PHONY: clean
clean:
	rm -f $(DICTDZ_FILES) $(INDEX_FILES) $(HTML_FILES)
	rm -rf $(DIST_DIR) $(DIST_TARBALLS)
	rm -rf $(DISTSRC_DIR) $(DISTSRC_TARBALLS)