Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In CMake, support declining items depending on the configuration used #13071

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

mmomtchev
Copy link
Contributor

@mmomtchev mmomtchev commented Apr 11, 2024

This adds support for conan CMake config files:

  • it centralizes the deduction of CMAKE_BUILD_TYPE which is not written to the trace file

  • it adds support for config generator expressions:

    $<$<CONFIG:Debug>:DEBUG_MODE>
    
  • it centralizes and fixes the handling of IMPORTED_LOCATION_<CONFIG> directives

@dcbaker dcbaker added the module:cmake Issues related to the cmake module, including cmake.subproject label Apr 11, 2024
test cases/cmake/12 generator expressions/meson.build Outdated Show resolved Hide resolved
mesonbuild/cmake/generator.py Outdated Show resolved Hide resolved
mesonbuild/cmake/generator.py Fixed Show fixed Hide fixed
mesonbuild/cmake/tracetargets.py Fixed Show fixed Hide fixed
@mmomtchev mmomtchev changed the title support CMake $<CONFIG> generator expressions In CMake, support declining items depending on the configuration used Apr 15, 2024
Comment on lines +840 to +841
mlog.debug('CMake build type set from the meson build type')
cmake_args += [f'-DCMAKE_BUILD_TYPE={self.build_type}']
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't this set CMAKE_BUILD_TYPE to "plain" in case of an unsupported build type? self.build_type falls back to the Meson build type if it isn't in the map.

Some CMake projects have a default build type they use in case no build type is provided. I don't think we should break that by setting some weird CMAKE_BUILD_TYPE.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Normally, with CMake, the CMAKE_BUILD_TYPE is considered mandatory according to their latest recommendations.

The default in CMake, unless the project has defined something else, is an empty string, which can lead to all type of problems.

I can skip setting the build type if it is not in the buildtype_map - but this would have an effect only for projects where the meson project uses an unusual build type naming scheme. I do not know beforehand what is the CMake naming scheme.

I wonder if I will be making more or less cases work if the default one is Debug/Release according to the meson build type.

You can still set it to an empty string if you want to force the project's default.

Copy link
Contributor

Choose a reason for hiding this comment

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

@jpakkane determines the vision of Meson, but if it were me, I would greatly prefer to interfere as little as possible with external tools (i.e. CMake). I would trust the subproject developers in setting the appropriate defaults for their projects more than any blanket approach implemented in Meson. Put differently, I prefer Meson to implement the simplest logic possible to enable correct building (but not any simpler). The proposed build type logic feels both too invasive and too fragile to me.

Copy link
Member

@dcbaker dcbaker left a comment

Choose a reason for hiding this comment

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

There's a bunch of fixups for previous patches later in the series, those should be squashed back into the patch that introduced the issue. I also have a few inline comments for you

if any(arg.startswith('-DCMAKE_BUILD_TYPE=') for arg in cmake_args):
# Allow to override the CMAKE_BUILD_TYPE environment variable
mlog.debug('CMake build type explicitly set')
cmake_build_type = next(arg for arg in cmake_args if arg.startswith('-DCMAKE_BUILD_TYPE=')).split('=')[1]
Copy link
Member

Choose a reason for hiding this comment

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

There's a helper in utils.universal called first that does the same thing, but is a little easier to read (IMHO), even though it is rather filter like.

@@ -134,7 +134,7 @@ def target_file(arg: str) -> str:

# Configurations (Debug, Release...)
# CMake is case-insensitive
'CONFIG': lambda x: '1' if x.upper() == trace.env.coredata.get_option(OptionKey('buildtype')).upper() else '0'
'CONFIG': lambda x: '1' if x.upper() == T.cast('str', trace.env.coredata.get_option(OptionKey('buildtype'))).upper() else '0'
Copy link
Member

Choose a reason for hiding this comment

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

Could you squash the first three patches together? They're one logical change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You do not intend to squash on merge?

@@ -54,7 +55,7 @@ def get_config_declined_property(target: CMakeTarget,
cfg = trace.build_type.upper() if trace.build_type else None
imported_cfgs: T.List[str] = []
if f'MAP_IMPORTED_CONFIG_{cfg}' in target.properties:
cfg = target.properties[f'MAP_IMPORTED_CONFIG_{cfg}'].upper()
cfg = target.properties[f'MAP_IMPORTED_CONFIG_{cfg}'][0].upper()
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem like a mypy issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it is not and I even added a unit test for this case in the next commit.

@@ -84,11 +84,12 @@ def __init__(self, name: str) -> None:
self.working_dir: T.Optional[Path] = None

class CMakeTraceParser:
def __init__(self, cmake_version: str, build_dir: Path, env: 'Environment', permissive: bool = True) -> None:
def __init__(self, cmake_version: str, build_dir: Path, env: 'Environment', build_type: str = None, permissive: bool = True) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

This breaks the strict-optional rule that we want to turn on later, so build_type: T.Optional[str] = None

cfg = trace.build_type.upper() if trace.build_type else None
imported_cfgs: T.List[str] = []
if f'MAP_IMPORTED_CONFIG_{cfg}' in target.properties:
cfg = target.properties[f'MAP_IMPORTED_CONFIG_{cfg}'].upper()
Copy link
Member

Choose a reason for hiding this comment

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

Didn't this get changed to target.properties[...][0].upper() in the last patch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it should be [0], it is an array, there is a test for this case now

Comment on lines 61 to 68
if cmake_is_debug(trace.env):
if 'DEBUG' in imported_cfgs:
cfg = 'DEBUG'
elif 'RELEASE' in imported_cfgs:
cfg = 'RELEASE'
else:
if 'RELEASE' in imported_cfgs:
cfg = 'RELEASE'
Copy link
Member

Choose a reason for hiding this comment

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

I think we can simplify this to:

if cmake_is_debug(trace.env) and 'DEBUG' in imported_cfgs:
    cfg = 'DEBUG'
else:
    cfg = 'RELEASE'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the previous author's version, I simplified it

@@ -12,7 +11,7 @@
import typing as T

if T.TYPE_CHECKING:
from .traceparser import CMakeTraceParser
from .traceparser import CMakeTraceParser, CMakeTarget
Copy link
Member

Choose a reason for hiding this comment

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

Should this be squashed into a previous patch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module:cmake Issues related to the cmake module, including cmake.subproject
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants