[Numpy-svn] r4229 - branches/numpy.scons/numpy/core

numpy-svn@scip... numpy-svn@scip...
Thu Oct 18 03:18:27 CDT 2007


Author: cdavid
Date: 2007-10-18 03:18:22 -0500 (Thu, 18 Oct 2007)
New Revision: 4229

Modified:
   branches/numpy.scons/numpy/core/SConstruct
   branches/numpy.scons/numpy/core/scons_support.py
Log:

* Clean up custom builders for numpy.core, using changes from cleanconfig
  branch
* Other minor clean up in numpy.core SConscript and support



Modified: branches/numpy.scons/numpy/core/SConstruct
===================================================================
--- branches/numpy.scons/numpy/core/SConstruct	2007-10-18 08:16:23 UTC (rev 4228)
+++ branches/numpy.scons/numpy/core/SConstruct	2007-10-18 08:18:22 UTC (rev 4229)
@@ -1,4 +1,4 @@
-# Last Change: Thu Oct 18 03:00 PM 2007 J
+# Last Change: Thu Oct 18 04:00 PM 2007 J
 # vim:syntax=python
 import os
 from os.path import join as pjoin, basename as pbasename, dirname as pdirname
@@ -8,8 +8,6 @@
 from numpy.distutils.scons import get_python_inc
 from numpy.distutils.scons import GetNumpyEnvironment, CheckTypeSize
 
-import SCons.Util
-
 def generate_config_header(target, source, env):
     t = open(str(target[0]), 'w')
     if not env.has_key('config_h_gen'):
@@ -21,7 +19,7 @@
             return "#define %s\n\n" % define
         elif value == 0:
             return r"/* #undef %s */\n\n" % define
-        elif SCons.Util.is_String(value):
+        else:
             return "#define %s %s\n\n" % (define, value)
     t.writelines([write_symbol(i[0], i[1]) for i in sym])
     t.write('\n')
@@ -196,6 +194,10 @@
 
 config.Finish()
 
+#==========
+#  Build
+#==========
+
 #---------------------------------------
 # Generate the public configuration file
 #---------------------------------------
@@ -212,11 +214,28 @@
 #---------------------------
 # Builder for generated code
 #---------------------------
-from scons_support import do_generate_api, generate_api_emitter, do_generate_ufunc_api
+from scons_support import do_generate_array_api, do_generate_ufunc_api, \
+                        generate_api_emitter,\
+                        generate_from_template, generate_from_template_emitter, \
+                        generate_umath, generate_umath_emitter
 
-# XXX: put the builder in scons support lib.
-bld = Builder(action = do_generate_api, emitter = generate_api_emitter)
-env.Append(BUILDERS = {'GenerateApi' : bld})
+array_api_gen_bld = Builder(action = do_generate_array_api, 
+                            emitter = generate_api_emitter)
+
+ufunc_api_gen_bld = Builder(action = do_generate_ufunc_api, 
+                            emitter = generate_api_emitter)
+
+template_bld = Builder(action = generate_from_template, 
+                       emitter = generate_from_template_emitter)
+
+umath_bld = Builder(action = generate_umath, 
+                    emitter = generate_umath_emitter)
+
+env.Append(BUILDERS = {'GenerateMultiarrayApi' : array_api_gen_bld,
+                       'GenerateUfuncApi' : ufunc_api_gen_bld,
+                       'GenerateFromTemplate' : template_bld,
+                       'GenerateUmath' : umath_bld})
+
 from os.path import join as pjoin
 
 # Take into account the change in config_sym (using emitter, see SubstDic
@@ -225,88 +244,55 @@
 bld = Builder(action = generate_config_header)
 env.Append(BUILDERS = {'GenerateConfigHeader' : bld})
 
-# Builder to generate .inc from .inc.src
-from numpy.distutils.conv_template import process_str
-def do_generate_from_template(targetfile, sourcefile, env):
-    t = open(targetfile, 'w')
-    s = open(sourcefile, 'r')
-    allstr = s.read()
-    s.close()
-    writestr = process_str(allstr)
-    t.write(writestr)
-    t.close()
-    return 0
+#------------------------
+# Generate generated code
+#------------------------
+config_header = env.GenerateConfigHeader(pjoin(env['build_dir'], 'config.h'), [])
 
-def generate_from_template(target, source, env):
-    for t, s in zip(target, source):
-        do_generate_from_template(str(t), str(s), env)
+scalartypes_src = env.GenerateFromTemplate(
+                    pjoin(env['build_dir'], 'src', 'scalartypes'), 
+                    pjoin(env['src_dir'], 'src', 'scalartypes.inc.src'))
 
