Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions mesonpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,13 +899,22 @@ def _run(self, cmd: Sequence[str]) -> None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also an imprecision in the commit message: the reconfigure argument is passed when the specified build directory is detected to contain a Meson build dir, not when it merely exist. And, as @rgommers already pointed out (IIRC) buildtype is not the only default argument passed. I would reword the commit message like this

BUG: do no pass default options when reconfiguring

This ensures build options specified by the user when the build was
first initialized are preserved, unless explicitly overridden.

Fixes #851.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Do I need to change the PR description too?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Updating the PR description as well may be useful - at least for me, this captured the essence of the change, and I didn't quite get it from the initial issue or PR description.

def _configure(self, reconfigure: bool = False) -> None:
"""Configure Meson project."""
if reconfigure:
default_build_options = []
else:
# default build options, overriding Meson's defaults.
# On reconfigure, the values will persist unless the user explicitly
# passes new values for them.
default_build_options = [
'-Dbuildtype=release',
'-Db_ndebug=if-release',
'-Db_vscrt=md',
]

setup_args = [
os.fspath(self._source_dir),
os.fspath(self._build_dir),
# default build options
'-Dbuildtype=release',
'-Db_ndebug=if-release',
'-Db_vscrt=md',
*default_build_options,
# user build options
*self._meson_args['setup'],
# pass native file last to have it override the python
Expand Down
20 changes: 20 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,23 @@ def test_ndebug(package_purelib_and_platlib, tmp_path, args, expected):
['ninja', '-C', os.fspath(project._build_dir), '-t', 'commands', '../plat.c^'],
stdout=subprocess.PIPE, check=True).stdout
assert (b'-DNDEBUG' in command) == expected


def test_default_options(package_purelib_and_platlib, tmp_path, mocker):
meson = mocker.spy(mesonpy.Project, '_run')

project = mesonpy.Project(package_purelib_and_platlib, tmp_path)

mesonpy_default_options = {'-Dbuildtype=release', '-Db_ndebug=if-release', '-Db_vscrt=md'}
# Check if default options were passed
assert mesonpy_default_options.issubset(meson.call_args_list[0][0][1])

options = {opt['name']: opt['value'] for opt in project._info('intro-buildoptions')}

# Check if default options have taken effect
assert options['buildtype'] == 'release'
assert options['b_ndebug'] == 'if-release'

compilers = project._info('intro-compilers')
if compilers['build']['c']['id'] in {'msvc', 'clang-cl', 'intel-cl'}:
assert options['b_vscrt'] == 'md'
29 changes: 29 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,35 @@ def test_invalid_build_dir(package_pure, tmp_path, mocker):
assert '--reconfigure' not in meson.call_args_list[0].args[1]
project.build()

def test_reconfigure_preserves_config(package_simple, tmp_path):
# initial configuration
project = mesonpy.Project(package_simple, tmp_path, meson_args={
'setup': ['-Dbuildtype=debug', '-Dwarning_level=2'],
})

initial_options = {opt['name']: opt['value'] for opt in project._info('intro-buildoptions')}

assert initial_options['buildtype'] == 'debug'
assert initial_options['debug'] is True
assert initial_options['warning_level'] == '2'

# reconfiguration with a new option
project = mesonpy.Project(package_simple, tmp_path, meson_args={
'setup': ['-Doptimization=2'],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is correct but a little obscure, given that it doesn't directly checks default/updated values of any of the build options that meson-python actually overrides. Perhaps also assert that the buildtype is and remains debug.

While we're at it here, it looks like there is no test that checks that the default buildtype is release nor the default for b_vscrt. There is an test for ndebug in test_options.py. Would you mind adding a new test there checking that the defaults are as expected on a clean build?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps also assert that the buildtype is and remains debug.

I added some assertions in the initial configuration and in the reconfiguration, but I feel like most of them were redundant, considering the last assertion initial_options == reconfigure_options

There is an test for ndebug in test_options.py. Would you mind adding a new test there checking that the defaults are as expected on a clean build?

Created a new test in test_options.py, but I am not sure if the vscrt assertion is correct, since I am not on Windows and couldn't test it.

})

reconfigure_options = {opt['name']: opt['value'] for opt in project._info('intro-buildoptions')}

assert reconfigure_options['buildtype'] == 'debug'
assert reconfigure_options['debug'] is True
assert reconfigure_options['warning_level'] == '2'

assert initial_options != reconfigure_options

initial_options['optimization'] = '2'
assert initial_options == reconfigure_options



@pytest.mark.skipif(not os.getenv('CI') or sys.platform != 'win32', reason='requires MSVC')
def test_compiler(venv, package_detect_compiler, tmp_path):
Expand Down