Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Versioning library #7468

Merged
merged 30 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions CMakeModules/VersionUtils.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
cmake_minimum_required(VERSION 3.5)

function(GENERATE_VERSION_METADATA)
# Execute `git` to grab the corresponding data.
execute_process(
COMMAND ${GIT_EXEC} rev-parse HEAD
WORKING_DIRECTORY ${SRC_DIR}
OUTPUT_VARIABLE V_HASH
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE )
execute_process(
COMMAND ${GIT_EXEC} diff --quiet
WORKING_DIRECTORY ${SRC_DIR}
RESULT_VARIABLE V_DIRTY
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE )

# If `V_DIRTY` is equal to 1, we know that the repository is dirty and vice versa.
if(${V_DIRTY})
set(V_DIRTY "true")
else()
set(V_DIRTY "false")
endif()

# Define the proper version metadata for the file `version_impl.cpp.in`.
set(_VERSION_MAJOR_ ${V_MAJOR})
set(_VERSION_MINOR_ ${V_MINOR})
set(_VERSION_PATCH_ ${V_PATCH})
set(_VERSION_SUFFIX_ ${V_SUFFIX})
set(_VERSION_HASH_ ${V_HASH})
set(_VERSION_DIRTY_ ${V_DIRTY})

# Modify and substitute the `.cpp.in` file for a `.cpp` in the build directory.
configure_file(
${CUR_SRC_DIR}/src/version_impl.cpp.in
${CUR_BIN_DIR}/src/version_impl.cpp
@ONLY )
endfunction(GENERATE_VERSION_METADATA)

GENERATE_VERSION_METADATA()
1 change: 1 addition & 0 deletions libraries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ add_subdirectory( wasm-jit )
add_subdirectory( appbase )
add_subdirectory( chain )
add_subdirectory( testing )
add_subdirectory( version )

#turn tools&tests off; not needed for library build
set(BUILD_TESTS OFF CACHE BOOL "Build GTest-based tests")
Expand Down
49 changes: 49 additions & 0 deletions libraries/version/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
cmake_minimum_required(VERSION 3.5)
project(Version)

# Define the version metadata by default, in case `git` cannot be found.
set(_VERSION_MAJOR_ "unknown")
set(_VERSION_MINOR_ "")
set(_VERSION_PATCH_ "")
set(_VERSION_SUFFIX_ "")
set(_VERSION_HASH_ "")
set(_VERSION_DIRTY_ "")

# Construct the library target.
add_library(
version
"${CMAKE_CURRENT_SOURCE_DIR}/src/version.cpp"
"${CMAKE_CURRENT_BINARY_DIR}/src/version_impl.cpp")

# Make dependencies visible to the given target library to be constructed.
target_include_directories(
version
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include/"
PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/" )

# Create a custom target to update the version metadata upon every build.
find_package(Git)
if(EXISTS ${CMAKE_SOURCE_DIR}/.git AND ${GIT_FOUND})
add_custom_target(
evaluate_every_build ALL
COMMAND ${CMAKE_COMMAND} -DGIT_EXEC=${GIT_EXECUTABLE}
-DCUR_BIN_DIR=${CMAKE_CURRENT_BINARY_DIR}
-DCUR_SRC_DIR=${CMAKE_CURRENT_SOURCE_DIR}
-DSRC_DIR=${CMAKE_SOURCE_DIR}
-DV_MAJOR=${VERSION_MAJOR}
-DV_MINOR=${VERSION_MINOR}
-DV_PATCH=${VERSION_PATCH}
-DV_SUFFIX=${VERSION_SUFFIX}
-P ${CMAKE_SOURCE_DIR}/CMakeModules/VersionUtils.cmake
BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/src/version_impl.cpp
COMMENT "Updating version metadata..." VERBATIM )

# Create a dependency for the given library target.
add_dependencies(version evaluate_every_build)
else()
# Modify and substitute the `.cpp.in` file for a `.cpp` in the build directory.
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/src/version_impl.cpp.in
${CMAKE_CURRENT_BINARY_DIR}/src/version_impl.cpp
@ONLY )
endif()
18 changes: 18 additions & 0 deletions libraries/version/include/eosio/version/version.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @file version.hpp
* @copyright defined in eos/LICENSE
*/

#pragma once

#include <string> // std::string

namespace eosio { namespace version {

///< Grab the basic version information of the client; example: `v1.8.0-rc1`
const std::string& version_client();

///< Grab the full version information of the client; example: `v1.8.0-rc1-7de458254[-dirty]`
const std::string& version_full();

} }
20 changes: 20 additions & 0 deletions libraries/version/src/version.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @file version.cpp
* @copyright defined in eos/LICENSE
*/

#include "version_impl.hpp"

namespace eosio { namespace version {

const std::string& version_client() {
static const std::string version{_version_client()};
return version;
}

const std::string& version_full() {
static const std::string version{_version_full()};
return version;
}

} }
45 changes: 45 additions & 0 deletions libraries/version/src/version_impl.cpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @file version_impl.cpp.in
* @copyright defined in eos/LICENSE
* \warning This file is machine generated. DO NOT EDIT. See version_impl.cpp.in for changes.
*/

