Skip to content

Commit

Permalink
python: drop usage of external make for extension build
Browse files Browse the repository at this point in the history
This change takes advantage of setuptools build_ext feature to discover the build toolchain and acompile the extension with no hidden dependency.

Python extension can now be build and installed properly on windows when Visual C++ Build Tools are installed.
  • Loading branch information
Toilal committed Mar 11, 2020
1 parent b819d32 commit 81e806a
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,13 @@
# limitations under the License.

import os
from setuptools import setup

from setuptools import Extension
from setuptools.command.build_ext import build_ext as BuildExt
from subprocess import Popen
from setuptools import setup
from setuptools.command.build_ext import build_ext

DIR = os.path.abspath(os.path.dirname(__file__))
LIB_OBJECTS = [
'core/desugarer.o',
'core/formatter.o',
'core/libjsonnet.o',
'core/lexer.o',
'core/parser.o',
'core/pass.o',
'core/static_analysis.o',
'core/string_utils.o',
'core/vm.o',
'third_party/md5/md5.o'
]

MODULE_SOURCES = ['python/_jsonnet.c']

def get_version():
"""
Expand All @@ -46,19 +33,20 @@ def get_version():
v_code = v_code[1:]
return v_code

class BuildJsonnetExt(BuildExt):
def run(self):
p = Popen(['make'] + LIB_OBJECTS, cwd=DIR)
p.wait()
if p.returncode != 0:
raise Exception('Could not build %s' % (', '.join(LIB_OBJECTS)))
BuildExt.run(self)

jsonnet_ext = Extension(
'_jsonnet',
sources=MODULE_SOURCES,
extra_objects=LIB_OBJECTS,
include_dirs = ['include', 'third_party/md5', 'third_party/json'],
sources=['python/_jsonnet.c',
'core/desugarer.cpp',
'core/formatter.cpp',
'core/libjsonnet.cpp',
'core/lexer.cpp',
'core/parser.cpp',
'core/pass.cpp',
'core/static_analysis.cpp',
'core/string_utils.cpp',
'core/vm.cpp',
'third_party/md5/md5.cpp'],
include_dirs=['core', 'include', 'third_party/md5', 'third_party/json'],
language='c++'
)

Expand All @@ -69,8 +57,8 @@ def run(self):
author_email='dcunnin@google.com',
version=get_version(),
cmdclass={
'build_ext': BuildJsonnetExt,
'build_ext': build_ext,
},
ext_modules=[jsonnet_ext],
test_suite="python._jsonnet_test",
)
)

0 comments on commit 81e806a

Please sign in to comment.