1# WARN: gmake syntax
2########################################################
3# Makefile for Ansible
4#
5# useful targets:
6#   make clean ---------------- clean up
7#   make webdocs -------------- produce ansible doc at docs/docsite/_build/html
8#   make coredocs ------------- produce core doc at docs/docsite/_build/html
9#   make sdist ---------------- produce a tarball
10#   make deb-src -------------- produce a DEB source
11#   make deb ------------------ produce a DEB
12#   make docs ----------------- rebuild the manpages (results are checked in)
13#   make gettext -------------- produce POT files for docs
14#   make generate-po ---------- generate language specific po file
15#   make needs-translation ---- generate list of file with unstranlated or fuzzy string for a specific language
16#   make tests ---------------- run the tests (see https://docs.ansible.com/ansible/devel/dev_guide/testing_units.html for requirements)
17
18########################################################
19# variable section
20
21NAME = ansible-core
22OS = $(shell uname -s)
23PREFIX ?= '/usr/local'
24SDIST_DIR ?= 'dist'
25
26# This doesn't evaluate until it's called. The -D argument is the
27# directory of the target file ($@), kinda like `dirname`.
28MANPAGES ?= $(patsubst %.rst.in,%,$(wildcard ./docs/man/man1/ansible*.1.rst.in))
29ifneq ($(shell which rst2man 2>/dev/null),)
30ASCII2MAN = rst2man $< $@
31else ifneq ($(shell which rst2man.py 2>/dev/null),)
32ASCII2MAN = rst2man.py $< $@
33else
34ASCII2MAN = @echo "ERROR: rst2man from docutils command is not installed but is required to build $(MANPAGES)" && exit 1
35endif
36
37PYTHON ?= python
38GENERATE_CLI = hacking/build-ansible.py generate-man
39
40# fetch version from project release.py as single source-of-truth
41VERSION := $(shell $(PYTHON) packaging/release/versionhelper/version_helper.py --raw || echo error)
42ifeq ($(findstring error,$(VERSION)), error)
43$(error "version_helper failed")
44endif
45
46# Get the branch information from git
47ifneq ($(shell which git),)
48GIT_DATE := $(shell git log -n 1 --format="%ci")
49GIT_HASH := $(shell git log -n 1 --format="%h")
50GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD | sed 's/[-_.\/]//g')
51GITINFO = .$(GIT_HASH).$(GIT_BRANCH)
52else
53GITINFO = ""
54endif
55
56ifeq ($(shell echo $(OS) | egrep -c 'Darwin|FreeBSD|OpenBSD|DragonFly'),1)
57DATE := $(shell date -j -r $(shell git log -n 1 --format="%ct") +%Y%m%d%H%M)
58CPUS ?= $(shell sysctl hw.ncpu|awk '{print $$2}')
59else
60DATE := $(shell date --utc --date="$(GIT_DATE)" +%Y%m%d%H%M)
61CPUS ?= $(shell nproc)
62endif
63
64# Intenationalisation and Localisation
65LANGUAGES ?=
66
67# DEB build parameters
68DEBUILD_BIN ?= debuild
69DEBUILD_OPTS = --source-option="-I"
70DPUT_BIN ?= dput
71DPUT_OPTS ?=
72DEB_DATE := $(shell LC_TIME=C date +"%a, %d %b %Y %T %z")
73DEB_VERSION ?= $(shell $(PYTHON) packaging/release/versionhelper/version_helper.py --debversion)
74ifeq ($(OFFICIAL),yes)
75    DEB_RELEASE ?= $(shell $(PYTHON) packaging/release/versionhelper/version_helper.py --debrelease)ppa
76    # Sign OFFICIAL builds using 'DEBSIGN_KEYID'
77    # DEBSIGN_KEYID is required when signing
78    ifneq ($(DEBSIGN_KEYID),)
79        DEBUILD_OPTS += -k$(DEBSIGN_KEYID)
80    endif
81else
82    DEB_RELEASE ?= 100.git$(DATE)$(GITINFO)
83    # Do not sign unofficial builds
84    DEBUILD_OPTS += -uc -us
85    DPUT_OPTS += -u
86endif
87DEBUILD = $(DEBUILD_BIN) $(DEBUILD_OPTS)
88DEB_PPA ?= ppa
89# Choose the desired Ubuntu release: lucid precise saucy trusty
90DEB_DIST ?= unstable
91
92# pbuilder parameters
93PBUILDER_ARCH ?= amd64
94PBUILDER_CACHE_DIR = /var/cache/pbuilder
95PBUILDER_BIN ?= pbuilder
96PBUILDER_OPTS ?= --debootstrapopts --variant=buildd --architecture $(PBUILDER_ARCH) --debbuildopts -b
97
98# ansible-test parameters
99ANSIBLE_TEST ?= bin/ansible-test
100TEST_FLAGS ?=
101
102# ansible-test units parameters (make test / make test-py3)
103PYTHON_VERSION ?= $(shell python2 -c 'import sys; print("%s.%s" % sys.version_info[:2])')
104PYTHON3_VERSION ?= $(shell python3 -c 'import sys; print("%s.%s" % sys.version_info[:2])')
105
106# ansible-test integration parameters (make integration)
107IMAGE ?= centos7
108TARGET ?=
109
110########################################################
111
112.PHONY: all
113all: clean python
114
115.PHONY: tests
116tests:
117	$(ANSIBLE_TEST) units -v --python $(PYTHON_VERSION) $(TEST_FLAGS)
118
119.PHONY: tests-py3
120tests-py3:
121	$(ANSIBLE_TEST) units -v --python $(PYTHON3_VERSION) $(TEST_FLAGS)
122
123.PHONY: integration
124integration:
125	$(ANSIBLE_TEST) integration -v --docker $(IMAGE) $(TARGET) $(TEST_FLAGS)
126
127# Regenerate %.1.rst if %.1.rst.in has been modified more
128# recently than %.1.rst.
129%.1.rst: %.1.rst.in
130	sed "s/%VERSION%/$(VERSION)/" $< > $@
131	rm $<
132
133# Regenerate %.1 if %.1.rst or release.py has been modified more
134# recently than %.1. (Implicitly runs the %.1.rst recipe)
135%.1: %.1.rst lib/ansible/release.py
136	$(ASCII2MAN)
137
138.PHONY: clean
139clean:
140	@echo "Cleaning up distutils stuff"
141	rm -rf build
142	rm -rf dist
143	rm -rf lib/ansible*.egg-info/
144	@echo "Cleaning up byte compiled python stuff"
145	find . -type f -regex ".*\.py[co]$$" -delete
146	find . -type d -name "__pycache__" -delete
147	@echo "Cleaning up editor backup files"
148	find . -type f -not -path ./test/units/inventory_test_data/group_vars/noparse/all.yml~ \( -name "*~" -or -name "#*" \) -delete
149	find . -type f \( -name "*.swp" \) -delete
150	@echo "Cleaning up manpage stuff"
151	find ./docs/man -type f -name "*.xml" -delete
152	find ./docs/man -type f -name "*.rst" -delete
153	find ./docs/man/man3 -type f -name "*.3" -delete
154	rm -f ./docs/man/man1/*
155	@echo "Cleaning up output from test runs"
156	rm -rf test/test_data
157	rm -rf shippable/
158	rm -rf logs/
159	rm -rf .cache/
160	rm -f test/units/.coverage*
161	rm -rf test/results/*/*
162	find test/ -type f -name '*.retry' -delete
163	@echo "Cleaning up symlink cache"
164	rm -f SYMLINK_CACHE.json
165	@echo "Cleaning up Debian building stuff"
166	rm -rf debian
167	rm -rf deb-build
168	rm -rf docs/json
169	rm -rf docs/js
170	@echo "Cleaning up docsite"
171	$(MAKE) -C docs/docsite clean
172
173.PHONY: python
174python:
175	$(PYTHON) setup.py build
176
177.PHONY: install
178install:
179	$(PYTHON) setup.py install
180
181install_manpages:
182	gzip -9 $(wildcard ./docs/man/man1/ansible*.1)
183	cp $(wildcard ./docs/man/man1/ansible*.1.gz) $(PREFIX)/man/man1/
184
185.PHONY: sdist_check
186sdist_check:
187	$(PYTHON) -c 'import setuptools, sys; sys.exit(int(not (tuple(map(int, setuptools.__version__.split("."))) > (39, 2, 0))))'
188	$(PYTHON) packaging/sdist/check-link-behavior.py
189
190.PHONY: sdist
191sdist: sdist_check clean docs
192	_ANSIBLE_SDIST_FROM_MAKEFILE=1 $(PYTHON) setup.py sdist --dist-dir=$(SDIST_DIR)
193
194# Official releases generate the changelog as the last commit before the release.
195# Snapshots shouldn't result in new checkins so the changelog is generated as
196# part of creating the tarball.
197.PHONY: snapshot
198snapshot: sdist_check clean docs changelog
199	_ANSIBLE_SDIST_FROM_MAKEFILE=1 $(PYTHON) setup.py sdist --dist-dir=$(SDIST_DIR)
200
201.PHONY: sdist_upload
202sdist_upload: clean docs
203	$(PYTHON) setup.py sdist upload 2>&1 |tee upload.log
204
205.PHONY: changelog
206changelog:
207	PYTHONPATH=./lib antsibull-changelog release -vv --use-ansible-doc && PYTHONPATH=./lib antsibull-changelog generate -vv --use-ansible-doc
208
209.PHONY: debian
210debian: sdist
211	@for DIST in $(DEB_DIST) ; do \
212	    mkdir -p deb-build/$${DIST} ; \
213	    tar -C deb-build/$${DIST} -xvf dist/$(NAME)-$(VERSION).tar.gz ; \
214	    cp -a packaging/debian deb-build/$${DIST}/$(NAME)-$(VERSION)/ ; \
215        sed -ie "s|%VERSION%|$(DEB_VERSION)|g;s|%RELEASE%|$(DEB_RELEASE)|;s|%DIST%|$${DIST}|g;s|%DATE%|$(DEB_DATE)|g" deb-build/$${DIST}/$(NAME)-$(VERSION)/debian/changelog ; \
216	done
217
218.PHONY: deb
219deb: deb-src
220	@for DIST in $(DEB_DIST) ; do \
221	    PBUILDER_OPTS="$(PBUILDER_OPTS) --distribution $${DIST} --basetgz $(PBUILDER_CACHE_DIR)/$${DIST}-$(PBUILDER_ARCH)-base.tgz --buildresult $(CURDIR)/deb-build/$${DIST}" ; \
222	    $(PBUILDER_BIN) create $${PBUILDER_OPTS} --othermirror "deb http://archive.ubuntu.com/ubuntu $${DIST} universe" ; \
223	    $(PBUILDER_BIN) update $${PBUILDER_OPTS} ; \
224	    $(PBUILDER_BIN) build $${PBUILDER_OPTS} deb-build/$${DIST}/$(NAME)_$(DEB_VERSION)-$(DEB_RELEASE)~$${DIST}.dsc ; \
225	done
226	@echo "#############################################"
227	@echo "Ansible DEB artifacts:"
228	@for DIST in $(DEB_DIST) ; do \
229	    echo deb-build/$${DIST}/$(NAME)_$(DEB_VERSION)-$(DEB_RELEASE)~$${DIST}_amd64.changes ; \
230	done
231	@echo "#############################################"
232
233# Build package outside of pbuilder, with locally installed dependencies.
234# Install BuildRequires as noted in packaging/debian/control.
235.PHONY: local_deb
236local_deb: debian
237	@for DIST in $(DEB_DIST) ; do \
238	    (cd deb-build/$${DIST}/$(NAME)-$(VERSION)/ && $(DEBUILD) -b) ; \
239	done
240	@echo "#############################################"
241	@echo "Ansible DEB artifacts:"
242	@for DIST in $(DEB_DIST) ; do \
243	    echo deb-build/$${DIST}/$(NAME)_$(DEB_VERSION)-$(DEB_RELEASE)~$${DIST}_amd64.changes ; \
244	done
245	@echo "#############################################"
246
247.PHONY: deb-src
248deb-src: debian
249	@for DIST in $(DEB_DIST) ; do \
250	    (cd deb-build/$${DIST}/$(NAME)-$(VERSION)/ && $(DEBUILD) -S) ; \
251	done
252	@echo "#############################################"
253	@echo "Ansible DEB artifacts:"
254	@for DIST in $(DEB_DIST) ; do \
255	    echo deb-build/$${DIST}/$(NAME)_$(DEB_VERSION)-$(DEB_RELEASE)~$${DIST}_source.changes ; \
256	done
257	@echo "#############################################"
258
259.PHONY: deb-upload
260deb-upload: deb
261	@for DIST in $(DEB_DIST) ; do \
262	    $(DPUT_BIN) $(DPUT_OPTS) $(DEB_PPA) deb-build/$${DIST}/$(NAME)_$(DEB_VERSION)-$(DEB_RELEASE)~$${DIST}_amd64.changes ; \
263	done
264
265.PHONY: deb-src-upload
266deb-src-upload: deb-src
267	@for DIST in $(DEB_DIST) ; do \
268	    $(DPUT_BIN) $(DPUT_OPTS) $(DEB_PPA) deb-build/$${DIST}/$(NAME)_$(DEB_VERSION)-$(DEB_RELEASE)~$${DIST}_source.changes ; \
269	done
270
271.PHONY: epub
272epub:
273	(cd docs/docsite/; CPUS=$(CPUS) $(MAKE) epub)
274
275# for arch or gentoo, read instructions in the appropriate 'packaging' subdirectory directory
276.PHONY: webdocs
277webdocs:
278	(cd docs/docsite/; CPUS=$(CPUS) $(MAKE) docs)
279
280.PHONY: coredocs
281coredocs:
282	(cd docs/docsite/; CPUS=$(CPUS) $(MAKE) coredocs)
283
284.PHONY: gettext
285gettext:
286	(cd docs/docsite/; CPUS=$(CPUS) $(MAKE) gettext)
287
288.PHONY: generate-po
289generate-po:
290	(cd docs/docsite/; CPUS=$(CPUS) LANGUAGES=$(LANGUAGES) $(MAKE) generate-po)
291
292.PHONY: needs-translation
293needs-translation:
294	(cd docs/docsite/; CPUS=$(CPUS) LANGUAGES=$(LANGUAGES) $(MAKE) needs-translation)
295
296.PHONY: linkcheckdocs
297linkcheckdocs:
298	(cd docs/docsite/; CPUS=$(CPUS) $(MAKE) linkcheckdocs)
299
300.PHONY: generate_rst
301generate_rst: lib/ansible/cli/*.py
302	mkdir -p ./docs/man/man1/ ; \
303	$(GENERATE_CLI) --template-file=docs/templates/man.j2 --output-dir=docs/man/man1/ --output-format man lib/ansible/cli/*.py
304
305
306docs: generate_rst
307	$(MAKE) $(MANPAGES)
308
309.PHONY: alldocs
310alldocs: docs webdocs
311
312version:
313	@echo $(VERSION)
314