Skip to content

Commit

Permalink
NMake Builds: Use rustc to query the required system libs
Browse files Browse the repository at this point in the history
Extend the helper batch file that we use to query the default Rust toolchain so
that we can use the rustc that corresponds to the Rust toolchain that we are
using to query the system libraries (i.e. from the Windows SDK), in order to
ensure that the final librvg DLL links, as Rust might involve changes that
require more system libraries to be required for the build, such as in issue

[1]: rust-lang/rust#108262

Fixes issue #968.

Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/842>
  • Loading branch information
fanc999-1 authored and Marge Bot committed Jun 13, 2023
1 parent 615c0e3 commit 3dcce37
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 76 deletions.
2 changes: 1 addition & 1 deletion win32/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ EXTRA_DIST = \
Makefile.vc \
MSVC-Builds.md \
rsvg-rust.mak \
rust-default-target.bat \
rust-query-cfg.bat \
pc_base.py \
replace.py \
rsvgpc.py
3 changes: 2 additions & 1 deletion win32/build-rules-msvc.mak
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ $(LIBRSVG_LIB): $(LIBRSVG_DLL)
# $(dependent_objects)
# <<
# @-if exist $@.manifest mt /manifest $@.manifest /outputresource:$@;2

$(LIBRSVG_DLL): $(RSVG_INTERNAL_LIB) $(librsvg_OBJS) $(LIBRSVG_DEF)
link /DLL $(LDFLAGS) $(LIBRSVG_DEP_LIBS) \
link /DLL $(LDFLAGS) $(LIBRSVG_DEP_LIBS) $(LIBRSVG_SYSTEM_DEP_LIBS) \
/implib:$(LIBRSVG_LIB) \
-out:$@ /def:$(LIBRSVG_DEF) @<<
$(librsvg_OBJS)
Expand Down
7 changes: 1 addition & 6 deletions win32/config-msvc.mak.in
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,7 @@ LIBRSVG_DEP_LIBS = \
$(LIBRSVG_EXTRA_DEP_LIBS) \
pango-1.0.lib \
cairo-gobject.lib \
$(BASE_DEP_LIBS) \
libxml2.lib \
advapi32.lib \
userenv.lib \
bcrypt.lib \
ws2_32.lib
$(BASE_DEP_LIBS)

RSVG_PIXBUF_LOADER_CFLAGS = \
$(BASE_CFLAGS) \
Expand Down
58 changes: 26 additions & 32 deletions win32/rsvg-rust.mak
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ CARGO = %HOMEPATH%\.cargo\bin\cargo
RUSTUP = %HOMEPATH%\.cargo\bin\rustup
!endif

!if [call rust-default-target.bat $(RUSTUP)]
!endif
!include rust-cfg.mak
!if [del /f/q rust-cfg.mak]
!endif

# For those who wish to use a particular toolchain version to build librsvg
!if defined(TOOLCHAIN_VERSION)
TOOLCHAIN_TYPE = $(TOOLCHAIN_VERSION)
Expand All @@ -27,19 +21,28 @@ TOOLCHAIN_TYPE =
!endif

!if "$(TOOLCHAIN_TYPE)" == ""
!if [call rust-query-cfg.bat use-rustup $(RUSTUP)]
!endif
!include rust-cfg.mak
!if [del /f/q rust-cfg.mak]
!endif
!if "$(RUST_DEFAULT_COMPILER)" != "pc-windows-msvc"
!error The default Rust toolchain is not an MSVC toolchain. Please use `rustup` to set the default to an MSVC toolchain
!endif
TOOLCHAIN_TYPE = $(RUST_DEFAULT_CHANNEL)
BUILD_HOST = $(RUST_DEFAULT_TARGET)
BUILD_HOST = $(RUST_DEFAULT_MSVC_TARGET)
RUST_HOST = $(RUST_DEFAULT_TARGET)
# non-default toolchain requested
!else
!if "$(PROCESSOR_ARCHITECTURE)" == "x64" || "$(PROCESSOR_ARCHITECTURE)" == "X64" || "$(PROCESSOR_ARCHITECTURE)" == "AMD64"
BUILD_HOST = x64
RUST_HOST = x86_64
!elseif "$(PROCESSOR_ARCHITECTURE)" == "ARM64"
BUILD_HOST = arm64
RUST_HOST = aarch64
!elseif "$(PROCESSOR_ARCHITECTURE)" == "x86"
BUILD_HOST = Win32
RUST_HOST = i686
!endif
!endif

