Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup version calculation #385

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion .github/actions/build-asset-unix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ fi
set -x
mkdir build-${target} && cd build-${target}
cmake .. -DCMAKE_BUILD_TYPE=Release
bash -eo pipefail ../embed-version.sh
cmake --build . --config Release --target $target -j $thread_count
chmod +x ./${exe_name}

Expand Down
61 changes: 39 additions & 22 deletions .github/actions/get-version.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,53 @@
#! /usr/bin/env bash
#!/usr/bin/env bash
# NOTE: This is meant to be run from the repo root dir
#
set -eo pipefail

os=$1
arch=$2

shift $#

# version_cmp=($(./extract-version.sh))
. ./extract-version.sh

ver_maj=$bb_ver_maj
ver_min=$bb_ver_min
ver_rev=$bb_ver_rev
ver_suffix=$bb_version_suffix

version="${ver_maj}.${ver_min}.${ver_rev}${ver_suffix}"
version_file="./VERSION"
if [ ! -f "$version_file" ]; then
echo "VERSION file not found!"
exit 1
fi

# echo "Ref name: '$GITHUB_REF_NAME'"
# if [[ "$GITHUB_REF_NAME" != "master" ]]; then
# suffix="-${GITHUB_REF_NAME}"
# fi
# Read VERSION file into an array
declare -a version_info=()
while IFS= read -r line; do
line=$(echo -n "$line" | tr -d '\r')
version_info+=("$line")
done < "$version_file"

# Extract major, minor, and revision numbers
IFS='.' read -ra ver_parts <<< "${version_info[0]}"
major="${ver_parts[0]}"
minor="${ver_parts[1]}"
revision="${ver_parts[2]}"

# Set suffix
suffix="${version_info[1]}"
if [ -z "$CI" ]; then
suffix="-dev"
elif [[ -n "$suffix" ]] && [[ "${suffix:0:1}" != "-" ]]; then
suffix="-${suffix}"
fi

# Set artifact extension
ext="tar.gz"

if [[ "$os" == "windows" ]]; then
ext="zip"
fi

echo "BB_VERSION=$version" >> $GITHUB_ENV
echo "BB_ARTIFACT_NAME=bladebit-v${version}-${os}-${arch}.${ext}" >> $GITHUB_ENV
echo "BB_ARTIFACT_NAME_CUDA=bladebit-cuda-v${version}-${os}-${arch}.${ext}" >> $GITHUB_ENV


# Create a full version string
version="${major}.${minor}.${revision}${suffix}"

if [[ -n $CI ]]; then
echo "BB_VERSION=$version" >> "$GITHUB_ENV"
echo "BB_ARTIFACT_NAME=bladebit-v${version}-${os}-${arch}.${ext}" >> "$GITHUB_ENV"
echo "BB_ARTIFACT_NAME_CUDA=bladebit-cuda-v${version}-${os}-${arch}.${ext}" >> "$GITHUB_ENV"
else
echo "BB_VERSION=$version"
echo "BB_ARTIFACT_NAME=bladebit-v${version}-${os}-${arch}.${ext}"
echo "BB_ARTIFACT_NAME_CUDA=bladebit-cuda-v${version}-${os}-${arch}.${ext}"
fi
2 changes: 0 additions & 2 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ jobs:
run: |
mkdir build && cd build
cmake ..
bash -eo pipefail ../embed-version.sh
cat ../src/Version.h
cmake --build . --target bladebit --config Release

Expand Down Expand Up @@ -519,7 +518,6 @@ jobs:
run: |
mkdir build_cuda && cd build_cuda
cmake ..
bash -eo pipefail ../embed-version.sh
cat ../src/Version.h
cmake --build . --target bladebit_cuda --config Release

Expand Down
83 changes: 37 additions & 46 deletions cmake_modules/EmbedVersion.cmake
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@

if((NOT DEFINED ENV{CI}) AND (NOT DEFINED CACHE{bb_version_embedded}))
message("Embedding local build version")

set(cmd_shell bash)
set(cmd_ext sh)
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")

find_program(bash_path NAMES bash.exe NO_CACHE)

if(${bash_path} MATCHES "-NOTFOUND")
set(cmd_shell powershell)
set(cmd_ext ps1)
else()
set(cmd_shell "${bash_path}")
endif()
endif()

