From 06bcc851ce9cee42e22e85827f9ec48a2f414d0f Mon Sep 17 00:00:00 2001 From: Marmaduke Woodman Date: Fri, 6 May 2022 16:09:03 +0200 Subject: [PATCH 1/3] init force arch --- cmdstanpy/model.py | 4 +++- cmdstanpy/utils.py | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/cmdstanpy/model.py b/cmdstanpy/model.py index 873bff09..b57e497e 100644 --- a/cmdstanpy/model.py +++ b/cmdstanpy/model.py @@ -43,6 +43,7 @@ do_command, get_logger, returncode_msg, + macos_make_arch_args, ) from . import progress as progbar @@ -413,7 +414,8 @@ def compile( 'MAKE', 'make' if platform.system() != 'Windows' else 'mingw32-make', ) - cmd = [make] + maybe_force_arch = macos_make_arch_args() + cmd = maybe_force_arch + [make] if self._compiler_options is not None: cmd.extend(self._compiler_options.compose()) cmd.append(Path(exe_file).as_posix()) diff --git a/cmdstanpy/utils.py b/cmdstanpy/utils.py index 03b7357b..74a836c0 100644 --- a/cmdstanpy/utils.py +++ b/cmdstanpy/utils.py @@ -1478,3 +1478,24 @@ def __enter__(self) -> Tuple[str, bool]: def __exit__(self, exc_type, exc_val, exc_tb) -> None: # type: ignore if self._tmpdir: shutil.rmtree(self._tmpdir, ignore_errors=True) + + +def macos_make_arch_args(): + """Determine what, if any, `arch -arm64` prefix for the `make` invocation.""" + if sys.platform != 'darwin': + return [] + pch_name = 'stan/src/stan/model/model_header.hpp.gch' + pch_path = f'{cmdstan_path()}/{pch_name}' + with tempfile.TemporaryDirectory() as td: + with open(f'{td}/foo.cpp', 'w') as fd: + fd.write('int foo() { }') + out = subprocess.Popen( + ['g++', '-include-pch', pch_path, '-c', 'foo.cpp'], + cwd=td, stderr=subprocess.PIPE + ) + err = out.stderr.read().decode('ascii') + match = re.search(r"was compiled for the target '(.*?)-", err, re.MULTILINE) + if match: + pch_arch, = match.groups() + return ['arch', '-' + pch_arch] + return [] \ No newline at end of file From a222da06c9aaca17b3e019d3f7636aec8661ae28 Mon Sep 17 00:00:00 2001 From: Marmaduke Woodman Date: Fri, 6 May 2022 17:19:21 +0200 Subject: [PATCH 2/3] simplify arch test --- cmdstanpy/model.py | 8 +++++--- cmdstanpy/utils.py | 24 ++++++++---------------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/cmdstanpy/model.py b/cmdstanpy/model.py index b57e497e..1f40a624 100644 --- a/cmdstanpy/model.py +++ b/cmdstanpy/model.py @@ -43,7 +43,7 @@ do_command, get_logger, returncode_msg, - macos_make_arch_args, + macos_make_arch, ) from . import progress as progbar @@ -414,8 +414,10 @@ def compile( 'MAKE', 'make' if platform.system() != 'Windows' else 'mingw32-make', ) - maybe_force_arch = macos_make_arch_args() - cmd = maybe_force_arch + [make] + + cmd = [make] + if sys.platform == 'darwin': + cmd = macos_make_arch() + cmd if self._compiler_options is not None: cmd.extend(self._compiler_options.compose()) cmd.append(Path(exe_file).as_posix()) diff --git a/cmdstanpy/utils.py b/cmdstanpy/utils.py index 74a836c0..1f4d77a4 100644 --- a/cmdstanpy/utils.py +++ b/cmdstanpy/utils.py @@ -1480,22 +1480,14 @@ def __exit__(self, exc_type, exc_val, exc_tb) -> None: # type: ignore shutil.rmtree(self._tmpdir, ignore_errors=True) -def macos_make_arch_args(): - """Determine what, if any, `arch -arm64` prefix for the `make` invocation.""" +def macos_make_arch(): + """Determine what `arch -arm64` prefix required for `make` invocations.""" if sys.platform != 'darwin': return [] - pch_name = 'stan/src/stan/model/model_header.hpp.gch' - pch_path = f'{cmdstan_path()}/{pch_name}' - with tempfile.TemporaryDirectory() as td: - with open(f'{td}/foo.cpp', 'w') as fd: - fd.write('int foo() { }') - out = subprocess.Popen( - ['g++', '-include-pch', pch_path, '-c', 'foo.cpp'], - cwd=td, stderr=subprocess.PIPE - ) - err = out.stderr.read().decode('ascii') - match = re.search(r"was compiled for the target '(.*?)-", err, re.MULTILINE) + cmd = ['file', cmdstan_path() + '/bin/stansummary'] + out = subprocess.check_output(cmd).decode('ascii').strip() + match = re.search(r"Mach-O 64-bit executable (.*?)$", out) if match: - pch_arch, = match.groups() - return ['arch', '-' + pch_arch] - return [] \ No newline at end of file + arch, = match.groups() + return ['arch', '-' + arch] + return [] From ecd75163a7fa55289c2bd0e45c73203d309ffb27 Mon Sep 17 00:00:00 2001 From: Marmaduke Woodman Date: Mon, 9 May 2022 10:56:40 +0200 Subject: [PATCH 3/3] add missing return type annot --- cmdstanpy/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmdstanpy/utils.py b/cmdstanpy/utils.py index 1f4d77a4..5a1403b5 100644 --- a/cmdstanpy/utils.py +++ b/cmdstanpy/utils.py @@ -1480,7 +1480,7 @@ def __exit__(self, exc_type, exc_val, exc_tb) -> None: # type: ignore shutil.rmtree(self._tmpdir, ignore_errors=True) -def macos_make_arch(): +def macos_make_arch() -> List[str]: """Determine what `arch -arm64` prefix required for `make` invocations.""" if sys.platform != 'darwin': return []