Add an explicit dependency between the output object files and the output directories that need to be created to hold those files. This ensures that the output directories are always created. Creating the output directories at parse time (current behavior) doesn't support the scenario where someone does "make clean all". The directories will be created during parsing, deleted during "clean" and then not available for the "all" target. Use an order-only prerequisite for the output directories, rather than a normal prerequisite, to avoid unnecessary recompilations.
Unified · Split
| @@ -20,11 +20,13 @@ LIBVFIO_OUTPUT := $(OUTPUT)/libvfio · 19 unmodified lines | |||
| 20 | LIBVFIO_O := $(patsubst %.c, $(LIBVFIO_OUTPUT)/%.o, $(LIBVFIO_C)) | 20 | LIBVFIO_O := $(patsubst %.c, $(LIBVFIO_OUTPUT)/%.o, $(LIBVFIO_C)) |
| 21 | 21 | ||
| 22 | LIBVFIO_O_DIRS := $(shell dirname $(LIBVFIO_O) | uniq) | 22 | LIBVFIO_O_DIRS := $(shell dirname $(LIBVFIO_O) | uniq) |
| 23 | $(shell mkdir -p $(LIBVFIO_O_DIRS)) | 23 | |
| 24 | $(LIBVFIO_O_DIRS): | ||
| 25 | mkdir -p $@ | ||
| 24 | 26 | ||
| 25 | CFLAGS += -I$(LIBVFIO_SRCDIR)/include | 27 | CFLAGS += -I$(LIBVFIO_SRCDIR)/include |
| 26 | 28 | ||
| 27 | $(LIBVFIO_O): $(LIBVFIO_OUTPUT)/%.o : $(LIBVFIO_SRCDIR)/%.c | 29 | $(LIBVFIO_O): $(LIBVFIO_OUTPUT)/%.o : $(LIBVFIO_SRCDIR)/%.c | $(LIBVFIO_O_DIRS) |
| 28 | $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ | 30 | $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ |
| 29 | 31 | ||
| 30 | EXTRA_CLEAN += $(LIBVFIO_OUTPUT) | 32 | EXTRA_CLEAN += $(LIBVFIO_OUTPUT) |