-
Notifications
You must be signed in to change notification settings - Fork 58
Add compiler and linker flag options and warning-as-errors switch #517
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: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -313,7 +313,8 @@ def setup(self): | |
| cflags = self.default_compiler_flags("c") + self.CFLAGS | ||
| if ccinfo.compiler != "gcc": | ||
| # QEMU's configure script adds --extra-cflags to CXXFLAGS and these flags are rejected by g++ | ||
| cflags += ["-Werror=implicit-function-declaration", "-Werror=incompatible-pointer-types"] | ||
| if self.warnings_as_errors: | ||
| cflags += ["-Werror=implicit-function-declaration", "-Werror=incompatible-pointer-types"] | ||
| self.configure_args.append("--extra-cflags=" + self.commandline_to_str(cflags)) | ||
| ldflags = self.default_ldflags + self.LDFLAGS | ||
| if self.config.portable_build: | ||
|
|
@@ -323,7 +324,8 @@ def setup(self): | |
| ldflags.append("-lpcre") | ||
| if ldflags: | ||
| self.configure_args.append("--extra-ldflags=" + self.commandline_to_str(ldflags)) | ||
| cflags += ["-Werror=implicit-function-declaration", "-Werror=incompatible-pointer-types"] | ||
|
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 we just delete this and only do it for clang? Not sure why it's here twice... |
||
| if self.warnings_as_errors: | ||
| cflags += ["-Werror=implicit-function-declaration", "-Werror=incompatible-pointer-types"] | ||
| cxxflags = self.default_compiler_flags("c++") + self.CXXFLAGS | ||
| # QEMU's configure script adds --extra-cflags to CXXFLAGS so remove duplicates | ||
| cxxflags = [flag for flag in cxxflags if flag not in cflags] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -658,6 +658,28 @@ def check_system_dependencies(self) -> None: | |
| @classmethod | ||
| def setup_config_options(cls, install_directory_help="", **kwargs) -> None: | ||
| super().setup_config_options(**kwargs) | ||
|
|
||
| cls.warnings_as_errors = cls.add_bool_option( | ||
| "warnings-as-errors", | ||
| help="Treat compiler warnings as errors (disabling this strips -Werror from default flags)", | ||
| default=True, | ||
| ) | ||
| cls.cflags = cls.add_list_option( | ||
|
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. I like the idea of adding a way to add extra CFLAGS/CXXFLAGS/LDFLAGS flag, but we are starting to hit the limits of argparse scalability so adding new options for every projects is actually hurting startup (and testing) speed quite noticeably. Are you setting this in a config file and building multiple targets at once? If not we could probably work around this by reading somethig like a CHERIBUILD_CFLAGS environment variable?
Contributor
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. Environment variable would work for me as well. I wonder if it should even prefixed with |
||
| "cflags", | ||
| metavar="FLAGS", | ||
| help="Additional C/C++ compiler flags to append", | ||
| ) | ||
| cls.cxxflags = cls.add_list_option( | ||
| "cxxflags", | ||
| metavar="FLAGS", | ||
| help="Additional C++ compiler flags to append", | ||
| ) | ||
| cls.ldflags = cls.add_list_option( | ||
| "ldflags", | ||
| metavar="FLAGS", | ||
| help="Additional linker flags to append", | ||
| ) | ||
|
|
||
| # --<target>-<suffix>/build-directory is not inherited from the unsuffixed target (unless there is only one | ||
| # supported target). | ||
| default_xtarget = cls.default_architecture() | ||
|
|
@@ -843,9 +865,14 @@ def _build_type_basic_compiler_flags(self): | |
| @property | ||
| def compiler_warning_flags(self) -> "list[str]": | ||
| if self.compiling_for_host(): | ||
| return self.common_warning_flags + self.host_warning_flags | ||
| flags = self.common_warning_flags + self.host_warning_flags | ||
| else: | ||
| return self.common_warning_flags + self.cross_warning_flags | ||
| flags = self.common_warning_flags + self.cross_warning_flags | ||
|
|
||
| if not self.warnings_as_errors: | ||
| flags = [f for f in flags if not f.startswith("-Werror")] | ||
|
|
||
| return flags | ||
|
|
||
| def default_compiler_flags(self, lang: CompilerLanguage = "c") -> "list[str]": | ||
| assert self._setup_called | ||
|
|
@@ -873,6 +900,7 @@ def default_compiler_flags(self, lang: CompilerLanguage = "c") -> "list[str]": | |
| else: | ||
| self.warning("Compiler", compiler.path, "does not support -fsanitize=cheri, please update your SDK") | ||
| if self.compiling_for_host(): | ||
| result.extend(self.cflags if lang == "c" else self.cxxflags) | ||
| return result | ||
| if self.config.csetbounds_stats: | ||
| result.extend( | ||
|
|
@@ -884,6 +912,7 @@ def default_compiler_flags(self, lang: CompilerLanguage = "c") -> "list[str]": | |
| # "-Xclang", "-cheri-bounds=everywhere-unsafe"]) | ||
| ] | ||
| ) | ||
| result.extend(self.cflags if lang == "c" else self.cxxflags) | ||
| return result | ||
|
|
||
| @property | ||
|
|
@@ -898,6 +927,7 @@ def default_ldflags(self) -> "list[str]": | |
| result.append("-fsanitize=cfi") | ||
| result.append("-fsanitize-cfi-cross-dso") | ||
| if self.compiling_for_host(): | ||
| result.extend(self.ldflags) | ||
| return result | ||
|
|
||
| # Should work fine without linker emulation (the linker should infer it from input files) | ||
|
|
@@ -918,6 +948,7 @@ def default_ldflags(self) -> "list[str]": | |
| # We need to include the constructor even if there is no reference to libstatcounters: | ||
| # TODO: always include the .a file? | ||
| result += ["-Wl,--whole-archive", "-lstatcounters", "-Wl,--no-whole-archive"] | ||
| result.extend(self.ldflags) | ||
| return result | ||
|
|
||
| def add_asan_flags(self): | ||
|
|
||
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.
Are you actually hitting those errors? I added those flags after spending too much time debugging issues that would have been caught. The first one should definitely not be happening, the second one has found some real problems.
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.
Sorry for the late reply.
Yes, as stated in one of the commit message I think two things happend
_Genericmacro on things likestrchr(), causingincompatible pointer typeincompatible pointer typeto an errorI understand and totally agree, from personal experience, with using strict compiler flags. However, as newer compilers get stricter and new code is using new language features, it is nice to be able to temporary disable certain features/warnings/errors, just to see if things still build with newer compilers/libc. Therefore, it is nice to have an opt-out mechanism.
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.
Hmm that's annoying. I agree that we will need to drop the werror once more distributions update glibc. Unless it's an easy fix?
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.
More like tons of easy fixes.
strchris used a lot. :)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.
Those should just be fixed, though I'm a bit surprised QEMU doesn't pass -std= upstream.