Skip to content

Commit 22f0689

Browse files
change setup.py to build directly not using the Makefile
The Makefile is problematic because it needs shell and the `od` command. But Python setuptools is perfectly capable of building some .cpp files on its own, so we can use that, it just needs a minor extension to generate core/std.jsonnet.h. Also update MANIFEST.in after the recent changes to third_party: The bundled RapidYAML was switched to the single-header version, and nlohmann json.hpp was moved to a subdirectory.
1 parent b8728ea commit 22f0689

File tree

2 files changed

+65
-51
lines changed

2 files changed

+65
-51
lines changed

MANIFEST.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
include LICENSE include/*.h core/*.cpp core/*.h python/*.c stdlib/std.jsonnet Makefile
2-
include third_party/json/*.hpp
2+
recursive-include third_party/json *.hpp
33
include third_party/md5/*.cpp third_party/md5/*.h
4-
recursive-include third_party/rapidyaml/rapidyaml *.hpp *.cpp *.h
4+
include third_party/rapidyaml/*.cpp third_party/rapidyaml/*.hpp
55
#recursive-include test_suite examples gc_stress benchmarks editors

setup.py

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,71 +13,85 @@
1313
# limitations under the License.
1414

1515
import os
16-
from setuptools import setup
17-
from setuptools import Extension
16+
import re
17+
import setuptools
1818
from setuptools.command.build_ext import build_ext as BuildExt
19-
from subprocess import Popen
2019

2120
DIR = os.path.abspath(os.path.dirname(__file__))
2221
# NOTE: If you are editing the array below then you probably also need
2322
# 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",
3636
]
3737

38-
MODULE_SOURCES = ['python/_jsonnet.c']
3938

4039
def get_version():
4140
"""
4241
Parses the version out of libjsonnet.h
4342
"""
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:
4545
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+
5153

5254
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+
5363
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()
5966

60-
jsonnet_ext = Extension(
61-
'_jsonnet',
62-
sources=MODULE_SOURCES,
63-
extra_objects=LIB_OBJECTS,
64-
include_dirs = ['include'],
65-
language='c++'
66-
)
6767

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-
author_email='[email protected]',
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+
author_email="[email protected]",
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",
8397
)

0 commit comments

Comments
 (0)