Skip to content

Add "objects" support to Rust targets #14031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/markdown/snippets/rust-objects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## `objects` added correctly to Rust executables

Any objects included in a Rust executable were previously ignored. They
are now added correctly.
36 changes: 24 additions & 12 deletions mesonbuild/backend/ninjabackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,19 +1027,13 @@ def generate_target(self, target) -> None:
pch_objects = []

o, od = self.flatten_object_list(target)
obj_targets = [t for t in od if t.uses_fortran()]
obj_list.extend(o)
fortran_order_deps = self.get_fortran_order_deps(od)

# We don't need this order dep if we're using dyndeps, as the
# depscanner will handle this for us, which produces a better dependency
# graph
fortran_order_deps: T.List[File] = []
if not self.use_dyndeps_for_fortran():
fortran_order_deps = [File(True, *os.path.split(self.get_target_filename(t))) for t in obj_targets]
fortran_inc_args: T.List[str] = []
if target.uses_fortran():
fortran_inc_args = mesonlib.listify([target.compilers['fortran'].get_include_args(
self.get_target_private_dir(t), is_system=False) for t in obj_targets])
self.get_target_private_dir(t), is_system=False) for t in od if t.uses_fortran()])

# add the private directories of all transitive dependencies, which
# are needed for their mod files
Expand Down Expand Up @@ -2029,7 +2023,7 @@ def get_rust_compiler_args(self, target: build.BuildTarget, rustc: Compiler, src
args += target.get_extra_args('rust')
return args

def get_rust_compiler_deps_and_args(self, target: build.BuildTarget, rustc: Compiler) -> T.Tuple[T.List[str], T.List[RustDep], T.List[str]]:
def get_rust_compiler_deps_and_args(self, target: build.BuildTarget, rustc: Compiler) -> T.Tuple[T.List[str], T.List[str], T.List[RustDep], T.List[str]]:
deps: T.List[str] = []
project_deps: T.List[RustDep] = []
args: T.List[str] = []
Expand Down Expand Up @@ -2061,6 +2055,12 @@ def _link_library(libname: str, static: bool, bundle: bool = False):
type_ += ':' + ','.join(modifiers)
args.append(f'-l{type_}={libname}')

objs, od = self.flatten_object_list(target)
for o in objs:
args.append(f'-Clink-arg={o}')
deps.append(o)
fortran_order_deps = self.get_fortran_order_deps(od)

linkdirs = mesonlib.OrderedSet()
external_deps = target.external_deps.copy()
target_deps = target.get_dependencies()
Expand Down Expand Up @@ -2146,7 +2146,7 @@ def _link_library(libname: str, static: bool, bundle: bool = False):
if isinstance(target, build.SharedLibrary) or has_shared_deps:
args += self.get_build_rpath_args(target, rustc)

return deps, project_deps, args
return deps, fortran_order_deps, project_deps, args

def generate_rust_target(self, target: build.BuildTarget) -> None:
rustc = T.cast('RustCompiler', target.compilers['rust'])
Expand All @@ -2170,7 +2170,7 @@ def generate_rust_target(self, target: build.BuildTarget) -> None:
depfile = os.path.join(self.get_target_private_dir(target), target.name + '.d')
args += self.get_rust_compiler_args(target, rustc, target.rust_crate_type, depfile)

deps, project_deps, deps_args = self.get_rust_compiler_deps_and_args(target, rustc)
deps, fortran_order_deps, project_deps, deps_args = self.get_rust_compiler_deps_and_args(target, rustc)
args += deps_args

proc_macro_dylib_path = None
Expand All @@ -2188,6 +2188,8 @@ def generate_rust_target(self, target: build.BuildTarget) -> None:
element = NinjaBuildElement(self.all_outputs, target_name, compiler_name, main_rust_file)
if orderdeps:
element.add_orderdep(orderdeps)
if fortran_order_deps:
element.add_orderdep(fortran_order_deps)
if deps:
# dependencies need to cause a relink, they're not just for ordering
element.add_dep(deps)
Expand All @@ -2203,7 +2205,7 @@ def generate_rust_target(self, target: build.BuildTarget) -> None:
rustdoc = rustc.get_rustdoc(self.environment)
args = rustdoc.get_exe_args()
args += self.get_rust_compiler_args(target.doctests.target, rustdoc, target.rust_crate_type)
_, _, deps_args = self.get_rust_compiler_deps_and_args(target.doctests.target, rustdoc)
_, _, _, deps_args = self.get_rust_compiler_deps_and_args(target.doctests.target, rustdoc)
args += deps_args
target.doctests.cmd_args = args.to_native() + [main_rust_file] + target.doctests.cmd_args

Expand Down Expand Up @@ -2518,6 +2520,16 @@ def use_dyndeps_for_fortran(self) -> bool:
minimum version is bumped to 1.10.'''
return self.ninja_has_dyndeps

def get_fortran_order_deps(self, deps: T.List[build.BuildTarget]) -> T.List[File]:
# We don't need this order dep if we're using dyndeps, as the
# depscanner will handle this for us, which produces a better dependency
# graph
if self.use_dyndeps_for_fortran():
return []

return [File(True, *os.path.split(self.get_target_filename(t))) for t in deps
if t.uses_fortran()]

def generate_fortran_dep_hack(self, crstr: str) -> None:
if self.use_dyndeps_for_fortran():
return
Expand Down
2 changes: 2 additions & 0 deletions mesonbuild/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3458,6 +3458,8 @@ def build_target(self, node: mparser.BaseNode, args: T.Tuple[str, SourcesVarargs

target = targetclass(name, self.subdir, self.subproject, for_machine, srcs, struct, objs,
self.environment, self.compilers[for_machine], kwargs)
if objs and target.uses_rust():
FeatureNew.single_use('objects in Rust targets', '1.8.0', self.subproject)

self.add_target(name, target)
self.project_args_frozen = True
Expand Down
15 changes: 15 additions & 0 deletions test cases/rust/27 objects/lib1-dylib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extern "C" {
fn from_lib1();
}

#[no_mangle]
extern "C" fn from_lib2()
{
println!("hello world from rust");
}

#[no_mangle]
pub extern "C" fn c_func()
{
unsafe { from_lib1(); }
}
11 changes: 11 additions & 0 deletions test cases/rust/27 objects/lib1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdio.h>
#include "lib1.h"
#include "lib2.h"

void from_lib2(void) {
printf("hello world from c\n");
}

void c_func(void) {
from_lib1();
}
4 changes: 4 additions & 0 deletions test cases/rust/27 objects/lib1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

void from_lib2(void);
void c_func(void);
8 changes: 8 additions & 0 deletions test cases/rust/27 objects/lib2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>
#include "lib1.h"
#include "lib2.h"

void from_lib1(void)
{
from_lib2();
}
3 changes: 3 additions & 0 deletions test cases/rust/27 objects/lib2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void from_lib1(void);
9 changes: 9 additions & 0 deletions test cases/rust/27 objects/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extern "C" {
fn c_func();
}

fn main() {
unsafe {
c_func();
}
}
28 changes: 28 additions & 0 deletions test cases/rust/27 objects/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
project('staticlib group', 'c', 'rust', meson_version: '>=1.8.0')

lib1 = static_library('lib1', 'lib1.c')
dep1 = declare_dependency(objects: lib1.extract_all_objects(recursive: false))
lib2 = static_library('lib2', 'lib2.c')
dep2 = declare_dependency(objects: lib2.extract_all_objects(recursive: false))
executable('lib1objs', 'main.rs',
objects: lib1.extract_all_objects(recursive: false),
link_with: lib2)
executable('lib2objs', 'main.rs',
objects: lib2.extract_all_objects(recursive: false),
link_with: lib1)
executable('lib1objs_as_dep', 'main.rs',
dependencies: dep1,
link_with: lib2)
executable('lib2objs_as_dep', 'main.rs',
dependencies: dep2,
link_with: lib1)

lib12 = shared_library('dylib2objs', 'lib1-dylib.rs',
objects: lib2.extract_all_objects(recursive: false),
rust_abi: 'c')
executable('dylib', 'main.rs', link_with: lib12)

lib12 = shared_library('dylib2objs_as_dep', 'lib1-dylib.rs',
dependencies: dep2,
rust_abi: 'c')
executable('dylib_as_dep', 'main.rs', link_with: lib12)
Loading