Merge pull request #5078 from ctiller/compatibility_mode

Make binary compatibility not the default
diff --git a/binding.gyp b/binding.gyp
index 3659bfc..82e36c4 100644
--- a/binding.gyp
+++ b/binding.gyp
@@ -55,7 +55,8 @@
           'UNICODE',
           '_UNICODE',
           'NOMINMAX',
-          'OPENSSL_NO_ASM'
+          'OPENSSL_NO_ASM',
+          'GPR_BACKWARDS_COMPATIBILITY_MODE'
         ],
         "msvs_settings": {
           'VCCLCompilerTool': {
@@ -78,7 +79,8 @@
           # supports ALPN. The target is "[major].[minor].[patch]". We split by
           # periods and take the first field to get the major version.
         'defines': [
-          'TSI_OPENSSL_ALPN_SUPPORT=<!(echo <(target) | cut -d. -f1)'
+          'TSI_OPENSSL_ALPN_SUPPORT=<!(echo <(target) | cut -d. -f1)',
+          'GPR_BACKWARDS_COMPATIBILITY_MODE'
         ],
         'include_dirs': [
           '<(node_root_dir)/deps/openssl/openssl/include',
diff --git a/include/grpc/impl/codegen/port_platform.h b/include/grpc/impl/codegen/port_platform.h
index 258063f..9256904 100644
--- a/include/grpc/impl/codegen/port_platform.h
+++ b/include/grpc/impl/codegen/port_platform.h
@@ -34,6 +34,14 @@
 #ifndef GRPC_IMPL_CODEGEN_PORT_PLATFORM_H
 #define GRPC_IMPL_CODEGEN_PORT_PLATFORM_H
 
+/*
+ * Define GPR_BACKWARDS_COMPATIBILITY_MODE to try harder to be ABI
+ * compatible with older platforms (currently only on Linux)
+ * Causes:
+ *  - some libc calls to be gotten via dlsym
+ *  - some syscalls to be made directly
+ */
+
 /* Get windows.h included everywhere (we need it) */
 #if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
 #ifndef WIN32_LEAN_AND_MEAN
diff --git a/setup.py b/setup.py
index ceca6f6..69e59e3 100644
--- a/setup.py
+++ b/setup.py
@@ -94,10 +94,10 @@
 if not "win32" in sys.platform:
   EXTENSION_LIBRARIES += ('m',)
 
-DEFINE_MACROS = (('OPENSSL_NO_ASM', 1), ('_WIN32_WINNT', 0x600))
+DEFINE_MACROS = (('OPENSSL_NO_ASM', 1), ('_WIN32_WINNT', 0x600), ('GPR_BACKWARDS_COMPATIBILITY_MODE', 1),)
 
-CFLAGS = ()
 LDFLAGS = ()
+CFLAGS = ()
 if "linux" in sys.platform:
   LDFLAGS += ('-Wl,-wrap,memcpy',)
 if "linux" in sys.platform or "darwin" in sys.platform:
diff --git a/src/core/support/env_linux.c b/src/core/support/env_linux.c
index 442cd82..1ca6fa1 100644
--- a/src/core/support/env_linux.c
+++ b/src/core/support/env_linux.c
@@ -52,6 +52,7 @@
 #include "src/core/support/string.h"
 
 char *gpr_getenv(const char *name) {
+#if defined(GPR_BACKWARDS_COMPATIBILITY_MODE)
   typedef char *(*getenv_type)(const char *);
   static getenv_type getenv_func = NULL;
   /* Check to see which getenv variant is supported (go from most
@@ -62,6 +63,10 @@
   }
   char *result = getenv_func(name);
   return result == NULL ? result : gpr_strdup(result);
+#else
+  char *result = secure_getenv(name);
+  return result == NULL ? result : gpr_strdup(result);
+#endif
 }
 
 void gpr_setenv(const char *name, const char *value) {
diff --git a/src/core/support/time_posix.c b/src/core/support/time_posix.c
index 1f92d7f..36d75e7 100644
--- a/src/core/support/time_posix.c
+++ b/src/core/support/time_posix.c
@@ -86,7 +86,7 @@
     gpr_precise_clock_now(&ret);
     return ret;
   } else {
-#if defined(__linux__) && !defined(GPR_NO_DIRECT_SYSCALLS)
+#if defined(GPR_BACKWARDS_COMPATIBILITY_MODE) && defined(__linux__)
     /* avoid ABI problems by invoking syscalls directly */
     syscall(SYS_clock_gettime, clockid_for_gpr_clock[clock_type], &now);
 #else
diff --git a/src/ruby/ext/grpc/extconf.rb b/src/ruby/ext/grpc/extconf.rb
index b7c6cb3..6b7001a 100644
--- a/src/ruby/ext/grpc/extconf.rb
+++ b/src/ruby/ext/grpc/extconf.rb
@@ -79,6 +79,7 @@
   ENV['EMBED_ZLIB'] = 'true'
   ENV['ARCH_FLAGS'] = RbConfig::CONFIG['ARCH_FLAG']
   ENV['ARCH_FLAGS'] = '-arch i386 -arch x86_64' if RUBY_PLATFORM =~ /darwin/
+  ENV['CFLAGS'] = '-DGPR_BACKWARDS_COMPATIBILITY_MODE'
 
   output_dir = File.expand_path(RbConfig::CONFIG['topdir'])
   grpc_lib_dir = File.join(output_dir, 'libs', grpc_config)
diff --git a/templates/binding.gyp.template b/templates/binding.gyp.template
index 7cee562..3ffbc06 100644
--- a/templates/binding.gyp.template
+++ b/templates/binding.gyp.template
@@ -57,7 +57,8 @@
             'UNICODE',
             '_UNICODE',
             'NOMINMAX',
-            'OPENSSL_NO_ASM'
+            'OPENSSL_NO_ASM',
+            'GPR_BACKWARDS_COMPATIBILITY_MODE'
           ],
           "msvs_settings": {
             'VCCLCompilerTool': {
@@ -80,7 +81,8 @@
             # supports ALPN. The target is "[major].[minor].[patch]". We split by
             # periods and take the first field to get the major version.
           'defines': [
-            'TSI_OPENSSL_ALPN_SUPPORT=<!(echo <(target) | cut -d. -f1)'
+            'TSI_OPENSSL_ALPN_SUPPORT=<!(echo <(target) | cut -d. -f1)',
+            'GPR_BACKWARDS_COMPATIBILITY_MODE'
           ],
           'include_dirs': [
             '<(node_root_dir)/deps/openssl/openssl/include',
diff --git a/tools/run_tests/artifact_targets.py b/tools/run_tests/artifact_targets.py
index 74d2a48..9cd02c5 100644
--- a/tools/run_tests/artifact_targets.py
+++ b/tools/run_tests/artifact_targets.py
@@ -175,7 +175,8 @@
     else:
       environ = {'CONFIG': 'opt',
                  'EMBED_OPENSSL': 'true',
-                 'EMBED_ZLIB': 'true'}
+                 'EMBED_ZLIB': 'true',
+                 'CFLAGS': '-DGPR_BACKWARDS_COMPATIBILITY_MODE'}
       if self.platform == 'linux':
         return create_docker_jobspec(self.name,
             'tools/dockerfile/grpc_artifact_linux_%s' % self.arch,