Skip to content

Commit

Permalink
Add custom build step for generating version header
Browse files Browse the repository at this point in the history
Generate a version header including git revision and dirty flag;
print version upon runtime startup.

Co-authored-by: Fabian Knorr <fabian.knorr@dps.uibk.ac.at>
  • Loading branch information
psalz and fknorr committed Jan 26, 2022
1 parent 2b1afac commit 0681c16
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 11 deletions.
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,20 @@ target_compile_definitions(celerity_runtime PUBLIC
CELERITY_FEATURE_UNNAMED_KERNELS=$<BOOL:${CELERITY_FEATURE_UNNAMED_KERNELS}>
)

# Generate header containing version information.
add_custom_target(
celerity_version_header
BYPRODUCTS "${CMAKE_CURRENT_LIST_DIR}/include/version.h"
COMMENT "Generating version header"
COMMAND "${CMAKE_COMMAND}"
-D "CELERITY_VERSION=${Celerity_VERSION}"
-D "CELERITY_SOURCE_DIR=${CMAKE_CURRENT_LIST_DIR}"
-P "${CMAKE_CURRENT_LIST_DIR}/cmake/GenerateVersionHeader.cmake"
DEPENDS "${CMAKE_CURRENT_LIST_DIR}/cmake/GenerateVersionHeader.cmake"
VERBATIM
)
add_dependencies(celerity_runtime celerity_version_header)

if(CELERITY_SYCL_IMPL STREQUAL "ComputeCpp")
target_link_libraries(celerity_runtime PUBLIC ComputeCpp::ComputeCpp)
endif()
Expand Down
40 changes: 40 additions & 0 deletions cmake/GenerateVersionHeader.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Split version string into components
string(REGEX MATCH "^([0-9]+)\\.([0-9]+)\\.([0-9]+)" _ ${CELERITY_VERSION})
set(CELERITY_VERSION_MAJOR ${CMAKE_MATCH_1})
set(CELERITY_VERSION_MINOR ${CMAKE_MATCH_2})
set(CELERITY_VERSION_PATCH ${CMAKE_MATCH_3})

message(VERBOSE "Celerity version is ${CELERITY_VERSION_MAJOR}.${CELERITY_VERSION_MINOR}.${CELERITY_VERSION_PATCH}")

# Attempt to obtain git revision / dirty status
set(CELERITY_VERSION_GIT_REVISION "unknown")
set(CELERITY_VERSION_GIT_IS_DIRTY 0)

find_package(Git QUIET)
if(GIT_FOUND)
execute_process(
COMMAND "${GIT_EXECUTABLE}" rev-parse --short HEAD
WORKING_DIRECTORY "${CELERITY_SOURCE_DIR}"
RESULT_VARIABLE EXIT_CODE
OUTPUT_VARIABLE GIT_REVISION
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(EXIT_CODE EQUAL 0)
set(CELERITY_VERSION_GIT_REVISION ${GIT_REVISION})
execute_process(
COMMAND "${GIT_EXECUTABLE}" diff --quiet HEAD
WORKING_DIRECTORY "${CELERITY_SOURCE_DIR}"
RESULT_VARIABLE CELERITY_VERSION_GIT_IS_DIRTY
ERROR_QUIET
)
endif()
endif()

# Simply write resulting header (which is git-ignored) into include directory.
# This way we don't need to do any additional work
# for setting up include paths, during installation etc.
configure_file("${CMAKE_CURRENT_LIST_DIR}/version.h.in"
"${CELERITY_SOURCE_DIR}/include/version.h"
@ONLY
)
10 changes: 10 additions & 0 deletions cmake/version.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

// clang-format off
#define CELERITY_VERSION_MAJOR @CELERITY_VERSION_MAJOR@
#define CELERITY_VERSION_MINOR @CELERITY_VERSION_MINOR@
#define CELERITY_VERSION_PATCH @CELERITY_VERSION_PATCH@

#define CELERITY_VERSION_GIT_REVISION "@CELERITY_VERSION_GIT_REVISION@"
#define CELERITY_VERSION_GIT_IS_DIRTY @CELERITY_VERSION_GIT_IS_DIRTY@
// clang-format on
1 change: 1 addition & 0 deletions include/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version.h
1 change: 1 addition & 0 deletions include/celerity.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "buffer.h"
#include "distr_queue.h"
#include "user_bench.h"
#include "version.h"

namespace celerity {
namespace runtime {
Expand Down
24 changes: 13 additions & 11 deletions src/runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
#include "scheduler.h"
#include "task_manager.h"
#include "user_bench.h"

#define CELERITY_STRINGIFY2(x) #x
#define CELERITY_STRINGIFY(x) CELERITY_STRINGIFY2(x)
#include "version.h"

namespace celerity {
namespace detail {
Expand All @@ -46,28 +44,32 @@ namespace detail {
return *instance;
}

auto get_pid() {
static auto get_pid() {
#ifdef _MSC_VER
return _getpid();
#else
return getpid();
#endif
}

const char* get_build_type() {
static std::string get_version_string() {
return fmt::format("{}.{}.{} {}{}", CELERITY_VERSION_MAJOR, CELERITY_VERSION_MINOR, CELERITY_VERSION_PATCH, CELERITY_VERSION_GIT_REVISION,
CELERITY_VERSION_GIT_IS_DIRTY ? "-dirty" : "");
}

static const char* get_build_type() {
#if defined(CELERITY_DETAIL_ENABLE_DEBUG)
return "debug";
#else
return "release";
#endif
}

const char* get_sycl_version() {
static std::string get_sycl_version() {
#if defined(__COMPUTECPP__)
return "ComputeCpp " CELERITY_STRINGIFY(COMPUTECPP_VERSION_MAJOR) "." CELERITY_STRINGIFY(COMPUTECPP_VERSION_MINOR) //
"." CELERITY_STRINGIFY(COMPUTECPP_VERSION_PATCH);
return fmt::format("ComputeCpp {}.{}.{}", COMPUTECPP_VERSION_MAJOR, COMPUTECPP_VERSION_MINOR, COMPUTECPP_VERSION_PATCH);
#elif defined(__HIPSYCL__) || defined(__HIPSYCL_TRANSFORM__)
return "hipSYCL " CELERITY_STRINGIFY(HIPSYCL_VERSION_MAJOR) "." CELERITY_STRINGIFY(HIPSYCL_VERSION_MINOR) "." CELERITY_STRINGIFY(HIPSYCL_VERSION_PATCH);
return fmt::format("hipSYCL {}.{}.{}", HIPSYCL_VERSION_MAJOR, HIPSYCL_VERSION_MINOR, HIPSYCL_VERSION_PATCH);
#elif CELERITY_DPCPP
return "DPC++ / Clang " __clang_version__;
#else
Expand Down Expand Up @@ -128,8 +130,8 @@ namespace detail {
});
}

default_logger->info(
logger_map({{"event", "initialized"}, {"pid", std::to_string(get_pid())}, {"build", get_build_type()}, {"sycl", get_sycl_version()}}));
default_logger->info(logger_map({{"event", "initialized"}, {"pid", std::to_string(get_pid())}, {"version", get_version_string()},
{"build", get_build_type()}, {"sycl", get_sycl_version()}}));
d_queue->init(*cfg, user_device);
}

Expand Down

0 comments on commit 0681c16

Please sign in to comment.