forked from mechmotum/cyipopt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
229 lines (196 loc) · 8.37 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# -*- coding: utf-8 -*-
"""
cyipopt: Python wrapper for the Ipopt optimization package, written in Cython.
Copyright (C) 2012-2015 Amit Aides
Copyright (C) 2015-2017 Matthias Kümmerer
Copyright (C) 2017-2024 cyipopt developers
License: EPL 2.0
"""
import sys
import os.path
from distutils.sysconfig import get_python_lib
import subprocess as sp
from setuptools import setup
from setuptools.extension import Extension
# install requirements before import
# NOTE : NumPy 1.25 is the first release of NumPy that you can link to the
# NumPy C-API and it will be guaranteed to be backward compatible back to NumPy
# 1.19, thus we set the minimum NumPy build version to 1.25. See:
# https://numpy.org/doc/stable/dev/depending_on_numpy.html#adding-a-dependency-on-numpy
# for more information.
from setuptools import dist
SETUP_REQUIRES = [
"cython>=0.29.28",
"numpy>=1.25",
"setuptools>=44.1.1",
]
dist.Distribution().fetch_build_eggs(SETUP_REQUIRES)
from Cython.Distutils import build_ext
import numpy as np
exec(open("cyipopt/version.py", encoding="utf-8").read())
PACKAGE_NAME = "cyipopt"
DEPRECATED_PACKAGE_NAME = "ipopt"
VERSION = __version__
DESCRIPTION = "A Cython wrapper to the IPOPT optimization package"
with open("README.rst", encoding="utf-8") as f:
LONG_DESCRIPTION = f.read()
KEYWORDS = [
"coin-or",
"interior-point",
"ipopt",
"nlp",
"nonlinear programming",
"optimization",
]
AUTHOR = "Jason K. Moore"
EMAIL = "[email protected]"
URL = "https://github.com/mechmotum/cyipopt"
INSTALL_REQUIRES = [
"numpy>=1.21.5",
]
LICENSE = "EPL-2.0"
CLASSIFIERS = [
"Development Status :: 5 - Production/Stable",
"License :: OSI Approved :: Eclipse Public License 2.0 (EPL-2.0)",
"Intended Audience :: Science/Research",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
def pkgconfig(*packages, **kw):
"""Returns a dictionary containing the include and library flags to pass to
a Python extension that depends on the provided packages.
Parameters
----------
*packages : one or more strings
These are the names of ``.pc`` files that pkg-config can locate, for
example ``ipopt`` for the ``ipopt.pc`` file.
**kw : list of strings
Any values that should be preset in the returned dictionary. These can
include lists of items for: ``include_dirs``, ``library_dirs``,
``libraries``, ``extra_compile_args``.
Returns
-------
kw : dictionary
Dictionary containing the keys: ``include_dirs``, ``library_dirs``,
``libraries``, ``extra_compile_args`` that are mapped to lists of
strings.
Notes
-----
This function is based on:
http://code.activestate.com/recipes/502261-python-distutils-pkg-config/#c2
"""
flag_map = {"-I": "include_dirs", "-L": "library_dirs", "-l": "libraries"}
output = sp.Popen(["pkg-config", "--libs", "--cflags"] + list(packages),
stdout=sp.PIPE).communicate()[0]
if not output: # output will be empty string if pkg-config finds nothing
msg = ("pkg-config was not able to find any of the requested packages "
"{} on your system. Make sure pkg-config can discover the .pc "
"files associated with the installed packages.")
raise OSError(msg.format(list(packages)))
output = output.decode("utf8")
for token in output.split():
if token[:2] in flag_map:
kw.setdefault(flag_map.get(token[:2]), []).append(token[2:])
else:
kw.setdefault("extra_compile_args", []).append(token)
kw["include_dirs"] += [np.get_include()]
return kw
def handle_ext_modules_win_32_conda_forge_ipopt():
conda_prefix = os.path.split(sys.executable)[0]
IPOPT_INCLUDE_DIRS = [os.path.join(conda_prefix, "Library", "include",
"coin-or"), np.get_include()]
IPOPT_LIBS = ["ipopt-3"]
IPOPT_LIB_DIRS = [os.path.join(conda_prefix, "Library", "lib")]
EXT_MODULES = [Extension("ipopt_wrapper",
["cyipopt/cython/ipopt_wrapper.pyx"],
include_dirs=IPOPT_INCLUDE_DIRS,
libraries=IPOPT_LIBS,
library_dirs=IPOPT_LIB_DIRS)]
DATA_FILES = None
include_package_data = True
return EXT_MODULES, DATA_FILES, include_package_data
def handle_ext_modules_win_32_other_ipopt():
IPOPT_INCLUDE_DIRS = [os.path.join(ipoptdir, "include", "coin-or"),
np.get_include()]
# These are the specific binaries in the IPOPT 3.13.2 binary download:
# https://github.com/coin-or/Ipopt/releases/download/releases%2F3.13.2/Ipopt-3.13.2-win64-msvs2019-md.zip
IPOPT_LIBS = ["ipopt.dll", "ipoptamplinterface.dll"]
IPOPT_LIB_DIRS = [os.path.join(ipoptdir, "lib")]
IPOPT_DLL = [
"ipopt-3.dll",
"ipoptamplinterface-3.dll",
"libifcoremd.dll",
"libmmd.dll",
"msvcp140.dll",
"svml_dispmd.dll",
"vcruntime140.dll",
]
IPOPT_DLL_DIRS = [os.path.join(ipoptdir, "bin")]
EXT_MODULES = [Extension("ipopt_wrapper",
["cyipopt/cython/ipopt_wrapper.pyx"],
include_dirs=IPOPT_INCLUDE_DIRS,
libraries=IPOPT_LIBS,
library_dirs=IPOPT_LIB_DIRS)]
DATA_FILES = [(get_python_lib(),
[os.path.join(IPOPT_DLL_DIRS[0], dll)
for dll in IPOPT_DLL])] if IPOPT_DLL else None
include_package_data = False
return EXT_MODULES, DATA_FILES, include_package_data
def handle_ext_modules_general_os():
ipopt_wrapper_ext = Extension("ipopt_wrapper",
["cyipopt/cython/ipopt_wrapper.pyx"],
**pkgconfig("ipopt"))
EXT_MODULES = [ipopt_wrapper_ext]
DATA_FILES = None
include_package_data = True
return EXT_MODULES, DATA_FILES, include_package_data
if __name__ == "__main__":
ipoptdir = os.environ.get("IPOPTWINDIR", "")
# conda-forge hosts a windows version of ipopt for ipopt versions >= 3.13.
# The location of the headers and binaries are in $CONDA_PREFIX/Library/
# and the library binary is named "libipopt.lib". If the IPOPTWINDIR
# environment variable is set to USECONDAFORGEIPOPT then this setup will be
# run.
if sys.platform == "win32" and ipoptdir == "USECONDAFORGEIPOPT":
print('Using Conda Forge Ipopt on Windows.')
ext_module_data = handle_ext_modules_win_32_conda_forge_ipopt()
elif sys.platform == "win32" and ipoptdir:
print('Using Ipopt in {} directory on Windows.'.format(ipoptdir))
ext_module_data = handle_ext_modules_win_32_other_ipopt()
elif sys.platform == "win32" and not ipoptdir:
ipoptdir = os.path.abspath(os.path.dirname(__file__))
msg = 'Using Ipopt adjacent to setup.py in {} on Windows.'
print(msg.format(ipoptdir))
ext_module_data = handle_ext_modules_win_32_other_ipopt()
else:
print('Using Ipopt found with pkg-config.')
ext_module_data = handle_ext_modules_general_os()
EXT_MODULES, DATA_FILES, include_package_data = ext_module_data
# NOTE : The `name` kwarg here is the distribution name, i.e. the name that
# PyPi uses for a collection of packages. Historically this has been
# `ipopt`, but as of 1.1.0 is `cyipopt`. `pip install cyipopt` will install
# the `cyipopt` and `ipopt` packages into the `site-packages` directory.
# Both `import cyipopt` and `import ipopt` will work, with the later giving
# a deprecation warning.
setup(name=PACKAGE_NAME,
version=VERSION,
author=AUTHOR,
author_email=EMAIL,
url=URL,
description=DESCRIPTION,
long_description=LONG_DESCRIPTION,
keywords=KEYWORDS,
license=LICENSE,
classifiers=CLASSIFIERS,
packages=[PACKAGE_NAME, DEPRECATED_PACKAGE_NAME],
setup_requires=SETUP_REQUIRES,
install_requires=INSTALL_REQUIRES,
include_package_data=include_package_data,
data_files=DATA_FILES,
zip_safe=False, # required for Py27 on Windows to work
cmdclass={"build_ext": build_ext},
ext_modules=EXT_MODULES,
)