Skip to content

Commit

Permalink
Merge pull request #245 from aminya/git-vcpkg
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya authored Jan 26, 2024
2 parents 0e89e7b + 42818f6 commit 95ecb4c
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ include(${_project_options_SOURCE_DIR}/Index.cmake)
# install vcpkg dependencies: - should be called before defining project()
run_vcpkg(
VCPKG_URL "https://github.com/microsoft/vcpkg.git"
VCPKG_REV "0fa8459cf3a7caca7adc58f992bc32ff13630684"
VCPKG_REV "6a3dd0874f153f8b375ec26210ea6d41dee3bb26"
)
# Set the project name and language
Expand Down
2 changes: 1 addition & 1 deletion docs/src/project_options_example.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ include(${_project_options_SOURCE_DIR}/Index.cmake)
# install vcpkg dependencies: - should be called before defining project()
run_vcpkg(
VCPKG_URL "https://github.com/microsoft/vcpkg.git"
VCPKG_REV "0fa8459cf3a7caca7adc58f992bc32ff13630684"
VCPKG_REV "6a3dd0874f153f8b375ec26210ea6d41dee3bb26"
ENABLE_VCPKG_UPDATE
)
Expand Down
66 changes: 65 additions & 1 deletion src/Git.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,19 @@ function(git_switch_back)
message(STATUS "Switch back failed. Trying to checkout previous branch")
execute_process(
COMMAND "${GIT_EXECUTABLE}" "checkout" "-" WORKING_DIRECTORY "${_fun_REPOSITORY_PATH}"
COMMAND_ERROR_IS_FATAL LAST
RESULT_VARIABLE _checkout_result
)

# if the checkout failed, try to checkout the default branch
if(NOT ${_checkout_result} EQUAL 0)
message(STATUS "Checkout previous branch failed. Trying to checkout default branch")
git_default_branch(default_branch REPOSITORY_PATH "${_fun_REPOSITORY_PATH}")
execute_process(
COMMAND "${GIT_EXECUTABLE}" "checkout" "${default_branch}"
WORKING_DIRECTORY "${_fun_REPOSITORY_PATH}" COMMAND_ERROR_IS_FATAL LAST
)
endif()

endif()
endif()
endfunction()
Expand Down Expand Up @@ -512,3 +523,56 @@ function(git_wait)
endif()
endwhile()
endfunction()

#[[.rst:
``git_default_branch``
======================
Get the default branch of the given repository. Defaults to master in case of failure
Input variables:
- ``REPOSITORY_PATH``: The path to the repository
Output variables:
- ``default_branch``: The variable to store the default branch in
.. code:: cmake
git_default_branch(
REPOSITORY_PATH
"$ENV{HOME}/vcpkg"
default_branch
)
]]
function(git_default_branch default_branch)
# use git symbolic-ref refs/remotes/origin/HEAD to get the default branch

set(oneValueArgs REPOSITORY_PATH)
cmake_parse_arguments(_fun "" "${oneValueArgs}" "" ${ARGN})

if("${_fun_REPOSITORY_PATH}" STREQUAL "")
message(FATAL_ERROR "REPOSITORY_PATH is required")
endif()

find_program(GIT_EXECUTABLE "git" REQUIRED)
execute_process(
COMMAND "${GIT_EXECUTABLE}" "symbolic-ref" "refs/remotes/origin/HEAD"
OUTPUT_VARIABLE _default_branch
WORKING_DIRECTORY "${_fun_REPOSITORY_PATH}"
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE _default_branch_result
)

if(${_default_branch_result} EQUAL 0)
string(REGEX REPLACE "refs/remotes/origin/" "" _default_branch "${_default_branch}")
else()
message(
WARNING "Could not get default branch of ${_fun_REPOSITORY_PATH}. Considering it as master"
)
set(_default_branch "master")
endif()

set(${default_branch} ${_default_branch} PARENT_SCOPE)
endfunction()
2 changes: 1 addition & 1 deletion src/Vcpkg.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ Or by specifying the options
run_vcpkg(
VCPKG_URL "https://github.com/microsoft/vcpkg.git"
VCPKG_REV "0fa8459cf3a7caca7adc58f992bc32ff13630684"
VCPKG_REV "6a3dd0874f153f8b375ec26210ea6d41dee3bb26"
ENABLE_VCPKG_UPDATE
)
Expand Down
8 changes: 4 additions & 4 deletions tests/install/vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json",
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json",
"name": "another-project",
"version-string": "0.1.0",
"builtin-baseline": "8dbd66f5a7821ced1ed57696b50375a977006813",
"builtin-baseline": "6a3dd0874f153f8b375ec26210ea6d41dee3bb26",
"dependencies": [
{
"name": "eigen3",
"version>=": "3.4.0"
"version>=": "3.4.0#2"
},
{
"name": "fmt",
"version>=": "8.1.1"
"version>=": "9.1.0#1"
}
]
}
4 changes: 2 additions & 2 deletions tests/myproj/include/mylib/lib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// test external pac
#include <Eigen/Dense>
#include <fmt/core.h>
#include <fmt/ostream.h>
#include <fmt/ranges.h>

