[Distutils] High level options interface

John Skaller skaller@maxtal.com.au
Tue, 22 Dec 1998 21:59:30 +1000


At 18:57 21/12/98 +0100, Martijn Faassen wrote:
>class extension_compiler:
>    def compile_C(filename):
>        pass    
>    def compile_C++(filename):
>        pass

There's no need for a base class, just a specification.
Please examine the following interface and see
a gcc implementation below that. At least criticize
the interface if you don't like it.

The interface:

# module 'interscript/compilers.c

class python_module:
  def __init__(self,**kwds):
  # initialise the object with kwds as configuration.
  
  def configure(self,**kwds):
  # extend or change configuration

  def compile(self,filename, **kwds):
  # compile the file, with given options
  # return the string name of the object file

  def link(self,modname, filenames, **kwds):
  # link the list of object files, with given options
  # to generate the named python module.
  # returns the name of the shared library

class application:
  # exactly the same, except produces an executable
  # tool that can be invoked from the command line

# module 'interscript/compilers.cpp
# exactly the same, except for C++

----------------------------------------------------------------------------
------
#line 121 "compilers.ipk"
import os
import sys
import string
import interscript.compilers.cconfig

class python_module:
  def __init__(self,**kwds):
    self.config = interscript.compilers.cconfig.config()
    self.config.append_dict(kwds)

  def configure(self,**kwds):
    self.config.append_dict(kwds)

  def compile(self,filename, **kwds):
    config = self.config.copy()
    config.append_dict(kwds)

    base = string.join(string.split(filename,'.')[:-1],'.')
    obj = base+'.o'
    cc = 'gcc -g -O2 -fpic -fPIC -pedantic '
    inc = '-I' + sys.prefix + '/include/python1.5 '
    if sys.prefix != sys.exec_prefix:
      inc = inc + '-I' + sys.exec_prefix + '/include/python1.5 '
    cstr = str(config)+' '
    arg = cc + cstr +  inc + '-c '+filename + ' -o '+ obj
    print 'system',repr(arg)
    os.system(arg)
    return obj

  def link(self,modname, filenames, **kwds):
    config = self.config.copy()
    config.append_dict(kwds)

    dll = modname +'.so'
    cc ='gcc -shared -Xlinker -export-dynamic '
    cstr = str(config) + ' '
    lib = '-L' + sys.exec_prefix + '/lib/python1.5 '
    files = string.join(filenames) + ' '
    arg = cc + cstr + lib + files + '-o ' + dll

    print 'system',repr(arg)
    os.system(arg)
    return dll

class application:
  def __init__(self,**kwds):
    self.config = interscript.compilers.cconfig.config()
    self.config.append_dict(kwds)

  def configure(self,**kwds):
    self.config.append_dict(kwds)

  def compile(self,filename, **kwds):
    config = self.config.copy()
    config.append_dict(kwds)

    base = string.join(string.split(filename,'.')[:-1],'.')
    obj = base+'.o'
    cc ='gcc -g -O2 -fpic -fPIC -pedantic '
    inc = '-I' + sys.prefix + '/include/python1.5 '
    if sys.prefix != sys.exec_prefix:
      inc = inc + '-I' + sys.exec_prefix + '/include/python1.5 '
    cstr = str(config)+' '
    arg = cc + cstr +  inc + '-c '+filename + ' -o '+ obj
    print 'system',repr(arg)
    os.system(arg)
    return obj

  def link(self,appname, filenames, **kwds):
    config = self.config.copy()
    config.append_dict(kwds)

    cc ='gcc '
    cstr = str(config) + ' '
    lib = '-L' + sys.exec_prefix + '/lib/python1.5 '
    files = string.join(filenames) + ' '
    arg = cc + cstr + lib + files + '-o ' + appname

    print 'system',repr(arg)
    os.system(arg)
    return appname

-------------------------------------------------------
John Skaller    email: skaller@maxtal.com.au
		http://www.maxtal.com.au/~skaller
		phone: 61-2-96600850
		snail: 10/1 Toxteth Rd, Glebe NSW 2037, Australia