-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsetup.py
More file actions
145 lines (124 loc) · 4.9 KB
/
Copy pathsetup.py
File metadata and controls
145 lines (124 loc) · 4.9 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""Build Python extension."""
import json
import os
import subprocess
import sys
from glob import glob
from pathlib import Path
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
class bdist_wheel(_bdist_wheel): # noqa: N801
def finalize_options(self):
super().finalize_options()
self.root_is_pure = True
self.python_tag = f"py{sys.version_info.major}{sys.version_info.minor}"
def assemble(asm_file: str, asm_directory: Path) -> None:
"""Assemble assembler code."""
obj_file = asm_file.split(".")[0] + ".o"
cwd = Path.cwd()
source_file = cwd / asm_directory / asm_file
obj_file = cwd / "artifacts" / obj_file
if obj_file.exists():
return
print(f"assembling {source_file}")
if not obj_file.parents[0].is_dir():
mkdir_command = f"mkdir {obj_file.parents[0]}"
print(mkdir_command)
subprocess.run(mkdir_command, shell=True, check=True)
assemble_command = (
f"as -mGOFF -I{source_file.parents[0]} -o {obj_file} {source_file}"
)
print(assemble_command)
subprocess.run(assemble_command, shell=True, check=True)
def generate_json_schema_header() -> None:
"""Generate SEAR JSON schema header."""
schema_absolute_path = Path.cwd() / "schema.json"
with open(schema_absolute_path) as f:
schema = json.dumps(json.load(f), separators=(",", ":"))
schema_header_absolute_path = Path.cwd() / "sear" / "sear_schema.hpp"
with open(schema_header_absolute_path, "w") as f:
f.write(
"\n".join(
[
"#ifndef __SEAR_SCHEMA_H_",
"#define __SEAR_SCHEMA_H_",
"",
f'#define SEAR_SCHEMA R"({schema})"_json',
"",
"#endif",
],
),
)
class BuildExtensionWithAssemblerAndC(build_ext):
"""Build Python extension that includes assembler code."""
def run(self):
os.environ["CC"] = "ibm-clang64"
os.environ["CFLAGS"] = "-std=c99"
os.environ["CXX"] = "ibm-clang++64"
os.environ["CXXFLAGS"] = "-std=c++17"
sear_source_path = Path("sear")
assemble("irrseq00.s", sear_source_path / "irrseq00")
super().run()
def main():
"""Python extension build entrypoint."""
cwd = Path.cwd()
# Use ZOPEN_ROOTFS to find OpenSSL and ZOSLIB.
if "ZOPEN_ROOTFS" not in os.environ and not ("ZOSLIB_ROOT" in os.environ and "OPENSSL_ROOT" in os.environ): # noqa: E501
raise RuntimeError(
"ZOPEN_ROOTFS is not set, but is required in order to "
+ "find the zopen community distributions of of OpenSSL "
+ "and ZOSLIB since they are build dependencies.\n"
+ "You can find more information about setting up zopen "
+ "community here: "
+ "https://zopen.community/#/Guides/QuickStart?id=installing-zopen-package-manager \n" # noqa: E501
+ "Alternatively set ZOSLIB_ROOT and OPENSSL_ROOT",
)
if "ZOPEN_ROOTFS" not in os.environ:
openssl_lib_path = os.environ["OPENSSL_ROOT"] + "/lib/"
openssl_include_path = os.environ["OPENSSL_ROOT"] + "/include/"
zoslib_lib_path = os.environ["ZOSLIB_ROOT"] + "/lib/"
else:
openssl_lib_path = os.environ["ZOPEN_ROOTFS"] + "/usr/local/lib/"
openssl_include_path = os.environ["ZOPEN_ROOTFS"] + "/usr/local/include/"
zoslib_lib_path = os.environ["ZOPEN_ROOTFS"] + "/usr/local/lib/"
assembled_object_path = cwd / "artifacts" / "irrseq00.o"
generate_json_schema_header()
setup_args = {
"ext_modules": [
Extension(
"sear._C",
sources=(
glob("sear/**/*.cpp")
+ glob("sear/*.cpp")
+ glob("externals/json-schema-validator/*.cpp")
+ ["sear/python/_sear.c"]
),
include_dirs=(
glob("sear/**/")
+ [
"sear",
"externals/json",
"externals/json-schema-validator",
"externals/iconv",
openssl_include_path,
]
),
extra_link_args=[
"-m64",
"-Wl,-b,edit=no",
"-Wl," + openssl_lib_path + "libcrypto.a",
"-Wl," + openssl_lib_path + "libssl.a",
"-Wl," + zoslib_lib_path + "libzoslib.a",
],
extra_objects=[f"{assembled_object_path}"],
),
],
"cmdclass": {
"build_ext": BuildExtensionWithAssemblerAndC,
"bdist_wheel": bdist_wheel,
},
}
setup(**setup_args)
if __name__ == "__main__":
main()