$OpenBSD: patch-Source_cmTarget_cxx,v 1.25 2015/03/26 07:10:02 dcoppa Exp $
--- Source/cmTarget.cxx.orig	Tue Mar 10 09:38:44 2015
+++ Source/cmTarget.cxx	Wed Mar 18 06:24:00 2015
@@ -322,7 +322,7 @@ void cmTarget::SetMakefile(cmMakefile* mf)
     this->SetPropertyDefault("ANDROID_API_MIN", 0);
     this->SetPropertyDefault("INSTALL_NAME_DIR", 0);
     this->SetPropertyDefault("INSTALL_RPATH", "");
-    this->SetPropertyDefault("INSTALL_RPATH_USE_LINK_PATH", "OFF");
+    this->SetPropertyDefault("INSTALL_RPATH_USE_LINK_PATH", "ON");
     this->SetPropertyDefault("SKIP_BUILD_RPATH", "OFF");
     this->SetPropertyDefault("BUILD_WITH_INSTALL_RPATH", "OFF");
     this->SetPropertyDefault("ARCHIVE_OUTPUT_DIRECTORY", 0);
@@ -3610,12 +3610,16 @@ std::string cmTarget::GetCompilePDBPath(const std::str
 //----------------------------------------------------------------------------
 bool cmTarget::HasSOName(const std::string& config) const
 {
+#if !defined(__OpenBSD__)
   // soname is supported only for shared libraries and modules,
   // and then only when the platform supports an soname flag.
   return ((this->GetType() == cmTarget::SHARED_LIBRARY ||
            this->GetType() == cmTarget::MODULE_LIBRARY) &&
           !this->GetPropertyAsBool("NO_SONAME") &&
           this->Makefile->GetSONameFlag(this->GetLinkerLanguage(config)));
+#else
+  return false;
+#endif
 }
 
 //----------------------------------------------------------------------------
@@ -4081,9 +4085,14 @@ void cmTarget::GetLibraryNames(std::string& name,
   // Check for library version properties.
   const char* version = this->GetProperty("VERSION");
   const char* soversion = this->GetProperty("SOVERSION");
+#if defined(__OpenBSD__)
+  if(this->GetType() != cmTarget::SHARED_LIBRARY &&
+     this->GetType() != cmTarget::MODULE_LIBRARY)
+#else
   if(!this->HasSOName(config) ||
      this->Makefile->IsOn("CMAKE_PLATFORM_NO_VERSIONED_SONAME") ||
      this->IsFrameworkOnApple())
+#endif
     {
     // Versioning is supported only for shared libraries and modules,
     // and then only when the platform supports an soname flag.
@@ -4111,6 +4120,36 @@ void cmTarget::GetLibraryNames(std::string& name,
   // The library name.
   name = prefix+base+suffix;
 
+#if defined(__OpenBSD__)
+  // Override shared library version using LIBxxx_VERSION
+  // environment variable. Needed for OpenBSD ports system.
+  if(this->GetType() == cmTarget::SHARED_LIBRARY ||
+     this->GetType() == cmTarget::MODULE_LIBRARY)
+    {
+    std::string env_name = "LIB" + base + "_VERSION";
+    char *env_vers_cstr = getenv(env_name.c_str());
+
+    if (env_vers_cstr && strlen(env_vers_cstr) > 0) {
+      // This means an override is present.
+      std::string env_vers = std::string(env_vers_cstr);
+
+      size_t first = env_vers.find_first_of(".");
+      size_t last = env_vers.find_first_of(".");
+
+      if ((first != last) || (first == std::string::npos)) {
+        std::string msg = "Bad ";
+        msg += env_name;
+        msg += " specification: ";
+        msg += env_vers;
+        this->Makefile->IssueMessage(cmake::FATAL_ERROR,
+                                     msg.c_str());
+      } else {
+        version = env_vers_cstr;
+      }
+    }
+  }
+#endif
+
   if(this->IsFrameworkOnApple())
     {
     realName = prefix;
@@ -4122,9 +4161,15 @@ void cmTarget::GetLibraryNames(std::string& name,
     }
   else
     {
+#if !defined(__OpenBSD__)
     // The library's soname.
     this->ComputeVersionedName(soName, prefix, base, suffix,
                                name, soversion);
+#else
+    // The library's soname.
+    this->ComputeVersionedName(soName, prefix, base, suffix,
+                               name, version);
+#endif
     // The library's real name on disk.
     this->ComputeVersionedName(realName, prefix, base, suffix,
                                name, version);
@@ -4157,7 +4202,23 @@ void cmTarget::ComputeVersionedName(std::string& vName
   if(version)
     {
     vName += ".";
+#if defined(__OpenBSD__)
+    // OpenBSD-style versioning for shared libraries.
+    // Convert libname.so.X.X.X to libname.so.X.X
+    int j = 0;
+    for (int i = 0; i < (int)strlen(version); i++)
+      {
+      if (version[i] == '.')
+        {
+        j++;
+        if (j == 2)
+          break;
+        }
+      vName += version[i];
+      }
+#else
     vName += version;
+#endif
     }
   vName += this->IsApple? suffix : std::string();
 }
