-
Notifications
You must be signed in to change notification settings - Fork 11
/
setup.py
968 lines (763 loc) · 28.6 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
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
"""
Installation script for Sire
This assumes that the python that is used to execute this script
is the conda / miniconda / miniforge environment into
which you want to install Sire.
USAGE:
python setup.py install_requires : Will install all of the dependencies
python setup.py build : Will install requires and will then
compile sire (takes a long time!)
python setup.py install : Will build sire and will then install
python setup.py install_module : Will only install the Python module
You can use `--skip-deps` to skip the installation of the conda dependencies
You can use `--skip-build` to skip the building of the corelib and wrappers
"""
import sys
import os
import platform
import subprocess
import shutil
import glob
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}. 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
# Debug - we need to print out all of the environment variables
# for key, value in os.environ.items():
# print(f"{key}\n{value}\n")
# 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)
# We need to import the 'parse_requirements' function to get the list
# of requirements - this will be in the 'actions' directory
sys.path.insert(0, os.path.join(curdir, "actions"))
# We need to verify that this is a Python that is part of a
# conda installation
if "PREFIX" in os.environ and "BUILD_PREFIX" in os.environ:
print("This a build initiated by conda-build")
conda_base = os.path.abspath(os.environ["PREFIX"])
print(f"Setting conda-base to {conda_base}")
else:
# Find the path to the conda 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)
python_exe = None
conda = None
if "CONDA_EXE" in os.environ:
conda = os.environ["CONDA_EXE"]
else:
conda = None
if "CONDA_DEFAULT_ENV" in os.environ:
conda_env = os.environ["CONDA_DEFAULT_ENV"]
else:
conda_env = 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")
if conda is None:
conda = os.path.join(conda_base, "Scripts", "conda.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")
if conda is None:
conda = os.path.join(conda_bin, "conda")
else:
print(
"Cannot find a 'python' binary in directory '%s'. "
"Are you running this script using the python executable "
"from a valid miniconda or anaconda installation?" % 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(
"--install-bss-deps",
action="store_true",
default=False,
help="Install BioSimSpace's dependencies too. This helps ensure "
"compatibility between Sire's and BioSimSpace's dependencies.",
)
parser.add_argument(
"--install-emle-deps",
action="store_true",
default=False,
help="Install emle-engine's dependencies too.",
)
parser.add_argument(
"--skip-deps",
action="store_true",
default=False,
help="Skip the installation of the dependencies (only use if you know "
"that they are already installed)",
)
parser.add_argument(
"--skip-dep",
action="append",
help="List of dependencies to skip when installing. This is useful when "
"you know that a particular dependency is already installed or "
"it is uninstallable on your system.",
)
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(
"action",
nargs="*",
help="Should be one of 'install_requires', 'build', 'install' or 'install_module'.\n"
"\n [install_requires] : Just install the conda dependencies.\n"
" [build] : 'install_requires' plus 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 or conda dependencies).",
)
return parser.parse_args()
_installed_deps = None
def _get_installed(conda: str):
"""Return the list of installed conda dependencies"""
global _installed_deps
if _installed_deps is None:
p = subprocess.Popen([conda, "list"], stdout=subprocess.PIPE)
_installed_deps = str(p.stdout.read())
return _installed_deps
def is_installed(dep: str, conda: str) -> bool:
"""Return whether or not the passed dependency is installed"""
installed = _get_installed(conda=conda)
return installed.find(dep) != -1
def _add_to_dependencies(dependencies, lines):
import re
for line in lines:
line = line.lstrip().rstrip()
words = re.split("[<>]*=", line)
if len(words) > 0:
package = words[0]
dependencies[package] = line
_is_conda_prepped = False
dependencies_to_skip = []
def conda_install(
dependencies, install_bss_reqs=False, install_emle_reqs=False, yes=True
):
"""Install the passed list of dependencies using conda"""
conda_exe = conda
global _is_conda_prepped
if not _is_conda_prepped:
if install_bss_reqs:
cmd = "%s config --prepend channels openbiosim/label/dev" % conda_exe
print("Activating openbiosim channel channel using: '%s'" % cmd)
status = subprocess.run(cmd.split())
if status.returncode != 0:
print("Failed to add openbiosim channel!")
sys.exit(-1)
print("\nSetting channel priorities to favour conda-forge")
cmd = "%s config --prepend channels conda-forge" % conda_exe
print("Activating conda-forge channel using: '%s'" % cmd)
status = subprocess.run(cmd.split())
if status.returncode != 0:
print("Failed to add conda-forge channel!")
sys.exit(-1)
cmd = "%s config --set channel_priority strict" % conda_exe
print("Setting channel priority to strict using: '%s'" % cmd)
status = subprocess.run(cmd.split())
if status.returncode != 0:
print("Failed to set channel priority!")
sys.exit(-1)
_is_conda_prepped = True
conda_install = [conda, "install"]
if yes:
conda_install.append("--yes")
deps = []
global dependencies_to_skip
try:
if len(dependencies_to_skip) > 0:
print(f"Skipping the following dependencies: {dependencies_to_skip}")
except Exception:
dependencies_to_skip = []
for dependency in dependencies:
if dependency == "python" or is_installed(dependency, conda_exe):
# no need to install again
continue
skip_dep = False
for skip in dependencies_to_skip:
if dependency.find(skip) != -1:
skip_dep = True
break
if skip_dep:
print(f"Skipping {dependency}")
continue
# remove duplicates
dep = f'"{dependency}"'
if dep not in deps:
deps.append(dep)
dependencies = deps
cmd = [*conda_install, *dependencies]
print("\nInstalling packages using:\n\n%s\n\n" % " ".join(cmd))
status = subprocess.run(cmd)
if status.returncode != 0:
print("Something went wrong installing dependencies!")
print("If the python or conda executables were updated")
print("in the last install, then this can prevent them")
print("from running again. Please re-execute this script.")
sys.exit(-1)
# Install emle-engine.
if install_emle_reqs:
cmd = [
"pip",
"install",
"git+https://github.com/chemle/emle-engine.git",
]
status = subprocess.run(cmd)
if status.returncode != 0:
print("Something went wrong installing emle-engine!")
sys.exit(-1)
def install_requires(install_bss_reqs=False, install_emle_reqs=False, yes=True):
"""Installs all of the dependencies. This can safely be called
multiple times, as it will cache the result to prevent future
installs taking too long
"""
print(f"Installing requirements for {platform_string}")
if not os.path.exists(conda):
print("\nSire can only be installed into a conda or miniconda environment.")
print(
"Please install conda, miniconda, miniforge or similar, then "
"activate the conda environment, then rerun this installation "
"script."
)
sys.exit(-1)
try:
import pip_requirements_parser as _pip_requirements_parser
from parse_requirements import parse_requirements
except Exception:
# this didn't import - maybe we are missing pip-requirements-parser
print("Installing pip-requirements-parser")
conda_install(
["pip-requirements-parser"],
install_bss_reqs,
install_emle_reqs=False,
yes=yes,
)
try:
from parse_requirements import parse_requirements
except ImportError as e:
print("\n\n[ERROR] ** You need to install pip-requirements-parser")
print("Run `conda install -c conda-forge pip-requirements-parser\n\n")
raise e
reqs = parse_requirements("requirements_host.txt")
build_reqs = parse_requirements("requirements_build.txt")
if install_bss_reqs:
bss_reqs = parse_requirements("requirements_bss.txt")
reqs = reqs + bss_reqs
if install_emle_reqs:
emle_reqs = parse_requirements("requirements_emle.txt")
reqs = reqs + emle_reqs
dependencies = build_reqs + reqs
conda_install(dependencies, install_bss_reqs, install_emle_reqs, yes=yes)
conda_install(dependencies, install_bss_reqs, yes=yes)
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"
else:
if conda_env is not None:
ext = "_" + conda_env.replace(" ", "_").replace(".", "_")
else:
ext = ""
return os.path.basename(conda_base.replace(" ", "_").replace(".", "_")) + ext
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 build")
CXX = os.environ["CXX"]
CC = os.environ["CC"]
# make sure that these compilers are in the path
CXX_bin = shutil.which(CXX)
CC_bin = shutil.which(CC)
print(f"{CXX} => {CXX_bin}")
print(f"{CC} => {CC_bin}")
if CXX_bin is None or CC_bin is None:
print("Cannot find the compilers requested by conda-build in the PATH")
print("Please check that the compilers are installed and available.")
sys.exit(-1)
# use the full paths, in case CMake struggles
CXX = CXX_bin
CC = CC_bin
elif is_macos:
try:
CXX = glob.glob(os.path.join(bindir, "clang++"))[0]
CC = glob.glob(os.path.join(bindir, "clang"))[0]
except Exception:
conda_install(["clang", "clangxx"], False, yes=True)
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 install these, e.g. via")
print("conda install clang clangxx")
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:
# Need this version of gcc to stay compatible with conda-forge
# (i.e. gemmi needs the exact same compiler version)
conda_install(["gcc==12.3.0", "gxx==12.3.0"], False, yes=True)
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 install these, e.g. via")
print("conda install gcc gxx")
sys.exit(-1)
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:
os.environ["CC"] = CC
if CXX:
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__":
args = parse_args()
if len(args.action) != 1:
print("Please use either 'install_requires', 'build' or 'install'")
sys.exit(-1)
install_bss = args.install_bss_deps
install_emle = args.install_emle_deps
if install_emle and is_windows:
raise NotImplementedError("EMLE is current not supported on Windows")
if args.skip_dep is not None:
dependencies_to_skip = args.skip_dep
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_deps or args.skip_build):
install_requires(
install_bss_reqs=install_bss, install_emle_reqs=install_emle
)
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":
if not args.skip_deps:
install_requires(install_bss_reqs=install_bss)
build(
ncores=args.ncores[0],
npycores=args.npycores[0],
coredefs=args.corelib,
pydefs=args.wrapper,
)
elif action == "install_requires":
install_requires(
install_bss_reqs=install_bss, install_emle_reqs=install_emle, yes=False
)
elif action == "install_module":
install_module(ncores=args.ncores[0])
else:
print(
f"Unrecognised action '{action}'. Please use 'install_requires', "
"'build', 'install' or 'install_module'"
)