diff --git a/.ciscripts/build-deploy-linux.sh b/.ciscripts/build-deploy-linux.sh index 4a5173616c..c65564b181 100755 --- a/.ciscripts/build-deploy-linux.sh +++ b/.ciscripts/build-deploy-linux.sh @@ -6,6 +6,16 @@ set -o pipefail set -u set -v +# Set the version for both vp and n3fit +gitroot=$(git rev-parse --show-toplevel) +gitversion=$(git describe --long --tags) +tag=$(git describe --abbrev=0 --tags) +gitversion=${gitversion/${tag}-/${tag}.} +githash=$(git rev-parse --short HEAD) +gitversion=${gitversion/-g${githash}/+g${githash}} +echo "build_version=\"${gitversion}\"" > ${gitroot}/n3fit/src/n3fit/version.py +echo "build_version=\"${gitversion}\"" > ${gitroot}/validphys2/src/validphys/version.py + #Set up netrc file for uploading/downloading echo "$NETRC_FILE" | base64 --decode > ~/.netrc @@ -41,4 +51,4 @@ if [ $? == 0 ]; then else echo "Conda package upload failed" exit 1 -fi \ No newline at end of file +fi diff --git a/.ciscripts/build-deploy-osx.sh b/.ciscripts/build-deploy-osx.sh index 8aac55d8c8..80c826bb3d 100755 --- a/.ciscripts/build-deploy-osx.sh +++ b/.ciscripts/build-deploy-osx.sh @@ -5,6 +5,16 @@ set -v #Set up netrc file for uploading/downloading echo "$NETRC_FILE" | base64 --decode > ~/.netrc +# Set the version for both vp and n3fit +gitroot=$(git rev-parse --show-toplevel) +gitversion=$(git describe --long --tags) +tag=$(git describe --abbrev=0 --tags) +gitversion=${gitversion/${tag}-/${tag}.} +githash=$(git rev-parse --short HEAD) +gitversion=${gitversion/-g${githash}/+g${githash}} +echo "build_version=\"${gitversion}\"" > ${gitroot}/n3fit/src/n3fit/version.py +echo "build_version=\"${gitversion}\"" > ${gitroot}/validphys2/src/validphys/version.py + conda build -q conda-recipe if [ $? != 0 ]; then echo failed to build diff --git a/CMakeLists.txt b/CMakeLists.txt index d7d689e2b2..84b70212e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,6 +42,7 @@ option(ENABLE_ASAN "Enable ASAN" OFF) option(ENABLE_DEAD_STRIP "Enable use of flag `-dead_strip-dylibs`" OFF) option(VP_DEV "validphys in developer mode" ON) option(N3_DEV "n3fit in developer mode" ON) +option(BURN_TAG "burn down the git tag in vp and n3fit (only local non-dev installation)" ON) set(PROFILE_PREFIX "" CACHE STRING "Where you store the 'data' folder. Default empty uses CMAKE_INSTALL_PREFIX/share/NNPDF.") if (PROFILE_PREFIX) @@ -147,11 +148,45 @@ add_subdirectory(nnpdfcpp) # evolven3fit add_subdirectory(n3fit/evolven3fit) +if((BURN_TAG) AND (NOT (VP_DEV) OR NOT (N3_DEV))) + # Find out where is the root of the git repository (if available) and finds the correct tag to burn down + # in the package. This just sets the variable, the actual burning is done down below by the validphys + # or n3fit installation and is found in the version.cmake file as it needs to be done post-installation + set(GIT_DIR "--git-dir=${PROJECT_SOURCE_DIR}/.git") + # get the current tag (ex: 3.4) and check at the same time whether git is available and the .git folder found + execute_process(COMMAND git ${GIT_DIR} describe --abbrev=0 --tags OUTPUT_VARIABLE GIT_TAG ERROR_VARIABLE GIT_ERROR) + if(GIT_ERROR STREQUAL "") + string(STRIP ${GIT_TAG} GIT_TAG) + # get the number of revisions since tag happened + execute_process(COMMAND git ${GIT_DIR} rev-list ${GIT_TAG}..HEAD --count OUTPUT_VARIABLE GIT_REVN) + string(STRIP ${GIT_REVN} GIT_REVN) + # get the shortname for the hash + execute_process(COMMAND git ${GIT_DIR} rev-parse --short HEAD OUTPUT_VARIABLE GIT_HASH) + string(STRIP ${GIT_HASH} GIT_HASH) + # find out whether the repository is in a dirty state + # (this returns ${GIT_TAG}-dev so we have to remove the tag) + execute_process(COMMAND git ${GIT_DIR} describe --abbrev=0 --tags --dirty=-dev OUTPUT_VARIABLE GIT_DIRTY) + string(STRIP ${GIT_DIRTY} GIT_DIRTY) + string(REPLACE "${GIT_TAG}" "" GIT_DIRTY ${GIT_DIRTY}) + # now concatenate everything + # with the format ex: 3.4.1880+g00b1741c-dev + set(GIT_VERSION "build_version='${GIT_TAG}.${GIT_REVN}+g${GIT_HASH}${GIT_DIRTY}'") + # now set the variable at install time + install(CODE "set(GIT_VERSION \"${GIT_VERSION}\")") + else(GIT_ERROR STREQUAL "") + set(BURN_TAG OFF) + endif(GIT_ERROR STREQUAL "") +endif() + # install validphys2 if(VP_DEV) - install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install -e ${PROJECT_SOURCE_DIR}/validphys2)") + install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install -e ${PROJECT_SOURCE_DIR}/validphys2)") else(VP_DEV) - install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install --no-deps --ignore-installed ${PROJECT_SOURCE_DIR}/validphys2)") + install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install --no-deps --ignore-installed ${PROJECT_SOURCE_DIR}/validphys2)") + if(BURN_TAG) + install(CODE "set(LIBRARY \"validphys\")") + install(SCRIPT ${PROJECT_SOURCE_DIR}/version.cmake) + endif(BURN_TAG) endif(VP_DEV) # install n3fit @@ -159,4 +194,8 @@ if(N3_DEV) install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install -e ${PROJECT_SOURCE_DIR}/n3fit)") else(N3_DEV) install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install --no-deps --ignore-installed ${PROJECT_SOURCE_DIR}/n3fit)") + if(BURN_TAG) + install(CODE "set(LIBRARY \"n3fit\")") + install(SCRIPT ${PROJECT_SOURCE_DIR}/version.cmake) + endif(BURN_TAG) endif(N3_DEV) diff --git a/conda-recipe/build.sh b/conda-recipe/build.sh index 78c2d4a0a9..b8bc3d039e 100644 --- a/conda-recipe/build.sh +++ b/conda-recipe/build.sh @@ -2,6 +2,6 @@ mkdir build cd build -cmake .. -DCMAKE_INSTALL_PREFIX=${PREFIX} -DVP_DEV=OFF -DN3_DEV=OFF +cmake .. -DCMAKE_INSTALL_PREFIX=${PREFIX} -DVP_DEV=OFF -DN3_DEV=OFF -DBURN_TAG=OFF make -j${CPU_COUNT} -make install \ No newline at end of file +make install diff --git a/conda-recipe/run_test.sh b/conda-recipe/run_test.sh index 33ebdca5d2..5b02830d7f 100644 --- a/conda-recipe/run_test.sh +++ b/conda-recipe/run_test.sh @@ -14,7 +14,7 @@ pytest --pyargs n3fit mkdir bldtest cd bldtest -cmake .. -DENABLE_TESTS=ON +cmake .. -DENABLE_TESTS=ON -DBURN_TAG=OFF make catch_test -j ./libnnpdf/tests/catch_test diff --git a/n3fit/src/n3fit/__init__.py b/n3fit/src/n3fit/__init__.py index e69de29bb2..73a5163bc9 100644 --- a/n3fit/src/n3fit/__init__.py +++ b/n3fit/src/n3fit/__init__.py @@ -0,0 +1,2 @@ +from n3fit.version import build_version +__version__ = build_version diff --git a/n3fit/src/n3fit/version.py b/n3fit/src/n3fit/version.py new file mode 100644 index 0000000000..adf81433ef --- /dev/null +++ b/n3fit/src/n3fit/version.py @@ -0,0 +1,41 @@ +# This file gets overwritten on deployemt, don't modify +def __give_git(): + # Get the path to this file + from pathlib import Path + + file_dir = Path(__file__).parent + from subprocess import run, CalledProcessError + + try: + result = run( + ["git", "describe", "--tags", "--long", "--dirty=-dev"], + capture_output=True, + text=True, + check=True, + cwd=file_dir, + ).stdout.strip() + tag = run( + ["git", "describe", "--abbrev=0", "--tags"], + capture_output=True, + text=True, + check=True, + cwd=file_dir, + ).stdout.strip() + githash = run( + ["git", "rev-parse", "--short", "HEAD"], + capture_output=True, + text=True, + check=True, + cwd=file_dir, + ).stdout.strip() + version = result.replace(f"-g{githash}", f"+g{githash}").replace( + f"{tag}-", f"{tag}." + ) + except CalledProcessError: + # In principle this function should not exist on an installed version + # but who knows. Also maybe git doesn't work on the machine or whatever + version = "unknown" + return version + + +build_version = __give_git() diff --git a/validphys2/src/validphys/__init__.py b/validphys2/src/validphys/__init__.py index 1704ed6675..d6c33482d3 100644 --- a/validphys2/src/validphys/__init__.py +++ b/validphys2/src/validphys/__init__.py @@ -1 +1,3 @@ -#We don't want to import stuff here that could slow down the import times \ No newline at end of file +#We don't want to import stuff here that could slow down the import times +from validphys.version import build_version +__version__ = build_version diff --git a/validphys2/src/validphys/version.py b/validphys2/src/validphys/version.py new file mode 100644 index 0000000000..adf81433ef --- /dev/null +++ b/validphys2/src/validphys/version.py @@ -0,0 +1,41 @@ +# This file gets overwritten on deployemt, don't modify +def __give_git(): + # Get the path to this file + from pathlib import Path + + file_dir = Path(__file__).parent + from subprocess import run, CalledProcessError + + try: + result = run( + ["git", "describe", "--tags", "--long", "--dirty=-dev"], + capture_output=True, + text=True, + check=True, + cwd=file_dir, + ).stdout.strip() + tag = run( + ["git", "describe", "--abbrev=0", "--tags"], + capture_output=True, + text=True, + check=True, + cwd=file_dir, + ).stdout.strip() + githash = run( + ["git", "rev-parse", "--short", "HEAD"], + capture_output=True, + text=True, + check=True, + cwd=file_dir, + ).stdout.strip() + version = result.replace(f"-g{githash}", f"+g{githash}").replace( + f"{tag}-", f"{tag}." + ) + except CalledProcessError: + # In principle this function should not exist on an installed version + # but who knows. Also maybe git doesn't work on the machine or whatever + version = "unknown" + return version + + +build_version = __give_git() diff --git a/version.cmake b/version.cmake new file mode 100644 index 0000000000..044ba44163 --- /dev/null +++ b/version.cmake @@ -0,0 +1,3 @@ +execute_process(COMMAND python -c "import ${LIBRARY}.version ; print(${LIBRARY}.version.__file__)" OUTPUT_VARIABLE VERSION_PATH) +STRING(STRIP ${VERSION_PATH} VERSION_PATH) +file(WRITE ${VERSION_PATH} ${GIT_VERSION})