@@ -14,24 +14,26 @@ pub use self::Version::*;
14
14
pub struct Build {
15
15
out_dir : Option < PathBuf > ,
16
16
target : Option < String > ,
17
- host : Option < String > ,
18
17
}
19
18
20
19
pub struct Artifacts {
21
- include_dir : PathBuf ,
22
20
lib_dir : PathBuf ,
23
21
libs : Vec < String > ,
24
22
}
25
23
26
- impl Build {
27
- #[ allow( clippy:: new_without_default) ]
28
- pub fn new ( ) -> Build {
24
+ impl Default for Build {
25
+ fn default ( ) -> Build {
29
26
Build {
30
- out_dir : env:: var_os ( "OUT_DIR" ) . map ( |s| PathBuf :: from ( s ) . join ( "lua-build" ) ) ,
27
+ out_dir : env:: var_os ( "OUT_DIR" ) . map ( PathBuf :: from) ,
31
28
target : env:: var ( "TARGET" ) . ok ( ) ,
32
- host : env:: var ( "HOST" ) . ok ( ) ,
33
29
}
34
30
}
31
+ }
32
+
33
+ impl Build {
34
+ pub fn new ( ) -> Build {
35
+ Build :: default ( )
36
+ }
35
37
36
38
pub fn out_dir < P : AsRef < Path > > ( & mut self , path : P ) -> & mut Build {
37
39
self . out_dir = Some ( path. as_ref ( ) . to_path_buf ( ) ) ;
@@ -43,43 +45,15 @@ impl Build {
43
45
self
44
46
}
45
47
46
- pub fn host ( & mut self , host : & str ) -> & mut Build {
47
- self . host = Some ( host. to_string ( ) ) ;
48
- self
49
- }
50
-
51
48
pub fn build ( & mut self , version : Version ) -> Artifacts {
52
- let target = & self . target . as_ref ( ) . expect ( "TARGET not set" ) [ ..] ;
53
- let host = & self . host . as_ref ( ) . expect ( "HOST not set" ) [ ..] ;
54
- let out_dir = self . out_dir . as_ref ( ) . expect ( "OUT_DIR not set" ) ;
55
- let lib_dir = out_dir. join ( "lib" ) ;
56
- let include_dir = out_dir. join ( "include" ) ;
57
-
58
- let source_dir_base = Path :: new ( env ! ( "CARGO_MANIFEST_DIR" ) ) ;
59
- let mut source_dir = match version {
60
- Lua51 => source_dir_base. join ( "lua-5.1.5" ) ,
61
- Lua52 => source_dir_base. join ( "lua-5.2.4" ) ,
62
- Lua53 => source_dir_base. join ( "lua-5.3.6" ) ,
63
- Lua54 => source_dir_base. join ( "lua-5.4.7" ) ,
64
- } ;
65
-
66
- if lib_dir. exists ( ) {
67
- fs:: remove_dir_all ( & lib_dir) . unwrap ( ) ;
68
- }
69
- fs:: create_dir_all ( & lib_dir) . unwrap ( ) ;
70
-
71
- if include_dir. exists ( ) {
72
- fs:: remove_dir_all ( & include_dir) . unwrap ( ) ;
73
- }
74
- fs:: create_dir_all ( & include_dir) . unwrap ( ) ;
49
+ let target = & self . target . as_ref ( ) . expect ( "TARGET is not set" ) [ ..] ;
50
+ let out_dir = self . out_dir . as_ref ( ) . expect ( "OUT_DIR is not set" ) ;
51
+ let manifest_dir = Path :: new ( env ! ( "CARGO_MANIFEST_DIR" ) ) ;
52
+ let mut source_dir = manifest_dir. join ( version. source_dir ( ) ) ;
53
+ let build_dir = out_dir. join ( "lua-build" ) ;
75
54
76
55
let mut config = cc:: Build :: new ( ) ;
77
- config
78
- . target ( target)
79
- . host ( host)
80
- . warnings ( false )
81
- . opt_level ( 2 )
82
- . cargo_metadata ( false ) ;
56
+ config. warnings ( false ) . cargo_metadata ( false ) ;
83
57
84
58
match target {
85
59
_ if target. contains ( "linux" ) => {
@@ -132,8 +106,8 @@ impl Build {
132
106
}
133
107
source_dir = cpp_source_dir
134
108
}
135
- _ => panic ! ( "don't know how to build Lua for {}" , target ) ,
136
- } ;
109
+ _ => panic ! ( "don't know how to build Lua for {target}" ) ,
110
+ }
137
111
138
112
if let Lua54 = version {
139
113
config. define ( "LUA_COMPAT_5_3" , None ) ;
@@ -145,93 +119,42 @@ impl Build {
145
119
config. define ( "LUA_USE_APICHECK" , None ) ;
146
120
}
147
121
148
- let lib_name = match version {
149
- Lua51 => "lua5.1" ,
150
- Lua52 => "lua5.2" ,
151
- Lua53 => "lua5.3" ,
152
- Lua54 => "lua5.4" ,
153
- } ;
154
-
155
122
config
156
123
. include ( & source_dir)
157
124
. flag ( "-w" ) // Suppress all warnings
158
125
. flag_if_supported ( "-fno-common" ) // Compile common globals like normal definitions
159
- . file ( source_dir. join ( "lapi.c" ) )
160
- . file ( source_dir. join ( "lauxlib.c" ) )
161
- . file ( source_dir. join ( "lbaselib.c" ) )
162
- // skipped: lbitlib.c (>= 5.2, <= 5.3)
163
- . file ( source_dir. join ( "lcode.c" ) )
164
- // skipped: lcorolib.c (>= 5.2)
165
- // skipped: lctype.c (>= 5.2)
166
- . file ( source_dir. join ( "ldblib.c" ) )
167
- . file ( source_dir. join ( "ldebug.c" ) )
168
- . file ( source_dir. join ( "ldo.c" ) )
169
- . file ( source_dir. join ( "ldump.c" ) )
170
- . file ( source_dir. join ( "lfunc.c" ) )
171
- . file ( source_dir. join ( "lgc.c" ) )
172
- . file ( source_dir. join ( "linit.c" ) )
173
- . file ( source_dir. join ( "liolib.c" ) )
174
- . file ( source_dir. join ( "llex.c" ) )
175
- . file ( source_dir. join ( "lmathlib.c" ) )
176
- . file ( source_dir. join ( "lmem.c" ) )
177
- . file ( source_dir. join ( "loadlib.c" ) )
178
- . file ( source_dir. join ( "lobject.c" ) )
179
- . file ( source_dir. join ( "lopcodes.c" ) )
180
- . file ( source_dir. join ( "loslib.c" ) )
181
- . file ( source_dir. join ( "lparser.c" ) )
182
- . file ( source_dir. join ( "lstate.c" ) )
183
- . file ( source_dir. join ( "lstring.c" ) )
184
- . file ( source_dir. join ( "lstrlib.c" ) )
185
- . file ( source_dir. join ( "ltable.c" ) )
186
- . file ( source_dir. join ( "ltablib.c" ) )
187
- . file ( source_dir. join ( "ltm.c" ) )
188
- . file ( source_dir. join ( "lundump.c" ) )
189
- // skipped: lutf8lib.c (>= 5.3)
190
- . file ( source_dir. join ( "lvm.c" ) )
191
- . file ( source_dir. join ( "lzio.c" ) ) ;
192
-
193
- match version {
194
- Lua51 => { }
195
- Lua52 => {
196
- config
197
- . file ( source_dir. join ( "lbitlib.c" ) )
198
- . file ( source_dir. join ( "lcorolib.c" ) )
199
- . file ( source_dir. join ( "lctype.c" ) ) ;
200
- }
201
- Lua53 => {
202
- config
203
- . file ( source_dir. join ( "lbitlib.c" ) )
204
- . file ( source_dir. join ( "lcorolib.c" ) )
205
- . file ( source_dir. join ( "lctype.c" ) )
206
- . file ( source_dir. join ( "lutf8lib.c" ) ) ;
207
- }
208
- Lua54 => {
209
- config
210
- . file ( source_dir. join ( "lcorolib.c" ) )
211
- . file ( source_dir. join ( "lctype.c" ) )
212
- . file ( source_dir. join ( "lutf8lib.c" ) ) ;
213
- }
214
- }
126
+ . add_files_by_ext ( & source_dir, "c" )
127
+ . out_dir ( & build_dir)
128
+ . compile ( version. lib_name ( ) ) ;
215
129
216
- config. out_dir ( & lib_dir) . compile ( lib_name) ;
130
+ Artifacts {
131
+ lib_dir : build_dir,
132
+ libs : vec ! [ version. lib_name( ) . to_string( ) ] ,
133
+ }
134
+ }
135
+ }
217
136
218
- for f in & [ "lauxlib.h" , "lua.h" , "luaconf.h" , "lualib.h" ] {
219
- fs:: copy ( source_dir. join ( f) , include_dir. join ( f) ) . unwrap ( ) ;
137
+ impl Version {
138
+ fn source_dir ( & self ) -> & str {
139
+ match self {
140
+ Lua51 => "lua-5.1.5" ,
141
+ Lua52 => "lua-5.2.4" ,
142
+ Lua53 => "lua-5.3.6" ,
143
+ Lua54 => "lua-5.4.7" ,
220
144
}
145
+ }
221
146
222
- Artifacts {
223
- lib_dir,
224
- include_dir,
225
- libs : vec ! [ lib_name. to_string( ) ] ,
147
+ fn lib_name ( & self ) -> & str {
148
+ match self {
149
+ Lua51 => "lua5.1" ,
150
+ Lua52 => "lua5.2" ,
151
+ Lua53 => "lua5.3" ,
152
+ Lua54 => "lua5.4" ,
226
153
}
227
154
}
228
155
}
229
156
230
157
impl Artifacts {
231
- pub fn include_dir ( & self ) -> & Path {
232
- & self . include_dir
233
- }
234
-
235
158
pub fn lib_dir ( & self ) -> & Path {
236
159
& self . lib_dir
237
160
}
@@ -243,9 +166,24 @@ impl Artifacts {
243
166
pub fn print_cargo_metadata ( & self ) {
244
167
println ! ( "cargo:rustc-link-search=native={}" , self . lib_dir. display( ) ) ;
245
168
for lib in self . libs . iter ( ) {
246
- println ! ( "cargo:rustc-link-lib=static={}" , lib ) ;
169
+ println ! ( "cargo:rustc-link-lib=static={lib}" ) ;
247
170
}
248
- println ! ( "cargo:include={}" , self . include_dir. display( ) ) ;
249
- println ! ( "cargo:lib={}" , self . lib_dir. display( ) ) ;
171
+ }
172
+ }
173
+
174
+ trait AddFilesByExt {
175
+ fn add_files_by_ext ( & mut self , dir : & Path , ext : & str ) -> & mut Self ;
176
+ }
177
+
178
+ impl AddFilesByExt for cc:: Build {
179
+ fn add_files_by_ext ( & mut self , dir : & Path , ext : & str ) -> & mut Self {
180
+ for entry in fs:: read_dir ( dir)
181
+ . unwrap ( )
182
+ . filter_map ( |e| e. ok ( ) )
183
+ . filter ( |e| e. path ( ) . extension ( ) == Some ( ext. as_ref ( ) ) )
184
+ {
185
+ self . file ( entry. path ( ) ) ;
186
+ }
187
+ self
250
188
}
251
189
}
0 commit comments