November 22, 2009

How to use flex and bison with qmake (my own way)

by orzel
Categories: Gentoo, KDE
Tags: , , ,
Comments: 7 Comments

qmake (sort of) supports lex/yacc, but modern people prefer to use flex/bison. If you google, you can find some tricks like convincing qmake that yacc is spelled ‘bison’ and so on.  Morever qmake supports for lex/yacc has several drawbacks, such as weird naming scheme, and (when using the tricks), dependencies are not building files in the right order.

Finally, I created my own *.pri files, and here they are:

% cat flex.pri
[plain]
flex.name = Flex ${QMAKE_FILE_IN}
flex.input = FLEXSOURCES
flex.output = ${QMAKE_FILE_PATH}/${QMAKE_FILE_BASE}.lexer.cpp
flex.commands = flex -o ${QMAKE_FILE_PATH}/${QMAKE_FILE_BASE}.lexer.cpp ${QMAKE_FILE_IN}
flex.CONFIG += target_predeps
flex.variable_out = GENERATED_SOURCES
silent:flex.commands = @echo Lex ${QMAKE_FILE_IN} && $$flex.commands
QMAKE_EXTRA_COMPILERS += flex
[/plain]
% cat bison.pri
[plain]
bison.name = Bison ${QMAKE_FILE_IN}
bison.input = BISONSOURCES
bison.output = ${QMAKE_FILE_PATH}/${QMAKE_FILE_BASE}.parser.cpp
bison.commands = bison -d -o ${QMAKE_FILE_PATH}/${QMAKE_FILE_BASE}.parser.cpp ${QMAKE_FILE_IN}
bison.CONFIG += target_predeps
bison.variable_out = GENERATED_SOURCES
silent:bison.commands = @echo Bison ${QMAKE_FILE_IN} && $$bison.commands
QMAKE_EXTRA_COMPILERS += bison
bison_header.input = BISONSOURCES
bison_header.output = ${QMAKE_FILE_PATH}/${QMAKE_FILE_BASE}.parser.hpp
bison_header.commands = bison -d -o ${QMAKE_FILE_PATH}/${QMAKE_FILE_BASE}.parser.cpp ${QMAKE_FILE_IN}
bison_header.CONFIG += target_predeps no_link
silent:bison_header.commands = @echo Bison ${QMAKE_FILE_IN} && $$bison.commands
QMAKE_EXTRA_COMPILERS += bison_header
[/plain]

Of course, the proper way to use those is to include them and then define FLEXSOURCES / BISONSOURCES as needed. Example:
[plain]
include(flex.pri)
include(bison.pri)
FLEXSOURCES = myproject.l
BISONSOURCES = myproject.y
[/plain]


7 Comments »

  1. shailesh says:

    Hey Thanks a lot!!!. Was struggling with this for quite some time. Just what I was looking for 🙂

  2. d4t4 says:

    Thanks a bunch!

  3. Nathan says:

    Does this work when compiling under Windows with mingw? In other words, does Qt know where to find flex and bison there, or is this a Linux+Mac-only solution? Thanks!

  4. Erm… no idea 🙂

  5. Martin B says:

    Thank you!
    Awesome script which work well in my Qt 4.7 Bison 2.5 and latest flex environment on latest UbuntuLinux.
    Thank you!

  6. Flo says:

    About the windows issue: Make sure the tools are in your PATH. It’s a hassle to set up under windows (Can’t resize the frigging dialog to enter the PATH – WTF?) but it should be possible..

Leave a Reply

Your email address will not be published. Required fields are marked *