I've been stuck on this one for a while now so really hoping someone can help! I have an array of folders of C/C++ source that are common code to several projects, and some more folders (by the side of the others) that contain projects themselves.
When I build a project, I want to be able to include a sources.mk from an external folder, and have the objects for those included sources go into a subfolder of the project directory so as not to clutter the common folder up with project-specific objects (each project can have different flags, optimisations etc.)
As an example, here's a folder structure:
The Makefile in projects/thing will include common/stuff/sources.mk to get a list of the sources in that folder (appended to the list of sources already in Makefile). Sources in sources.mk are prepended with the relative path between Makefile and sources.mk as follows:
THIS_TOP := $(dir $(lastword $(MAKEFILE_LIST)))
SOURCES_C += $(THIS_TOP)/aaa.c
I want all objects to go into (dir I'm building in)/objdir/, preferably with subfolders based on the path from source so e.g.:
cd projects/thing; make
I make a list of object files and strip the ../ out of the relative path as follows:
_OBJECTS = $(subst ../,,$(__OBJECTS))
OBJECTS = $(patsubst %,$(OBJDIR)/%,$(_OBJECTS))
The problem I have is that I can't seem to come up with a rule (to go in common.mk) to make this local .o from a remote .c! I've tried a second expansion to try to make a rule that searches in the list of .c files for the one that corresponds to the .o that is required, but can't make that work due to the lack of multiple wildcards in filter...
$(OBJDIR)/%.o: $$(filter %$$(addsuffix .c,$$(subst .o,,$$(notdir $$@))),$$(SOURCES_C)) | $(OBJDIR)/ $(OBJSUBDIRS)/
$(CC) -o $@ $< $(INCLUDES) $(CPPFLAGS) $(CFLAGS) $(WARNINGS) $(DEPGENFLAGS)
I'm really tearing my hair out on this one... has anyone managed something similar? If all else fails I'll just build them as static libraries (already have libraries building in subdir of project folder like I want) but since that creates extra makefiles to maintain I'd really like to avoid it.
Any help greatly appreciated!