-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsetup.py
More file actions
722 lines (579 loc) · 21.1 KB
/
setup.py
File metadata and controls
722 lines (579 loc) · 21.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
"""
Installation script for Sire
This assumes that the python that is used to execute this script
is within a conda / pixi environment that already has all of the
required dependencies installed. Use pixi to create the environment
before running this script, e.g.:
pixi install -e dev
USAGE:
python setup.py build : Compile sire (takes a long time!)
python setup.py install : Will build sire and then install
python setup.py install_module : Will only install the Python module
"""
import glob
import json
import os
import platform
import subprocess
import shutil
import sys
try:
# We have to check the version, but we can't do this by
# importing sire (this will block installing of files on
# windows, as we can't copy over a library file that is linked
# into the application)
curver = (
subprocess.check_output(
[sys.executable, os.path.join("build/get_sire_version.py")]
)
.decode("utf8")
.lstrip()
.rstrip()
)
if len(curver) == 0:
curver = None
except Exception:
curver = None
# If Sire is already installed, see if the version number has changed.
if curver:
with open("version.txt", "r") as f:
ver = f.readline().strip()
# Abort if the versions differ.
if curver != ver:
raise EnvironmentError(
f"This environment already contains an install of Sire version {curver}. "
"Please delete the installation or create a new environment before "
f"installing version {ver}. If you are using pixi, run: pixi clean -e dev. "
"Also remove old build directories from 'build'. "
)
try:
# Find out how much memory we have in total.
# The wrappers need 4 GB per core to compile
import psutil
total_memory_gb = psutil.virtual_memory()[0] / (1024 * 1024 * 1024)
except Exception:
total_memory_gb = None
# We can only run this script from the sire directory
curdir = os.path.abspath(".")
if os.path.abspath(os.path.dirname(sys.argv[0])) != curdir:
print("You can only run this script from the sire directory")
sys.exit(-1)
# Detect the environment prefix (conda or pixi)
if "PREFIX" in os.environ and "BUILD_PREFIX" in os.environ:
print("This a build initiated by conda-build or rattler-build")
conda_base = os.path.abspath(os.environ["PREFIX"])
print(f"Setting conda-base to {conda_base}")
else:
# Find the prefix from the Python executable
conda_base = os.path.abspath(os.path.dirname(sys.executable))
if os.path.basename(conda_base) == "bin":
conda_base = os.path.dirname(conda_base)
if "CONDA_DEFAULT_ENV" in os.environ:
conda_env = os.environ["CONDA_DEFAULT_ENV"]
else:
conda_env = None
python_exe = None
if os.path.exists(os.path.join(conda_base, "python.exe")):
# Windows
conda_bin = os.path.join(conda_base, "Library", "bin")
python_exe = os.path.join(conda_base, "python.exe")
elif os.path.exists(os.path.join(conda_base, "bin", "python")):
# MacOS and Linux
conda_bin = os.path.join(conda_base, "bin")
python_exe = os.path.join(conda_bin, "python")
else:
print(
"Cannot find a 'python' binary in directory '%s'. "
"Are you running this script using the python executable "
"from a valid conda or pixi environment?" % conda_base
)
sys.exit(-1)
# Get the build operating system and processor
is_linux = False
is_windows = False
is_macos = False
platform_name = platform.system()
machine = platform.machine()
if machine in ["aarch64", "arm64"]:
machine = "arm64"
elif machine in ["x86_64", "AMD64"]:
machine = "x86_64"
else:
print(f"Unrecognised architecture ({machine}). Compile at your own risk.")
exe_suffix = ""
if platform_name == "Linux":
platform_name = "linux"
is_linux = True
elif platform_name == "Darwin":
platform_name = "macos"
is_macos = True
elif platform_name == "Windows":
platform_name = "windows"
exe_suffix = ".exe"
is_windows = True
else:
print("Unrecognised build platform: %s" % platform_name)
print("We cannot compile sire.")
sys.exit(-1)
platform_string = f"{platform_name}_{machine}"
def parse_args():
import argparse
import multiprocessing
parser = argparse.ArgumentParser()
ncores = multiprocessing.cpu_count()
if total_memory_gb is None:
# it is safest to half the number of python build cores
if ncores % 2 == 0:
npycores = int(ncores / 2)
else:
npycores = int((ncores + 1) / 2)
else:
# we need at least 3 GB RAM per core
npycores = min(ncores, int(total_memory_gb / 3))
if npycores < 1:
npycores = 1
print(f"Total memory available: {total_memory_gb} GB")
print(f"Number of python compile cores: {npycores}")
parser.add_argument(
"-C",
"--corelib",
action="append",
nargs=1,
metavar=("PARAMETER=VALUE",),
default=[],
help="pass CMake definitions for corelib",
)
parser.add_argument(
"-W",
"--wrapper",
action="append",
nargs=1,
metavar=("PARAMETER=VALUE",),
default=[],
help="pass CMake definitions for wrapper",
)
parser.add_argument(
"-G",
"--generator",
action="append",
nargs=1,
metavar=("GENERATOR",),
default=[],
help="pass CMake generator",
)
parser.add_argument(
"-A",
"--architecture",
action="append",
nargs=1,
metavar=("ARCHITECTURE",),
default=[],
help="pass CMake generator architecture, e.g. WIN64",
)
parser.add_argument(
"-n",
"--ncores",
action="store",
type=int,
nargs=1,
metavar=("N_CORES",),
default=[ncores],
help="Number of CPU cores used for compiling corelib "
"(defaults to all available on the current system)",
)
parser.add_argument(
"-N",
"--npycores",
action="store",
type=int,
nargs=1,
metavar=("N_PYTHON_CORES",),
default=[npycores],
help="Number of CPU cores used for compiling Python wrappers "
"(defaults to the number of CPU cores used for compiling corelib)",
)
parser.add_argument(
"--skip-build",
action="store_true",
default=False,
help="Skip the build of the C++ code (only use if you know that "
"the C++ code is already built)",
)
parser.add_argument(
"--install-metadata",
action="store_true",
default=False,
help="Install package metadata. This is useful when you are building "
"from source but still want to be able to query the installation using "
"conda list sire.",
)
parser.add_argument(
"action",
nargs="*",
help="Should be one of 'build', 'install' or 'install_module'.\n"
"\n [build] : Compile and install corelib, and just compile the wrappers.\n"
" [install] : 'build' plus install the wrappers and install the module.\n"
" [install_module] : Just install the module (no compilation).",
)
return parser.parse_args()
def add_default_cmake_defs(cmake_defs, ncores):
for a in (
"ANACONDA_BASE=%s" % conda_base.replace("\\", "/"),
"BUILD_NCORES=%s" % ncores,
):
found = False
for d in cmake_defs:
if a in d[0]:
found = True
break
if not found:
cmake_defs.append([a])
if is_macos:
# don't compile with AVX as the resulting binaries won't
# work on M1 macs
cmake_defs.append(["SIRE_DISABLE_AVX=ON"])
cmake_defs.append(["SIRE_DISABLE_AVX512F=ON"])
def make_cmd(ncores, install=False):
if is_windows:
action = "INSTALL" if install else "ALL_BUILD"
make_args = "%s -- /m:%s /p:Configuration=Release /p:Platform=x64" % (
action,
ncores,
)
else:
action = "install" if install else "all"
make_args = "%s -- VERBOSE=1 -j %s" % (action, ncores)
return make_args.split()
def _get_build_ext():
if "CONDA_BUILD" in os.environ and os.environ["CONDA_BUILD"] == "1":
return "conda_build"
elif "PIXI_ENVIRONMENT_NAME" in os.environ:
return os.environ["PIXI_ENVIRONMENT_NAME"]
elif conda_env is not None:
return conda_env
else:
return os.path.basename(conda_base.replace(" ", "_").replace(".", "_"))
def _get_bin_dir():
if "CONDA_BUILD" in os.environ and os.environ["CONDA_BUILD"] == "1":
bindir = os.environ["BUILD_PREFIX"]
if is_windows:
return os.path.join(bindir, "Library", "bin")
else:
return os.path.join(bindir, "bin")
else:
return conda_bin
def build(ncores: int = 1, npycores: int = 1, coredefs=[], pydefs=[]):
print("\nCompiling the C++ code")
CC = None
CXX = None
cmake = "cmake"
if is_windows:
cmake = f"{cmake}.exe"
bindir = _get_bin_dir()
cmake = os.path.join(bindir, cmake)
if "CONDA_BUILD" in os.environ and os.environ["CONDA_BUILD"] == "1":
conda_build = True
else:
os.environ["CONDA_BUILD"] = "0"
conda_build = False
# get the compilers
if conda_build:
print("This is a conda/rattler build")
if is_windows:
# Windows: vcvars is activated, let CMake find the MSVC.
CXX = None
CC = None
else:
# Try to get compilers from environment
CXX = os.environ.get("CXX")
CC = os.environ.get("CC")
elif is_macos:
try:
CXX = glob.glob(os.path.join(bindir, "clang++"))[0]
CC = glob.glob(os.path.join(bindir, "clang"))[0]
except Exception:
print("Cannot find the conda clang++ binaries!")
print("Please ensure your environment has clang and clangxx installed.")
print("If using pixi, run: pixi install -e dev")
sys.exit(-1)
elif is_linux:
try:
CXX = glob.glob(os.path.join(bindir, "*-g++"))[0]
CC = glob.glob(os.path.join(bindir, "*-gcc"))[0]
except Exception:
print("Cannot find the conda g++ binaries!")
print("Please ensure your environment has gcc and gxx installed.")
print("If using pixi, run: pixi install -e dev")
sys.exit(-1)
if CC is not None and CXX is not None:
print(f"Using compilers {CC} | {CXX}")
# Make sure all of the above output is printed to the screen
# before we start running any actual compilation
sys.stdout.flush()
# Now that the dependencies are installed, the next step
# is to use cmake to build the corelib and wrapper in the build/corelib
# and build/wrapper directories
# change into the build/corelib directory
OLDPWD = os.getcwd()
build_ext = _get_build_ext()
build_dir = os.path.abspath("build")
coredir = os.path.join(build_dir, f"{build_ext}_corelib")
if not os.path.exists(coredir):
os.makedirs(coredir)
if not os.path.isdir(coredir):
print("SOMETHING IS WRONG. %s is not a directory?" % coredir)
sys.exit(-1)
os.chdir(coredir)
if os.path.exists("CMakeCache.txt"):
# we have run cmake in this directory before. Run it again.
status = subprocess.run([cmake, "."])
else:
# this is the first time we are running cmake
sourcedir = os.path.join(
os.path.dirname(os.path.dirname(os.getcwd())), "corelib"
)
if not os.path.exists(os.path.join(sourcedir, "CMakeLists.txt")):
print(
"SOMETHING IS WRONG. There is no file %s"
% os.path.join(sourcedir, "CMakeLists.txt")
)
sys.exit(-1)
for a in ("NetCDF_ROOT_DIR", "OPENMM_ROOT_DIR"):
for i, d in enumerate(args.corelib):
if a in d[0]:
v = args.corelib.pop(i)[0]
if not a in os.environ:
os.environ[a] = v.split("=")[-1]
add_default_cmake_defs(args.corelib, ncores)
cmake_cmd = [
cmake,
*sum([["-D", d[0]] for d in args.corelib], []),
*sum([["-G", g[0]] for g in args.generator], []),
*sum([["-A", a[0]] for a in args.architecture], []),
sourcedir,
]
if CC is not None:
os.environ["CC"] = CC
if CXX is not None:
os.environ["CXX"] = CXX
print(" ".join(cmake_cmd))
sys.stdout.flush()
status = subprocess.run(cmake_cmd)
if status.returncode != 0:
print("SOMETHING WENT WRONG WHEN USING CMAKE ON CORELIB!")
print("\n== OUTPUT LOG ==")
try:
for line in open("CMakeFiles/CMakeOutput.log").readlines():
print(line.strip())
except Exception as e:
print(e)
print("\n== ERROR LOG ==")
try:
for line in open("CMakeFiles/CMakeError.log").readlines():
print(line.strip())
except Exception as e:
print(e)
sys.exit(-1)
# Now that cmake has run, we can compile and install corelib
######
###### Compiling and installing corelib
######
# Compile and install, as need to install to compile the wrappers
make_args = make_cmd(ncores, True)
print('NOW RUNNING "%s" --build . --target %s' % (cmake, " ".join(make_args)))
sys.stdout.flush()
status = subprocess.run([cmake, "--build", ".", "--target", *make_args])
if status.returncode != 0:
print("SOMETHING WENT WRONG WHEN COMPILING CORELIB!")
sys.exit(-1)
######
###### Compiling wrapper
######
# Make sure that the Python in conda is used
pydefs.append([f"PYTHON_EXECUTABLE={python_exe}"])
os.chdir(OLDPWD)
wrapperdir = os.path.join(build_dir, f"{build_ext}_wrapper")
if not os.path.exists(wrapperdir):
os.makedirs(wrapperdir)
if not os.path.isdir(wrapperdir):
print("SOMETHING IS WRONG. %s is not a directory?" % wrapperdir)
sys.exit(-1)
os.chdir(wrapperdir)
if os.path.exists("CMakeCache.txt"):
# we have run cmake in this directory before. Run it again.
status = subprocess.run([cmake, "."])
else:
# this is the first time we are running cmake
sourcedir = os.path.join(
os.path.dirname(os.path.dirname(os.getcwd())), "wrapper"
)
if not os.path.exists(os.path.join(sourcedir, "CMakeLists.txt")):
print(
"SOMETHING IS WRONG. There is no file %s"
% os.path.join(sourcedir, "CMakeLists.txt")
)
sys.exit(-1)
add_default_cmake_defs(args.wrapper, npycores)
cmake_cmd = [
cmake,
*sum([["-D", d[0]] for d in args.wrapper], []),
*sum([["-G", g[0]] for g in args.generator], []),
*sum([["-A", a[0]] for a in args.architecture], []),
sourcedir,
]
print(" ".join(cmake_cmd))
sys.stdout.flush()
status = subprocess.run(cmake_cmd)
if status.returncode != 0:
print("SOMETHING WENT WRONG WHEN USING CMAKE ON WRAPPER!")
sys.exit(-1)
# Just compile the wrappers
make_args = make_cmd(npycores, False)
print('NOW RUNNING "%s" --build . --target %s' % (cmake, " ".join(make_args)))
sys.stdout.flush()
status = subprocess.run([cmake, "--build", ".", "--target", *make_args])
if status.returncode != 0:
print("SOMETHING WENT WRONG WHEN COMPILING THE WRAPPERS!")
sys.exit(-1)
os.chdir(OLDPWD)
def install_module(ncores: int = 1):
print("\nInstalling the module")
OLDPWD = os.getcwd()
build_ext = _get_build_ext()
build_dir = os.path.abspath("build")
cmake = "cmake"
if is_windows:
cmake = f"{cmake}.exe"
bindir = _get_bin_dir()
cmake = os.path.join(bindir, cmake)
moduledir = os.path.join(build_dir, f"{build_ext}_module")
if not os.path.exists(moduledir):
os.makedirs(moduledir)
if not os.path.isdir(moduledir):
print("SOMETHING IS WRONG. %s is not a directory?" % moduledir)
sys.exit(-1)
os.chdir(moduledir)
if os.path.exists("CMakeCache.txt"):
# we have run cmake in this directory before. Run it again.
status = subprocess.run([cmake, "."])
else:
# this is the first time we are running cmake
sourcedir = os.path.join(
os.path.dirname(os.path.dirname(os.getcwd())), "src", "sire"
)
if not os.path.exists(os.path.join(sourcedir, "CMakeLists.txt")):
print(
"SOMETHING IS WRONG. There is no file %s"
% os.path.join(sourcedir, "CMakeLists.txt")
)
sys.exit(-1)
add_default_cmake_defs(args.wrapper, ncores)
cmake_cmd = [
cmake,
*sum([["-D", d[0]] for d in args.wrapper], []),
*sum([["-G", g[0]] for g in args.generator], []),
*sum([["-A", a[0]] for a in args.architecture], []),
sourcedir,
]
print(" ".join(cmake_cmd))
sys.stdout.flush()
status = subprocess.run(cmake_cmd)
if status.returncode != 0:
print("SOMETHING WENT WRONG WHEN USING CMAKE ON MODULE!")
sys.exit(-1)
make_args = make_cmd(ncores, True)
# Now that cmake has run, we can compile and install wrapper
print('NOW RUNNING "%s" --build . --target %s' % (cmake, " ".join(make_args)))
sys.stdout.flush()
status = subprocess.run([cmake, "--build", ".", "--target", *make_args])
if status.returncode != 0:
print("SOMETHING WENT WRONG WHEN COMPILING WRAPPER!")
sys.exit(-1)
def install(ncores: int = 1, npycores: int = 1):
print("\nInstalling sire")
OLDPWD = os.getcwd()
build_ext = _get_build_ext()
build_dir = os.path.abspath("build")
wrapperdir = os.path.join(build_dir, f"{build_ext}_wrapper")
cmake = "cmake"
if is_windows:
cmake = f"{cmake}.exe"
bindir = _get_bin_dir()
cmake = os.path.join(bindir, cmake)
if not os.path.exists(wrapperdir):
os.makedirs(wrapperdir)
if not os.path.isdir(wrapperdir):
print("SOMETHING IS WRONG. %s is not a directory?" % wrapperdir)
sys.exit(-1)
os.chdir(wrapperdir)
# Now install the wrappers
make_args = make_cmd(npycores, True)
print('NOW RUNNING "%s" --build . --target %s' % (cmake, " ".join(make_args)))
sys.stdout.flush()
status = subprocess.run([cmake, "--build", ".", "--target", *make_args])
if status.returncode != 0:
print("SOMETHING WENT WRONG WHEN COMPILING THE WRAPPERS!")
sys.exit(-1)
os.chdir(OLDPWD)
install_module(ncores=ncores)
if __name__ == "__main__":
OLDPWD = os.getcwd()
args = parse_args()
if len(args.action) != 1:
print("Please use either 'build', 'install' or 'install_module'")
sys.exit(-1)
action = args.action[0]
if is_windows and (args.generator is None or len(args.generator) == 0):
args.generator = [["Visual Studio 17 2022"]]
args.architecture = [["x64"]]
elif is_macos:
# fix compile bug when INSTALL_NAME_TOOL is not set
if "INSTALL_NAME_TOOL" not in os.environ:
os.environ["INSTALL_NAME_TOOL"] = "install_name_tool"
if action == "install":
if not args.skip_build:
build(
ncores=args.ncores[0],
npycores=args.npycores[0],
coredefs=args.corelib,
pydefs=args.wrapper,
)
install(ncores=args.ncores[0], npycores=args.npycores[0])
elif action == "build":
build(
ncores=args.ncores[0],
npycores=args.npycores[0],
coredefs=args.corelib,
pydefs=args.wrapper,
)
elif action == "install_module":
install_module(ncores=args.ncores[0])
else:
print(
f"Unrecognised action '{action}'. Please use "
"'build', 'install' or 'install_module'"
)
# Create minimist package metadata so that 'conda list sire' works.
if args.install_metadata:
os.chdir(OLDPWD)
if "CONDA_PREFIX" in os.environ:
metadata_dir = os.path.join(os.environ["CONDA_PREFIX"], "conda-meta")
if os.path.exists(metadata_dir):
# Get the Python version.
pyver = f"py{sys.version_info.major}{sys.version_info.minor}"
metadata = {
"name": "sire",
"version": open("version.txt").readline().strip(),
"build": pyver,
"build_number": 0,
"channel": "local",
"size": 0,
"license": "GPL-3.0-or-later",
"subdir": platform_string,
}
metadata_file = os.path.join(
metadata_dir, f"sire-{metadata['version']}-{pyver}.json"
)
with open(metadata_file, "w") as f:
json.dump(metadata, f, indent=2)
print(f"Created conda package metadata file: {metadata_file}")