Search code examples
gnu-makeyacc

Parallell make with multiple yacc parsers


I have a project that contains multiple parsers generated using yacc. There are rules for each parser that looks something like:

xgram.c xgram.h : xgram.y
    yacc xgram.y
    mv y.tab.c xgram.c
    mv y.tab.h xgram.h

If I run this with make it works fine since each parser will be generated in sequence.

However, if I run it with make -j 10 there is an obviously a conflict about the y.tab.c file since the commands for each grammar will generate its own y.tab.h in parallel.

How can I avoid this? The only thing that I have found and tried is to use TMPDIR on each yacc command line to make it create the output files in different directories, which did not work.

Any other ideas?


Solution

  • I'm sure that you mis-typed the mv commands in your recipe.

    If you have bison, see the other answer.

    If your version of yacc is POSIX-compliant it should support the -b option, in which case you can use:

    xgram.c xgram.h : xgram.y
            yacc -b xgram -d xgram.y
            mv xgram.tab.c xgram.c
            mv xgram.tab.h xgram.h
    

    If you're not using a POSIX yacc you can do a hack like this:

    xgram.c xgram.h : xgram.y
            mkdir -p tmp_xgram
            (cd tmp_xgram && yacc -d ../xgram.y && mv y.tab.c ../xgram.c && mv y.tab.h ../xgram.h)
            rm -rf tmp_xgram
    

    I think that will work. If it still complains you may have to actually copy ../xgram.y to the temp directory first...

    If you have GNU make 4.3 you can use xgram.c xgram.h &: xgram.y to get a more correct rule, rather than :