Skip to content

Commit b8d2ed4

Browse files
committed
Cmake: use ENV{VCPKG_ROOT}, fix msgfmt + minor.
Set VCPKG_DIR from ENV{VCPKG_ROOT} if set, otherwise set ENV{VCPKG_ROOT}. Set VCPKG_ARCH from VCPKG_TARGET_TRIPLET if set. Fix check for VCPKG_ARCH being defined. Describe options in comment at the top of the file. Add build*/ out-of-source build dirs to toplevel .gitignore. Change vcpkg_install.bat to use %VCPKG_ROOT% set by cmake or present in the environment, and update/install only what's necessary. Use Gettext.Tools from nuget on Windows for msgfmt. Fix cmake hardlinking script invocation by adding WORKING_DIRECTORY. Signed-off-by: Rafael Kitover <[email protected]>
1 parent 5437f0f commit b8d2ed4

File tree

5 files changed

+192
-34
lines changed

5 files changed

+192
-34
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,4 @@ Release/
245245
*.dSYM
246246
/contrib/buildsystems/out
247247
CMakeSettings.json
248+
build*/

compat/vcbuild/vcpkg_install.bat

+19-11
Original file line numberDiff line numberDiff line change
@@ -40,38 +40,46 @@ REM ================================================================
4040
@FOR /F "delims=" %%D IN ("%~dp0") DO @SET cwd=%%~fD
4141
cd %cwd%
4242

43-
dir vcpkg\vcpkg.exe >nul 2>nul && GOTO :install_libraries
43+
IF NOT DEFINED VCPKG_ROOT (
44+
set VCPKG_ROOT=%cwd%\vcpkg
45+
) ELSE (IF NOT EXIST %VCPKG_ROOT%\..\ (
46+
echo Invalid VCPKG_ROOT: %VCPKG_ROOT%, not under a valid directory.
47+
exit /B 1
48+
))
49+
50+
IF EXIST %VCPKG_ROOT%\vcpkg.exe goto :install_libraries
4451

4552
git.exe version 2>nul
4653
IF ERRORLEVEL 1 (
47-
echo "***"
48-
echo "Git not found. Please adjust your CMD path or Git install option."
49-
echo "***"
50-
EXIT /B 1 )
54+
echo ***
55+
echo Git not found. Please adjust your CMD path or Git install option.
56+
echo ***
57+
EXIT /B 1
58+
)
5159

52-
echo Fetching vcpkg in %cwd%vcpkg
53-
git.exe clone https://github.com/Microsoft/vcpkg vcpkg
60+
echo Fetching vcpkg in %VCPKG_ROOT%
61+
git.exe clone https://github.com/Microsoft/vcpkg %VCPKG_ROOT%
5462
IF ERRORLEVEL 1 ( EXIT /B 1 )
5563

56-
cd vcpkg
64+
cd %VCPKG_ROOT%
5765
echo Building vcpkg
5866
powershell -exec bypass scripts\bootstrap.ps1
5967
IF ERRORLEVEL 1 ( EXIT /B 1 )
6068

61-
echo Successfully installed %cwd%vcpkg\vcpkg.exe
69+
echo Successfully installed %VCPKG_ROOT%\vcpkg.exe
6270

6371
:install_libraries
6472

6573
echo Installing third-party libraries(%arch%)...
6674
FOR %%i IN (zlib expat libiconv openssl libssh2 curl) DO (
67-
cd %cwd%vcpkg
75+
cd %VCPKG_ROOT%
6876
IF NOT EXIST "packages\%%i_%arch%" CALL :sub__install_one %%i
6977
IF ERRORLEVEL 1 ( EXIT /B 1 )
7078
)
7179

7280
:install_defines
7381
cd %cwd%
74-
SET inst=%cwd%vcpkg\installed\%arch%
82+
SET inst=%VCPKG_ROOT%\installed\%arch%
7583

7684
echo vcpkg_inc=-I"%inst%\include">VCPKG-DEFS
7785
echo vcpkg_rel_lib=-L"%inst%\lib">>VCPKG-DEFS

