Commit 90eb91a5 authored by Robert Kimball's avatar Robert Kimball Committed by Scott Cyphers

Add a more reasonable function to query ngraph version info (#3410)

* wip

* parse working with one test

* Add version information query and unit tests for parsing version strings

* add more comments

* doc strings

* style
parent 16906283
......@@ -14,7 +14,22 @@
// limitations under the License.
//*****************************************************************************
#include <cstddef>
#include "ngraph/util.hpp"
using namespace std;
extern "C" const char* get_ngraph_version_string()
{
return NGRAPH_VERSION;
}
namespace ngraph
{
void get_version(size_t& major, size_t& minor, size_t& patch, std::string& extra)
{
string version = NGRAPH_VERSION;
ngraph::parse_version_string(version, major, minor, patch, extra);
}
}
......@@ -20,10 +20,28 @@
#pragma once
#include <string>
#ifdef IN_NGRAPH_LIBRARY
#error("ngraph.hpp is for external use only")
#endif
extern "C" const char* get_ngraph_version_string();
namespace ngraph
{
/// \brief Function to query parsed version information of the version of ngraph which
/// contains this function. Version information strictly follows Semantic Versioning
/// http://semver.org
/// \param major Returns the major part of the version
/// \param minor Returns the minor part of the version
/// \param patch Returns the patch part of the version
/// \param extra Returns the extra part of the version. This includes everything following
/// the patch version number.
///
/// \note Throws a runtime_error if there is an error during parsing
void get_version(size_t& major, size_t& minor, size_t& patch, std::string& extra);
}
/// \namespace ngraph
/// \brief The Intel Nervana Graph C++ API.
......
......@@ -557,3 +557,59 @@ AxisVector ngraph::get_permutation_to_default_order(const AxisVector& axis_order
}
return out;
}
void ngraph::parse_version_string(
std::string version, size_t& major, size_t& minor, size_t& patch, string& extra)
{
// Since regex is broken in gcc 4.8 I will just manually parse the version string
// Version strings look like `0.25.0-rc.0+7c32240` or `v0.25.0-rc.0+7c32240`
size_t start;
size_t end;
extra = "";
start = (version[0] == 'v' ? 1 : 0);
end = version.find_first_of('.', start);
string major_str = version.substr(start, end - start);
start = end + 1;
end = version.find_first_of('.', start);
string minor_str = version.substr(start, end - start);
start = end + 1;
end = version.find_first_of("-+", start);
string patch_str = version.substr(start, end - start);
start = end;
if (start != string::npos)
{
extra = version.substr(start);
}
size_t err;
bool error = false;
try
{
major = stoi(major_str, &err);
if (err != major_str.size())
{
error = true;
}
minor = stoi(minor_str, &err);
if (err != minor_str.size())
{
error = true;
}
patch = stoi(patch_str, &err);
if (err != patch_str.size())
{
error = true;
}
}
catch (...)
{
error = true;
}
if (error)
{
throw runtime_error("Error parsing version string '" + version + "'");
}
}
......@@ -345,6 +345,20 @@ namespace ngraph
value_type m_value;
};
/// \brief Function to query parsed version information of the version of ngraph which
/// contains this function. Version information strictly follows Semantic Versioning
/// http://semver.org
/// \param version The major part of the version
/// \param major Returns the major part of the version
/// \param minor Returns the minor part of the version
/// \param patch Returns the patch part of the version
/// \param extra Returns the extra part of the version. This includes everything following
/// the patch version number.
///
/// \note Throws a runtime_error if there is an error during parsing
void parse_version_string(
std::string version, size_t& major, size_t& minor, size_t& patch, std::string& extra);
} // end namespace ngraph
std::ostream& operator<<(std::ostream& os, const ngraph::NodeVector& nv);
......@@ -60,6 +60,7 @@ set(SRC
input_output_assign.cpp
main.cpp
misc.cpp
ngraph_api.cpp
node_input_output.cpp
nop_elimination.cpp
op.cpp
......@@ -368,6 +369,7 @@ endif()
target_link_libraries(unit-test PRIVATE ngraph_test_util)
target_link_libraries(unit-test PRIVATE ngraph libgtest)
target_compile_definitions(unit-test PRIVATE NGRAPH_VERSION_LABEL="${NGRAPH_VERSION_LABEL}")
if (NGRAPH_JSON_ENABLE)
target_link_libraries(unit-test PRIVATE libjson)
endif()
......
//*****************************************************************************
// Copyright 2017-2019 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#include "gtest/gtest.h"
#include "ngraph/ngraph.hpp"
using namespace ngraph;
using namespace std;
TEST(ngraph_api, parse_version)
{
size_t major;
size_t minor;
size_t patch;
string extra;
{
string version = "0.25.1-rc.0+7c32240";
parse_version_string(version, major, minor, patch, extra);
EXPECT_EQ(0, major);
EXPECT_EQ(25, minor);
EXPECT_EQ(1, patch);
EXPECT_STREQ(extra.c_str(), "-rc.0+7c32240");
}
{
string version = "v0.25.1-rc.0+7c32240";
parse_version_string(version, major, minor, patch, extra);
EXPECT_EQ(0, major);
EXPECT_EQ(25, minor);
EXPECT_EQ(1, patch);
EXPECT_STREQ(extra.c_str(), "-rc.0+7c32240");
}
{
string version = "0.25.1+7c32240";
parse_version_string(version, major, minor, patch, extra);
EXPECT_EQ(0, major);
EXPECT_EQ(25, minor);
EXPECT_EQ(1, patch);
EXPECT_STREQ(extra.c_str(), "+7c32240");
}
{
string version = "0.25.1";
parse_version_string(version, major, minor, patch, extra);
EXPECT_EQ(0, major);
EXPECT_EQ(25, minor);
EXPECT_EQ(1, patch);
EXPECT_STREQ(extra.c_str(), "");
}
{
string version = "x0.25.1";
EXPECT_THROW(parse_version_string(version, major, minor, patch, extra), runtime_error);
}
{
string version = "0x.25.1";
EXPECT_THROW(parse_version_string(version, major, minor, patch, extra), runtime_error);
}
{
string version = ".25.1";
EXPECT_THROW(parse_version_string(version, major, minor, patch, extra), runtime_error);
}
{
string version = "123.456";
EXPECT_THROW(parse_version_string(version, major, minor, patch, extra), runtime_error);
}
{
string version = "";
EXPECT_THROW(parse_version_string(version, major, minor, patch, extra), runtime_error);
}
{
string version = "this is a test";
EXPECT_THROW(parse_version_string(version, major, minor, patch, extra), runtime_error);
}
}
TEST(ngraph_api, version)
{
string version_label = NGRAPH_VERSION_LABEL;
size_t expected_major;
size_t expected_minor;
size_t expected_patch;
string expected_extra;
parse_version_string(
version_label, expected_major, expected_minor, expected_patch, expected_extra);
size_t actual_major;
size_t actual_minor;
size_t actual_patch;
string actual_extra;
get_version(actual_major, actual_minor, actual_patch, actual_extra);
EXPECT_EQ(expected_major, actual_major);
EXPECT_EQ(expected_minor, actual_minor);
EXPECT_EQ(expected_patch, actual_patch);
EXPECT_STREQ(expected_extra.c_str(), actual_extra.c_str());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment