Generated lexer depended on parser, fixed
[kdevelop:the-user-kdevelop-pg-qt.git] / cmake / modules / FindKDevelop-PG-Qt.cmake
1 #
2 # Find the KDevelop Parser Generator and set various variables accordingly
3 #
4 # KDEVPGQT_DIR and can be set to adjust the directory where the kdevelop-pg-qt
5 # headers and executable are searched. This should point to the installation
6 # directory of kdevelop-pg-qt
7 #
8 # KDEVPGQT_FOUND - set to TRUE if KDevelop-PG was found FALSE otherwise
9 #
10 # KDEVPGQT_INCLUDE_DIR         - include dir of kdevelop-pg, for example /usr/include/kdevelop-pg
11 #                              can be set by the user to select different include dirs
12 # KDEVPGQT_EXECUTABLE          - the absolute path to the kdevelop-pg executable
13 #
14 # KDEVPGQT_GENERATE(SRC_FILE_VAR OUTPUT language
15 #                     [NAMESPACE ns] [DEBUG_VISITOR] [TOKEN_TEXT] [DUMP_INFO]
16 #                     GRAMMARFILE ADDITIONALDEPS)
17 #     macro to add a custom target for the generation of the parser
18 #     OUTPUT will be given to kdev-pg as the --output parameter and thus sets the filename prefix
19 #     NAMESPACE can be given to choose a namespace different from the OUTPUT value
20 #     DEBUG_VISITOR will run kdevelop-pg with the --debug-visitor argument to generate a simple
21 #                   visitor that will print debug messages
22 #     TOKEN_TEXT will run kdevelop-pg with the --token-text argument to generate a simple
23 #                function that returns a readable name of a token
24 #     DUMP_INFO will tell kdevelop-pg to dump extra information about symbols, terminals and rules
25 #               into files in the binary dir
26 #     Note: The macro only exists when KDEVPG was found
27 #
28 # Copyright (c) 2007 Andreas Pakulat <apaku@gmx.de>
29 # Redistribution and use is allowed according to the terms of the BSD license.
30 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
31
32 if( KDEVPGQT_INCLUDE_DIR AND KDEVPGQT_EXECUTABLE )
33     set(KDevelop-PG-Qt_FIND_QUIETLY TRUE)
34 endif( KDEVPGQT_INCLUDE_DIR AND KDEVPGQT_EXECUTABLE )
35
36 if(WIN32)
37     file(TO_CMAKE_PATH "$ENV{PROGRAMFILES}" _progFiles)
38     set(_KDEVPGQT_DIR ${_progFiles}/kdevelop-pg-qt)
39 endif(WIN32)
40
41 if( NOT KDEVPGQT_DIR )
42     find_path( _kdevpgqtIncDir kdevelop-pg-qt/kdev-pg-token-stream.h
43         PATHS
44         ${CMAKE_INSTALL_PREFIX}/include
45         ${_KDEVPGQT_DIR}/include
46     )
47     if( _kdevpgqtIncDir )
48         set(KDEVPGQT_INCLUDE_DIR ${_kdevpgqtIncDir}/kdevelop-pg-qt)
49     endif( _kdevpgqtIncDir )
50     find_program( KDEVPGQT_EXECUTABLE NAMES kdev-pg-qt
51         PATHS
52         ${CMAKE_INSTALL_PREFIX}/bin
53         ${_KDEVPGQT_DIR}/bin
54     )
55
56     set(KDEVPGQT_INCLUDE_DIR ${KDEVPGQT_INCLUDE_DIR} CACHE PATH "kdevelop-pg-qt include directory containing the headers")
57     set( KDEVPGQT_EXECUTABLE ${KDEVPGQT_EXECUTABLE} CACHE PATH "executable for kdevelop-pg-qt" )
58 else( NOT KDEVPGQT_DIR )
59     find_path( _kdevpgqtIncDir kdevelop-pg-qt/kdev-pg-token-stream.h
60         PATHS
61         ${CMAKE_INSTALL_PREFIX}/include
62         ${_KDEVPGQT_DIR}/include
63         ${KDEVPGQT_DIR}/include
64     )
65     set(KDEVPGQT_INCLUDE_DIR ${_kdevpgIncDir}/kdevelop-pg-qt)
66     find_program( KDEVPGQT_EXECUTABLE NAMES kdev-pg-qt
67         PATHS
68         ${CMAKE_INSTALL_PREFIX}/bin
69         ${_KDEVPGQT_DIR}/bin
70         ${KDEVPGQT_DIR}/bin
71     )
72     set(KDEVPGQT_INCLUDE_DIR ${KDEVPGQT_INCLUDE_DIR} CACHE PATH "kdevelop-pg-qt include directory containing the headers")
73     set( KDEVPGQT_EXECUTABLE ${KDEVPGQT_EXECUTABLE} CACHE PATH "executable for kdevelop-pg-qt" )
74 endif( NOT KDEVPGQT_DIR )
75 if( KDEVPGQT_INCLUDE_DIR
76  AND KDEVPGQT_EXECUTABLE)
77
78     if( NOT KDevelop-PG-Qt_FIND_QUIETLY )
79         message(STATUS "Found KDevelop-PG-Qt")
80     endif( NOT KDevelop-PG-Qt_FIND_QUIETLY )
81
82 # if all modules found
83     set(KDEVPGQT_FOUND TRUE)
84
85     macro(KDEVPGQT_GENERATE _srcVar _language )
86         set(_outputList
87             "${CMAKE_CURRENT_BINARY_DIR}/${_language}tokentype.h")
88         set(_depList ${ARGN})
89         set(_astList
90             "${CMAKE_CURRENT_BINARY_DIR}/${_language}ast.h")
91         set(_parserList
92             "${CMAKE_CURRENT_BINARY_DIR}/${_language}parser.h"
93             "${CMAKE_CURRENT_BINARY_DIR}/${_language}parser.cpp"
94             "${CMAKE_CURRENT_BINARY_DIR}/${_language}visitor.h"
95             "${CMAKE_CURRENT_BINARY_DIR}/${_language}visitor.cpp"
96             "${CMAKE_CURRENT_BINARY_DIR}/${_language}defaultvisitor.h"
97             "${CMAKE_CURRENT_BINARY_DIR}/${_language}defaultvisitor.cpp")
98         set(_lexerList)
99         set(_dbgVisit)
100         set(_namespace)
101         set(_tokenText)
102         set(_dumpInfo)
103         if(CMAKE_COMPILER_IS_GNUCC)
104           set(_beautifulCode)
105         else(CMAKE_COMPILER_IS_GNUCC)
106           set(_beautifulCode --compatible-error-aware-code)
107         endif(CMAKE_COMPILER_IS_GNUCC)
108         set(_conflicts)
109         while(1)
110         list(GET _depList 0 _arg)
111         if( ${_arg} STREQUAL "NAMESPACE" )
112             list(GET _depList 1 _namespace)
113             list(REMOVE_AT _depList 0 1)
114             set(_namespace --namespace=${_namespace})
115         elseif( ${_arg} STREQUAL "DEBUG_VISITOR" )
116             list(REMOVE_AT _depList 0)
117             set(_dbgVisit "--debug-visitor")
118             set(_outputList ${_outputList}
119                 "${CMAKE_CURRENT_BINARY_DIR}/${_language}debugvisitor.h"
120             )
121         elseif( ${_arg} STREQUAL "TOKEN_TEXT" )
122             list(REMOVE_AT _depList 0)
123             set(_tokenText "--token-text")
124             set(_outputList ${_outputList}
125                 "${CMAKE_CURRENT_BINARY_DIR}/${_language}tokentext.h"
126             )
127         elseif( ${_arg} STREQUAL "DUMP_INFO" )
128             list(REMOVE_AT _depList 0)
129             set(_dumpInfo --terminals --symbols --rules)
130             set(_outputList ${_outputList}
131                 "${CMAKE_CURRENT_BINARY_DIR}/kdev-pg-terminals"
132                 "${CMAKE_CURRENT_BINARY_DIR}/kdev-pg-symbols"
133                 "${CMAKE_CURRENT_BINARY_DIR}/kdev-pg-rules"
134             )
135         elseif( ${_arg} STREQUAL "BEAUTIFUL_CODE" )
136             list(REMOVE_AT _depList 0)
137             set(_beautifulCode --beautiful-code)
138         elseif( ${_arg} STREQUAL "STRICT_CONFLICTS" )
139             list(REMOVE_AT _depList 0)
140             set(_conflicts --strict-conflicts)
141         elseif( ${_arg} STREQUAL "IGNORE_CONFLICTS" )
142             list(REMOVE_AT _depList 0)
143             set(_conflicts --ignore-conflicts)
144         elseif( ${_arg} STREQUAL "GENERATE_LEXER" )
145           list(REMOVE_AT _depList 0)
146           set(_lexerList
147               "${CMAKE_CURRENT_BINARY_DIR}/${_language}lexer.h"
148               "${CMAKE_CURRENT_BINARY_DIR}/${_language}lexer.cpp")
149         else( ${_arg} STREQUAL "IGNORE_CONFLICTS" )
150             break()
151         endif( ${_arg} STREQUAL "NAMESPACE" )
152         endwhile(1)
153         
154         set(_outputList ${_outputList} ${_lexerList} ${_astList} ${_parserList})
155         
156         list(GET _depList 0 _grammarFile)
157         list(REMOVE_AT _depList 0)
158         if(NOT _grammarFile)
159             message(ERROR "No grammar file given to KDEVPGQT_GENERATE macro")
160         endif(NOT _grammarFile)
161         add_custom_command(
162             OUTPUT
163                     ${_outputList}
164             MAIN_DEPENDENCY "${_grammarFile}"
165             DEPENDS ${_depList} ${KDEVPGQT_EXECUTABLE}
166             COMMAND ${KDEVPGQT_EXECUTABLE}
167             ARGS    --output=${_language} ${_namespace}
168                     ${_dbgVisit} ${_dumpInfo} ${_beautifulCode} ${_conflicts} "${_grammarFile}"
169             WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
170         )
171         set_source_files_properties("${_outputList}" GENERATED)
172         set( ${_srcVar}
173               "${_outputList}"
174            )
175     endmacro(KDEVPGQT_GENERATE)
176
177
178 else( KDEVPGQT_INCLUDE_DIR
179  AND KDEVPGQT_EXECUTABLE)
180
181     if( KDevelop-PG-Qt_FIND_REQUIRED)
182         message(FATAL_ERROR "Couldn't find KDevelop-PG-Qt.")
183     else( KDevelop-PG-Qt_FIND_REQUIRED)
184         message(STATUS "Couldn't find KDevelop-PG-Qt.")
185     endif( KDevelop-PG-Qt_FIND_REQUIRED)
186     message(STATUS "You can set KDEVPGQT_DIR to help cmake find KDevelop-PG-Qt")
187     set(KDEVPGQT_FOUND FALSE)
188
189 endif( KDEVPGQT_INCLUDE_DIR
190  AND KDEVPGQT_EXECUTABLE)
191
192 if(KDEVPGQT_FOUND)
193 message(STATUS "Using kdevelop-pg-qt include dir: ${KDEVPGQT_INCLUDE_DIR}")
194 message(STATUS "Using kdevelop-pg-qt executable: ${KDEVPGQT_EXECUTABLE}")
195 endif(KDEVPGQT_FOUND)
196
197