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

Fix #9386: use variant instead of unique_ptr to prevent compilers failing on the code generation #9391

Merged
merged 6 commits into from Jun 26, 2021

Conversation

rubidium42
Copy link
Contributor

@rubidium42 rubidium42 commented Jun 21, 2021

Motivation / Problem

In the words of TB:

MacOS said: BOOOO
GCC said: fuck this shit

Description

Fixes #9386 by using std::variant to forego the need to generate code for a new and as such needing to bother with delete at a later stage.
The forwarding constructor of std::variant is quite picky (at least on MSVC). It does not like to resolve overloaded functions, so some functions needed to be renamed to be resolvable. Furthermore the 'naked' initializer for SaveLoad was causing issues at some places, so effectively 'name' it by adding SaveLoad to the SLE* macros.
Finally resolving from a variant to a type is not super trivial when you do not know the exact type, for this std::visit is used which helps but including that in a simple iterator is beyond my C++-foo, so fell back to a helper function to convert the type of the 'auto loop' to the needed/wanted const SettingDesc *.

Limitations

std::visit requires Mac OS 10.14 or higher which has been out-of-support for half a year... and that causes deprecations warnings that I can't solve.

Checklist for review

Some things are not automated, and forgotten often. This list is a reminder for the reviewers.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, gs_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

@glx22
Copy link
Contributor

glx22 commented Jun 21, 2021

Don't forget to update MACOSX_DEPLOYMENT_TARGET env variable in workflows too.

Rename the zero-parameter NetworkValidateClientName to NetworkValidateOurClientName to make it clearer it is performed on our client name, and to make it a non-overloaded function to aid with the variant being added a few commits later
One UpdateServiceInterval has two parameters to update the service interval for a vehicle type, the other for all vehicle types at once. Rename the latter to help with function resolution for the introduction of variants.
… the saveload macros

This to help variant's constructor to be able to resolve the constructor of the setting
…gDesc constructor

This as using std::string causes much more variables to be tracked, potentially causing problemes for certain compilers in certain situations
…ants instead of new + unique_ptr

With std::variant all memory can be figured out at compile time, so the compiler needs to keep track of fewer elements. It also saves out a unique_ptr and its memory management, over a slight impact for resolving a setting.
@TrueBrain
Copy link
Member

Reminder we shouldn't forget to update https://github.com/OpenTTD/website/blob/master/_data/download-descriptions.yml if this lands.

@TrueBrain TrueBrain merged commit 98e653d into OpenTTD:master Jun 26, 2021
@rubidium42 rubidium42 deleted the issue-9386 branch July 1, 2021 17:05
@inferiorhumanorgans
Copy link

inferiorhumanorgans commented May 1, 2023

I couldn't tell you how or why your CI builds work, however on my local machine running OSX 10.14 this breaks the build as CMakeLists.txt sets the compiler to target OSX 10.13 while the minimum version required is 10.14.

[ 38%] Building CXX object CMakeFiles/openttd_lib.dir/src/script/api/script_gamesettings.cpp.o
In file included from ~/OpenTTD/src/script/api/script_gamesettings.cpp:12:
~/OpenTTD/src/script/api/../../settings_internal.h:320:14: error: 'visit<(lambda at ~/OpenTTD/src/script/api/../../settings_internal.h:320:20), const
      std::__1::variant<IntSettingDesc, BoolSettingDesc, OneOfManySettingDesc, ManyOfManySettingDesc, StringSettingDesc, ListSettingDesc, NullSettingDesc> &>' is unavailable: introduced in macOS 10.14
        return std::visit([](auto&& arg) -> const SettingDesc * { return &arg; }, desc);
                    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/variant:1535:26: note: 'visit<(lambda at
      ~/OpenTTD/src/script/api/../../settings_internal.h:320:20), const std::__1::variant<IntSettingDesc, BoolSettingDesc, OneOfManySettingDesc, ManyOfManySettingDesc, StringSettingDesc,
      ListSettingDesc, NullSettingDesc> &>' has been explicitly marked unavailable here
constexpr decltype(auto) visit(_Visitor&& __visitor, _Vs&&... __vs) {
                         ^
1 error generated.
make[2]: *** [CMakeFiles/openttd_lib.dir/src/script/api/script_gamesettings.cpp.o] Error 1
make[1]: *** [CMakeFiles/openttd_lib.dir/all] Error 2
make: *** [all] Error 2
$ uname -v
Darwin Kernel Version 18.7.0: Tue Jun 22 19:37:08 PDT 2021; root:xnu-4903.278.70~1/RELEASE_X86_64
$ grep -rF 10.13 *
CMakeLists.txt:set(CMAKE_OSX_DEPLOYMENT_TARGET 10.13)
os/macosx/Info.plist.in:        <string>10.13.0</string>
$ git rev-parse head
003bab3c8a0f7c51e0dba649229f426100a726b1

@glx22
Copy link
Contributor

glx22 commented May 2, 2023

Minimum target has been changed back to 10.13 in #10253.

@inferiorhumanorgans
Copy link

Right, but builds fail with unavailable: introduced in macOS 10.14.

@glx22
Copy link
Contributor

glx22 commented May 2, 2023

Maybe it depends on Xcode version, CI currently uses Xcode 14.2 and MacOSX13.1 SDK

@inferiorhumanorgans
Copy link

Yeah I'm building (trying to) on OSX 10.14.6 with whatever the newest XCode is that I could get away with (11.3.1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Building on MacOS fails with "bus error: 10"
4 participants