-
Notifications
You must be signed in to change notification settings - Fork 440
/
setup.py
112 lines (100 loc) · 3.49 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import re
import setuptools
from setuptools.command.build_ext import build_ext as BuildExt
DIR = os.path.abspath(os.path.dirname(__file__))
# NOTE: If you are editing the array below then you probably also need
# to change MANIFEST.in.
LIB_SOURCES = [
"core/desugarer.cpp",
"core/formatter.cpp",
"core/libjsonnet.cpp",
"core/lexer.cpp",
"core/parser.cpp",
"core/pass.cpp",
"core/path_utils.cpp",
"core/static_analysis.cpp",
"core/string_utils.cpp",
"core/vm.cpp",
"third_party/md5/md5.cpp",
"third_party/rapidyaml/rapidyaml.cpp",
"python/_jsonnet.c",
]
def get_version():
"""
Parses the version out of libjsonnet.h
"""
rx = re.compile(r'^\s*#\s*define\s+LIB_JSONNET_VERSION\s+"v([0-9.]+)"\s*$')
with open(os.path.join(DIR, "include/libjsonnet.h")) as f:
for line in f:
m = rx.match(line)
if m:
return m.group(1)
raise Exception(
"could not find LIB_JSONNET_VERSION definition in include/libjsonnet.h"
)
class BuildJsonnetExt(BuildExt):
def _pack_std_jsonnet(self):
print("generating core/std.jsonnet.h from stdlib/std.jsonnet")
with open("stdlib/std.jsonnet", "rb") as f:
stdlib = f.read()
with open("core/std.jsonnet.h", "w", encoding="utf-8") as f:
f.write(",".join(str(x) for x in stdlib))
f.write(",0\n\n")
def build_extension(self, ext):
# At this point, the compiler has been chosen so we add compiler-specific flags.
# There is unfortunately no built in support for this in setuptools.
# Feature request: https://github.com/pypa/setuptools/issues/1819
print("Setting compile flags for compiler type " + self.compiler.compiler_type)
# This is quite hacky as we're modifying the Extension object itself.
if self.compiler.compiler_type == "msvc":
ext.extra_compile_args.append("/std:c++17")
else:
ext.extra_compile_args.append("-std=c++17")
# Actually build.
super().build_extension(ext)
def run(self):
self._pack_std_jsonnet()
super().run()
setuptools.setup(
name="jsonnet",
url="https://jsonnet.org",
project_urls={
"Source": "https://github.com/google/jsonnet",
},
description="Python bindings for Jsonnet - The data templating language ",
license="Apache License 2.0",
author="David Cunningham",
author_email="[email protected]",
version=get_version(),
cmdclass={
"build_ext": BuildJsonnetExt,
},
ext_modules=[
setuptools.Extension(
"_jsonnet",
sources=LIB_SOURCES,
include_dirs=[
"include",
"third_party/md5",
"third_party/json",
"third_party/rapidyaml",
],
language="c++",
)
],
test_suite="python._jsonnet_test",
)