[CMake] cmake, lex & yacc

Gonzalo Garramuño ggarra at advancedsl.com.ar
Fri Dec 7 02:52:15 EST 2007


Timur Ivanov wrote:
> Hello!
> 
> Sorry if it was discussed already but I can't find anything really
> useful for me.
> 
> In current project I use lex & yacc and Makefile rules looks like:
> 

cmake currently does not ship with bison and flex modules, 
unfortunately.  Here's a couple of mine.

--------------- FindFlex.cmake
# - Try to find Flex
# Once done this will define
#
#  FLEX_FOUND - system has Flex
#  FLEX_EXECUTABLE - path of the flex executable
#  FLEX_VERSION - the version string, like "2.5.31"
#


FIND_PROGRAM(FLEX_EXECUTABLE NAMES flex flex.exe )

IF(FLEX_EXECUTABLE)
     SET(FLEX_FOUND TRUE)

     EXECUTE_PROCESS(COMMAND ${FLEX_EXECUTABLE} --version
         OUTPUT_VARIABLE _FLEX_VERSION
     )
     string (REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" FLEX_VERSION 
"${_FLEX_VERSION}")
ENDIF(FLEX_EXECUTABLE)

IF(FLEX_FOUND)
   IF(NOT Flex_FIND_QUIETLY)
     MESSAGE(STATUS "Found Flex: ${FLEX_EXECUTABLE}")
   ENDIF(NOT Flex_FIND_QUIETLY)
ELSE(FLEX_FOUND)
   IF(Flex_FIND_REQUIRED)
     MESSAGE(FATAL_ERROR "Could not find Flex")
   ENDIF(Flex_FIND_REQUIRED)
ENDIF(FLEX_FOUND)
---------------------------

-------------------- FindBison.cmake
# - Try to find Bison
# Once done this will define
#
#  BISON_FOUND - system has Bison
#  BISON_EXECUTABLE - path of the bison executable
#  BISON_VERSION - the version string, like "2.5.31"
#

FIND_PROGRAM(BISON_EXECUTABLE NAMES bison bison.exe )

IF(BISON_EXECUTABLE)
     SET(BISON_FOUND TRUE)

     EXECUTE_PROCESS(COMMAND ${BISON_EXECUTABLE} --version
         OUTPUT_VARIABLE _BISON_VERSION
     )
     string (REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" BISON_VERSION 
"${_BISON_VERSION}")
ENDIF(BISON_EXECUTABLE)

IF(BISON_FOUND)
   IF(NOT Bison_FIND_QUIETLY)
     MESSAGE(STATUS "Found Bison: ${BISON_EXECUTABLE}")
   ENDIF(NOT Bison_FIND_QUIETLY)
ELSE(BISON_FOUND)
   IF(Bison_FIND_REQUIRED)
     MESSAGE(FATAL_ERROR "Could not find Bison")
   ENDIF(Bison_FIND_REQUIRED)
ENDIF(BISON_FOUND)
--------------------------

----------- Sample usage


FIND_PACKAGE( Flex  REQUIRED)
FIND_PACKAGE( Bison REQUIRED)

#
# Create custom command for flex/lex (note the outputs)
#
ADD_CUSTOM_COMMAND(
    SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/grammar.lex"
    COMMAND ${FLEX_EXECUTABLE}
    ARGS -o"${CMAKE_CURRENT_BINARY_DIR}/lex.yy.c"
         -Pmrl_yy -8 -CFe "${CMAKE_CURRENT_SOURCE_DIR}/grammar.lex"
    TARGET miparser
    OUTPUTS "${CMAKE_CURRENT_BINARY_DIR}/lex.yy.c"
    )

#
# Create custom commands for bison/yacc (note the DEPENDS)
#
ADD_CUSTOM_COMMAND(
   SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/grammar.y"
   COMMAND ${BISON_EXECUTABLE}
   ARGS -l -pmrl_yy  -o"${CMAKE_CURRENT_BINARY_DIR}/grammar.tab.cpp"
              "${CMAKE_CURRENT_SOURCE_DIR}/grammar.y"
   TARGET miparser
   DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/lex.yy.c"
   OUTPUTS "${CMAKE_CURRENT_BINARY_DIR}/grammar.tab.cpp"
)



-- 
Gonzalo Garramuño
ggarra at advancedsl.com.ar

AMD4400 - ASUS48N-E
GeForce7300GT
Xubuntu Gutsy


More information about the CMake mailing list