$OpenBSD: patch-Source_cmGeneratorTarget_cxx,v 1.15 2020/12/30 07:17:34 rsadowski Exp $

Index: Source/cmGeneratorTarget.cxx
--- Source/cmGeneratorTarget.cxx.orig
+++ Source/cmGeneratorTarget.cxx
@@ -4774,9 +4774,14 @@ cmGeneratorTarget::Names cmGeneratorTarget::GetLibrary
   // Check for library version properties.
   cmProp version = this->GetProperty("VERSION");
   cmProp soversion = this->GetProperty("SOVERSION");
+#if defined(__OpenBSD__)
+  if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
+      this->GetType() != cmStateEnums::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.
     version = nullptr;
@@ -4800,6 +4805,36 @@ cmGeneratorTarget::Names cmGeneratorTarget::GetLibrary
 
   // The library name.
   targetNames.Output = prefix + targetNames.Base + suffix;
+
+#if defined(__OpenBSD__)
+  // Override shared library version using LIBxxx_VERSION
+  // environment variable. Needed for OpenBSD ports system.
+  if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
+      this->GetType() == cmStateEnums::MODULE_LIBRARY) {
+
+    std::string env_vers;
+    const std::string env_name("LIB" + targetNames.Base + "_VERSION");
+    if (char * env = getenv(env_name.c_str()))
+      env_vers = env;
+
+    if (!env_vers.empty()) {
+      const size_t first = env_vers.find_first_of(".");
+      const size_t last = env_vers.find_last_of(".");
+
+      if ((first != last) || (first == std::string::npos)) {
+        std::string msg = "Bad ";
+        msg += env_name;
+        msg += " specification: ";
+        msg += env_vers;
+        this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
+      } else {
+        version = new std::string(env_vers);
+        soversion = new std::string(env_vers);
+      }
+    }
+  }
+#endif
+
 
   if (this->IsFrameworkOnApple()) {
     targetNames.Real = prefix;
