@@ -858,3 +858,69 @@ impl HashStamp {
858858 fs:: write ( & self . path , self . hash . as_deref ( ) . unwrap_or ( b"" ) )
859859 }
860860}
861+
862+ #[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
863+ pub struct CrtBeginEnd {
864+ pub target : TargetSelection ,
865+ }
866+
867+ impl Step for CrtBeginEnd {
868+ type Output = PathBuf ;
869+
870+ fn should_run ( run : ShouldRun < ' _ > ) -> ShouldRun < ' _ > {
871+ run. path ( "src/llvm-project/compiler-rt/lib/crt" )
872+ }
873+
874+ fn make_run ( run : RunConfig < ' _ > ) {
875+ run. builder . ensure ( CrtBeginEnd { target : run. target } ) ;
876+ }
877+
878+ /// Build crtbegin.o/crtend.o for musl target.
879+ fn run ( self , builder : & Builder < ' _ > ) -> Self :: Output {
880+ let out_dir = builder. native_dir ( self . target ) . join ( "crt" ) ;
881+
882+ if builder. config . dry_run {
883+ return out_dir;
884+ }
885+
886+ let crtbegin_src = builder. src . join ( "src/llvm-project/compiler-rt/lib/crt/crtbegin.c" ) ;
887+ let crtend_src = builder. src . join ( "src/llvm-project/compiler-rt/lib/crt/crtend.c" ) ;
888+ if up_to_date ( & crtbegin_src, & out_dir. join ( "crtbegin.o" ) )
889+ && up_to_date ( & crtend_src, & out_dir. join ( "crtendS.o" ) )
890+ {
891+ return out_dir;
892+ }
893+
894+ builder. info ( "Building crtbegin.o and crtend.o" ) ;
895+ t ! ( fs:: create_dir_all( & out_dir) ) ;
896+
897+ let mut cfg = cc:: Build :: new ( ) ;
898+
899+ if let Some ( ar) = builder. ar ( self . target ) {
900+ cfg. archiver ( ar) ;
901+ }
902+ cfg. compiler ( builder. cc ( self . target ) ) ;
903+ cfg. cargo_metadata ( false )
904+ . out_dir ( & out_dir)
905+ . target ( & self . target . triple )
906+ . host ( & builder. config . build . triple )
907+ . warnings ( false )
908+ . debug ( false )
909+ . opt_level ( 3 )
910+ . file ( crtbegin_src)
911+ . file ( crtend_src) ;
912+
913+ // Those flags are defined in src/llvm-project/compiler-rt/lib/crt/CMakeLists.txt
914+ // Currently only consumer of those objects is musl, which use .init_array/.fini_array
915+ // instead of .ctors/.dtors
916+ cfg. flag ( "-std=c11" )
917+ . define ( "CRT_HAS_INITFINI_ARRAY" , None )
918+ . define ( "EH_USE_FRAME_REGISTRY" , None ) ;
919+
920+ cfg. compile ( "crt" ) ;
921+
922+ t ! ( fs:: copy( out_dir. join( "crtbegin.o" ) , out_dir. join( "crtbeginS.o" ) ) ) ;
923+ t ! ( fs:: copy( out_dir. join( "crtend.o" ) , out_dir. join( "crtendS.o" ) ) ) ;
924+ out_dir
925+ }
926+ }
0 commit comments