#include "version_impl.hpp"

namespace eosio { namespace version {

const std::string version_major {"@_VERSION_MAJOR_@" };
const std::string version_minor {"@_VERSION_MINOR_@" };
const std::string version_patch {"@_VERSION_PATCH_@" };
const std::string version_suffix{"@_VERSION_SUFFIX_@"};
const std::string version_hash {"@_VERSION_HASH_@" };
const bool version_dirty { @_VERSION_DIRTY_@ };

std::string _version_client() {
if (version_major == "unknown") {
std::string version{"unknown"};
return version;
}
else {
std::string version{'v' + version_major + '.' + version_minor + '.' + version_patch + '-' + version_suffix};
return version;
}
}

std::string _version_full() {
if (version_major == "unknown") {
std::string version{"unknown"};
return version;
}
else {
std::string version{'v' + version_major + '.' + version_minor + '.' + version_patch + '-' + version_suffix + '-' + version_hash};

if (version_dirty == true) {
version += "-dirty";
}

return version;
}
}

} }
18 changes: 18 additions & 0 deletions libraries/version/src/version_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @file version_impl.hpp
* @copyright defined in eos/LICENSE
*/

#pragma once

#include <string> // std::string

namespace eosio { namespace version {

///< Helper function for `version_client()`
std::string _version_client();

///< Helper function for `version_full()`
std::string _version_full();

} }
19 changes: 1 addition & 18 deletions programs/cleos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,6 @@ if( GPERFTOOLS_FOUND )
list( APPEND PLATFORM_SPECIFIC_LIBS tcmalloc )
endif()

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../../.git)
find_package(Git)
if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --short=8 HEAD
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../.."
OUTPUT_VARIABLE "cleos_BUILD_VERSION"
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "Git commit revision: ${cleos_BUILD_VERSION}")
else()
set(cleos_BUILD_VERSION 0)
endif()
else()
set(cleos_BUILD_VERSION 0)
endif()

find_package(Intl REQUIRED)

set(LOCALEDIR ${CMAKE_INSTALL_PREFIX}/share/locale)
Expand All @@ -36,7 +19,7 @@ configure_file(config.hpp.in config.hpp ESCAPE_QUOTES)
target_include_directories(${CLI_CLIENT_EXECUTABLE_NAME} PUBLIC ${Intl_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})

target_link_libraries( ${CLI_CLIENT_EXECUTABLE_NAME}
PRIVATE appbase chain_api_plugin producer_plugin chain_plugin http_plugin eosio_chain fc ${CMAKE_DL_LIBS} ${PLATFORM_SPECIFIC_LIBS} ${Intl_LIBRARIES} )
PRIVATE appbase version chain_api_plugin producer_plugin chain_plugin http_plugin eosio_chain fc ${CMAKE_DL_LIBS} ${PLATFORM_SPECIFIC_LIBS} ${Intl_LIBRARIES} )


copy_bin( ${CLI_CLIENT_EXECUTABLE_NAME} )
Expand Down
11 changes: 5 additions & 6 deletions programs/cleos/config.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
#pragma once

namespace eosio { namespace client { namespace config {
constexpr char version_str[] = "${cleos_BUILD_VERSION}";
constexpr char locale_path[] = "${LOCALEDIR}";
constexpr char locale_domain[] = "${LOCALEDOMAIN}";
constexpr char key_store_executable_name[] = "${KEY_STORE_EXECUTABLE_NAME}";
constexpr char node_executable_name[] = "${NODE_EXECUTABLE_NAME}";
}}}
constexpr char locale_path[] {"${LOCALEDIR}" };
constexpr char locale_domain[] {"${LOCALEDOMAIN}" };
constexpr char key_store_executable_name[]{"${KEY_STORE_EXECUTABLE_NAME}"};
constexpr char node_executable_name[] {"${NODE_EXECUTABLE_NAME}" };
} } }
10 changes: 8 additions & 2 deletions programs/cleos/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ Usage: ./cleos create account [OPTIONS] creator name OwnerKey ActiveKey
#include <eosio/chain_plugin/chain_plugin.hpp>
#include <eosio/chain/contract_types.hpp>

#include <eosio/version/version.hpp>

#pragma push_macro("N")
#undef N

Expand Down Expand Up @@ -2402,8 +2404,12 @@ int main( int argc, char** argv ) {
auto version = app.add_subcommand("version", localized("Retrieve version information"), false);
version->require_subcommand();

version->add_subcommand("client", localized("Retrieve version information of the client"))->set_callback([] {
std::cout << localized("Build version: ${ver}", ("ver", eosio::client::config::version_str)) << std::endl;
version->add_subcommand("client", localized("Retrieve basic version information of the client"))->set_callback([] {
std::cout << eosio::version::version_client() << '\n';
});

version->add_subcommand("full", localized("Retrieve full version information of the client"))->set_callback([] {
std::cout << eosio::version::version_full() << '\n';
});

// Create subcommand
Expand Down