PDA

View Full Version : a sed and regex question



lehe
09-03-2009, 12:45 PM
Hi,
I am generating some dependency files from gcc -MM.


Makefile.depend: *.h *.cc Makefile
$(CXX) -MM $(CXXFLAGS) *.cc > Makefile.depend


Each rule in the resulted file is like:


main.o : main.cc defs.h \
src/misc.h

I want to specify the directory where the object files will be put, i.e.


$(BIN_DIR)main.o : main.cc defs.h \
src/misc.h

How to accomplish by using sed?

Note that I am not considering specifying the directories of source but the ones of object files.

Thanks and regards!

Kennedy
09-03-2009, 01:05 PM
CC := arm-linux-gcc
PWD := $(shell pwd)
ALLFILES_FILE := $(PWD)/.allfiles
LSFILES_FILE := $(PWD)/.filels
CHILDMK_FILE := $(PWD)/.child
ALLTMP_FILES := $(ALLFILES_FILE) $(LSFILES_FILE) $(CHILDMK_FILE)
CFLAGS += -I$(PWD)/../include
ALLFILES := sh $(ALLFILES_FILE)
LSFILES := sh $(LSFILES_FILE)
SHELL_HEAD := \#!/bin/sh
DIRFIND_CMD := find -maxdepth 1 -mindepth 1 -type d
DIRAWK_CMD := awk -F/ '{print $$2}'
DIR_CMD := $(DIRFIND_CMD) | $(DIRAWK_CMD)
FILEFIND_CMD := ls *.c | awk -F. '{print \$$1}'
ASSIGNFILE_CMD := FILES=\`$(FILEFIND_CMD)\`
SEDCMD := \`echo \"sed 's/ /,\$${PRENAME}_/g' | sed 's/__/_/g'\"\`
DSPFILE_CMD := echo \$${PRENAME}_\$${FILES} | eval \$${SEDCMD}
GETLD := \`grep LD_FLAGS \$$@.c | awk -F: '{print \$$2}'\`
COMPILE_CMD := \$$(CC) \$$(CFLAGS) \$$\$$LIBS -o../\$$(PRENAME)_\$$@ \$$@.c
FLAGSGET_CMD := \`grep LD_FLAGS \$$@.c | awk -F: '{print \$$\$$2}'\`
EXTRACT_LDFLAGS := export LIBS=\"$(FLAGSGET_CMD)\"

ifndef PARSE_DIRS
PARSE_DIRS := $(shell $(DIR_CMD))
endif


.PHONY: all runsub clean getallfile getfilels childmake childcopy cleanup \
runsub
export CC CFLAGS ALLFILES LSFILES

all: getallfile getfilels childcopy runsub cleanup

runsub:
@for i in $(PARSE_DIRS) ; do \
$(MAKE) PRENAME=$$i -C $$i ; \
done

clean: runclean cleanup

runclean: getallfile getfilels childcopy
@for i in $(PARSE_DIRS) ; do \
export PRENAME=$$i ; $(MAKE) -C $$i clean ; \
done

cleanup:
@for i in $(PARSE_DIRS) ; do \
rm -f $$i/Makefile ; \
done
@for i in $(ALLTMP_FILES) ; do \
rm -f $$i ; \
done

childcopy: childmake
@for i in $(PARSE_DIRS) ; do \
cp $(CHILDMK_FILE) $$i/Makefile ; \
done

getallfile:
@echo "$(SHELL_HEAD)" > $(ALLFILES_FILE)
@echo "$(ASSIGNFILE_CMD)" >> $(ALLFILES_FILE)
@echo "echo \$${FILES}" >> $(ALLFILES_FILE)
@chmod +x $(ALLFILES_FILE)

getfilels:
@echo "$(SHELL_HEAD)" > $(LSFILES_FILE)
@echo "SEDCMD=$(SEDCMD)" >> $(LSFILES_FILE)
@echo "$(ASSIGNFILE_CMD)" >> $(LSFILES_FILE)
@echo "$(DSPFILE_CMD)" >> $(LSFILES_FILE)
@chmod +x $(LSFILES_FILE)

childmake:
@echo "BINS := \$$(shell \$$(ALLFILES))" > $(CHILDMK_FILE)
@echo "BINS_LS := \$$(shell \$$(LSFILES))" >> $(CHILDMK_FILE)
@echo ".PHONY: all clean" >> $(CHILDMK_FILE)
@echo "all: \$$(BINS)" >> $(CHILDMK_FILE)
@echo "clean:" >> $(CHILDMK_FILE)
@echo " @rm -f ../{\$$(BINS_LS)}" >> $(CHILDMK_FILE)
@echo "%:" >> $(CHILDMK_FILE)
@echo " @$(EXTRACT_LDFLAGS) ; $(COMPILE_CMD)" >> $(CHILDMK_FILE)


You can have/use this if you'd like.

EDIT: I removed this, then put it back up. For a brief moment, I thought I hadn't cleansed it of proprietary data.