From 1e0f3744f88628008e66e5ba055ef6cf2ea2c479 Mon Sep 17 00:00:00 2001 From: "Meredith L. Patterson" Date: Tue, 26 Nov 2013 18:12:28 -0800 Subject: [PATCH 1/5] my god, it's a distutils script --- src/bindings/python/setup.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/bindings/python/setup.py diff --git a/src/bindings/python/setup.py b/src/bindings/python/setup.py new file mode 100644 index 0000000..362b252 --- /dev/null +++ b/src/bindings/python/setup.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python + +""" +setup.py for Hammer bindings +""" + +from distutils.core import setup, Extension + +setup(name="hammer", + version="0.9.0", + author="Upstanding Hackers, LLC", + description="""The Hammer parser combinator library""", + ext_modules=[Extension('_hammer', ['hammer.i'], + swig_opts=['-DHAMMER_INTERNAL__NO_STDARG_H', '-I../../'], + define_macros = [('SWIG', None)], + extra_compile_args = ['-fPIC', + '-std=gnu99',], + include_dirs=['../../'], + library_dirs=['../../'], + libraries=['hammer'],)], + py_modules=['hammer'], +) From 53bc886e2f1f0bb3d91d3ecff0e67e6ac1a1c9b4 Mon Sep 17 00:00:00 2001 From: "Meredith L. Patterson" Date: Tue, 26 Nov 2013 19:09:27 -0800 Subject: [PATCH 2/5] distutils being invoked from scons; ld is failing with 'libhammer.a: archive has no index, run ranlib to add one' --- src/bindings/python/SConscript | 20 +++++++++++--------- src/bindings/python/setup.py | 24 ++++++++++++++++-------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/bindings/python/SConscript b/src/bindings/python/SConscript index 0f1e9a9..0a905b7 100644 --- a/src/bindings/python/SConscript +++ b/src/bindings/python/SConscript @@ -4,18 +4,20 @@ Import('env libhammer_shared testruns') pythonenv = env.Clone(IMPLICIT_COMMAND_DEPENDENCIES = 0) -pythonenv.Append(CPPPATH = ['../../']) -pythonenv.Append(CCFLAGS = ['-fpic', '-DSWIG', '-Wno-all', '-Wno-extra', '-Wno-error']) -pythonenv.ParseConfig("pkg-config --cflags python") -pythonenv.Append(LIBS = ['hammer']) -pythonenv.Append(LIBPATH = ['../../']) -pythonenv.Append(SWIGFLAGS = ['-DHAMMER_INTERNAL__NO_STDARG_H', '-Isrc/', '-python']) +#pythonenv.Append(CPPPATH = ['../../']) +#pythonenv.Append(CCFLAGS = ['-fpic', '-DSWIG', '-Wno-all', '-Wno-extra', '-Wno-error']) +#pythonenv.ParseConfig("pkg-config --cflags python") +#pythonenv.Append(LIBS = ['hammer']) +#pythonenv.Append(LIBPATH = ['../../']) +#pythonenv.Append(SWIGFLAGS = ['-DHAMMER_INTERNAL__NO_STDARG_H', '-Isrc/', '-python']) -pythonenv.Command("hammer.i", "../swig/hammer.i", Copy("$TARGET", "$SOURCE")) -swig = ['hammer.i'] -libhammer_python = pythonenv.SharedLibrary('hammer', swig, SHLIBPREFIX='_') +swig = pythonenv.Command("hammer.i", "../swig/hammer.i", Copy("$TARGET", "$SOURCE")) +setup = ['setup.py'] +pydir = os.path.join(env['BUILD_BASE'], 'src/bindings/python') +#libhammer_python = pythonenv.SharedLibrary('hammer', swig, SHLIBPREFIX='_') +libhammer_python = pythonenv.Command(None, [swig, setup], 'python ' + os.path.join(pydir, 'setup.py') + ' build_ext --swig-opts="-outdir ' + pydir + ' -DHAMMER_INTERNAL__NO_STDARG_H -Isrc"') Default(libhammer_python) pytestenv = pythonenv.Clone() diff --git a/src/bindings/python/setup.py b/src/bindings/python/setup.py index 362b252..2f42fa0 100644 --- a/src/bindings/python/setup.py +++ b/src/bindings/python/setup.py @@ -3,20 +3,28 @@ """ setup.py for Hammer bindings """ - +import os.path, sys from distutils.core import setup, Extension setup(name="hammer", version="0.9.0", author="Upstanding Hackers, LLC", + author_email="hammer@upstandinghackers.com", + url="https://github.com/UpstandingHackers/hammer", description="""The Hammer parser combinator library""", - ext_modules=[Extension('_hammer', ['hammer.i'], - swig_opts=['-DHAMMER_INTERNAL__NO_STDARG_H', '-I../../'], - define_macros = [('SWIG', None)], - extra_compile_args = ['-fPIC', - '-std=gnu99',], - include_dirs=['../../'], - library_dirs=['../../'], + ext_modules=[Extension('_hammer', [os.path.join(os.path.dirname(sys.argv[0]), + 'hammer.i')], + # swig_opts is set by SConscript + define_macros=[('SWIG', None)], + depends=['allocator.h', + 'glue.h', + 'hammer.h', + 'internal.h',], + extra_compile_args=['-fPIC', + '-std=gnu99',], + include_dirs=['src'], + library_dirs=['src'], libraries=['hammer'],)], + py_modules=['hammer'], ) From 20bf8c4b797eb20a9c6c94a75c90423e75ce2f5e Mon Sep 17 00:00:00 2001 From: "Meredith L. Patterson" Date: Tue, 26 Nov 2013 19:25:41 -0800 Subject: [PATCH 3/5] distutils and working tests! --- src/bindings/python/SConscript | 4 ++-- src/bindings/python/setup.py | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/bindings/python/SConscript b/src/bindings/python/SConscript index 0a905b7..dcea205 100644 --- a/src/bindings/python/SConscript +++ b/src/bindings/python/SConscript @@ -17,13 +17,13 @@ swig = pythonenv.Command("hammer.i", "../swig/hammer.i", Copy("$TARGET", "$SOURC setup = ['setup.py'] pydir = os.path.join(env['BUILD_BASE'], 'src/bindings/python') #libhammer_python = pythonenv.SharedLibrary('hammer', swig, SHLIBPREFIX='_') -libhammer_python = pythonenv.Command(None, [swig, setup], 'python ' + os.path.join(pydir, 'setup.py') + ' build_ext --swig-opts="-outdir ' + pydir + ' -DHAMMER_INTERNAL__NO_STDARG_H -Isrc"') +libhammer_python = pythonenv.Command(['hammer.py', 'hammer_wrap.c'], [swig, setup], 'python ' + os.path.join(pydir, 'setup.py') + ' build_ext --inplace') Default(libhammer_python) pytestenv = pythonenv.Clone() pytestenv['ENV']['LD_LIBRARY_PATH'] = os.path.dirname(str(libhammer_shared[0])) pytests = ['hammer_tests.py'] -pytestexec = pytestenv.Command(['hammer.pyc', 'hammer_tests.pyc'], pytests + libhammer_python, "nosetests -vv $SOURCE") +pytestexec = pytestenv.Command(['hammer.pyc', 'hammer_tests.pyc'], pytests + libhammer_python, "LD_LIBRARY_PATH=" + os.path.dirname(str(libhammer_shared[0])) + " nosetests -vv $SOURCE") pytest = Alias("testpython", [pytestexec], pytestexec) AlwaysBuild(pytest) testruns.append(pytest) diff --git a/src/bindings/python/setup.py b/src/bindings/python/setup.py index 2f42fa0..720590e 100644 --- a/src/bindings/python/setup.py +++ b/src/bindings/python/setup.py @@ -3,18 +3,22 @@ """ setup.py for Hammer bindings """ -import os.path, sys +import os, os.path, sys from distutils.core import setup, Extension +invoked = os.getcwd() +os.chdir(os.path.dirname(sys.argv[0])) + setup(name="hammer", version="0.9.0", author="Upstanding Hackers, LLC", author_email="hammer@upstandinghackers.com", url="https://github.com/UpstandingHackers/hammer", description="""The Hammer parser combinator library""", - ext_modules=[Extension('_hammer', [os.path.join(os.path.dirname(sys.argv[0]), - 'hammer.i')], - # swig_opts is set by SConscript + ext_modules=[Extension('_hammer', ['hammer.i'], + swig_opts=['-DHAMMER_INTERNAL__NO_STDARG_H', + #('-outdir', os.getcwd()), + '-I../../'], define_macros=[('SWIG', None)], depends=['allocator.h', 'glue.h', @@ -22,9 +26,11 @@ setup(name="hammer", 'internal.h',], extra_compile_args=['-fPIC', '-std=gnu99',], - include_dirs=['src'], - library_dirs=['src'], + include_dirs=['../../'], + library_dirs=['../../'], libraries=['hammer'],)], py_modules=['hammer'], ) + +os.chdir(invoked) From 79f498cdab10931a66139d5cd048e714b5248f2e Mon Sep 17 00:00:00 2001 From: "Meredith L. Patterson" Date: Tue, 26 Nov 2013 19:54:09 -0800 Subject: [PATCH 4/5] python bindings install target works. libhammer.so must be on LD_LIBRARY_PATH. --- SConstruct | 14 +++++++------- src/bindings/python/SConscript | 16 ++++------------ 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/SConstruct b/SConstruct index e30f6df..81bc849 100644 --- a/SConstruct +++ b/SConstruct @@ -95,9 +95,15 @@ env["ENV"].update(x for x in os.environ.items() if x[0].startswith("CCC_")) #env.Append(CPPPATH=os.path.join('#', "hammer")) testruns = [] +targets = ["$libpath", + "$incpath", + "$parsersincpath", + "$backendsincpath", + "$pkgconfigpath"] Export('env') Export('testruns') +Export('targets') if not GetOption("in_place"): env['BUILD_BASE'] = 'build/$VARIANT' @@ -108,12 +114,6 @@ else: lib = env.SConscript(["src/SConscript"]) env.Alias(env.SConscript(["examples/SConscript"])) -#env.Command('test', '$BUILD_BASE/src/test_suite', 'env LD_LIBRARY_PATH=$BUILD_BASE/src $SOURCE') - env.Alias("test", testruns) -env.Alias("install", "$libpath") -env.Alias("install", "$incpath") -env.Alias("install", "$parsersincpath") -env.Alias("install", "$backendsincpath") -env.Alias("install", "$pkgconfigpath") +env.Alias("install", targets) diff --git a/src/bindings/python/SConscript b/src/bindings/python/SConscript index dcea205..d91a942 100644 --- a/src/bindings/python/SConscript +++ b/src/bindings/python/SConscript @@ -1,22 +1,12 @@ # -*- python -*- import os, os.path -Import('env libhammer_shared testruns') +Import('env libhammer_shared testruns targets') pythonenv = env.Clone(IMPLICIT_COMMAND_DEPENDENCIES = 0) -#pythonenv.Append(CPPPATH = ['../../']) -#pythonenv.Append(CCFLAGS = ['-fpic', '-DSWIG', '-Wno-all', '-Wno-extra', '-Wno-error']) -#pythonenv.ParseConfig("pkg-config --cflags python") -#pythonenv.Append(LIBS = ['hammer']) -#pythonenv.Append(LIBPATH = ['../../']) -#pythonenv.Append(SWIGFLAGS = ['-DHAMMER_INTERNAL__NO_STDARG_H', '-Isrc/', '-python']) - - - swig = pythonenv.Command("hammer.i", "../swig/hammer.i", Copy("$TARGET", "$SOURCE")) setup = ['setup.py'] pydir = os.path.join(env['BUILD_BASE'], 'src/bindings/python') -#libhammer_python = pythonenv.SharedLibrary('hammer', swig, SHLIBPREFIX='_') libhammer_python = pythonenv.Command(['hammer.py', 'hammer_wrap.c'], [swig, setup], 'python ' + os.path.join(pydir, 'setup.py') + ' build_ext --inplace') Default(libhammer_python) @@ -28,4 +18,6 @@ pytest = Alias("testpython", [pytestexec], pytestexec) AlwaysBuild(pytest) testruns.append(pytest) - +pyinstallexec = pythonenv.Command(None, libhammer_python, 'python ' + os.path.join(pydir, 'setup.py ') + ' install') +pyinstall = Alias("installpython", [pyinstallexec], pyinstallexec) +targets.append(pyinstall) From f58ec8ecb7ca7358af58084d5fc0764767718b41 Mon Sep 17 00:00:00 2001 From: "Meredith L. Patterson" Date: Tue, 26 Nov 2013 20:00:55 -0800 Subject: [PATCH 5/5] tidying and a README about this part of the build --- src/bindings/python/README.md | 1 + src/bindings/python/setup.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 src/bindings/python/README.md diff --git a/src/bindings/python/README.md b/src/bindings/python/README.md new file mode 100644 index 0000000..bdbf95e --- /dev/null +++ b/src/bindings/python/README.md @@ -0,0 +1 @@ +The distutils script in this directory is meant to be invoked from scons. It will work if you build it here (`python setup.py build_ext`, optionally `--inplace` as scons invokes it), but we haven't done much testing of it outside the build system and don't really intend to. YHBW. \ No newline at end of file diff --git a/src/bindings/python/setup.py b/src/bindings/python/setup.py index 720590e..308283c 100644 --- a/src/bindings/python/setup.py +++ b/src/bindings/python/setup.py @@ -7,7 +7,8 @@ import os, os.path, sys from distutils.core import setup, Extension invoked = os.getcwd() -os.chdir(os.path.dirname(sys.argv[0])) +if (os.path.dirname(sys.argv[0]) != ''): + os.chdir(os.path.dirname(sys.argv[0])) setup(name="hammer", version="0.9.0", @@ -17,7 +18,6 @@ setup(name="hammer", description="""The Hammer parser combinator library""", ext_modules=[Extension('_hammer', ['hammer.i'], swig_opts=['-DHAMMER_INTERNAL__NO_STDARG_H', - #('-outdir', os.getcwd()), '-I../../'], define_macros=[('SWIG', None)], depends=['allocator.h',