-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsetup.py
More file actions
197 lines (151 loc) · 5.17 KB
/
setup.py
File metadata and controls
197 lines (151 loc) · 5.17 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import os
import re
import sys
import glob
import platform
import subprocess
from setuptools import setup, find_packages, Extension
from setuptools.command.build_ext import build_ext
from distutils.version import LooseVersion
class CMakeExtension(Extension):
def __init__(self, name, sourcedir=""):
super().__init__(name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
class CMakeBuild(build_ext):
def run(self):
try:
subprocess.check_output(["cmake", "--version"])
except OSError:
raise RuntimeError(
"CMake must be installed to build the following extensions: " +
", ".join(e.name for e in self.extensions))
if platform.system() == "Windows":
RuntimeError("Windows is not supported")
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext):
extdir = os.path.abspath(
os.path.dirname(self.get_ext_fullpath(ext.name)))
cmake_args = [
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + extdir,
"-DPYTHON_EXECUTABLE=" + sys.executable]
cfg = "Debug" if self.debug else "Release"
build_args = ["--config", cfg]
if platform.system() == "Windows":
RuntimeError("Windows is not supported")
else:
cmake_args += ["-DCMAKE_BUILD_TYPE=" + cfg]
cmake_args += ["-DCMAKE_CXX_FLAGS_RELEASE='-fopenmp -O2 -fPIC'"]
build_args += ["--", "-j2"]
env = os.environ.copy()
env["CXXFLAGS"] = "{} -DVERSION_INFO=\\\"{}\\\"".format(
env.get("CXXFLAGS", ""), self.distribution.get_version())
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
subprocess.check_call(
["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
subprocess.check_call(
["cmake", "--build", "."] + build_args, cwd=self.build_temp)
#-----------------------------------------------------------------------
header_paths = sorted(
glob.glob("src/pybind/*.h") + glob.glob("src/pybind/*/*.h"))
pybinds = [] # pybind11's binding functions
classes = [] # bound class names
functions = [] # bound function names
for path_h in header_paths:
path_s = path_h[:-len(".h")] + ".cpp"
with open(path_s, "r") as f:
lines = f.readlines()
# parse lines in the source file one by one
# and put data into `pybinds`, `classes`, and `functions`
for i, line in enumerate(lines):
line_ = line.lstrip()
if line_.startswith("void pybind::py_"):
pybinds.append(" {}(m);".format(re.split("[ (]", line_)[1]))
elif line_.startswith("py::class_<") and 12 < len(line_):
classes.append(re.split("[,<>]", line_)[1])
elif line_.startswith("m.def("):
if 7 < len(line_):
functions.append(line_[6:].split("\"")[1])
else:
functions.append(lines[i+1].lstrip().split("\"")[1])
def priority(x):
"""
Elements and Updater class must be included earlier
than the other classes.
"""
if x.count("element"):
return 1
elif x.count("updater"):
return 2
else:
return 3 + x.count("_")
header_paths.sort(key=priority)
pybinds.sort(key=priority)
classes.sort()
functions.sort()
#-----------------------------------------------------------------------
# write header paths and pybind11's binding functions to `src/pybind.h`
with open("src/pybind.h", "w") as f:
f.write("""/*!
@file src/pybind.h
@brief This file includes all the header files containing definitions
of functions to bind C++ and Python.
@author Takayuki Kobayashi
@date 2018/09/08
This file is not included in the GitHub repository.
It will be created at installation time.
To find the way how this file is written,
please see `setup.py` in the root directory of this project.
*/
#ifndef PYBIND_H
#define PYBIND_H
#include <pybind11/pybind11.h>
{}
//! Macro for generating Python module named `_ppap4lmp`.
PYBIND11_MODULE(_ppap4lmp, m)
{{
{}
}}
#endif
""".format(
"\n".join(
"#include \"{}\"".format(p.replace("src/", "", 1))
for p in header_paths),
"\n".join(pybinds)))
#-----------------------------------------------------------------------
# write class and function names to `ppap4lmp/__init__.py`
with open("ppap4lmp/__init__.py", "w") as f:
f.write("""from ._version import version_info, __version__
from ._ppap4lmp import \\
{}
__all__ = [
"{}"
]
""".format(
", ".join(classes + list(set(functions))),
"\", \n \"".join(classes + list(set(functions)))))
#-----------------------------------------------------------------------
version_ns = {}
with open(os.path.join("ppap4lmp", "_version.py")) as f:
exec(f.read(), {}, version_ns)
with open("README.md", "r") as fh:
long_description = fh.read()
setup(
name="ppap4lmp",
version=version_ns["__version__"],
author="Takayuki Kobayashi",
author_email="iris.takayuki@gmail.com",
description="PostProcess and Analysis Program for LAMMPS",
license="MIT",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/irisTa56/ppap4lmp",
ext_modules=[CMakeExtension("ppap4lmp._ppap4lmp")],
cmdclass=dict(build_ext=CMakeBuild),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
],
packages=find_packages(),
)