我有这样的文件夹结构
include/
src/
| parser.yy
| scanner.ll
并在
src/CMakeLists.txt
:
SET(BisonOutput ${CMAKE_SOURCE_DIR}/src/_parser.cpp)
IF(BISON_FOUND)
ADD_CUSTOM_COMMAND(
OUTPUT ${BisonOutput}
COMMAND ${BISON_EXECUTABLE}
--defines=${CMAKE_SOURCE_DIR}/include/_parser.hpp
--output=${BisonOutput}
${CMAKE_SOURCE_DIR}/src/parser.yy
COMMENT "Generating parser"
)
ENDIF()
SET(FlexOutput ${CMAKE_SOURCE_DIR}/src/_scanner.cpp)
IF(FLEX_FOUND)
ADD_CUSTOM_COMMAND(
OUTPUT ${FlexOutput}
COMMAND ${FLEX_EXECUTABLE}
--outfile=${FlexOutput}
${CMAKE_SOURCE_DIR}/src/scanner.ll
COMMENT "Generating scanner"
)
ENDIF()
但是,文件locations.hh、position.hh 和stack.hh 是在
src
目录内生成的。是否可以以某种方式指定我希望在 include
目录中生成这些文件?
我正在使用 Bison 3.0.4 和 Flex 2.6.4
我不确定 Bison/Flex 是否允许您在单独的位置生成它们,但生成后您可以使用 CMake 将它们复制到
include
目录。使用 add_custom_command
添加另一个
PRE_BUILD
调用,以确保它们在构建目标之前被复制:
ADD_CUSTOM_COMMAND(
TARGET MyTarget PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/src/locations.hh ${CMAKE_SOURCE_DIR}/include
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/src/position.hh ${CMAKE_SOURCE_DIR}/include
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/src/stack.hh ${CMAKE_SOURCE_DIR}/include
)
CMake 确实添加了对 Flex 和 Bison 的支持。
这是一个例子:
...
find_package(BISON)
bison_target(name_of_my_parser
${CMAKE_CURRENT_SOURCE_DIR}/parser.ypp
${CMAKE_CURRENT_BINARY_DIR}/parser.cpp
DEFINES_FILE
${CMAKE_CURRENT_BINARY_DIR}/parser.hpp)
find_package(FLEX)
bison_target(name_of_my_lexer
${CMAKE_CURRENT_SOURCE_DIR}/lexer.l
${CMAKE_CURRENT_BINARY_DIR}/lexer.cpp
DEFINES_FILE
${CMAKE_CURRENT_BINARY_DIR}/lexer.h)
include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
add_executable(a_program
...#include other source files
${BISON_name_of_my_parser_OUTPUTS} # macro defined by cmake
${LEXER_name_of_my_lexer_OUTPUTS}) # macro defined by cmake
...
这假设
src/
| parser.ypp
| lexer.l