简体   繁体   English

CMake 和 Flex/Bison

[英]CMake and Flex/Bison

I am converting my build system from configure/make to a cmake system我正在将我的构建系统从 configure/make 转换为 cmake 系统

The system has some autogenerated files, from bison/flex.系统有一些自动生成的文件,来自 bison/flex。 The original makefile commands are:原始的 makefile 命令是:

bison --defines=tokens.h --output=parser.cpp parser.y
flex --outfile=scanner.cpp scanner.l

I came acrossthis ancient link which seems to explain how to do it, but when i run cmake with the following custom commands, nothing appears to happen (no error messages, no file generation)我遇到了这个古老的链接,它似乎解释了如何做,但是当我使用以下自定义命令运行 cmake 时,似乎没有任何反应(没有错误消息,没有文件生成)

FIND_PACKAGE(BISON REQUIRED)
IF(BISON_FOUND)
    ADD_CUSTOM_COMMAND(
      SOURCE ${CMAKE_SOURCE_DIR}/src/rcdgen/parser.y
      COMMAND ${BISON_EXECUTABLE}
      ARGS --defines=${CMAKE_SOURCE_DIR}/src/rcdgen/tokens.h
           -o ${CMAKE_SOURCE_DIR}/src/rcdgen/parser.cpp
           ${CMAKE_SOURCE_DIR}/src/rcdgen/parser.y
      COMMENT "Generating parser.cpp"
      OUTPUT ${CMAKE_SOURCE_DIR}/src/rcdgen/parser.cpp
    )
ENDIF(BISON_FOUND)

FIND_PACKAGE(FLEX REQUIRED)
IF(FLEX_FOUND)
    ADD_CUSTOM_COMMAND(
      SOURCE ${CMAKE_SOURCE_DIR}/src/rcdgen/scanner.l
      COMMAND ${FLEX_EXECUTABLE}
      ARGS -o${CMAKE_SOURCE_DIR}/src/rcdgen/parser.cpp
           ${CMAKE_SOURCE_DIR}/src/rcdgen/scanner.l
      COMMENT "Generating scanner.cpp"
      OUTPUT ${CMAKE_SOURCE_DIR}/src/rcdgen/scanner.cpp
    )
ENDIF(FLEX_FOUND)

I am new to cmake, so it's a bit confusing to me.我是 cmake 的新手,所以对我来说有点困惑。 Does anyone have any idea what a working custom_command would be?有谁知道一个有效的 custom_command 是什么?

The new hotness for bison usage is actually documented at cmake.org So for a simple parser project: 野牛使用的新热点实际上记录在cmake.org上。所以对于一个简单的解析器项目:

bison_target(parser fl.ypp fl.tab.cpp)
add_executable(fl ${BISON_parser_OUTPUTS})

is what you'd do. 是你要做的。 Likewise for Flex. 同样适用于Flex。

The format of your add_custom_command s is not quite right, but they appear to be almost correct. add_custom_command的格式不太正确,但它们看起来几乎是正确的。 There are two versions of add_custom_command , and the one you want is the one which produces an output file (the parts inside square brackets are optional): add_custom_command有两个版本,你想要的是生成输出文件的那个(方括号内的部分是可选的):

add_custom_command(OUTPUT output1 [output2 ...]
                   COMMAND command1 [ARGS] [args1...]
                   [COMMAND command2 [ARGS] [args2...] ...]
                   [MAIN_DEPENDENCY depend]
                   [DEPENDS [depends...]]
                   [IMPLICIT_DEPENDS <lang1> depend1
                                    [<lang2> depend2] ...]
                   [WORKING_DIRECTORY dir]
                   [COMMENT comment] [VERBATIM] [APPEND])

The idea is that the custom command only executes if the file specified as the OUTPUT of this command is used as an input elsewhere in the same CMakeLists.txt (eg in an add_library or add_executable call). 我们的想法是,只有当指定为此命令的OUTPUT的文件用作同一CMakeLists.txt中其他位置的输入时(例如,在add_libraryadd_executable调用中),才会执行自定义命令。

The custom command therefore will only run at build time (ie when you run make ), not at configure time (when you run CMake), and only if you're building a target which directly or indirectly needs the OUTPUT file. 因此,自定义命令仅在构建时运行(即运行make ),而不是在配置时(运行CMake时),并且仅在您构建直接或间接需要OUTPUT文件的目标时运行。

To fix your commands, I think the following should work (untested): 要修复命令,我认为以下内容应该有效(未经测试):

FIND_PACKAGE(BISON REQUIRED)
SET(BisonOutput ${CMAKE_SOURCE_DIR}/src/rcdgen/parser.cpp)
IF(BISON_FOUND)
    ADD_CUSTOM_COMMAND(
      OUTPUT ${BisonOutput}
      COMMAND ${BISON_EXECUTABLE}
              --defines=${CMAKE_SOURCE_DIR}/src/rcdgen/tokens.h
              --output=${BisonOutput}
              ${CMAKE_SOURCE_DIR}/src/rcdgen/parser.y
      COMMENT "Generating parser.cpp"
    )
ENDIF()

FIND_PACKAGE(FLEX REQUIRED)
SET(FlexOutput ${CMAKE_SOURCE_DIR}/src/rcdgen/scanner.cpp)
IF(FLEX_FOUND)
    ADD_CUSTOM_COMMAND(
      OUTPUT ${FlexOutput}
      COMMAND ${FLEX_EXECUTABLE}
              --outfile=${FlexOutput}
              ${CMAKE_SOURCE_DIR}/src/rcdgen/scanner.l
      COMMENT "Generating scanner.cpp"
    )
ENDIF()

ADD_LIBRARY(MyLib ${BisonOutput} ${FlexOutput})

要在ubuntu系统上启用 flex 和 bison:

apt-get install flex bison

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM