Skip to content

Commit

Permalink
Change install prefix and cmake version parsing (#2530)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyashton authored Apr 29, 2021
1 parent 6327f90 commit 96e197b
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 26 deletions.
4 changes: 3 additions & 1 deletion .azure-pipelines-templates/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ steps:
- script: |
set -ex
CCF_VERSION=$(<../../build/VERSION)
CCF_VERSION=$(<../../build/VERSION_LONG)
CCF_VERSION=${CCF_VERSION#ccf-}
echo "Setting npm package version to ${CCF_VERSION}"
npm version $CCF_VERSION
npm pack
PKG=`ls *.tgz`
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.0.0-rc1]

### Changed

- By default, CCF is now installed under `/opt/ccf` rather than `/opt/ccf-x.y.z`.

## [0.99.4]

### Fixed
Expand Down Expand Up @@ -823,6 +829,7 @@ Some discrepancies with the TR remain, and are being tracked under https://githu

Initial pre-release

[1.0.0-rc1]: https://github.com/microsoft/CCF/releases/tag/ccf-1.0.0-rc1
[0.99.4]: https://github.com/microsoft/CCF/releases/tag/ccf-0.99.4
[0.99.3]: https://github.com/microsoft/CCF/releases/tag/ccf-0.99.3
[0.99.2]: https://github.com/microsoft/CCF/releases/tag/ccf-0.99.2
Expand Down
11 changes: 8 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ project(

set(ENV{BETTER_EXCEPTIONS} 1)

message(STATUS "CCF version=${CCF_VERSION}")
message(STATUS "CCF release version=${CCF_RELEASE_VERSION}")
message(STATUS "CCF version = ${CCF_VERSION}")
message(STATUS "CCF release version = ${CCF_RELEASE_VERSION}")
message(STATUS "CCF version suffix = ${CCF_VERSION_SUFFIX}")

include(${CCF_DIR}/cmake/cpack_settings.cmake)

Expand All @@ -26,7 +27,7 @@ include(${CCF_DIR}/cmake/cpack_settings.cmake)
#
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX
"/opt/${CCF_VERSION}"
"/opt/ccf"
CACHE PATH "Default install prefix" FORCE
)
endif()
Expand Down Expand Up @@ -441,6 +442,10 @@ if(BUILD_TESTS)
APPEND
PROPERTY LABELS unit_test
)

add_test(NAME versionifier_test
COMMAND ${PYTHON} ${CMAKE_SOURCE_DIR}/python/versionifier.py
)
endif()

# Picobench benchmarks
Expand Down
9 changes: 9 additions & 0 deletions cmake/cpack_settings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ set(CPACK_RESOURCE_FILE_LICENSE "${CCF_DIR}/LICENSE")
set(CPACK_PACKAGE_VERSION ${CCF_RELEASE_VERSION})
set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})

if(CCF_VERSION_SUFFIX)
set(CPACK_DEBIAN_PACKAGE_VERSION
"${CCF_RELEASE_VERSION}~${CCF_VERSION_SUFFIX}"
)
message(
STATUS "Debian package will include suffix: ${CPACK_DEBIAN_PACKAGE_VERSION}"
)
endif()

# CPack variables for Debian packages
set(CPACK_DEBIAN_PACKAGE_DEPENDS
"open-enclave (>=0.15.0), libuv1 (>= 1.18.0), libc++1-8, libc++abi1-8"
Expand Down
62 changes: 41 additions & 21 deletions cmake/version.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,69 @@

unset(CCF_VERSION)
unset(CCF_RELEASE_VERSION)
unset(CCF_VERSION_SUFFIX)

# If possible, deduce project version from git environment
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git)
find_package(Git)

execute_process(
COMMAND "bash" "-c" "${GIT_EXECUTABLE} describe --tags | grep ccf-"
COMMAND "bash" "-c" "${GIT_EXECUTABLE} describe --tags"
OUTPUT_VARIABLE "CCF_VERSION"
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE RETURN_CODE
)
if(NOT RETURN_CODE STREQUAL "0")
message(FATAL_ERROR "Error calling git describe")
endif()

# Convert git description into cmake list, separated at '-'
string(REPLACE "-" ";" CCF_VERSION_COMPONENTS ${CCF_VERSION})

# Check that the first element equals "ccf"
list(GET CCF_VERSION_COMPONENTS 0 FIRST)
if(NOT FIRST STREQUAL "ccf")
message(
FATAL_ERROR
"Git repository does not appear to contain any tag starting with ccf- (the repository should be cloned with sufficient depth to access the latest \"ccf-*\" tag)"
)
endif()
execute_process(
COMMAND "bash" "-c"
"${GIT_EXECUTABLE} describe --tags --abbrev=0 | tr -d ccf-"
OUTPUT_VARIABLE "CCF_RELEASE_VERSION"
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()

if(NOT CCF_RELEASE_VERSION)
else()
# If not in a git environment (e.g. release tarball), deduce version from the
# source directory name
execute_process(
COMMAND "bash" "-c"
"[[ $(basename ${CMAKE_CURRENT_SOURCE_DIR}) =~ ^CCF-.* ]]"
RESULT_VARIABLE "IS_CCF_FOLDER"
COMMAND "bash" "-c" "basename ${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_VARIABLE "CCF_VERSION"
OUTPUT_STRIP_TRAILING_WHITESPACE
)

if(NOT ${IS_CCF_FOLDER} STREQUAL "0")
message(FATAL_ERROR "Sources directory is not in \"CCF-...\" folder")
# Convert directory name into cmake list, separated at '-'
string(REPLACE "-" ";" CCF_VERSION_COMPONENTS ${CCF_VERSION})

# Check that the first element equals "ccf"
list(GET CCF_VERSION_COMPONENTS 0 FIRST)
if(NOT FIRST STREQUAL "ccf")
message(FATAL_ERROR "Sources directory is not in \"ccf-...\" folder")
endif()

execute_process(
COMMAND "bash" "-c" "basename ${CMAKE_CURRENT_SOURCE_DIR} | cut -d'-' -f2"
OUTPUT_VARIABLE "CCF_VERSION"
OUTPUT_STRIP_TRAILING_WHITESPACE
message(
STATUS "Extracting CCF version from sources directory: ${CCF_VERSION}"
)
endif()

set(CCF_RELEASE_VERSION ${CCF_VERSION})
message(STATUS "CCF version deduced from sources directory: ${CCF_VERSION}")
# Check that we have at least ccf-x.y.z
list(LENGTH CCF_VERSION_COMPONENTS CCF_VERSION_COMPONENTS_LENGTH)
if(NOT CCF_VERSION_COMPONENTS_LENGTH GREATER_EQUAL 2)
message(FATAL_ERROR "Version does not contain expected ccf-x.y.z")
endif()

# Get the main version number
list(GET CCF_VERSION_COMPONENTS 1 CCF_RELEASE_VERSION)

# If there is any suffix, store it
if(CCF_VERSION_COMPONENTS_LENGTH GREATER 2)
list(SUBLIST CCF_VERSION_COMPONENTS 2 -1 CCF_VERSION_SUFFIX)
list(JOIN CCF_VERSION_SUFFIX "-" CCF_VERSION_SUFFIX)
endif()

# Check that release version is semver
Expand All @@ -68,3 +85,6 @@ endif()

file(WRITE ${CMAKE_BINARY_DIR}/VERSION "${CCF_RELEASE_VERSION}")
install(FILES ${CMAKE_BINARY_DIR}/VERSION DESTINATION share)

file(WRITE ${CMAKE_BINARY_DIR}/VERSION_LONG "${CCF_VERSION}")
install(FILES ${CMAKE_BINARY_DIR}/VERSION_LONG DESTINATION share)
3 changes: 2 additions & 1 deletion python/setup.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from setuptools import setup # type: ignore
from os import path
import versionifier

PACKAGE_NAME = "ccf"
UTILITIES_PATH = "utils"
Expand All @@ -17,7 +18,7 @@ with open('requirements.txt') as f:

setup(
name=PACKAGE_NAME,
version="@CCF_RELEASE_VERSION@",
version=str(versionifier.to_python_version("@CCF_VERSION@")),
description="Set of tools and utilities for the Confidential Consortium Framework (CCF)",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
73 changes: 73 additions & 0 deletions python/versionifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the Apache 2.0 License.

# pylint: disable=import-error, no-name-in-module
from setuptools.extern.packaging.version import ( # type: ignore
Version,
InvalidVersion,
)


def remove_prefix(s, prefix):
if s.startswith(prefix):
return s[len(prefix) :]
return s


def replace_char(s, n, c):
return s[:n] + str(c) + s[n + 1 :]


def to_python_version(original):
unprefixed = remove_prefix(original, "ccf-")

# Try to parse this as a Version (with automatic normalisation).
# If it fails, try making part of the suffix a local version specifier (+foo).
# Keep expanding this suffix until you get a valid version, or run out of attempts.
next_attempt = unprefixed
next_replace = len(next_attempt)
while True:
try:
version = Version(next_attempt)
return version
except InvalidVersion:
next_replace = unprefixed.rfind("-", 0, next_replace)
if next_replace == -1:
break
next_attempt = replace_char(unprefixed, next_replace, "+")

raise ValueError(f"Cannot convert '{original}' to a Version")


if __name__ == "__main__":
# Run some tests that expected versions are correctly parsed
v = to_python_version("1")
assert v.release == (1,)

v = to_python_version("1.2.3")
assert v.release == (1, 2, 3)

v = to_python_version("ccf-1.2.3")
assert v.release == (1, 2, 3)

v = to_python_version("ccf-1.2.3-a42")
assert v.release == (1, 2, 3)
assert v.pre == ("a", 42)

v = to_python_version("ccf-1.2.3-rc1")
assert v.release == (1, 2, 3)
assert v.pre == ("rc", 1)

v = to_python_version("ccf-1.2.3-dev2")
assert v.release == (1, 2, 3)
assert v.dev == 2

v = to_python_version("ccf-1.2.3-dev3-5-deadbeef")
assert v.release == (1, 2, 3)
assert v.dev == 3
assert v.local == "5.deadbeef"

v = to_python_version("ccf-1.2.3-42-deadbeef")
assert v.release == (1, 2, 3)
assert v.post == 42
assert v.local == "deadbeef"

0 comments on commit 96e197b

Please sign in to comment.