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

Make Version.cmake more error-resistant / Fixed sign-compare (size != rep_len) in usb.c (Fixes #772) #872

Merged
merged 11 commits into from
Mar 21, 2020
Merged
14 changes: 14 additions & 0 deletions .travis-lin-mingw.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

DIR=$PWD

if [ "$TRAVIS_OS_NAME" == "linux" ]; then
echo "WORK DIR:$DIR"
mkdir -p $DIR/build/linux-mingw32-release
cd $DIR/build/linux-mingw32-release
echo "cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=./cmake/linux-mingw32.cmake -DCMAKE_INSTALL_PREFIX=$PWD/_install $DIR"
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=./cmake/linux-mingw32.cmake -DCMAKE_INSTALL_PREFIX=$PWD/_install $DIR
echo "make"
make
fi

3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ addons:
- g++-6
- gcc-6
- libusb-1.0.0-dev
- p7zip
- mingw-w64

script:
- git fetch --tags
- printenv
- cmake --version
- ./.travis.sh
- ./.travis-lin-mingw.sh
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.6.0
1.6.0
128 changes: 86 additions & 42 deletions cmake/Version.cmake
Original file line number Diff line number Diff line change
@@ -1,51 +1,95 @@
# Determine project version
# * Using Git
# * Local .version file
find_package (Git QUIET)

set(__detect_version 0)

find_package (Git)

if (GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
# Working off a git repo, using git versioning
# Check if HEAD is pointing to a tag
execute_process (
COMMAND "${GIT_EXECUTABLE}" describe --always --tag
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
OUTPUT_VARIABLE PROJECT_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)

# If the sources have been changed locally, add -dirty to the version.
execute_process (
COMMAND "${GIT_EXECUTABLE}" diff --quiet
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
RESULT_VARIABLE res)

if (res EQUAL 1)
set (PROJECT_VERSION "${PROJECT_VERSION}-dirty")
endif()

# strip a leading v off of the version as proceeding code expectes just the version numbering.
string(REGEX REPLACE "^v" "" PROJECT_VERSION ${PROJECT_VERSION})

string(REGEX REPLACE "^(0|[1-9][0-9]*)[.](0|[1-9][0-9]*)[.](0|[1-9][0-9]*)(-[.0-9A-Za-z-]+)?([+][.0-9A-Za-z-]+)?$"
"\\1;\\2;\\3" PROJECT_VERSION_LIST ${PROJECT_VERSION})
list(LENGTH PROJECT_VERSION_LIST len)
if(len EQUAL 3)
list(GET PROJECT_VERSION_LIST 0 PROJECT_VERSION_MAJOR)
list(GET PROJECT_VERSION_LIST 1 PROJECT_VERSION_MINOR)
list(GET PROJECT_VERSION_LIST 2 PROJECT_VERSION_PATCH)
endif()
elseif(EXISTS ${PROJECT_SOURCE_DIR}/.version)
# If git is not available (e.g. when building from source package)
# we can extract the package version from .version file.
file (STRINGS .version PROJECT_VERSION)

# TODO create function to extract semver from file or string and check if it is correct instead of copy-pasting
string(REGEX REPLACE "^(0|[1-9][0-9]*)[.](0|[1-9][0-9]*)[.](0|[1-9][0-9]*)(-[.0-9A-Za-z-]+)?([+][.0-9A-Za-z-]+)?$"
"\\1;\\2;\\3" PROJECT_VERSION_LIST ${PROJECT_VERSION})
list(GET PROJECT_VERSION_LIST 0 PROJECT_VERSION_MAJOR)
list(GET PROJECT_VERSION_LIST 1 PROJECT_VERSION_MINOR)
list(GET PROJECT_VERSION_LIST 2 PROJECT_VERSION_PATCH)
# Working off a git repo, using git versioning
# Check if HEAD is pointing to a tag
execute_process (
COMMAND "${GIT_EXECUTABLE}" describe --always --tag
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
OUTPUT_VARIABLE PROJECT_VERSION
RESULT_VARIABLE GIT_DESCRIBE_RESULT
slyshykO marked this conversation as resolved.
Show resolved Hide resolved
ERROR_VARIABLE GIT_DESCRIBE_ERROR
OUTPUT_STRIP_TRAILING_WHITESPACE)

if(GIT_DESCRIBE_RESULT EQUAL 0)
# If the sources have been changed locally, add -dirty to the version.
execute_process (
COMMAND "${GIT_EXECUTABLE}" diff --quiet
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
RESULT_VARIABLE res)

if (res EQUAL 1)
set (PROJECT_VERSION "${PROJECT_VERSION}-dirty")
endif()

# strip a leading v off of the version as proceeding code expectes just the version numbering.
string(REGEX REPLACE "^v" "" PROJECT_VERSION ${PROJECT_VERSION})

string(REGEX REPLACE "^(0|[1-9][0-9]*)[.](0|[1-9][0-9]*)[.](0|[1-9][0-9]*)(-[.0-9A-Za-z-]+)?([+][.0-9A-Za-z-]+)?$"
"\\1;\\2;\\3" PROJECT_VERSION_LIST ${PROJECT_VERSION})
list(LENGTH PROJECT_VERSION_LIST len)
if(len EQUAL 3)
list(GET PROJECT_VERSION_LIST 0 PROJECT_VERSION_MAJOR)
list(GET PROJECT_VERSION_LIST 1 PROJECT_VERSION_MINOR)
list(GET PROJECT_VERSION_LIST 2 PROJECT_VERSION_PATCH)
set(__detect_version 1)
set(__version_str "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
if(EXISTS "${PROJECT_SOURCE_DIR}/.version")
file(READ "${PROJECT_SOURCE_DIR}/.version" __version_file)
if(NOT __version_str STREQUAL __version_file)
message(STATUS "Rewrite ${PROJECT_SOURCE_DIR}/.version with ${__version_str}")
endif()
else()
file(WRITE "${PROJECT_SOURCE_DIR}/.version" ${__version_str})
endif()
else()
message(STATUS "Fail to extract version's parts from \"${PROJECT_VERSION}\"")
endif()

else(GIT_DESCRIBE_RESULT EQUAL 0)
message(WARNING "git describe failed.")
message(WARNING "${GIT_DESCRIBE_ERROR}")
endif(GIT_DESCRIBE_RESULT EQUAL 0)
else()
message(FATAL_ERROR "Unable to determine project version")
message(STATUS "Git or repo not found.")
endif()



if(NOT __detect_version)
message(STATUS "Try to detect version from \"${PROJECT_SOURCE_DIR}/.version\" file.")
if(EXISTS ${PROJECT_SOURCE_DIR}/.version)
# If git is not available (e.g. when building from source package)
# we can extract the package version from .version file.
file (STRINGS .version PROJECT_VERSION)

# TODO create function to extract semver from file or string and check if it is correct instead of copy-pasting
string(REGEX REPLACE "^(0|[1-9][0-9]*)[.](0|[1-9][0-9]*)[.](0|[1-9][0-9]*)(-[.0-9A-Za-z-]+)?([+][.0-9A-Za-z-]+)?$"
"\\1;\\2;\\3" PROJECT_VERSION_LIST ${PROJECT_VERSION})
list(LENGTH PROJECT_VERSION_LIST len)
if(len EQUAL 3)
list(GET PROJECT_VERSION_LIST 0 PROJECT_VERSION_MAJOR)
list(GET PROJECT_VERSION_LIST 1 PROJECT_VERSION_MINOR)
list(GET PROJECT_VERSION_LIST 2 PROJECT_VERSION_PATCH)
set(__detect_version 1)
else()
message(STATUS "Fail to extract version's parts from \"${PROJECT_VERSION}\"")
endif()
else()
message(STATUS "File \"${PROJECT_SOURCE_DIR}/.version\" did not exists.")
endif()
endif()

if(NOT __detect_version)
message(FATAL_ERROR "Unable to determine project version")
endif()


message(STATUS "stlink version: ${PROJECT_VERSION}")
message(STATUS " Major ${PROJECT_VERSION_MAJOR} Minor ${PROJECT_VERSION_MINOR} Patch ${PROJECT_VERSION_PATCH}")
3 changes: 2 additions & 1 deletion cmake/modules/FindLibUSB.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ if(NOT LIBUSB_FOUND)
file(MAKE_DIRECTORY ${LIBUSB_WIN_OUTPUT_FOLDER})

if(${ZIP_EXECUTABLE} MATCHES "p7zip")
execute_process(COMMAND ${ZIP_EXECUTABLE} -d --keep -f ${LIBUSB_WIN_ARCHIVE_PATH} WORKING_DIRECTORY ${LIBUSB_WIN_OUTPUT_FOLDER})
#execute_process(COMMAND ${ZIP_EXECUTABLE} -d --keep -f ${LIBUSB_WIN_ARCHIVE_PATH} WORKING_DIRECTORY ${LIBUSB_WIN_OUTPUT_FOLDER})
execute_process(COMMAND ${ZIP_EXECUTABLE} -d ${LIBUSB_WIN_ARCHIVE_PATH} WORKING_DIRECTORY ${LIBUSB_WIN_OUTPUT_FOLDER})
else()
execute_process(COMMAND ${ZIP_EXECUTABLE} x -y ${LIBUSB_WIN_ARCHIVE_PATH} -o${LIBUSB_WIN_OUTPUT_FOLDER})
endif()
Expand Down
2 changes: 1 addition & 1 deletion src/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ int _stlink_usb_version(stlink_t *sl) {
cmd[i++] = STLINK_APIV3_GET_VERSION_EX;

size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
if (size != rep_len) {
if (size != (ssize_t)rep_len) {
printf("[!] send_recv STLINK_APIV3_GET_VERSION_EX\n");
return (int) size;
}
Expand Down