@@ -24,13 +24,18 @@ pub fn docker_command(subcommand: &str) -> Result<Command> {
24
24
if let Ok ( ce) = get_container_engine ( ) {
25
25
let mut command = Command :: new ( ce) ;
26
26
command. arg ( subcommand) ;
27
- command. args ( & [ "--userns" , "host" ] ) ;
28
27
Ok ( command)
29
28
} else {
30
29
Err ( "no container engine found; install docker or podman" . into ( ) )
31
30
}
32
31
}
33
32
33
+ pub fn docker_run_command ( ) -> Result < Command > {
34
+ let mut command = docker_command ( "run" ) ?;
35
+ command. args ( & [ "--userns" , "host" ] ) ;
36
+ Ok ( command)
37
+ }
38
+
34
39
/// Register binfmt interpreters
35
40
pub fn register ( target : & Target , verbose : bool ) -> Result < ( ) > {
36
41
let cmd = if target. is_windows ( ) {
@@ -42,14 +47,22 @@ pub fn register(target: &Target, verbose: bool) -> Result<()> {
42
47
binfmt-support qemu-user-static"
43
48
} ;
44
49
45
- docker_command ( "run" ) ?
50
+ docker_run_command ( ) ?
46
51
. arg ( "--privileged" )
47
52
. arg ( "--rm" )
48
53
. arg ( "ubuntu:16.04" )
49
54
. args ( & [ "sh" , "-c" , cmd] )
50
55
. run ( verbose)
51
56
}
52
57
58
+ fn copy_files ( dir : & PathBuf , target : & str , verbose : bool ) -> Result < ( ) > {
59
+ docker_command ( "cp" ) ?
60
+ . args ( & [ format ! ( "{}" , dir. display( ) ) , format ! ( "helper:{}" , target) ] )
61
+ . run_and_get_status ( verbose) ?;
62
+
63
+ Ok ( ( ) )
64
+ }
65
+
53
66
pub fn run ( target : & Target ,
54
67
args : & [ String ] ,
55
68
target_dir : & Option < PathBuf > ,
@@ -87,6 +100,22 @@ pub fn run(target: &Target,
87
100
let mount_root = mount_finder. find_mount_path ( & root) ;
88
101
let sysroot = mount_finder. find_mount_path ( & sysroot) ;
89
102
103
+ docker_command ( "volume" ) ?
104
+ . args ( & [ "create" , "data-volume" ] )
105
+ . run_and_get_status ( verbose) ?;
106
+
107
+ docker_command ( "create" ) ?
108
+ . args ( & [ "-v" , "data-volume:/cross" ] )
109
+ . args ( & [ "--name" , "helper" , "busybox" , "sh" , "-c" , & format ! ( "mkdir /cross/target && chown {user}:{grp} /cross/target && chown -R {user}:{grp} /cross/*" , user = id:: user( ) , grp = id:: group( ) ) ] )
110
+ . run_and_get_status ( verbose) ?;
111
+
112
+ copy_files ( & cargo_dir, "/cross" , verbose) ?;
113
+ copy_files ( & xargo_dir, "/cross" , verbose) ?;
114
+ copy_files ( & sysroot, "/cross" , verbose) ?;
115
+ copy_files ( & mount_root, "/cross" , verbose) ?;
116
+
117
+ docker_command ( "start" ) ?. arg ( "helper" ) . run_and_get_status ( verbose) ?;
118
+
90
119
let mut cmd = if uses_xargo {
91
120
SafeCommand :: new ( "xargo" )
92
121
} else {
@@ -97,7 +126,7 @@ pub fn run(target: &Target,
97
126
98
127
let runner = None ;
99
128
100
- let mut docker = docker_command ( "run" ) ?;
129
+ let mut docker = docker_run_command ( ) ?;
101
130
102
131
if let Some ( toml) = toml {
103
132
let validate_env_var = |var : & str | -> Result < ( ) > {
@@ -146,9 +175,9 @@ pub fn run(target: &Target,
146
175
}
147
176
148
177
docker
149
- . args ( & [ "-e" , "XARGO_HOME=/xargo" ] )
150
- . args ( & [ "-e" , "CARGO_HOME=/cargo" ] )
151
- . args ( & [ "-e" , "CARGO_TARGET_DIR=/target" ] )
178
+ . args ( & [ "-e" , & format ! ( "XARGO_HOME=/cross/{}" , xargo_dir . file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ) ] )
179
+ . args ( & [ "-e" , & format ! ( "CARGO_HOME=/cross/{}" , cargo_dir . file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ) ] )
180
+ . args ( & [ "-e" , "CARGO_TARGET_DIR=/cross/ target" ] )
152
181
. args ( & [ "-e" , & format ! ( "USER={}" , id:: username( ) . unwrap( ) . unwrap( ) ) ] ) ;
153
182
154
183
if let Ok ( value) = env:: var ( "QEMU_STRACE" ) {
@@ -166,14 +195,8 @@ pub fn run(target: &Target,
166
195
167
196
docker
168
197
. args ( & [ "-e" , & format ! ( "CROSS_RUNNER={}" , runner. unwrap_or_else( String :: new) ) ] )
169
- . args ( & [ "-v" , & format ! ( "{}:/xargo:Z" , xargo_dir. display( ) ) ] )
170
- . args ( & [ "-v" , & format ! ( "{}:/cargo:Z" , cargo_dir. display( ) ) ] )
171
- // Prevent `bin` from being mounted inside the Docker container.
172
- . args ( & [ "-v" , "/cargo/bin" ] )
173
- . args ( & [ "-v" , & format ! ( "{}:/{}:Z" , mount_root. display( ) , mount_root. display( ) ) ] )
174
- . args ( & [ "-v" , & format ! ( "{}:/rust:Z,ro" , sysroot. display( ) ) ] )
175
- . args ( & [ "-v" , & format ! ( "{}:/target:Z" , target_dir. display( ) ) ] )
176
- . args ( & [ "-w" , & mount_root. display ( ) . to_string ( ) ] ) ;
198
+ . args ( & [ "-v" , "data-volume:/cross" ] )
199
+ . args ( & [ "-w" , & format ! ( "/cross/{}" , mount_root. file_name( ) . unwrap( ) . to_str( ) . unwrap( ) ) ] ) ;
177
200
178
201
if atty:: is ( Stream :: Stdin ) {
179
202
docker. arg ( "-i" ) ;
@@ -184,7 +207,18 @@ pub fn run(target: &Target,
184
207
185
208
docker
186
209
. arg ( & image ( toml, target) ?)
187
- . args ( & [ "sh" , "-c" , & format ! ( "PATH=$PATH:/rust/bin {:?}" , cmd) ] )
210
+ . args ( & [ "sh" , "-c" , & format ! ( "PATH=$PATH:/cross/{}/bin {:?}" , sysroot. file_name( ) . unwrap( ) . to_str( ) . unwrap( ) , cmd) ] )
211
+ . run_and_get_status ( verbose) ?;
212
+
213
+ docker_command ( "cp" ) ?
214
+ . args ( & [ "helper:/cross/target" , & format ! ( "{}" , target_dir. parent( ) . unwrap( ) . display( ) ) ] )
215
+ . run_and_get_status ( verbose) ?;
216
+
217
+ docker_command ( "rm" ) ?
218
+ . arg ( "helper" )
219
+ . run_and_get_status ( verbose) ?;
220
+ docker_command ( "volume" ) ?
221
+ . args ( & [ "rm" , "data-volume" ] )
188
222
. run_and_get_status ( verbose)
189
223
}
190
224
0 commit comments