[Cmake] Using CMake to generate flex and bison files

Andy Cedilnik andy . cedilnik at kitware . com
13 Sep 2002 12:24:47 -0400


Hello All, 

Just wanted to share this with you all. If you ever want to use CMake to
run flex and bison, here is an example on how to do that:

# Create target for the parser
 ADD_CUSTOM_TARGET(FooParser echo "Creating parser.c")

# Create custom command for flex/lex (note the outputs)
 ADD_CUSTOM_COMMAND(
   SOURCE ${Foo_SOURCE_DIR}/src/lexer.l
   COMMAND ${FLEX_EXECUTABLE} 
   ARGS -o${Foo_BINARY_DIR}/src/lexer.c
        ${Foo_SOURCE_DIR}/src/lexer.l
   TARGET FooParser
   OUTPUTS ${Foo_BINARY_DIR}/src/lexer.c)

# Create custom command for bison/yacc (note the DEPENDS)
 ADD_CUSTOM_COMMAND(
   SOURCE ${Foo_SOURCE_DIR}/src/parser.y
   COMMAND ${BISON_EXECUTABLE} 
   ARGS -y ${Foo_SOURCE_DIR}/src/parser.y
        -o ${Foo_BINARY_DIR}/src/parser.c
   TARGET FooParser
   DEPENDS ${Foo_BINARY_DIR}/src/lexer.c
   OUTPUTS ${Foo_BINARY_DIR}/src/parser.c)

# Add parser.c to the list of sources
 SET(Foo_SRCS ${Foo_SRCS} ${Foo_BINARY_DIR}/src/parser.c)

# Since parser.c does not exists yet when cmake is run, mark
# it as generated
 SET_SOURCE_FILES_PROPERTIES(${Foo_BINARY_DIR}/src/parser.c GENERATED)

# Include binary directory to include lexer.c in parser.c
 INCLUDE_DIRECTORIES(${Foo_BINARY_DIR}/src)

				Andy Cedilnik
				Kitware Inc.