[Python-checkins] CVS: distutils/distutils cygwinccompiler.py,1.1,1.2

Greg Ward python-dev@python.org
Thu, 29 Jun 2000 15:57:58 -0700


Update of /cvsroot/python/distutils/distutils
In directory slayer.i.sourceforge.net:/tmp/cvs-serv5227

Modified Files:
	cygwinccompiler.py 
Log Message:
Cleaned up and reformatted by Rene Liebscher.
More reformatting by me.
Also added some editorial comments.

Index: cygwinccompiler.py
===================================================================
RCS file: /cvsroot/python/distutils/distutils/cygwinccompiler.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** cygwinccompiler.py	2000/06/21 03:33:03	1.1
--- cygwinccompiler.py	2000/06/29 22:57:55	1.2
***************
*** 1,9 ****
  """distutils.cygwinccompiler
  
! Contains the CygwinCCompiler class, a subclass of UnixCCompiler that handles
! the Gnu Win32 C compiler.
! It also contains the Mingw32CCompiler class which handles the mingw32 compiler
! (same as cygwin in no-cygwin mode.)
! 
  """
  
--- 1,8 ----
  """distutils.cygwinccompiler
  
! Provides the CygwinCCompiler class, a subclass of UnixCCompiler that
! handles the Cygwin port of the GNU C compiler to Windows.  It also contains
! the Mingw32CCompiler class which handles the mingw32 port of GCC (same as
! cygwin in no-cygwin mode).
  """
  
***************
*** 12,47 ****
  __revision__ = "$Id$"
  
! import os,sys,string,tempfile
  from distutils import sysconfig
  from distutils.unixccompiler import UnixCCompiler
  
! # Because these compilers aren't configured in Python's config.h file by default
! # we should at least warn the user if he used this unmodified version. 
! def check_if_config_h_is_gcc_ready():
!         """ checks, if the gcc-compiler is mentioned in config.h 
!             if it is not, compiling probably doesn't work """ 
!         from distutils import sysconfig
!         import string,sys
          try:
!                 # It would probably better to read single lines to search.
!                 # But we do this only once, and it is fast enough 
!                 f=open(sysconfig.get_config_h_filename())
!                 s=f.read()
!                 f.close()
!                 try:
!                         string.index(s,"__GNUC__") # is somewhere a #ifdef __GNUC__ or something similar
!                 except:
!                         sys.stderr.write ("warning: Python's config.h doesn't seem to support your compiler.\n")
!         except: # unspecific error => ignore
!                 pass
  
  
  # This is called when the module is imported, so we make this check only once
! check_if_config_h_is_gcc_ready()
  
  
- # XXX Things not currently handled:
- #   * see UnixCCompiler
- 
  class CygwinCCompiler (UnixCCompiler):
  
--- 11,54 ----
  __revision__ = "$Id$"
  
! import os,sys,string
  from distutils import sysconfig
  from distutils.unixccompiler import UnixCCompiler
  
! # Because these compilers aren't configured in Python's config.h file by
! # default we should at least warn the user if he is using a unmodified
! # version.
! 
! def check_config_h():
!     """Checks if the GCC compiler is mentioned in config.h.  If it is not,
!     compiling probably doesn't work, so print a warning to stderr.
!     """
! 
!     # XXX the result of the check should be returned!
! 
!     from distutils import sysconfig
!     import string,sys
!     try:
!         # It would probably better to read single lines to search.
!         # But we do this only once, and it is fast enough 
!         f=open(sysconfig.get_config_h_filename())
!         s=f.read()
!         f.close()
          try:
!             # is somewhere a #ifdef __GNUC__ or something similar
!             string.index(s,"__GNUC__") 
!         except ValueError:
!             sys.stderr.write ("warning: "+
!                 "Python's config.h doesn't seem to support your compiler.\n")
!     except IOError:
!         # if we can't read this file, we cannot say it is wrong
!         # the compiler will complain later about this file as missing
!         pass
  
  
  # This is called when the module is imported, so we make this check only once
! # XXX why not make it only when the compiler is needed?
! check_config_h()
  
  
  class CygwinCCompiler (UnixCCompiler):
  
***************
*** 55,73 ****
          UnixCCompiler.__init__ (self, verbose, dry_run, force)
  
