-
Notifications
You must be signed in to change notification settings - Fork 15
/
build_pyoptsparse.py
executable file
·1339 lines (1115 loc) · 45.4 KB
/
build_pyoptsparse.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
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import argparse
import os
import platform
import re
import shutil
import sys
import subprocess
import tarfile
from pathlib import Path, PurePath
import tempfile
from colors import *
from shutil import which
from packaging.version import Version, parse
# Default options that the user can change with command line switches
opts = {
'prefix': str(Path(Path.home() / 'pyoptsparse')),
'linear_solver': 'mumps',
'build_pyoptsparse': True,
'intel_compiler_suite': False,
'snopt_dir': None,
'hsl_tar_file': None,
'include_paropt': False,
'include_ipopt': True,
'keep_build_dir': False,
'check_sanity': True,
'conda_cmd': 'conda',
'force_build': False,
'ignore_conda': False,
'ignore_mamba': False,
'verbose': False,
'compile_required': True, # Not set directly by the user, but determined from other options
'uninstall': False,
'pyoptsparse_version': None, # Parsed pyOptSparse version, set by finish_setup()
'make_name': 'make',
'fall_back': False
}
# Information about the host, status, and constants
sys_info = {
'gcc_major_ver': -1,
'gcc_is_apple_clang': False,
'line_color': 'white',
'msg_color': 'gray',
'gnu_sanity_check_done': False,
'python_sanity_check_done': False,
'compile_cores': int(os.cpu_count()/2),
'sys_name': platform.system(),
'conda_activate_dir': None,
'conda_deactivate_dir': None,
'conda_env_script': 'pyoptsparse_lib.sh',
'conda_forge_available': False
}
# Where to find each package, which branch to use if obtained by git,
# and which include file to test to see if it's already installed
build_info = {
'metis': {
'branch': 'releases/2.0.0',
'url': 'https://github.com/coin-or-tools/ThirdParty-Metis.git',
'src_lib_glob': 'libcoinmetis*',
'include_subdir': 'metis',
'include_file': 'metis.h'
},
'mumps': {
'branch': 'releases/3.0.2',
'url': 'https://github.com/coin-or-tools/ThirdParty-Mumps.git',
'src_lib_glob': 'libcoinmumps*',
'include_subdir': 'mumps',
'include_file': 'mumps_c_types.h'
},
'ipopt': {
'branch': 'releases/3.14.7',
'url': 'https://github.com/coin-or/Ipopt.git',
'src_lib_glob': 'lib*ipopt*',
'include_subdir': '.',
'include_glob_list': ['Ip*.hpp', 'Sens*.hpp', 'Ip*.h', 'Ip*.inc'],
'include_file': 'IpoptConfig.h'
},
'pyoptsparse': {
'branch': 'v2.10.1',
'url': 'https://github.com/mdolab/pyoptsparse.git',
},
'hsl': {
'branch': 'releases/2.2.1',
'url': 'https://github.com/coin-or-tools/ThirdParty-HSL',
'src_lib_glob': 'libcoinhsl*',
'include_subdir': 'hsl',
'include_file': 'CoinHslConfig.h'
},
'paropt': {
'branch': 'v2.1.4',
'url': 'https://github.com/smdogroup/paropt.git',
'src_lib_glob': 'libparopt*',
}
}
def process_command_line():
""" Validate command line arguments and update options, or print usage and exit. """
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description='''
Download, configure, build, and/or install pyOptSparse with dependencies.
Temporary working directories are created, which are removed after
installation unless -d is used.
When running with a conda environment active, all packages that can be installed
with conda will be, except when command line arguments modify this behavior. If
found, mamba will be used to install/uninstall unless -m is invoked.
''',
epilog='''
NOTES:
When using HSL as the linear solver, the source code tar file can be obtained
from http://www.hsl.rl.ac.uk/ipopt/
If PARDISO is selected as the linear solver, the Intel compiler suite with MKL
must be available.
Examples:
build_pyoptsparse
build_pyoptsparse --intel --linear-solver=pardiso
build_pyoptsparse -l hsl -n -t ../../coinhsl-archive-2014.01.17.tar.gz
'''
)
parser.add_argument("-a", "--paropt",
help="Add ParOpt support. Default: no ParOpt",
action="store_true",
default=opts['include_paropt'])
parser.add_argument("-b", "--branch",
help=f"pyOptSparse release. \
Default: {build_info['pyoptsparse']['branch']}",
default=build_info['pyoptsparse']['branch'])
parser.add_argument("-c", "--conda-cmd",
help=f"Command to install packages with if conda is used. \
Default: {opts['conda_cmd']}")
parser.add_argument("-d", "--no-delete",
help="Do not erase the build directories after completion.",
action="store_true",
default=opts['keep_build_dir'])
parser.add_argument("-e", "--ignore-conda",
help="Do not install conda packages, install under conda environment, \
or uninstall from the conda environment.",
action="store_true",
default=opts['ignore_conda'])
parser.add_argument("-f", "--force-build",
help="Build/rebuild packages even if found to be installed or \
can be installed with conda.",
action="store_true",
default=opts['force_build'])
parser.add_argument("-g", "--fall-back",
help="If a conda package fails to install, fall back to \
building from source. If selected, the build environment is \
tested in case it is needed.",
action="store_true",
default=opts['fall_back'])
parser.add_argument("-k", "--no-sanity-check",
help="Skip the sanity checks.",
action="store_true",
default=not opts['check_sanity'])
parser.add_argument("-i", "--intel",
help="Build with the Intel compiler suite instead of GNU.",
action="store_true",
default=opts['intel_compiler_suite'])
parser.add_argument("-l", "--linear-solver",
help="Which linear solver to use with IPOPT. Default: mumps",
choices=['mumps', 'hsl', 'pardiso'],
default=opts['linear_solver'])
parser.add_argument("-m", "--ignore-mamba",
help="Do not use mamba to install conda packages. \
Default: Use mamba if found",
action="store_true",
default=opts['ignore_mamba'])
parser.add_argument("-n", "--no-install",
help=f"Prepare, but do {yellow('not')} build/install pyOptSparse itself. \
Default: install",
action="store_true",
default=not opts['build_pyoptsparse'])
parser.add_argument("-o", "--no-ipopt",
help="Do not install IPOPT. Default: install IPOPT",
action="store_true",
default=not opts['include_ipopt']),
parser.add_argument("-p", "--prefix",
help=f"Where to install if not a conda/venv environment. Default: {opts['prefix']}",
default=opts['prefix'])
parser.add_argument("-s", "--snopt-dir",
help="Include SNOPT from SNOPT-DIR. Default: no SNOPT",
default=opts['snopt_dir'])
parser.add_argument("-t", "--hsl-tar-file",
help="If HSL is the linear solver, use this as the path \
to the tar file of the HSL source. \
E.g. -t ../../coinhsl-archive-2014.01.17.tar.gz",
default=opts['hsl_tar_file'])
parser.add_argument("-u", "--uninstall",
help="Attempt to remove include/lib files previously built from source \
(using the same --prefix) and/or installed with conda in the same \
environment, then exit. Default: Do not uninstall",
action="store_true",
default=opts['uninstall'])
parser.add_argument("-v", "--verbose",
help="Show output from git, configure, make, conda, etc. and expand \
all environment variables.",
action="store_true",
default=opts['verbose'])
args = parser.parse_args()
# Update options with user selections
opts['include_paropt'] = args.paropt
opts['include_ipopt'] = not args.no_ipopt
build_info['pyoptsparse']['branch'] = args.branch
# Determine conda settings
opts['ignore_conda'] = args.ignore_conda
opts['ignore_mamba'] = args.ignore_mamba
if opts['ignore_conda'] is False and conda_is_active():
if args.conda_cmd is not None:
opts['conda_cmd'] = args.conda_cmd
else:
if opts['ignore_mamba'] is True or which('mamba') is None:
opts['conda_cmd'] = 'conda'
else:
opts['conda_cmd'] = 'mamba'
# Make sure conda forge channel is available
if args.uninstall is False:
note('Checking for conda-forge')
cmd_list=['info','--unsafe-channels']
result = run_conda_cmd(cmd_list)
if re.search(r'conda.*forge', result.stdout) is not None:
sys_info['conda_forge_available'] = True
note_ok()
else:
print(f'{yellow("WARNING")}: The conda-forge channel is not configured, cannot \
install conda packages. Falling back to building from source.')
opts['compile_required'] = True
opts['keep_build_dir'] = args.no_delete
opts['force_build'] = args.force_build
opts['fall_back'] = args.fall_back
opts['check_sanity'] = not args.no_sanity_check
opts['linear_solver'] = args.linear_solver
if opts['linear_solver'] == 'pardiso':
opts['intel_compiler_suite'] = True
else:
opts['intel_compiler_suite'] = args.intel
opts['prefix'] = args.prefix
opts['build_pyoptsparse'] = not args.no_install
opts['snopt_dir'] = args.snopt_dir
opts['hsl_tar_file'] = args.hsl_tar_file
opts['verbose'] = args.verbose
opts['uninstall'] = args.uninstall
def announce(msg:str):
"""
Print an important message in color with a line above and below.
Parameters
----------
msg : str
The information to be printed.
"""
print(color(f' {msg} '.center(79, '-'), '#d0d0d0', 'blue'))
def note(msg:str):
"""
Print a quick status message. If not in verbose mode, do not terminate with
a newline because the result of the operation will print after.
Parameters
----------
msg : str
The information to be printed.
"""
if opts['verbose'] is False:
print(msg, end="... ")
sys.stdout.flush()
else:
print(msg + '...')
def note_ok():
""" Print a green OK message to follow up a note() with. """
if opts['verbose'] is False:
print(green('OK'))
def note_failed():
""" Print a red failure message to follow up a note() with. """
if opts['verbose'] is False:
print(red('failed'))
def code(msg:str)->str:
"""
Create a message with "code" hilighting.
Parameters
----------
msg : str
The information to be printed.
"""
return color(msg, 'orange', style='underline')
def try_fallback(pkg:str, e:Exception):
"""
When a conda package installation fails, this is called to print status info
and determine whether it's possible to try building from source.
Parameters
----------
pkg : str
The name of the package that failed to install.
e : Exception
That exception that was caught when the failure occurred.
"""
note_failed()
if opts['fall_back'] is True:
print(yellow(f'Installing {pkg} with conda failed, falling back to source build.'))
else:
print(yellow('Use the --fall-back switch to build source on failed conda installs.'))
raise e
def initialize():
""" Perform a collection of setup tasks """
global dir_stack
dir_stack = []
if conda_is_active() and (opts['ignore_conda'] is False):
opts['prefix']=os.environ['CONDA_PREFIX']
sys_info['conda_activate_dir'] = Path(opts['prefix']) / 'etc' / 'conda' / 'activate.d'
sys_info['conda_deactivate_dir'] = Path(opts['prefix']) / 'etc' / 'conda' / 'deactivate.d'
elif venv_is_active():
opts['prefix']=os.environ['VIRTUAL_ENV']
def conda_is_active() -> bool:
""" Determine if a conda environment is active. """
return ('CONDA_PREFIX' in os.environ)
def allow_install_with_conda() -> bool:
""" Determine if we can install with conda. """
return conda_is_active() and (opts['ignore_conda'] is False) and \
(sys_info['conda_forge_available'] is True)
def venv_is_active() -> bool:
""" Determine if a Python virtual environment is active. """
return ('VIRTUAL_ENV' in os.environ)
def subst_env_for_path(path:str)->str:
"""
If a well-known env var is the initial part of the path, substitute the name
of that var to make it easier to read.
Parameters
----------
path : str
The path to check for environment variables.
Returns
-------
str
The possibly updated path.
"""
if opts['verbose'] is True: return path
for testvar in ['TMPDIR', 'TMP_DIR', 'TEMP_DIR', 'CONDA_PREFIX', 'VIRTUAL_ENV']:
if testvar in os.environ and re.match(os.environ[testvar], path) is not None:
new_path = PurePath(re.sub(os.environ[testvar], f'${testvar}/', path))
return str(new_path)
return path
def run_cmd(cmd_list, do_check=True, raise_error=True)->bool:
"""
Run a command with provided arguments. Hide output unless there's an error
or verbose mode is enabled.
Parameters
----------
cmd_list : list
Each token of the command line is a separate member of the list.
do_check : bool
If true, test whether the process returns a non-zero status.
raise_error: bool
Only matters if do_check is true. If false, raise an exception
if the process returns a non-zero status. If true, do not raise
an exception, but have the function return False.
Returns
-------
subprocess.CompletedProcess
The result of the finished command.
"""
result = None
try:
result = subprocess.run(cmd_list, check=do_check, capture_output=True, text=True)
except subprocess.CalledProcessError as inst:
if opts['verbose'] is True:
print(inst.stdout, inst.stderr)
if raise_error is True:
raise inst
if opts['verbose'] is True and result is not None:
print(result.stdout, result.stderr)
return result
def check_make(errors:list):
"""
Find the best make command and test its viability.
Parameters
----------
errors : list
Accumulated pre-check error messages.
"""
if 'MAKE' in os.environ:
opts['make_name'] = os.environ['MAKE']
elif which('gmake') is not None:
opts['make_name'] = 'gmake'
if find_required_command(opts['make_name'], errors):
# If the make command is found, test whether it's GNU make
cmd_list=[opts['make_name'], '--version']
result = subprocess.run(cmd_list, check=False, capture_output=True, text=True)
if str(result.stdout).find('GNU Make') == -1:
print(f'{yellow("WARNING")}: {opts["make_name"]} is not GNU Make. '
'Source code builds may fail.')
def make_install(parallel_procs:int=sys_info['compile_cores'], make_args = None, do_install=True):
"""
Run 'make' followed by 'make install' in the current directory.
Parameters
----------
parallel_procs : int
Start this many parallel make processes. Defaults to half of the system cores.
Some packages fail when built in parallel, so 1 should be used in those cases.
"""
note('Building')
os.environ['MAKEFLAGS'] = f'-j {str(parallel_procs)}'
make_cmd=[opts['make_name']]
if make_args is not None:
make_cmd.extend(make_args)
run_cmd(cmd_list=make_cmd)
note_ok()
if do_install is True:
note('Installing')
run_cmd(cmd_list=[opts['make_name'],'install'])
note_ok()
def run_conda_cmd(cmd_args):
"""
Shorthand for performing a conda operation.
Parameters
----------
cmd_list : list
Each token of the command line is a separate member of the list. The conda
executable name is prepended, so should not be included in the list.
Returns
-------
subprocess.CompletedProcess
The result of the finished command.
"""
cmd_list = [opts['conda_cmd']]
cmd_list.extend(cmd_args)
return run_cmd(cmd_list)
def pip_install(pip_install_args, pkg_desc='packages'):
"""
Shorthand for performing a 'pip install' operation.
Parameters
----------
pip_install_args : list
Each token of the command line is a separate member of the list. The
is prepended with 'python -m pip install'; '-q' is added when not verbose.
"""
cmd_list = ['python', '-m', 'pip', 'install']
if opts['verbose'] is False:
cmd_list.append('-q')
cmd_list.extend(pip_install_args)
note(f'Installing {pkg_desc} with pip')
run_cmd(cmd_list)
note_ok()
def install_conda_pkg(pkg_name:str):
"""
Shorthand for performing a 'conda install' operation for a single package.
Parameters
----------
pkg_name : str
The name of the package to install.
"""
note(f'Installing {pkg_name.upper()} with conda')
install_args = ['install', '-y', pkg_name]
run_conda_cmd(cmd_args=install_args)
note_ok()
def pushd(dirname):
"""
Preserve the current directory name in a stack, then change to the specified directory.
Parameters
----------
dirname : str
The absolute or relative name of the folder to change to.
"""
dir_stack.append(str(Path.cwd()))
os.chdir(dirname)
print(f'Changed directory to {code(str(subst_env_for_path(dirname)))}')
def popd():
""" Change to the top directory name on the stack of names. """
dirname = dir_stack.pop()
os.chdir(dirname)
print(f'Changed directory back to {code(subst_env_for_path(dirname))}')
def get_coin_inc_dir()->str:
"""
Determine what the path to the MUMPS/METIS/IPOPT include directory is, if it exists.
Returns
-------
str
The absolute path to the correct existing directory, or None if not found.
"""
coin_inc_dirs = ['coin-or', 'coin']
for coin_dir in coin_inc_dirs:
coin_path = Path(opts["prefix"]) / 'include' / coin_dir
if coin_path.is_dir():
return str(coin_path)
return None
def get_coin_lib_name(pkg:str)->str:
"""
Determine whether the required lib starts with 'lib' or 'libcoin'.
Parameters
----------
pkg : str
The name of the library to test.
Returns
-------
str
The pkg parameter prefaced with either 'coin' or nothing.
"""
lib_vars = ['coin', '']
for lv in lib_vars:
lib_glob = f"lib{lv}{pkg}*"
found_libs = sorted(Path(f"{opts['prefix']}/lib").glob(lib_glob))
if len(found_libs) > 0:
return f'{lv}{pkg}'
return None
def git_clone(build_key:str, auto_delete:bool=True):
"""
Create a temporary directory, change to it, and clone the repository associated
with the specified package key.
Parameters
----------
build_key : str
A key in the build_info dict with info about the selected package.
auto_delete : bool
Override the 'keep_build_dir' setting. Auto-delete if true, leave if false.
Returns
-------
context manager OR str
When the 'keep_build_dir' option is False, an object with info about the directory,
which causes the directory to be cleaned up and removed when it goes out of scope.
When the 'keep_build_dir' option is True, returns a str with the name of the folder.
"""
d = build_info[build_key]
announce(f'Building {build_key.upper()} from source code')
if opts['keep_build_dir'] is True or auto_delete is False:
build_dir = tempfile.mkdtemp()
dir_name = build_dir
print(f"Remember to delete {code(subst_env_for_path(dir_name))} afterwards.")
else:
build_dir = tempfile.TemporaryDirectory()
dir_name = build_dir.name
note(f'Cloning {d["url"]}')
run_cmd(cmd_list=['git', 'clone', '-q', d['url'], dir_name])
note_ok()
pushd(dir_name)
if d["branch"]:
# We don't care about the "detached HEAD" warning:
run_cmd(cmd_list=['git', 'config', '--local', 'advice.detachedHead', 'false'])
note(f'Checking out branch {d["branch"]}')
run_cmd(cmd_list=['git', 'checkout', '-q', d['branch']])
return build_dir
def allow_build(build_key:str) -> bool:
"""
Determine whether the specified package should be built from source.
Parameters
----------
build_key : str
A key in the build_info dict with info about the selected package.
Returns
-------
bool
True if the package is not yet installed or force_build is true, false if already built.
"""
coin_dir = get_coin_inc_dir()
if coin_dir is None:
build_ok = True
else:
d = build_info[build_key]
include_file = Path(coin_dir) / d['include_subdir'] / d['include_file']
build_ok = opts['force_build'] or not include_file.is_file()
if build_ok is False:
print(f"{build_key.upper()} is already installed under {opts['prefix']}, {yellow('skipping build')}.")
return build_ok
def install_metis_from_src():
""" Git clone the METIS repo, build the library, and install it and the include files. """
if not allow_build('metis'):
return
metis_dir = git_clone('metis')
os.environ['METIS_DIR'] = metis_dir if isinstance(metis_dir, str) else metis_dir.name
run_cmd(['./get.Metis'])
os.environ['CFLAGS'] = '-Wno-implicit-function-declaration'
note("Running configure")
run_cmd(cmd_list=['./configure', f'--prefix={opts["prefix"]}'])
note_ok()
make_install()
popd()
def install_metis():
""" Install METIS either through conda or building. """
if allow_install_with_conda() and opts['force_build'] is False:
try:
install_conda_pkg('metis')
os.environ['METIS_DIR'] = os.environ['CONDA_PREFIX']
return
except Exception as e:
try_fallback('METIS', e)
install_metis_from_src()
def get_common_solver_config_cmd():
""" Gets common configuration options for Mumps and HSL solvers. """
coin_dir = get_coin_inc_dir()
cflags = f'-w -I{opts["prefix"]}/include -I{coin_dir} -I{coin_dir}/metis'
fcflags = cflags
if sys_info['gcc_major_ver'] >= 10:
fcflags = '-fallow-argument-mismatch ' + fcflags
metis_lib = get_coin_lib_name('metis')
metis_lflags = f'-L{opts["prefix"]}/lib -l{metis_lib}'
if platform.system() == "Linux":
metis_lflags += ' -lm'
config_opts = [
'--with-metis',
f'--with-metis-lflags={metis_lflags}',
f'--with-metis-cflags={cflags}',
f'--prefix={opts["prefix"]}',
f'CFLAGS={cflags}',
f'FCFLAGS={fcflags}'
]
# Disable OpenMP support if we are on macOS building with Apple Clang.
if sys_info['gcc_is_apple_clang']:
config_opts.append('--disable-openmp')
cnf_cmd_list = ['./configure']
cnf_cmd_list.extend(config_opts)
return cnf_cmd_list
def install_mumps_from_src():
""" Git clone the MUMPS repo, build the library, and install it and the include files. """
if not allow_build('mumps'):
return
build_dir = git_clone('mumps')
run_cmd(['./get.Mumps'])
cnf_cmd_list = get_common_solver_config_cmd()
note("Running configure")
run_cmd(cmd_list=cnf_cmd_list)
note_ok()
make_install(1) # MUMPS build can fail with parallel make
popd()
def install_paropt_from_src():
"""
Git clone the PAROPT repo, build the library, and install it and the include files.
"""
build_dir = git_clone('paropt')
pip_install(['Cython'], pkg_desc='Cython')
# Use build defaults as per ParOpt instructions:
Path('Makefile.in.info').rename('Makefile.in')
make_vars = [f'PAROPT_DIR={Path.cwd()}']
if sys_info['sys_name'] == 'Darwin':
make_vars.extend(['SO_EXT=dylib', 'SO_LINK_FLAGS=-fPIC -dynamiclib -undefined dynamic_lookup',
f'METIS_INCLUDE=-I{os.environ["METIS_DIR"]}/include/',
f'METIS_LIB=-L{os.environ["METIS_DIR"]}/lib/',
'-lmetis'])
else:
make_vars.extend(['SO_EXT=so', 'SO_LINK_FLAGS=-fPIC -shared',
f'METIS_INCLUDE=-I{os.environ["METIS_DIR"]}/include/',
f'METIS_LIB=-L{os.environ["METIS_DIR"]}/lib/',
'-lmetis'])
make_install(make_args=make_vars, do_install=False)
pip_install(['./'], pkg_desc='paropt')
lib_dest_dir = str(Path(opts['prefix']) / 'lib')
note(f'Copying library files to {code(subst_env_for_path(lib_dest_dir))}')
lib_files = sorted(Path('lib').glob('libparopt*'))
for lib in lib_files:
shutil.copy2(str(lib), lib_dest_dir)
note_ok()
popd()
def install_ipopt_from_src(config_opts:list=None):
"""
Git clone the IPOPT repo, build the library, and install it and the include files.
Parameters
----------
config_opts : list
Additional options to use with the IPOPT configure script.
"""
if not allow_build('ipopt') or opts['include_ipopt'] is False:
return
build_dir = git_clone('ipopt')
cnf_cmd_list = ['./configure', f'--prefix={opts["prefix"]}', '--disable-java']
# Don't accidentally use PARDISO if it wasn't selected:
if opts['linear_solver'] != 'pardiso': cnf_cmd_list.append('--disable-pardisomkl')
if config_opts is not None: cnf_cmd_list.extend(config_opts)
note("Running configure")
run_cmd(cmd_list=cnf_cmd_list)
note_ok()
make_install()
popd()
def install_ipopt(config_opts:list=None):
"""
Install IPOPT either through conda or building.
Parameters
----------
config_opts : list
Additional options to use with the IPOPT configure script if building.
"""
if allow_install_with_conda() and opts['force_build'] is False:
try:
install_conda_pkg('ipopt')
return
except Exception as e:
try_fallback('IPOPT', e)
install_ipopt_from_src(config_opts=config_opts)
def install_mumps():
""" Install MUMPS either through conda or building. """
if allow_install_with_conda() and opts['force_build'] is False:
try:
install_conda_pkg('mumps-include')
install_conda_pkg('mumps-seq')
install_conda_pkg('mumps-mpi')
return
except Exception as e:
try_fallback('MUMPS', e)
install_mumps_from_src()
def install_with_mumps():
""" Install METIS, MUMPS, and IPOPT. """
install_metis()
install_mumps()
if opts['include_ipopt'] is True:
# Get this info in case we need to build IPOPT from source
coin_dir = get_coin_inc_dir()
mumps_lib = get_coin_lib_name('mumps')
ipopt_opts = [
'--with-mumps',
f'--with-mumps-lflags=-L{opts["prefix"]}/lib -l{mumps_lib}',
f'--with-mumps-cflags=-I{coin_dir}/mumps',
'--without-asl',
'--without-hsl'
]
install_ipopt(config_opts=ipopt_opts)
def install_hsl_from_src():
""" Build HSL from the user-supplied source tar file. """
if not allow_build('hsl'):
return
build_dir = git_clone('hsl')
# Extract the HSL tar file and rename the folder to 'coinhsl'
# First, determine the name of the top-level folder:
with tarfile.open(opts['hsl_tar_file'], 'r') as tf:
hsl_dir_name = tf.getnames()[0]
run_cmd(cmd_list=['tar', 'xf', opts['hsl_tar_file']]) # Extract
Path(hsl_dir_name).rename('coinhsl') # Rename
cnf_cmd_list = get_common_solver_config_cmd()
note("Running configure")
run_cmd(cmd_list=cnf_cmd_list)
note_ok()
make_install()
popd()
def install_with_hsl():
""" Install pyOptSparse using the HSL linear solver """
install_metis()
install_hsl_from_src()
coin_dir = get_coin_inc_dir()
metis_lib = get_coin_lib_name('metis')
ipopt_opts = [
'--with-hsl',
f'--with-hsl-lflags=-L{opts["prefix"]}/lib -lcoinhsl -l{metis_lib}',
f'--with-hsl-cflags=-I{coin_dir}/hsl',
'--disable-linear-solver-loader'
]
install_ipopt_from_src(config_opts=ipopt_opts)
install_pyoptsparse_from_src()
def install_with_pardiso():
""" Build IPOPT with the PARDISO linear solver. """
# install_ipopt_from_src(config_opts=['--with-lapack=-mkl'])
install_ipopt_from_src()
# pyOptSparse doesn't do well with Intel compilers, so unset:
# select_gnu_compiler()
install_pyoptsparse_from_src()
def copy_snopt_files(build_dirname):
"""
Copy SNOPT source files into the pyOptSparse build dir, excluding snopth.f.
Parameters
----------
build_dirname : str
The directory where pyOptSparse is being built/installed from.
"""
note('Copying SNOPT source files')
snoptc_f_list = sorted(Path(opts['snopt_dir']).rglob('snoptc.f'))
all_snopt_files = sorted(Path(snoptc_f_list[0]).parent.glob('*'))
dest_dir = str(Path(build_dirname) / 'pyoptsparse' / 'pySNOPT' / 'source')
exclude_snopth_f = re.compile('.*snopth.f')
for sfile in all_snopt_files:
src_file = str(sfile)
# copy source files, exclude any directories (e.g. f2py/)
if not exclude_snopth_f.match(src_file) and not sfile.is_dir():
shutil.copy2(src_file, dest_dir)
note_ok()
def patch_pyoptsparse_src():
""" Some versions of pyOptSparse need to be modified slightly to build correctly. """
if opts['pyoptsparse_version'] < parse('2.6.3'):
pushd("pyoptsparse/pyIPOPT")
note("Patching for versions < 2.6.3")
setup_py_path = Path("setup.py")
pos_setup_py = open(str(setup_py_path))
data = pos_setup_py.read()
pos_setup_py.close()
setup_py_path.rename('setup.py.orig')
new_data = re.sub(r'libraries=.+,', 'libraries=["ipopt"],', data)
with open(str(setup_py_path), 'w') as setup_py_path:
setup_py_path.write(new_data)
note_ok()
popd()
def install_pyoptsparse_from_src():
""" Git clone the pyOptSparse repo and use pip to install it. """
# First, build PAROPT if selected:
if opts['include_paropt'] is True:
install_paropt_from_src()
build_dir = git_clone('pyoptsparse', opts['build_pyoptsparse'])
if opts['include_ipopt'] is True:
os.environ['IPOPT_INC'] = get_coin_inc_dir()
os.environ['IPOPT_LIB'] = str(Path(opts["prefix"]) / 'lib')
os.environ['IPOPT_DIR'] = str(Path(opts["prefix"]))
os.environ['CFLAGS'] = '-Wno-implicit-function-declaration -std=c99'
# Pull in SNOPT source:
if opts['snopt_dir'] is not None:
build_dir_str = build_dir if isinstance(build_dir, str) else build_dir.name
copy_snopt_files(build_dir_str)
if opts['build_pyoptsparse'] is True:
patch_pyoptsparse_src()
pip_install(pip_install_args=['--no-cache-dir', './'], pkg_desc='pyoptsparse')
else:
announce('Not building pyOptSparse by request')
if opts['include_ipopt'] is True:
print(f"""
Make sure to set these environment variables before building it yourself:
{code(f'export IPOPT_INC={subst_env_for_path(os.environ["IPOPT_INC"])}')}
{code(f'export IPOPT_LIB={subst_env_for_path(os.environ["IPOPT_LIB"])}')}
""")
popd()
def uninstall_built_item(build_key:str):
""" Uninstall a specific item that was previously built from source code. """
d = build_info[build_key]
if 'include_subdir' in d:
inc_dir = Path(opts['prefix']) / 'include' / 'coin-or' / d['include_subdir']
if 'include_glob_list' in d:
# If there's a list of glob patterns, remove found files individually instead
# of removing an entire include subdirectory:
note(f'Removing {build_key.upper()} include files')
for glob_item in d['include_glob_list']:
for inc_file in sorted(Path(inc_dir).glob(glob_item)):
Path(inc_file).unlink()
try:
inc_dir.rmdir()
except:
pass
note_ok()
else:
# If there's no chance that other include files will be installed in the same
# folder, just remove the whole subdirectory.
if inc_dir.is_dir():
note(f'Removing {build_key.upper()} include directory')
shutil.rmtree(inc_dir)
note_ok()
# Remove individual library files.
if 'src_lib_glob' in d:
lib_dir = Path(opts['prefix']) / 'lib'
lib_file_list = sorted(lib_dir.glob(d['src_lib_glob']))
if len(lib_file_list) > 0:
note(f'Removing {build_key.upper()} library files')
for lib_file in lib_file_list:
Path(lib_file).unlink()
note_ok()
def uninstall_paropt_and_pyoptsparse():
""" Both ParOpt and pyOptSparse were installed with pip. """
# Uninstall pyOptSparse
note('Removing pyOptSparse')
run_cmd(cmd_list=['pip','uninstall','-y','pyOptSparse'], do_check=False)
note_ok()
note('Removing PAROPT package')
run_cmd(cmd_list=['pip','uninstall','-y','paropt'], do_check=False)
note_ok()