12
12
13
13
use util:: PkgId ;
14
14
use core:: libc:: consts:: os:: posix88:: { S_IRUSR , S_IWUSR , S_IXUSR } ;
15
+ use core:: os:: mkdir_recursive;
15
16
16
17
#[ deriving( Eq ) ]
17
18
pub enum OutputType { Main , Lib , Bench , Test }
@@ -23,7 +24,7 @@ pub fn rust_path() -> ~[Path] {
23
24
~[ Path ( "." ) ]
24
25
}
25
26
26
- static u_rwx: i32 = ( S_IRUSR | S_IWUSR | S_IXUSR ) as i32 ;
27
+ pub static u_rwx: i32 = ( S_IRUSR | S_IWUSR | S_IXUSR ) as i32 ;
27
28
28
29
/// Creates a directory that is readable, writeable,
29
30
/// and executable by the user. Returns true iff creation
@@ -70,34 +71,137 @@ pub fn pkgid_src_in_workspace(pkgid: PkgId, workspace: &Path) -> Path {
70
71
result. push ( pkgid. path . to_str ( ) )
71
72
}
72
73
74
+ /// Figure out what the executable name for <pkgid> in <workspace>'s build
75
+ /// directory is, and if the file exists, return it.
76
+ pub fn built_executable_in_workspace ( pkgid : PkgId , workspace : & Path ) -> Option < Path > {
77
+ let mut result = workspace. push ( "build" ) ;
78
+ result = result. push_rel ( & pkgid. path ) ;
79
+ // should use a target-specific subdirectory
80
+ result = mk_output_path ( Main , fmt ! ( "%s-%s" , pkgid. path. to_str( ) , pkgid. version. to_str( ) ) ,
81
+ result) ;
82
+ debug ! ( "built_executable_in_workspace: checking whether %s exists" ,
83
+ result. to_str( ) ) ;
84
+ if os:: path_exists ( & result) {
85
+ Some ( result)
86
+ }
87
+ else {
88
+ None
89
+ }
90
+ }
91
+
92
+ /// Figure out what the library name for <pkgid> in <workspace>'s build
93
+ /// directory is, and if the file exists, return it.
94
+ pub fn built_library_in_workspace ( pkgid : PkgId , workspace : & Path ) -> Option < Path > {
95
+ let mut result = workspace. push ( "build" ) ;
96
+ result = result. push_rel ( & pkgid. path ) ;
97
+ // should use a target-specific subdirectory
98
+ result = mk_output_path ( Lib , pkgid. path . to_str ( ) , result) ;
99
+ debug ! ( "built_library_in_workspace: checking whether %s exists" ,
100
+ result. to_str( ) ) ;
101
+
102
+ // We don't know what the hash is, so we have to search through the directory
103
+ // contents
104
+ let dir_contents = os:: list_dir ( & result. pop ( ) ) ;
105
+ debug ! ( "dir has %? entries" , dir_contents. len( ) ) ;
106
+
107
+ // n.b. This code assumes the pkgid's path only has one element
108
+ let lib_prefix = fmt ! ( "%s%s" , os:: consts:: DLL_PREFIX , pkgid. path. to_str( ) ) ;
109
+ let lib_filetype = fmt ! ( "%s%s" , pkgid. version. to_str( ) , os:: consts:: DLL_SUFFIX ) ;
110
+
111
+ debug ! ( "lib_prefix = %s and lib_filetype = %s" , lib_prefix, lib_filetype) ;
112
+
113
+ let mut result_filename = None ;
114
+ for dir_contents. each |& p| {
115
+ let mut which = 0 ;
116
+ let mut hash = None ;
117
+ // Find a filename that matches the pattern: (lib_prefix)-hash-(version)(lib_suffix)
118
+ // and remember what the hash was
119
+ for p. each_split_char( '-' ) |piece| {
120
+ debug ! ( "a piece = %s" , piece) ;
121
+ if which == 0 && piece != lib_prefix {
122
+ break ;
123
+ }
124
+ else if which == 0 {
125
+ which += 1 ;
126
+ }
127
+ else if which == 1 {
128
+ hash = Some ( piece. to_owned ( ) ) ;
129
+ which += 1 ;
130
+ }
131
+ else if which == 2 && piece != lib_filetype {
132
+ hash = None ;
133
+ break ;
134
+ }
135
+ else if which == 2 {
136
+ break ;
137
+ }
138
+ else {
139
+ // something went wrong
140
+ hash = None ;
141
+ break ;
142
+ }
143
+ }
144
+ if hash. is_some ( ) {
145
+ result_filename = Some ( p) ;
146
+ break ;
147
+ }
148
+ }
149
+
150
+ // Return the filename that matches, which we now know exists
151
+ // (if result_filename != None)
152
+ debug ! ( "result_filename = %?" , result_filename) ;
153
+ match result_filename {
154
+ None => None ,
155
+ Some ( result_filename) => {
156
+ let result_filename = result. with_filename ( result_filename) ;
157
+ debug ! ( "result_filename = %s" , result_filename. to_str( ) ) ;
158
+ Some ( result_filename)
159
+ }
160
+ }
161
+ }
162
+
73
163
/// Returns the executable that would be installed for <pkgid>
74
164
/// in <workspace>
165
+ /// As a side effect, creates the bin-dir if it doesn't exist
75
166
pub fn target_executable_in_workspace ( pkgid: PkgId , workspace: & Path ) -> Path {
76
- let result = workspace. push ( "bin" ) ;
77
- // should use a target-specific subdirectory
78
- mk_output_path ( Main , pkgid. path . to_str ( ) , result)
167
+ target_file_in_workspace( pkgid, workspace, Main )
79
168
}
80
169
81
170
82
171
/// Returns the executable that would be installed for <pkgid>
83
172
/// in <workspace>
173
+ /// As a side effect, creates the bin-dir if it doesn't exist
84
174
pub fn target_library_in_workspace ( pkgid: PkgId , workspace: & Path ) -> Path {
85
- let result = workspace. push ( "lib" ) ;
86
- mk_output_path ( Lib , pkgid. path . to_str ( ) , result)
175
+ target_file_in_workspace( pkgid, workspace, Lib )
87
176
}
88
177
89
178
/// Returns the test executable that would be installed for <pkgid>
90
179
/// in <workspace>
91
180
pub fn target_test_in_workspace ( pkgid: PkgId , workspace: & Path ) -> Path {
92
- let result = workspace. push ( "build" ) ;
93
- mk_output_path ( Test , pkgid. path . to_str ( ) , result)
181
+ target_file_in_workspace( pkgid, workspace, Test )
94
182
}
95
183
96
184
/// Returns the bench executable that would be installed for <pkgid>
97
185
/// in <workspace>
98
186
pub fn target_bench_in_workspace ( pkgid: PkgId , workspace: & Path ) -> Path {
99
- let result = workspace. push ( "build" ) ;
100
- mk_output_path ( Bench , pkgid. path . to_str ( ) , result)
187
+ target_file_in_workspace( pkgid, workspace, Bench )
188
+ }
189
+
190
+ fn target_file_in_workspace ( pkgid : PkgId , workspace : & Path ,
191
+ what : OutputType ) -> Path {
192
+ use conditions:: bad_path:: cond;
193
+
194
+ let ( subdir, create_dir) = match what {
195
+ Main => ( "bin" , true ) , Lib => ( "lib" , true ) , Test | Bench => ( "build" , false )
196
+ } ;
197
+ let result = workspace. push ( subdir) ;
198
+ if create_dir {
199
+ if !os:: path_exists ( & result) && !mkdir_recursive ( & result, u_rwx) {
200
+ cond. raise ( ( result, fmt ! ( "I couldn't create the %s dir" , subdir) ) ) ;
201
+ }
202
+ }
203
+ mk_output_path ( what, pkgid. path . to_str ( ) , result)
204
+
101
205
}
102
206
103
207
/// Return the directory for <pkgid>'s build artifacts in <workspace>.
@@ -123,7 +227,11 @@ pub fn mk_output_path(what: OutputType, short_name: ~str, dir: Path) -> Path {
123
227
match what {
124
228
Lib => dir. push ( os:: dll_filename ( short_name) ) ,
125
229
_ => dir. push ( fmt ! ( "%s%s%s" , short_name,
126
- if what == Test { ~"test" } else { ~" " } ,
230
+ match what {
231
+ Test => "test" ,
232
+ Bench => "bench" ,
233
+ _ => ""
234
+ }
127
235
os:: EXE_SUFFIX ) )
128
236
}
129
237
}
0 commit comments