Skip to content

Commit

Permalink
Exfiltrate more linker flags with get_rust_ldflags.py
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus committed Jul 19, 2018
1 parent 6b77c65 commit 81f254c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 27 deletions.
6 changes: 6 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ config("deno_config") {
if (is_debug) {
defines = [ "DEBUG" ]
}
if (is_mac) {
libs = [ "resolv" ]
}
if (is_win) {
libs = [ "userenv.lib" ]
}
}

rust_executable("deno") {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,51 @@ def main():
os.close(argsfile_fd)
os.unlink(argsfile_path)

# Write all linker inputs that are dependencies to stdout.
# Note that in addition to all .rlib files, we also capture a file named
# `crate_name.crate.allocator.rcgu.o`, which contains the implementation
# of `__rust_malloc()`, `__rust_free()` etc.
# From the list of captured linker arguments, build a list of ldflags that
# we actually need.
ldflags = []
next_arg_is_flag_value = False
for arg in args:
if arg.endswith(".rlib") or arg.endswith(".crate.allocator.rcgu.o"):
print(arg)
# Note that within the following if/elif blocks, `pass` means `arg`
# gets included in `ldflags`. The final `else` clause filters out
# unrecognized/unwanted flags.
if next_arg_is_flag_value:
# We're looking at a value that follows certain parametric flags,
# e.g. the path in '-L <path>'.
next_arg_is_flag_value = False
elif arg.endswith(".rlib"):
# Built-in Rust library, e.g. `libstd-8524caae8408aac2.rlib`.
pass
elif arg.endswith(".crate.allocator.rcgu.o"):
# This file is needed because it contains certain allocator
# related symbols (e.g. `__rust_alloc`, `__rust_oom`).
# The Rust compiler normally generates this file just before
# linking an executable. We pass `-Csave-temps` to rustc so it
# doesn't delete the file when it's done linking.
pass
elif arg.endswith(".lib") and not arg.startswith("msvcrt"):
# Include most Windows static/import libraries (e.g. `ws2_32.lib`).
# However we exclude Rusts choice of C runtime (mvcrt*.lib), since
# it makes poor choices.
pass
elif arg == "-l" or arg == "-L":
# `-l <name>`: Link with library (GCC style).
# `-L <path>`: Linker search path (GCC style).
next_arg_is_flag_value = True # Ensure flag argument is captured.
elif arg == "-Wl,--start-group" or arg == "-Wl,--end-group":
# Start or end of an archive group (GCC style).
pass
elif arg.upper().startswith("/LIBPATH:"):
# `/LIBPATH:<path>`: Linker search path (Microsoft style).
pass
else:
# No matches -- don't add this flag to ldflags.
continue

ldflags += [arg]

# Write the filtered ldflags to stdout, separated by newline characters.
sys.stdout.write("\n".join(ldflags))


if __name__ == '__main__':
Expand Down
31 changes: 10 additions & 21 deletions build_extra/rust/rust.gni
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
empty_rs_path = rebase_path("empty.rs", root_build_dir)
rust_default_libs_bin =
exec_script("get_rustc_default_libs.py", [ empty_rs_path ], "list lines")
rust_default_libs_test = exec_script("get_rustc_default_libs.py",
[
empty_rs_path,
"--test",
],
"list lines")

rust_required_system_libs = []
if (current_os == "mac") {
rust_required_system_libs += [ "resolv" ]
}
if (current_os == "win") {
rust_required_system_libs += [ "userenv.lib" ]
}
rust_bin_ldflags =
exec_script("get_rust_ldflags.py", [ empty_rs_path ], "list lines")
rust_test_ldflags = exec_script("get_rust_ldflags.py",
[
empty_rs_path,
"--test",
],
"list lines")

declare_args() {
# Absolute path of rust build files.
Expand Down Expand Up @@ -186,7 +178,6 @@ template("rust_staticlib") {
"testonly",
])
crate_type = "staticlib"
libs = rust_required_system_libs
}
}

Expand All @@ -203,13 +194,11 @@ template("rust_executable") {
forward_variables_from(invoker, "*")

if (defined(is_test) && is_test) {
ldflags = rust_default_libs_test
ldflags = rust_test_ldflags
} else {
ldflags = rust_default_libs_bin
ldflags = rust_bin_ldflags
}

libs = rust_required_system_libs

if (!defined(deps)) {
deps = []
}
Expand Down

0 comments on commit 81f254c

Please sign in to comment.