summaryrefslogtreecommitdiffstats
path: root/BisonMacros.cmake
blob: d6dcb19e8e425a4c089362ed191cf9d280961e5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# /*
#   For more information, please see: http://software.sci.utah.edu

#   The MIT License

#   Copyright (c) 2005-2006
#   Scientific Computing and Imaging Institute, University of Utah

#   License for the specific language governing rights and limitations under
#   Permission is hereby granted, free of charge, to any person obtaining a
#   copy of this software and associated documentation files (the "Software"),
#   to deal in the Software without restriction, including without limitation
#   the rights to use, copy, modify, merge, publish, distribute, sublicense,
#   and/or sell copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following conditions:

#   The above copyright notice and this permission notice shall be included
#   in all copies or substantial portions of the Software.

#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
#   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#   DEALINGS IN THE SOFTWARE.
# */

SET(PARSERS_FOUND FOOBAR)

# These variables need to be specified in order to get CMake not to
# barf on the IF(EXISTS ${BISON_EXECUTABLE} ..) expression even though
# the code shouldn't get called.  By setting them to BISON_EXECUTABLE

SET(BISON_EXECUTABLE "BISON_EXECUTABLE-NOTFOUND" CACHE FILEPATH "bison executable")
SET(FLEX_EXECUTABLE "FLEX_EXECUTABLE-NOTFOUND" CACHE FILEPATH "flex executable")
# Mark these variables as advanced options
MARK_AS_ADVANCED(FORCE BISON_EXECUTABLE)
MARK_AS_ADVANCED(FORCE FLEX_EXECUTABLE)

# You need at least version 2.4 for this to work.
IF("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 2.4)
  MESSAGE("You need at least version 2.4 for generating flex and bison parsers.  Go get it from http://www.cmake.org/HTML/Download.html")
  SET(PARSERS_FOUND 0)

ELSE("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 2.4)

  FIND_PROGRAM(BISON_EXECUTABLE
    NAMES bison
    PATHS ${BISON_DIR} )

  FIND_PROGRAM(FLEX_EXECUTABLE
    NAMES flex
    PATHS ${FLEX_DIR} )

  IF(EXISTS ${BISON_EXECUTABLE} AND EXISTS ${FLEX_EXECUTABLE})
    SET(PARSERS_FOUND 1)

  ELSE(EXISTS ${BISON_EXECUTABLE} AND EXISTS ${FLEX_EXECUTABLE})
    SET(PARSERS_FOUND 0)
    # Print some error messages to the user
    IF (NOT EXISTS ${BISON_EXECUTABLE})
      MESSAGE("Couldn't find bison executable.  Please check value in BISON_EXECUTABLE in advanced settings.")
    ENDIF (NOT EXISTS ${BISON_EXECUTABLE})
    IF (NOT EXISTS ${FLEX_EXECUTABLE})
      MESSAGE("Couldn't find flex executable.  Please check value in FLEX_EXECUTABLE in advanced settings.")
    ENDIF (NOT EXISTS ${FLEX_EXECUTABLE})

  ENDIF(EXISTS ${BISON_EXECUTABLE} AND EXISTS ${FLEX_EXECUTABLE})

ENDIF("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 2.4)

# These are helper functions for parsers.

# parser is the parser file name like parser.y
# lexer is like lexer.l

# The names of the output files will be based on the input names.
# BF_SOURCES will be parser.cc, parser.h and lexer.cc.

MACRO(GENERATE_BISON_FLEX_SOURCES parser parser_args
                                  lexer  lexer_args)
  GET_FILENAME_COMPONENT(parser_base "${parser}" NAME_WE)

  SET(BISON_TAB_C "${CMAKE_CURRENT_BINARY_DIR}/${parser_base}.tab.c")
  SET(BISON_TAB_H "${CMAKE_CURRENT_BINARY_DIR}/${parser_base}.tab.h")
  SET(BISON_CC    "${CMAKE_CURRENT_BINARY_DIR}/${parser_base}.cc")
  SET(BISON_H     "${CMAKE_CURRENT_BINARY_DIR}/${parser_base}.h")

  ADD_CUSTOM_COMMAND(
    OUTPUT ${BISON_TAB_C} ${BISON_TAB_H}
    COMMAND ${BISON_EXECUTABLE}
    ARGS "${parser}" ${parser_args} "--defines"
    DEPENDS "${parser}"
    COMMENT "Generating ${BISON_TAB_C} ${BISON_TAB_H} from ${parser}"
    )

  ADD_CUSTOM_COMMAND(
    OUTPUT ${BISON_CC}
    COMMAND      ${CMAKE_COMMAND}
    ARGS -E copy ${BISON_TAB_C} ${BISON_CC}
    DEPENDS ${BISON_TAB_C}
    COMMENT "Copying ${BISON_TAB_C} to ${BISON_CC}"
    )

  ADD_CUSTOM_COMMAND(
    OUTPUT ${BISON_H}
    COMMAND      ${CMAKE_COMMAND}
    ARGS -E copy ${BISON_TAB_H} ${BISON_H}
    DEPENDS ${BISON_TAB_H}
    COMMENT "Copying ${BISON_TAB_H} to ${BISON_H}"
    )

  GET_FILENAME_COMPONENT(lexer_base "${lexer}" NAME_WE)
  SET(FLEX_C "${CMAKE_CURRENT_BINARY_DIR}/lex.yy.c")
  SET(FLEX_CC "${CMAKE_CURRENT_BINARY_DIR}/${lexer_base}.cc")
  
  ADD_CUSTOM_COMMAND(
    OUTPUT ${FLEX_C}
    COMMAND ${FLEX_EXECUTABLE}
    ARGS "${lexer}" ${lexer_args}
    DEPENDS "${lexer}" ${BISON_H}
    COMMENT "Generating ${FLEX_C} from ${lexer}"
    )

  ADD_CUSTOM_COMMAND(
    OUTPUT ${FLEX_CC}
    COMMAND      ${CMAKE_COMMAND}
    ARGS -E copy ${FLEX_C} ${FLEX_CC}
    DEPENDS ${FLEX_C}
    COMMENT "Copying ${FLEX_C} to ${FLEX_CC}"
    )

  SET(BF_SOURCES ${BISON_CC} ${BISON_H} ${FLEX_CC})
  
ENDMACRO(GENERATE_BISON_FLEX_SOURCES)