浏览代码

Improving Makefile support for shared libraries.

-) Properly linking built-in OpenSSL into the grpc library.
-) grpc now properly depends on gpr when linking shared code.
-) Properly naming the shared library with all their aliases.
-) Properly installing the shared library aliases on the system.
-) Potentially supporting Darwin and MINGW32 targets for shared libraries.

Caveat: if using shared libraries, some tests will not compile anymore if they want to use OpenSSL functions, as they are no longer publically available externally. Which is the feature we were seeking.

The Makefile currently does it properly, by linking the tests statically. This only applies when using external code and Makefiles, if said external code improperly assumes SSL is available through grpc.
	Change on 2014/12/22 by nnoble <nnoble@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=82656438
nnoble 11 年之前
父节点
当前提交
5b7f32a2bb
共有 5 个文件被更改,包括 193 次插入40 次删除
  1. 75 16
      Makefile
  2. 6 0
      build.json
  3. 104 24
      templates/Makefile.template
  4. 3 0
      vsprojects/vs2013/grpc.sln
  5. 5 0
      vsprojects/vs2013/grpc.vcxproj

文件差异内容过多而无法显示
+ 75 - 16
Makefile


+ 6 - 0
build.json

@@ -278,6 +278,9 @@
         "src/core/tsi/transport_security.h",
         "src/core/tsi/transport_security.h",
         "src/core/tsi/transport_security_interface.h",
         "src/core/tsi/transport_security_interface.h",
         "src/core/tsi/transport_security_test_lib.h"
         "src/core/tsi/transport_security_test_lib.h"
