ObjectFileLoader.st
branchjv
changeset 3968 4e4f134b6e26
parent 3966 7399600c96cf
parent 3962 ce73bb115aa3
child 3970 72df4cc677f0
equal deleted inserted replaced
3967:706e93959a9c 3968:4e4f134b6e26
  2663      errorMessage homeDir fmt|
  2663      errorMessage homeDir fmt|
  2664 
  2664 
  2665     osType := OperatingSystem getOSType.
  2665     osType := OperatingSystem getOSType.
  2666 
  2666 
  2667     osType = #win32 ifTrue:[
  2667     osType = #win32 ifTrue:[
  2668 	self activityNotification:'generating shared object'.
  2668         self activityNotification:'generating shared object'.
  2669 	ParserFlags linkArgs isNil ifTrue:[
  2669         ParserFlags linkArgs isNil ifTrue:[
  2670 	    ld := self linkCommand , ' ' , baseFileName , '.obj'.
  2670             ld := self linkCommand , ' ' , baseFileName , '.obj'.
  2671 	    ld := ld
  2671             ld := ld
  2672 	       , ' /NOPACK /NOLOGO /DEBUG /MACHINE:I386 /DLL'
  2672                , ' /NOPACK /NOLOGO /DEBUG /MACHINE:I386 /DLL'
  2673 	       , ' /OUT:' , baseFileName , '.dll '
  2673                , ' /OUT:' , baseFileName , '.dll '
  2674 "/               , ' /DEF:' , baseFileName , '.def'.
  2674 "/               , ' /DEF:' , baseFileName , '.def'.
  2675 	] ifFalse:[
  2675         ] ifFalse:[
  2676 "/            libDir := ParserFlags libDirectory.
  2676 "/            libDir := ParserFlags libDirectory.
  2677 "/            (libDir notNil and:[libDir asFilename exists]) ifFalse:[
  2677 "/            (libDir notNil and:[libDir asFilename exists]) ifFalse:[
  2678 		ParserFlags useBorlandC ifTrue:[
  2678                 ParserFlags useBorlandC ifTrue:[
  2679 		    libDirBasename := 'lib\bc'
  2679                     libDirBasename := 'lib\bc'
  2680 		] ifFalse:[
  2680                 ] ifFalse:[
  2681 		    ParserFlags useVisualC ifTrue:[
  2681                     ParserFlags useVisualC ifTrue:[
  2682 			libDirBasename := 'lib\vc'
  2682                         libDirBasename := 'lib\vc'
  2683 		    ] ifFalse:[
  2683                     ] ifFalse:[
  2684 			(ParserFlags useMingw32 or:[ParserFlags useMingw64]) ifTrue:[
  2684                         (ParserFlags useMingw32 or:[ParserFlags useMingw64]) ifTrue:[
  2685 			    libDirBasename := 'lib\mingw'
  2685                             libDirBasename := 'lib\mingw'
  2686 			] ifFalse:[
  2686                         ] ifFalse:[
  2687 			    libDirBasename := 'lib\vc'
  2687                             libDirBasename := 'lib\vc'
  2688 			]
  2688                         ]
  2689 		    ]
  2689                     ]
  2690 		].
  2690                 ].
  2691 		homeDir := Smalltalk packagePath detect:[:p | (p asFilename / 'stx' / libDirBasename) exists] ifNone:nil.
  2691                 homeDir := Smalltalk packagePath detect:[:p | (p asFilename / 'stx' / libDirBasename) exists] ifNone:nil.
  2692 		homeDir notNil ifTrue:[
  2692                 homeDir notNil ifTrue:[
  2693 		    libDir := (homeDir asFilename / 'stx' / libDirBasename) pathName
  2693                     libDir := (homeDir asFilename / 'stx' / libDirBasename) pathName
  2694 		].
  2694                 ].
  2695 		libDir isNil ifTrue:[
  2695                 libDir isNil ifTrue:[
  2696 		    "/ some fallback
  2696                     "/ some fallback
  2697 		    libDir := #( '..'
  2697                     libDir := #( '..'
  2698 				 '..\..'
  2698                                  '..\..'
  2699 				 '..\..\..'
  2699                                  '..\..\..'
  2700 				 '..\..\stx'
  2700                                  '..\..\stx'
  2701 				 '..\..\..\stx'
  2701                                  '..\..\..\stx'
  2702 			       ) detect:[:p | (p asFilename / libDirBasename) exists] ifNone:nil.
  2702                                ) detect:[:p | (p asFilename / libDirBasename) exists] ifNone:nil.
  2703 
  2703 
  2704 		    libDir isNil ifTrue:[
  2704                     libDir isNil ifTrue:[
  2705 			LastError := errorMessage := 'could not locate directory where .lib files are (',libDirBasename,')'.
  2705                         LastError := errorMessage := 'could not locate directory where .lib files are (',libDirBasename,')'.
  2706 			ObjectFileLoadError raiseRequestErrorString:errorMessage.
  2706                         ObjectFileLoadError raiseRequestErrorString:errorMessage.
  2707 			^ nil
  2707                         ^ nil
  2708 		    ].
  2708                     ].
  2709 		].
  2709                 ].
  2710 "/            ].
  2710 "/            ].
  2711 
  2711 
  2712 	    ParserFlags useBorlandC ifTrue:[
  2712             ParserFlags useBorlandC ifTrue:[
  2713 		ld := self linkCommand , ' ' , (ParserFlags linkArgs bindWith:baseFileName).
  2713                 ld := self linkCommand , ' ' , (ParserFlags linkArgs bindWith:baseFileName).
  2714 		ld := ld , ' c0d32.obj ' , baseFileName , '.obj '.
  2714                 ld := ld , ' c0d32.obj ' , baseFileName , '.obj '.
  2715 		ld := ld , ',' , baseFileName , '.dll,,',libDir,'\librun.lib '.
  2715                 ld := ld , ',' , baseFileName , '.dll,,',libDir,'\librun.lib '.
  2716 		ld := ld , (ParserFlags searchedLibraries asStringCollection asStringWith: $ ).
  2716                 ld := ld , (ParserFlags searchedLibraries asStringCollection asStringWith: $ ).
  2717 		ld := ld , ' ',libDir,'\cs32i.lib,,'.
  2717                 ld := ld , ' ',libDir,'\cs32i.lib,,'.
  2718 	    ] ifFalse:[
  2718             ] ifFalse:[
  2719 		ParserFlags useVisualC ifTrue:[
  2719                 ParserFlags useVisualC ifTrue:[
  2720 		    "/ todo: fix for correct link libs
  2720                     "/ todo: fix for correct link libs
  2721 		    ld := self linkCommand , ' ' , (ParserFlags linkArgs bindWith:baseFileName).
  2721                     ld := self linkCommand , ' ' , (ParserFlags linkArgs bindWith:baseFileName).
  2722 		    ld := ld , ' c0d32.obj ' , baseFileName , '.obj '.
  2722                     ld := ld , ' c0d32.obj ' , baseFileName , '.obj '.
  2723 		    ld := ld , ',' , baseFileName , '.dll,,',libDir,'\librun.lib '.
  2723                     ld := ld , ',' , baseFileName , '.dll,,',libDir,'\librun.lib '.
  2724 		    ld := ld , (ParserFlags searchedLibraries asStringCollection asStringWith: $ ).
  2724                     ld := ld , (ParserFlags searchedLibraries asStringCollection asStringWith: $ ).
  2725 		    ld := ld , ' ',libDir,'\cs32i.lib,,'.
  2725                     ld := ld , ' ',libDir,'\cs32i.lib,,'.
  2726 		] ifFalse:[
  2726                 ] ifFalse:[
  2727 		    (ParserFlags useMingw64 or:[ParserFlags useMingw32]) ifTrue:[
  2727                     (ParserFlags useMingw64 or:[ParserFlags useMingw32]) ifTrue:[
  2728 			ld := self linkCommand , ' ' , (ParserFlags linkArgs bindWith:baseFileName).
  2728                         ld := self linkCommand , ' ' , (ParserFlags linkArgs bindWith:baseFileName).
  2729 			ld := ld , ' -shared -o ',baseFileName,'.dll '.
  2729                         ld := ld , ' -shared -o ',baseFileName,'.dll '.
  2730 			ld := ld , baseFileName,'.obj '.
  2730                         ld := ld , baseFileName,'.obj '.
  2731 			ld := ld , libDir,'\librun.lib '.
  2731                         ld := ld , libDir,'\librun.lib '.
  2732 			ld := ld , (ParserFlags searchedLibraries asStringCollection asStringWith: $ ).
  2732                         ld := ld , (ParserFlags searchedLibraries asStringCollection asStringWith: $ ).
  2733 		    ] ifFalse:[
  2733                     ] ifFalse:[
  2734 			ObjectFileLoadError raiseRequestErrorString:'for dynamic objects, only borlandC is (currently) supported'.
  2734                         ObjectFileLoadError raiseRequestErrorString:'for dynamic objects, only borlandC is (currently) supported'.
  2735 			LastError := 'for dynamic objects, only borlandC is (currently) supported'.
  2735                         LastError := 'for dynamic objects, only borlandC is (currently) supported'.
  2736 			^ nil
  2736                         ^ nil
  2737 		    ]
  2737                     ]
  2738 		]
  2738                 ]
  2739 	    ]
  2739             ]
  2740 	].
  2740         ].
  2741 
  2741 
  2742 	outfile := (baseFileName , '.out').
  2742         outfile := (baseFileName , '.out').
  2743 	(Verbose or:[ STCCompilerInterface verbose ]) ifTrue:[
  2743         (Verbose or:[ STCCompilerInterface verbose ]) ifTrue:[
  2744 	    Transcript showCR:('executing: ',ld).
  2744             Transcript showCR:('executing: ',ld).
  2745 	].
  2745         ].
  2746 
  2746 
  2747 	ok := OperatingSystem executeCommand:(ld , ' >' , outfile).
  2747         ok := OperatingSystem executeCommand:(ld , ' >' , outfile) showWindow:false.
  2748 	ok ifFalse:[
  2748         ok ifFalse:[
  2749 	    output := (baseFileName , '.out') asFilename contentsOfEntireFile.
  2749             output := (baseFileName , '.out') asFilename contentsOfEntireFile.
  2750 	    Transcript showCR:output; endEntry.
  2750             Transcript showCR:output; endEntry.
  2751 	    Transcript showCR:'Failed linkCommand:'.
  2751             Transcript showCR:'Failed linkCommand:'.
  2752 	    Transcript showCR:ld.
  2752             Transcript showCR:ld.
  2753 	    Transcript showCR:'ParserFlags are:'.
  2753             Transcript showCR:'ParserFlags are:'.
  2754 	    Transcript showCR:ParserFlags.
  2754             Transcript showCR:ParserFlags.
  2755 	].
  2755         ].
  2756 
  2756 
  2757 	#('.obj' '.out' '.tds' '.ilc' '.ild'
  2757         #('.obj' '.out' '.tds' '.ilc' '.ild'
  2758 	  '.ilf' '.ils' '.lib' '.map' '.def' '.o') do:[:eachSuffix|
  2758           '.ilf' '.ils' '.lib' '.map' '.def' '.o') do:[:eachSuffix|
  2759 	    (baseFileName , eachSuffix) asFilename delete.
  2759             (baseFileName , eachSuffix) asFilename delete.
  2760 	].
  2760         ].
  2761 
  2761 
  2762 	ok ifFalse:[
  2762         ok ifFalse:[
  2763 	    LastError := output.
  2763             LastError := output.
  2764 	    "/ ObjectFileLoadError raiseRequestErrorString:'link failed'.
  2764             "/ ObjectFileLoadError raiseRequestErrorString:'link failed'.
  2765 	    ^ nil
  2765             ^ nil
  2766 	].
  2766         ].
  2767 	oFileName := (baseFileName , self sharedLibraryExtension) asFilename name.
  2767         oFileName := (baseFileName , self sharedLibraryExtension) asFilename name.
  2768 	^ oFileName
  2768         ^ oFileName
  2769     ].
  2769     ].
  2770 
  2770 
  2771     "/ UNIX systems
  2771     "/ UNIX systems
  2772 
  2772 
  2773     ld := self linkCommand ? 'ld'.
  2773     ld := self linkCommand ? 'ld'.
  2774     needSharedObject := false.
  2774     needSharedObject := false.
  2775 
  2775 
  2776     fmt := ObjectFileLoader loadableBinaryObjectFormat.
  2776     fmt := ObjectFileLoader loadableBinaryObjectFormat.
  2777     (fmt == #elf
  2777     (fmt == #elf
  2778     or:[ fmt == #macho ]) ifTrue:[
  2778     or:[ fmt == #macho ]) ifTrue:[
  2779 	"
  2779         "
  2780 	 link it to a shared object with 'ld -shared'
  2780          link it to a shared object with 'ld -shared'
  2781 	"
  2781         "
  2782 	needSharedObject := true.
  2782         needSharedObject := true.
  2783 	ld := self linkCommand ? 'cc'.
  2783         ld := self linkCommand ? 'cc'.
  2784 	ldArg := self linkSharedArgs.
  2784         ldArg := self linkSharedArgs.
  2785 	ldArg isNil ifTrue:[
  2785         ldArg isNil ifTrue:[
  2786 	    "/ some default
  2786             "/ some default
  2787 	    ExternalAddress pointerSize == 4 ifTrue:[
  2787             ExternalAddress pointerSize == 4 ifTrue:[
  2788 		ldArg := '-m32 -shared'.
  2788                 ldArg := '-m32 -shared'.
  2789 	    ] ifFalse:[
  2789             ] ifFalse:[
  2790 		ldArg := '--shared'.
  2790                 ldArg := '--shared'.
  2791 	    ]
  2791             ]
  2792 	]
  2792         ]
  2793     ].
  2793     ].
  2794 
  2794 
  2795     osType = #irix ifTrue:[
  2795     osType = #irix ifTrue:[
  2796 	"
  2796         "
  2797 	 link it to a shared object with 'ld -shared'
  2797          link it to a shared object with 'ld -shared'
  2798 	"
  2798         "
  2799 	needSharedObject := true.
  2799         needSharedObject := true.
  2800 	ldArg := self linkSharedArgs ? '-shared'.
  2800         ldArg := self linkSharedArgs ? '-shared'.
  2801     ].
  2801     ].
  2802 
  2802 
  2803     osType = #'sys5_4' ifTrue:[
  2803     osType = #'sys5_4' ifTrue:[
  2804 	"
  2804         "
  2805 	 link it to a shared object with 'ld -G'
  2805          link it to a shared object with 'ld -G'
  2806 	"
  2806         "
  2807 	needSharedObject := true.
  2807         needSharedObject := true.
  2808 	ldArg := self linkSharedArgs ? '-G'.
  2808         ldArg := self linkSharedArgs ? '-G'.
  2809     ].
  2809     ].
  2810 
  2810 
  2811     osType = #solaris ifTrue:[
  2811     osType = #solaris ifTrue:[
  2812 	"
  2812         "
  2813 	 link it to a shared object with 'ld -G -B dynamic'
  2813          link it to a shared object with 'ld -G -B dynamic'
  2814 	"
  2814         "
  2815 	needSharedObject := true.
  2815         needSharedObject := true.
  2816 	ldArg := self linkSharedArgs ? '-G -Bdynamic'.
  2816         ldArg := self linkSharedArgs ? '-G -Bdynamic'.
  2817     ].
  2817     ].
  2818 
  2818 
  2819     osType = #hpux ifTrue:[
  2819     osType = #hpux ifTrue:[
  2820 	"
  2820         "
  2821 	 link it to a shared object with 'ld -b -B immediate'
  2821          link it to a shared object with 'ld -b -B immediate'
  2822 	"
  2822         "
  2823 	needSharedObject := true.
  2823         needSharedObject := true.
  2824 	ldArg := self linkSharedArgs ? '-b -B immediate'.
  2824         ldArg := self linkSharedArgs ? '-b -B immediate'.
  2825     ].
  2825     ].
  2826 
  2826 
  2827     osType = #aix ifTrue:[
  2827     osType = #aix ifTrue:[
  2828 	self activityNotification:'create export file'.
  2828         self activityNotification:'create export file'.
  2829 
  2829 
  2830 	"/ create an exports file.
  2830         "/ create an exports file.
  2831 	expFileName := './' , baseFileName , '.exp'.
  2831         expFileName := './' , baseFileName , '.exp'.
  2832 	[
  2832         [
  2833 	    expFile := expFileName asFilename writeStream.
  2833             expFile := expFileName asFilename writeStream.
  2834 	    expFile nextPutAll:'#!! ./' , baseFileName , (self sharedLibraryExtension).
  2834             expFile nextPutAll:'#!! ./' , baseFileName , (self sharedLibraryExtension).
  2835 	    expFile cr.
  2835             expFile cr.
  2836 	    expFile nextPutAll:'_' , baseFileName , '_Init'.
  2836             expFile nextPutAll:'_' , baseFileName , '_Init'.
  2837 	    expFile close.
  2837             expFile close.
  2838 	] on:FileStream openErrorSignal do:[:ex| "do nothing"].
  2838         ] on:FileStream openErrorSignal do:[:ex| "do nothing"].
  2839 
  2839 
  2840 	self activityNotification:'generating shared object'.
  2840         self activityNotification:'generating shared object'.
  2841 
  2841 
  2842 	"
  2842         "
  2843 	 link it to a shared object with 'cc -bI:...librun.exp -bE -bMSRE'
  2843          link it to a shared object with 'cc -bI:...librun.exp -bE -bMSRE'
  2844 	"
  2844         "
  2845 	needSharedObject := true.
  2845         needSharedObject := true.
  2846 	ld := 'cc'.
  2846         ld := 'cc'.
  2847 	librunExpFileName := Smalltalk getSystemFileName:'lib/librun_aix.exp'.
  2847         librunExpFileName := Smalltalk getSystemFileName:'lib/librun_aix.exp'.
  2848 	librunExpFileName isNil ifTrue:[
  2848         librunExpFileName isNil ifTrue:[
  2849 	    LastError := 'missing exports file: ''lib/librun_aix.exp'' - cannot link'.
  2849             LastError := 'missing exports file: ''lib/librun_aix.exp'' - cannot link'.
  2850 	    ^ nil
  2850             ^ nil
  2851 	].
  2851         ].
  2852 
  2852 
  2853 	ldArg := '-bI:' , librunExpFileName ,
  2853         ldArg := '-bI:' , librunExpFileName ,
  2854 		' -bE:' , baseFileName , '.exp' ,
  2854                 ' -bE:' , baseFileName , '.exp' ,
  2855 		' -bM:SRE -e _' , baseFileName , '_Init'.
  2855                 ' -bM:SRE -e _' , baseFileName , '_Init'.
  2856     ].
  2856     ].
  2857 
  2857 
  2858     oFileName := baseFileName , self objectFileExtension.
  2858     oFileName := baseFileName , self objectFileExtension.
  2859     needSharedObject ifTrue:[
  2859     needSharedObject ifTrue:[
  2860 	self activityNotification:'generating shared object'.
  2860         self activityNotification:'generating shared object'.
  2861 
  2861 
  2862 	soFileName := baseFileName , self sharedLibraryExtension.
  2862         soFileName := baseFileName , self sharedLibraryExtension.
  2863 	OperatingSystem removeFile:soFileName.
  2863         OperatingSystem removeFile:soFileName.
  2864 	ld := ld , ' ' , ldArg , ' ', (ParserFlags linkArgs ? '') ,
  2864         ld := ld , ' ' , ldArg , ' ', (ParserFlags linkArgs ? '') ,
  2865 		 ' -o ' , soFileName , ' ' , oFileName , ' ',
  2865                  ' -o ' , soFileName , ' ' , oFileName , ' ',
  2866 		 ((ParserFlags searchedLibraries  ? #()) asStringCollection asStringWith: $ ).
  2866                  ((ParserFlags searchedLibraries  ? #()) asStringCollection asStringWith: $ ).
  2867 
  2867 
  2868 	(Verbose or:[ STCCompilerInterface verbose ]) ifTrue:[
  2868         (Verbose or:[ STCCompilerInterface verbose ]) ifTrue:[
  2869 	    Transcript showCR:('linking with: ',ld).
  2869             Transcript showCR:('linking with: ',ld).
  2870 	].
  2870         ].
  2871 	ok := OperatingSystem executeCommand:(ld , ' >errorOutput 2>&1').
  2871         ok := OperatingSystem executeCommand:(ld , ' >errorOutput 2>&1').
  2872 
  2872 
  2873 	ok ifFalse:[
  2873         ok ifFalse:[
  2874 	    output := 'errorOutput' asFilename contentsOfEntireFile.
  2874             output := 'errorOutput' asFilename contentsOfEntireFile.
  2875 	    Transcript showCR:'linker error message:'.
  2875             Transcript showCR:'linker error message:'.
  2876 	    Transcript showCR:output.
  2876             Transcript showCR:output.
  2877 	    Transcript showCR:'linker command:'.
  2877             Transcript showCR:'linker command:'.
  2878 	    Transcript showCR:ld; endEntry.
  2878             Transcript showCR:ld; endEntry.
  2879 	].
  2879         ].
  2880 
  2880 
  2881 	OperatingSystem removeFile:oFileName.
  2881         OperatingSystem removeFile:oFileName.
  2882 	expFileName notNil ifTrue:[
  2882         expFileName notNil ifTrue:[
  2883 	    OperatingSystem removeFile:expFileName
  2883             OperatingSystem removeFile:expFileName
  2884 	].
  2884         ].
  2885 	^ soFileName.
  2885         ^ soFileName.
  2886     ].
  2886     ].
  2887 
  2887 
  2888     "
  2888     "
  2889      assume we can load an ordinary binary
  2889      assume we can load an ordinary binary
  2890     "
  2890     "