Expand All @@ -55,34 +58,13 @@ FORCE_CROSS = 0
# Setup cross builds if needed
!if "$(PLAT)" == "x64"
RUST_TARGET = x86_64
!if "$(BUILD_HOST)" != "$(PLAT)"
FORCE_CROSS = 1
!if "$(BUILD_HOST)" == "arm64"
RUST_HOST = aarch64
!elseif "$(BUILD_HOST)" == "Win32"
RUST_HOST = i686
!endif
!endif
!elseif "$(PLAT)" == "arm64"
RUST_TARGET = aarch64
!if "$(BUILD_HOST)" != "$(PLAT)"
FORCE_CROSS = 1
!if "$(BUILD_HOST)" == "x64"
RUST_HOST = x86_64
!elseif "$(BUILD_HOST)" == "Win32"
RUST_HOST = i686
!endif
!endif
!else
RUST_TARGET = i686
!endif
!if "$(BUILD_HOST)" != "$(PLAT)"
FORCE_CROSS = 1
!if "$(BUILD_HOST)" == "arm64"
RUST_HOST = aarch64
!elseif "$(BUILD_HOST)" == "x64"
RUST_HOST = x86_64
!endif
!endif
!endif

!if "$(VALID_CFGSET)" == "TRUE"
Expand All @@ -92,11 +74,8 @@ BUILD_RUST = 0
!endif

!if "$(BUILD_RUST)" == "1"

CARGO_TARGET = $(RUST_TARGET)-pc-windows-msvc
CARGO_TARGET_TOOLCHAIN = +$(TOOLCHAIN_TYPE)-$(CARGO_TARGET)

RUSTUP_CMD = $(RUSTUP) default $(DEFAULT_TARGET)
CARGO_TARGET_DIR = vs$(VSVER)\$(CFG)\$(PLAT)\obj\rsvg_c_api
CARGO_TARGET_DIR_FLAG = --target-dir=$(CARGO_TARGET_DIR)

Expand All @@ -109,10 +88,25 @@ CARGO_CMD = $(CARGO) $(RUST_HOST_TOOLCHAIN) --locked build $(CARGO_TARGET_CMD) $
CARGO_CLEAN_CMD = $(CARGO) $(RUST_HOST_TOOLCHAIN) clean $(CARGO_TARGET_CMD) $(MANIFEST_PATH_FLAG) $(CARGO_TARGET_DIR_FLAG)
CARGO_TARGET_OUTPUT_DIR = $(CARGO_TARGET_DIR)\$(CARGO_TARGET)\$(CFG)
!else
CARGO_TARGET_TOOLCHAIN = +$(TOOLCHAIN_TYPE)-$(CARGO_TARGET)

CARGO_CMD = $(CARGO) $(CARGO_TARGET_TOOLCHAIN) --locked build $(MANIFEST_PATH_FLAG) $(CARGO_TARGET_DIR_FLAG)
CARGO_CLEAN_CMD = $(CARGO) $(CARGO_TARGET_TOOLCHAIN) clean $(MANIFEST_PATH_FLAG) $(CARGO_TARGET_DIR_FLAG)
CARGO_TARGET_OUTPUT_DIR = $(CARGO_TARGET_DIR)\$(CFG)
!endif

# Query the system libs that we will be using to link the librsvg DLL
!if $(FORCE_CROSS) > 0
!if [call rust-query-cfg.bat check-syslibs $(RUSTUP) $(RUST_HOST_TOOLCHAIN) $(RUST_TARGET)]
!endif
!else
!if [call rust-query-cfg.bat check-syslibs $(RUSTUP) $(CARGO_TARGET_TOOLCHAIN) $(RUST_TARGET)]
!endif
!endif
!include rust-sys-libs.mak
!if [del /f/q rust-sys-libs.mak]
!endif

!if "$(CFG)" == "release" || "$(CFG)" == "Release"
CARGO_CMD = $(CARGO_CMD) --release
!endif
Expand Down
36 changes: 0 additions & 36 deletions win32/rust-default-target.bat

This file was deleted.