! 	# our compiler uses other names
! 	self.cc='gcc'
! 	self.ld_shared='dllwrap'
! 	self.ldflags_shared=[]
! 
!         # some variables to manage the differences between cygwin and mingw32
!         self.dllwrap_options=["--target=i386-cygwin32"]
!         # specification of entry point is not necessary
!         
!         self.dll_additional_libraries=[
!                # cygwin shouldn't need msvcrt, but without the dll's will crash
!                # perhaps something about initialization (Python uses it, too)
                 # mingw32 needs it in all cases
!                                         "msvcrt"
!                                       ]
          
      # __init__ ()
--- 62,81 ----
          UnixCCompiler.__init__ (self, verbose, dry_run, force)
  
!         # Hard-code GCC because that's what this is all about.
!         # XXX optimization, warnings etc. should be customizable.
!         self.set_executables(compiler='gcc -O -Wall',
!                              compiler_so='gcc -O -Wall',
!                              linker_exe='gcc',
!                              linker_so='dllwrap --target=i386-cygwin32')
! 
!         # cygwin and mingw32 need different sets of libraries 
!         self.dll_libraries=[
!                # cygwin shouldn't need msvcrt, 
!                # but without the dll's will crash
!                # ( gcc version 2.91.57 )
!                # perhaps something about initialization
                 # mingw32 needs it in all cases
!                             "msvcrt"
!                             ]
          
      # __init__ ()
***************
*** 81,139 ****
                              runtime_library_dirs=None,
                              export_symbols=None,
! 			    debug=0,
                              extra_preargs=None,
!                             extra_postargs=None):
!         
!         if libraries==None:
!                 libraries=[]
          
!         python_library=["python"+str(sys.hexversion>>24)+str((sys.hexversion>>16)&0xff)]
!         libraries=libraries+python_library+self.dll_additional_libraries
          
!         # if you don't need the def-file afterwards, it is
!         # better to use for it tempfile.mktemp() as its name
!         # (unix-style compilers don't like backslashes in filenames)
!         win_dll_def_file=string.replace(tempfile.mktemp(),"\\","/")
!         #win_dll_def_file=output_filename[:-len(self.shared_lib_extension)]+".def"
!         #win_dll_exp_file=output_filename[:-len(self.shared_lib_extension)]+".exp"
!         #win_dll_lib_file=output_filename[:-len(self.shared_lib_extension)]+".a"
          
          # Make .def file
!         # (It would probably better to check if we really need this, but for this we had to
!         # insert some unchanged parts of UnixCCompiler, and this is not what I want.) 
!         f=open(win_dll_def_file,"w")
          f.write("EXPORTS\n") # intro
!         # always export a function "init"+module_name
!         if not debug:
!                 f.write("init"+os.path.basename(output_filename)[:-len(self.shared_lib_extension)]+"\n")
!         else: # in debug mode outfile_name is something like XXXXX_d.pyd
!                 f.write("init"+os.path.basename(output_filename)[:-(len(self.shared_lib_extension)+2)]+"\n")
!         # if there are more symbols to export
!         # insert code here to write them in f
!         if export_symbols!=None: 
              for sym in export_symbols:
!                 f.write(sym+"\n")                
          f.close()
          
!         if extra_preargs==None:
!                 extra_preargs=[]
          
!         extra_preargs=extra_preargs+[
                          #"--verbose",
!                         #"--output-exp",win_dll_exp_file,
!                         #"--output-lib",win_dll_lib_file,
!                         "--def",win_dll_def_file
!                         ]+ self.dllwrap_options
          
!         # who wants symbols and a many times greater output file
          # should explicitely switch the debug mode on 
!         # otherwise we let dllwrap strip the outputfile
!         # (On my machine unstripped_file=stripped_file+254KB
          #   10KB < stripped_file < ??100KB ) 
          if not debug: 
!                 extra_preargs=extra_preargs+["-s"] 
! 	
! 	try:        
!     	    UnixCCompiler.link_shared_object(self,
                              objects,
                              output_filename,
--- 89,157 ----
                              runtime_library_dirs=None,
                              export_symbols=None,
!                             debug=0,
                              extra_preargs=None,
!                             extra_postargs=None,
!                             build_temp=None):
          
!         if libraries == None:
!             libraries = []
          
