Fix Mac OS library loading for unittests.

On Mac OS there is no rpath to set for the executable to locate
libraries. Instead, dylibs themselves have an install_name which is the
path where executables can load them from (added to the executable at
link time).

Bug swiftshader:68

Change-Id: Ia72eba0076ec83fc4492a5c118b4f5420d03a640
Reviewed-on: https://swiftshader-review.googlesource.com/10208
Reviewed-by: Alexis Hétu <sugoi@google.com>
Tested-by: Nicolas Capens <capn@google.com>
diff --git a/src/Common/SharedLibrary.hpp b/src/Common/SharedLibrary.hpp
index 8d881c1..ab710eb 100644
--- a/src/Common/SharedLibrary.hpp
+++ b/src/Common/SharedLibrary.hpp
@@ -59,7 +59,7 @@
 		}
 	}
 
-	return 0;
+	return nullptr;
 }
 
 #if defined(_WIN32)
@@ -70,7 +70,7 @@
 
 	inline void *getLibraryHandle(const char *path)
 	{
-		HMODULE module = 0;
+		HMODULE module = NULL;
 		GetModuleHandleEx(0, path, &module);
 		return (void*)module;
 	}
@@ -103,7 +103,7 @@
 				return dlopen(path, RTLD_LAZY | RTLD_LOCAL);   // Increment reference count
 			}
 
-			return 0;
+			return nullptr;
 		#endif
 	}
 
diff --git a/src/OpenGL/libEGL/BUILD.gn b/src/OpenGL/libEGL/BUILD.gn
index fcd609b..90d87ea 100644
--- a/src/OpenGL/libEGL/BUILD.gn
+++ b/src/OpenGL/libEGL/BUILD.gn
@@ -43,10 +43,8 @@
 }
 
 shared_library("swiftshader_libEGL") {
-  if (!is_mac) {
-    output_name = "libEGL"
-    output_dir = "$root_out_dir/swiftshader"
-  }
+  output_name = "libEGL"
+  output_dir = "$root_out_dir/swiftshader"
 
   deps = [
     "//build/config:exe_and_shlib_deps",
@@ -74,13 +72,12 @@
       "Quartz.framework",
       "Cocoa.framework",
     ]
+    ldflags = [ "-Wl,-install_name,@loader_path/swiftshader/libEGL.dylib" ]
   } else if (is_win) {
     configs -= [ "//build/config/win:unicode" ]
     ldflags = [ "/DEF:" + rebase_path("libGLESv2.def", root_build_dir) ]
   } else if (is_linux) {
-    sources += [
-      "../../Main/libX11.cpp",
-    ]
+    sources += [ "../../Main/libX11.cpp" ]
     ldflags =
         [ "-Wl,--version-script=" + rebase_path("exports.map", root_build_dir) ]
   }
diff --git a/src/OpenGL/libGLESv2/BUILD.gn b/src/OpenGL/libGLESv2/BUILD.gn
index 1cdb672..cc1c8cd 100644
--- a/src/OpenGL/libGLESv2/BUILD.gn
+++ b/src/OpenGL/libGLESv2/BUILD.gn
@@ -55,16 +55,14 @@
 }
 
 shared_library("swiftshader_libGLESv2") {
-  if (!is_mac) {
-    output_name = "libGLESv2"
-    output_dir = "$root_out_dir/swiftshader"
-  }
+  output_name = "libGLESv2"
+  output_dir = "$root_out_dir/swiftshader"
 
   deps = [
-    "//build/config:exe_and_shlib_deps",
     "../../OpenGL/compiler:swiftshader_opengl_compiler",
     "../../Reactor:swiftshader_reactor",
     "../../Renderer:swiftshader_renderer",
+    "//build/config:exe_and_shlib_deps",
   ]
 
   sources = [
@@ -95,6 +93,8 @@
   if (is_win) {
     configs -= [ "//build/config/win:unicode" ]
     ldflags = [ "/DEF:" + rebase_path("libGLESv2.def", root_build_dir) ]
+  } else if (is_mac) {
+    ldflags = [ "-Wl,-install_name,@loader_path/swiftshader/libGLESv2.dylib" ]
   } else if (is_linux) {
     ldflags =
         [ "-Wl,--version-script=" + rebase_path("exports.map", root_build_dir) ]
diff --git a/tests/unittests/BUILD.gn b/tests/unittests/BUILD.gn
index 0d2a119..135ca8c 100644
--- a/tests/unittests/BUILD.gn
+++ b/tests/unittests/BUILD.gn
@@ -16,12 +16,12 @@
 
 test("swiftshader_unittests") {
   deps = [
-    "//third_party/swiftshader/src/OpenGL/libEGL:swiftshader_libEGL",
-    "//third_party/swiftshader/src/OpenGL/libGLESv2:swiftshader_libGLESv2",
     "//base",
     "//base/test:test_support",
     "//testing/gmock",
     "//testing/gtest",
+    "//third_party/swiftshader/src/OpenGL/libEGL:swiftshader_libEGL",
+    "//third_party/swiftshader/src/OpenGL/libGLESv2:swiftshader_libGLESv2",
   ]
 
   sources = [
@@ -29,13 +29,9 @@
     "unittests.cpp",
   ]
 
-  include_dirs = [
-    "../../include",   # Khronos headers
-  ]
+  include_dirs = [ "../../include" ]  # Khronos headers
 
-  defines = [
-    "GL_GLEXT_PROTOTYPES",
-  ]
+  defines = [ "GL_GLEXT_PROTOTYPES" ]
 
   # Make sure we're loading SwiftShader's libraries, not ANGLE's or the system
   # provided ones. On Windows an explicit LoadLibrary("swiftshader\lib*.dll")
@@ -46,12 +42,9 @@
       "/DELAYLOAD:libGLESv2.dll",
     ]
   } else if (is_mac) {
-    ldflags = [
-      "-Wl,-install_name,@rpath/\$ORIGIN/swiftshader",
-    ]
+    # On Mac OS the rpath is part of the dylib and a full file path instead of
+    # a directory, which is added to the executable at link time.
   } else {
-    ldflags = [
-      "-Wl,-rpath=\$ORIGIN/swiftshader",
-    ]
+    ldflags = [ "-Wl,-rpath=\$ORIGIN/swiftshader" ]
   }
 }