Skip to content

Commit

Permalink
Cmake: use ENV{VCPKG_ROOT}, fix msgfmt + minor.
Browse files Browse the repository at this point in the history
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 linking script invocation by adding WORKING_DIRECTORY.

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
  • Loading branch information
rkitover committed Feb 4, 2022
1 parent 5437f0f commit 156fa9a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,4 @@ Release/
*.dSYM
/contrib/buildsystems/out
CMakeSettings.json
build*/
30 changes: 19 additions & 11 deletions compat/vcbuild/vcpkg_install.bat
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,46 @@ REM ================================================================
@FOR /F "delims=" %%D IN ("%~dp0") DO @SET cwd=%%~fD
cd %cwd%

dir vcpkg\vcpkg.exe >nul 2>nul && GOTO :install_libraries
IF NOT DEFINED VCPKG_ROOT (
set VCPKG_ROOT=%cwd%\vcpkg
) ELSE (IF NOT EXIST %VCPKG_ROOT%\..\ (
echo Invalid VCPKG_ROOT: %VCPKG_ROOT%, not under a valid directory.
exit /B 1
))

IF EXIST %VCPKG_ROOT%\vcpkg.exe goto :install_libraries

git.exe version 2>nul
IF ERRORLEVEL 1 (
echo "***"
echo "Git not found. Please adjust your CMD path or Git install option."
echo "***"
EXIT /B 1 )
echo ***
echo Git not found. Please adjust your CMD path or Git install option.
echo ***
EXIT /B 1
)

echo Fetching vcpkg in %cwd%vcpkg
git.exe clone https://github.com/Microsoft/vcpkg vcpkg
echo Fetching vcpkg in %VCPKG_ROOT%
git.exe clone https://github.com/Microsoft/vcpkg %VCPKG_ROOT%
IF ERRORLEVEL 1 ( EXIT /B 1 )

cd vcpkg
cd %VCPKG_ROOT%
echo Building vcpkg
powershell -exec bypass scripts\bootstrap.ps1
IF ERRORLEVEL 1 ( EXIT /B 1 )

echo Successfully installed %cwd%vcpkg\vcpkg.exe
echo Successfully installed %VCPKG_ROOT%\vcpkg.exe

:install_libraries

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

:install_defines
cd %cwd%
SET inst=%cwd%vcpkg\installed\%arch%
SET inst=%VCPKG_ROOT%\installed\%arch%

