Skip to content

Commit

Permalink
Improve handling of git info in version number
Browse files Browse the repository at this point in the history
The version is now automatically increased from the previous release
when building a dev version. The full code hash is also included, which
prevents pip from caching a wheel during development without having to use
uuid in the `file://` URL. Removing uuid means that we can now install
featomic with `uv pip install`.
  • Loading branch information
Luthaf committed Nov 28, 2024
1 parent 91c5590 commit c85f8f0
Show file tree
Hide file tree
Showing 25 changed files with 650 additions and 166 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/rust-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ jobs:
run: sudo rm -rf /usr/share/dotnet /usr/local/lib/android || true

- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Configure git safe directory
if: matrix.container == 'ubuntu:20.04'
run: git config --global --add safe.directory /__w/featomic/featomic

- name: "copy the code to C: drive"
if: matrix.os == 'windows-2019'
Expand Down
23 changes: 23 additions & 0 deletions .github/workflows/torch-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
tests:
runs-on: ${{ matrix.os }}
name: ${{ matrix.os }} / Torch ${{ matrix.torch-version }}
container: ${{ matrix.container }}
strategy:
matrix:
include:
Expand All @@ -28,6 +29,14 @@ jobs:
cargo-test-flags: --release
do-valgrind: true

- os: ubuntu-20.04
container: ubuntu:20.04
extra-name: ", cmake 3.16"
torch-version: 2.5.*
python-version: "3.12"
cargo-test-flags: ""
cxx-flags: -fsanitize=undefined -fsanitize=address -fno-omit-frame-pointer -g

- os: macos-14
torch-version: 2.3.*
python-version: "3.12"
Expand All @@ -40,7 +49,20 @@ jobs:
cargo-test-flags: --release

steps:
- name: install dependencies in container
if: matrix.container == 'ubuntu:20.04'
run: |
apt update
apt install -y software-properties-common
apt install -y cmake make gcc g++ git curl
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Configure git safe directory
if: matrix.container == 'ubuntu:20.04'
run: git config --global --add safe.directory /__w/featomic/featomic

- name: setup rust
uses: dtolnay/rust-toolchain@master
Expand Down Expand Up @@ -77,3 +99,4 @@ jobs:
# Use the CPU only version of torch when building/running the code
PIP_EXTRA_INDEX_URL: https://download.pytorch.org/whl/cpu
FEATOMIC_TORCH_TEST_VERSION: ${{ matrix.torch-version }}
CXXFLAGS: ${{ matrix.cxx-flags }}
69 changes: 64 additions & 5 deletions featomic-torch/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ endif()
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/VERSION FEATOMIC_TORCH_VERSION)
string(STRIP ${FEATOMIC_TORCH_VERSION} FEATOMIC_TORCH_VERSION)

include(cmake/dev-versions.cmake)
create_development_version("${FEATOMIC_TORCH_VERSION}" FEATOMIC_TORCH_FULL_VERSION)
message(STATUS "Building featomic-torch v${FEATOMIC_TORCH_FULL_VERSION}")

# strip any -dev/-rc suffix on the version since project(VERSION) does not support it
string(REGEX REPLACE "([0-9]*)\\.([0-9]*)\\.([0-9]*).*" "\\1.\\2.\\3" FEATOMIC_TORCH_VERSION ${FEATOMIC_TORCH_FULL_VERSION})
project(featomic_torch
VERSION ${FEATOMIC_TORCH_VERSION}
LANGUAGES CXX
)
set(PROJECT_VERSION ${FEATOMIC_TORCH_FULL_VERSION})


