[593] | 1 | # $Id: common.gmk,v 1.43 2007/06/28 14:33:38 gcosmo Exp $ |
---|
[472] | 2 | # ---------------------------------------------------------------- |
---|
| 3 | # Common part of GNUmakefile for libraries. John Allison, 5/7/95. |
---|
| 4 | # ---------------------------------------------------------------- |
---|
| 5 | # Libraries are created according to G4SYSTEM. G.Cosmo, 11/6/96. |
---|
| 6 | # Introduced G4LIBDIR and G4TMPDIR. G.Cosmo, 23/6/98. |
---|
| 7 | |
---|
| 8 | ifndef G4LIBDIR |
---|
| 9 | G4LIBDIR := $(G4LIB)/$(G4SYSTEM) |
---|
| 10 | endif |
---|
| 11 | G4TMPDIR := $(G4TMP)/$(G4SYSTEM)/$(name) |
---|
| 12 | |
---|
| 13 | sources := $(wildcard src/*.cc) |
---|
| 14 | objects := $(patsubst src/%.cc,$(G4TMPDIR)/%.o,$(sources)) |
---|
| 15 | dependencies := $(patsubst src/%.cc,$(G4TMPDIR)/%.d,$(sources)) |
---|
| 16 | |
---|
| 17 | g4libraries_to_build := |
---|
| 18 | ifeq ($(G4LIB_NO_SHARED),) |
---|
| 19 | ifneq ($(G4LIB_BUILD_SHARED),) |
---|
| 20 | g4libraries_to_build += $(G4LIBDIR)/lib$(name).$(SHEXT) |
---|
| 21 | endif |
---|
| 22 | endif |
---|
| 23 | ifneq ($(G4LIB_BUILD_STATIC),) |
---|
| 24 | g4libraries_to_build += $(G4LIBDIR)/lib$(name).a |
---|
| 25 | endif |
---|
| 26 | |
---|
| 27 | # GPPFLAGS is defined here to make the .d file(s) and include it(them). |
---|
| 28 | |
---|
| 29 | GPPFLAGS := "-M" |
---|
| 30 | |
---|
| 31 | ############################################################################### |
---|
| 32 | # |
---|
| 33 | # Actual gmake targets. |
---|
| 34 | # |
---|
| 35 | |
---|
| 36 | lib: $(g4libraries_to_build) |
---|
| 37 | |
---|
| 38 | ifeq ($(G4LIB_NO_SHARED),) |
---|
| 39 | ifneq ($(G4LIB_BUILD_SHARED),) |
---|
| 40 | # Make shared library. |
---|
| 41 | $(G4LIBDIR)/lib$(name).$(SHEXT): $(G4TMPDIR)/obj.last |
---|
| 42 | @if [ ! -d $(G4LIBDIR) ] ; then mkdir $(G4LIBDIR) ;fi |
---|
| 43 | @echo Creating shared library $@ ... |
---|
| 44 | @$(RM) $@ |
---|
| 45 | # use architecture specific macro defined in sys/$(G4SYSTEM).gmk |
---|
| 46 | $(build-granular-shared-lib) |
---|
| 47 | endif |
---|
| 48 | endif |
---|
| 49 | |
---|
| 50 | ifneq ($(G4LIB_BUILD_STATIC),) |
---|
| 51 | # Make static (archive) library. |
---|
| 52 | $(G4LIBDIR)/lib$(name).a: $(G4TMPDIR)/obj.last |
---|
| 53 | @if [ ! -d $(G4LIBDIR) ] ; then mkdir $(G4LIBDIR) ;fi |
---|
| 54 | @echo Creating/replacing object files in $(G4LIBDIR)/lib$(name).a ... |
---|
| 55 | @rm -f $(G4LIBDIR)/lib$(name).a |
---|
| 56 | @$(AR) $(OUT_LIB)$(G4LIBDIR)/lib$(name).a $(G4TMPDIR)/*.o |
---|
| 57 | @if [ X$(G4SYSTEM) != XWIN32-VC ] ; then \ |
---|
| 58 | if [ -f /usr/bin/ranlib -o -f /bin/ranlib ] ; then \ |
---|
| 59 | ranlib $(G4LIBDIR)/lib$(name).a ; fi ; fi |
---|
| 60 | endif |
---|
| 61 | |
---|
| 62 | ############################################################################### |
---|
| 63 | # |
---|
| 64 | # Actual targets for .o, .d files |
---|
| 65 | # |
---|
| 66 | |
---|
| 67 | $(G4TMPDIR)/%.o: src/%.cc |
---|
| 68 | ifdef CPPVERBOSE |
---|
| 69 | $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(OUT_OBJ)$(G4TMPDIR)/$(*F).o src/$*.cc |
---|
| 70 | else |
---|
| 71 | @echo Compiling $*.cc ... |
---|
| 72 | @$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(OUT_OBJ)$(G4TMPDIR)/$(*F).o src/$*.cc |
---|
| 73 | endif |
---|
| 74 | |
---|
| 75 | # .PHONY targets are executed regardless of time-stamp of any file of |
---|
| 76 | # same name. |
---|
| 77 | .PHONY: all obj lib clean clean_libs includes |
---|
| 78 | |
---|
| 79 | obj: $(G4TMPDIR)/obj.last |
---|
| 80 | |
---|
| 81 | # Touch the versioning file |
---|
| 82 | $(G4TMPDIR)/obj.last: $(objects) |
---|
| 83 | @touch $@ |
---|
| 84 | |
---|
| 85 | # Make the .d file(s) and include it(them). |
---|
| 86 | |
---|
| 87 | # The ideas for this come from the GNU Make Manual, Section 4.12, |
---|
| 88 | # Generating Prerequisites Automatically. The g++ compiler has an |
---|
| 89 | # option -M or -MM to write to standard output a list of dependencies |
---|
| 90 | # based on the #include statements. The "sed" adds the dependency |
---|
| 91 | # file itself as a second target. The result is a mini-makefile which |
---|
| 92 | # specifies the .o and .d files as targets which depend on all the |
---|
| 93 | # files found through the #include statements. This file is then |
---|
| 94 | # included, causing GNU Make to honour these dependencies. |
---|
| 95 | |
---|
| 96 | # The "set -e" causes the shell to exit with an error when the "g++" |
---|
| 97 | # fails (otherwise it would only notice the last command in the |
---|
| 98 | # pipeline, namely "sed"). GNU Make notices the error and exits |
---|
| 99 | # sooner than it otherwise would (still not as soon as I expect, |
---|
| 100 | # though!). Even then, an empty file is made, so "[ -s $@ ] || rm -f |
---|
| 101 | # $@" removes it ([ -s filename ] gives zero exit code only if file |
---|
| 102 | # exists and has a size greater than zero). This avoids making |
---|
| 103 | # corrupt .d files which would play havoc with your next build. |
---|
| 104 | |
---|
| 105 | $(G4TMPDIR)/%.d: src/%.cc |
---|
| 106 | @echo Making dependency for file $< ... |
---|
| 107 | @if [ ! -d $(G4TMPDIR) ] ; then mkdir -p $(G4TMPDIR) ;fi |
---|
| 108 | @set -e;\ |
---|
| 109 | g++ $(GPPFLAGS) $(CPPFLAGS) -w $< |\ |
---|
| 110 | sed 's!$*\.o!$(G4TMPDIR)/& $@!' >$@;\ |
---|
| 111 | [ -s $@ ] || rm -f $@ |
---|
| 112 | ifneq ($(dependencies),) |
---|
[593] | 113 | ifneq ($(MAKECMDGOALS),clean) |
---|
[472] | 114 | -include $(dependencies) |
---|
| 115 | endif |
---|
[593] | 116 | endif |
---|
[472] | 117 | |
---|
| 118 | # |
---|
| 119 | # Installation of include files |
---|
| 120 | # |
---|
| 121 | installed_includes:=$(foreach file,$(wildcard include/*),$(shell test -f $(file) && echo $(file))) |
---|
| 122 | installed_includes:=$(patsubst include/%,$(G4INCLUDE)/%,$(installed_includes)) |
---|
| 123 | |
---|
| 124 | # NOTE: the double colon rule allows to add other rules for the same target |
---|
| 125 | # |
---|
[593] | 126 | includes:: $(installed_includes) |
---|
[472] | 127 | |
---|
| 128 | # Static Pattern rules, see GNU make manual for details. |
---|
[593] | 129 | # target(s): target-pattern : dep-pattern |
---|
[472] | 130 | # |
---|
| 131 | $(installed_includes): $(G4INCLUDE)/% : include/% |
---|
| 132 | @cp -p $< $@ |
---|
| 133 | |
---|
| 134 | # |
---|
| 135 | # Clean up libraries |
---|
| 136 | # |
---|
| 137 | ifndef G4EXLIB |
---|
| 138 | clean:: |
---|
| 139 | @echo Cleaning up ... |
---|
| 140 | @rm -f $(G4LIBDIR)/lib$(name).a |
---|
| 141 | @rm -f $(G4LIBDIR)/*$(name).lib |
---|
| 142 | @rm -f $(G4LIBDIR)/*$(name).exp |
---|
| 143 | @rm -f $(G4LIBDIR)/lib$(name).$(SHEXT) |
---|
| 144 | @rm -rf $(G4TMPDIR) |
---|
| 145 | endif |
---|
| 146 | |
---|
| 147 | clean_libs:: |
---|
| 148 | @echo Removing library lib$(name).a ... |
---|
| 149 | @rm -f $(G4LIBDIR)/*$(name).a |
---|
| 150 | @echo Removing library lib$(name).$(SHEXT) ... |
---|
| 151 | @rm -f $(G4LIBDIR)/*$(name).lib |
---|
| 152 | @rm -f $(G4LIBDIR)/*$(name).exp |
---|
| 153 | @rm -f $(G4LIBDIR)/*$(name).$(SHEXT) |
---|