-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathsetup.py
More file actions
370 lines (339 loc) · 12.1 KB
/
setup.py
File metadata and controls
370 lines (339 loc) · 12.1 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# Copyright 2018 The Cornac Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""
Release instruction:
- Check that tests run correctly with all CI tools.
- Change __version__ in pyproject.toml, cornac/__init__.py, docs/source/conf.py.
- Commit and release a version on GitHub, Actions will be triggered to build and upload to PyPI.
- Update conda-forge feedstock with new version and SHA256 hash of the new .tar.gz archive on PyPI (optional), the conda-forge bot will detect a new version and create PR after a while.
- Check on https://anaconda.org/conda-forge/cornac that new version is available for all platforms.
"""
import os
import sys
import shutil
import platform
from setuptools import Extension, Command, setup, find_packages
from Cython.Distutils import build_ext
import numpy as np
with open("README.md", "r") as fh:
long_description = fh.read()
USE_OPENMP = False # we'll turn it on only if we find a working libomp
def candidates_from_env():
# Let users/CI point to a custom libomp
incs = []
libs = []
omp_dir = os.environ.get("OMP_DIR")
if omp_dir:
incs += [os.path.join(omp_dir, "include")]
libs += [os.path.join(omp_dir, "lib")]
inc_env = os.environ.get("OMP_INCLUDE")
lib_env = os.environ.get("OMP_LIB")
if inc_env: incs.append(inc_env)
if lib_env: libs.append(lib_env)
# Conda environments
conda = os.environ.get("CONDA_PREFIX")
if conda:
incs += [os.path.join(conda, "include")]
libs += [os.path.join(conda, "lib")]
# Homebrew (Apple silicon and Intel) and MacPorts (optional, don’t require them)
incs += ["/opt/homebrew/opt/libomp/include", "/usr/local/opt/libomp/include", "/opt/local/include"]
libs += ["/opt/homebrew/opt/libomp/lib", "/usr/local/opt/libomp/lib", "/opt/local/lib"]
# Common fallbacks
incs += ["/usr/local/include", "/usr/include"]
libs += ["/usr/local/lib", "/usr/lib"]
# De-dup while keeping order
def uniq(xs):
seen=set(); out=[]
for x in xs:
if x and x not in seen:
out.append(x); seen.add(x)
return out
return uniq(incs), uniq(libs)
def find_libomp():
incs, libs = candidates_from_env()
inc_dir = next((d for d in incs if os.path.exists(os.path.join(d, "omp.h"))), None)
# Prefer a real libomp over stubs
lib_names = ["libomp.dylib", "libomp.a"]
lib_dir = None
for d in libs:
if any(os.path.exists(os.path.join(d, n)) for n in lib_names):
lib_dir = d
break
return inc_dir, lib_dir
compile_args = []
link_args = []
if sys.platform.startswith("win"):
compile_args = ["/O2", "/openmp"]
link_args = []
elif sys.platform.startswith("darwin"):
# Always use Clang on macOS
os.environ.setdefault("CC", "clang")
os.environ.setdefault("CXX", "clang++")
# Force single-arch arm64 on Apple silicon unless caller overrides
if platform.machine() == "arm64" and not os.environ.get("ARCHFLAGS"):
os.environ["ARCHFLAGS"] = "-arch arm64"
mac_min = os.environ.get("MACOSX_DEPLOYMENT_TARGET", "12.0")
# Base flags good for Clang/libc++
compile_args += [
"-O3", "-ffast-math",
"-Wno-unused-function",
"-std=c++11",
"-stdlib=libc++",
f"-mmacosx-version-min={mac_min}",
]
link_args += [
"-std=c++11",
"-stdlib=libc++",
f"-mmacosx-version-min={mac_min}",
]
# Optional OpenMP (only if a usable libomp is present)
if os.environ.get("CORNAC_DISABLE_OPENMP") != "1":
inc_dir, lib_dir = find_libomp()
if inc_dir and lib_dir:
compile_args += ["-Xpreprocessor", "-fopenmp", f"-I{inc_dir}"]
link_args += [f"-L{lib_dir}", "-lomp", f"-Wl,-rpath,{lib_dir}"]
USE_OPENMP = True
elif os.environ.get("CORNAC_FORCE_OPENMP") == "1":
raise RuntimeError(
"CORNAC_FORCE_OPENMP=1 but libomp was not found; set OMP_INCLUDE/OMP_LIB or OMP_DIR."
)
else:
# Linux/Unix: prefer OpenMP via compiler default (GCC/Clang + libgomp)
compile_args += [
"-O3", "-ffast-math",
"-Wno-unused-function",
"-Wno-maybe-uninitialized",
"-std=c++11",
"-fopenmp",
]
link_args += ["-std=c++11", "-fopenmp"]
USE_OPENMP = True
extensions = [
Extension(
name="cornac.models.c2pf.c2pf",
sources=[
"cornac/models/c2pf/cython/c2pf.pyx",
"cornac/models/c2pf/cpp/cpp_c2pf.cpp",
],
include_dirs=[
"cornac/models/c2pf/cpp/",
"cornac/utils/external/eigen/Eigen",
"cornac/utils/external/eigen/unsupported/Eigen/",
],
language="c++",
),
Extension(
name="cornac.models.nmf.recom_nmf",
sources=["cornac/models/nmf/recom_nmf.pyx"],
include_dirs=[np.get_include()],
language="c++",
),
Extension(
name="cornac.models.pmf.pmf",
sources=["cornac/models/pmf/cython/pmf.pyx"],
language="c++",
),
Extension(
name="cornac.models.mcf.mcf",
sources=["cornac/models/mcf/cython/mcf.pyx"],
language="c++",
),
Extension(
name="cornac.models.sorec.sorec",
sources=["cornac/models/sorec/cython/sorec.pyx"],
language="c++",
),
Extension(
"cornac.models.hpf.hpf",
sources=[
"cornac/models/hpf/cython/hpf.pyx",
"cornac/models/hpf/cpp/cpp_hpf.cpp",
],
include_dirs=[
"cornac/models/hpf/cpp/",
"cornac/utils/external/eigen/Eigen",
"cornac/utils/external/eigen/unsupported/Eigen/",
],
language="c++",
),
Extension(
name="cornac.models.mf.backend_cpu",
sources=["cornac/models/mf/backend_cpu.pyx"],
include_dirs=[np.get_include()],
language="c++",
extra_compile_args=compile_args,
extra_link_args=link_args,
),
Extension(
name="cornac.models.baseline_only.recom_bo",
sources=["cornac/models/baseline_only/recom_bo.pyx"],
include_dirs=[np.get_include()],
language="c++",
extra_compile_args=compile_args,
extra_link_args=link_args,
),
Extension(
name="cornac.models.efm.recom_efm",
sources=["cornac/models/efm/recom_efm.pyx"],
include_dirs=[np.get_include()],
language="c++",
),
Extension(
name="cornac.models.comparer.recom_comparer_obj",
sources=["cornac/models/comparer/recom_comparer_obj.pyx"],
include_dirs=[np.get_include()],
language="c++",
),
Extension(
name="cornac.models.bpr.recom_bpr",
sources=["cornac/models/bpr/recom_bpr.pyx"],
include_dirs=[np.get_include(), "cornac/utils/external"],
language="c++",
extra_compile_args=compile_args,
extra_link_args=link_args,
),
Extension(
name="cornac.models.bpr.recom_wbpr",
sources=["cornac/models/bpr/recom_wbpr.pyx"],
include_dirs=[np.get_include(), "cornac/utils/external"],
language="c++",
extra_compile_args=compile_args,
extra_link_args=link_args,
),
Extension(
name="cornac.models.sbpr.recom_sbpr",
sources=["cornac/models/sbpr/recom_sbpr.pyx"],
include_dirs=[np.get_include(), "cornac/utils/external"],
language="c++",
extra_compile_args=compile_args,
extra_link_args=link_args,
),
Extension(
name="cornac.models.lrppm.recom_lrppm",
sources=["cornac/models/lrppm/recom_lrppm.pyx"],
include_dirs=[np.get_include(), "cornac/utils/external"],
language="c++",
extra_compile_args=compile_args,
extra_link_args=link_args,
),
Extension(
name="cornac.models.mter.recom_mter",
sources=["cornac/models/mter/recom_mter.pyx"],
include_dirs=[np.get_include(), "cornac/utils/external"],
language="c++",
extra_compile_args=compile_args,
extra_link_args=link_args,
),
Extension(
name="cornac.models.companion.recom_companion",
sources=["cornac/models/companion/recom_companion.pyx"],
include_dirs=[np.get_include(), "cornac/utils/external"],
language="c++",
extra_compile_args=compile_args,
extra_link_args=link_args,
),
Extension(
name="cornac.models.comparer.recom_comparer_sub",
sources=["cornac/models/comparer/recom_comparer_sub.pyx"],
include_dirs=[np.get_include(), "cornac/utils/external"],
language="c++",
extra_compile_args=compile_args,
extra_link_args=link_args,
),
Extension(
name="cornac.models.mmmf.recom_mmmf",
sources=["cornac/models/mmmf/recom_mmmf.pyx"],
include_dirs=[np.get_include(), "cornac/utils/external"],
language="c++",
extra_compile_args=compile_args,
extra_link_args=link_args,
),
Extension(
name="cornac.models.knn.similarity",
sources=["cornac/models/knn/similarity.pyx"],
include_dirs=[np.get_include()],
language="c++",
extra_compile_args=compile_args,
extra_link_args=link_args,
),
Extension(
name="cornac.utils.fast_dict",
sources=["cornac/utils/fast_dict.pyx"],
include_dirs=[np.get_include()],
language="c++",
),
Extension(
name="cornac.utils.fast_dot",
sources=["cornac/utils/fast_dot.pyx"],
language="c++",
extra_compile_args=compile_args,
extra_link_args=link_args,
),
Extension(
name="cornac.utils.fast_sparse_funcs",
sources=["cornac/utils/fast_sparse_funcs.pyx"],
include_dirs=[np.get_include()],
language="c++",
),
]
if sys.platform.startswith("linux"): # Linux supported only
extensions += [
Extension(
name="cornac.models.fm.backend_libfm",
sources=["cornac/models/fm/backend_libfm.pyx"],
include_dirs=[
np.get_include(),
"cornac/models/fm/libfm/util/",
"cornac/models/fm/libfm/fm_core/",
"cornac/models/fm/libfm/libfm/src/",
],
language="c++",
extra_compile_args=compile_args,
extra_link_args=link_args,
)
]
class CleanCommand(Command):
description = "Remove build artifacts from the source tree"
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
# Remove .cpp and .so files for a clean build
if os.path.exists("build"):
shutil.rmtree("build")
for dirpath, dirnames, filenames in os.walk("cornac"):
for filename in filenames:
root, extension = os.path.splitext(filename)
if extension in [".so", ".pyd", ".dll", ".pyc"]:
os.unlink(os.path.join(dirpath, filename))
if extension in [".c", ".cpp"]:
pyx_file = str.replace(filename, extension, ".pyx")
if os.path.exists(os.path.join(dirpath, pyx_file)):
os.unlink(os.path.join(dirpath, filename))
for dirname in dirnames:
if dirname == "__pycache__":
shutil.rmtree(os.path.join(dirpath, dirname))
cmdclass = {
"clean": CleanCommand,
"build_ext": build_ext,
}
setup(
ext_modules=extensions,
extras_require={"tests": ["pytest", "pytest-pep8", "pytest-xdist", "pytest-cov", "Flask"]},
cmdclass=cmdclass,
packages=find_packages(),
)