1# DistTarget.cmake
2#
3# Defines custom targets related to distributing source code.
4# It requires to have populated 'PROJECT_NAME' and 'PROJECT_VERSION' variables,
5# possibly through the project() command. It also uses 'PROJECT_DISTCONFIGURE_PARAMS'
6# variable when configuring the unpacked distribution.
7#
8# Added targets:
9# dist - only creates a tarball
10# disttest - creates a tarball and 'make && make install' it to a temporary prefix
11#    to verify that the code can be built and installed; it also verifies
12#    that the first line of the NEWS file contains the same version as
13#    the tarball and that it claims today's date.
14# distcheck - similar to 'disttest', only runs also 'make check' before installing
15
16# Filenames for tarball
17set(ARCHIVE_BASE_NAME ${PROJECT_NAME}-${PROJECT_VERSION})
18set(ARCHIVE_FULL_NAME ${ARCHIVE_BASE_NAME}.tar.xz)
19
20add_custom_target(dist
21	COMMAND ${CMAKE_COMMAND} -E chdir . "${CMAKE_SOURCE_DIR}/cmake/verify-pre-dist.sh"
22	COMMAND ${CMAKE_COMMAND} -E echo "Creating '${ARCHIVE_FULL_NAME}'..."
23	COMMAND git archive --prefix=${ARCHIVE_BASE_NAME}/ HEAD | xz -z > ${CMAKE_BINARY_DIR}/${ARCHIVE_FULL_NAME}
24	COMMAND ${CMAKE_COMMAND} -E echo "Distribution tarball '${ARCHIVE_FULL_NAME}' created at ${CMAKE_BINARY_DIR}"
25	WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
26)
27
28set(disttest_extract_dir "${CMAKE_BINARY_DIR}/${ARCHIVE_BASE_NAME}")
29set(disttest_build_dir "${disttest_extract_dir}/_build")
30set(disttest_install_dir "${disttest_extract_dir}/_install")
31
32add_custom_command(OUTPUT ${disttest_build_dir}/Makefile
33	# remove any left-over directory
34	COMMAND ${CMAKE_COMMAND} -E remove_directory ${disttest_extract_dir}
35
36	# extract the tarball
37	COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_BINARY_DIR} tar -xf ${ARCHIVE_FULL_NAME}
38
39	# verify the NEWS file contains what it should contain
40	COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_BINARY_DIR}
41		bash ${CMAKE_SOURCE_DIR}/cmake/verify-news-file.sh "${disttest_extract_dir}/NEWS" "${PROJECT_VERSION}"
42
43	# create a _build sub-directory
44	COMMAND ${CMAKE_COMMAND} -E make_directory "${disttest_build_dir}"
45
46	# configure the project with PROJECT_DISTCHECK_PARAMS
47	COMMAND ${CMAKE_COMMAND} -E chdir "${disttest_build_dir}"
48		${CMAKE_COMMAND} -G "Unix Makefiles"
49			${PROJECT_DISTCONFIGURE_PARAMS}
50			-DCMAKE_BUILD_TYPE=Release
51			-DCMAKE_INSTALL_PREFIX="${disttest_install_dir}"
52			..
53
54	# 'make' the project
55	COMMAND ${CMAKE_COMMAND} -E chdir ${disttest_build_dir} make -j
56
57	DEPENDS dist
58	COMMENT "Building from distribution tarball ${ARCHIVE_FULL_NAME}..."
59)
60
61add_custom_target(distcheck
62	# 'make check' the project
63	COMMAND ${CMAKE_COMMAND} -E chdir ${disttest_build_dir} make -j check
64
65	# 'make install' the project
66	COMMAND ${CMAKE_COMMAND} -E chdir ${disttest_build_dir} make -j install
67
68	# if we get this far, then everything worked, thus clean up
69	COMMAND ${CMAKE_COMMAND} -E remove_directory ${disttest_extract_dir}
70
71	# and show the good news
72	COMMAND ${CMAKE_COMMAND} -E echo "distcheck of '${ARCHIVE_FULL_NAME}' succeeded"
73
74	DEPENDS ${disttest_build_dir}/Makefile
75)
76
77add_custom_target(disttest
78	# 'make install' the project
79	COMMAND ${CMAKE_COMMAND} -E chdir ${disttest_build_dir} make -j install
80
81	# if we get this far, then everything worked, thus clean up
82	COMMAND ${CMAKE_COMMAND} -E remove_directory ${disttest_extract_dir}
83
84	# and show the good news
85	COMMAND ${CMAKE_COMMAND} -E echo "disttest of '${ARCHIVE_FULL_NAME}' succeeded"
86
87	DEPENDS ${disttest_build_dir}/Makefile
88)
89