Skip to content

cmake: unify GCC, Clang and PNaCl compiler configuration #1197

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

Merged
merged 3 commits into from
Sep 10, 2024

Conversation

illwieckz
Copy link
Member

@illwieckz illwieckz commented Jun 20, 2024

  • unify GCC, Clang and PNaCl compiler configuration
  • pass more build options to the sub-cmake game build
  • always set C standard, it will be required for building zlib with Saigo or Wasi
  • add options to not enforce C/C++ standards
  • move the target specific build options to another if/else block

- unify GCC, Clang and PNaCl compiler configuration
- pass more build options to the sub-cmake game build
- always set C standard, it will be required for building zlib with Saigo or Wasi
- add options to not enforce C/C++ standards
- move the target specific build options to another if/else block
@illwieckz
Copy link
Member Author

Now PNaCl uses the same configuration as GCC and Clang, avoids discrepancies like this to be introduced:

This also make possible to pass build options like LTO to the sub-cmake game call. I'm not sure it really does something with PNaCl as the produced files are the same size (and LTO usually makes our binaries much smaller), but the compiler accepts the option so, it's not bad to pass it. It will be ready for other compilers like Saigo or Wasi.

The C standard is now always enforced, it was required for NaCl because NaCl uses some in-tree zlib source. It makes possible to use an in-tree zlib for things other than PNaCl, something we would want for Saigo or Wasi. And if we ship an own zlib, there is no reason to not build the engine against it. The usage or the in-tree zlib for every binary is not part of that PR, but this PR makes the CMake code ready for it.

Also, for debugging purpose, one can disable the C/C++ standard enforcement with advanced USE_RECOMMENDED_C_STANDARD and USE_RECOMMENDED_CXX_STANDARD variables.

@illwieckz illwieckz force-pushed the illwieckz/cmake-unify branch 3 times, most recently from 28b8504 to 83e7281 Compare June 20, 2024 17:51
@illwieckz
Copy link
Member Author

illwieckz commented Jun 20, 2024

I forgot to mention it, but this also makes explicit the optimization level used for the selected build profile. In the future I plan to make RelWithDebInfo use -O3 so everything will be there to just flip the number. For releases we currently use RelWithDebInfo with default -O2:

@illwieckz
Copy link
Member Author

I forgot to mention it, but this also makes explicit the optimization level used for the selected build profile.

Well, this is probably a bad idea, even if someday we override them.

The reason why I went this way was because the NaCl code path as doing it, so when I unified it with other compilers, I thought “why not?”. It happens the NaCl code path doing it is a workaround for CMake failing to detect that PNaCl is a clang-compatible compiler. We better want to rely on extensive compiler detection as implemented in #982 to set the fallbacks for every clang-compatible compiler we detect:

So for now I'll revert the unconditional setting of compiler optimizations and keep the if (NACL) workaround until we can rely on something more generic thanks to #982.

@illwieckz illwieckz force-pushed the illwieckz/cmake-unify branch 3 times, most recently from 39c18bd to a4b5c8d Compare June 21, 2024 07:01
@slipher
Copy link
Member

slipher commented Jun 22, 2024

Why should there be an option not to use the appropriate language standard? I don't get it. Maybe it was useful one time, but not everything needs to be a checkbox option, if someone needs to do something really obscure they can edit the build system.

@illwieckz illwieckz force-pushed the illwieckz/cmake-unify branch from a4b5c8d to 5468d65 Compare June 22, 2024 09:37
@illwieckz
Copy link
Member Author

Why should there be an option not to use the appropriate language standard?

Generic answer: I like knobs.

Specific answers:

  • It makes easier for testing other standards without the default ones getting in the way, so it could be useful as a debug tool the day we raise the standards.
  • It also makes easy to test the build without those flags, I remember that when I toyed with Android at first it was failing to build with the flags but the without it built. Finding the root cause of why it didn't built with the flag was a specific task that was a different task than testing the rest of the code.
  • Not all the code base require those standards. For example C++14 isn't required to build a working game server, C++11 is enough.
  • I really like to be able to disable stuff for testing and debugging purpose in general, and I had to comment out those lines at least three times in the recent years for testing purpose, and editing code breaks rebasing and other things.

