Search code examples
sedmakefilegnu

Join two lists of strings in a makefile


In my makefile there is a variable that contains all source files:

SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))

I now want to create a DEPFILES that contains the same files as SOURCES with two main differences:

  1. Each file ends with ".d" instead of ".c"
  2. Each file contains a "." before its basename, such that the resulting file is hidden.

I came up with this expression, which works well:

DEPFILES := $(foreach s,$(SOURCES:.$(SRCEXT)=.$(DEPSEXT)),$(shell echo $(s) | sed -e 's/^\(.*\/\)/\1./')))

Initially, I split SOURCES into the following lists:

PRE := $(dir $(SOURCES))
SUF := $(addprefix ".", $(notdir $(SOURCES)))

Question: How do you join those two lists in a makefile, such that the results equals DEPFILES? In other words: How do you pairwisely concat the strings from both lists?


Solution

  • Here is a simpler approach using makefile only and no shell - if I understood your requirement ok:

    SRCS += bob/src1.c fred/src2.c src3.c
    DEPS=$(join $(addsuffix ., $(dir $(SRCS))), $(notdir $(SRCS:.c=.d)))
    
    # Some debug:
    $(info 1. $(SRCS))
    $(info 2. $(SRCS:.c=.d))
    $(info 3. $(notdir $(SRCS:.c=.d)))
    $(info 4. $(dir $(SRCS)))
    
    .PHONY: all
    all:
        @echo SRCS: $(SRCS)
        @echo DEPS: $(DEPS)
    
    
    

    I broke the output down into steps so you can see what each part does - saves me to explain it!

    outputs:

    1. bob/src1.c fred/src2.c src3.c
    2. bob/src1.d fred/src2.d src3.d
    3. src1.d src2.d src3.d
    4. bob/ fred/ ./
    SRCS: bob/src1.c fred/src2.c src3.c
    DEPS: bob/.src1.d fred/.src2.d ./.src3.d