-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathextensions.py
More file actions
126 lines (103 loc) · 3.81 KB
/
extensions.py
File metadata and controls
126 lines (103 loc) · 3.81 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
import logging
import os
import platform
import shutil
import sys
from pathlib import Path
from subprocess import check_call
from typing import Dict, List
from setuptools import Extension
from setuptools.command.build_ext import build_ext
class NLOptBuildExtension(Extension):
def __init__(self, name: str, version: str):
super().__init__(name, sources=[])
# Source dir should be at the root directory
self.source_dir = Path(__file__).parent.absolute()
self.version = version
class NLOptBuild(build_ext):
def run(self):
try:
check_call(["cmake", "--version"])
except OSError:
raise RuntimeError("CMake must be installed")
if platform.system() not in ("Windows", "Linux", "Darwin"):
raise RuntimeError(f"Unsupported os: {platform.system()}")
for ext in self.extensions:
if isinstance(ext, NLOptBuildExtension):
self.build_extension(ext)
@property
def config(self):
return "Debug" if self.debug else "Release"
def build_extension(self, ext: Extension):
# - make sure path ends with delimiter
# - required for auto-detection of auxiliary "native" libs
ext_dir = Path(self.get_ext_fullpath(ext.name)).parent.absolute()
_ed = ext_dir.as_posix()
build_dir = create_directory(Path(self.build_temp))
# package builds in 2 steps, first to compile the nlopt package and second to build the DLL
cmd = [
"cmake",
"-LAH",
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={_ed}",
f"-DPython_EXECUTABLE={sys.executable}",
"-DNLOPT_GUILE=OFF",
"-DNLOPT_MATLAB=OFF",
"-DNLOPT_OCTAVE=OFF",
ext.source_dir.as_posix(),
]
if platform.system() == "Windows":
cmd.insert(
2, f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{self.config.upper()}={_ed}"
)
execute_command(
cmd=cmd,
cwd=build_dir,
env={
**os.environ.copy(),
"CXXFLAGS": f'{os.environ.get("CXXFLAGS", "")} -DVERSION_INFO="{self.distribution.get_version()}"',
},
)
# build the DLL
execute_command(
[
"cmake",
"--build",
".",
"--config",
self.config,
"--",
"-m" if platform.system() == "Windows" else "-j2",
],
cwd=build_dir,
)
# Copy over the important bits
nlopt_py = build_dir / "extern" / "nlopt" / "src" / "swig" / "python" / "nlopt.py"
logging.info(
f"Ext Dir - {ext_dir}\n"
+ "\n".join(f" - {file.as_posix()}" for file in ext_dir.rglob("*"))
)
for folder in [ext_dir, nlopt_py.parent]:
logging.info(
f"Files in {folder.as_posix()}\n"
+ "\n".join(f" - {f.name}" for f in folder.iterdir())
)
logging.info(
f"Attempting to copy nlopt.py file from {nlopt_py.parent.as_posix()} to {ext_dir.as_posix()}"
)
if not nlopt_py.exists():
raise RuntimeError("swig python file was not generated")
shutil.copy2(nlopt_py, ext_dir / "nlopt.py")
with open(ext_dir / "__init__.py", "w") as f:
f.write(
f"""from .nlopt import *
__version__ = '{ext.version}'
""".lstrip()
)
def create_directory(path: Path):
if path.exists():
shutil.rmtree(path)
path.mkdir(exist_ok=True, parents=True)
return path
def execute_command(cmd: List[str], cwd: Path, env: Dict[str, str] = os.environ):
logging.info(f"Running Command: {cwd.as_posix()}: {' '.join(cmd)}")
check_call(cmd, cwd=cwd.as_posix(), env=env)