// test std libraries
#include <iostream>
Expand All @@ -24,7 +24,7 @@ int some_fun() {
auto eigen_vec = Eigen::VectorXd::LinSpaced(10, 0, 1);

// print the vector
fmt::print("{}", eigen_vec);
fmt::print("[{}]", fmt::join(eigen_vec, ", "));

return 0;
}
6 changes: 3 additions & 3 deletions tests/myproj/src/main/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// test external pac
#include <Eigen/Dense>
#include <fmt/core.h>
#include <fmt/ostream.h>
#include <fmt/ranges.h>

// test std libraries
#include <iostream>
Expand All @@ -19,11 +19,11 @@ int main() {
fmt::print("Hello from fmt{}", "!");

Eigen::VectorXd eigen_vec = Eigen::Vector3d(1, 2, 3);
fmt::print("{}", eigen_vec);
fmt::print("[{}]", fmt::join(eigen_vec, ", "));

#if !defined(__MINGW32__) && !defined(__MSYS__) // TODO fails
Eigen::VectorXd eigen_vec2 = Eigen::VectorXd::LinSpaced(10, 0, 1);

Check warning on line 25 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (macos-12, llvm, true, true)

10 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]

Check warning on line 25 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (macos-11, gcc, true, true)

10 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]

Check warning on line 25 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (macos-12, llvm, true, true)

10 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]

Check warning on line 25 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (macos-11, gcc, true, true)

10 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
fmt::print("{}", eigen_vec2);
fmt::print("[{}]", fmt::join(eigen_vec2, ", "));
#endif

// trigger address sanitizer
Expand Down
6 changes: 3 additions & 3 deletions tests/myproj/src/mylib2/lib.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// test external pac
#include <Eigen/Dense>

Check warning on line 2 in tests/myproj/src/mylib2/lib.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, gcc, true, true)

included header Dense is not used directly [misc-include-cleaner]
#include <fmt/core.h>
#include <fmt/ostream.h>
#include <fmt/ranges.h>

Check warning on line 4 in tests/myproj/src/mylib2/lib.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, gcc, true, true)

included header ranges.h is not used directly [misc-include-cleaner]

// test std libraries
#include <iostream>

Check warning on line 7 in tests/myproj/src/mylib2/lib.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, gcc, true, true)

included header iostream is not used directly [misc-include-cleaner]
Expand All @@ -19,11 +19,11 @@ int some_fun2() {
fmt::print("Hello from fmt{}", "!");

Eigen::VectorXd eigen_vec = Eigen::Vector3d(1, 2, 3);
fmt::print("{}", eigen_vec);
fmt::print("[{}]", fmt::join(eigen_vec, ", "));

#if !defined(__MINGW32__) && !defined(__MSYS__) // TODO fails
Eigen::VectorXd eigen_vec2 = Eigen::VectorXd::LinSpaced(10, 0, 1);

Check warning on line 25 in tests/myproj/src/mylib2/lib.cpp

View workflow job for this annotation

GitHub Actions / Test (macos-12, llvm, true, true)

10 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]

Check warning on line 25 in tests/myproj/src/mylib2/lib.cpp

View workflow job for this annotation

GitHub Actions / Test (macos-11, gcc, true, true)

10 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]

Check warning on line 25 in tests/myproj/src/mylib2/lib.cpp

View workflow job for this annotation

GitHub Actions / Test (macos-11, gcc, true, true)

10 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
fmt::print("{}", eigen_vec2);
fmt::print("[{}]", fmt::join(eigen_vec2, ", "));
#endif

return 0;
Expand Down
8 changes: 4 additions & 4 deletions tests/myproj/vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json",
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json",
"name": "myproject",
"version-string": "0.1.0",
"builtin-baseline": "8dbd66f5a7821ced1ed57696b50375a977006813",
"builtin-baseline": "6a3dd0874f153f8b375ec26210ea6d41dee3bb26",
"dependencies": [
{
"name": "eigen3",
"version>=": "3.4.0"
"version>=": "3.4.0#2"
},
{
"name": "fmt",
"version>=": "8.1.1"
"version>=": "9.1.0#1"
}
]
}
1 change: 1 addition & 0 deletions tests/rpi4-vcpkg/vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json",
"name": "example",
"version-string": "0.1.0",
"dependencies": [
Expand Down

0 comments on commit 95ecb4c

Please sign in to comment.