|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 | 15 | import os
|
16 |
| -from setuptools import setup |
17 |
| -from setuptools import Extension |
| 16 | +import re |
| 17 | +import setuptools |
18 | 18 | from setuptools.command.build_ext import build_ext as BuildExt
|
19 |
| -from subprocess import Popen |
20 | 19 |
|
21 | 20 | DIR = os.path.abspath(os.path.dirname(__file__))
|
22 | 21 | # NOTE: If you are editing the array below then you probably also need
|
23 | 22 | # to change MANIFEST.in.
|
24 |
| -LIB_OBJECTS = [ |
25 |
| - 'core/desugarer.o', |
26 |
| - 'core/formatter.o', |
27 |
| - 'core/libjsonnet.o', |
28 |
| - 'core/lexer.o', |
29 |
| - 'core/parser.o', |
30 |
| - 'core/pass.o', |
31 |
| - 'core/static_analysis.o', |
32 |
| - 'core/string_utils.o', |
33 |
| - 'core/vm.o', |
34 |
| - 'third_party/md5/md5.o', |
35 |
| - 'third_party/rapidyaml/rapidyaml.o', |
| 23 | +LIB_SOURCES = [ |
| 24 | + "core/desugarer.cpp", |
| 25 | + "core/formatter.cpp", |
| 26 | + "core/libjsonnet.cpp", |
| 27 | + "core/lexer.cpp", |
| 28 | + "core/parser.cpp", |
| 29 | + "core/pass.cpp", |
| 30 | + "core/static_analysis.cpp", |
| 31 | + "core/string_utils.cpp", |
| 32 | + "core/vm.cpp", |
| 33 | + "third_party/md5/md5.cpp", |
| 34 | + "third_party/rapidyaml/rapidyaml.cpp", |
| 35 | + "python/_jsonnet.c", |
36 | 36 | ]
|
37 | 37 |
|
38 |
| -MODULE_SOURCES = ['python/_jsonnet.c'] |
39 | 38 |
|
40 | 39 | def get_version():
|
41 | 40 | """
|
42 | 41 | Parses the version out of libjsonnet.h
|
43 | 42 | """
|
44 |
| - with open(os.path.join(DIR, 'include/libjsonnet.h')) as f: |
| 43 | + rx = re.compile(r'^\s*#\s*define\s+LIB_JSONNET_VERSION\s+"v([0-9.]+)"\s*$') |
| 44 | + with open(os.path.join(DIR, "include/libjsonnet.h")) as f: |
45 | 45 | for line in f:
|
46 |
| - if '#define' in line and 'LIB_JSONNET_VERSION' in line: |
47 |
| - v_code = line.partition('LIB_JSONNET_VERSION')[2].strip('\n "') |
48 |
| - if v_code[0] == "v": |
49 |
| - v_code = v_code[1:] |
50 |
| - return v_code |
| 46 | + m = rx.match(line) |
| 47 | + if m: |
| 48 | + return m.group(1) |
| 49 | + raise Exception( |
| 50 | + "could not find LIB_JSONNET_VERSION definition in include/libjsonnet.h" |
| 51 | + ) |
| 52 | + |
51 | 53 |
|
52 | 54 | class BuildJsonnetExt(BuildExt):
|
| 55 | + def _pack_std_jsonnet(self): |
| 56 | + print("generating core/std.jsonnet.h from stdlib/std.jsonnet") |
| 57 | + with open("stdlib/std.jsonnet", "rb") as f: |
| 58 | + stdlib = f.read() |
| 59 | + with open("core/std.jsonnet.h", "w", encoding="utf-8") as f: |
| 60 | + f.write(",".join(str(x) for x in stdlib)) |
| 61 | + f.write(",0\n\n") |
| 62 | + |
53 | 63 | def run(self):
|
54 |
| - p = Popen(['make'] + LIB_OBJECTS, cwd=DIR) |
55 |
| - p.wait() |
56 |
| - if p.returncode != 0: |
57 |
| - raise Exception('Could not build %s' % (', '.join(LIB_OBJECTS))) |
58 |
| - BuildExt.run(self) |
| 64 | + self._pack_std_jsonnet() |
| 65 | + super().run() |
59 | 66 |
|
60 |
| -jsonnet_ext = Extension( |
61 |
| - '_jsonnet', |
62 |
| - sources=MODULE_SOURCES, |
63 |
| - extra_objects=LIB_OBJECTS, |
64 |
| - include_dirs = ['include'], |
65 |
| - language='c++' |
66 |
| -) |
67 | 67 |
|
68 |
| -setup(name='jsonnet', |
69 |
| - url='https://jsonnet.org', |
70 |
| - project_urls={ |
71 |
| - 'Source': 'https://github.com/google/jsonnet', |
72 |
| - }, |
73 |
| - description='Python bindings for Jsonnet - The data templating language ', |
74 |
| - license="Apache License 2.0", |
75 |
| - author='David Cunningham', |
76 |
| - |
77 |
| - version=get_version(), |
78 |
| - cmdclass={ |
79 |
| - 'build_ext': BuildJsonnetExt, |
80 |
| - }, |
81 |
| - ext_modules=[jsonnet_ext], |
82 |
| - test_suite="python._jsonnet_test", |
| 68 | +setuptools.setup( |
| 69 | + name="jsonnet", |
| 70 | + url="https://jsonnet.org", |
| 71 | + project_urls={ |
| 72 | + "Source": "https://github.com/google/jsonnet", |
| 73 | + }, |
| 74 | + description="Python bindings for Jsonnet - The data templating language ", |
| 75 | + license="Apache License 2.0", |
| 76 | + author="David Cunningham", |
| 77 | + |
| 78 | + version=get_version(), |
| 79 | + cmdclass={ |
| 80 | + "build_ext": BuildJsonnetExt, |
| 81 | + }, |
| 82 | + ext_modules=[ |
| 83 | + setuptools.Extension( |
| 84 | + "_jsonnet", |
| 85 | + sources=LIB_SOURCES, |
| 86 | + include_dirs=[ |
| 87 | + "include", |
| 88 | + "third_party/md5", |
| 89 | + "third_party/json", |
| 90 | + "third_party/rapidyaml", |
| 91 | + ], |
| 92 | + extra_compile_args=["-std=c++17"], |
| 93 | + language="c++", |
| 94 | + ) |
| 95 | + ], |
| 96 | + test_suite="python._jsonnet_test", |
83 | 97 | )
|
0 commit comments