help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

common.mk


From: Mihai Draghicioiu
Subject: common.mk
Date: Sat, 21 Mar 2009 11:54:46 +0200

Hi guys! I'm making an includable makefile, to use with my projects.
It works like this: If I have a directory and i want to make a program
in it, i just put in its Makefile:

programs = foo
foo_SRCS = foo.cpp bar.cpp
foo_CXXFLAGS = -g -Wall

include ../common.mk

and it works fine. The same goes for a folder with a library, or more.

But if I have libraries AND programs, I get the following warnings:

../common.mk:81: warning: overriding commands for target `.cpp.o'
../common.mk:81: warning: ignoring old commands for target `.cpp.o'
../common.mk:83: warning: overriding commands for target `.cpp.o'
../common.mk:81: warning: ignoring old commands for target `.cpp.o'

That's because, in order to take advantage of foo_CXXFLAGS, i need to
define .cpp.o inside a 'define' block, and I have to do the same for
the library, so .cpp.o is defined twice, differently.

I can think of two workarounds, one would be to set CXXFLAGS for each
cpp file (foo_cpp_CXXFLAGS), but that would mean I can't use the same
cpp file in two programs simultaneously. The other solution would be
to output something like foo-foo.o, and foo-bar.o (program-cppfile.o),
but I have no idea how to do that.

Either way, the .cpp.o target should get separated from the 'define' blocks.

Also, any helpful hints on how to improve common.mk are welcome. I
plan to use it for my projects, to avoid using automake and autoconf
and whatnot.

Here's the current version of common.mk:



#colours
RESETC=\033[0;37m
COMPILING=\033[1;32m
COMPILERC=\033[0;32m
LINKING=\033[1;33;44m
LINKERC=\033[0;33m
ERRORC=\033[1;31m
SUCCESSC=\033[1;32;44m

.PHONY: all clean $(subdirs)
all: $(subdirs) $(libraries) $(programs)
        @echo "$(SUCCESSC)All targets done!$(RESETC)"

##
## Subdir template
##
define subdir_template
$(1):
        @cd $(1) && $$(MAKE)
endef

##
## Program template
##
define program_template
$(1)_OBJS = $$($(1)_SRCS:.cpp=.o)

$(1): $$($(1)_OBJS)
        @echo "$$(LINKING)Linking address@hidden(RESETC)"
        @echo "$$(LINKERC)$$(CXX) $$^ $$(LDFLAGS) $$($(1)_LDFLAGS)
$$($(1)_LIBS:%=-l%) -o address@hidden(RESETC)"
        @if ! $$(CXX) $$^ $$(LDFLAGS) $$($(1)_LDFLAGS)
$$($(1)_LIBS:%=-l%) -o $$@; then \
        echo "$$(ERRORC)Linking failed for $$@ $$(RESETC)"; \
        exit 1; \
        fi

.cpp.o:
        @echo "$$(COMPILERC)$(CXX) $$(CXXFLAGS) $$($(1)_CXXFLAGS) -MMD
-c $$< -o address@hidden(RESETC)";
        @if $(CXX) $$(CXXFLAGS) $$($(1)_CXXFLAGS) -MMD -c $$< -o $$@; then \
         cp $$*.d $$*.P; \
         sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$$$//' -e
'/^$$$$/ d' -e 's/$$$$/ :/' < $$*.d >> $$*.P; \
         rm -f $$*.d; \
        else \
         echo "$$(ERRORC)Compilation failed for $$@ $$(RESETC)"; \
         rm -f $$*.d; \
         exit 1; \
        fi

-include $$($(1)_SRCS:.cpp=.P)
endef

##
## Library template
##
define library_template
$(2)_OBJS = $$($(2)_SRCS:.cpp=.o)

$(1): $$($(2)_OBJS)
        @echo "$$(LINKING)Archiving address@hidden(RESETC)"
        ar crus $$@ $$^

.cpp.o:
        @echo "$$(COMPILERC)$(CXX) $$(CXXFLAGS) $$($(2)_CXXFLAGS) -MMD
-c $$< -o address@hidden(RESETC)";
        @if $(CXX) $$(CXXFLAGS) $$($(2)_CXXFLAGS) -MMD -c $$< -o $$@; then \
         cp $$*.d $$*.P; \
         sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$$$//' -e
'/^$$$$/ d' -e 's/$$$$/ :/' < $$*.d >> $$*.P; \
         rm -f $$*.d; \
        else \
         echo "$$(ERRORC)Compilation failed for $$@ $$(RESETC)"; \
         rm -f $$*.d; \
         exit 1; \
        fi

-include $$($(1)_SRCS:.cpp=.P)
endef

CXXFLAGS=-g -Wall
LIBS=-g -Wall

$(foreach subdir,$(subdirs),$(eval $(call subdir_template,$(subdir))))

$(foreach prog,$(programs),$(eval $(call program_template,$(prog))))

$(foreach lib,$(libraries),$(eval $(call
library_template,$(lib),$(subst .,_,$(lib)))))

clean:
        @echo "$(ERRORC)Cleaning...$(RESETC)"
        rm -f *.[aodP] $(programs)
        @if [ "x$(subdirs)" != "x" ]; then \
         for d in $(subdirs); do \
          (cd $$d && $(MAKE) clean); \
         done; \
        fi




And here's an example makefile, that generates the warnings:

LDFLAGS=-L. -lglt
CXXFLAGS=-I. -DDEBUG -g -Wall

programs=test1 test2
test1_SRCS=test1.cpp
test2_SRCS=test2.cpp

libraries=libglt.a
libglt_a_SRCS=glt.cpp

include ../common.mk




You can get the full working example at
http://gltools.svn.sourceforge.net/viewvc/gltools/ (download tarball
at http://gltools.svn.sourceforge.net/viewvc/gltools.tar.gz?view=tar )


Thanks,
Mihai




reply via email to

[Prev in Thread] Current Thread [Next in Thread]