1# Copyright (c) 2011, 2021, Oracle and/or its affiliates.
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
24# Handle/create the "INFO_*" files describing a MySQL (server) binary.
25# This is part of the fix for bug#42969.
26
27
28# Several of cmake's variables need to be translated from '@' notation
29# to '${}', this is done by the "configure" call in top level "CMakeLists.txt".
30# If further variables are used in this file, add them to this list.
31
32SET(VERSION "@VERSION@")
33SET(MAJOR_VERSION "@MAJOR_VERSION@")
34SET(MINOR_VERSION "@MINOR_VERSION@")
35SET(PATCH_VERSION "@PATCH_VERSION@")
36SET(CMAKE_SOURCE_DIR "@CMAKE_SOURCE_DIR@")
37SET(CMAKE_BINARY_DIR "@CMAKE_BINARY_DIR@")
38SET(CMAKE_GENERATOR "@CMAKE_GENERATOR@")
39SET(CMAKE_SIZEOF_VOID_P "@CMAKE_SIZEOF_VOID_P@")
40SET(GIT_EXECUTABLE "@GIT_EXECUTABLE@")
41SET(CMAKE_CROSSCOMPILING "@CMAKE_CROSSCOMPILING@")
42SET(CMAKE_HOST_SYSTEM "@CMAKE_HOST_SYSTEM@")
43SET(CMAKE_HOST_SYSTEM_PROCESSOR "@CMAKE_HOST_SYSTEM_PROCESSOR@")
44SET(CMAKE_SYSTEM "@CMAKE_SYSTEM@")
45SET(CMAKE_SYSTEM_PROCESSOR "@CMAKE_SYSTEM_PROCESSOR@")
46
47
48# Create an "INFO_SRC" file with information about the source (only).
49# We use "git log", if possible, and the "VERSION" contents.
50#
51# Outside development (git tree), the "INFO_SRC" file will not be modified
52# provided it exists (from "make dist" or a source tarball creation).
53
54MACRO(CREATE_INFO_SRC target_dir)
55  SET(INFO_SRC "${target_dir}/INFO_SRC")
56
57  SET(PERLSCRIPT
58    "use warnings; use POSIX qw(strftime); "
59    "print strftime \"%F %T %z\", localtime;")
60  EXECUTE_PROCESS(
61    COMMAND perl -e "${PERLSCRIPT}"
62    RESULT_VARIABLE result
63    OUTPUT_VARIABLE bdate
64    ERROR_VARIABLE error
65  )
66  IF(error)
67    MESSAGE(STATUS "Could not determine build-date: <${error}>")
68  ENDIF()
69
70  IF(GIT_EXECUTABLE AND EXISTS ${CMAKE_SOURCE_DIR}/.git)
71    # Sources are in a GIT repository: Always update.
72    EXECUTE_PROCESS(
73      COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
74      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
75      OUTPUT_VARIABLE bname
76    )
77
78    EXECUTE_PROCESS(
79      COMMAND ${GIT_EXECUTABLE} log -1
80      --pretty="commit: %H%ndate: %ci%nbuild-date: ${bdate} %nshort: %h%nbranch: ${bname}"
81      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
82      OUTPUT_VARIABLE VERSION_INFO
83    )
84
85    ## Output from git is quoted with "", remove them.
86    STRING(REPLACE "\"" "" VERSION_INFO "${VERSION_INFO}")
87    FILE(WRITE ${INFO_SRC} "${VERSION_INFO}\n")
88    # to debug, add: FILE(APPEND ${INFO_SRC} "\nResult ${RESULT}\n")
89    # For better readability ...
90    FILE(APPEND ${INFO_SRC}
91      "MySQL source ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}\n")
92  ELSEIF(EXISTS ${INFO_SRC})
93    # Outside a git tree, there is no need to change an existing "INFO_SRC",
94    # it cannot be improved.
95  ELSEIF(EXISTS ${CMAKE_SOURCE_DIR}/Docs/INFO_SRC)
96    # If we are building from a source distribution, it also contains "INFO_SRC".
97    # Similar, the export used for a release build already has the file.
98    FILE(READ ${CMAKE_SOURCE_DIR}/Docs/INFO_SRC SOURCE_INFO)
99    FILE(WRITE ${INFO_SRC} "${SOURCE_INFO}\n")
100  ELSEIF(EXISTS ${CMAKE_SOURCE_DIR}/INFO_SRC)
101    # This is not the proper location, but who knows ...
102    FILE(READ ${CMAKE_SOURCE_DIR}/INFO_SRC SOURCE_INFO)
103    FILE(WRITE ${INFO_SRC} "${SOURCE_INFO}\n")
104  ELSE()
105    # This is a fall-back.
106    FILE(WRITE ${INFO_SRC} "\nMySQL source ${VERSION}\n")
107  ENDIF()
108ENDMACRO(CREATE_INFO_SRC)
109
110
111# This is for the "real" build, must be run again with each cmake run
112# to make sure we report the current flags (not those of some previous run).
113
114MACRO(CREATE_INFO_BIN)
115  SET(INFO_BIN "Docs/INFO_BIN")
116
117  FILE(WRITE ${INFO_BIN} "===== Information about the build process: =====\n")
118  IF (WIN32)
119    EXECUTE_PROCESS(COMMAND cmd /c date /T
120        OUTPUT_VARIABLE TMP_DATE OUTPUT_STRIP_TRAILING_WHITESPACE)
121  ELSEIF(UNIX)
122    EXECUTE_PROCESS(COMMAND date "+%Y-%m-%d %H:%M:%S"
123        OUTPUT_VARIABLE TMP_DATE OUTPUT_STRIP_TRAILING_WHITESPACE)
124  ELSE()
125    SET(TMP_DATE "(no date command known for this platform)")
126  ENDIF()
127  SITE_NAME(HOSTNAME)
128  FILE(APPEND ${INFO_BIN} "Build was run at ${TMP_DATE} on host '${HOSTNAME}'\n\n")
129
130  # According to the cmake docs, these variables should always be set.
131  # However, they are empty in my tests, using cmake 2.6.4 on Linux, various Unix, and Windows.
132  # Still, include this code, so we will profit if a build environment does provide that info.
133  IF(CMAKE_HOST_SYSTEM)
134    FILE(APPEND ${INFO_BIN} "Build was done on  ${CMAKE_HOST_SYSTEM} using ${CMAKE_HOST_SYSTEM_PROCESSOR}\n")
135  ENDIF()
136  IF(CMAKE_CROSSCOMPILING)
137    FILE(APPEND ${INFO_BIN} "Build was done for ${CMAKE_SYSTEM} using ${CMAKE_SYSTEM_PROCESSOR}\n")
138  ENDIF()
139
140  # ${CMAKE_VERSION} doesn't work in 2.6.0, use the separate components.
141  FILE(APPEND ${INFO_BIN} "Build was done using cmake ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION} \n\n")
142
143  IF (WIN32)
144    FILE(APPEND ${INFO_BIN} "===== Compiler / generator used: =====\n")
145    FILE(APPEND ${INFO_BIN} ${CMAKE_GENERATOR} "\n\n")
146  ELSEIF(UNIX)
147    FILE(APPEND ${INFO_BIN} "===== Compiler flags used (from the 'sql/' subdirectory): =====\n")
148    IF(EXISTS sql/CMakeFiles/sql.dir/flags.make)
149      EXECUTE_PROCESS(COMMAND egrep "^# compile|^C_|^CXX_" sql/CMakeFiles/sql.dir/flags.make OUTPUT_VARIABLE COMPILE_FLAGS)
150      FILE(APPEND ${INFO_BIN} ${COMPILE_FLAGS} "\n")
151    ELSE()
152      FILE(APPEND ${INFO_BIN} "File 'sql/CMakeFiles/sql.dir/flags.make' is not yet found.\n\n")
153    ENDIF()
154  ENDIF()
155  FILE(APPEND ${INFO_BIN} "Pointer size: ${CMAKE_SIZEOF_VOID_P}\n\n")
156
157  FILE(APPEND ${INFO_BIN} "===== Feature flags used: =====\n")
158  IF(EXISTS ${CMAKE_BINARY_DIR}/CMakeCache.txt)
159    # Attention: "-N" prevents cmake from entering a recursion, and it must be a separate flag from "-L".
160    EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -N -L ${CMAKE_BINARY_DIR} OUTPUT_VARIABLE FEATURE_FLAGS)
161    FILE(APPEND ${INFO_BIN} ${FEATURE_FLAGS} "\n")
162  ELSE()
163    FILE(APPEND ${INFO_BIN} "File 'CMakeCache.txt' is not yet found.\n\n")
164  ENDIF()
165
166  FILE(APPEND ${INFO_BIN} "===== EOF =====\n")
167ENDMACRO(CREATE_INFO_BIN)
168
169