@@ -529,6 +529,7 @@ impl Step for Llvm {
529529 }
530530 } ;
531531
532+ // Manuel: TODO: Do we need that for Enzyme too?
532533 // When building LLVM with LLVM_LINK_LLVM_DYLIB for macOS, an unversioned
533534 // libLLVM.dylib will be built. However, llvm-config will still look
534535 // for a versioned path like libLLVM-14.dylib. Manually create a symbolic
@@ -849,6 +850,76 @@ fn get_var(var_base: &str, host: &str, target: &str) -> Option<OsString> {
849850 . or_else ( || env:: var_os ( var_base) )
850851}
851852
853+ #[ derive( Debug , Copy , Clone , Hash , PartialEq , Eq ) ]
854+ pub struct Enzyme {
855+ pub target : TargetSelection ,
856+ }
857+
858+ impl Step for Enzyme {
859+ type Output = PathBuf ;
860+ const ONLY_HOSTS : bool = true ;
861+
862+ fn should_run ( run : ShouldRun < ' _ > ) -> ShouldRun < ' _ > {
863+ run. path ( "src/tools/enzyme/enzyme" )
864+ }
865+
866+ fn make_run ( run : RunConfig < ' _ > ) {
867+ run. builder . ensure ( Enzyme { target : run. target } ) ;
868+ }
869+
870+ /// Compile Enzyme for `target`.
871+ fn run ( self , builder : & Builder < ' _ > ) -> PathBuf {
872+ builder. require_submodule (
873+ "src/tools/enzyme" ,
874+ Some ( "The Enzyme sources are required for autodiff." ) ,
875+ ) ;
876+ if builder. config . dry_run ( ) {
877+ let out_dir = builder. enzyme_out ( self . target ) ;
878+ return out_dir;
879+ }
880+ let target = self . target ;
881+
882+ let LlvmResult { llvm_config, .. } = builder. ensure ( Llvm { target : self . target } ) ;
883+
884+ let out_dir = builder. enzyme_out ( target) ;
885+ let done_stamp = out_dir. join ( "enzyme-finished-building" ) ;
886+ if done_stamp. exists ( ) {
887+ return out_dir;
888+ }
889+
890+ builder. info ( & format ! ( "Building Enzyme for {}" , target) ) ;
891+ let _time = helpers:: timeit ( & builder) ;
892+ t ! ( fs:: create_dir_all( & out_dir) ) ;
893+
894+ builder. update_submodule ( Path :: new ( "src" ) . join ( "tools" ) . join ( "enzyme" ) . to_str ( ) . unwrap ( ) ) ;
895+ let mut cfg = cmake:: Config :: new ( builder. src . join ( "src/tools/enzyme/enzyme/" ) ) ;
896+ // TODO: Find a nicer way to use Enzyme Debug builds
897+ //cfg.profile("Debug");
898+ //cfg.define("CMAKE_BUILD_TYPE", "Debug");
899+ configure_cmake ( builder, target, & mut cfg, true , LdFlags :: default ( ) , & [ ] ) ;
900+
901+ // Re-use the same flags as llvm to control the level of debug information
902+ // generated for lld.
903+ let profile = match ( builder. config . llvm_optimize , builder. config . llvm_release_debuginfo ) {
904+ ( false , _) => "Debug" ,
905+ ( true , false ) => "Release" ,
906+ ( true , true ) => "RelWithDebInfo" ,
907+ } ;
908+
909+ cfg. out_dir ( & out_dir)
910+ . profile ( profile)
911+ . env ( "LLVM_CONFIG_REAL" , & llvm_config)
912+ . define ( "LLVM_ENABLE_ASSERTIONS" , "ON" )
913+ . define ( "ENZYME_EXTERNAL_SHARED_LIB" , "ON" )
914+ . define ( "LLVM_DIR" , builder. llvm_out ( target) ) ;
915+
916+ cfg. build ( ) ;
917+
918+ t ! ( File :: create( & done_stamp) ) ;
919+ out_dir
920+ }
921+ }
922+
852923#[ derive( Debug , Clone , Hash , PartialEq , Eq ) ]
853924pub struct Lld {
854925 pub target : TargetSelection ,
0 commit comments