Skip to content

Commit

Permalink
factor out homebrew utils and make better errors
Browse files Browse the repository at this point in the history
Signed-off-by: Jade Abraham <[email protected]>
  • Loading branch information
jabraham17 committed Jun 10, 2024
1 parent 9466a6a commit 52dbae3
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 84 deletions.
5 changes: 3 additions & 2 deletions util/chplenv/chpl_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys

import chpl_platform, chpl_locale_model, overrides
import homebrew_utils
from utils import which, error, memoize, warning


Expand Down Expand Up @@ -428,7 +429,7 @@ def get_system_compile_args(flag):
paths.append('-I/usr/local/include')

# Add Homebrew include directory if Homebrew is installed
homebrew_prefix = chpl_platform.get_homebrew_prefix()
homebrew_prefix = homebrew_utils.get_homebrew_prefix()
if homebrew_prefix:
paths.append('-I' + homebrew_prefix + '/include')

Expand Down Expand Up @@ -474,7 +475,7 @@ def get_system_link_args(flag):
paths.append('-L/usr/local/lib')

# Add Homebrew lib directory if Homebrew is installed
homebrew_prefix = chpl_platform.get_homebrew_prefix()
homebrew_prefix = homebrew_utils.get_homebrew_prefix()
if homebrew_prefix:
paths.append('-L' + homebrew_prefix + '/lib')

Expand Down
28 changes: 4 additions & 24 deletions util/chplenv/chpl_gmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,6 @@ def get():

return gmp_val


@memoize
def has_system_gmp_install():
# try pkg-config
if third_party_utils.pkgconfig_has_system_package('gmp'):
return True
# try homebrew
if chpl_platform.get_homebrew_prefix('gmp'):
return True
return False

@memoize
def get_uniq_cfg_path():
return third_party_utils.default_uniq_cfg_path()
Expand All @@ -65,12 +54,8 @@ def get_compile_args():
args = third_party_utils.pkgconfig_get_system_compile_args('gmp')
if args != (None, None):
return args
# try homebrew
gmp_prefix = chpl_platform.get_homebrew_prefix('gmp')
if gmp_prefix:
return ([], ['-I{0}'.format(os.path.join(gmp_prefix, 'include'))])

error("Could not find a suitable GMP installation. Please install GMP or set CHPL_GMP=bundled or CHPL_GMP=none.")
else:
third_party_utils.could_not_find_pkgconfig_pkg("gmp", "CHPL_GMP")

return ([ ], [ ])

Expand All @@ -87,13 +72,8 @@ def get_link_args():
args = third_party_utils.pkgconfig_get_system_link_args('gmp')
if args != (None, None):
return args
# try homebrew
gmp_prefix = chpl_platform.get_homebrew_prefix('gmp')
if gmp_prefix:
return ([], ['-L{0}'.format(os.path.join(gmp_prefix, 'lib')),
'-lgmp'])

error("Could not find a suitable GMP installation. Please install GMP or set CHPL_GMP=bundled or CHPL_GMP=none.")
else:
third_party_utils.could_not_find_pkgconfig_pkg("gmp", "CHPL_GMP")

return ([ ], [ ])

Expand Down
26 changes: 6 additions & 20 deletions util/chplenv/chpl_hwloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@ def get_compile_args():
args = third_party_utils.pkgconfig_get_system_compile_args('hwloc')
if args != (None, None):
return args
# try homebrew
hwloc_prefix = chpl_platform.get_homebrew_prefix('hwloc')
if hwloc_prefix:
return ([], ['-I{0}'.format(os.path.join(hwloc_prefix, 'include'))])

error("Could not find a suitable hwloc installation. Please install hwloc or set CHPL_HWLOC=bundled or CHPL_HWLOC=none.")
else:
third_party_utils.could_not_find_pkgconfig_pkg("hwloc", "CHPL_HWLOC")

return ([ ], [ ])

Expand All @@ -66,14 +62,8 @@ def get_link_args():
error("CHPL_HWLOC=system requires hwloc >= 2.1", ValueError)

