Skip to content

Commit d550c77

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 d550c77

File tree

3 files changed

+96
-34
lines changed

3 files changed

+96
-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

+76-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.
@@ -67,7 +74,27 @@ if(NOT DEFINED CMAKE_EXPORT_COMPILE_COMMANDS)
6774
endif()
6875

6976
if(USE_VCPKG)
70-
set(VCPKG_DIR "${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg")
77+
if(NOT DEFINED VCPKG_DIR)
78+
if(DEFINED ENV{VCPKG_ROOT})
79+
set(VCPKG_DIR "$ENV{VCPKG_ROOT}")
80+
else()
81+
set(VCPKG_DIR "${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg")
82+
endif()
83+
84+
# Make sure ENV{VCPKG_ROOT} is set for vcpkg_install.bat
85+
# and anything else that may need it.
86+
set(ENV{VCPKG_ROOT} "${VCPKG_DIR}")
87+
endif()
88+
89+
if(NOT DEFINED VCPKG_ARCH)
90+
if(DEFINED VCPKG_TARGET_TRIPLET)
91+
set(VCPKG_ARCH "${VCPKG_TARGET_TRIPLET}")
92+
else()
93+
message("VCPKG_ARCH: unset, using 'x64-windows'")
94+
set(VCPKG_ARCH "x64-windows")
95+
endif()
96+
endif()
97+
7198
message("WIN32: ${WIN32}") # show its underlying text values
7299
message("VCPKG_DIR: ${VCPKG_DIR}")
73100
message("VCPKG_ARCH: ${VCPKG_ARCH}") # maybe unset
@@ -77,14 +104,8 @@ if(USE_VCPKG)
77104
message("CMAKE_GENERATOR_PLATFORM: ${CMAKE_GENERATOR_PLATFORM}")
78105
message("CMAKE_EXPORT_COMPILE_COMMANDS: ${CMAKE_EXPORT_COMPILE_COMMANDS}")
79106
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()
107+
message("Initializing vcpkg and building the Git's dependencies (this may take a while...)")
108+
execute_process(COMMAND ${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg_install.bat ${VCPKG_ARCH})
88109
list(APPEND CMAKE_PREFIX_PATH "${VCPKG_DIR}/installed/${VCPKG_ARCH}")
89110

90111
# In the vcpkg edition, we need this to be able to link to libcurl
@@ -207,19 +228,50 @@ if(WIN32 AND NOT MSVC)#not required for visual studio builds
207228
endif()
208229
endif()
209230

210-
if(NO_GETTEXT)
211-
message(STATUS "msgfmt not used under NO_GETTEXT")
212-
else()
231+
macro(get_nuget)
232+
if(NOT EXISTS ${CMAKE_BINARY_DIR}/nuget.exe)
233+
file(DOWNLOAD "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" ${CMAKE_BINARY_DIR}/nuget.exe)
234+
endif()
235+
236+
# Add nuget package source.
237+
execute_process(
238+
COMMAND nuget sources add -Name "NuGet official package source" -Source "https://api.nuget.org/v3/index.json"
239+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
240+
)
241+
endmacro()
242+
243+
macro(nuget_install pkg)
244+
execute_process(
245+
COMMAND nuget.exe install ${pkg} -OutputDirectory ${CMAKE_BINARY_DIR}/nuget
246+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
247+
)
248+
249+
# Find the path to the binaries in the package and add them to find path.
250+
file(GLOB pkg_dir ${CMAKE_BINARY_DIR}/nuget/${pkg}*)
251+
252+
list(APPEND CMAKE_PROGRAM_PATH ${pkg_dir}/tools/bin)
253+
254+
set(CMAKE_PROGRAM_PATH "${CMAKE_PROGRAM_PATH}" CACHE STRING "External Program Search Path" FORCE)
255+
endmacro()
256+
257+
find_program(msgfmt msgfmt)
258+
if(NOT msgfmt)
259+
if(WIN32)
260+
get_nuget()
261+
nuget_install(Gettext.Tools)
262+
endif()
263+
213264
find_program(MSGFMT_EXE msgfmt)
265+
214266
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()
267+
message(WARNING "msgfmt not available and/or could not be installed, text translations won't be built.")
222268
endif()
269+
else()
270+
set(MSGFMT_EXE ${msgfmt})
271+
endif()
272+
273+
if(MSGFMT_EXE)
274+
set(MSGFMT_EXE ${MSGFMT_EXE} CACHE STRING "gettext msgfmt utility path" FORCE)
223275
endif()
224276

225277
#Force all visual studio outputs to CMAKE_BINARY_DIR
@@ -813,7 +865,8 @@ endif()
813865

814866
add_custom_command(OUTPUT ${git_links} ${git_http_links}
815867
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/CreateLinks.cmake
816-
DEPENDS git git-remote-http)
868+
DEPENDS git git-remote-http
869+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
817870
add_custom_target(git-links ALL DEPENDS ${git_links} ${git_http_links})
818871

819872

@@ -1074,7 +1127,7 @@ file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "RUNTIME_PREFIX='${RUNTIME_PRE
10741127
file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "NO_PYTHON='${NO_PYTHON}'\n")
10751128
file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "SUPPORTS_SIMPLE_IPC='${SUPPORTS_SIMPLE_IPC}'\n")
10761129
if(USE_VCPKG)
1077-
file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PATH=\"$PATH:$TEST_DIRECTORY/../compat/vcbuild/vcpkg/installed/${VCPKG_ARCH}/bin\"\n")
1130+
file(APPEND ${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS "PATH=\"$PATH:${VCPKG_DIR}/installed/${VCPKG_ARCH}/bin\"\n")
10781131
endif()
10791132

10801133
#Make the tests work when building out of the source tree

0 commit comments

Comments
 (0)