Skip to content

Commit

Permalink
meson: Fix generating static and both libraries under MSVC
Browse files Browse the repository at this point in the history
Back when I implemented the mesonification of librsvg, I forced the
installable target of rsvg-2 to be a shared library. This was because
Meson and Rust, at link time, differ on the naming convention expected
of static libraries:

rust-lang/rust#122455

https://mesonbuild.com/FAQ.html#why-does-building-my-project-with-msvc-output-static-libraries-called-libfooa

I did not realise at the time that the fix merged in !938 (commit
73d7167) was not aware of this issue,
and so it broke the Cerbero builds targeting Microsoft compilers.
This commit works around the Rust bug by following the Linux convention for
static libraries, and then using a custom target to rename the Rust-generated
library if under MSVC.

All targets that depend on librsvg_lib do so now unconditionally, given
they must link at a time where the file outputs are settled.

Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/957>
  • Loading branch information
amyspark committed Apr 18, 2024
1 parent 702c4e3 commit e8dc0cb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
3 changes: 2 additions & 1 deletion gdk-pixbuf-loader/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pixbufloader = custom_target(
console: true,
install: true,
install_dir: pixbuf_dep.get_variable(pkgconfig: 'gdk_pixbuf_moduledir'),
depends: librsvg_rust_dep,
depend_files: pixbufloader_sources,
env: extra_env,
command: [
Expand Down Expand Up @@ -65,6 +66,6 @@ if build_tests.allowed()
'--packages', 'pixbufloader-svg',
],
env: extra_env,
depends: librsvg_lib
depends: pixbufloader
)
endif
16 changes: 4 additions & 12 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,7 @@ extra_env.set('PKG_CONFIG', pkg_config.full_path())

pkg_config_path = get_option('pkg_config_path')
if pkg_config_path.length() > 0
pathsep = build_machine.system() == 'windows' ? ';' : ':'
extra_env.set('PKG_CONFIG_PATH', pathsep.join(pkg_config_path))
extra_env.set('PKG_CONFIG_PATH', pkg_config_path)
endif

# Set up the environment that will be fed to the build
Expand All @@ -375,8 +374,8 @@ current_library_path = run_command(
)

extra_env.set(var, current_library_path.stdout().strip())
extra_env.prepend(var, meson.project_build_root() / 'rsvg')
if host_system == 'windows'
extra_env.prepend('PATH', meson.project_build_root() / 'rsvg')
foreach i: library_dependencies_sole
x = i.get_variable(pkgconfig: 'bindir', default_value: '')
if x != ''
Expand All @@ -391,17 +390,10 @@ includeinc = include_directories('include')

# Set the suffixes up
if host_system == 'windows'
lib_prefix = is_msvc_style ? '' : 'lib'
ext_dynamic = 'dll'
ext_static = is_msvc_style ? 'lib' : 'a'
ext_exe = '.exe'
if is_msvc_style
lib_prefix = ''
ext_import = 'dll.lib'
ext_static = 'lib'
else
lib_prefix = 'lib'
ext_import = 'dll.a'
ext_static = 'a'
endif
elif host_system == 'darwin'
lib_prefix = 'lib'
ext_dynamic = 'dylib'
Expand Down
39 changes: 33 additions & 6 deletions rsvg/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,13 @@ else
)
endif

if get_option('default_library') == 'shared'
librsvg_lib = shared_library(
librsvg_lib = librsvg_c_lib

# Wait on this dependency before trying to build using Rust
librsvg_rust_dep = rust_artifacts

if get_option('default_library') in ['shared', 'both']
librsvg_shared_lib = shared_library(
rsvg_ver,
rsvg_dummy,
link_whole: librsvg_c_lib,
Expand All @@ -227,8 +232,30 @@ if get_option('default_library') == 'shared'
install: true,
version: meson.project_version() # it's not exact as m4
)
else
librsvg_lib = librsvg_c_lib
if get_option('default_library') == 'shared'
librsvg_lib = librsvg_shared_lib
endif
librsvg_rust_dep = librsvg_shared_lib
endif
if get_option('default_library') in ['static', 'both']
# Rust cannot yet use import libraries for MSVC which do not end in .lib.
# The static library must be manually generated so that it matches Meson's
# naming convention.
# See https://github.com/rust-lang/rust/issues/122455
librsvg_lib = custom_target(
'rsvg-2-static',
input: librsvg_c_lib,
output: '@0@rsvg-@1@.@2@'.format(lib_prefix, librsvg_api_major, 'a'),
command: [
python,
'-c',
'import os; import sys; import shutil; shutil.copy(sys.argv[1], sys.argv[2])',
'@INPUT0@',
'@OUTPUT0@'
]
)

librsvg_rust_dep = librsvg_lib
endif

# This is the main dependency to use for tests; it means "link to the library we just built"
Expand Down Expand Up @@ -340,7 +367,7 @@ if build_tests.allowed()
'--packages', 'librsvg',
] + avif_feature_args,
env: extra_env,
depends: librsvg_lib
depends: librsvg_rust_dep
)

test(
Expand All @@ -356,6 +383,6 @@ if build_tests.allowed()
] + avif_feature_args
+ pixbuf_feature_args,
env: extra_env,
depends: get_option('default_library') == 'shared' ? librsvg_lib : rust_artifacts
depends: librsvg_rust_dep
)
endif
2 changes: 1 addition & 1 deletion rsvg_convert/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rsvg_convert = custom_target(
console: true,
install: true,
install_dir: get_option('prefix') / get_option('bindir'),
depends: rust_artifacts,
depends: librsvg_rust_dep,
depend_files: rsvg_convert_files,
env: extra_env,
command: [
Expand Down

0 comments on commit e8dc0cb

Please sign in to comment.