1# Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License, version 2.0,
5# as published by the Free Software Foundation.
6#
7# This program is also distributed with certain software (including
8# but not limited to OpenSSL) that is licensed under separate terms,
9# as designated in a particular file or component or in included license
10# documentation.  The authors of MySQL hereby grant you an additional
11# permission to link the program and your derivative works with the
12# separately licensed software that they have included with MySQL.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License, version 2.0, for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22
23# Make source distribution
24# If git is present, run git archive.
25# Otherwise, just run cpack with source configuration.
26
27SET(CMAKE_SOURCE_DIR "@CMAKE_SOURCE_DIR@")
28SET(CMAKE_BINARY_DIR "@CMAKE_BINARY_DIR@")
29SET(CPACK_SOURCE_PACKAGE_FILE_NAME "@CPACK_SOURCE_PACKAGE_FILE_NAME@")
30SET(CMAKE_CPACK_COMMAND  "@CMAKE_CPACK_COMMAND@")
31SET(CMAKE_COMMAND  "@CMAKE_COMMAND@")
32SET(GIT_EXECUTABLE "@GIT_EXECUTABLE@")
33SET(GTAR_EXECUTABLE "@GTAR_EXECUTABLE@")
34SET(TAR_EXECUTABLE "@TAR_EXECUTABLE@")
35SET(CMAKE_GENERATOR "@CMAKE_GENERATOR@")
36SET(CMAKE_MAKE_PROGRAM "@CMAKE_MAKE_PROGRAM@")
37SET(CMAKE_SYSTEM_NAME "@CMAKE_SYSTEM_NAME@")
38
39SET(VERSION "@VERSION@")
40
41SET(MYSQL_DOCS_LOCATION "@MYSQL_DOCS_LOCATION@")
42
43
44SET(PACKAGE_DIR  ${CMAKE_BINARY_DIR}/${CPACK_SOURCE_PACKAGE_FILE_NAME})
45
46FILE(REMOVE_RECURSE ${PACKAGE_DIR})
47FILE(REMOVE ${PACKAGE_DIR}.tar.gz )
48
49# Only allow git if source dir itself is a git repository
50IF(GIT_EXECUTABLE)
51  EXECUTE_PROCESS(
52    COMMAND "${GIT_EXECUTABLE}" rev-parse --show-toplevel
53    OUTPUT_VARIABLE GIT_ROOT
54    ERROR_VARIABLE GIT_ROOT_ERROR
55    OUTPUT_STRIP_TRAILING_WHITESPACE
56    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
57    RESULT_VARIABLE RESULT
58  )
59
60  IF(NOT RESULT EQUAL 0 OR NOT GIT_ROOT STREQUAL ${CMAKE_SOURCE_DIR})
61    MESSAGE(STATUS "This is not a git repository")
62    SET(GIT_EXECUTABLE)
63  ENDIF()
64ENDIF()
65
66IF(GIT_EXECUTABLE)
67  MESSAGE(STATUS "Running git archive -o ${PACKAGE_DIR}.tar")
68  EXECUTE_PROCESS(
69    COMMAND "${GIT_EXECUTABLE}" archive --format=tar
70    --prefix=${CPACK_SOURCE_PACKAGE_FILE_NAME}/ -o ${PACKAGE_DIR}.tar HEAD
71    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
72    RESULT_VARIABLE RESULT
73  )
74  IF(NOT RESULT EQUAL 0)
75    SET(GIT_EXECUTABLE)
76  ELSE()
77    # Unpack tarball
78    EXECUTE_PROCESS(
79      COMMAND ${CMAKE_COMMAND} -E tar xf ${PACKAGE_DIR}.tar
80      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
81      RESULT_VARIABLE TAR_RESULT
82    )
83    IF(NOT TAR_RESULT EQUAL 0)
84      SET(GIT_EXECUTABLE)
85    ELSE()
86      # Remove tarball after unpacking
87      EXECUTE_PROCESS(
88        COMMAND ${CMAKE_COMMAND} -E remove ${PACKAGE_DIR}.tar
89        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
90      )
91    ENDIF()
92  ENDIF()
93ENDIF()
94
95IF(NOT GIT_EXECUTABLE)
96  MESSAGE(STATUS "git not found or source dir is not a repo, use CPack")
97
98  IF(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
99    # In-source build is the worst option, we have to cleanup source tree.
100
101    # Save bison output first.
102    CONFIGURE_FILE(${CMAKE_BINARY_DIR}/sql/sql_yacc.cc
103       ${CMAKE_BINARY_DIR}/sql_yacc.cc COPYONLY)
104    CONFIGURE_FILE(${CMAKE_BINARY_DIR}/sql/sql_yacc.h
105       ${CMAKE_BINARY_DIR}/sql_yacc.h COPYONLY)
106
107    IF(CMAKE_GENERATOR MATCHES "Makefiles")
108    # make clean
109    EXECUTE_PROCESS(
110      COMMAND ${CMAKE_MAKE_PROGRAM} clean
111      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
112    )
113    ENDIF()
114
115    # Restore bison output
116    CONFIGURE_FILE(${CMAKE_BINARY_DIR}/sql_yacc.cc
117       ${CMAKE_BINARY_DIR}/sql/sql_yacc.cc COPYONLY)
118    CONFIGURE_FILE(${CMAKE_BINARY_DIR}/sql_yacc.h
119       ${CMAKE_BINARY_DIR}/sql/sql_yacc.h COPYONLY)
120    FILE(REMOVE ${CMAKE_BINARY_DIR}/sql_yacc.cc)
121    FILE(REMOVE ${CMAKE_BINARY_DIR}/sql_yacc.h)
122  ENDIF()
123
124  EXECUTE_PROCESS(
125    COMMAND ${CMAKE_CPACK_COMMAND} -G TGZ --config ./CPackSourceConfig.cmake
126    ${CMAKE_BINARY_DIR}/CPackSourceConfig.cmake
127
128    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
129  )
130  EXECUTE_PROCESS(
131  COMMAND ${CMAKE_COMMAND} -E tar xzf
132    ${CPACK_SOURCE_PACKAGE_FILE_NAME}.tar.gz
133    ${PACK_SOURCE_PACKAGE_FILE_NAME}
134    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
135  )
136ENDIF()
137
138# Copy bison output
139CONFIGURE_FILE(${CMAKE_BINARY_DIR}/sql/sql_yacc.h
140   ${PACKAGE_DIR}/sql/sql_yacc.h COPYONLY)
141CONFIGURE_FILE(${CMAKE_BINARY_DIR}/sql/sql_yacc.cc
142   ${PACKAGE_DIR}/sql/sql_yacc.cc COPYONLY)
143
144# Copy spec files
145SET(SPECFILENAME "mysql.${VERSION}.spec")
146IF("${VERSION}" MATCHES "-ndb-")
147  STRING(REGEX REPLACE "^.*-ndb-" "" NDBVERSION "${VERSION}")
148  SET(SPECFILENAME "mysql-cluster-${NDBVERSION}.spec")
149ENDIF()
150CONFIGURE_FILE(${CMAKE_BINARY_DIR}/support-files/${SPECFILENAME}
151   ${PACKAGE_DIR}/support-files/${SPECFILENAME} COPYONLY)
152
153# Add documentation, if user has specified where to find them
154IF(MYSQL_DOCS_LOCATION)
155  MESSAGE("Copying documentation files from " ${MYSQL_DOCS_LOCATION})
156  EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy_directory "${MYSQL_DOCS_LOCATION}" "${PACKAGE_DIR}")
157ENDIF()
158
159# Ensure there is an "INFO_SRC" file.
160INCLUDE(${CMAKE_BINARY_DIR}/info_macros.cmake)
161IF(NOT EXISTS ${PACKAGE_DIR}/Docs/INFO_SRC)
162  CREATE_INFO_SRC(${PACKAGE_DIR}/Docs)
163ENDIF()
164
165MESSAGE(STATUS "Preparing UDFs")
166EXECUTE_PROCESS(COMMAND autoreconf --install
167  WORKING_DIRECTORY ${PACKAGE_DIR}/UDF/
168  RESULT_VARIABLE udf_ret
169)
170
171IF(${udf_ret})
172  MESSAGE(FATAL_ERROR "Could not setup autotools build for UDF/: ${udf_ret}")
173ENDIF()
174
175# In case we used CPack, it could have copied some
176# extra files that are not usable on different machines.
177FILE(REMOVE ${PACKAGE_DIR}/CMakeCache.txt)
178
179# When packing source, prefer gnu tar  to "cmake -P tar"
180# cmake does not preserve timestamps.gnuwin32 tar is broken, cygwin is ok
181
182IF(CMAKE_SYSTEM_NAME MATCHES "Windows")
183  IF (EXISTS C:/cygwin/bin/tar.exe)
184    SET(TAR_EXECUTABLE C:/cygwin/bin/tar.exe)
185  ENDIF()
186ENDIF()
187
188IF(GTAR_EXECUTABLE)
189  SET(GNUTAR ${GTAR_EXECUTABLE})
190ELSEIF(TAR_EXECUTABLE)
191  EXECUTE_PROCESS(
192    COMMAND "${TAR_EXECUTABLE}" --version
193    RESULT_VARIABLE RESULT OUTPUT_VARIABLE OUT ERROR_VARIABLE ERR
194  )
195  IF(RESULT EQUAL 0 AND OUT MATCHES "GNU")
196    SET(GNUTAR ${TAR_EXECUTABLE})
197  ENDIF()
198ENDIF()
199
200SET($ENV{GZIP} "--best")
201
202IF(GNUTAR)
203  SET(PACK_COMMAND
204  ${GNUTAR} cfz
205  ${CPACK_SOURCE_PACKAGE_FILE_NAME}.tar.gz
206  ${CPACK_SOURCE_PACKAGE_FILE_NAME}
207  )
208ELSE()
209  SET(PACK_COMMAND ${CMAKE_COMMAND} -E tar cfz
210  ${CPACK_SOURCE_PACKAGE_FILE_NAME}.tar.gz
211  ${CPACK_SOURCE_PACKAGE_FILE_NAME}
212)
213ENDIF()
214
215MESSAGE(STATUS "Creating source package")
216
217EXECUTE_PROCESS(
218  COMMAND ${PACK_COMMAND}
219)
220MESSAGE(STATUS "Source package ${PACKAGE_DIR}.tar.gz created")
221