option(FEATOMIC_TORCH_TESTS "Build featomic-torch C++ tests" OFF)
option(FEATOMIC_TORCH_FETCH_METATENSOR_TORCH "Download and build the metatensor_torch library before building featomic_torch" OFF)
Expand All @@ -46,8 +54,56 @@ if (${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
endif()
endif()

set(REQUIRED_FEATOMIC_VERSION "0.1")
find_package(featomic ${REQUIRED_FEATOMIC_VERSION} CONFIG REQUIRED)
function(check_compatible_versions _dependency_ _actual_ _requested_)
if(${_actual_} MATCHES "^([0-9]+)\\.([0-9]+)")
set(_actual_major_ "${CMAKE_MATCH_1}")
set(_actual_minor_ "${CMAKE_MATCH_2}")
else()
message(FATAL_ERROR "Failed to parse actual version: ${_actual_}")
endif()

if(${_requested_} MATCHES "^([0-9]+)\\.([0-9]+)")
set(_requested_major_ "${CMAKE_MATCH_1}")
set(_requested_minor_ "${CMAKE_MATCH_2}")
else()
message(FATAL_ERROR "Failed to parse requested version: ${_requested_}")
endif()

if (${_requested_major_} EQUAL 0 AND ${_actual_minor_} EQUAL ${_requested_minor_})
# major version is 0 and same minor version, everything is fine
elseif (${_actual_major_} EQUAL ${_requested_major_})
# same major version, everything is fine
else()
# not compatible
message(FATAL_ERROR "Incompatible versions for ${_dependency_}: we need v${_requested_}, but we got v${_actual_}")
endif()
endfunction()


set(REQUIRED_FEATOMIC_VERSION "0.0.0")
if (NOT "$ENV{FEATOMIC_NO_LOCAL_DEPS}" STREQUAL "1")
# If building a dev version, we also need to update the
# REQUIRED_FEATOMIC_VERSION in the same way we update the
# featomic-torch version
create_development_version("${REQUIRED_FEATOMIC_VERSION}" FEATOMIC_FULL_VERSION)
else()
set(FEATOMIC_FULL_VERSION ${REQUIRED_FEATOMIC_VERSION})
endif()
string(REGEX REPLACE "([0-9]*)\\.([0-9]*).*" "\\1.\\2" REQUIRED_FEATOMIC_VERSION ${FEATOMIC_FULL_VERSION})

# Either featomic is built as part of the same CMake project, or we try to
# find the corresponding CMake package
if (TARGET featomic)
get_target_property(FEATOMIC_BUILD_VERSION featomic BUILD_VERSION)
check_compatible_versions("featomic" ${FEATOMIC_BUILD_VERSION} ${REQUIRED_FEATOMIC_VERSION})
else()
find_package(featomic ${REQUIRED_FEATOMIC_VERSION} CONFIG REQUIRED)

get_target_property(FEATOMIC_LOCATION featomic IMPORTED_LOCATION)
get_filename_component(FEATOMIC_LOCATION ${FEATOMIC_LOCATION} DIRECTORY)
message(STATUS "Using local featomic from ${FEATOMIC_LOCATION}")
endif()


# FindCUDNN.cmake distributed with PyTorch is a bit broken, so we have a
# fixed version in `cmake/FindCUDNN.cmake`
Expand All @@ -58,17 +114,17 @@ find_package(Torch 1.12 REQUIRED)
# ============================================================================ #
# Setup metatensor_torch

set(METATENSOR_FETCH_VERSION "0.6.0")
set(METATENSOR_FETCH_VERSION "0.6.1")
set(REQUIRED_METATENSOR_TORCH_VERSION "0.6")
if (FEATOMIC_TORCH_FETCH_METATENSOR_TORCH)
message(STATUS "Fetching metatensor-torch @ ${METATENSOR_FETCH_VERSION} from github")
message(STATUS "Fetching metatensor-torch from github")

set(URL_ROOT "https://github.com/lab-cosmo/metatensor/releases/download")
include(FetchContent)
FetchContent_Declare(
metatensor_torch
URL ${URL_ROOT}/metatensor-torch-v${METATENSOR_FETCH_VERSION}/metatensor-torch-cxx-${METATENSOR_FETCH_VERSION}.tar.gz
URL_HASH SHA256=f050743662ece38948b2087dd025d60110645716840dbfc5370c059e1275d0cf
URL_HASH SHA256=0941da4bc6d25ee73b597774d3c8c6edf6a44f134139bd93b33834eae52ac4dd
)

if (CMAKE_VERSION VERSION_GREATER 3.18)
Expand All @@ -82,6 +138,9 @@ if (FEATOMIC_TORCH_FETCH_METATENSOR_TORCH)
endif()
else()
find_package(metatensor_torch ${REQUIRED_METATENSOR_TORCH_VERSION} REQUIRED CONFIG)
get_target_property(METATENSOR_TORCH_LOCATION metatensor_torch IMPORTED_LOCATION)
get_filename_component(METATENSOR_TORCH_LOCATION ${METATENSOR_TORCH_LOCATION} DIRECTORY)
message(STATUS "Using local metatensor-torch from ${METATENSOR_TORCH_LOCATION}")
endif()


Expand Down
2 changes: 1 addition & 1 deletion featomic-torch/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
0.0.0
96 changes: 96 additions & 0 deletions featomic-torch/cmake/dev-versions.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Parse a `_version_` number, and store its components in `_major_` `_minor_`
# `_patch_` and `_rc_`
function(parse_version _version_ _major_ _minor_ _patch_ _rc_)
string(REGEX MATCH "([0-9]+)\\.([0-9]+)\\.([0-9]+)(-rc)?([0-9]+)?" _ "${_version_}")

if(${CMAKE_MATCH_COUNT} EQUAL 3)
set(${_rc_} "" PARENT_SCOPE)
elseif(${CMAKE_MATCH_COUNT} EQUAL 5)
set(${_rc_} ${CMAKE_MATCH_5} PARENT_SCOPE)
else()
message(FATAL_ERROR "invalid version string ${_version_}")
endif()

set(${_major_} ${CMAKE_MATCH_1} PARENT_SCOPE)
set(${_minor_} ${CMAKE_MATCH_2} PARENT_SCOPE)
set(${_patch_} ${CMAKE_MATCH_3} PARENT_SCOPE)
endfunction()

if (CMAKE_VERSION VERSION_LESS "3.17")
# CMAKE_CURRENT_FUNCTION_LIST_DIR was added in CMake 3.17
set(CMAKE_CURRENT_FUNCTION_LIST_DIR "${CMAKE_CURRENT_LIST_DIR}")
endif()

# Get the time of the last modification since the last tag/release, and a hash
# of the latest commit/full state of a dirty repository
function(git_version_info _output_last_mtime_ _output_git_hash_)
set(_script_ "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../../scripts/git-version-info.py")

if (EXISTS "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/git_version_info")
# When building from a tarball, the script is executed and the result
# put in this file
file(STRINGS "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/git_version_info" _file_content_)
list(GET _file_content_ 0 _last_mtime_)
list(GET _file_content_ 1 _git_hash_)

elseif (EXISTS "${_script_}")
# When building from a checkout, we'll need to run the script
find_package(Python COMPONENTS Interpreter REQUIRED)
execute_process(
COMMAND "${Python_EXECUTABLE}" "${_script_}" "featomic-torch-v"
RESULT_VARIABLE _status_
OUTPUT_VARIABLE _stdout_
ERROR_VARIABLE _stderr_
WORKING_DIRECTORY ${CMAKE_CURRENT_FUNCTION_LIST_DIR}
)

if (NOT ${_status_} EQUAL 0)
message(WARNING
"git-version-info.py failed, version number might be wrong:\nstdout: ${_stdout_}\nstderr: ${_stderr_}")
set(${_output_} 0 PARENT_SCOPE)
return()
endif()

if (NOT "${_stderr_}" STREQUAL "")
message(WARNING "git-version-info.py gave some errors, version number might be wrong:\nstdout: ${_stdout_}\nstderr: ${_stderr_}")
endif()

string(REPLACE "\n" ";" _lines_ ${_stdout_})
list(GET _lines_ 0 _last_mtime_)
list(GET _lines_ 1 _git_hash_)
else()
message(FATAL_ERROR "could not update git version information")
endif()

string(STRIP ${_last_mtime_} _last_mtime_)
set(${_output_last_mtime_} ${_last_mtime_} PARENT_SCOPE)

string(STRIP ${_git_hash_} _git_hash_)
set(${_output_git_hash_} ${_git_hash_} PARENT_SCOPE)
endfunction()


# Take the version declared in the package, and increase the right number if we
# are actually installing a developement version from after the latest git tag
function(create_development_version _version_ _output_)
git_version_info(_last_mtime_ _git_hash_)

parse_version(${_version_} _major_ _minor_ _patch_ _rc_)
if(${_last_mtime_} STREQUAL "0")
# we are building a release, leave the version number as-is
if("${_rc_}" STREQUAL "")
set(${_output_} "${_major_}.${_minor_}.${_patch_}" PARENT_SCOPE)
else()
set(${_output_} "${_major_}.${_minor_}.${_patch_}-rc${_rc_}" PARENT_SCOPE)
endif()
else()
# we are building a development version, increase the right part of the version
if("${_rc_}" STREQUAL "")
math(EXPR _minor_ "${_minor_} + 1")
set(${_output_} "${_major_}.${_minor_}.0-dev${_last_mtime_}+${_git_hash_}" PARENT_SCOPE)
else()
math(EXPR _rc_ "${_rc_} + 1")
set(${_output_} "${_major_}.${_minor_}.${_patch_}-rc${_rc_}-dev${_last_mtime_}+${_git_hash_}" PARENT_SCOPE)
endif()
endif()
endfunction()
9 changes: 8 additions & 1 deletion featomic-torch/tests/cmake-project/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ cmake_minimum_required(VERSION 3.16)

project(featomic-torch-test-cmake-project CXX)

find_package(featomic_torch 0.1 CONFIG REQUIRED)

# We need to update the REQUIRED_FEATOMIC_VERSION in the same way we update the
# featomic version for dev builds
include(../../cmake/dev-versions.cmake)
set(REQUIRED_FEATOMIC_TORCH_VERSION "0.0.0")
create_development_version("${REQUIRED_FEATOMIC_TORCH_VERSION}" FEATOMIC_TORCH_FULL_VERSION)
string(REGEX REPLACE "([0-9]*)\\.([0-9]*).*" "\\1.\\2" REQUIRED_FEATOMIC_TORCH_VERSION ${FEATOMIC_TORCH_FULL_VERSION})
find_package(featomic_torch ${REQUIRED_FEATOMIC_TORCH_VERSION} REQUIRED)

add_executable(torch-main src/main.cpp)
target_link_libraries(torch-main featomic_torch)
Expand Down
16 changes: 13 additions & 3 deletions featomic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@ foreach(line ${CARGO_TOML_CONTENT})
endif()
endforeach()

include(cmake/dev-versions.cmake)
create_development_version("${FEATOMIC_VERSION}" FEATOMIC_FULL_VERSION)
message(STATUS "Building featomic v${FEATOMIC_FULL_VERSION}")

# strip any -dev/-rc suffix on the version since project(VERSION) does not support it
string(REGEX REPLACE "([0-9]*)\\.([0-9]*)\\.([0-9]*).*" "\\1.\\2.\\3" FEATOMIC_VERSION ${FEATOMIC_FULL_VERSION})
project(featomic
VERSION ${FEATOMIC_VERSION}
LANGUAGES C CXX
LANGUAGES C CXX # we need to declare a language to access CMAKE_SIZEOF_VOID_P later
)
set(PROJECT_VERSION ${FEATOMIC_FULL_VERSION})

# We follow the standard CMake convention of using BUILD_SHARED_LIBS to provide
# either a shared or static library as a default target. But since cargo always
Expand Down Expand Up @@ -225,7 +232,7 @@ endif()
set(METATENSOR_FETCH_VERSION "0.1.11")
set(METATENSOR_REQUIRED_VERSION "0.1")
if (FEATOMIC_FETCH_METATENSOR)
message(STATUS "Fetching metatensor @ ${METATENSOR_FETCH_VERSION} from github")
message(STATUS "Fetching metatensor-core from github")

include(FetchContent)
set(URL_ROOT "https://github.com/lab-cosmo/metatensor/releases/download")
Expand All @@ -250,12 +257,13 @@ if (FEATOMIC_FETCH_METATENSOR)
set(METATENSOR_RPATH "$$\\{ORIGIN\\}")
else()
find_package(metatensor ${METATENSOR_REQUIRED_VERSION} REQUIRED CONFIG)

# in case featomic gets installed in a different place than metatensor,
# set the RPATH to the directory where we found metatensor
get_target_property(METATENSOR_LOCATION metatensor::shared IMPORTED_LOCATION)
get_filename_component(METATENSOR_LOCATION ${METATENSOR_LOCATION} DIRECTORY)
set(METATENSOR_RPATH "${METATENSOR_LOCATION}")

message(STATUS "Using local metatensor from ${METATENSOR_LOCATION}")
endif()

# ============================================================================ #
Expand Down Expand Up @@ -344,6 +352,7 @@ set(FEATOMIC_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include/)
set_target_properties(featomic::shared PROPERTIES
IMPORTED_LOCATION ${FEATOMIC_SHARED_LOCATION}
INTERFACE_INCLUDE_DIRECTORIES ${FEATOMIC_INCLUDE_DIR}
BUILD_VERSION "${FEATOMIC_FULL_VERSION}"
)
target_compile_features(featomic::shared INTERFACE cxx_std_17)

Expand All @@ -357,6 +366,7 @@ set_target_properties(featomic::static PROPERTIES
IMPORTED_LOCATION ${FEATOMIC_STATIC_LOCATION}
INTERFACE_INCLUDE_DIRECTORIES ${FEATOMIC_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES "${CARGO_DEFAULT_LIBRARIES}"
BUILD_VERSION "${FEATOMIC_FULL_VERSION}"
)

if (BUILD_SHARED_LIBS)
Expand Down
2 changes: 1 addition & 1 deletion featomic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "featomic"
version = "0.1.0"
version = "0.0.0"
authors = ["Guillaume Fraux <[email protected]>"]
edition = "2021"
rust-version = "1.74"
Expand Down
Loading

0 comments on commit c85f8f0

Please sign in to comment.