-def generate_from_template_emitter(target, source, env):
-    base, ext = SCons.Util.splitext(pbasename(str(source[0])))
-    t = pjoin(pdirname(str(target[0])), base)
-    return ([t], source)
-    
-def do_generate_umath(targetfile, sourcefile, env):
-    t = open(targetfile, 'w')
-    from code_generators import generate_umath
-    code = generate_umath.make_code(generate_umath.defdict, generate_umath.__file__)
-    t.write(code)
-    t.close()
+arraytypes_src = env.GenerateFromTemplate(
+                    pjoin(env['build_dir'], 'src', 'arraytypes'), 
+                    pjoin(env['src_dir'], 'src', 'arraytypes.inc.src'))
 
-def generate_umath(target, source, env):
-    for t, s in zip(target, source):
-        do_generate_umath(str(t), str(s), env)
+sortmodule_src = env.GenerateFromTemplate(
+                    pjoin(env['build_dir'], 'src', '_sortmodule'), 
+                    pjoin(env['src_dir'], 'src', '_sortmodule.c.src'))
 
-def generate_umath_emitter(target, source, env):
-    t = str(target[0]) + '.c'
-    return ([t], source)
-    
-bld = Builder(action = generate_umath, emitter = generate_umath_emitter)
-env.Append(BUILDERS = {'GenerateUmath' : bld})
+umathmodule_src = env.GenerateFromTemplate(
+                    pjoin(env['build_dir'], 'src', 'umathmodule'), 
+                    pjoin(env['src_dir'], 'src', 'umathmodule.c.src'))
 
-bld = Builder(action = do_generate_ufunc_api, emitter = generate_api_emitter)
-env.Append(BUILDERS = {'GenerateUfuncApi' : bld})
+scalarmathmodule_src = env.GenerateFromTemplate(
+                    pjoin(env['build_dir'], 'src', 'scalarmathmodule'), 
+                    pjoin(env['src_dir'], 'src', 'scalarmathmodule.c.src'))
 
-bld = Builder(action = generate_from_template, emitter = generate_from_template_emitter)
-env.Append(BUILDERS = {'FromTemplate' : bld})
+umath = env.GenerateUmath(
+            pjoin(env['build_dir'], '__umath_generated'), 
+            pjoin(env['src_dir'], 'code_generators', 'generate_umath.py'))
 
-#------------------------
-# Generate generated code
-#------------------------
-config_header = env.GenerateConfigHeader(pjoin(env['build_dir'], 'config.h'), [])
+multiarray_api = env.GenerateMultiarrayApi(
+                        pjoin(env['build_dir'], 'multiarray_api'), 
+                        [ pjoin(env['src_dir'], 'code_generators', 
+                                'array_api_order.txt'),
+                          pjoin(env['src_dir'], 'code_generators', 
+                                'multiarray_api_order.txt')])
 
-scalartypes_src = env.FromTemplate(pjoin(env['build_dir'], 'src', 'scalartypes'), 
-                                   pjoin(env['src_dir'], 'src', 'scalartypes.inc.src'))
-arraytypes_src = env.FromTemplate(pjoin(env['build_dir'], 'src', 'arraytypes'), 
-                                   pjoin(env['src_dir'], 'src', 'arraytypes.inc.src'))
-sortmodule_src = env.FromTemplate(pjoin(env['build_dir'], 'src', '_sortmodule'), 
-                                   pjoin(env['src_dir'], 'src', '_sortmodule.c.src'))
-umathmodule_src = env.FromTemplate(pjoin(env['build_dir'], 'src', 'umathmodule'), 
-                                   pjoin(env['src_dir'], 'src', 'umathmodule.c.src'))
-scalarmathmodule_src = env.FromTemplate(pjoin(env['build_dir'], 'src', 
-                                              'scalarmathmodule'), 
-                                        pjoin(env['src_dir'], 'src', 
-                                              'scalarmathmodule.c.src'))
+ufunc_api = env.GenerateUfuncApi(
+                    pjoin(env['build_dir'], 'ufunc_api'), 
+                    pjoin(env['src_dir'], 'code_generators', 'ufunc_api_order.txt'))
 
