| 1 |
# Copyright 2015 Google Inc. All rights reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
import os |
| 16 |
import re |
| 17 |
import sys |
| 18 |
|
| 19 |
import setuptools |
| 20 |
from setuptools.command.build_ext import build_ext as BuildExt |
| 21 |
|
| 22 |
DIR = os.path.abspath(os.path.dirname(__file__)) |
| 23 |
# NOTE: If you are editing the array below then you probably also need |
| 24 |
# to change MANIFEST.in. |
| 25 |
LIB_SOURCES = [ |
| 26 |
"core/desugarer.cpp", |
| 27 |
"core/formatter.cpp", |
| 28 |
"core/libjsonnet.cpp", |
| 29 |
"core/lexer.cpp", |
| 30 |
"core/parser.cpp", |
| 31 |
"core/pass.cpp", |
| 32 |
"core/path_utils.cpp", |
| 33 |
"core/static_analysis.cpp", |
| 34 |
"core/string_utils.cpp", |
| 35 |
"core/vm.cpp", |
| 36 |
"third_party/md5/md5.cpp", |
| 37 |
"python/_jsonnet.c", |
| 38 |
] |
| 39 |
|
| 40 |
|
| 41 |
def get_version(): |
| 42 |
""" |
| 43 |
Parses the version out of libjsonnet.h |
| 44 |
""" |
| 45 |
rx = re.compile( |
| 46 |
r'^\s*#\s*define\s+LIB_JSONNET_VERSION\s+"v([0-9.]+(?:-?[a-z][a-z0-9]*)?)"\s*$' |
| 47 |
) |
| 48 |
with open(os.path.join(DIR, "include/libjsonnet.h")) as f: |
| 49 |
for line in f: |
| 50 |
m = rx.match(line) |
| 51 |
if m: |
| 52 |
return m.group(1) |
| 53 |
raise Exception( |
| 54 |
"could not find LIB_JSONNET_VERSION definition in include/libjsonnet.h" |
| 55 |
) |
| 56 |
|
| 57 |
|
| 58 |
class BuildJsonnetExt(BuildExt): |
| 59 |
def _pack_std_jsonnet(self): |
| 60 |
print("generating core/std.jsonnet.h from stdlib/std.jsonnet") |
| 61 |
with open("stdlib/std.jsonnet", "rb") as f: |
| 62 |
stdlib = f.read() |
| 63 |
with open("core/std.jsonnet.h", "w", encoding="utf-8") as f: |
| 64 |
f.write(",".join(str(x) for x in stdlib)) |
| 65 |
f.write(",0\n\n") |
| 66 |
|
| 67 |
def build_extensions(self): |
| 68 |
# At this point, the compiler has been chosen so we add compiler-specific flags. |
| 69 |
# There is unfortunately no built in support for this in setuptools. |
| 70 |
# Feature request: https://github.com/pypa/setuptools/issues/1819 |
| 71 |
print("Adjusting compiler for compiler type " + self.compiler.compiler_type) |
| 72 |
# This is quite hacky as we're modifying the Extension object itself. |
| 73 |
if self.compiler.compiler_type == "msvc": |
| 74 |
for ext in self.extensions: |
| 75 |
ext.extra_compile_args.append("/std:c++17") |
| 76 |
else: |
| 77 |
# -std=c++17 should only be applied to C++ build, |
| 78 |
# not when compiling C source code. Unfortunately, |
| 79 |
# the extra_compile_args applies to both. Instead, |
| 80 |
# patch the CC/CXX commands in the compiler object. |
| 81 |
# |
| 82 |
# Note that older versions of distutils/setuptools do not |
| 83 |
# have the necessary separation between C and C++ compilers. |
| 84 |
# This requires setuptools 72.2. |
| 85 |
for v in ("compiler_cxx", "compiler_so_cxx"): |
| 86 |
if not hasattr(self.compiler, v): |
| 87 |
print( |
| 88 |
f"WARNING: cannot adjust flag {v}, " |
| 89 |
f"compiler type {self.compiler.compiler_type}, " |
| 90 |
f"compiler class {type(self.compiler).__name__}" |
| 91 |
) |
| 92 |
continue |
| 93 |
current = getattr(self.compiler, v) |
| 94 |
self.compiler.set_executable(v, current + ["-std=c++17"]) |
| 95 |
super().build_extensions() |
| 96 |
|
| 97 |
def run(self): |
| 98 |
self._pack_std_jsonnet() |
| 99 |
super().run() |
| 100 |
|
| 101 |
|
| 102 |
# Default to not using the Limited API. |
| 103 |
_PY_LIMITED_API = None |
| 104 |
_bdist_limited_api_tag = None |
| 105 |
if not hasattr(sys, "_is_gil_enabled") or sys._is_gil_enabled(): |
| 106 |
# If we're on Python 3.10 or later, use Limited API 3.10. |
| 107 |
# Otherwise, drop back to 3.8. |
| 108 |
if sys.hexversion >= 0x030A0000: |
| 109 |
_PY_LIMITED_API = 0x030A0000 |
| 110 |
_bdist_limited_api_tag = "cp310" |
| 111 |
elif sys.hexversion >= 0x03080000: |
| 112 |
_PY_LIMITED_API = 0x03080000 |
| 113 |
_bdist_limited_api_tag = "cp38" |
| 114 |
|
| •115 |
macros = [ |
| 116 |
("USE_SYSTEM_RAPIDYAML","1"), |
| 117 |
("RYML_NEW_ERROR_API","1"), |
| 118 |
] |
| 119 |
if _PY_LIMITED_API is not None: |
| 120 |
macros.append(("Py_LIMITED_API", f"{_PY_LIMITED_API:#010x}")) |
| 121 |
|
| 122 |
setuptools.setup( |
| 123 |
name="jsonnet", |
| 124 |
version=get_version(), |
| 125 |
license="Apache-2.0", |
| 126 |
cmdclass={ |
| 127 |
"build_ext": BuildJsonnetExt, |
| 128 |
}, |
| 129 |
ext_modules=[ |
| 130 |
setuptools.Extension( |
| 131 |
"_jsonnet", |
| •132 |
define_macros=macros, |
| 133 |
py_limited_api=(_PY_LIMITED_API is not None), |
| 134 |
sources=LIB_SOURCES, |
| 135 |
include_dirs=[ |
| 136 |
"include", |
| 137 |
"third_party/md5", |
| 138 |
], |
| •139 |
libraries=["ryml"], |
| 140 |
language="c++", |
| 141 |
) |
| 142 |
], |
| 143 |
options=( |
| 144 |
{"bdist_wheel": {"py_limited_api": _bdist_limited_api_tag}} |
| 145 |
if _bdist_limited_api_tag is not None |
| 146 |
else {} |
| 147 |
), |
| 148 |
) |