forked from clever-lang/clever
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindRE2C.cmake
137 lines (123 loc) · 6.32 KB
/
FindRE2C.cmake
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
# - Find re2c executable and provides macros to generate custom build rules
# The module defines the following variables:
# RE2C_EXECUTABLE - path to the re2c program
# RE2C_VERSION - version of re2c
# RE2C_FOUND - true if the program was found
# If re2c is found, the module defines the macros:
# RE2C_TARGET(<Name> <Re2cInput> <CodeOutput> [COMPILE_FLAGS <string>])
# which will create a custom rule to generate a lexer. <Re2cInput> is
# the path to a .re file. <CodeOutput> is the name of the source file
# generated by re2c. A header file is also be generated, and contains
# the token list. If COMPILE_FLAGS option is specified, the next
# parameter is added in the re2c command line. The macro defines a set
# of variables:
# RE2C_${Name}_DEFINED - true is the macro ran successfully
# RE2C_${Name}_INPUT - The input source file, an alias for <YaccInput>
# RE2C_${Name}_OUTPUT_SOURCE - The source file generated by re2c
# RE2C_${Name}_OUTPUT_HEADER - The header file generated by re2c
# RE2C_${Name}_OUTPUTS - The sources files generated by re2c
# RE2C_${Name}_COMPILE_FLAGS - Options used in the re2c command line
#
# re2c scanners oftenly use tokens defined by Bison: the code generated
# by re2c depends of the header generated by Bison. This module also
# defines a macro:
# ADD_RE2C_BISON_DEPENDENCY(Re2cTarget BisonTarget)
# which adds the required dependency between a scanner and a parser
# where <Re2cTarget> and <BisonTarget> are the first parameters of
# respectively RE2C_TARGET and BISON_TARGET macros.
#
# Example:
# FIND_PACKAGE(RE2C)
# BISON_TARGET(MyParser parser.y ${PROJECT_BINARY_DIR}/parser.cpp)
# RE2C_TARGET(MyScanner lexer.l ${PROJECT_BINARY_DIR}/lexer.cpp)
# ADD_RE2C_BISON_DEPENDENCY(MyScanner MyParser)
# ADD_EXECUTABLE(Foo main.cpp ${BISON_MyParser_OUTPUTS} ${RE2C_MyScanner_OUTPUTS})
#
# Copyright (c) 2011, Higor Eurípedes
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the University of California, Berkeley nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
FIND_PROGRAM(RE2C_EXECUTABLE re2c DOC "path to the re2c executable")
MARK_AS_ADVANCED(RE2C_EXECUTABLE)
IF(RE2C_EXECUTABLE)
EXECUTE_PROCESS(COMMAND ${RE2C_EXECUTABLE} --version
OUTPUT_VARIABLE RE2C_version_output
ERROR_VARIABLE RE2C_version_error
RESULT_VARIABLE RE2C_version_result
OUTPUT_STRIP_TRAILING_WHITESPACE)
IF(NOT ${RE2C_version_result} EQUAL 2)
MESSAGE(SEND_ERROR "Command \"${RE2C_EXECUTABLE} --version\" failed with output:\n${RE2C_version_error}")
ELSE(NOT ${RE2C_version_result} EQUAL 2)
STRING(REGEX REPLACE "^re2c ([^\n]+)$" "\\1"
RE2C_VERSION "${RE2C_version_output}")
ENDIF(NOT ${RE2C_version_result} EQUAL 2)
MACRO(RE2C_TARGET Name Re2cInput Re2cOutput)
SET(RE2C_TARGET_output_header "")
SET(RE2C_TARGET_command_opt "")
SET(RE2C_TARGET_outputs "${Re2cOutput}")
IF(NOT ${ARGC} EQUAL 3 AND NOT ${ARGC} EQUAL 5)
MESSAGE(SEND_ERROR "Usage RE2C_TARGET(<Name> <Re2cInput> <CodeOutput> [COMPILE_FLAGS <string>])
")
ELSE(NOT ${ARGC} EQUAL 3 AND NOT ${ARGC} EQUAL 5)
# Parsing parameters
IF(${ARGC} EQUAL 5)
IF("${ARGV3}" STREQUAL "COMPILE_FLAGS")
SET(RE2C_TARGET_extraopts "${ARGV4}")
SEPARATE_ARGUMENTS(RE2C_TARGET_extraopts)
LIST(APPEND RE2C_TARGET_cmdopt ${RE2C_TARGET_extraopts})
ENDIF("${ARGV3}" STREQUAL "COMPILE_FLAGS")
ENDIF(${ARGC} EQUAL 5)
# Header's name generated by re2c (see option -d)
#LIST(APPEND RE2C_TARGET_cmdopt "-d")
#STRING(REGEX REPLACE "^(.*)\\.(.{1-3})$" "\\1.h\\2"
# RE2C_${Name}_OUTPUT_HEADER "${ARGV2}")
#LIST(APPEND RE2C_TARGET_outputs "${RE2C_${Name}_OUTPUT_HEADER}")
ADD_CUSTOM_COMMAND(OUTPUT ${RE2C_TARGET_outputs}
${RE2C_TARGET_extraoutputs}
COMMAND ${RE2C_EXECUTABLE} ${RE2C_TARGET_cmdopt} -o${ARGV2} ${ARGV1}
DEPENDS ${ARGV1}
COMMENT "[RE2C][${Name}] Building scanner with re2c ${RE2C_VERSION}"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
# define target variables
SET(RE2C_${Name}_DEFINED TRUE)
SET(RE2C_${Name}_INPUT ${ARGV1})
SET(RE2C_${Name}_OUTPUTS ${RE2C_TARGET_outputs})
SET(RE2C_${Name}_COMPILE_FLAGS ${RE2C_TARGET_cmdopt})
SET(RE2C_${Name}_OUTPUT_SOURCE "${Re2cOutput}")
ENDIF(NOT ${ARGC} EQUAL 3 AND NOT ${ARGC} EQUAL 5)
ENDMACRO(RE2C_TARGET)
MACRO(ADD_RE2C_BISON_DEPENDENCY Re2cTarget BisonTarget)
IF(NOT RE2C_${Re2cTarget}_OUTPUTS)
MESSAGE(SEND_ERROR "Re2c target `${Re2cTarget}' does not exists.")
ENDIF(NOT RE2C_${Re2cTarget}_OUTPUTS)
IF(NOT BISON_${BisonTarget}_OUTPUTS)
MESSAGE(SEND_ERROR "Bison target `${BisonTarget}' does not exists.")
ENDIF(NOT BISON_${BisonTarget}_OUTPUTS)
SET_SOURCE_FILES_PROPERTIES(${RE2C_${Re2cTarget}_OUTPUTS}
PROPERTIES OBJECT_DEPENDS ${BISON_${BisonTarget}_OUTPUT_HEADER})
ENDMACRO(ADD_RE2C_BISON_DEPENDENCY)
ENDIF(RE2C_EXECUTABLE)
#INCLUDE(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(RE2C REQUIRED_VARS RE2C_EXECUTABLE
VERSION_VAR RE2C_VERSION)
# FindRE2C.cmake ends here