execute_process(COMMAND ${cmd_shell} ${CMAKE_SOURCE_DIR}/extract-version.${cmd_ext} major OUTPUT_VARIABLE bb_ver_maj WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMAND_ERROR_IS_FATAL ANY)
execute_process(COMMAND ${cmd_shell} ${CMAKE_SOURCE_DIR}/extract-version.${cmd_ext} minor OUTPUT_VARIABLE bb_ver_min WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMAND_ERROR_IS_FATAL ANY)
execute_process(COMMAND ${cmd_shell} ${CMAKE_SOURCE_DIR}/extract-version.${cmd_ext} revision OUTPUT_VARIABLE bb_ver_rev WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMAND_ERROR_IS_FATAL ANY)
execute_process(COMMAND ${cmd_shell} ${CMAKE_SOURCE_DIR}/extract-version.${cmd_ext} suffix OUTPUT_VARIABLE bb_ver_suffix WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMAND_ERROR_IS_FATAL ANY)
execute_process(COMMAND ${cmd_shell} ${CMAKE_SOURCE_DIR}/extract-version.${cmd_ext} commit OUTPUT_VARIABLE bb_ver_commit WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMAND_ERROR_IS_FATAL ANY)

# Remove trailing whitespace incurred in windows gitbash
string(STRIP "${bb_ver_maj}" bb_ver_maj)
string(STRIP "${bb_ver_min}" bb_ver_min)
string(STRIP "${bb_ver_rev}" bb_ver_rev)
string(STRIP "${bb_ver_suffix}" bb_ver_suffix)
string(STRIP "${bb_ver_commit}" bb_ver_commit)

set(bb_ver_suffix ${bb_ver_suffix}-dev)

# This is slow on windows, so let's cache them
set(bb_ver_maj ${bb_ver_maj} CACHE STRING "")
set(bb_ver_min ${bb_ver_min} CACHE STRING "")
set(bb_ver_rev ${bb_ver_rev} CACHE STRING "")
set(bb_ver_suffix ${bb_ver_suffix} CACHE STRING "")
set(bb_ver_commit ${bb_ver_commit} CACHE STRING "")
# Read the version from the file
file(READ "${CMAKE_SOURCE_DIR}/VERSION" version_file_content)
string(STRIP "${version_file_content}" version_file_content)
string(REPLACE "\n" ";" version_file_lines "${version_file_content}")

# Parse major, minor, and revision numbers
list(GET version_file_lines 0 version_str)
string(REPLACE "." ";" version_numbers "${version_str}")
list(GET version_numbers 0 bb_ver_maj)
list(GET version_numbers 1 bb_ver_min)
list(GET version_numbers 2 bb_ver_rev)

# Parse the optional suffix
list(LENGTH version_file_lines version_file_lines_length)
if(${version_file_lines_length} GREATER 1)
list(GET version_file_lines 1 bb_ver_suffix)
else()
set(bb_ver_suffix "")
endif()

if(NOT DEFINED ENV{CI})
add_compile_definitions(BLADEBIT_VERSION_MAJ=${bb_ver_maj})
add_compile_definitions(BLADEBIT_VERSION_MIN=${bb_ver_min})
add_compile_definitions(BLADEBIT_VERSION_REV=${bb_ver_rev})
add_compile_definitions(BLADEBIT_VERSION_SUFFIX="${bb_ver_suffix}")
add_compile_definitions(BLADEBIT_GIT_COMMIT="${bb_ver_commit}")
# Determine if we are in a CI environment
if(DEFINED ENV{CI})
# CI build; use the suffix from the VERSION file
set(bb_ver_suffix_final "-${bb_ver_suffix}")
else()
# Local build; use "-dev" as the suffix
set(bb_ver_suffix_final "-dev")
endif()

set(bb_version_embedded on CACHE BOOL "Version embedding has already happened.")
# Get the Git commit hash
execute_process(COMMAND git rev-parse HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE bb_ver_commit
OUTPUT_STRIP_TRAILING_WHITESPACE)

# Set compile definitions
add_compile_definitions(BLADEBIT_VERSION_MAJ=${bb_ver_maj})
add_compile_definitions(BLADEBIT_VERSION_MIN=${bb_ver_min})
add_compile_definitions(BLADEBIT_VERSION_REV=${bb_ver_rev})
add_compile_definitions(BLADEBIT_VERSION_SUFFIX="${bb_ver_suffix_final}")
add_compile_definitions(BLADEBIT_GIT_COMMIT="${bb_ver_commit}")
28 changes: 0 additions & 28 deletions embed-version.sh

This file was deleted.

60 changes: 0 additions & 60 deletions extract-version.ps1

This file was deleted.

81 changes: 0 additions & 81 deletions extract-version.sh

This file was deleted.