Index: Source/cmGeneratorTarget.cxx
--- Source/cmGeneratorTarget.cxx.orig
+++ Source/cmGeneratorTarget.cxx
@@ -3427,6 +3427,50 @@ cmGeneratorTarget::Names cmGeneratorTarget::GetLibrary
   targetNames.Output =
     cmStrCat(components.prefix, targetNames.Base, components.suffix);
 
+#if defined(__OpenBSD__)
+  // Override shared library version using LIBxxx_VERSION
+  // environment variable. Needed for OpenBSD ports system.
+  const auto isUseSahredLibs = [&]() -> bool {
+    std::string use_slibs;
+    return  ((cmSystemTools::GetEnv("MODCMAKE_USE_SHARED_LIBS", use_slibs) &&
+              use_slibs == std::string("yes")));
+  };
+
+  // Target name libXXX with PREFIX set to ""
+  auto getLibName = [&](std::string& baseName) {
+    const std::string toMatch = "lib";
+    if (baseName.find(toMatch) == 0 && components.prefix == "")
+        return baseName.substr(toMatch.length());
+    return baseName;
+  };
+
+  if (isUseSahredLibs() && this->GetType() == cmStateEnums::SHARED_LIBRARY) {
+    std::string lib_version;
+    const std::string lib_name = "LIB" + getLibName(targetNames.Base) + "_VERSION";
+    if (cmSystemTools::GetEnv(lib_name, lib_version)) {
+      if (!lib_version.empty()) {
+        const size_t first = lib_version.find_first_of(".");
+        const size_t last = lib_version.find_last_of(".");
+
+        if ((first != last) || (first == std::string::npos)) {
+          const std::string msg = "Bad " + lib_name + " specification: " + lib_version;
+          this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
+        }
+        else {
+          version = cmValue(new std::string(lib_version));
+          soversion = cmValue(new std::string(lib_version));
+        }
+      }
+    }
+    else {
+      if (soversion) {
+        version = cmValue(new std::string("0.0"));
+        soversion = cmValue(new std::string("0.0"));
+      }
+    }
+  }
+#endif
+
   if (this->IsFrameworkOnApple()) {
     targetNames.Real = components.prefix;
     if (!this->Makefile->PlatformIsAppleEmbedded()) {
