rules.mak 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. ifeq ($(LIBDIR),)
  2. LIBDIR = ../lib
  3. endif
  4. ifeq ($(BINDIR),)
  5. BINDIR = ../bin
  6. endif
  7. #
  8. # The name(s) of output lib file(s) (e.g. libapp.a).
  9. #
  10. LIB := $($(APP)_LIB)
  11. SHLIB = $($(APP)_SHLIB)
  12. SONAME = $($(APP)_SONAME)
  13. ifeq ($(SHLIB_SUFFIX),so)
  14. SHLIB_OPT := -shared -Wl,-soname,$(SHLIB)
  15. else ifeq ($(SHLIB_SUFFIX),dylib)
  16. SHLIB_OPT := -dynamiclib -undefined dynamic_lookup -flat_namespace
  17. else ifeq ($(SHLIB_SUFFIX),dll)
  18. SHLIB_OPT := -shared -Wl,-soname,$(SHLIB)
  19. else
  20. SHLIB_OPT :=
  21. endif
  22. #
  23. # The name of output executable file (e.g. app.exe).
  24. #
  25. EXE = $($(APP)_EXE)
  26. #
  27. # Source directory
  28. #
  29. SRCDIR = $($(APP)_SRCDIR)
  30. #
  31. # Output directory for object files (i.e. output/target)
  32. #
  33. OBJDIR = output/$(app)-$(TARGET_NAME)
  34. ifeq ($(OS_NAME),linux-kernel)
  35. export $(APP)_CFLAGS += -DKBUILD_MODNAME=$(app) -DKBUILD_BASENAME=$(app)
  36. endif
  37. #
  38. # OBJS is ./output/target/file.o
  39. #
  40. OBJS = $(foreach file, $($(APP)_OBJS), $(OBJDIR)/$(file))
  41. OBJDIRS := $(sort $(dir $(OBJS)))
  42. #
  43. # FULL_SRCS is ../src/app/file1.c ../src/app/file1.S
  44. #
  45. FULL_SRCS = $(foreach file, $($(APP)_OBJS), $(SRCDIR)/$(basename $(file)).m $(SRCDIR)/$(basename $(file)).c $(SRCDIR)/$(basename $(file)).cpp $(SRCDIR)/$(basename $(file)).cc $(SRCDIR)/$(basename $(file)).S)
  46. #
  47. # When generating dependency (gcc -MM), ideally we use only either
  48. # CFLAGS or CXXFLAGS (not both). But I just couldn't make if/ifeq to work.
  49. #
  50. #DEPFLAGS = $($(APP)_CXXFLAGS) $($(APP)_CFLAGS)
  51. DEPCFLAGS = $($(APP)_CFLAGS)
  52. DEPCXXFLAGS = $($(APP)_CXXFLAGS)
  53. # Dependency file
  54. DEP_FILE := .$(app)-$(TARGET_NAME).depend
  55. print_common:
  56. @echo "###"
  57. @echo "### DUMPING MAKE VARIABLES (I WON'T DO ANYTHING ELSE):"
  58. @echo "###"
  59. @echo APP=$(APP)
  60. @echo OBJDIR=$(OBJDIR)
  61. @echo OBJDIRS=$(OBJDIRS)
  62. @echo OBJS=$(OBJS)
  63. @echo SRCDIR=$(SRCDIR)
  64. @echo FULL_SRCS=$(FULL_SRCS)
  65. @echo $(APP)_CFLAGS=$($(APP)_CFLAGS)
  66. @echo $(APP)_CXXFLAGS=$($(APP)_CXXFLAGS)
  67. @echo $(APP)_LDFLAGS=$($(APP)_LDFLAGS)
  68. # @echo DEPFLAGS=$(DEPFLAGS)
  69. @echo CC=$(CC)
  70. @echo AR=$(AR)
  71. @echo AR_FLAGS=$(AR_FLAGS)
  72. @echo RANLIB=$(RANLIB)
  73. print_bin: print_common
  74. @echo EXE=$(subst /,$(HOST_PSEP),$(BINDIR)/$(EXE))
  75. @echo BINDIR=$(BINDIR)
  76. print_lib: print_common
  77. ifneq ($(LIB),)
  78. @echo LIB=$(subst /,$(HOST_PSEP),$(LIBDIR)/$(LIB))
  79. endif
  80. ifneq ($(SHLIB),)
  81. @echo SHLIB=$(subst /,$(HOST_PSEP),$(LIBDIR)/$(SHLIB))
  82. endif
  83. ifneq ($(SONAME),)
  84. @echo SONAME=$(subst /,$(HOST_PSEP),$(LIBDIR)/$(SONAME))
  85. endif
  86. @echo LIBDIR=$(LIBDIR)
  87. ifneq ($(LIB),)
  88. $(subst /,$(HOST_PSEP),$(LIBDIR)/$(LIB)): $(OBJDIRS) $(OBJS) $($(APP)_EXTRA_DEP)
  89. if test ! -d $(LIBDIR); then $(subst @@,$(subst /,$(HOST_PSEP),$(LIBDIR)),$(HOST_MKDIR)); fi
  90. $(AR) $(AR_FLAGS) $@ $(OBJS)
  91. $(RANLIB) $@
  92. endif
  93. ifneq ($(SHLIB),)
  94. $(subst /,$(HOST_PSEP),$(LIBDIR)/$(SHLIB)): $(OBJDIRS) $(OBJS) $($(APP)_EXTRA_DEP)
  95. if test ! -d $(LIBDIR); then $(subst @@,$(subst /,$(HOST_PSEP),$(LIBDIR)),$(HOST_MKDIR)); fi
  96. $(LD) $(LDOUT)$(subst /,$(HOST_PSEP),$@) \
  97. $(subst /,$(HOST_PSEP),$(OBJS)) $($(APP)_LDFLAGS) $(SHLIB_OPT)
  98. endif
  99. ifneq ($(SONAME),)
  100. $(subst /,$(HOST_PSEP),$(LIBDIR)/$(SONAME)): $(subst /,$(HOST_PSEP),$(LIBDIR)/$(SHLIB))
  101. ln -sf $(SHLIB) $@
  102. endif
  103. ifneq ($(EXE),)
  104. $(subst /,$(HOST_PSEP),$(BINDIR)/$(EXE)): $(OBJDIRS) $(OBJS) $($(APP)_EXTRA_DEP)
  105. if test ! -d $(BINDIR); then $(subst @@,$(subst /,$(HOST_PSEP),$(BINDIR)),$(HOST_MKDIR)); fi
  106. $(LD) $(LDOUT)$(subst /,$(HOST_PSEP),$(BINDIR)/$(EXE)) \
  107. $(subst /,$(HOST_PSEP),$(OBJS)) $($(APP)_LDFLAGS)
  108. endif
  109. $(OBJDIR)/$(app).o: $(OBJDIRS) $(OBJS)
  110. $(CROSS_COMPILE)ld -r -o $@ $(OBJS)
  111. $(OBJDIR)/$(app).ko: $(OBJDIR)/$(app).o | $(OBJDIRS)
  112. @echo Creating kbuild Makefile...
  113. @echo "# Our module name:" > $(OBJDIR)/Makefile
  114. @echo 'obj-m += $(app).o' >> $(OBJDIR)/Makefile
  115. @echo >> $(OBJDIR)/Makefile
  116. @echo "# Object members:" >> $(OBJDIR)/Makefile
  117. @echo -n '$(app)-objs += ' >> $(OBJDIR)/Makefile
  118. @for file in $($(APP)_OBJS); do \
  119. echo -n "$$file " >> $(OBJDIR)/Makefile; \
  120. done
  121. @echo >> $(OBJDIR)/Makefile
  122. @echo >> $(OBJDIR)/Makefile
  123. @echo "# Prevent .o files to be built by kbuild:" >> $(OBJDIR)/Makefile
  124. @for file in $($(APP)_OBJS); do \
  125. echo ".PHONY: `pwd`/$(OBJDIR)/$$file" >> $(OBJDIR)/Makefile; \
  126. done
  127. @echo >> $(OBJDIR)/Makefile
  128. @echo all: >> $(OBJDIR)/Makefile
  129. @echo -e "\tmake -C $(KERNEL_DIR) M=`pwd`/$(OBJDIR) modules $(KERNEL_ARCH)" >> $(OBJDIR)/Makefile
  130. @echo Invoking kbuild...
  131. $(MAKE) -C $(OBJDIR)
  132. ../lib/$(app).ko: $(LIB) $(OBJDIR)/$(app).ko
  133. cp $(OBJDIR)/$(app).ko ../lib
  134. $(OBJDIR)/%$(OBJEXT): $(SRCDIR)/%.m | $(OBJDIRS)
  135. $(CC) $($(APP)_CFLAGS) \
  136. $(CC_OUT)$(subst /,$(HOST_PSEP),$@) \
  137. $(subst /,$(HOST_PSEP),$<)
  138. $(OBJDIR)/%$(OBJEXT): $(SRCDIR)/%.c | $(OBJDIRS)
  139. $(CC) $($(APP)_CFLAGS) \
  140. $(CC_OUT)$(subst /,$(HOST_PSEP),$@) \
  141. $(subst /,$(HOST_PSEP),$<)
  142. $(OBJDIR)/%$(OBJEXT): $(SRCDIR)/%.S | $(OBJDIRS)
  143. $(CC) $($(APP)_CFLAGS) \
  144. $(CC_OUT)$(subst /,$(HOST_PSEP),$@) \
  145. $(subst /,$(HOST_PSEP),$<)
  146. $(OBJDIR)/dshowclasses.o: $(SRCDIR)/dshowclasses.cpp | $(OBJDIRS)
  147. $(CXX) $($(APP)_CXXFLAGS) -I$(SRCDIR)/../../../third_party/BaseClasses -fpermissive \
  148. $(CC_OUT)$(subst /,$(HOST_PSEP),$@) \
  149. $(subst /,$(HOST_PSEP),$<)
  150. $(OBJDIR)/%$(OBJEXT): $(SRCDIR)/%.cpp | $(OBJDIRS)
  151. $(CXX) $($(APP)_CXXFLAGS) \
  152. $(CC_OUT)$(subst /,$(HOST_PSEP),$@) \
  153. $(subst /,$(HOST_PSEP),$<)
  154. $(OBJDIR)/%$(OBJEXT): $(SRCDIR)/%.cc | $(OBJDIRS)
  155. $(CXX) $($(APP)_CXXFLAGS) \
  156. $(CC_OUT)$(subst /,$(HOST_PSEP),$@) \
  157. $(subst /,$(HOST_PSEP),$<)
  158. $(OBJDIRS):
  159. $(subst @@,$(subst /,$(HOST_PSEP),$@),$(HOST_MKDIR))
  160. $(LIBDIR):
  161. $(subst @@,$(subst /,$(HOST_PSEP),$@),$(HOST_MKDIR))
  162. $(BINDIR):
  163. $(subst @@,$(subst /,$(HOST_PSEP),$@),$(HOST_MKDIR))
  164. clean:
  165. $(subst @@,$(subst /,$(HOST_PSEP),$(OBJDIR)/*),$(HOST_RMR))
  166. $(subst @@,$(subst /,$(HOST_PSEP),$(OBJDIR)),$(HOST_RMDIR))
  167. ifeq ($(OS_NAME),linux-kernel)
  168. rm -f ../lib/$(app).o
  169. endif
  170. gcov-report:
  171. for file in $(FULL_SRCS); do \
  172. gcov $$file -n -o $(OBJDIR); \
  173. done
  174. realclean: clean
  175. ifneq ($(LIB),)
  176. $(subst @@,$(subst /,$(HOST_PSEP),$(LIBDIR)/$(LIB)),$(HOST_RM))
  177. endif
  178. ifneq ($(SHLIB),)
  179. $(subst @@,$(subst /,$(HOST_PSEP),$(LIBDIR)/$(SHLIB)),$(HOST_RM))
  180. endif
  181. ifneq ($(SONAME),)
  182. $(subst @@,$(subst /,$(HOST_PSEP),$(LIBDIR)/$(SONAME)),$(HOST_RM))
  183. endif
  184. ifneq ($(EXE),)
  185. $(subst @@,$(subst /,$(HOST_PSEP),$(BINDIR)/$(EXE)),$(HOST_RM))
  186. endif
  187. $(subst @@,$(DEP_FILE),$(HOST_RM))
  188. ifeq ($(OS_NAME),linux-kernel)
  189. rm -f ../lib/$(app).ko
  190. endif
  191. depend:
  192. $(subst @@,$(DEP_FILE),$(HOST_RM))
  193. for F in $(FULL_SRCS); do \
  194. if test -f $$F; then \
  195. echo "$(OBJDIR)/" | tr -d '\n' >> $(DEP_FILE); \
  196. if echo $$F | grep -q "\.c[c|pp]"; then \
  197. dep="$(CXX) -M $(DEPCXXFLAGS) $$F"; \
  198. else \
  199. dep="$(CC) -M $(DEPCFLAGS) $$F"; \
  200. fi; \
  201. if eval $$dep | sed '/^#/d' >> $(DEP_FILE); then \
  202. true; \
  203. else \
  204. echo 'err:' >> $(DEP_FILE); \
  205. rm -f $(DEP_FILE); \
  206. exit 1; \
  207. fi; \
  208. fi; \
  209. done;
  210. dep: depend
  211. -include $(DEP_FILE)