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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
set(ANTLR_VERSION @ANTLR_VERSION@)
@PACKAGE_INIT@
if (NOT ANTLR4_CPP_GENERATED_SRC_DIR)
set(ANTLR4_GENERATED_SRC_DIR ${CMAKE_BINARY_DIR}/antlr4_generated_src)
endif()
FIND_PACKAGE(Java COMPONENTS Runtime REQUIRED)
#
# The ANTLR generator will output the following files given the input file f.g4
#
# Input -> f.g4
# Output -> f.h
# -> f.cpp
#
# the following files will only be produced if there is a parser contained
# Flag -visitor active
# Output -> <f>BaseVisitor.h
# -> <f>BaseVisitor.cpp
# -> <f>Visitor.h
# -> <f>Visitor.cpp
#
# Flag -listener active
# Output -> <f>BaseListener.h
# -> <f>BaseListener.cpp
# -> <f>Listener.h
# -> <f>Listener.cpp
#
# See documentation in markup
#
function(antlr4_generate
Antlr4_ProjectTarget
Antlr4_InputFile
Antlr4_GeneratorType
)
set( Antlr4_GeneratedSrcDir ${ANTLR4_GENERATED_SRC_DIR}/${Antlr4_ProjectTarget} )
get_filename_component(Antlr4_InputFileBaseName ${Antlr4_InputFile} NAME_WE )
list( APPEND Antlr4_GeneratorStatusMessage "Common Include-, Source- and Tokenfiles" )
if ( ${Antlr4_GeneratorType} STREQUAL "LEXER")
set(Antlr4_LexerBaseName "${Antlr4_InputFileBaseName}")
set(Antlr4_ParserBaseName "")
else()
if ( ${Antlr4_GeneratorType} STREQUAL "PARSER")
set(Antlr4_LexerBaseName "")
set(Antlr4_ParserBaseName "${Antlr4_InputFileBaseName}")
else()
if ( ${Antlr4_GeneratorType} STREQUAL "BOTH")
set(Antlr4_LexerBaseName "${Antlr4_InputFileBaseName}Lexer")
set(Antlr4_ParserBaseName "${Antlr4_InputFileBaseName}Parser")
else()
message(FATAL_ERROR "The third parameter must be LEXER, PARSER or BOTH")
endif ()
endif ()
endif ()
# Prepare list of generated targets
list( APPEND Antlr4_GeneratedTargets "${Antlr4_GeneratedSrcDir}/${Antlr4_InputFileBaseName}.tokens" )
list( APPEND Antlr4_GeneratedTargets "${Antlr4_GeneratedSrcDir}/${Antlr4_InputFileBaseName}.interp" )
list( APPEND DependentTargets "${Antlr4_GeneratedSrcDir}/${Antlr4_InputFileBaseName}.tokens" )
if ( NOT ${Antlr4_LexerBaseName} STREQUAL "" )
list( APPEND Antlr4_GeneratedTargets "${Antlr4_GeneratedSrcDir}/${Antlr4_LexerBaseName}.h" )
list( APPEND Antlr4_GeneratedTargets "${Antlr4_GeneratedSrcDir}/${Antlr4_LexerBaseName}.cpp" )
endif ()
if ( NOT ${Antlr4_ParserBaseName} STREQUAL "" )
list( APPEND Antlr4_GeneratedTargets "${Antlr4_GeneratedSrcDir}/${Antlr4_ParserBaseName}.h" )
list( APPEND Antlr4_GeneratedTargets "${Antlr4_GeneratedSrcDir}/${Antlr4_ParserBaseName}.cpp" )
endif ()
# process optional arguments ...
if ( ( ARGC GREATER_EQUAL 4 ) AND ARGV3 )
set(Antlr4_BuildListenerOption "-listener")
list( APPEND Antlr4_GeneratedTargets "${Antlr4_GeneratedSrcDir}/${Antlr4_InputFileBaseName}BaseListener.h" )
list( APPEND Antlr4_GeneratedTargets "${Antlr4_GeneratedSrcDir}/${Antlr4_InputFileBaseName}BaseListener.cpp" )
list( APPEND Antlr4_GeneratedTargets "${Antlr4_GeneratedSrcDir}/${Antlr4_InputFileBaseName}Listener.h" )
list( APPEND Antlr4_GeneratedTargets "${Antlr4_GeneratedSrcDir}/${Antlr4_InputFileBaseName}Listener.cpp" )
list( APPEND Antlr4_GeneratorStatusMessage ", Listener Include- and Sourcefiles" )
else()
set(Antlr4_BuildListenerOption "-no-listener")
endif ()
if ( ( ARGC GREATER_EQUAL 5 ) AND ARGV4 )
set(Antlr4_BuildVisitorOption "-visitor")
list( APPEND Antlr4_GeneratedTargets "${Antlr4_GeneratedSrcDir}/${Antlr4_InputFileBaseName}BaseVisitor.h" )
list( APPEND Antlr4_GeneratedTargets "${Antlr4_GeneratedSrcDir}/${Antlr4_InputFileBaseName}BaseVisitor.cpp" )
list( APPEND Antlr4_GeneratedTargets "${Antlr4_GeneratedSrcDir}/${Antlr4_InputFileBaseName}Visitor.h" )
list( APPEND Antlr4_GeneratedTargets "${Antlr4_GeneratedSrcDir}/${Antlr4_InputFileBaseName}Visitor.cpp" )
list( APPEND Antlr4_GeneratorStatusMessage ", Visitor Include- and Sourcefiles" )
else()
set(Antlr4_BuildVisitorOption "-no-visitor")
endif ()
if ( (ARGC GREATER_EQUAL 6 ) AND (NOT ${ARGV5} STREQUAL "") )
set(Antlr4_NamespaceOption "-package;${ARGV5}")
list( APPEND Antlr4_GeneratorStatusMessage " in Namespace ${ARGV5}" )
else()
set(Antlr4_NamespaceOption "")
endif ()
if ( (ARGC GREATER_EQUAL 7 ) AND (NOT ${ARGV6} STREQUAL "") )
set(Antlr4_AdditionalDependencies ${ARGV6})
else()
set(Antlr4_AdditionalDependencies "")
endif ()
if ( (ARGC GREATER_EQUAL 8 ) AND (NOT ${ARGV7} STREQUAL "") )
set(Antlr4_LibOption "-lib;${ARGV7}")
list( APPEND Antlr4_GeneratorStatusMessage " using Library ${ARGV7}" )
else()
set(Antlr4_LibOption "")
endif ()
if(NOT Java_FOUND)
message(FATAL_ERROR "Java is required to process grammar or lexer files! - Use 'FIND_PACKAGE(Java COMPONENTS Runtime REQUIRED)'")
endif()
if(NOT EXISTS "${ANTLR4_JAR_LOCATION}")
message(FATAL_ERROR "Unable to find antlr tool. ANTLR4_JAR_LOCATION:${ANTLR4_JAR_LOCATION}")
endif()
# The call to generate the files
add_custom_command(
OUTPUT ${Antlr4_GeneratedTargets}
# Remove target directory
COMMAND
${CMAKE_COMMAND} -E remove_directory ${Antlr4_GeneratedSrcDir}
# Create target directory
COMMAND
${CMAKE_COMMAND} -E make_directory ${Antlr4_GeneratedSrcDir}
COMMAND
# Generate files
"${Java_JAVA_EXECUTABLE}" -jar "${ANTLR4_JAR_LOCATION}" -Werror -Dlanguage=Cpp ${Antlr4_BuildListenerOption} ${Antlr4_BuildVisitorOption} ${Antlr4_LibOption} ${ANTLR4_GENERATED_OPTIONS} -o "${Antlr4_GeneratedSrcDir}" ${Antlr4_NamespaceOption} "${Antlr4_InputFile}"
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
MAIN_DEPENDENCY "${Antlr4_InputFile}"
DEPENDS ${Antlr4_AdditionalDependencies}
)
# set output variables in parent scope
set( ANTLR4_INCLUDE_DIR_${Antlr4_ProjectTarget} ${Antlr4_GeneratedSrcDir} PARENT_SCOPE)
set( ANTLR4_SRC_FILES_${Antlr4_ProjectTarget} ${Antlr4_GeneratedTargets} PARENT_SCOPE)
set( ANTLR4_TOKEN_FILES_${Antlr4_ProjectTarget} ${DependentTargets} PARENT_SCOPE)
set( ANTLR4_TOKEN_DIRECTORY_${Antlr4_ProjectTarget} ${Antlr4_GeneratedSrcDir} PARENT_SCOPE)
# export generated cpp files into list
foreach(generated_file ${Antlr4_GeneratedTargets})
if (NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
set_source_files_properties(
${generated_file}
PROPERTIES
COMPILE_FLAGS -Wno-overloaded-virtual
)
endif ()
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
set_source_files_properties(
${generated_file}
PROPERTIES
COMPILE_FLAGS -wd4251
)
endif ()
endforeach(generated_file)
message(STATUS "Antlr4 ${Antlr4_ProjectTarget} - Building " ${Antlr4_GeneratorStatusMessage} )
endfunction()
|