!         # Additional libraries: the python library is always needed on
!         # Windows we need the python version without the dot, eg. '15'
! 
!         pythonlib = ("python%d%d" %
!                      (sys.hexversion >> 24, (sys.hexversion >> 16) & 0xff))
!         libraries.append(pythonlib)
!         libraries.extend(self.dll_libraries)
! 
!         # name of extension
! 
!         # XXX WRONG WRONG WRONG
!         # this is NOT the place to make guesses about Python namespaces;
!         # that MUST be done in build_ext.py
! 
!         if not debug:
!             ext_name = os.path.basename(output_filename)[:-len(".pyd")]
!         else:
!             ext_name = os.path.basename(output_filename)[:-len("_d.pyd")]
! 
!         def_file = os.path.join(build_temp, ext_name + ".def")
!         #exp_file = os.path.join(build_temp, ext_name + ".exp")
!         #lib_file = os.path.join(build_temp, 'lib' + ext_name + ".a")
          
          # Make .def file
!         # (It would probably better to check if we really need this, 
!         # but for this we had to insert some unchanged parts of 
!         # UnixCCompiler, and this is not what we want.) 
!         f = open(def_file,"w")
          f.write("EXPORTS\n") # intro
!         if export_symbols == None: 
!             # export a function "init" + ext_name
!             f.write("init" + ext_name + "\n")    
!         else:
!             # if there are more symbols to export write them into f
              for sym in export_symbols:
!                 f.write(sym+"\n")                    
          f.close()
          
!         if extra_preargs == None:
!                 extra_preargs = []
          
!         extra_preargs = extra_preargs + [
                          #"--verbose",
!                         #"--output-exp",exp_file,
!                         #"--output-lib",lib_file,
!                         "--def",def_file
!                         ]
          
!         # who wants symbols and a many times larger output file
          # should explicitely switch the debug mode on 
!         # otherwise we let dllwrap strip the output file
!         # (On my machine unstripped_file = stripped_file + 254KB
          #   10KB < stripped_file < ??100KB ) 
          if not debug: 
!             extra_preargs = extra_preargs + ["-s"] 
!         
!         UnixCCompiler.link_shared_object(self,
                              objects,
                              output_filename,
***************
*** 142,152 ****
                              library_dirs,
                              runtime_library_dirs,
! 			    None, # export_symbols, we do this with our def-file 
                              debug,
                              extra_preargs,
!                             extra_postargs)
!         finally: 
!     	    # we don't need the def-file anymore
!     	    os.remove(win_dll_def_file) 
          
      # link_shared_object ()
--- 160,168 ----
                              library_dirs,
                              runtime_library_dirs,
!                             None, # export_symbols, we do this with our def-file
                              debug,
                              extra_preargs,
!                             extra_postargs,
!                             build_temp)
          
      # link_shared_object ()
***************
*** 154,157 ****
--- 170,174 ----
  # class CygwinCCompiler
  
+ 
  # the same as cygwin plus some additional parameters
  class Mingw32CCompiler (CygwinCCompiler):
***************
*** 165,177 ****
  
          CygwinCCompiler.__init__ (self, verbose, dry_run, force)
  
-         self.ccflags = self.ccflags + ["-mno-cygwin"]
-         self.dllwrap_options=[
-                                 # mingw32 doesn't really need 'target' 
-                                 # and cygwin too (it seems, it is enough
-                                 # to specify a different entry point)                
-                                 #"--target=i386-mingw32",
-                                 "--entry","_DllMain@12"
-                                 ]
          # no additional libraries need 
          # (only msvcrt, which is already added by CygwinCCompiler)
--- 182,195 ----
  
          CygwinCCompiler.__init__ (self, verbose, dry_run, force)
+ 
+         self.set_executables(compiler='gcc -mno-cygwin -O -Wall',
+                              compiler_so='gcc -mno-cygwin -O -Wall',
+                              linker_exe='gcc -mno-cygwin',
+                              linker_so='dllwrap' 
+                                     + ' --target=i386-mingw32'
+                                     + ' --entry _DllMain@12')
+         # mingw32 doesn't really need 'target' and cygwin too (it seems, 
+         # it is enough to specify a different entry point)                
  
          # no additional libraries need 
          # (only msvcrt, which is already added by CygwinCCompiler)