# || _BSD_SOURCE || _SVID_SOURCE || _GNU_SOURCE
# isascii:
# _XOPEN_SOURCE || _DEFAULT_SOURCE || _SVID_SOURCE
add_definitions(-D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=500)
Copy link
Member

Choose a reason for hiding this comment

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

What platform is this stuff needed on?

Copy link
Member Author

Choose a reason for hiding this comment

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

With -std=c++14 instead of -std=gnu++14.

Copy link
Member

Choose a reason for hiding this comment

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

I would understand -DUSE_RECOMMENDED_C_STANDARD=0 as telling the build system to get out of the way and not set any C standard flags. I oppose having it mean keep setting language standard flags, but it a different way.

Copy link
Member Author

Choose a reason for hiding this comment

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

The code now only sets GNU dialects.

if (USE_HARDENING)
message(STATUS "Enabling hardening")

# PNaCl accepts the flags bug do not define __stack_chk_guard and __stack_chk_fail.
Copy link
Member

Choose a reason for hiding this comment

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

Typos, but does not

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.


# Link-time optimizations.
if (USE_LTO)
message(STATUS "Enabling link time optimizations")
Copy link
Member

Choose a reason for hiding this comment

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

The combination of adding these verbose messages, and passing unimplemented-in-NaCl build options such as USE_ARCH_INTRINSICS or USE_LTO to the NaCl build is unfortunate. It just causes the sub-build to lie with false messages such "Enabling link time optimizations"

Copy link
Member Author

Choose a reason for hiding this comment

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

I removed the “Enabling link time optimizations” message, it was not saying the feature was enabled but the user asked for it so it was wrong anyway.

Passing the USE_LTO option doesn't sound problematic. If the compiler lies about supporting LTO, it's not our fault. I also turned the flto setting as a “try”, so even setting USE_LTO on compilers that would reject the flag wouldn't break the build, and CMake will report the status. PNaCl will lie but that's not our problem.

The USE_ARCH_INTRINSICS would make sense the day we use Wasm, as Wasm support some kind of SIMD. Right now it's a bit useless for PNaCL but PNaCl not doing anything with it doesn't mean it's wrong to pass the option, in case the toolchain can do something with it.

Copy link
Member Author

@illwieckz illwieckz Sep 7, 2024

Choose a reason for hiding this comment

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

USE_LTO is not passed anymore to the NACL sub-cmake invocation, also the option is ignored if for some reason the option is passed (like doing some manual stuff). After investigation, it happens that neither PNaCl's underlying clang nor Saigo clang support the -flto option. It happens that the pnacl-clang wrapper accepts the option but I don't see how it would do something with it, and the produced binaries are the sames anyway. On Saigo side, clang accepts the option but the link fails (the LTO linker plugin probably doesn't support the NaCl bitcode to begin with).

@slipher
Copy link
Member

slipher commented Jun 24, 2024

Having a knob that makes the program fail to build kinda sucks. But on the other hand, it sucks that there is often no way to disable all the flags added by our build system, like you could with something like CXXGLAGS='' in a simpler build system. So I can see the utility of having a way to knock out some flags. 👍

@illwieckz illwieckz force-pushed the illwieckz/cmake-unify branch from 5468d65 to a508b0a Compare June 27, 2024 16:00
@illwieckz illwieckz force-pushed the illwieckz/cmake-unify branch from a508b0a to d70ac44 Compare June 27, 2024 16:58
@illwieckz
Copy link
Member Author

I moved the setting of PNaCl default build flag to the PNaCl toolchain itself. It makes much sense like that. I haven't set the -O0 for debug as it is not what CMake does for other clang-based compilers:

@illwieckz illwieckz force-pushed the illwieckz/cmake-unify branch 2 times, most recently from 130a688 to 0a35127 Compare June 27, 2024 21:48
@illwieckz illwieckz force-pushed the illwieckz/cmake-unify branch 4 times, most recently from 7e0bf64 to 4fa40eb Compare September 7, 2024 12:22
@illwieckz
Copy link
Member Author

This now looks ready to me.

@slipher
Copy link
Member

slipher commented Sep 10, 2024

LGTM

@illwieckz illwieckz merged commit c659393 into master Sep 10, 2024
9 checks passed
@illwieckz illwieckz deleted the illwieckz/cmake-unify branch September 10, 2024 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants