From 3d4afd33a714f2faa50bd6930bc2a9cb08729770 Mon Sep 17 00:00:00 2001 From: Rob Falck Date: Tue, 6 May 2025 13:49:42 -0400 Subject: [PATCH 1/4] Added --pip-cmd option that allows the user to specify "uv pip" if they want to use uv pip to install pyoptsparse. Existing values for compiler options (CC, FC, CXX) will no longer be overridden if already set by the user. This was necessary for compatibility with brew compilers on MacOS. --- build_pyoptsparse.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/build_pyoptsparse.py b/build_pyoptsparse.py index 7a49b9c..3eb03b8 100755 --- a/build_pyoptsparse.py +++ b/build_pyoptsparse.py @@ -36,7 +36,8 @@ 'uninstall': False, 'pyoptsparse_version': None, # Parsed pyOptSparse version, set by finish_setup() 'make_name': 'make', - 'fall_back': False + 'fall_back': False, + 'pip_cmd': 'pip' } # Information about the host, status, and constants @@ -184,6 +185,9 @@ def process_command_line(): 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("--pip-cmd", + help=f"pip command to use. Set to to --pip-cmd=='uv pip' if using uv. Default: {opts['pip_cmd']}", + default=opts['pip_cmd']) parser.add_argument("-s", "--snopt-dir", help="Include SNOPT from SNOPT-DIR. Default: no SNOPT", default=opts['snopt_dir']) @@ -250,6 +254,7 @@ def process_command_line(): opts['prefix'] = args.prefix opts['build_pyoptsparse'] = not args.no_install + opts['pip_cmd'] = args.pip_cmd opts['snopt_dir'] = args.snopt_dir opts['hsl_tar_file'] = args.hsl_tar_file opts['verbose'] = args.verbose @@ -487,11 +492,11 @@ def pip_install(pip_install_args, pkg_desc='packages'): 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'] + cmd_list = opts['pip_cmd'].split() + ['install'] if opts['verbose'] is False: cmd_list.append('-q') cmd_list.extend(pip_install_args) - note(f'Installing {pkg_desc} with pip') + note(f'Installing {pkg_desc} with {opts["pip_cmd"]}') run_cmd(cmd_list) note_ok() @@ -1158,7 +1163,7 @@ def check_sanity(): check_make(errors) required_cmds.extend(['git', os.environ['CC'], os.environ['CXX'], os.environ['FC']]) if opts['build_pyoptsparse'] is True: - required_cmds.extend(['pip', 'swig']) + required_cmds.extend(['uv', 'swig']) if opts['compile_required'] is False: required_cmds.append(opts['conda_cmd']) @@ -1197,17 +1202,23 @@ def check_sanity(): def select_intel_compilers(): """ Set environment variables to use Intel compilers. """ - os.environ['CC'] = 'icc' - os.environ['CXX'] = 'icpc' - os.environ['FC'] = 'ifort' + if 'CC' not in os.environ: + os.environ['CC'] = 'icc' + if 'CXX' not in os.environ: + os.environ['CXX'] = 'icpc' + if 'FC' not in os.environ: + os.environ['FC'] = 'ifort' sys_info['gcc_major_ver'] = -1 sys_info['gcc_is_apple_clang'] = False def select_gnu_compilers(): """ Set environment variables to use GNU compilers. """ - os.environ['CC'] = 'gcc' - os.environ['CXX'] = 'g++' - os.environ['FC'] = 'gfortran' + if 'CC' not in os.environ: + os.environ['CC'] = 'gcc' + if 'CXX' not in os.environ: + os.environ['CXX'] = 'g++' + if 'FC' not in os.environ: + os.environ['FC'] = 'gfortran' gcc_ver = subprocess.run(['gcc', '-dumpversion'], capture_output=True) sys_info['gcc_major_ver'] = int(gcc_ver.stdout.decode('UTF-8').split('.')[0]) gcc_version = subprocess.run(['gcc', '--version'], capture_output=True) From 68d787f4f421a6fbcb3ac4ee15c7aace69b38bcd Mon Sep 17 00:00:00 2001 From: Rob Falck Date: Tue, 6 May 2025 15:44:20 -0400 Subject: [PATCH 2/4] cleanup --- build_pyoptsparse.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/build_pyoptsparse.py b/build_pyoptsparse.py index 3eb03b8..5139a5e 100755 --- a/build_pyoptsparse.py +++ b/build_pyoptsparse.py @@ -1202,23 +1202,17 @@ def check_sanity(): def select_intel_compilers(): """ Set environment variables to use Intel compilers. """ - if 'CC' not in os.environ: - os.environ['CC'] = 'icc' - if 'CXX' not in os.environ: - os.environ['CXX'] = 'icpc' - if 'FC' not in os.environ: - os.environ['FC'] = 'ifort' + os.environ.get('CC', 'icc') + os.environ.get('CXX', 'icpc') + os.environ.get('FC', 'ifort') sys_info['gcc_major_ver'] = -1 sys_info['gcc_is_apple_clang'] = False def select_gnu_compilers(): """ Set environment variables to use GNU compilers. """ - if 'CC' not in os.environ: - os.environ['CC'] = 'gcc' - if 'CXX' not in os.environ: - os.environ['CXX'] = 'g++' - if 'FC' not in os.environ: - os.environ['FC'] = 'gfortran' + os.environ.get('CC', 'gcc') + os.environ.get('CXX', 'g++') + os.environ.get('FC', 'gfortran') gcc_ver = subprocess.run(['gcc', '-dumpversion'], capture_output=True) sys_info['gcc_major_ver'] = int(gcc_ver.stdout.decode('UTF-8').split('.')[0]) gcc_version = subprocess.run(['gcc', '--version'], capture_output=True) From 84634f8b7061f52e2a748a751f54ef14e71d5734 Mon Sep 17 00:00:00 2001 From: Rob Falck Date: Wed, 7 May 2025 09:17:32 -0400 Subject: [PATCH 3/4] remove uv from required commands. The user is expected to have uv if they set the pip-cmd to `uv pip`. --- build_pyoptsparse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build_pyoptsparse.py b/build_pyoptsparse.py index 5139a5e..03f5b05 100755 --- a/build_pyoptsparse.py +++ b/build_pyoptsparse.py @@ -1163,7 +1163,7 @@ def check_sanity(): check_make(errors) required_cmds.extend(['git', os.environ['CC'], os.environ['CXX'], os.environ['FC']]) if opts['build_pyoptsparse'] is True: - required_cmds.extend(['uv', 'swig']) + required_cmds.extend(['swig']) if opts['compile_required'] is False: required_cmds.append(opts['conda_cmd']) From e3ac029239f8aed3ad76b6cf1a00e4cc24cfc351 Mon Sep 17 00:00:00 2001 From: Rob Falck Date: Wed, 7 May 2025 10:44:36 -0400 Subject: [PATCH 4/4] Make sure we are setting compiler envs if not already set. --- build_pyoptsparse.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/build_pyoptsparse.py b/build_pyoptsparse.py index 03f5b05..f7d99e2 100755 --- a/build_pyoptsparse.py +++ b/build_pyoptsparse.py @@ -1202,17 +1202,17 @@ def check_sanity(): def select_intel_compilers(): """ Set environment variables to use Intel compilers. """ - os.environ.get('CC', 'icc') - os.environ.get('CXX', 'icpc') - os.environ.get('FC', 'ifort') + os.environ['CC'] = os.environ.get('CC', 'icc') + os.environ['CXX'] = os.environ.get('CXX', 'icpc') + os.environ['FC'] = os.environ.get('FC', 'ifort') sys_info['gcc_major_ver'] = -1 sys_info['gcc_is_apple_clang'] = False def select_gnu_compilers(): """ Set environment variables to use GNU compilers. """ - os.environ.get('CC', 'gcc') - os.environ.get('CXX', 'g++') - os.environ.get('FC', 'gfortran') + os.environ['CC'] = os.environ.get('CC', 'gcc') + os.environ['CXX'] = os.environ.get('CXX', 'g++') + os.environ['FC'] = os.environ.get('FC', 'gfortran') gcc_ver = subprocess.run(['gcc', '-dumpversion'], capture_output=True) sys_info['gcc_major_ver'] = int(gcc_ver.stdout.decode('UTF-8').split('.')[0]) gcc_version = subprocess.run(['gcc', '--version'], capture_output=True)