library.mak 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- make -*-
  2. # This creates a shared library.
  3. # Input
  4. # $(SOURCE) - The source code to use
  5. # $(HEADERS) - Exported header files and private header files
  6. # $(LIBRARY) - The name of the library without lib or .so
  7. # $(MAJOR) - The major version number of this library
  8. # $(MINOR) - The minor version number of this library
  9. # All output is writtin to .opic files in the build directory to
  10. # signify the PIC output.
  11. # See defaults.mak for information about LOCAL
  12. # Some local definitions
  13. LOCAL := lib$(LIBRARY).so.$(MAJOR).$(MINOR)
  14. $(LOCAL)-OBJS := $(addprefix $(OBJ)/,$(addsuffix .opic,$(notdir $(basename $(SOURCE)))))
  15. $(LOCAL)-DEP := $(addprefix $(DEP)/,$(addsuffix .opic.d,$(notdir $(basename $(SOURCE)))))
  16. $(LOCAL)-HEADERS := $(addprefix $(INCLUDE)/,$(HEADERS))
  17. $(LOCAL)-SONAME := lib$(LIBRARY).so.$(MAJOR)
  18. $(LOCAL)-SLIBS := $(SLIBS)
  19. $(LOCAL)-LIBRARY := $(LIBRARY)
  20. # Install the command hooks
  21. headers: $($(LOCAL)-HEADERS)
  22. library: $(LIB)/lib$(LIBRARY).so $(LIB)/lib$(LIBRARY).so.$(MAJOR) $(LIB)/lib$(LIBRARY).a
  23. clean: clean/$(LOCAL)
  24. veryclean: veryclean/$(LOCAL)
  25. # The clean rules
  26. .PHONY: clean/$(LOCAL) veryclean/$(LOCAL)
  27. clean/$(LOCAL):
  28. -rm -f $($(@F)-OBJS) $($(@F)-DEP)
  29. veryclean/$(LOCAL): clean/$(LOCAL)
  30. -rm -f $($(@F)-HEADERS) $(LIB)/lib$($(@F)-LIBRARY).so*
  31. # Build rules for the two symlinks
  32. .PHONY: $(LIB)/lib$(LIBRARY).so.$(MAJOR) $(LIB)/lib$(LIBRARY).so
  33. $(LIB)/lib$(LIBRARY).so.$(MAJOR): $(LIB)/lib$(LIBRARY).so.$(MAJOR).$(MINOR)
  34. ln -sf $(<F) $@
  35. $(LIB)/lib$(LIBRARY).so: $(LIB)/lib$(LIBRARY).so.$(MAJOR).$(MINOR)
  36. ln -sf $(<F) $@
  37. # The binary build rule
  38. $(LIB)/lib$(LIBRARY).so.$(MAJOR).$(MINOR): $($(LOCAL)-HEADERS) $($(LOCAL)-OBJS)
  39. -rm -f $(LIB)/lib$($(@F)-LIBRARY).so* 2> /dev/null
  40. echo Building shared library $@
  41. $(CXX) $(CXXFLAGS) $(LDFLAGS) $(PICFLAGS) $(LFLAGS) -o $@ \
  42. $(LFLAGS_SO) $(SONAME_MAGIC)$($(@F)-SONAME) -shared \
  43. $(filter %.opic,$^) $($(@F)-SLIBS)
  44. $(LIB)/lib$(LIBRARY).a: $($(LOCAL)-HEADERS) $($(LOCAL)-OBJS)
  45. -rm -f $(LIB)/lib$($(@F)-LIBRARY).a 2> /dev/null
  46. echo Building static library $@
  47. $(AR) rc $@ $(filter %.opic,$^)
  48. ranlib $@
  49. # Compilation rules
  50. vpath %.cc $(SUBDIRS)
  51. $(OBJ)/%.opic: %.cc
  52. echo Compiling $< to $@
  53. $(CXX) -c $(INLINEDEPFLAG) $(CPPFLAGS) $(CXXFLAGS) $(PICFLAGS) -o $@ $<
  54. $(DoDep)
  55. # Include the dependencies that are available
  56. The_DFiles = $(wildcard $($(LOCAL)-DEP))
  57. ifneq ($(words $(The_DFiles)),0)
  58. include $(The_DFiles)
  59. endif