[Release] Webkit2-efl-123997_0.11.46
[framework/web/webkit-efl.git] / Source / cmake / FindBISON.cmake
1 # - Find bison executable and provides macros to generate custom build rules
2 # The module defines the following variables:
3 #
4 #  BISON_EXECUTABLE - path to the bison program
5 #  BISON_VERSION - version of bison
6 #  BISON_FOUND - true if the program was found
7 #
8 # If bison is found, the module defines the macros:
9 #  BISON_TARGET(<Name> <YaccInput> <CodeOutput> [VERBOSE <file>]
10 #              [COMPILE_FLAGS <string>])
11 # which will create  a custom rule to generate  a parser. <YaccInput> is
12 # the path to  a yacc file. <CodeOutput> is the name  of the source file
13 # generated by bison.  A header file is also  be generated, and contains
14 # the  token  list.  If  COMPILE_FLAGS  option is  specified,  the  next
15 # parameter is  added in the bison  command line.  if  VERBOSE option is
16 # specified, <file> is created  and contains verbose descriptions of the
17 # grammar and parser. The macro defines a set of variables:
18 #  BISON_${Name}_DEFINED - true is the macro ran successfully
19 #  BISON_${Name}_INPUT - The input source file, an alias for <YaccInput>
20 #  BISON_${Name}_OUTPUT_SOURCE - The source file generated by bison
21 #  BISON_${Name}_OUTPUT_HEADER - The header file generated by bison
22 #  BISON_${Name}_OUTPUTS - The sources files generated by bison
23 #  BISON_${Name}_COMPILE_FLAGS - Options used in the bison command line
24 #
25 #  ====================================================================
26 #  Example:
27 #
28 #   find_package(BISON)
29 #   BISON_TARGET(MyParser parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp)
30 #   add_executable(Foo main.cpp ${BISON_MyParser_OUTPUTS})
31 #  ====================================================================
32
33 #=============================================================================
34 # Copyright 2009 Kitware, Inc.
35 # Copyright 2006 Tristan Carel
36 #
37 # Distributed under the OSI-approved BSD License (the "License");
38 # see accompanying file Copyright.txt for details.
39 #
40 # This software is distributed WITHOUT ANY WARRANTY; without even the
41 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
42 # See the License for more information.
43 #=============================================================================
44 # (To distributed this file outside of CMake, substitute the full
45 #  License text for the above reference.)
46
47 FIND_PROGRAM(BISON_EXECUTABLE bison DOC "path to the bison executable")
48 MARK_AS_ADVANCED(BISON_EXECUTABLE)
49
50 IF(BISON_EXECUTABLE)
51
52   EXECUTE_PROCESS(COMMAND ${BISON_EXECUTABLE} --version
53     OUTPUT_VARIABLE BISON_version_output
54     ERROR_VARIABLE BISON_version_error
55     RESULT_VARIABLE BISON_version_result
56     OUTPUT_STRIP_TRAILING_WHITESPACE)
57   IF(NOT ${BISON_version_result} EQUAL 0)
58     MESSAGE(SEND_ERROR "Command \"${BISON_EXECUTABLE} --version\" failed with output:\n${BISON_version_error}")
59   ELSE()
60     STRING(REGEX REPLACE "^bison \\(GNU Bison\\) ([^\n]+)\n.*" "\\1"
61       BISON_VERSION "${BISON_version_output}")
62   ENDIF()
63
64   # internal macro
65   MACRO(BISON_TARGET_option_verbose Name BisonOutput filename)
66     LIST(APPEND BISON_TARGET_cmdopt "--verbose")
67     GET_FILENAME_COMPONENT(BISON_TARGET_output_path "${BisonOutput}" PATH)
68     GET_FILENAME_COMPONENT(BISON_TARGET_output_name "${BisonOutput}" NAME_WE)
69     ADD_CUSTOM_COMMAND(OUTPUT ${filename}
70       COMMAND ${CMAKE_COMMAND}
71       ARGS -E copy
72       "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output"
73       "${filename}"
74       DEPENDS
75       "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output"
76       COMMENT "[BISON][${Name}] Copying bison verbose table to ${filename}"
77       WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/Source)
78     SET(BISON_${Name}_VERBOSE_FILE ${filename})
79     LIST(APPEND BISON_TARGET_extraoutputs
80       "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output")
81   ENDMACRO(BISON_TARGET_option_verbose)
82
83   # internal macro
84   MACRO(BISON_TARGET_option_extraopts Options)
85     SET(BISON_TARGET_extraopts "${Options}")
86     SEPARATE_ARGUMENTS(BISON_TARGET_extraopts)
87     LIST(APPEND BISON_TARGET_cmdopt ${BISON_TARGET_extraopts})
88   ENDMACRO(BISON_TARGET_option_extraopts)
89
90   #============================================================
91   # BISON_TARGET (public macro)
92   #============================================================
93   #
94   MACRO(BISON_TARGET Name BisonInput BisonOutput)
95     SET(BISON_TARGET_output_header "")
96     SET(BISON_TARGET_command_opt "")
97     SET(BISON_TARGET_outputs "${BisonOutput}")
98     IF(NOT ${ARGC} EQUAL 3 AND NOT ${ARGC} EQUAL 5 AND NOT ${ARGC} EQUAL 7)
99       MESSAGE(SEND_ERROR "Usage")
100     ELSE()
101       # Parsing parameters
102       IF(${ARGC} GREATER 5 OR ${ARGC} EQUAL 5)
103         IF("${ARGV3}" STREQUAL "VERBOSE")
104           BISON_TARGET_option_verbose(${Name} ${BisonOutput} "${ARGV4}")
105         ENDIF()
106         IF("${ARGV3}" STREQUAL "COMPILE_FLAGS")
107           BISON_TARGET_option_extraopts("${ARGV4}")
108         ENDIF()
109       ENDIF()
110
111       IF(${ARGC} EQUAL 7)
112         IF("${ARGV5}" STREQUAL "VERBOSE")
113           BISON_TARGET_option_verbose(${Name} ${BisonOutput} "${ARGV6}")
114         ENDIF()
115
116         IF("${ARGV5}" STREQUAL "COMPILE_FLAGS")
117           BISON_TARGET_option_extraopts("${ARGV6}")
118         ENDIF()
119       ENDIF()
120
121       # Header's name generated by bison (see option -d)
122       LIST(APPEND BISON_TARGET_cmdopt "-d")
123       STRING(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\2" _fileext "${ARGV2}")
124       STRING(REPLACE "c" "h" _fileext ${_fileext})
125       STRING(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\1${_fileext}"
126           BISON_${Name}_OUTPUT_HEADER "${ARGV2}")
127       LIST(APPEND BISON_TARGET_outputs "${BISON_${Name}_OUTPUT_HEADER}")
128
129       ADD_CUSTOM_COMMAND(OUTPUT ${BISON_TARGET_outputs}
130         ${BISON_TARGET_extraoutputs}
131         COMMAND ${BISON_EXECUTABLE}
132         ARGS ${BISON_TARGET_cmdopt} -o ${ARGV2} ${ARGV1}
133         DEPENDS ${ARGV1}
134         COMMENT "[BISON][${Name}] Building parser with bison ${BISON_VERSION}"
135         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
136
137       # define target variables
138       SET(BISON_${Name}_DEFINED TRUE)
139       SET(BISON_${Name}_INPUT ${ARGV1})
140       SET(BISON_${Name}_OUTPUTS ${BISON_TARGET_outputs})
141       SET(BISON_${Name}_COMPILE_FLAGS ${BISON_TARGET_cmdopt})
142       SET(BISON_${Name}_OUTPUT_SOURCE "${BisonOutput}")
143
144     ENDIF(NOT ${ARGC} EQUAL 3 AND NOT ${ARGC} EQUAL 5 AND NOT ${ARGC} EQUAL 7)
145   ENDMACRO(BISON_TARGET)
146   #
147   #============================================================
148
149 ENDIF(BISON_EXECUTABLE)
150
151 INCLUDE(FindPackageHandleStandardArgs)
152 FIND_PACKAGE_HANDLE_STANDARD_ARGS(BISON DEFAULT_MSG BISON_EXECUTABLE)
153
154 # FindBISON.cmake ends here