+      ],
+      "deps": [
+        "gpr"
       ]
       ]
     },
     },
     {
     {
@@ -356,6 +359,9 @@
         "src/cpp/server/thread_pool.h",
         "src/cpp/server/thread_pool.h",
         "src/cpp/stream/stream_context.h",
         "src/cpp/stream/stream_context.h",
         "src/cpp/util/time.h"
         "src/cpp/util/time.h"
+      ],
+      "deps": [
+        "grpc"
       ]
       ]
     },
     },
     {
     {

+ 104 - 24
templates/Makefile.template

@@ -99,7 +99,6 @@ LDFLAGS += -g -pthread -fPIC
 INCLUDES = . include gens
 INCLUDES = . include gens
 LIBS = rt m z event event_pthreads pthread
 LIBS = rt m z event event_pthreads pthread
 LIBSXX = protobuf
 LIBSXX = protobuf
-LIBS_SECURE = ssl crypto dl
 LIBS_PROTOC = protoc protobuf
 LIBS_PROTOC = protoc protobuf
 
 
 ifneq ($(wildcard /usr/src/gtest/src/gtest-all.cc),)
 ifneq ($(wildcard /usr/src/gtest/src/gtest-all.cc),)
@@ -124,7 +123,6 @@ CPPFLAGS += $(CPPFLAGS_NO_ARCH) $(ARCH_FLAGS)
 LDFLAGS += $(ARCH_FLAGS)
 LDFLAGS += $(ARCH_FLAGS)
 LDLIBS += $(addprefix -l, $(LIBS))
 LDLIBS += $(addprefix -l, $(LIBS))
 LDLIBSXX += $(addprefix -l, $(LIBSXX))
 LDLIBSXX += $(addprefix -l, $(LIBSXX))
-LDLIBS_SECURE += $(addprefix -l, $(LIBS_SECURE))
 HOST_LDLIBS_PROTOC += $(addprefix -l, $(LIBS_PROTOC))
 HOST_LDLIBS_PROTOC += $(addprefix -l, $(LIBS_PROTOC))
 
 
 HOST_CPPFLAGS = $(CPPFLAGS)
 HOST_CPPFLAGS = $(CPPFLAGS)
@@ -142,6 +140,16 @@ ifeq ($(SYSTEM),)
 SYSTEM = $(HOST_SYSTEM)
 SYSTEM = $(HOST_SYSTEM)
 endif
 endif
 
 
+ifeq ($(SYSTEM),MINGW32)
+SHARED_EXT = dll
+endif
+ifeq ($(SYSTEM),Darwin)
+SHARED_EXT = dylib
+endif
+ifeq ($(SHARED_EXT),)
+SHARED_EXT = so.$(VERSION)
+endif
+
 ifeq ($(wildcard .git),)
 ifeq ($(wildcard .git),)
 IS_GIT_FOLDER = false
 IS_GIT_FOLDER = false
 else
 else
@@ -196,11 +204,16 @@ OPENSSL_DEP = third_party/openssl/libssl.a
 OPENSSL_MERGE_LIBS += third_party/openssl/libssl.a third_party/openssl/libcrypto.a
 OPENSSL_MERGE_LIBS += third_party/openssl/libssl.a third_party/openssl/libcrypto.a
 CPPFLAGS += -Ithird_party/openssl/include
 CPPFLAGS += -Ithird_party/openssl/include
 LDFLAGS += -Lthird_party/openssl
 LDFLAGS += -Lthird_party/openssl
+LIBS_SECURE = dl
 else
 else
 NO_SECURE = true
 NO_SECURE = true
 endif
 endif
+else
+LIBS_SECURE = ssl crypto dl
 endif
 endif
 
 
+LDLIBS_SECURE += $(addprefix -l, $(LIBS_SECURE))
+
 ifneq ($(DEP_MISSING),)
 ifneq ($(DEP_MISSING),)
 NO_DEPS = true
 NO_DEPS = true
 endif
 endif
@@ -310,7 +323,7 @@ shared: shared_c shared_cxx
 shared_c: dep_c\
 shared_c: dep_c\
 % for lib in libs:
 % for lib in libs:
 % if lib.build == 'all' and not lib.get('c++', False):
 % if lib.build == 'all' and not lib.get('c++', False):
- libs/$(TGTDIR)/lib${lib.name}.so.$(VERSION)\
+ libs/$(TGTDIR)/lib${lib.name}.$(SHARED_EXT)\
 % endif
 % endif
 % endfor
 % endfor
 
 
@@ -318,7 +331,7 @@ shared_c: dep_c\
 shared_cxx: dep_cxx\
 shared_cxx: dep_cxx\
 % for lib in libs:
 % for lib in libs:
 % if lib.build == 'all' and lib.get('c++', False):
 % if lib.build == 'all' and lib.get('c++', False):
- libs/$(TGTDIR)/lib${lib.name}.so.$(VERSION)\
+ libs/$(TGTDIR)/lib${lib.name}.$(SHARED_EXT)\
 % endif
 % endif
 % endfor
 % endfor
 
 
@@ -436,7 +449,7 @@ strip-shared_c: shared_c
 % if not lib.get("c++", False):
 % if not lib.get("c++", False):
 % if lib.build == "all":
 % if lib.build == "all":
 	$(E) "[STRIP]   Stripping lib${lib.name}.so"
 	$(E) "[STRIP]   Stripping lib${lib.name}.so"
-	$(Q) $(STRIP) libs/$(TGTDIR)/lib${lib.name}.so.$(VERSION)
+	$(Q) $(STRIP) libs/$(TGTDIR)/lib${lib.name}.$(SHARED_EXT)
 % endif
 % endif
 % endif
 % endif
 % endfor
 % endfor
@@ -446,7 +459,7 @@ strip-shared_cxx: shared_cxx
 % if lib.get("c++", False):
 % if lib.get("c++", False):
 % if lib.build == "all":
 % if lib.build == "all":
 	$(E) "[STRIP]   Stripping lib${lib.name}.so"
 	$(E) "[STRIP]   Stripping lib${lib.name}.so"
-	$(Q) $(STRIP) libs/$(TGTDIR)/lib${lib.name}.so.$(VERSION)
+	$(Q) $(STRIP) libs/$(TGTDIR)/lib${lib.name}.$(SHARED_EXT)
 % endif
 % endif
 % endif
 % endif
 % endfor
 % endfor
@@ -569,21 +582,49 @@ install-shared_c: shared_c strip-shared_c
 % for lib in libs:
 % for lib in libs:
 % if not lib.get("c++", False):
 % if not lib.get("c++", False):
 % if lib.build == "all":
 % if lib.build == "all":
-	$(E) "[INSTALL] Installing lib${lib.name}.so"
-	$(Q) $(INSTALL) libs/$(TGTDIR)/lib${lib.name}.so.$(VERSION) $(prefix)/lib/lib${lib.name}.so.$(VERSION)
+ifeq ($(SYSTEM),MINGW32)
+	$(E) "[INSTALL] Installing ${lib.name}.$(SHARED_EXT)"
+	$(Q) $(INSTALL) libs/$(TGTDIR)/${lib.name}.$(SHARED_EXT) $(prefix)/lib/${lib.name}.$(SHARED_EXT)
+	$(Q) $(INSTALL) libs/$(TGTDIR)/lib${lib.name}-imp.a $(prefix)/lib/lib${lib.name}-imp.a
+else
+	$(E) "[INSTALL] Installing lib${lib.name}.$(SHARED_EXT)"
+	$(Q) $(INSTALL) libs/$(TGTDIR)/lib${lib.name}.$(SHARED_EXT) $(prefix)/lib/lib${lib.name}.$(SHARED_EXT)
+ifneq ($(SYSTEM),Darwin)
+	$(Q) ln -sf lib${lib.name}.$(SHARED_EXT) $(prefix)/lib/lib${lib.name}.so
+endif
+endif
 % endif
 % endif
 % endif
 % endif
 % endfor
 % endfor
+ifneq ($(SYSTEM),MINGW32)
+ifneq ($(SYSTEM),Darwin)
+	$(Q) ldconfig
+endif
+endif
 
 
 install-shared_cxx: shared_cxx strip-shared_cxx
 install-shared_cxx: shared_cxx strip-shared_cxx
 % for lib in libs:
 % for lib in libs:
 % if lib.get("c++", False):
 % if lib.get("c++", False):
 % if lib.build == "all":
 % if lib.build == "all":
-	$(E) "[INSTALL] Installing lib${lib.name}.so"
-	$(Q) $(INSTALL) libs/$(TGTDIR)/lib${lib.name}.so.$(VERSION) $(prefix)/lib/lib${lib.name}.so.$(VERSION)
+ifeq ($(SYSTEM),MINGW32)
+	$(E) "[INSTALL] Installing ${lib.name}.$(SHARED_EXT)"
+	$(Q) $(INSTALL) libs/$(TGTDIR)/${lib.name}.$(SHARED_EXT) $(prefix)/lib/${lib.name}.$(SHARED_EXT)
+	$(Q) $(INSTALL) libs/$(TGTDIR)/lib${lib.name}-imp.a $(prefix)/lib/lib${lib.name}-imp.a
+else
+	$(E) "[INSTALL] Installing lib${lib.name}.$(SHARED_EXT)"
+	$(Q) $(INSTALL) libs/$(TGTDIR)/lib${lib.name}.$(SHARED_EXT) $(prefix)/lib/lib${lib.name}.$(SHARED_EXT)
+ifneq ($(SYSTEM),Darwin)
+	$(Q) ln -sf lib${lib.name}.$(SHARED_EXT) $(prefix)/lib/lib${lib.name}.so
+endif
+endif
 % endif
 % endif
 % endif
 % endif
 % endfor
 % endfor
+ifneq ($(SYSTEM),MINGW32)
+ifneq ($(SYSTEM),Darwin)
+	$(Q) ldconfig
+endif
+endif
 
 
 clean:\
 clean:\
 % for lib in libs:
 % for lib in libs:
@@ -639,6 +680,14 @@ ifeq ($(NO_SECURE),true)
 
 
 libs/$(TGTDIR)/lib${lib.name}.a: openssl_dep_error
 libs/$(TGTDIR)/lib${lib.name}.a: openssl_dep_error
 
 
+% if lib.build == "all":
+ifeq ($(SYSTEM),MINGW32)
+libs/$(TGTDIR)/${lib.name}.$(SHARED_EXT): openssl_dep_error
+else
+libs/$(TGTDIR)/lib${lib.name}.$(SHARED_EXT): openssl_dep_error
+endif
+% endif
+
 else
 else
 
 
 libs/$(TGTDIR)/lib${lib.name}.a: $(OPENSSL_DEP) $(LIB${lib.name.upper()}_OBJS)
 libs/$(TGTDIR)/lib${lib.name}.a: $(OPENSSL_DEP) $(LIB${lib.name.upper()}_OBJS)
@@ -659,19 +708,51 @@ libs/$(TGTDIR)/lib${lib.name}.a: $(LIB${lib.name.upper()}_OBJS)
 % endif
 % endif
 % endif
 % endif
 
 
+<%
+  if lib.get('c++', False):
+    ld = '$(LDXX)'
+  else:
+    ld = '$(LD)'
+
+  out_base = 'libs/$(TGTDIR)/' + lib.name
+  out_libbase = 'libs/$(TGTDIR)/lib' + lib.name
+
+  common = '$(LIB' + lib.name.upper() + '_OBJS) $(LDLIBS)'
+
+  libs = ''
+  lib_deps = ''
+  mingw_libs = ''
+  mingw_lib_deps = ''
+  for dep in lib.get('deps', []):
+    libs = libs + ' -l' + dep
+    lib_deps = lib_deps + 'libs/$(TGTDIR)/lib' + dep + '.$(SHARED_EXT)'
+    mingw_libs = mingw_libs + ' -l' + dep + '-imp'
+    mingw_lib_deps = mingw_lib_deps + 'libs/$(TGTDIR)/' + dep + '.$(SHARED_EXT)'
+
+  if lib.get('secure', True):
+    common = common + ' $(LDLIBS_SECURE) $(OPENSSL_MERGE_LIBS)'
+    lib_deps = lib_deps + ' $(OPENSSL_DEP)'
+    mingw_lib_deps = mingw_lib_deps + ' $(OPENSSL_DEP)'
+
+%>
+
 % if lib.build == "all":
 % if lib.build == "all":
-libs/$(TGTDIR)/lib${lib.name}.so.$(VERSION): $(LIB${lib.name.upper()}_OBJS)
+ifeq ($(SYSTEM),MINGW32)
+${out_base}.$(SHARED_EXT): $(LIB${lib.name.upper()}_OBJS) ${mingw_lib_deps}
 	$(E) "[LD]      Linking $@"
 	$(E) "[LD]      Linking $@"
 	$(Q) mkdir -p `dirname $@`
 	$(Q) mkdir -p `dirname $@`
-% if lib.get('c++', False):
-	$(Q) $(LDXX) $(LDFLAGS) -shared -Wl,-soname,lib${lib.name}.so.${settings.version.major} \
-% else:
-	$(Q) $(LD) $(LDFLAGS) -shared -Wl,-soname,lib${lib.name}.so.${settings.version.major} \
-% endif
--o libs/$(TGTDIR)/lib${lib.name}.so.$(VERSION) $(LIB${lib.name.upper()}_OBJS) $(LDLIBS)\
-% if lib.get('secure', True):
- $(LDLIBS_SECURE)\
-% endif
+	$(Q) ${ld} $(LDFLAGS) -Llibs/$(TGTDIR) -shared -Wl,--output-def=${out_base}.def -Wl,--out-implib=${out_libbase}-imp.a -o ${out_base}.$(SHARED_EXT) ${common}${mingw_libs}
+else
+${out_libbase}.$(SHARED_EXT): $(LIB${lib.name.upper()}_OBJS) ${lib_deps}
+	$(E) "[LD]      Linking $@"
+	$(Q) mkdir -p `dirname $@`
+ifeq ($(SYSTEM),Darwin)
+	$(Q) ${ld} $(LDFLAGS) -Llibs/$(TGTDIR) -dynamiclib -o ${out_libbase}.$(SHARED_EXT) ${common}${libs}
+else
+	$(Q) ${ld} $(LDFLAGS) -Llibs/$(TGTDIR) -shared -Wl,-soname,lib${lib.name}.so.${settings.version.major} -o ${out_libbase}.$(SHARED_EXT) ${common}${libs}
+	$(Q) ln -sf lib${lib.name}.$(SHARED_EXT) ${out_libbase}.so
+endif
+endif
 % endif
 % endif
 
 
 % if lib.get('secure', True):
 % if lib.get('secure', True):
@@ -696,7 +777,7 @@ clean_lib${lib.name}:
 	$(Q) $(RM) $(LIB${lib.name.upper()}_OBJS)
 	$(Q) $(RM) $(LIB${lib.name.upper()}_OBJS)
 	$(Q) $(RM) $(LIB${lib.name.upper()}_DEPS)
 	$(Q) $(RM) $(LIB${lib.name.upper()}_DEPS)
 	$(Q) $(RM) libs/$(TGTDIR)/lib${lib.name}.a
 	$(Q) $(RM) libs/$(TGTDIR)/lib${lib.name}.a
-	$(Q) $(RM) libs/$(TGTDIR)/lib${lib.name}.so.$(VERSION)
+	$(Q) $(RM) libs/$(TGTDIR)/lib${lib.name}.$(SHARED_EXT)
 </%def>
 </%def>
 
 
 <%def name="maketarget(tgt)">
 <%def name="maketarget(tgt)">
@@ -736,14 +817,13 @@ bins/$(TGTDIR)/${tgt.name}: $(${tgt.name.upper()}_OBJS)\
 % if tgt.build == 'test':
 % if tgt.build == 'test':
  $(GTEST_LIB)\
  $(GTEST_LIB)\
 % endif
 % endif
- -Llibs/$(TGTDIR)\
 % else:
 % else:
 	$(E) "[LD]      Linking $@"
 	$(E) "[LD]      Linking $@"
 	$(Q) mkdir -p `dirname $@`
 	$(Q) mkdir -p `dirname $@`
-	$(Q) $(LD) $(LDFLAGS) $(${tgt.name.upper()}_OBJS) -Llibs/$(TGTDIR)\
+	$(Q) $(LD) $(LDFLAGS) $(${tgt.name.upper()}_OBJS)\
 % endif
 % endif
 % for dep in tgt.deps:
 % for dep in tgt.deps:
- -l${dep}\
+ libs/$(TGTDIR)/lib${dep}.a\
 % endfor
 % endfor
 % if tgt.get("c++", False):
 % if tgt.get("c++", False):
 % if tgt.build == 'protoc':
 % if tgt.build == 'protoc':

+ 3 - 0
vsprojects/vs2013/grpc.sln

@@ -6,6 +6,9 @@ MinimumVisualStudioVersion = 10.0.40219.1
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "gpr", "gpr.vcxproj", "{B23D3D1A-9438-4EDA-BEB6-9A0A03D17792}"
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "gpr", "gpr.vcxproj", "{B23D3D1A-9438-4EDA-BEB6-9A0A03D17792}"
 EndProject
 EndProject
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "grpc", "grpc.vcxproj", "{29D16885-7228-4C31-81ED-5F9187C7F2A9}"
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "grpc", "grpc.vcxproj", "{29D16885-7228-4C31-81ED-5F9187C7F2A9}"
+	ProjectSection(ProjectDependencies) = postProject
+		{B23D3D1A-9438-4EDA-BEB6-9A0A03D17792} = {B23D3D1A-9438-4EDA-BEB6-9A0A03D17792}
+	EndProjectSection
 EndProject
 EndProject
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "grpc_test_util", "grpc_test_util.vcxproj", "{17BCAFC0-5FDC-4C94-AEB9-95F3E220614B}"
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "grpc_test_util", "grpc_test_util.vcxproj", "{17BCAFC0-5FDC-4C94-AEB9-95F3E220614B}"
 EndProject
 EndProject

+ 5 - 0
vsprojects/vs2013/grpc.vcxproj

@@ -345,6 +345,11 @@
     <ClCompile Include="..\..\third_party\cJSON\cJSON.c">
     <ClCompile Include="..\..\third_party\cJSON\cJSON.c">
     </ClCompile>
     </ClCompile>
   </ItemGroup>
   </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="gpr.vcxproj">
+      <Project>{B23D3D1A-9438-4EDA-BEB6-9A0A03D17792}</Project>
+    </ProjectReference>
+  </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
   </ImportGroup>

部分文件因为文件数量过多而无法显示