-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Internal IR for compiler args #14748
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
base: master
Are you sure you want to change the base?
Changes from all commits
7f519f1
9e858ba
e38e4fb
d03773a
f5c35c7
8243a7c
45aff63
fd3bc13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # Copyright © 2025 Intel Corporation | ||
|
|
||
| """An abstract IR for compile arguments. | ||
|
|
||
| This provides us with a way to pass arguments around in a non-compiler specific | ||
| way internally, and lower them into a non-abstract format when writing them in | ||
| the backend. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
| import dataclasses | ||
| import typing as T | ||
|
|
||
| if T.TYPE_CHECKING: | ||
| from typing_extensions import TypeAlias | ||
|
|
||
| # A Union of all Argument types | ||
| Argument: TypeAlias = T.Union[ | ||
| 'Opaque', 'Warning', 'Error', 'Define', 'Undefine', 'LinkerSearch', | ||
| 'LinkLibrary', 'Rpath', | ||
| ] | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class Opaque: | ||
|
|
||
| """An opaque argument. | ||
|
|
||
| This is an argument of unknown type, and Meson will do nothing but proxy it | ||
| through, except to apply a prefix to the value if it thinks it's necessary. | ||
|
|
||
| :param value: | ||
| """ | ||
|
|
||
| value: str | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class Warning: | ||
|
|
||
| """A compiler warning. | ||
|
|
||
| :param target: The warning to enable or disable. This will be stored as | ||
| given (ie, we don't try to convert between compilers). | ||
| :param enable: If true then enable the warning, otherwise suppress it. | ||
| """ | ||
|
|
||
| target: str | ||
| enable: bool | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class Error: | ||
|
|
||
| """A compiler error. | ||
|
|
||
| :param target: The warning to enable or disable. This will be stored as | ||
| given (ie, we don't try to convert between compilers). | ||
| """ | ||
|
|
||
| target: str | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class Define: | ||
|
|
||
| """A pre-processor define. | ||
|
|
||
| :param target: The value to define. | ||
| :param value: An optional value to set the define to. If undefined them the | ||
| value will be defined with no value. | ||
| """ | ||
|
|
||
| target: str | ||
| value: T.Optional[str] | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class Undefine: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For warnings we have enable=True, why not the same for Define?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fact since (EDIT: fixed)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are NOT the same, one sets it to implicitly For both,
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I really want to make it impossible to represent invalid state. So we basically have three distinct cases: So... would representing it as: class Define:
name: str
value: str | boolwork correctly? You'd have value = False mean
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, sorry. I meant that
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe, but would that mean the user sets
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes... OTOH you do want to collapse Maybe some kind of
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know that we have to guarantee we pass exactly the same arguments as long as we pass equivalent arguments. Especially since we already attempt to do some level of de-duplication. I'm perfectly fine with |
||
|
|
||
| """A pre-processor undefine. | ||
|
|
||
| :param target: The value to define. | ||
| """ | ||
|
|
||
| target: str | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class LinkerSearch: | ||
|
|
||
| """A location for the linker to search for libraries. | ||
|
|
||
| :param path: The path to search. | ||
| """ | ||
|
|
||
| path: str | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class LinkLibrary: | ||
|
|
||
| """A library to link with. | ||
|
|
||
| :param name: The name of the library to link. | ||
| :param absolute: If the path is an absolute path | ||
| """ | ||
|
|
||
| name: str | ||
| absolute: bool = False | ||
|
|
||
|
|
||
| class Rpath: | ||
|
|
||
| """A runtime-path to add to a linked library. | ||
|
|
||
| :param path: the path to add | ||
| """ | ||
|
|
||
| path: str | ||
|
|
||
|
|
||
| # TODO: rpath | ||
| # TODO: rust cfgs | ||
| # TODO: other flags we might handle differently and want to convert like lto, sanitizers, etc | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| import subprocess | ||
| import typing as T | ||
|
|
||
| from ... import arguments | ||
| from ... import mesonlib | ||
| from ... import mlog | ||
| from ...options import OptionKey, UserStdOption | ||
|
|
@@ -539,6 +540,59 @@ def get_preprocess_to_file_args(self) -> T.List[str]: | |
| lang = gnu_lang_map.get(self.language, 'assembler-with-cpp') | ||
| return self.get_preprocess_only_args() + [f'-x{lang}'] | ||
|
|
||
| def make_arguments_abstract(self, args: T.List[str]) -> T.List[arguments.Argument]: | ||
| ret: T.List[arguments.Argument] = [] | ||
|
|
||
| for arg in args: | ||
| if arg.startswith('-D'): | ||
| v: T.Optional[str] | ||
| arg = arg.removeprefix('-D') | ||
| if '=' in arg: | ||
| k, v = arg.split('=') | ||
| else: | ||
| k, v = arg, None | ||
| ret.append(arguments.Define(k, v)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Defines can be spelled on the command line as
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's possible we will have to do something more sophisticated before this can land, right now my biggest concern is getting to the point that i can prove that the IR can do everything we currently do with the POSIX style string parsing, and solve some of the difficult problems we can't solve easily. So, for the moment I'd rather not get hung up here, but I will leave these opened to keep them in mind.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also an issue we'll need to resolve for Rust because |
||
| elif arg.startswith('-U'): | ||
| ret.append(arguments.Undefine(arg.removeprefix('-U'))) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same is true for |
||
| elif arg.startswith('-l'): | ||
| ret.append(arguments.LinkerSearch(arg.removeprefix('-l'))) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. search? |
||
| elif arg.startswith('-Werror='): | ||
| ret.append(arguments.Error(arg.removeprefix('-Werror='))) | ||
| elif arg.startswith('-Wno-'): | ||
| ret.append(arguments.Warning(arg.removeprefix('-Wno-'), False)) | ||
| elif arg.startswith('-W'): | ||
| ret.append(arguments.Warning(arg.removeprefix('-W'), True)) | ||
| else: | ||
| ret.append(arguments.Opaque(arg)) | ||
|
|
||
| return ret | ||
|
|
||
| def make_arguments_concrete(self, args: T.List[arguments.Argument]) -> T.List[str]: | ||
| ret: T.List[str] = [] | ||
|
|
||
| for arg in args: | ||
| match arg: | ||
| case arguments.Define(name, None): | ||
| ret.append(f'-D{name}') | ||
| case arguments.Define(name, value): | ||
| ret.append(f'-D{name}={value}') | ||
| case arguments.Undefine(name): | ||
| ret.append(f'-U{name}') | ||
| case arguments.Error(name): | ||
| ret.append(f'-Werror={name}') | ||
| case arguments.Warning(name, True): | ||
| ret.append(f'-W{name}') | ||
| case arguments.Warning(name, False): | ||
| ret.append(f'-Wno-{name}') | ||
| case arguments.LinkerSearch(name): | ||
| ret.append(f'-L{name}') | ||
| case arguments.LinkLibrary(name): | ||
| ret.append(f'-l{name}') | ||
| case arguments.Opaque(value): | ||
| ret.append(value) | ||
|
|
||
| return ret | ||
|
|
||
|
|
||
| class GnuCompiler(GnuLikeCompiler): | ||
| """ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think an
enablemember went missing here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I the
enabledocstring is a copy-paste error. There is not-Wno-error=...in any compiler AFAIK, so no need to represent it.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/any/every/ ?
So I guess yeah, if it can't be universally represented it might as well be opaque whenever it appears.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Originally I had Warning() with both enable and error fields, but realized that you could end up with the invalid state of
errordisabled. I guess we could leave it and have some kind of validation pass later, but I prefer to not be able to model invalid state if possible.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-Wno-error is valid at least on gcc and clang. Given f.c:
you have:
Without the flag you get an error with both compilers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, GCC does not document that on their warnings page, just
-W, -Wnoand-Werror=`Since it does exist I'll fold it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you could have an enum
DISABLED, DEFAULT_LEVEL, WARNING, ERRORcorresponding to-Wno-,-W,-Wno-error=,-Werror=.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I was looking at the htmlized version of the info page, It's probably there but not readily obvious