78 changes: 78 additions & 0 deletions win32/rust-query-cfg.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
@echo off
if not "%1" == "use-rustup" if not "%1" == "check-syslibs" goto :err_badopt
if "%2" == "" goto :err_badopt

set RUSTUP=%2
if "%1" == "check-syslibs" goto :check_syslibs

FOR /F "tokens=* USEBACKQ" %%F IN (`%RUSTUP% default`) DO (
SET RUST_DEFAULT_TOOLCHAIN=%%F
)

:: We want to be very sure that if we are using the default
:: Rust toolchain for the build, we are indeed using an msvc
:: one!

FOR /F "tokens=1,2,3,4,5* delims=-" %%a IN ("%RUST_DEFAULT_TOOLCHAIN%") do (
:: <version>-<platform>-pc-windows-msvc (default) or stable-<platform>-pc-windows-msvc (default)
if not "%%a" == "nightly" set CHANNEL=%%a
if not "%%a" == "nightly" set TARGET=%%b
if not "%%a" == "nightly" FOR /F "tokens=1,2 delims= " %%o IN ("%%e") do (
set TOOLCHAIN_COMPILER=%%c-%%d-%%o
)

:: nightly-yyyy-mm-dd-<platform>-pc-windows-msvc (default)
if "%%a" == "nightly" set CHANNEL=%%a-%%b-%%c
if "%%a" == "nightly" set TARGET=%%e
if "%%a" == "nightly" FOR /F "tokens=1,2 delims= " %%o IN ("%%f") do (
set TOOLCHAIN_COMPILER=%%o
)
)

if "%TARGET%" == "aarch64" set NMAKE_TGT=amd64
if "%TARGET%" == "x86_64" set NMAKE_TGT=x64
if "%TARGET%" == "i686" set NMAKE_TGT=Win32

if exist rust-cfg.mak goto :EOF
echo RUST_DEFAULT_CHANNEL=%CHANNEL%>>rust-cfg.mak
echo RUST_DEFAULT_TARGET=%TARGET%>>rust-cfg.mak
echo RUST_DEFAULT_MSVC_TARGET=%NMAKE_TGT%>>rust-cfg.mak
echo RUST_DEFAULT_COMPILER=%TOOLCHAIN_COMPILER%>>rust-cfg.mak
goto :EOF

:check_syslibs
if "%3" == "" goto :err_badopt
if "%4" == "" goto :err_badopt
if not "%4" == "x86_64" if not "%4" == "aarch64" if not "%4" == "i686" goto :err_badopt

:: grab the results from the following command to extract the needed system
:: libs for linking for Rust builds (this is printed in stderr)
if exist rust-libs.txt goto :check_libs
%RUSTUP:rustup=rustc% %3 --target=%4-pc-windows-msvc ^
--crate-type staticlib --print native-static-libs - < nul 2>>rust-libs.txt

:check_libs
for /f "tokens=1,2*" %%l in ('findstr /ic:"note: native-static-libs:" /b rust-libs.txt') DO (
set ALL_SYS_LIBS=%%n
)
SETLOCAL ENABLEDELAYEDEXPANSION
set LINK_SYS_LIBS=
del /f/q rust-libs.txt
del /f/q rust_out.lib
:: Filter out kernel32.lib, msvcrt.lib and legacy_stdio_definitions.lib
:: they will be pulled in automatically
for %%q in (%ALL_SYS_LIBS%) do (
if not "%%q" == "kernel32.lib" if not "%%q" == "msvcrt.lib" ^
if not "%%q" == "legacy_stdio_definitions.lib" ^
if not "!LINK_SYS_LIBS!" == "" set LINK_SYS_LIBS=!LINK_SYS_LIBS! %%q

if not "%%q" == "kernel32.lib" if not "%%q" == "msvcrt.lib" ^
if not "%%q" == "legacy_stdio_definitions.lib" ^
if "!LINK_SYS_LIBS!" == "" set LINK_SYS_LIBS=%%q
)

echo LIBRSVG_SYSTEM_DEP_LIBS=%LINK_SYS_LIBS%>rust-sys-libs.mak
goto :EOF

:err_badopt
echo Usage: %0 [use-rustup^|check-syslibs] rustup-executable ^<rust-toolchain-for-check-syslibs^> ^<rust-target-platform-for-check-syslibs^>

0 comments on commit 3dcce37

Please sign in to comment.