Move version.cpp out of hostcommon - only used by hostpolicy (#83864)
authorElinor Fung <elfung@microsoft.com>
Fri, 24 Mar 2023 17:10:53 +0000 (10:10 -0700)
committerGitHub <noreply@github.com>
Fri, 24 Mar 2023 17:10:53 +0000 (10:10 -0700)
src/native/corehost/hostcommon/files.cmake
src/native/corehost/hostpolicy/files.cmake
src/native/corehost/hostpolicy/version.cpp [new file with mode: 0644]
src/native/corehost/hostpolicy/version.h [new file with mode: 0644]
src/native/corehost/version.cpp [deleted file]
src/native/corehost/version.h [deleted file]

index a79ac208399e98dfc0fd6adb31f5aac0df39dc1b..43bd124b9b592914d26c2ffc494ff94a0e5e00f3 100644 (file)
@@ -12,7 +12,6 @@ list(APPEND SOURCES
     ${CMAKE_CURRENT_LIST_DIR}/../fx_definition.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../fx_reference.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../fxr/fx_ver.cpp
-    ${CMAKE_CURRENT_LIST_DIR}/../version.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../version_compatibility_range.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../runtime_config.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../bundle/info.cpp
@@ -27,7 +26,6 @@ list(APPEND HEADERS
     ${CMAKE_CURRENT_LIST_DIR}/../fx_definition.h
     ${CMAKE_CURRENT_LIST_DIR}/../fx_reference.h
     ${CMAKE_CURRENT_LIST_DIR}/../fxr/fx_ver.h
-    ${CMAKE_CURRENT_LIST_DIR}/../version.h
     ${CMAKE_CURRENT_LIST_DIR}/../version_compatibility_range.h
     ${CMAKE_CURRENT_LIST_DIR}/../runtime_config.h
     ${CMAKE_CURRENT_LIST_DIR}/../bundle/info.h
index bf7161574e5d3e619f0898cdc8190abdb6a8322a..1d9cbd396860461e0f088b850b5e4d130e1676c9 100644 (file)
@@ -17,6 +17,7 @@ list(APPEND SOURCES
     ${CMAKE_CURRENT_LIST_DIR}/hostpolicy.cpp
     ${CMAKE_CURRENT_LIST_DIR}/hostpolicy_init.cpp
     ${CMAKE_CURRENT_LIST_DIR}/shared_store.cpp
+    ${CMAKE_CURRENT_LIST_DIR}/version.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../bundle/dir_utils.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../bundle/extractor.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../bundle/file_entry.cpp
@@ -34,6 +35,7 @@ list(APPEND HEADERS
     ${CMAKE_CURRENT_LIST_DIR}/hostpolicy_context.h
     ${CMAKE_CURRENT_LIST_DIR}/hostpolicy_init.h
     ${CMAKE_CURRENT_LIST_DIR}/shared_store.h
+    ${CMAKE_CURRENT_LIST_DIR}/version.h
     ${CMAKE_CURRENT_LIST_DIR}/../hostpolicy.h
     ${CMAKE_CURRENT_LIST_DIR}/../corehost_context_contract.h
     ${CMAKE_CURRENT_LIST_DIR}/../bundle/dir_utils.h
diff --git a/src/native/corehost/hostpolicy/version.cpp b/src/native/corehost/hostpolicy/version.cpp
new file mode 100644 (file)
index 0000000..ea64360
--- /dev/null
@@ -0,0 +1,169 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+#include <cassert>
+#include "pal.h"
+#include "version.h"
+
+// This type matches semantics of System.Version used by tooling and differs from fx_ver_t:
+// -- version_t does not require the third segment
+// -- version_t does not support the "pre" label behavior
+// -- if version_t is an empty value (-1 for all segments) then as_str() returns an empty string
+// -- Different terminology; fx_ver_t(major, minor, patch, build) vs version_t(major, minor, build, revision)
+
+version_t::version_t() : version_t(-1, -1, -1, -1) { }
+
+version_t::version_t(int major, int minor, int build, int revision)
+    : m_major(major)
+    , m_minor(minor)
+    , m_build(build)
+    , m_revision(revision) { }
+
+bool version_t::operator ==(const version_t& b) const
+{
+    return compare(*this, b) == 0;
+}
+
+bool version_t::operator !=(const version_t& b) const
+{
+    return !operator ==(b);
+}
+
+bool version_t::operator <(const version_t& b) const
+{
+    return compare(*this, b) < 0;
+}
+
+bool version_t::operator >(const version_t& b) const
+{
+    return compare(*this, b) > 0;
+}
+
+bool version_t::operator <=(const version_t& b) const
+{
+    return compare(*this, b) <= 0;
+}
+
+bool version_t::operator >=(const version_t& b) const
+{
+    return compare(*this, b) >= 0;
+}
+
+pal::string_t version_t::as_str() const
+{
+    pal::stringstream_t stream;
+
+    if (m_major >= 0)
+    {
+        stream << m_major;
+
+        if (m_minor >= 0)
+        {
+            stream << _X(".") << m_minor;
+
+            if (m_build >= 0)
+            {
+                stream << _X(".") << m_build;
+
+                if (m_revision >= 0)
+                {
+                    stream << _X(".") << m_revision;
+                }
+            }
+        }
+    }
+
+    return stream.str();
+}
+
+/*static*/ int version_t::compare(const version_t&a, const version_t& b)
+{
+    if (a.m_major != b.m_major)
+    {
+        return (a.m_major > b.m_major) ? 1 : -1;
+    }
+
+    if (a.m_minor != b.m_minor)
+    {
+        return (a.m_minor > b.m_minor) ? 1 : -1;
+    }
+
+    if (a.m_build != b.m_build)
+    {
+        return (a.m_build > b.m_build) ? 1 : -1;
+    }
+
+    if (a.m_revision != b.m_revision)
+    {
+        return (a.m_revision > b.m_revision) ? 1 : -1;
+    }
+
+    return 0;
+}
+
+bool parse_internal(const pal::string_t& ver, version_t* ver_out)
+{
+    unsigned major = -1;
+    size_t maj_start = 0;
+    size_t maj_sep = ver.find(_X('.'));
+    if (maj_sep == pal::string_t::npos)
+    {
+        return false; // minor required
+    }
+    if (!try_stou(ver.substr(maj_start, maj_sep), &major))
+    {
+        return false;
+    }
+
+    unsigned minor = -1;
+    size_t min_start = maj_sep + 1;
+    size_t min_sep = ver.find(_X('.'), min_start);
+    if (min_sep == pal::string_t::npos)
+    {
+        if (!try_stou(ver.substr(min_start), &minor))
+        {
+            return false;
+        }
+        *ver_out = version_t(major, minor, -1, -1);
+        return true; // build and revision not required
+    }
+    if (!try_stou(ver.substr(min_start, min_sep - min_start), &minor))
+    {
+        return false;
+    }
+
+    unsigned build = -1;
+    size_t build_start = min_sep + 1;
+    size_t build_sep = ver.find(_X('.'), build_start);
+    if (build_sep == pal::string_t::npos)
+    {
+        if (!try_stou(ver.substr(build_start), &build))
+        {
+            return false;
+        }
+        *ver_out = version_t(major, minor, build, -1);
+        return true; // revision not required
+    }
+    if (!try_stou(ver.substr(build_start, build_sep - build_start), &build))
+    {
+        return false;
+    }
+
+    unsigned revision = -1;
+    size_t revision_start = build_sep + 1;
+    if (!try_stou(ver.substr(revision_start), &revision))
+    {
+        return false;
+    }
+    *ver_out = version_t(major, minor, build, revision);
+
+    return true;
+}
+
+/* static */
+bool version_t::parse(const pal::string_t& ver, version_t* ver_out)
+{
+    bool valid = parse_internal(ver, ver_out);
+    assert(!valid || ver_out->as_str() == ver);
+    return valid;
+}
diff --git a/src/native/corehost/hostpolicy/version.h b/src/native/corehost/hostpolicy/version.h
new file mode 100644 (file)
index 0000000..84c69d9
--- /dev/null
@@ -0,0 +1,40 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+#ifndef __VERSION_H__
+#define __VERSION_H__
+
+#include "pal.h"
+#include "utils.h"
+
+struct version_t
+{
+    version_t();
+    version_t(int major, int minor, int build, int revision);
+
+    int get_major() const { return m_major; }
+    int get_minor() const { return m_minor; }
+    int get_build() const { return m_build; }
+    // int get_revision() const { return m_revision; }
+
+    pal::string_t as_str() const;
+
+    bool operator ==(const version_t& b) const;
+    bool operator !=(const version_t& b) const;
+    bool operator <(const version_t& b) const;
+    bool operator >(const version_t& b) const;
+    bool operator <=(const version_t& b) const;
+    bool operator >=(const version_t& b) const;
+
+    static bool parse(const pal::string_t& ver, version_t* ver_out);
+
+private:
+    int m_major;
+    int m_minor;
+    int m_build;
+    int m_revision;
+
+    static int compare(const version_t&a, const version_t& b);
+};
+
+#endif // __VERSION_H__
diff --git a/src/native/corehost/version.cpp b/src/native/corehost/version.cpp
deleted file mode 100644 (file)
index ea64360..0000000
+++ /dev/null
@@ -1,169 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-#include <cassert>
-#include "pal.h"
-#include "version.h"
-
-// This type matches semantics of System.Version used by tooling and differs from fx_ver_t:
-// -- version_t does not require the third segment
-// -- version_t does not support the "pre" label behavior
-// -- if version_t is an empty value (-1 for all segments) then as_str() returns an empty string
-// -- Different terminology; fx_ver_t(major, minor, patch, build) vs version_t(major, minor, build, revision)
-
-version_t::version_t() : version_t(-1, -1, -1, -1) { }
-
-version_t::version_t(int major, int minor, int build, int revision)
-    : m_major(major)
-    , m_minor(minor)
-    , m_build(build)
-    , m_revision(revision) { }
-
-bool version_t::operator ==(const version_t& b) const
-{
-    return compare(*this, b) == 0;
-}
-
-bool version_t::operator !=(const version_t& b) const
-{
-    return !operator ==(b);
-}
-
-bool version_t::operator <(const version_t& b) const
-{
-    return compare(*this, b) < 0;
-}
-
-bool version_t::operator >(const version_t& b) const
-{
-    return compare(*this, b) > 0;
-}
-
-bool version_t::operator <=(const version_t& b) const
-{
-    return compare(*this, b) <= 0;
-}
-
-bool version_t::operator >=(const version_t& b) const
-{
-    return compare(*this, b) >= 0;
-}
-
-pal::string_t version_t::as_str() const
-{
-    pal::stringstream_t stream;
-
-    if (m_major >= 0)
-    {
-        stream << m_major;
-
-        if (m_minor >= 0)
-        {
-            stream << _X(".") << m_minor;
-
-            if (m_build >= 0)
-            {
-                stream << _X(".") << m_build;
-
-                if (m_revision >= 0)
-                {
-                    stream << _X(".") << m_revision;
-                }
-            }
-        }
-    }
-
-    return stream.str();
-}
-
-/*static*/ int version_t::compare(const version_t&a, const version_t& b)
-{
-    if (a.m_major != b.m_major)
-    {
-        return (a.m_major > b.m_major) ? 1 : -1;
-    }
-
-    if (a.m_minor != b.m_minor)
-    {
-        return (a.m_minor > b.m_minor) ? 1 : -1;
-    }
-
-    if (a.m_build != b.m_build)
-    {
-        return (a.m_build > b.m_build) ? 1 : -1;
-    }
-
-    if (a.m_revision != b.m_revision)
-    {
-        return (a.m_revision > b.m_revision) ? 1 : -1;
-    }
-
-    return 0;
-}
-
-bool parse_internal(const pal::string_t& ver, version_t* ver_out)
-{
-    unsigned major = -1;
-    size_t maj_start = 0;
-    size_t maj_sep = ver.find(_X('.'));
-    if (maj_sep == pal::string_t::npos)
-    {
-        return false; // minor required
-    }
-    if (!try_stou(ver.substr(maj_start, maj_sep), &major))
-    {
-        return false;
-    }
-
-    unsigned minor = -1;
-    size_t min_start = maj_sep + 1;
-    size_t min_sep = ver.find(_X('.'), min_start);
-    if (min_sep == pal::string_t::npos)
-    {
-        if (!try_stou(ver.substr(min_start), &minor))
-        {
-            return false;
-        }
-        *ver_out = version_t(major, minor, -1, -1);
-        return true; // build and revision not required
-    }
-    if (!try_stou(ver.substr(min_start, min_sep - min_start), &minor))
-    {
-        return false;
-    }
-
-    unsigned build = -1;
-    size_t build_start = min_sep + 1;
-    size_t build_sep = ver.find(_X('.'), build_start);
-    if (build_sep == pal::string_t::npos)
-    {
-        if (!try_stou(ver.substr(build_start), &build))
-        {
-            return false;
-        }
-        *ver_out = version_t(major, minor, build, -1);
-        return true; // revision not required
-    }
-    if (!try_stou(ver.substr(build_start, build_sep - build_start), &build))
-    {
-        return false;
-    }
-
-    unsigned revision = -1;
-    size_t revision_start = build_sep + 1;
-    if (!try_stou(ver.substr(revision_start), &revision))
-    {
-        return false;
-    }
-    *ver_out = version_t(major, minor, build, revision);
-
-    return true;
-}
-
-/* static */
-bool version_t::parse(const pal::string_t& ver, version_t* ver_out)
-{
-    bool valid = parse_internal(ver, ver_out);
-    assert(!valid || ver_out->as_str() == ver);
-    return valid;
-}
diff --git a/src/native/corehost/version.h b/src/native/corehost/version.h
deleted file mode 100644 (file)
index 84c69d9..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-#ifndef __VERSION_H__
-#define __VERSION_H__
-
-#include "pal.h"
-#include "utils.h"
-
-struct version_t
-{
-    version_t();
-    version_t(int major, int minor, int build, int revision);
-
-    int get_major() const { return m_major; }
-    int get_minor() const { return m_minor; }
-    int get_build() const { return m_build; }
-    // int get_revision() const { return m_revision; }
-
-    pal::string_t as_str() const;
-
-    bool operator ==(const version_t& b) const;
-    bool operator !=(const version_t& b) const;
-    bool operator <(const version_t& b) const;
-    bool operator >(const version_t& b) const;
-    bool operator <=(const version_t& b) const;
-    bool operator >=(const version_t& b) const;
-
-    static bool parse(const pal::string_t& ver, version_t* ver_out);
-
-private:
-    int m_major;
-    int m_minor;
-    int m_build;
-    int m_revision;
-
-    static int compare(const version_t&a, const version_t& b);
-};
-
-#endif // __VERSION_H__