You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2016/02/09 18:11:28 UTC

[11/12] lucy-clownfish git commit: Build py C extension for runtime.

Build py C extension for runtime.


Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/bebd2447
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/bebd2447
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/bebd2447

Branch: refs/heads/master
Commit: bebd2447680dc23be903dfb8000158725699014d
Parents: 49361b7
Author: Marvin Humphrey <ma...@rectangular.com>
Authored: Tue Jan 19 20:44:50 2016 -0800
Committer: Marvin Humphrey <ma...@rectangular.com>
Committed: Sat Feb 6 10:23:21 2016 -0800

----------------------------------------------------------------------
 runtime/python/setup.py                  | 67 +++++++++++++++++++++++++--
 runtime/python/src/clownfish/__init__.py |  3 ++
 2 files changed, 66 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/bebd2447/runtime/python/setup.py
----------------------------------------------------------------------
diff --git a/runtime/python/setup.py b/runtime/python/setup.py
index fdaa593..dbc41f5 100644
--- a/runtime/python/setup.py
+++ b/runtime/python/setup.py
@@ -13,7 +13,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from distutils.core import setup
+from distutils.core import setup, Extension
 from distutils.command.build import build as _build
 from distutils.command.clean import clean as _clean
 from distutils.cmd import Command as _Command
@@ -21,16 +21,25 @@ from distutils.dep_util import newer_group
 import distutils.ccompiler
 import os
 import glob
+import platform
 import shutil
 import subprocess
 import sysconfig
 import sys
 import unittest
 
+def ext_build_dir(base):
+    """Returns the build directory for compiled extensions"""
+    pattern = "lib.{platform}-{version[0]}.{version[1]}"
+    dirname = pattern.format(platform=sysconfig.get_platform(),
+                             version=sys.version_info)
+    return os.path.join(base, 'build', dirname)
+
 # Get a compiler object and and strings representing the compiler type and
 # CFLAGS.  Add the Python headers include dir to CFLAGS.
 compiler = distutils.ccompiler.new_compiler()
 cflags = sysconfig.get_config_var('CFLAGS')
+cflags = cflags + " -I" + sysconfig.get_path('include')
 compiler_type = distutils.ccompiler.get_default_compiler()
 
 # There's no public way to get a string representing the compiler executable
@@ -48,12 +57,20 @@ CHARMONIZER_C        = os.path.join(COMMON_SOURCE_DIR, 'charmonizer.c')
 CHARMONIZER_EXE_NAME = compiler.executable_filename('charmonizer')
 CHARMONIZER_EXE_PATH = os.path.join(os.curdir, CHARMONIZER_EXE_NAME)
 CHARMONY_H_PATH      = 'charmony.h'
-
-c_filepaths = []
+LIBCLOWNFISH_NAME    = 'libclownfish.a' # TODO portability
+LIBCLOWNFISH_PATH    = os.path.abspath(os.path.join(os.curdir, LIBCLOWNFISH_NAME))
+AUTOGEN_INCLUDE      = os.path.join('autogen', 'include')
+CFC_DIR              = os.path.join(BASE_DIR, 'compiler', 'python')
+CFC_BUILD_DIR        = ext_build_dir(os.path.join(CFC_DIR))
+PY_BINDING_DIR       = os.path.abspath(os.curdir)
+BINDING_FILE         = '_clownfish.c'
+
+c_filepaths = [BINDING_FILE]
 paths_to_clean = [
     CHARMONIZER_EXE_PATH,
     CHARMONY_H_PATH,
     '_charm*',
+    BINDING_FILE,
 ]
 
 def _quotify(text):
@@ -97,6 +114,35 @@ class charmony(_Command):
             print(" ".join(command))
             subprocess.check_call(command)
 
+class libclownfish(_Command):
+    description = "Build the Clownfish runtime core as a static archive."
+    user_options = []
+    def initialize_options(self):
+        pass
+    def finalize_options(self):
+        pass
+
+    def run(self):
+        self.run_command('charmony')
+        self.run_cfc()
+        subprocess.check_call([make_command, '-j', 'static'])
+
+    def run_cfc(self):
+        sys.path.append(CFC_DIR)
+        sys.path.append(CFC_BUILD_DIR)
+        import cfc
+        hierarchy = cfc.model.Hierarchy(dest="autogen")
+        hierarchy.add_source_dir(CORE_SOURCE_DIR)
+        hierarchy.build()
+        header = "Autogenerated by setup.py"
+        core_binding = cfc.binding.BindCore(hierarchy=hierarchy, header=header)
+        modified = core_binding.write_all_modified()
+        if modified:
+            py_binding = cfc.binding.Python(hierarchy=hierarchy)
+            py_binding.set_header(header)
+            py_binding.write_bindings(parcel="Clownfish", dest=PY_BINDING_DIR)
+            hierarchy.write_log()
+
 class my_clean(_clean):
     def run(self):
         _clean.run(self)
@@ -113,6 +159,7 @@ class my_clean(_clean):
 class my_build(_build):
     def run(self):
         self.run_command('charmony')
+        self.run_command('libclownfish')
         _build.run(self)
 
 class test(_Command):
@@ -142,6 +189,16 @@ class test(_Command):
         # restore sys.path
         sys.path = orig_sys_path
 
+clownfish_extension = Extension('clownfish._clownfish',
+                                 include_dirs = [
+                                    CORE_SOURCE_DIR,
+                                    AUTOGEN_INCLUDE,
+                                    CFEXT_DIR,
+                                    os.curdir,
+                                 ],
+                                 extra_link_args = [LIBCLOWNFISH_PATH],
+                                 sources = c_filepaths)
+
 setup(name = 'clownfish',
       version = '0.4.0',
       description = 'Clownfish runtime',
@@ -154,7 +211,9 @@ setup(name = 'clownfish',
           'build': my_build,
           'clean': my_clean,
           'charmony': charmony,
+          'libclownfish': libclownfish,
           'test': test,
       },
-      package_dir={'': 'src'},)
+      package_dir={'': 'src'},
+      ext_modules = [clownfish_extension])
 

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/bebd2447/runtime/python/src/clownfish/__init__.py
----------------------------------------------------------------------
diff --git a/runtime/python/src/clownfish/__init__.py b/runtime/python/src/clownfish/__init__.py
index 09697dc..5c3afae 100644
--- a/runtime/python/src/clownfish/__init__.py
+++ b/runtime/python/src/clownfish/__init__.py
@@ -13,3 +13,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import clownfish._clownfish
+from clownfish._clownfish import *
+