1# Copyright (c) 2009, 2018, 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
23FIND_PROGRAM(BISON_EXECUTABLE bison DOC "path to the bison executable")
24MARK_AS_ADVANCED(BISON_EXECUTABLE "")
25
26IF(NOT BISON_EXECUTABLE)
27  MESSAGE(WARNING "Bison executable not found in PATH")
28ELSEIF(BISON_EXECUTABLE AND NOT BISON_USABLE)
29  # Check version as well
30  EXEC_PROGRAM(${BISON_EXECUTABLE} ARGS --version OUTPUT_VARIABLE BISON_VERSION_STR)
31   # Get first line in case it's multiline
32   STRING(REGEX REPLACE "([^\n]+).*" "\\1" FIRST_LINE "${BISON_VERSION_STR}")
33   # get version information
34   STRING(REGEX REPLACE ".* ([0-9]+)\\.([0-9]+)" "\\1" BISON_VERSION_MAJOR "${FIRST_LINE}")
35   STRING(REGEX REPLACE ".* ([0-9]+)\\.([0-9]+)" "\\2" BISON_VERSION_MINOR "${FIRST_LINE}")
36   IF (BISON_VERSION_MAJOR LESS 2)
37     MESSAGE(WARNING "Bison version is old. please update to version 2")
38   ELSE()
39     SET(BISON_USABLE 1 CACHE INTERNAL "Bison version 2 or higher")
40   ENDIF()
41ENDIF()
42
43
44# Handle out-of-source build from source package with possibly broken
45# bison. Copy bison output to from source to build directory, if not already
46# there
47MACRO(COPY_BISON_OUTPUT input_cc input_h output_cc output_h)
48  IF(EXISTS ${input_cc} AND NOT EXISTS ${output_cc})
49    CONFIGURE_FILE(${input_cc} ${output_cc} COPYONLY)
50    CONFIGURE_FILE(${input_h}  ${output_h}  COPYONLY)
51  ENDIF()
52ENDMACRO()
53
54
55# Use bison to generate C++ and header file
56MACRO (RUN_BISON input_yy output_cc output_h)
57  IF(BISON_USABLE)
58    ADD_CUSTOM_COMMAND(
59      OUTPUT ${output_cc}
60             ${output_h}
61      COMMAND ${BISON_EXECUTABLE}  -y -p MYSQL
62       --output=${output_cc}
63       --defines=${output_h}
64        ${input_yy}
65        DEPENDS ${input_yy}
66	)
67  ELSE()
68    # Bison is missing or not usable, e.g too old
69    IF(EXISTS  ${output_cc} AND EXISTS ${output_h})
70      IF(${input_yy} IS_NEWER_THAN ${output_cc}  OR  ${input_yy} IS_NEWER_THAN ${output_h})
71        # Possibly timestamps are messed up in source distribution.
72        MESSAGE(WARNING "No usable bison found, ${input_yy} will not be rebuilt.")
73      ENDIF()
74    ELSE()
75      # Output files are missing, bail out.
76      SET(ERRMSG
77         "Bison (GNU parser generator) is required to build MySQL."
78         "Please install bison."
79      )
80      IF(WIN32)
81       SET(ERRMSG ${ERRMSG}
82       "You can download bison from http://gnuwin32.sourceforge.net/packages/bison.htm "
83       "Choose 'Complete package, except sources' installation. We recommend to "
84       "install bison into a directory without spaces, e.g C:\\GnuWin32.")
85      ENDIF()
86      MESSAGE(FATAL_ERROR ${ERRMSG})
87    ENDIF()
88  ENDIF()
89ENDMACRO()
90