echo vcpkg_inc=-I"%inst%\include">VCPKG-DEFS
echo vcpkg_rel_lib=-L"%inst%\lib">>VCPKG-DEFS
Expand Down
99 changes: 76 additions & 23 deletions contrib/buildsystems/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@ This process generates a Makefile(Linux/*BSD/MacOS), Visual Studio solution(Wind
Run `make` to build Git on Linux/*BSD/MacOS.
Open git.sln on Windows and build Git.
NOTE: By default CMake will install vcpkg locally to your source tree on configuration,
to avoid this, add `-DNO_VCPKG=TRUE` to the command line when configuring.
NOTE: By default CMake will install vcpkg locally to your source tree on
configuration, to use your own VCPKG tree, set `$env:VCPKG_ROOT` to the
path or pass `VCPKG_DIR`.
To set the vcpkg arch (target triplet) pass `VCPKG_ARCH` e.g.:
`-DVCPKG_ARCH=x64-windows`.
To not use VCPKG, add `-DUSE_VCPKG=FALSE` to the command line when
configuring.
The Visual Studio default generator changed in v16.6 from its Visual Studio
implemenation to `Ninja` This required changes to many CMake scripts.
Expand All @@ -67,7 +74,27 @@ if(NOT DEFINED CMAKE_EXPORT_COMPILE_COMMANDS)
endif()

if(USE_VCPKG)
set(VCPKG_DIR "${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg")
if(NOT DEFINED VCPKG_DIR)
if(DEFINED ENV{VCPKG_ROOT})
set(VCPKG_DIR "$ENV{VCPKG_ROOT}")
else()
set(VCPKG_DIR "${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg")
endif()

# Make sure ENV{VCPKG_ROOT} is set for vcpkg_install.bat
# and anything else that may need it.
set(ENV{VCPKG_ROOT} "${VCPKG_DIR}")
endif()

if(NOT DEFINED VCPKG_ARCH)
if(DEFINED VCPKG_TARGET_TRIPLET)
set(VCPKG_ARCH "${VCPKG_TARGET_TRIPLET}")
else()
message("VCPKG_ARCH: unset, using 'x64-windows'")
set(VCPKG_ARCH "x64-windows")
endif()
endif()

message("WIN32: ${WIN32}") # show its underlying text values
message("VCPKG_DIR: ${VCPKG_DIR}")
message("VCPKG_ARCH: ${VCPKG_ARCH}") # maybe unset
Expand All @@ -77,14 +104,8 @@ if(USE_VCPKG)
message("CMAKE_GENERATOR_PLATFORM: ${CMAKE_GENERATOR_PLATFORM}")
message("CMAKE_EXPORT_COMPILE_COMMANDS: ${CMAKE_EXPORT_COMPILE_COMMANDS}")
message("ENV(CMAKE_EXPORT_COMPILE_COMMANDS): $ENV{CMAKE_EXPORT_COMPILE_COMMANDS}")
if(NOT EXISTS ${VCPKG_DIR})
message("Initializing vcpkg and building the Git's dependencies (this will take a while...)")
execute_process(COMMAND ${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg_install.bat ${VCPKG_ARCH})
endif()
if(NOT EXISTS ${VCPKG_ARCH})
message("VCPKG_ARCH: unset, using 'x64-windows'")
set(VCPKG_ARCH "x64-windows") # default from vcpkg_install.bat
endif()
message("Initializing vcpkg and building the Git's dependencies (this may take a while...)")
execute_process(COMMAND ${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg_install.bat ${VCPKG_ARCH})
list(APPEND CMAKE_PREFIX_PATH "${VCPKG_DIR}/installed/${VCPKG_ARCH}")

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

if(NO_GETTEXT)
message(STATUS "msgfmt not used under NO_GETTEXT")
else()
macro(get_nuget)
if(NOT EXISTS ${CMAKE_BINARY_DIR}/nuget.exe)
file(DOWNLOAD "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" ${CMAKE_BINARY_DIR}/nuget.exe)
endif()

# Add nuget package source.
execute_process(
COMMAND nuget sources add -Name "NuGet official package source" -Source "https://api.nuget.org/v3/index.json"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
endmacro()

macro(nuget_install pkg)
execute_process(
COMMAND nuget.exe install ${pkg} -OutputDirectory ${CMAKE_BINARY_DIR}/nuget
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

# Find the path to the binaries in the package and add them to find path.
file(GLOB pkg_dir ${CMAKE_BINARY_DIR}/nuget/${pkg}*)

list(APPEND CMAKE_PROGRAM_PATH ${pkg_dir}/tools/bin)

set(CMAKE_PROGRAM_PATH "${CMAKE_PROGRAM_PATH}" CACHE STRING "External Program Search Path" FORCE)
endmacro()

find_program(msgfmt msgfmt)
if(NOT msgfmt)
if(WIN32)
get_nuget()
nuget_install(Gettext.Tools)
endif()

find_program(MSGFMT_EXE msgfmt)

if(NOT MSGFMT_EXE)
if(USE_VCPKG)
set(MSGFMT_EXE ${CMAKE_SOURCE_DIR}/compat/vcbuild/vcpkg/downloads/tools/msys2/msys64/usr/bin/msgfmt.exe)
endif()
if(NOT EXISTS ${MSGFMT_EXE})
message(WARNING "Text Translations won't be built")
unset(MSGFMT_EXE)
endif()
message(WARNING "msgfmt not available and/or could not be installed, text translations won't be built.")
endif()
else()
set(MSGFMT_EXE ${msgfmt})
endif()

if(MSGFMT_EXE)
set(MSGFMT_EXE ${MSGFMT_EXE} CACHE STRING "gettext msgfmt utility path" FORCE)
endif()

#Force all visual studio outputs to CMAKE_BINARY_DIR
Expand Down Expand Up @@ -813,7 +865,8 @@ endif()

add_custom_command(OUTPUT ${git_links} ${git_http_links}
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/CreateLinks.cmake
DEPENDS git git-remote-http)
DEPENDS git git-remote-http
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_custom_target(git-links ALL DEPENDS ${git_links} ${git_http_links})


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

#Make the tests work when building out of the source tree
Expand Down

0 comments on commit 156fa9a

Please sign in to comment.