1

I'd like to use automatic dependency generation with files generated from flex and bison.

Right now, I'm doing this:

CSRC=$(wildcard *.c)
OSRC=grammar.y scanner.l
OUTPUT_DIR="../bin"

SOLIDC_OBJS=solid_ast.o solid_strbuf.o solid_strlit.o solidc.o 
SOLIDL_OBJS=solidl.o solid_ast.o solid_strbuf.o

CFLAGS += -MD -MP
CC=clang
BISON=bison
FLEX=flex
MKDIR=mkdir -p

all: solidc

solidc: setup $(SOLIDC_OBJS)
    $(BISON) -vd grammar.y
    $(FLEX) --header-file=scanner.yy.h -o scanner.yy.c scanner.l
    $(CC) -o $(OUTPUT_DIR)/$@ $(SOLIDC_OBJS) scanner.yy.o grammar.tab.o

setup:
    @$(MKDIR) $(OUTPUT_DIR)

run:
    $(OUTPUT_DIR)/$(OUTPUT_NAME)

clean:
    $(RM) $(wildcard $(OUTPUT_DIR)/*) $(wildcard *.o) $(wildcard *.d) \
        $(wildcard scanner.yy.*) $(wildcard grammar.tab.*) \
        $(wildcard grammar.output)

-include $(SRC:%.c=%.d) scanner.yy.d grammar.tab.d

But this doesn't work because scanner.yy.d and grammar.tab.d never get a chance to be generated.

What's the best way to work around this?


Please note that this question is actually not the same as GNU make: Generating automatic dependencies with generated header files, because I'm using the "less efficient" -include method rather than sef, in addition to lots of other things.

Community
  • 1
  • 1
Aaron Yodaiken
  • 19,163
  • 32
  • 103
  • 184
  • I'm not very familiar with flex and bison. Do the `$(FLEX)` and `$(BISON)` commands shown generate `scanner.yy.d` and `grammar.tab.d`? Or if they don't, do you know other commands that will? – Beta Jun 22 '12 at 19:37
  • No. They generate scanner.yy.c and grammar.tab.c. I guess you could manually $(CC) those? – Aaron Yodaiken Jun 22 '12 at 22:04

1 Answers1

0

If I understand you right, you can just make grammar.tab.o and scanner.yy.o prerequisites of solidc, and then the object rule will produce dependency files for them, the same as for any object.

SOLIDC_OBJS=solid_ast.o solid_strbuf.o solid_strlit.o solidc.o grammar.tab.o scanner.yy.o

all: solidc

grammar.tab.c: grammar.y
    $(BISON) -vd grammar.y

scanner.yy.c: scanner.l
    $(FLEX) --header-file=scanner.yy.h -o scanner.yy.c scanner.l

solidc: setup $(SOLIDC_OBJS)
    $(CC) -o $(OUTPUT_DIR)/$@ $(SOLIDC_OBJS)

(We could tighten these rules up a little, but let's get them working first.)

Beta
  • 96,650
  • 16
  • 149
  • 150
  • The two actions you added don't seem to be run? Thanks, though. – Aaron Yodaiken Jun 23 '12 at 00:32
  • This may take a few iterations. Try `rm -f grammar.tab.c ; make grammar.tab.c` and if that works, `rm -f grammar.tab.o ; make grammar.tab.o`. – Beta Jun 23 '12 at 00:53
  • How about `rm -f grammar.tab.c ; make solidc`? – Beta Jun 23 '12 at 01:40
  • What do you mean "doesn't work"? What happens? And when you `make grammar.tab.o` does it build `grammar.tab.d`? When you `make solidc.o` does it build `solidc.d`? Please edit your question to add the results of these experiments. – Beta Jun 24 '12 at 14:59