22
33use crate :: { check:: * , error:: * } ;
44use std:: { env, fs, path:: * , process:: Command , str:: FromStr } ;
5- use walkdir:: WalkDir ;
65
76/// Interface for 32-bit interger (LP64) and 64-bit integer (ILP64)
87#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
@@ -345,12 +344,6 @@ impl Default for Configure {
345344 }
346345}
347346
348- /// Deliverables of `make` command
349- pub struct Deliverables {
350- /// Inspection what `make` command really show.
351- pub make_conf : MakeConf ,
352- }
353-
354347impl Configure {
355348 fn make_args ( & self ) -> Vec < String > {
356349 let mut args = Vec :: new ( ) ;
@@ -396,37 +389,6 @@ impl Configure {
396389 args
397390 }
398391
399- /// Inspect existing build deliverables, and validate them.
400- ///
401- /// Error
402- /// ------
403- /// - No build deliverables exist
404- /// - Build deliverables are not valid
405- /// - e.g. `self.no_lapack == false`, but the existing library does not contains LAPACK symbols.
406- ///
407- pub fn inspect ( & self , out_dir : impl AsRef < Path > ) -> Result < Deliverables , Error > {
408- let out_dir = out_dir. as_ref ( ) ;
409- let make_conf = MakeConf :: new ( out_dir. join ( "Makefile.conf" ) ) ?;
410- if !self . no_static {
411- let lib_path = out_dir. join ( "libopenblas.a" ) ;
412- if !lib_path. exists ( ) {
413- return Err ( Error :: LibraryNotExist { path : lib_path } ) ;
414- }
415- }
416- if !self . no_shared {
417- let lib_path = if cfg ! ( target_os = "macos" ) {
418- out_dir. join ( "libopenblas.dylib" )
419- } else {
420- out_dir. join ( "libopenblas.so" )
421- } ;
422- if !lib_path. exists ( ) {
423- return Err ( Error :: LibraryNotExist { path : lib_path } ) ;
424- }
425- }
426-
427- Ok ( Deliverables { make_conf } )
428- }
429-
430392 /// Build OpenBLAS
431393 ///
432394 /// Libraries are created directly under `out_dir` e.g. `out_dir/libopenblas.a`
@@ -441,38 +403,12 @@ impl Configure {
441403 self ,
442404 openblas_root : impl AsRef < Path > ,
443405 out_dir : impl AsRef < Path > ,
444- ) -> Result < Deliverables , Error > {
406+ ) -> Result < MakeConf , Error > {
407+ let root = openblas_root. as_ref ( ) ;
445408 let out_dir = out_dir. as_ref ( ) ;
446- if !out_dir. exists ( ) {
447- fs:: create_dir_all ( out_dir) ?;
448- }
449-
450409 // Do not build if libraries and Makefile.conf already exist and are valid
451- if let Ok ( deliv) = self . inspect ( out_dir) {
452- return Ok ( deliv) ;
453- }
454-
455- // Copy OpenBLAS sources from this crate to `out_dir`
456- let root = openblas_root. as_ref ( ) ;
457- for entry in WalkDir :: new ( root) {
458- let entry = entry. expect ( "Unknown IO error while walkdir" ) ;
459- let dest = out_dir. join (
460- entry
461- . path ( )
462- . strip_prefix ( root)
463- . expect ( "Directory entry is not under root" ) ,
464- ) ;
465- if dest. exists ( ) {
466- // Do not overwrite
467- // Cache of previous build should be cleaned by `cargo clean`
468- continue ;
469- }
470- if entry. file_type ( ) . is_dir ( ) {
471- fs:: create_dir ( & dest) ?;
472- }
473- if entry. file_type ( ) . is_file ( ) {
474- fs:: copy ( entry. path ( ) , & dest) ?;
475- }
410+ if let Ok ( make_conf) = MakeConf :: new ( out_dir. join ( "Makefile.conf" ) ) {
411+ return Ok ( make_conf) ;
476412 }
477413
478414 // check if cross compile is needed
@@ -494,7 +430,7 @@ impl Configure {
494430 let out = fs:: File :: create ( out_dir. join ( "out.log" ) ) . expect ( "Cannot create log file" ) ;
495431 let err = fs:: File :: create ( out_dir. join ( "err.log" ) ) . expect ( "Cannot create log file" ) ;
496432 match Command :: new ( "make" )
497- . current_dir ( out_dir )
433+ . current_dir ( root )
498434 . stdout ( out)
499435 . stderr ( err)
500436 . args ( self . make_args ( ) )
@@ -514,7 +450,32 @@ impl Configure {
514450 return Err ( e) ;
515451 }
516452 }
517- self . inspect ( out_dir)
453+
454+ // Copy OpenBLAS and Makefile.conf from this crate to `out_dir`
455+ let mut file_names = Vec :: new ( ) ;
456+ if !self . no_static {
457+ file_names. push ( "libopenblas.a" ) ;
458+ }
459+ if !self . no_shared {
460+ if cfg ! ( target_os = "macos" ) {
461+ file_names. push ( "libopenblas.dylib" ) ;
462+ } else {
463+ file_names. push ( "libopenblas.so" ) ;
464+ }
465+ }
466+ file_names. push ( "Makefile.conf" ) ;
467+ for file_name in file_names {
468+ let src = root. join ( file_name) ;
469+ let dest = out_dir. join ( file_name) ;
470+ if dest. exists ( ) {
471+ // Do not overwrite
472+ // Cache of previous build should be cleaned by `cargo clean`
473+ continue ;
474+ }
475+ fs:: copy ( src, dest) ?;
476+ }
477+
478+ MakeConf :: new ( out_dir. join ( "Makefile.conf" ) )
518479 }
519480}
520481
@@ -540,53 +501,73 @@ mod tests {
540501 #[ test]
541502 fn build_default ( ) {
542503 let root = PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" ) ) ;
504+ let out_dir = root. join ( "test_build/build_default" ) ;
543505 let opt = Configure :: default ( ) ;
544- let _detail = opt
545- . build ( get_openblas_source ( ) , root. join ( "test_build/build_default" ) )
546- . unwrap ( ) ;
506+ let _ = opt. build ( get_openblas_source ( ) , & out_dir) . unwrap ( ) ;
547507 }
548508
549509 #[ ignore]
550510 #[ test]
551511 fn build_no_shared ( ) {
552512 let root = PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" ) ) ;
513+ let out_dir = root. join ( "test_build/build_no_shared" ) ;
553514 let mut opt = Configure :: default ( ) ;
554515 opt. no_shared = true ;
555- let detail = opt
556- . build (
557- get_openblas_source ( ) ,
558- root. join ( "test_build/build_no_shared" ) ,
559- )
560- . unwrap ( ) ;
561- assert ! ( detail. shared_lib. is_none( ) ) ;
516+ opt. build ( get_openblas_source ( ) , & out_dir) . unwrap ( ) ;
517+ let _ = LibInspect :: new ( out_dir. join ( "libopenblas.a" ) ) . unwrap ( ) ;
562518 }
563519
564520 #[ ignore]
565521 #[ test]
566522 fn build_no_lapacke ( ) {
567523 let root = PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" ) ) ;
524+ let out_dir = root. join ( "test_build/build_no_lapacke" ) ;
568525 let mut opt = Configure :: default ( ) ;
569526 opt. no_lapacke = true ;
570- let detail = opt
571- . build (
572- get_openblas_source ( ) ,
573- root. join ( "test_build/build_no_lapacke" ) ,
574- )
575- . unwrap ( ) ;
576- let shared_lib = detail. shared_lib . unwrap ( ) ;
577- assert ! ( shared_lib. has_lapack( ) ) ;
578- assert ! ( !shared_lib. has_lapacke( ) ) ;
527+ let _ = opt. build ( get_openblas_source ( ) , & out_dir) . unwrap ( ) ;
528+ let lib_name = if cfg ! ( target_os = "macos" ) {
529+ "libopenblas.dylib"
530+ } else {
531+ "libopenblas.so"
532+ } ;
533+ let lib_inspect = LibInspect :: new ( out_dir. join ( lib_name) ) . unwrap ( ) ;
534+
535+ assert ! ( lib_inspect. has_lapack( ) ) ;
536+ assert ! ( !lib_inspect. has_lapacke( ) ) ;
537+ }
538+
539+ #[ ignore]
540+ #[ test]
541+ fn build_no_cblas ( ) {
542+ let root = PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" ) ) ;
543+ let out_dir = root. join ( "test_build/build_no_cblas" ) ;
544+ let mut opt = Configure :: default ( ) ;
545+ opt. no_lapacke = true ;
546+ let _ = opt. build ( get_openblas_source ( ) , & out_dir) . unwrap ( ) ;
547+ let lib_name = if cfg ! ( target_os = "macos" ) {
548+ "libopenblas.dylib"
549+ } else {
550+ "libopenblas.so"
551+ } ;
552+ let lib_inspect = LibInspect :: new ( out_dir. join ( lib_name) ) . unwrap ( ) ;
553+
554+ assert ! ( !lib_inspect. has_cblas( ) ) ;
579555 }
580556
581557 #[ ignore]
582558 #[ test]
583559 fn build_openmp ( ) {
584560 let root = PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" ) ) ;
561+ let out_dir = root. join ( "test_build/build_openmp" ) ;
585562 let mut opt = Configure :: default ( ) ;
586563 opt. use_openmp = true ;
587- let detail = opt
588- . build ( get_openblas_source ( ) , root. join ( "test_build/build_openmp" ) )
589- . unwrap ( ) ;
590- assert ! ( detail. shared_lib. unwrap( ) . has_lib( "gomp" ) ) ;
564+ let _ = opt. build ( get_openblas_source ( ) , & out_dir) . unwrap ( ) ;
565+ let lib_name = if cfg ! ( target_os = "macos" ) {
566+ "libopenblas.dylib"
567+ } else {
568+ "libopenblas.so"
569+ } ;
570+ let lib_inspect = LibInspect :: new ( out_dir. join ( lib_name) ) . unwrap ( ) ;
571+ assert ! ( lib_inspect. has_lib( "gomp" ) ) ;
591572 }
592573}
0 commit comments