From 5e554c15982617a89b85aeb71592c20bfa7bdecd Mon Sep 17 00:00:00 2001
From: Renmingshuai <renmingshuai@huawei.com>
Date: Tue, 13 Apr 2021 16:25:43 +0800
Subject: [PATCH] optimize the efficiency of compiling dpdk

---
 config/common_base    |   5 +++
 mk/rte.combinedlib.mk |  10 +++++
 mk/rte.lib.mk         | 102 +++++++++++++++++++++++++++++++++++++++---
 3 files changed, 110 insertions(+), 7 deletions(-)

diff --git a/config/common_base b/config/common_base
index 57b1349..392e6c3 100644
--- a/config/common_base
+++ b/config/common_base
@@ -59,6 +59,11 @@ CONFIG_RTE_ENABLE_LTO=n
 #
 CONFIG_RTE_BUILD_SHARED_LIB=n
 
+#
+# Compile to both static library and share library
+#
+CONFIG_RTE_BUILD_BOTH_STATIC_AND_SHARED_LIBS=n
+
 #
 # Use newest code breaking previous ABI
 #
diff --git a/mk/rte.combinedlib.mk b/mk/rte.combinedlib.mk
index 9d0f935..1088543 100644
--- a/mk/rte.combinedlib.mk
+++ b/mk/rte.combinedlib.mk
@@ -15,9 +15,16 @@ RTE_LIBNAME := dpdk
 COMBINEDLIB := lib$(RTE_LIBNAME)$(EXT)
 
 LIBS := $(filter-out $(COMBINEDLIB), $(sort $(notdir $(wildcard $(RTE_OUTPUT)/lib/*$(EXT)))))
+ifeq ($(CONFIG_RTE_BUILD_BOTH_STATIC_AND_SHARED_LIBS),y)
+COMBINEDLIB_SO := lib$(RTE_LIBNAME).so
+LIBS_SO := $(filter-out $(COMBINEDLIB_SO), $(sort $(notdir $(wildcard $(RTE_OUTPUT)/lib/*.so))))
+endif
 
 all: FORCE
 	$(Q)echo "GROUP ( $(LIBS) )" > $(RTE_OUTPUT)/lib/$(COMBINEDLIB)
+ifeq ($(CONFIG_RTE_BUILD_BOTH_STATIC_AND_SHARED_LIBS),y)
+	$(Q)echo "GROUP ( $(LIBS_SO) )" > $(RTE_OUTPUT)/lib/$(COMBINEDLIB_SO)
+endif
 
 #
 # Clean all generated files
@@ -25,6 +32,9 @@ all: FORCE
 .PHONY: clean
 clean:
 	$(Q)rm -f $(RTE_OUTPUT)/lib/$(COMBINEDLIB)
+ifeq ($(CONFIG_RTE_BUILD_BOTH_STATIC_AND_SHARED_LIBS),y)
+	$(Q)rm -f $(RTE_OUTPUT)/lib/$(COMBINEDLIB_SO)
+endif
 
 .PHONY: FORCE
 FORCE:
diff --git a/mk/rte.lib.mk b/mk/rte.lib.mk
index 4516d1c..78f3c27 100644
--- a/mk/rte.lib.mk
+++ b/mk/rte.lib.mk
@@ -19,13 +19,6 @@ else ifeq ($(LIBABIVER),)
 LIBABIVER := 0.$(shell cat $(RTE_SRCDIR)/ABI_VERSION | tr -d '.')
 endif
 
-ifeq ($(CONFIG_RTE_BUILD_SHARED_LIB),y)
-LIB := $(patsubst %.a,%.so.$(LIBABIVER),$(LIB))
-ifeq ($(EXTLIB_BUILD),n)
-CPU_LDFLAGS += --version-script=$(SRCDIR)/$(EXPORT_MAP)
-endif
-endif
-
 
 _BUILD = $(LIB)
 PREINSTALL = $(SYMLINK-FILES-y)
@@ -34,6 +27,16 @@ _CLEAN = doclean
 
 LDLIBS += $(EXECENV_LDLIBS-y)
 
+ifeq ($(CONFIG_RTE_BUILD_BOTH_STATIC_AND_SHARED_LIBS),y)
+LIB_SO = $(LIB)
+LIB_SO := $(patsubst %.a,%.so.$(LIBABIVER),$(LIB_SO))
+ifeq ($(EXTLIB_BUILD),n)
+CPU_LDFLAGS += --version-script=$(SRCDIR)/$(EXPORT_MAP)
+endif
+_BUILD += $(LIB_SO)
+_INSTALL += $(INSTALL-FILES-y) $(RTE_OUTPUT)/lib/$(LIB_SO)
+endif
+
 .PHONY: all
 all: install
 
@@ -74,6 +77,89 @@ ifneq ($(CC_SUPPORTS_Z),false)
 NO_UNDEFINED := -z defs
 endif
 
+ifeq ($(CONFIG_RTE_BUILD_BOTH_STATIC_AND_SHARED_LIBS),y)
+O_TO_S = $(LD) -L$(RTE_SDK_BIN)/lib $(_CPU_LDFLAGS) $(EXTRA_LDFLAGS) \
+          -shared $(OBJS-y) $(NO_UNDEFINED) $(LDLIBS) -Wl,-soname,$(LIB_SO) -o $(LIB_SO)
+O_TO_S_STR = $(subst ','\'',$(O_TO_S)) #'# fix syntax highlight
+O_TO_S_DISP = $(if $(V),"$(O_TO_S_STR)","  LD $(@)")
+O_TO_S_DO = @set -e; \
+	echo $(O_TO_S_DISP); \
+	$(O_TO_S) && \
+	echo $(O_TO_S_CMD) > $(call exe2cmd,$(@))
+
+-include .$(LIB_SO).cmd
+
+#
+# Archive objects in .a file if needed
+#
+$(LIB): $(OBJS-y) $(DEP_$(LIB)) FORCE
+	@[ -d $(dir $@) ] || mkdir -p $(dir $@)
+	$(if $(D),\
+	    @echo -n "$< -> $@ " ; \
+	    echo -n "file_missing=$(call boolean,$(file_missing)) " ; \
+	    echo -n "cmdline_changed=$(call boolean,$(call cmdline_changed,$(O_TO_A_STR))) " ; \
+	    echo -n "depfile_missing=$(call boolean,$(depfile_missing)) " ; \
+	    echo "depfile_newer=$(call boolean,$(depfile_newer)) ")
+	$(if $(or \
+	    $(file_missing),\
+	    $(call cmdline_changed,$(O_TO_A_STR)),\
+	    $(depfile_missing),\
+	    $(depfile_newer)),\
+	    $(O_TO_A_DO))
+
+$(LIB_SO): $(OBJS-y) $(DEP_$(LIB_SO)) FORCE
+ifeq ($(LIBABIVER),)
+	@echo "Must Specify a $(LIB_SO) ABI version"
+	@false
+endif
+	@[ -d $(dir $@) ] || mkdir -p $(dir $@)
+	$(if $(D),\
+		@echo -n "$< -> $@ " ; \
+		echo -n "file_missing=$(call boolean,$(file_missing)) " ; \
+		echo -n "cmdline_changed=$(call boolean,$(call cmdline_changed,$(O_TO_S_STR))) " ; \
+		echo -n "depfile_missing=$(call boolean,$(depfile_missing)) " ; \
+		echo "depfile_newer=$(call boolean,$(depfile_newer)) ")
+	$(if $(or \
+		$(file_missing),\
+		$(call cmdline_changed,$(O_TO_S_STR)),\
+		$(depfile_missing),\
+		$(depfile_newer)),\
+		$(O_TO_S_DO))
+
+#
+# install lib in $(RTE_OUTPUT)/lib
+#
+$(RTE_OUTPUT)/lib/$(LIB): $(LIB)
+	@echo "  INSTALL-LIB $(LIB)"
+	@[ -d $(RTE_OUTPUT)/lib ] || mkdir -p $(RTE_OUTPUT)/lib
+	cp -f $(LIB) $(RTE_OUTPUT)/lib
+
+$(RTE_OUTPUT)/lib/$(LIB_SO): $(LIB_SO)
+	@echo "  INSTALL-LIB $(LIB_SO)"
+	@[ -d $(RTE_OUTPUT)/lib ] || mkdir -p $(RTE_OUTPUT)/lib
+	cp -f $(LIB_SO) $(RTE_OUTPUT)/lib
+	ln -s -f $< $(shell echo $@ | sed 's/\.so.*/.so/')
+
+#
+# Clean all generated files
+#
+.PHONY: clean
+clean: _postclean
+
+.PHONY: doclean
+doclean:
+	$(Q)rm -rf $(LIB) $(LIB_SO) $(OBJS-all) $(DEPS-all) $(DEPSTMP-all) \
+          $(CMDS-all) .$(LIB).cmd $(INSTALL-FILES-all) *.pmd.c *.pmd.o
+	$(Q)rm -f $(_BUILD_TARGETS) $(_INSTALL_TARGETS) $(_CLEAN_TARGETS)
+
+else
+ifeq ($(CONFIG_RTE_BUILD_SHARED_LIB),y)
+LIB := $(patsubst %.a,%.so.$(LIBABIVER),$(LIB))
+ifeq ($(EXTLIB_BUILD),n)
+CPU_LDFLAGS += --version-script=$(SRCDIR)/$(EXPORT_MAP)
+endif
+endif
+
 O_TO_S = $(LD) -L$(RTE_SDK_BIN)/lib $(_CPU_LDFLAGS) $(EXTRA_LDFLAGS) \
 	  -shared $(OBJS-y) $(NO_UNDEFINED) $(LDLIBS) -Wl,-soname,$(LIB) -o $(LIB)
 O_TO_S_STR = $(subst ','\'',$(O_TO_S)) #'# fix syntax highlight
@@ -148,6 +234,8 @@ doclean:
 	  $(CMDS-all) .$(LIB).cmd $(INSTALL-FILES-all) *.pmd.c *.pmd.o
 	$(Q)rm -f $(_BUILD_TARGETS) $(_INSTALL_TARGETS) $(_CLEAN_TARGETS)
 
+endif
+
 include $(RTE_SDK)/mk/internal/rte.compile-post.mk
 include $(RTE_SDK)/mk/internal/rte.install-post.mk
 include $(RTE_SDK)/mk/internal/rte.clean-post.mk
-- 
2.19.1