-umath = env.GenerateUmath(pjoin(env['build_dir'], '__umath_generated'), 
-        pjoin(env['src_dir'], 'code_generators', 'generate_umath.py'))
-
-multiarray_api = env.GenerateApi(pjoin(env['build_dir'], 'multiarray_api'), 
-    [pjoin(env['src_dir'], 'code_generators', 'array_api_order.txt'),
-    pjoin(env['src_dir'], 'code_generators', 'multiarray_api_order.txt')])
-ufunc_api = env.GenerateUfuncApi(pjoin(env['build_dir'], 'ufunc_api'), 
-    pjoin(env['src_dir'], 'code_generators', 'ufunc_api_order.txt'))
-
 # XXX
 env.Append(CCFLAGS = "-Wall -fno-strict-aliasing")
 
+env.Append(CPPPATH = [pjoin(env['src_dir'], 'include'), env['build_dir']])
+
 #-----------------
 # Build multiarray
 #-----------------
-env.Append(CPPPATH = [pjoin(env['src_dir'], 'include'), env['build_dir']])
-multiarray_src = [
-    pjoin('src', 'multiarraymodule.c'),
-]
+multiarray_src = [pjoin('src', 'multiarraymodule.c')]
 multiarray = env.NumpyPythonExtension('multiarray', source = multiarray_src)
 
 #------------------

Modified: branches/numpy.scons/numpy/core/scons_support.py
===================================================================
--- branches/numpy.scons/numpy/core/scons_support.py	2007-10-18 08:16:23 UTC (rev 4228)
+++ branches/numpy.scons/numpy/core/scons_support.py	2007-10-18 08:18:22 UTC (rev 4229)
@@ -1,160 +1,82 @@
 from os.path import join as pjoin, dirname as pdirname, basename as pbasename
 
 from code_generators.generate_array_api import \
-        types, h_template as multiarray_h_template, \
-        c_template as multiarray_c_template
+     do_generate_api as nowrap_do_generate_array_api
 from code_generators.generate_ufunc_api import \
-        h_template as ufunc_h_template, \
-        c_template as ufunc_c_template
-import code_generators.genapi as genapi
+     do_generate_api as nowrap_do_generate_ufunc_api
 
-import SCons.Errors
+from numpy.distutils.conv_template import process_str
 
-# XXX: better refactor code_generators.generate* functions, and use them
-# directly
-def do_generate_api(target, source, env):
-    """source has to be a sequence of OBJECT, MULTIARRAY txt files."""
-    h_file = str(target[0])
-    c_file = str(target[1])
-    t_file = str(target[2])
+def split_ext(string):
+    sp = string.rsplit('.', 1)
+    if len(sp) == 1:
+        return (sp[0], '')
+    else:
+        return sp
+#------------------------------------
+# Ufunc and multiarray API generators
+#------------------------------------
+def do_generate_array_api(target, source, env):
+    nowrap_do_generate_array_api([str(i) for i in target], 
+                                 [str(i) for i in source])
+    return 0
 
-    if not len(source) == 2:
-        # XXX
-        assert 0 == 1
-    OBJECT_API_ORDER = str(source[0])
-    MULTIARRAY_API_ORDER = str(source[1])
-    objectapi_list = genapi.get_api_functions('OBJECT_API',
-                                              OBJECT_API_ORDER)
-    multiapi_list = genapi.get_api_functions('MULTIARRAY_API',
-                                             MULTIARRAY_API_ORDER)
-    # API fixes for __arrayobject_api.h
-
-    fixed = 10
-    numtypes = len(types) + fixed
-    numobject = len(objectapi_list) + numtypes
-    nummulti = len(multiapi_list)
-    numtotal = numobject + nummulti
-
-    module_list = []
-    extension_list = []
-    init_list = []
-
-    # setup types
-    for k, atype in enumerate(types):
-        num = fixed + k
-        astr = "        (void *) &Py%sArrType_Type," % types[k]
-        init_list.append(astr)
-        astr = "static PyTypeObject Py%sArrType_Type;" % types[k]
-        module_list.append(astr)
-        astr = "#define Py%sArrType_Type (*(PyTypeObject *)PyArray_API[%d])" % \
-               (types[k], num)
-        extension_list.append(astr)
-
-    # set up object API
-    genapi.add_api_list(numtypes, 'PyArray_API', objectapi_list,
-                        module_list, extension_list, init_list)
-
-    # set up multiarray module API
-    genapi.add_api_list(numobject, 'PyArray_API', multiapi_list,
-                        module_list, extension_list, init_list)
-
-
-    # Write to header
-    fid = open(h_file, 'w')
-    s = multiarray_h_template % ('\n'.join(module_list), '\n'.join(extension_list))
-    fid.write(s)
-    fid.close()
-
-    # Write to c-code
-    fid = open(c_file, 'w')
-    s = multiarray_c_template % '\n'.join(init_list)
-    fid.write(s)
-    fid.close()
-
-    # write to documentation
-    fid = open(t_file, 'w')
-    fid.write('''
-===========
-Numpy C-API
-===========
-
-Object API
-==========
-''')
-    for func in objectapi_list:
-        fid.write(func.to_ReST())
-        fid.write('\n\n')
-    fid.write('''
-
-Multiarray API
-==============
-''')
-    for func in multiapi_list:
-        fid.write(func.to_ReST())
-        fid.write('\n\n')
-    fid.close()
-
+def do_generate_ufunc_api(target, source, env):
+    nowrap_do_generate_ufunc_api([str(i) for i in target], 
+                                 [str(i) for i in source])
     return 0
 
 def generate_api_emitter(target, source, env):
