Skip to content

Commit 1e5cf66

Browse files
committed
Compose stanc args separately
1 parent a40b6a1 commit 1e5cf66

2 files changed

Lines changed: 24 additions & 28 deletions

File tree

cmdstanpy/compiler_opts.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,16 @@ def validate_stanc_opts(self) -> None:
169169
for opt in ignore:
170170
del self._stanc_options[opt]
171171
if paths is not None:
172-
self._stanc_options['include-paths'] = paths
173-
bad_paths = [
174-
dir
175-
for dir in self._stanc_options['include-paths']
176-
if not os.path.exists(dir)
177-
]
172+
bad_paths = [dir for dir in paths if not os.path.exists(dir)]
178173
if any(bad_paths):
179174
raise ValueError(
180175
'invalid include paths: {}'.format(', '.join(bad_paths))
181176
)
182177

178+
self._stanc_options['include-paths'] = [
179+
os.path.abspath(os.path.expanduser(path)) for path in paths
180+
]
181+
183182
def validate_cpp_opts(self) -> None:
184183
"""
185184
Check cpp compiler args.
@@ -254,19 +253,19 @@ def add(self, new_opts: "CompilerOptions") -> None: # noqa: disable=Q000
254253

255254
def add_include_path(self, path: str) -> None:
256255
"""Adds include path to existing set of compiler options."""
256+
path = os.path.abspath(os.path.expanduser(path))
257257
if 'include-paths' not in self._stanc_options:
258258
self._stanc_options['include-paths'] = [path]
259259
elif path not in self._stanc_options['include-paths']:
260260
self._stanc_options['include-paths'].append(path)
261261

262-
def compose(self) -> List[str]:
263-
"""Format makefile options as list of strings."""
262+
def compose_stanc(self) -> List[str]:
264263
opts = []
265264
if self._stanc_options is not None and len(self._stanc_options) > 0:
266265
for key, val in self._stanc_options.items():
267266
if key == 'include-paths':
268267
opts.append(
269-
'STANCFLAGS+=--include-paths='
268+
'--include-paths='
270269
+ ','.join(
271270
(
272271
Path(p).as_posix()
@@ -275,9 +274,14 @@ def compose(self) -> List[str]:
275274
)
276275
)
277276
elif key == 'name':
278-
opts.append(f'STANCFLAGS+=--name={val}')
277+
opts.append(f'--name={val}')
279278
else:
280-
opts.append(f'STANCFLAGS+=--{key}')
279+
opts.append(f'--{key}')
280+
return opts
281+
282+
def compose(self) -> List[str]:
283+
"""Format makefile options as list of strings."""
284+
opts = ['STANCFLAGS+=' + flag for flag in self.compose_stanc()]
281285
if self._cpp_options is not None and len(self._cpp_options) > 0:
282286
for key, val in self._cpp_options.items():
283287
opts.append(f'{key}={val}')

cmdstanpy/model.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ def __init__(
148148
if not self._name:
149149
self._name, _ = os.path.splitext(filename)
150150

151-
# TODO: When minimum version is 2.27, use --info instead
152151
# if program has include directives, record path
153152
with open(self._stan_file, 'r') as fd:
154153
program = fd.read()
@@ -275,22 +274,15 @@ def src_info(self) -> Dict[str, Any]:
275274
if self.stan_file is None:
276275
return result
277276
try:
278-
includes = ''
279-
if (
280-
self.stanc_options is not None
281-
and 'include-paths' in self.stanc_options
282-
):
283-
print(self.stanc_options)
284-
includes = '--include-paths=' + ','.join(
285-
Path(p).as_posix()
286-
for p in self.stanc_options['include-paths'] # type: ignore
287-
)
288-
cmd = [
289-
os.path.join('.', 'bin', 'stanc' + EXTENSION),
290-
includes,
291-
'--info',
292-
self.stan_file,
293-
]
277+
cmd = (
278+
[os.path.join('.', 'bin', 'stanc' + EXTENSION)]
279+
# handle include-paths, allow-undefined etc
280+
+ self._compiler_options.compose_stanc()
281+
+ [
282+
'--info',
283+
self.stan_file,
284+
]
285+
)
294286
sout = io.StringIO()
295287
do_command(cmd=cmd, cwd=cmdstan_path(), fd_out=sout)
296288
result = json.loads(sout.getvalue())

0 commit comments

Comments
 (0)