return third_party_utils.pkgconfig_get_system_link_args('hwloc')
# try homebrew
hwloc_prefix = chpl_platform.get_homebrew_prefix('hwloc')
if hwloc_prefix:
# TODO: this should also check the version
return ([], ['-L{0}'.format(os.path.join(hwloc_prefix, 'lib')),
'-lhwloc'])

error("Could not find a suitable hwloc installation. Please install hwloc or set CHPL_HWLOC=bundled or CHPL_HWLOC=none.")
else:
third_party_utils.could_not_find_pkgconfig_pkg("hwloc", "CHPL_HWLOC")

return ([ ], [ ])

Expand All @@ -89,12 +79,8 @@ def get_prefix():
prefix = run_command(['pkg-config', '--variable', 'prefix', 'hwloc'])
if prefix:
return prefix.strip()
# try homebrew
hwloc_prefix = chpl_platform.get_homebrew_prefix('hwloc')
if hwloc_prefix:
return hwloc_prefix.strip()

error("Could not find a suitable hwloc installation. Please install hwloc or set CHPL_HWLOC=bundled or CHPL_HWLOC=none.")
else:
third_party_utils.could_not_find_pkgconfig_pkg("hwloc", "CHPL_HWLOC")

return ''

Expand Down
23 changes: 7 additions & 16 deletions util/chplenv/chpl_jemalloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import optparse

import chpl_bin_subdir, chpl_compiler, chpl_mem, chpl_platform, overrides, third_party_utils
import homebrew_utils
from utils import error, memoize, run_command, warning


Expand Down Expand Up @@ -97,13 +98,9 @@ def get_compile_args(flag):
args = third_party_utils.pkgconfig_get_system_compile_args('jemalloc')
if args != (None, None):
return args
# try homebrew
jemalloc_prefix = chpl_platform.get_homebrew_prefix('jemalloc')
if jemalloc_prefix:
return ([], ['-I{0}'.format(os.path.join(jemalloc_prefix, 'include'))])

envname = "CHPL_TARGET_JEMALLOC" if flag == "target" else "CHPL_HOST_JEMALLOC"
error("Could not find a suitable jemalloc installation. Please install jemalloc or set {}=bundled".format(envname, envname))
else:
envname = "CHPL_TARGET_JEMALLOC" if flag == "target" else "CHPL_HOST_JEMALLOC"
third_party_utils.could_not_find_pkgconfig_pkg("jemalloc", envname)

return ([ ], [ ])

Expand Down Expand Up @@ -133,15 +130,9 @@ def get_link_args(flag):
args = third_party_utils.pkgconfig_get_system_link_args('jemalloc')
if args != (None, None):
return args
# try homebrew
jemalloc_prefix = chpl_platform.get_homebrew_prefix('jemalloc')
if jemalloc_prefix:
return ([], ['-L{0}'.format(os.path.join(jemalloc_prefix, 'lib')),
'-ljemalloc'])

envname = "CHPL_TARGET_JEMALLOC" if flag == "target" else "CHPL_HOST_JEMALLOC"
error("Could not find a suitable jemalloc installation. Please install jemalloc or set {}=bundled or {}=none.".format(envname, envname))

else:
envname = "CHPL_TARGET_JEMALLOC" if flag == "target" else "CHPL_HOST_JEMALLOC"
third_party_utils.could_not_find_pkgconfig_pkg("jemalloc", envname)
return ([ ], [ ])

def _main():
Expand Down
3 changes: 2 additions & 1 deletion util/chplenv/chpl_llvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import chpl_bin_subdir, chpl_arch, chpl_compiler, chpl_platform, overrides
from chpl_home_utils import get_chpl_third_party, get_chpl_home
import chpl_gpu
import homebrew_utils
from utils import which, memoize, error, run_command, try_run_command, warning
from collections import defaultdict

Expand Down Expand Up @@ -254,7 +255,7 @@ def find_system_llvm_config():
return llvm_config


homebrew_prefix = chpl_platform.get_homebrew_prefix()
homebrew_prefix = homebrew_utils.get_homebrew_prefix()

