From e75c0fc9254ab7c2741c29ec229cc4cc33f3d543 Mon Sep 17 00:00:00 2001
From: Daniel Ylitalo <daniel@blodan.se>
Date: Wed, 15 Feb 2017 02:01:15 +0100
Subject: [PATCH] FreeBSD compatibility (#7073)

* Adding FreeBSD compatibility (non-breaking changes)

* Adding FreeBSD compatibility (BREAKING changes)
 - FreeBSD has libdl integrated into libc so -ldl is not available

* Adding FreeBSD compatibility (BREAKING changes)
 - FreeBSD has libdl integrated into libc so -ldl is not available

* - Define FreeBSD config
- Clear -ldl linkopts for FreeBSD only

* Clear -ldl linkopts for FreeBSD only

* Changing to local condition just as previously done with darwin

* Removing one indentation for syntax validation

* Removing one indentation here too

* - Changing new config name to lowercase
- Fixing buildifier warning
---
 tensorflow/BUILD                              |  6 ++++++
 tensorflow/core/BUILD                         | 21 ++++++++++++++-----
 tensorflow/core/platform/posix/error.cc       |  2 +-
 .../android_armv7a_cpu_utils_helper.cc        |  1 +
 .../android_armv7a_cpu_utils_helper.h         |  2 ++
 tensorflow/stream_executor/BUILD              | 14 +++++++------
 tensorflow/stream_executor/rng.cc             |  2 +-
 third_party/gpus/cuda/BUILD.tpl               | 12 +++++++++--
 third_party/gpus/cuda_configure.bzl           |  2 +-
 9 files changed, 46 insertions(+), 16 deletions(-)

diff --git a/tensorflow/BUILD b/tensorflow/BUILD
index e6dffdbaf4b..5c64eef1765 100644
--- a/tensorflow/BUILD
+++ b/tensorflow/BUILD
@@ -104,6 +104,12 @@ config_setting(
     visibility = ["//visibility:public"],
 )
 
+config_setting(
+    name = "freebsd",
+    values = {"cpu": "freebsd"},
+    visibility = ["//visibility:public"],
+)
+
 package_group(
     name = "internal",
     packages = ["//tensorflow/..."],
diff --git a/tensorflow/core/BUILD b/tensorflow/core/BUILD
index cb4358960c6..6e745567550 100644
--- a/tensorflow/core/BUILD
+++ b/tensorflow/core/BUILD
@@ -1196,7 +1196,10 @@ cc_library(
     ],
     copts = tf_copts(),
     defines = tf_additional_lib_defines(),
-    linkopts = ["-ldl"],
+    linkopts = select({
+        "//tensorflow:freebsd": [],
+        "//conditions:default": ["-ldl"],
+    }),
     deps = tf_additional_lib_deps() + [
         ":lib_hash_crc32c_accelerate_internal",
         ":lib_proto_parsing",
@@ -1223,7 +1226,10 @@ cc_library(
     ],
     hdrs = ["lib/gif/gif_io.h"],
     copts = tf_copts(),
-    linkopts = ["-ldl"],
+    linkopts = select({
+        "//tensorflow:freebsd": [],
+        "//conditions:default": ["-ldl"],
+    }),
     deps = [
         ":lib",
         "//tensorflow/core/platform/default/build_config:gif",
@@ -1242,7 +1248,10 @@ cc_library(
         "lib/jpeg/jpeg_mem.h",
     ],
     copts = tf_copts(),
-    linkopts = ["-ldl"],
+    linkopts = select({
+        "//tensorflow:freebsd": [],
+        "//conditions:default": ["-ldl"],
+    }),
     deps = [
         ":lib",
         "//tensorflow/core/platform/default/build_config:jpeg",
@@ -1320,8 +1329,10 @@ tf_cuda_library(
         "util/tensor_slice_util.h",
     ],
     copts = tf_copts(),
-    linkopts = [
-        "-ldl",
+    linkopts = select({
+        "//tensorflow:freebsd": [],
+        "//conditions:default": ["-ldl"],
+    }) + [
         "-lm",
     ],
     deps = [
diff --git a/tensorflow/core/platform/posix/error.cc b/tensorflow/core/platform/posix/error.cc
index 94a3a6ab5d7..df5c8008792 100644
--- a/tensorflow/core/platform/posix/error.cc
+++ b/tensorflow/core/platform/posix/error.cc
@@ -131,7 +131,7 @@ error::Code ErrnoToCode(int err_number) {
     case ENETUNREACH:   // Network unreachable
     case ENOLCK:        // No locks available
     case ENOLINK:       // Link has been severed
-#if !(defined(__APPLE__) || defined(_WIN32))
+#if !(defined(__APPLE__) || defined(__FreeBSD__) || defined(_WIN32))
     case ENONET:  // Machine is not on the network
 #endif
       code = error::UNAVAILABLE;
diff --git a/tensorflow/core/platform/profile_utils/android_armv7a_cpu_utils_helper.cc b/tensorflow/core/platform/profile_utils/android_armv7a_cpu_utils_helper.cc
index 61b6fa0c84b..927e2bc572f 100644
--- a/tensorflow/core/platform/profile_utils/android_armv7a_cpu_utils_helper.cc
+++ b/tensorflow/core/platform/profile_utils/android_armv7a_cpu_utils_helper.cc
@@ -24,6 +24,7 @@ limitations under the License.
 #include <string.h>
 #include <sys/ioctl.h>
 #include <sys/syscall.h>
+#include <sys/types.h>
 #include <unistd.h>
 
 #include "tensorflow/core/lib/strings/stringprintf.h"
diff --git a/tensorflow/core/platform/profile_utils/android_armv7a_cpu_utils_helper.h b/tensorflow/core/platform/profile_utils/android_armv7a_cpu_utils_helper.h
index c4fe2fc5b3c..09c365dd9e2 100644
--- a/tensorflow/core/platform/profile_utils/android_armv7a_cpu_utils_helper.h
+++ b/tensorflow/core/platform/profile_utils/android_armv7a_cpu_utils_helper.h
@@ -16,6 +16,8 @@ limitations under the License.
 #ifndef TENSORFLOW_PLATFORM_PROFILEUTILS_ANDROID_ARMV7A_CPU_UTILS_HELPER_H__
 #define TENSORFLOW_PLATFORM_PROFILEUTILS_ANDROID_ARMV7A_CPU_UTILS_HELPER_H__
 
+#include <sys/types.h>
+
 #include "tensorflow/core/platform/macros.h"
 #include "tensorflow/core/platform/profile_utils/i_cpu_utils_helper.h"
 #include "tensorflow/core/platform/types.h"
diff --git a/tensorflow/stream_executor/BUILD b/tensorflow/stream_executor/BUILD
index 651f7a39499..00faccced6f 100644
--- a/tensorflow/stream_executor/BUILD
+++ b/tensorflow/stream_executor/BUILD
@@ -24,9 +24,10 @@ cc_library(
         "lib/gtl/*.h",
         "platform/**/*.h",
     ]),
-    linkopts = [
-        "-ldl",
-    ],
+    linkopts = select({
+        "//tensorflow:freebsd": [],
+        "//conditions:default": ["-ldl"],
+    }),
     visibility = ["//visibility:public"],
     deps = [
         "//tensorflow/core:lib",
@@ -45,9 +46,10 @@ cc_library(
             exclude = ["cuda/cuda_platform_id.cc"],
         ),
     ),
-    linkopts = [
-        "-ldl",
-    ],
+    linkopts = select({
+        "//tensorflow:freebsd": [],
+        "//conditions:default": ["-ldl"],
+    }),
     visibility = ["//visibility:public"],
     deps = [
         ":stream_executor",
diff --git a/tensorflow/stream_executor/rng.cc b/tensorflow/stream_executor/rng.cc
index 08f91584bfc..1c05005067c 100644
--- a/tensorflow/stream_executor/rng.cc
+++ b/tensorflow/stream_executor/rng.cc
@@ -41,7 +41,7 @@ bool RngSupport::CheckSeed(const uint8 *seed, uint64 seed_bytes) {
   return true;
 }
 
-#if defined(__APPLE__)
+#if defined(__APPLE__) || defined(__FreeBSD__)
 const int RngSupport::kMinSeedBytes;
 const int RngSupport::kMaxSeedBytes;
 #endif
diff --git a/third_party/gpus/cuda/BUILD.tpl b/third_party/gpus/cuda/BUILD.tpl
index 87541056198..e101f9fbd84 100644
--- a/third_party/gpus/cuda/BUILD.tpl
+++ b/third_party/gpus/cuda/BUILD.tpl
@@ -33,6 +33,12 @@ config_setting(
     visibility = ["//visibility:public"],
 )
 
+config_setting(
+    name = "freebsd",
+    values = {"cpu": "freebsd"},
+    visibility = ["//visibility:public"],
+)
+
 cc_library(
     name = "cuda_headers",
     hdrs = glob([
@@ -49,8 +55,10 @@ cc_library(
     name = "cudart_static",
     srcs = ["lib/%{cudart_static_lib}"],
     includes = ["include/"],
-    linkopts = [
-        "-ldl",
+    linkopts = select({
+        ":freebsd": [],
+        "//conditions:default": ["-ldl"],
+    }) + [
         "-lpthread",
         %{cudart_static_linkopt}
     ],
diff --git a/third_party/gpus/cuda_configure.bzl b/third_party/gpus/cuda_configure.bzl
index 15e1dfc5217..01e070f2be3 100644
--- a/third_party/gpus/cuda_configure.bzl
+++ b/third_party/gpus/cuda_configure.bzl
@@ -368,7 +368,7 @@ def _lib_name(lib, cpu_value, version="", static=False):
   Returns:
     The platform-specific name of the library.
   """
-  if cpu_value == "Linux":
+  if cpu_value in ("Linux", "FreeBSD"):
     if static:
       return "lib%s.a" % lib
     else: