tags:

views:

35

answers:

1

I am writing a generic makefile to build static libraries. It seems to work well so far, except for the line calling sed:

# Generic makefile to build a static library
ARCH       = linux

CFLAGS     = -O3 -Wall

SOURCES    = src
BUILD_DIR  = build/$(ARCH)
TARGET     = $(BUILD_DIR)/libz.a

CFILES     = $(foreach dir,$(SOURCES),$(wildcard $(dir)/*.c))
OBJECTS    = $(addprefix $(BUILD_DIR)/,$(CFILES:.c=.o))

# Pull in the dependencies if they exist
# http://scottmcpeak.com/autodepend/autodepend.html
-include $(OBJECTS:.o=.dep)

default: create-dirs $(TARGET)

$(TARGET): $(OBJECTS)
    $(AR) -rc $(TARGET) $^

$(BUILD_DIR)/%.o: %.c 
    $(CC) $(CFLAGS) -c $< -o $@ 
    $(CC) -M $(CFLAGS) $*.c > $(BUILD_DIR)/$*.tmp
    sed s/.*:/$(BUILD_DIR)\\/$*.o:/ $(BUILD_DIR)/$*.tmp > $(BUILD_DIR)/$*.dep
    @rm $(BUILD_DIR)/$*.tmp

.PHONY: create-dirs
create-dirs:
    @for p in $(SOURCES); do mkdir -p $(BUILD_DIR)/$$p; done

.PHONY: clean
clean:
    rm -fr $(BUILD_DIR)

sed is used to replace the path/name of the object file with the full path of where the object actually is. e.g. 'src/foo.o:' is replaced with 'build/linux/src/foo.o:' in this example. $(BUILD_DIR) and $* in the replacement string both contain forward slashes when expanded - how do I pass them to sed?

Note: This might have been answered here before, but I am so far unable to apply those answers to my specific problem!

+4  A: 
  • You can use anything else than forward slashes as separator in sed. E.g. sed s~foo~bar~g
  • You can use double quotes " (at least in the shell), and variables will still be expanded: echo "Hello $PLANET"
Sjoerd