contrib/buildsystems/CMakeLists.txt

+53-23
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,15 @@ This process generates a Makefile(Linux/*BSD/MacOS), Visual Studio solution(Wind
4444
Run `make` to build Git on Linux/*BSD/MacOS.
4545
Open git.sln on Windows and build Git.
4646
47-
NOTE: By default CMake will install vcpkg locally to your source tree on configuration,
48-
to avoid this, add `-DNO_VCPKG=TRUE` to the command line when configuring.
47+
NOTE: By default CMake will install vcpkg locally to your source tree on
48+
configuration, to use your own VCPKG tree, set `$env:VCPKG_ROOT` to the
49+
path or pass `VCPKG_DIR`.
50+
51+
To set the vcpkg arch (target triplet) pass `VCPKG_ARCH` e.g.:
52+
`-DVCPKG_ARCH=x64-windows`.
53+
54+
To not use VCPKG, add `-DUSE_VCPKG=FALSE` to the command line when
55+
configuring.
4956
5057
The Visual Studio default generator changed in v16.6 from its Visual Studio
5158
implemenation to `Ninja` This required changes to many CMake scripts.
@@ -56,6 +63,8 @@ cmake_minimum_required(VERSION 3.14)
5663
#set the source directory to root of git
5764
set(CMAKE_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
5865

66+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)
67+
5968
option(USE_VCPKG "Whether or not to use vcpkg for obtaining dependencies. Only applicable to Windows platforms" ON)
6069
if(NOT WIN32)
6170
set(USE_VCPKG OFF CACHE BOOL FORCE)
@@ -67,7 +76,27 @@ if(NOT DEFINED CMAKE_EXPORT_COMPILE_COMMANDS)
6776
endif()
6877

6978
if(USE_VCPKG)
70-
set(VCPKG_DIR "${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg")
79+
if(NOT DEFINED VCPKG_DIR)
80+
if(DEFINED ENV{VCPKG_ROOT})
81+
set(VCPKG_DIR "$ENV{VCPKG_ROOT}")
82+
else()
83+
set(VCPKG_DIR "${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg")
84+
endif()
85+
86+
# Make sure ENV{VCPKG_ROOT} is set for vcpkg_install.bat
87+
# and anything else that may need it.
88+
set(ENV{VCPKG_ROOT} "${VCPKG_DIR}")
89+
endif()
90+
91+
if(NOT DEFINED VCPKG_ARCH)
92+
if(DEFINED VCPKG_TARGET_TRIPLET)
93+
set(VCPKG_ARCH "${VCPKG_TARGET_TRIPLET}")
94+
else()
95+
message("VCPKG_ARCH: unset, using 'x64-windows'")
96+
set(VCPKG_ARCH "x64-windows")
97+
endif()
98+
endif()
99+
71100
message("WIN32: ${WIN32}") # show its underlying text values
72101
message("VCPKG_DIR: ${VCPKG_DIR}")
73102
message("VCPKG_ARCH: ${VCPKG_ARCH}") # maybe unset
@@ -77,14 +106,8 @@ if(USE_VCPKG)
77106
message("CMAKE_GENERATOR_PLATFORM: ${CMAKE_GENERATOR_PLATFORM}")
78107
message("CMAKE_EXPORT_COMPILE_COMMANDS: ${CMAKE_EXPORT_COMPILE_COMMANDS}")
79108
message("ENV(CMAKE_EXPORT_COMPILE_COMMANDS): $ENV{CMAKE_EXPORT_COMPILE_COMMANDS}")
80-
if(NOT EXISTS ${VCPKG_DIR})
81-
message("Initializing vcpkg and building the Git's dependencies (this will take a while...)")
82-
execute_process(COMMAND ${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg_install.bat ${VCPKG_ARCH})
83-
endif()
84-
if(NOT EXISTS ${VCPKG_ARCH})
85-
message("VCPKG_ARCH: unset, using 'x64-windows'")
86-
set(VCPKG_ARCH "x64-windows") # default from vcpkg_install.bat
87-
endif()
109+
message("Initializing vcpkg and building the Git's dependencies (this may take a while...)")
110+
execute_process(COMMAND ${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg_install.bat ${VCPKG_ARCH})
88111
list(APPEND CMAKE_PREFIX_PATH "${VCPKG_DIR}/installed/${VCPKG_ARCH}")
89112

90113
# In the vcpkg edition, we need this to be able to link to libcurl
@@ -207,18 +230,22 @@ if(WIN32 AND NOT MSVC)#not required for visual studio builds
207230
endif()
208231
endif()
209232

210-
if(NO_GETTEXT)
211-
message(STATUS "msgfmt not used under NO_GETTEXT")
212-
else()
233+
234+
find_program(MSGFMT_EXE msgfmt)
235+
236+
if(NOT MSGFMT_EXE)
237+
if(WIN32)
238+
include(NuGet)
239+
nuget_install(Gettext.Tools)
240+
endif()
241+
242+
unset(MSGFMT_EXE)
243+
unset(MSGFMT_EXE CACHE)
244+
213245
find_program(MSGFMT_EXE msgfmt)
246+
214247
if(NOT MSGFMT_EXE)
215-
if(USE_VCPKG)
216-
set(MSGFMT_EXE ${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg/downloads/tools/msys2/msys64/usr/bin/msgfmt.exe)
217-
endif()
218-
if(NOT EXISTS ${MSGFMT_EXE})
219-
message(WARNING "Text Translations won't be built")
220-
unset(MSGFMT_EXE)
221-
endif()
248+
message(WARNING "msgfmt not available and/or could not be installed, text translations won't be built.")
222249
endif()
223250
endif()
224251

@@ -813,7 +840,8 @@ endif()
813840

814841
add_custom_command(OUTPUT ${git_links} ${git_http_links}
815842
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/CreateLinks.cmake
816-
DEPENDS git git-remote-http)
843+
DEPENDS git git-remote-http
844+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
817845
add_custom_target(git-links ALL DEPENDS ${git_links} ${git_http_links})
818846

819847

@@ -1074,7 +1102,7 @@ file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "RUNTIME_PREFIX='${RUNTIME_PRE
10741102
file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_PYTHON='${NO_PYTHON}'\n")
10751103
file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "SUPPORTS_SIMPLE_IPC='${SUPPORTS_SIMPLE_IPC}'\n")
10761104
if(USE_VCPKG)
1077-
file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PATH=\"$PATH:$TEST_DIRECTORY/../compat/vcbuild/vcpkg/installed/${VCPKG_ARCH}/bin\"\n")
1105+
file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PATH=\"$PATH:${VCPKG_DIR}/installed/${VCPKG_ARCH}/bin\"\n")
10781106
endif()
10791107

10801108
#Make the tests work when building out of the source tree
@@ -1106,3 +1134,5 @@ foreach(tsh ${test_scipts})
11061134
endforeach()
11071135

11081136
endif()#BUILD_TESTING
1137+
1138+
# vim:set sw=8 ts=8 noet:
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
include(Utilities)
2+
3+
#
4+
# nuget_install([pkg_name])
5+
#
6+
# Installs nuget.exe in the output directory if not already present
7+
# and uses it to install pkg_name if provided.
8+
#
9+
# If the package contains a 'tools/bin' directory, it will be
10+
# appended to CMAKE_PROGRAM_PATH.
11+
#
12+
# Throws a fatal error if any problems are encountered.
13+
#
14+
function(nuget_install pkg)
15+
if(NOT EXISTS "${CMAKE_BINARY_DIR}/nuget.exe")
16+
file_download("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" "${CMAKE_BINARY_DIR}/nuget.exe" REQUIRED)
17+
18+
# Add nuget package source if not already present.
19+
execute_process(
20+
COMMAND nuget sources add -Name "NuGet official package source" -Source "https://api.nuget.org/v3/index.json"
21+
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
22+
OUTPUT_QUIET
23+
ERROR_QUIET
24+
)
25+
endif()
26+
27+
if("${pkg}" STREQUAL "")
28+
return()
29+
endif()
30+
31+
execute_process(
32+
COMMAND nuget.exe install "${pkg}" -OutputDirectory "${CMAKE_BINARY_DIR}/nuget"
33+
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
34+
OUTPUT_VARIABLE install_output
35+
RESULT_VARIABLE install_exit_status
36+
)
37+
38+
if(NOT install_exit_status EQUAL 0)
39+
message(FATAL_ERROR "NuGet installation of package '${pkg}' failed, exit code: ${install_exit_status}")
40+
endif()
41+
42+
string(REGEX REPLACE ".* package '[^0-9]+([0-9.]+)'.*" "\\1" pkg_ver "${install_output}")
43+
44+
set(pkg_dir "${CMAKE_BINARY_DIR}/nuget/${pkg}.${pkg_ver}")
45+
46+
if(NOT IS_DIRECTORY "${pkg_dir}")
47+
message(FATAL_ERROR "NuGet installation of package '${pkg}' failed, package directory '${pkg_dir}' does not exist.")
48+
endif()
49+
50+
set(pkg_bin "${pkg_dir}/tools/bin")
51+
52+
if(IS_DIRECTORY "${pkg_bin}")
53+
list(APPEND CMAKE_PROGRAM_PATH "${pkg_bin}")
54+
set(CMAKE_PROGRAM_PATH "${CMAKE_PROGRAM_PATH}" PARENT_SCOPE)
55+
set(CMAKE_PROGRAM_PATH "${CMAKE_PROGRAM_PATH}" CACHE STRING "External Program Search Path" FORCE)
56+
endif()
57+
endfunction()
58+
59+
# vim:set sw=8 ts=8 noet:
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#
2+
# file_download(
3+
# url dest_path
4+
# ["STATUS" status_out_var]
5+
# ["REQUIRED"]
6+
# )
7+
#
8+
# Wrapper for cmake file(DOWNLOAD ...) with optional error
9+
# checking. If REQUIRED is passed in, a fatal error will be thrown
10+
# on failure. Otherwise if STATUS is passed in, it will store the
11+
# status list in the out var just like file(DOWNLOAD ...).
12+
#
13+
function(file_download url dest_path)
14+
if(NOT url OR NOT dest_path)
15+
message(FATAL_ERROR "file_download: syntax: file_download(url destination_path [OPTIONS]")
16+
endif()
17+
18+
file(DOWNLOAD "${url}" "${dest_path}" STATUS status_list)
19+
list(GET status_list 0 status)
20+
21+
if("STATUS" IN_LIST ARGN)
22+
list(FIND ARGN STATUS status_idx)
23+
math(EXPR out_var_idx "${status_idx} + 1")
24+
list(GET ARGN "${out_var_idx}" out_var)
25+
26+
if("${out_var}" STREQUAL "")
27+
message(FATAL_ERROR "STATUS must be followed by output variable name for the status list.")
28+
endif()
29+
30+
set("${out_var}" "${status_list}" PARENT_SCOPE)
31+
endif()
32+
33+
if ("REQUIRED" IN_LIST ARGN)
34+
if(NOT status EQUAL 0)
35+
list(GET status_list 1 error_str)
36+
message(FATAL_ERROR "Download of '${url}' failed: ${error_str}")
37+
endif()
38+
endif()
39+
endfunction()
40+
41+
#
42+
# build_option(
43+
# opt_name type help_string [default]
44+
# ["ALIASES" alias1 [alias2 ...]]
45+
# )
46+
#
47+
# If PRIMARY_OPT is not set, uses the value of any alias name
48+
# provided, in order of precedence provides, to set PRIMARY_OPT and
49+
# all listed aliases. Otherwise set all aliases to the value of
50+
# PRIMARY_OPT, or the default if not set either.
51+
#
52+
# On cmake >= 3.13 precedence is given to CACHE variables, and the
53+
# namesake normal variable is overwritten with the CACHE value. On
54+
# earlier versions, the cache variable will be overwritten with the
55+
# normal variable.
56+
#
57+
function(build_option opt type help_string default)
58+
endfunction()
59+
60+
# vim:set sw=8 ts=8 noet:

0 commit comments

Comments
 (0)