Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
977f2795
R
Rust
项目概览
int
/
Rust
11 个月 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
977f2795
编写于
9月 01, 2021
作者:
B
bjorn3
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move add_rlib and add_native_library to cg_ssa
This deduplicates logic between codegen backends
上级
3ed6c1d2
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
84 addition
and
163 deletion
+84
-163
compiler/rustc_codegen_cranelift/src/archive.rs
compiler/rustc_codegen_cranelift/src/archive.rs
+19
-64
compiler/rustc_codegen_llvm/src/back/archive.rs
compiler/rustc_codegen_llvm/src/back/archive.rs
+18
-80
compiler/rustc_codegen_ssa/src/back/archive.rs
compiler/rustc_codegen_ssa/src/back/archive.rs
+4
-9
compiler/rustc_codegen_ssa/src/back/link.rs
compiler/rustc_codegen_ssa/src/back/link.rs
+43
-10
未找到文件。
compiler/rustc_codegen_cranelift/src/archive.rs
浏览文件 @
977f2795
...
...
@@ -4,8 +4,7 @@
use
std
::
fs
::
File
;
use
std
::
path
::{
Path
,
PathBuf
};
use
rustc_codegen_ssa
::
back
::
archive
::{
find_library
,
ArchiveBuilder
};
use
rustc_codegen_ssa
::
METADATA_FILENAME
;
use
rustc_codegen_ssa
::
back
::
archive
::
ArchiveBuilder
;
use
rustc_session
::
Session
;
use
object
::{
Object
,
ObjectSymbol
,
SymbolKind
};
...
...
@@ -19,7 +18,6 @@ enum ArchiveEntry {
pub
(
crate
)
struct
ArArchiveBuilder
<
'a
>
{
sess
:
&
'a
Session
,
dst
:
PathBuf
,
lib_search_paths
:
Vec
<
PathBuf
>
,
use_gnu_style_archive
:
bool
,
no_builtin_ranlib
:
bool
,
...
...
@@ -31,8 +29,6 @@ pub(crate) struct ArArchiveBuilder<'a> {
impl
<
'a
>
ArchiveBuilder
<
'a
>
for
ArArchiveBuilder
<
'a
>
{
fn
new
(
sess
:
&
'a
Session
,
output
:
&
Path
,
input
:
Option
<&
Path
>
)
->
Self
{
use
rustc_codegen_ssa
::
back
::
link
::
archive_search_paths
;
let
(
src_archives
,
entries
)
=
if
let
Some
(
input
)
=
input
{
let
mut
archive
=
ar
::
Archive
::
new
(
File
::
open
(
input
)
.unwrap
());
let
mut
entries
=
Vec
::
new
();
...
...
@@ -55,7 +51,6 @@ fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> Self {
ArArchiveBuilder
{
sess
,
dst
:
output
.to_path_buf
(),
lib_search_paths
:
archive_search_paths
(
sess
),
use_gnu_style_archive
:
sess
.target.archive_format
==
"gnu"
,
// FIXME fix builtin ranlib on macOS
no_builtin_ranlib
:
sess
.target.is_like_osx
,
...
...
@@ -85,42 +80,27 @@ fn add_file(&mut self, file: &Path) {
));
}
fn
add_native_library
(
&
mut
self
,
name
:
rustc_span
::
symbol
::
Symbol
,
verbatim
:
bool
)
{
let
location
=
find_library
(
name
,
verbatim
,
&
self
.lib_search_paths
,
self
.sess
);
self
.add_archive
(
location
.clone
(),
|
_
|
false
)
.unwrap_or_else
(|
e
|
{
panic!
(
"failed to add native library {}: {}"
,
location
.to_string_lossy
(),
e
);
});
}
fn
add_rlib
(
&
mut
self
,
rlib
:
&
Path
,
name
:
&
str
,
lto
:
bool
,
skip_objects
:
bool
,
)
->
std
::
io
::
Result
<
()
>
{
let
obj_start
=
name
.to_owned
();
self
.add_archive
(
rlib
.to_owned
(),
move
|
fname
:
&
str
|
{
// Ignore metadata files, no matter the name.
if
fname
==
METADATA_FILENAME
{
return
true
;
}
fn
add_archive
<
F
>
(
&
mut
self
,
archive_path
:
&
Path
,
mut
skip
:
F
)
->
std
::
io
::
Result
<
()
>
where
F
:
FnMut
(
&
str
)
->
bool
+
'static
,
{
let
mut
archive
=
ar
::
Archive
::
new
(
std
::
fs
::
File
::
open
(
&
archive_path
)
?
);
let
archive_index
=
self
.src_archives
.len
();
// Don't include Rust objects if LTO is enabled
if
lto
&&
fname
.starts_with
(
&
obj_start
)
&&
fname
.ends_with
(
".o"
)
{
return
true
;
let
mut
i
=
0
;
while
let
Some
(
entry
)
=
archive
.next_entry
()
{
let
entry
=
entry
?
;
let
file_name
=
String
::
from_utf8
(
entry
.header
()
.identifier
()
.to_vec
())
.map_err
(|
err
|
std
::
io
::
Error
::
new
(
std
::
io
::
ErrorKind
::
InvalidData
,
err
))
?
;
if
!
skip
(
&
file_name
)
{
self
.entries
.push
((
file_name
,
ArchiveEntry
::
FromArchive
{
archive_index
,
entry_index
:
i
}));
}
// Otherwise if this is *not* a rust object and we're skipping
// objects then skip this file
if
skip_objects
&&
(
!
fname
.starts_with
(
&
obj_start
)
||
!
fname
.ends_with
(
".o"
))
{
return
true
;
i
+=
1
;
}
// ok, don't skip this
false
})
self
.src_archives
.push
((
archive_path
.to_owned
(),
archive
));
Ok
(())
}
fn
update_symbols
(
&
mut
self
)
{}
...
...
@@ -264,28 +244,3 @@ fn inject_dll_import_lib(
bug!
(
"injecting dll imports is not supported"
);
}
}
impl
<
'a
>
ArArchiveBuilder
<
'a
>
{
fn
add_archive
<
F
>
(
&
mut
self
,
archive_path
:
PathBuf
,
mut
skip
:
F
)
->
std
::
io
::
Result
<
()
>
where
F
:
FnMut
(
&
str
)
->
bool
+
'static
,
{
let
mut
archive
=
ar
::
Archive
::
new
(
std
::
fs
::
File
::
open
(
&
archive_path
)
?
);
let
archive_index
=
self
.src_archives
.len
();
let
mut
i
=
0
;
while
let
Some
(
entry
)
=
archive
.next_entry
()
{
let
entry
=
entry
?
;
let
file_name
=
String
::
from_utf8
(
entry
.header
()
.identifier
()
.to_vec
())
.map_err
(|
err
|
std
::
io
::
Error
::
new
(
std
::
io
::
ErrorKind
::
InvalidData
,
err
))
?
;
if
!
skip
(
&
file_name
)
{
self
.entries
.push
((
file_name
,
ArchiveEntry
::
FromArchive
{
archive_index
,
entry_index
:
i
}));
}
i
+=
1
;
}
self
.src_archives
.push
((
archive_path
,
archive
));
Ok
(())
}
}
compiler/rustc_codegen_llvm/src/back/archive.rs
浏览文件 @
977f2795
...
...
@@ -9,18 +9,15 @@
use
crate
::
llvm
::
archive_ro
::{
ArchiveRO
,
Child
};
use
crate
::
llvm
::{
self
,
ArchiveKind
,
LLVMMachineType
,
LLVMRustCOFFShortExport
};
use
rustc_codegen_ssa
::
back
::
archive
::{
find_library
,
ArchiveBuilder
};
use
rustc_codegen_ssa
::{
looks_like_rust_object_file
,
METADATA_FILENAME
};
use
rustc_codegen_ssa
::
back
::
archive
::
ArchiveBuilder
;
use
rustc_data_structures
::
temp_dir
::
MaybeTempDir
;
use
rustc_middle
::
middle
::
cstore
::{
DllCallingConvention
,
DllImport
};
use
rustc_session
::
Session
;
use
rustc_span
::
symbol
::
Symbol
;
struct
ArchiveConfig
<
'a
>
{
pub
sess
:
&
'a
Session
,
pub
dst
:
PathBuf
,
pub
src
:
Option
<
PathBuf
>
,
pub
lib_search_paths
:
Vec
<
PathBuf
>
,
}
/// Helper for adding many files to an archive.
...
...
@@ -54,13 +51,7 @@ fn is_relevant_child(c: &Child<'_>) -> bool {
}
fn
archive_config
<
'a
>
(
sess
:
&
'a
Session
,
output
:
&
Path
,
input
:
Option
<&
Path
>
)
->
ArchiveConfig
<
'a
>
{
use
rustc_codegen_ssa
::
back
::
link
::
archive_search_paths
;
ArchiveConfig
{
sess
,
dst
:
output
.to_path_buf
(),
src
:
input
.map
(|
p
|
p
.to_path_buf
()),
lib_search_paths
:
archive_search_paths
(
sess
),
}
ArchiveConfig
{
sess
,
dst
:
output
.to_path_buf
(),
src
:
input
.map
(|
p
|
p
.to_path_buf
())
}
}
/// Map machine type strings to values of LLVM's MachineTypes enum.
...
...
@@ -111,57 +102,23 @@ fn src_files(&mut self) -> Vec<String> {
.collect
()
}
/// Adds all of the contents of a native library to this archive. This will
/// search in the relevant locations for a library named `name`.
fn
add_native_library
(
&
mut
self
,
name
:
Symbol
,
verbatim
:
bool
)
{
let
location
=
find_library
(
name
,
verbatim
,
&
self
.config.lib_search_paths
,
self
.config.sess
);
self
.add_archive
(
&
location
,
|
_
|
false
)
.unwrap_or_else
(|
e
|
{
self
.config.sess
.fatal
(
&
format!
(
"failed to add native library {}: {}"
,
location
.to_string_lossy
(),
e
));
});
}
/// Adds all of the contents of the rlib at the specified path to this
/// archive.
///
/// This ignores adding the bytecode from the rlib, and if LTO is enabled
/// then the object file also isn't added.
fn
add_rlib
(
&
mut
self
,
rlib
:
&
Path
,
name
:
&
str
,
lto
:
bool
,
skip_objects
:
bool
,
)
->
io
::
Result
<
()
>
{
// Ignoring obj file starting with the crate name
// as simple comparison is not enough - there
// might be also an extra name suffix
let
obj_start
=
name
.to_owned
();
self
.add_archive
(
rlib
,
move
|
fname
:
&
str
|
{
// Ignore metadata files, no matter the name.
if
fname
==
METADATA_FILENAME
{
return
true
;
}
// Don't include Rust objects if LTO is enabled
if
lto
&&
looks_like_rust_object_file
(
fname
)
{
return
true
;
}
// Otherwise if this is *not* a rust object and we're skipping
// objects then skip this file
if
skip_objects
&&
(
!
fname
.starts_with
(
&
obj_start
)
||
!
fname
.ends_with
(
".o"
))
{
return
true
;
fn
add_archive
<
F
>
(
&
mut
self
,
archive
:
&
Path
,
skip
:
F
)
->
io
::
Result
<
()
>
where
F
:
FnMut
(
&
str
)
->
bool
+
'static
,
{
let
archive_ro
=
match
ArchiveRO
::
open
(
archive
)
{
Ok
(
ar
)
=>
ar
,
Err
(
e
)
=>
return
Err
(
io
::
Error
::
new
(
io
::
ErrorKind
::
Other
,
e
)),
};
if
self
.additions
.iter
()
.any
(|
ar
|
ar
.path
()
==
archive
)
{
return
Ok
(());
}
// ok, don't skip this
false
})
self
.additions
.push
(
Addition
::
Archive
{
path
:
archive
.to_path_buf
(),
archive
:
archive_ro
,
skip
:
Box
::
new
(
skip
),
});
Ok
(())
}
/// Adds an arbitrary file to this archive
...
...
@@ -270,25 +227,6 @@ fn src_archive(&mut self) -> Option<&ArchiveRO> {
self
.src_archive
.as_ref
()
.unwrap
()
.as_ref
()
}
fn
add_archive
<
F
>
(
&
mut
self
,
archive
:
&
Path
,
skip
:
F
)
->
io
::
Result
<
()
>
where
F
:
FnMut
(
&
str
)
->
bool
+
'static
,
{
let
archive_ro
=
match
ArchiveRO
::
open
(
archive
)
{
Ok
(
ar
)
=>
ar
,
Err
(
e
)
=>
return
Err
(
io
::
Error
::
new
(
io
::
ErrorKind
::
Other
,
e
)),
};
if
self
.additions
.iter
()
.any
(|
ar
|
ar
.path
()
==
archive
)
{
return
Ok
(());
}
self
.additions
.push
(
Addition
::
Archive
{
path
:
archive
.to_path_buf
(),
archive
:
archive_ro
,
skip
:
Box
::
new
(
skip
),
});
Ok
(())
}
fn
llvm_archive_kind
(
&
self
)
->
Result
<
ArchiveKind
,
&
str
>
{
let
kind
=
&*
self
.config.sess.target.archive_format
;
kind
.parse
()
.map_err
(|
_
|
kind
)
...
...
compiler/rustc_codegen_ssa/src/back/archive.rs
浏览文件 @
977f2795
...
...
@@ -6,7 +6,7 @@
use
std
::
io
;
use
std
::
path
::{
Path
,
PathBuf
};
pub
fn
find_library
(
pub
(
super
)
fn
find_library
(
name
:
Symbol
,
verbatim
:
bool
,
search_paths
:
&
[
PathBuf
],
...
...
@@ -48,14 +48,9 @@ pub trait ArchiveBuilder<'a> {
fn
remove_file
(
&
mut
self
,
name
:
&
str
);
fn
src_files
(
&
mut
self
)
->
Vec
<
String
>
;
fn
add_rlib
(
&
mut
self
,
path
:
&
Path
,
name
:
&
str
,
lto
:
bool
,
skip_objects
:
bool
,
)
->
io
::
Result
<
()
>
;
fn
add_native_library
(
&
mut
self
,
name
:
Symbol
,
verbatim
:
bool
);
fn
add_archive
<
F
>
(
&
mut
self
,
archive
:
&
Path
,
skip
:
F
)
->
io
::
Result
<
()
>
where
F
:
FnMut
(
&
str
)
->
bool
+
'static
;
fn
update_symbols
(
&
mut
self
);
fn
build
(
self
);
...
...
compiler/rustc_codegen_ssa/src/back/link.rs
浏览文件 @
977f2795
...
...
@@ -19,7 +19,7 @@
use
rustc_target
::
spec
::{
LinkOutputKind
,
LinkerFlavor
,
LldFlavor
,
SplitDebuginfo
};
use
rustc_target
::
spec
::{
PanicStrategy
,
RelocModel
,
RelroLevel
,
SanitizerSet
,
Target
};
use
super
::
archive
::
ArchiveBuilder
;
use
super
::
archive
::
{
find_library
,
ArchiveBuilder
}
;
use
super
::
command
::
Command
;
use
super
::
linker
::{
self
,
Linker
};
use
super
::
rpath
::{
self
,
RPathConfig
};
...
...
@@ -230,6 +230,9 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(
tmpdir
:
&
MaybeTempDir
,
)
->
Result
<
B
,
ErrorReported
>
{
info!
(
"preparing rlib to {:?}"
,
out_filename
);
let
lib_search_paths
=
archive_search_paths
(
sess
);
let
mut
ab
=
<
B
as
ArchiveBuilder
>
::
new
(
sess
,
out_filename
,
None
);
for
obj
in
codegen_results
.modules
.iter
()
.filter_map
(|
m
|
m
.object
.as_ref
())
{
...
...
@@ -262,7 +265,15 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(
|
NativeLibKind
::
Unspecified
=>
continue
,
}
if
let
Some
(
name
)
=
lib
.name
{
ab
.add_native_library
(
name
,
lib
.verbatim
.unwrap_or
(
false
));
let
location
=
find_library
(
name
,
lib
.verbatim
.unwrap_or
(
false
),
&
lib_search_paths
,
sess
);
ab
.add_archive
(
&
location
,
|
_
|
false
)
.unwrap_or_else
(|
e
|
{
sess
.fatal
(
&
format!
(
"failed to add native library {}: {}"
,
location
.to_string_lossy
(),
e
));
});
}
}
...
...
@@ -541,13 +552,35 @@ fn link_staticlib<'a, B: ArchiveBuilder<'a>>(
matches!
(
lib
.kind
,
NativeLibKind
::
Static
{
bundle
:
None
|
Some
(
true
),
..
})
&&
!
relevant_lib
(
sess
,
lib
)
});
ab
.add_rlib
(
path
,
&
name
.as_str
(),
are_upstream_rust_objects_already_included
(
sess
)
&&
!
ignored_for_lto
(
sess
,
&
codegen_results
.crate_info
,
cnum
),
skip_object_files
,
)
let
lto
=
are_upstream_rust_objects_already_included
(
sess
)
&&
!
ignored_for_lto
(
sess
,
&
codegen_results
.crate_info
,
cnum
);
// Ignoring obj file starting with the crate name
// as simple comparison is not enough - there
// might be also an extra name suffix
let
obj_start
=
name
.as_str
()
.to_owned
();
ab
.add_archive
(
path
,
move
|
fname
:
&
str
|
{
// Ignore metadata files, no matter the name.
if
fname
==
METADATA_FILENAME
{
return
true
;
}
// Don't include Rust objects if LTO is enabled
if
lto
&&
looks_like_rust_object_file
(
fname
)
{
return
true
;
}
// Otherwise if this is *not* a rust object and we're skipping
// objects then skip this file
if
skip_object_files
&&
(
!
fname
.starts_with
(
&
obj_start
)
||
!
fname
.ends_with
(
".o"
))
{
return
true
;
}
// ok, don't skip this
false
})
.unwrap
();
all_native_libs
.extend
(
codegen_results
.crate_info.native_libraries
[
&
cnum
]
.iter
()
.cloned
());
...
...
@@ -1218,7 +1251,7 @@ fn preserve_objects_for_their_debuginfo(sess: &Session) -> bool {
sess
.split_debuginfo
()
==
SplitDebuginfo
::
Unpacked
}
pub
fn
archive_search_paths
(
sess
:
&
Session
)
->
Vec
<
PathBuf
>
{
fn
archive_search_paths
(
sess
:
&
Session
)
->
Vec
<
PathBuf
>
{
sess
.target_filesearch
(
PathKind
::
Native
)
.search_path_dirs
()
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录