paths = [ ]
for vers in llvm_versions():
Expand Down
16 changes: 0 additions & 16 deletions util/chplenv/chpl_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,6 @@ def is_arch_linux():
arch_file = "/etc/arch-release"
return os.path.exists(arch_file)

# if running on a system with homebrew, return the homebrew prefix
# if not, return None
@memoize
def get_homebrew_prefix(pkg=None):
# Check to see if Homebrew is installed. If it is, return the prefix.
cmd = ['brew', '--prefix']
if pkg is not None:
cmd.append(str(pkg))
exists, retcode, my_out, my_err = try_run_command(cmd)
if exists and retcode == 0:
# Make sure to include homebrew search path
homebrew_prefix = my_out.strip()
return homebrew_prefix

return None

def _main():
parser = optparse.OptionParser(usage='usage: %prog [--host|target])')
parser.add_option('--host', dest='flag', action='store_const',
Expand Down
43 changes: 43 additions & 0 deletions util/chplenv/homebrew_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python3

import sys

from utils import error, memoize, try_run_command

@memoize
def get_homebrew_prefix(pkg=None):
"""
If running on a system with Homebrew, return the Homebrew prefix.
If not, return None.
If pkg is provided, return the Homebrew prefix for that package.
"""
cmd = ['brew', '--prefix']
if pkg is not None:
cmd.append(str(pkg))
exists, retcode, my_out, _ = try_run_command(cmd)
if exists and retcode == 0:
# Make sure to include homebrew search path
homebrew_prefix = my_out.strip()
return homebrew_prefix

return None

@memoize
def homebrew_exists():
"""Check if Homebrew is installed on the system."""
return get_homebrew_prefix() is not None

@memoize
def homebrew_pkg_exists(pkg):
"""Check if a Homebrew package is installed on the system."""
cmd = ['brew', 'list', pkg]
exists, retcode, _, _ = try_run_command(cmd)
return exists and retcode == 0

def _main():
sys.stdout.write("{0}\n".format(get_homebrew_prefix()))


if __name__ == '__main__':
_main()
17 changes: 12 additions & 5 deletions util/chplenv/third_party_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import chpl_lib_pic, chpl_locale_model, chpl_platform
from chpl_home_utils import get_chpl_home, get_chpl_third_party, using_chapel_module
from utils import error, memoize, run_command, warning, try_run_command
import homebrew_utils

#
# This is the default unique configuration path which
Expand Down Expand Up @@ -98,10 +99,6 @@ def filter_libs(bundled_libs, system_libs):

return (bundled_ret, system_ret)

@memoize
def pkgconfig_has_system_package(pkg):
exists, returncode, _, _ = try_run_command(['pkg-config', '--exists', pkg])
return exists and returncode == 0
#
# Return compiler arguments required to use a system library known to
# pkg-config. The pkg argument should be the name of a system-installed
Expand Down Expand Up @@ -174,7 +171,8 @@ def pkgconfig_default_static():
@memoize
def pkgconfig_get_system_link_args(pkg, static=pkgconfig_default_static()):
# check that pkg-config knows about the package in question
if not pkgconfig_has_system_package(pkg):
exists, returncode, my_stdout, my_stderr = try_run_command(['pkg-config', '--exists', pkg])
if returncode:
return (None, None)
# run pkg-config to get the link flags
static_arg = [ ]
Expand Down Expand Up @@ -429,3 +427,12 @@ def read_bundled_pkg_config_file(pkg, ucp='', pcfile=''):
replace_path = install_path

return (read_pkg_config_file(pcpath, find_path, replace_path), pcpath)


def could_not_find_pkgconfig_pkg(pkg, envname):
if homebrew_utils.homebrew_exists() and homebrew_utils.homebrew_pkg_exists(pkg):
# tell user to install pkg-config as well
error("{0} is installed via homebrew, but pkg-config is not installed. Please install pkg-config with `brew install pkg-config`.".format(pkg))
else:
install_str = " with `brew install {0}` ".format(pkg) if homebrew_utils.homebrew_exists() else ""
error("Could not find a suitable {0} installation. Please install {0}{1}or set {2}=bundled`.".format(pkg, install_str, envname))

0 comments on commit 52dbae3

Please sign in to comment.