-    """Returns the list of targets generated by the code generator for array api."""
-    base, ext = SCons.Util.splitext(str(target[0]))
+    """Returns the list of targets generated by the code generator for array
+    api and ufunc api."""
+    base, ext = split_ext(str(target[0]))
     dir = pdirname(base)
     ba = pbasename(base)
     h = pjoin(dir, '__' + ba + '.h')
     c = pjoin(dir, '__' + ba + '.c')
     txt = base + '.txt'
-    print h, c, txt
+    #print h, c, txt
     t = [h, c, txt]
     return (t, source)
 
-def do_generate_ufunc_api(target, source, env):
-    """source has to be a txt file."""
-    h_file = str(target[0])
-    c_file = str(target[1])
-    d_file = str(target[2])
+#-------------------------
+# From template generators
+#-------------------------
+# XXX: this is general and can be used outside numpy.core.
+def do_generate_from_template(targetfile, sourcefile, env):
+    t = open(targetfile, 'w')
+    s = open(sourcefile, 'r')
+    allstr = s.read()
+    s.close()
+    writestr = process_str(allstr)
+    t.write(writestr)
+    t.close()
+    return 0
 
-    targets = (h_file, c_file, d_file)
+def generate_from_template(target, source, env):
+    for t, s in zip(target, source):
+        do_generate_from_template(str(t), str(s), env)
 
-    UFUNC_API_ORDER = str(source[0])
-    ufunc_api_list = genapi.get_api_functions('UFUNC_API', UFUNC_API_ORDER)
+def generate_from_template_emitter(target, source, env):
+    base, ext = split_ext(pbasename(str(source[0])))
+    t = pjoin(pdirname(str(target[0])), base)
+    return ([t], source)
+    
+#----------------
+# umath generator
+#----------------
+def do_generate_umath(targetfile, sourcefile, env):
+    t = open(targetfile, 'w')
+    from code_generators import generate_umath
+    code = generate_umath.make_code(generate_umath.defdict, generate_umath.__file__)
+    t.write(code)
+    t.close()
 
-    # API fixes for __arrayobject_api.h
+def generate_umath(target, source, env):
+    for t, s in zip(target, source):
+        do_generate_umath(str(t), str(s), env)
 
-    fixed = 1
-    nummulti = len(ufunc_api_list)
-    numtotal = fixed + nummulti
-
-    module_list = []
-    extension_list = []
-    init_list = []
-
-    # set up object API
-    genapi.add_api_list(fixed, 'PyUFunc_API', ufunc_api_list,
-                        module_list, extension_list, init_list)
-
-    # Write to header
-    fid = open(h_file, 'w')
-    s = ufunc_h_template % ('\n'.join(module_list), '\n'.join(extension_list))
-    fid.write(s)
-    fid.close()
-
-    # Write to c-code
-    fid = open(c_file, 'w')
-    s = ufunc_c_template % '\n'.join(init_list)
-    fid.write(s)
-    fid.close()
-
-    # Write to documentation
-    fid = open(d_file, 'w')
-    fid.write('''
-=================
-Numpy Ufunc C-API
-=================
-''')
-    for func in ufunc_api_list:
-        fid.write(func.to_ReST())
-        fid.write('\n\n')
-    fid.close()
-
-    return 0
+def generate_umath_emitter(target, source, env):
+    t = str(target[0]) + '.c'
+    return ([t], source)
+    



More information about the Numpy-svn mailing list