Skip to content

Commit

Permalink
feat: discover brew dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-auchincloss committed Apr 28, 2024
1 parent fadd0b1 commit 2c82386
Show file tree
Hide file tree
Showing 10 changed files with 275 additions and 155 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ jobs:
brew update -f
brew install libomp llvm
echo "
export PATH=\"/usr/local/opt/llvm/bin:\$PATH\"
export PATH=\"$(brew --prefix)/opt/llvm/bin:\$PATH\"
export DYLD_LIBRARY_PATH=\"$(brew --prefix)/lib:$DYLD_LIBRARY_PATH\"
export LDFLAGS=\"-L/usr/local/opt/llvm/lib\"
export CPPFLAGS=\"-I/usr/local/opt/llvm/include\"
export LDFLAGS=\"-L$(brew --prefix)/opt/llvm/lib\"
export CPPFLAGS=\"-I$(brew --prefix)/opt/llvm/include\"
export LD=ld.lld
export AR=llvm-ar
export RANLIB=llvm-ranlib
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ jobs:
brew update -f
brew install libomp llvm
echo "
export PATH=\"/usr/local/opt/llvm/bin:\$PATH\"
export PATH=\"$(brew --prefix)/opt/llvm/bin:\$PATH\"
export DYLD_LIBRARY_PATH=\"$(brew --prefix)/lib:$DYLD_LIBRARY_PATH\"
export LDFLAGS=\"-L/usr/local/opt/llvm/lib\"
export CPPFLAGS=\"-I/usr/local/opt/llvm/include\"
export LDFLAGS=\"-L$(brew --prefix)/opt/llvm/lib\"
export CPPFLAGS=\"-I$(brew --prefix)/opt/llvm/include\"
export LD=ld.lld
export AR=llvm-ar
export RANLIB=llvm-ranlib
Expand Down
6 changes: 3 additions & 3 deletions COVERAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
| src/hatch_cython/\_\_init\_\_.py | 2 | 0 | 0 | 0 | 100% |
| src/hatch_cython/config/\_\_init\_\_.py | 2 | 0 | 0 | 0 | 100% |
| src/hatch_cython/config/autoimport.py | 9 | 0 | 2 | 0 | 100% |
| src/hatch_cython/config/config.py | 139 | 9 | 58 | 5 | 92% |
| src/hatch_cython/config/defaults.py | 6 | 0 | 0 | 0 | 100% |
| src/hatch_cython/config/config.py | 142 | 9 | 60 | 6 | 92% |
| src/hatch_cython/config/defaults.py | 28 | 3 | 10 | 2 | 87% |
| src/hatch_cython/config/files.py | 35 | 1 | 16 | 2 | 94% |
| src/hatch_cython/config/flags.py | 70 | 1 | 24 | 0 | 99% |
| src/hatch_cython/config/includes.py | 15 | 1 | 8 | 1 | 91% |
Expand All @@ -17,4 +17,4 @@
| src/hatch_cython/plugin.py | 243 | 12 | 156 | 10 | 94% |
| src/hatch_cython/temp.py | 13 | 0 | 2 | 0 | 100% |
| src/hatch_cython/utils.py | 44 | 4 | 20 | 1 | 89% |
| **TOTAL** | **748** | **38** | **355** | **26** | **94%** |
| **TOTAL** | **773** | **41** | **367** | **29** | **94%** |
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ templated_win = { supported = ["int", "float", "complex"] }
templated_win_x86_64 = { supported = ["int", "float", "np.double"]}
```

## Notes

- MacOS users with brew installed will have `brew --prefix` libs and include paths added in compilation step. Code parsing is found [here](./src/hatch_cython/config/defaults.py#L11)

## Development

### Requirements
Expand Down
27 changes: 20 additions & 7 deletions src/hatch_cython/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from hatchling.builders.hooks.plugin.interface import BuildHookInterface

from hatch_cython.config.autoimport import Autoimport
from hatch_cython.config.defaults import get_default_compile, get_default_link
from hatch_cython.config.defaults import brew_path, get_default_compile, get_default_link
from hatch_cython.config.files import FileArgs
from hatch_cython.config.flags import EnvFlags, parse_env_args
from hatch_cython.config.includes import parse_includes
Expand Down Expand Up @@ -89,12 +89,25 @@ def parse_from_dict(cls: BuildHookInterface):
PlatformArgs(arg="/openmp", platforms="windows"),
PlatformArgs(arg="-fopenmp", platforms="linux"),
PlatformArgs(arg="-lomp", platforms="darwin", marker=LTPY311, apply_to_marker=running_in_ci),
PlatformArgs(
arg="-L/usr/local/opt/llvm/lib/c++ -Wl,-rpath,/usr/local/opt/llvm/lib/c++",
platforms=["darwin"],
depends_path=True,
),
]

brew = brew_path()
if brew:
link.extend(
[
PlatformArgs(
arg=f"-L{brew}/opt/llvm/lib/c++ -Wl,-rpath,{brew}/llvm/lib/c++",
platforms=["darwin"],
depends_path=True,
),
PlatformArgs(
arg="-L/usr/local/opt/llvm/lib/c++ -Wl,-rpath,/usr/local/llvm/lib/c++",
platforms=["darwin"],
depends_path=True,
),
]
)

cma = ({*cfg.compile_args}).union({*comp})
cfg.compile_args = list(cma)
seb = ({*cfg.extra_link_args}).union({*link})
Expand Down Expand Up @@ -231,7 +244,7 @@ def flush(it):

# side effect
list(map(flush, args.values()))
return flat
return list({*flat})

def asdict(self):
d = asdict(self)
Expand Down
43 changes: 39 additions & 4 deletions src/hatch_cython/config/defaults.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,53 @@
from subprocess import CalledProcessError, check_output

from hatch_cython.config.platform import PlatformArgs
from hatch_cython.constants import POSIX_CORE
from hatch_cython.utils import aarch, memo, plat

BREW = "brew"


@memo
def brew_path():
if plat() == "darwin":
# no user input - S603 is false positive
try:
proc = check_output([BREW, "--prefix"]) # noqa: S603
except CalledProcessError:
proc = None
dec = proc.decode().replace("\n", "") if proc else None
if dec and dec != "":
return dec
return "/opt/homebrew" if aarch() == "arm64" else "/usr/local"


def get_default_link():
return [
PlatformArgs(arg="-L/opt/homebrew/lib", platforms=POSIX_CORE, depends_path=True),
base = [
PlatformArgs(arg="-L/usr/local/lib", platforms=POSIX_CORE, depends_path=True),
PlatformArgs(arg="-L/usr/local/opt", platforms=POSIX_CORE, depends_path=True),
]

brew = brew_path()
if brew:
base.extend(
[
PlatformArgs(arg=f"-L{brew}/opt", platforms=POSIX_CORE, depends_path=True),
PlatformArgs(arg=f"-L{brew}/lib", platforms=POSIX_CORE, depends_path=True),
]
)
return base


def get_default_compile():
return [
base = [
PlatformArgs(arg="-O2"),
PlatformArgs(arg="-I/opt/homebrew/include", platforms=POSIX_CORE, depends_path=True),
PlatformArgs(arg="-I/usr/local/include", platforms=POSIX_CORE, depends_path=True),
]
brew = brew_path()
if brew:
base.extend(
[
PlatformArgs(arg=f"-I{brew}/include", platforms=POSIX_CORE, depends_path=True),
]
)
return base
6 changes: 0 additions & 6 deletions test_libraries/simple_structure/hatch.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ compile_args = [
"linux",
"darwin"
], arg = "-Wcpp"},
{platforms = [
"darwin"
], arch = "x86_64", arg = "-arch x86_64"},
{platforms = [
"darwin"
], arch = "arm64", arg = "-arch arm64"},
{platforms = [
"darwin"
], arch = "x86_64", arg = "-I/usr/local/opt/llvm/include", depends_path = true, marker = "python_version <= '3.10'"},
Expand Down
6 changes: 0 additions & 6 deletions test_libraries/src_structure/hatch.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ compile_args = [
"linux",
"darwin"
], arg = "-Wcpp"},
{platforms = [
"darwin"
], arch = "x86_64", arg = "-arch x86_64"},
{platforms = [
"darwin"
], arch = "arm64", arg = "-arch arm64"},
{platforms = [
"darwin"
], arch = "x86_64", arg = "-I/usr/local/opt/llvm/include", depends_path = true, marker = "python_version <= '3.10'"},
Expand Down
Loading

0 comments on commit 2c82386

Please sign in to comment.