MegaGlest / megaglest-source

MegaGlest real-time strategy game engine (cross-platform, 3-d)
http://megaglest.org/
357 stars 90 forks source link

Build fails with LTO #275

Closed eli-schwartz closed 1 month ago

eli-schwartz commented 8 months ago

I tried to build with the following *FLAGS to optimize the build: -flto=4 -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing

Note the -Werror=* flags are used to help detect cases where the compiler tries to optimize by assuming UB cannot exist in the source code -- if it does exist, ordinarily the code would be miscompiled, and this says to make the miscompilation a fatal error.

I got this error:

[100%] Linking CXX executable megaglest
cd /var/tmp/portage/games-strategy/megaglest-3.13.0-r1/work/megaglest-3.13.0_build/source/glest_game && /usr/bin/cmake -E cmake_link_script CMakeFiles/megaglest.dir/link.txt --verbose=1
/usr/bin/x86_64-pc-linux-gnu-g++ -march=native -fstack-protector-all -O2 -pipe -fdiagnostics-color=always -frecord-gcc-switches -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -fstack-clash-protection -flto=4 -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing  -Wformat -Werror=format-security -O3  -g -O3  -DCUSTOM_DATA_INSTALL_PATH=/usr/share/megaglest/ -Wl,-O1 -Wl,--as-needed -flto=4 -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing -Wl,--defsym=__gentoo_check_ldflags__=0 -rdynamic CMakeFiles/megaglest.dir/ai/ai.cpp.o CMakeFiles/megaglest.dir/ai/ai_interface.cpp.o CMakeFiles/megaglest.dir/ai/ai_rule.cpp.o CMakeFiles/megaglest.dir/ai/path_finder.cpp.o CMakeFiles/megaglest.dir/facilities/auto_test.cpp.o CMakeFiles/megaglest.dir/facilities/components.cpp.o CMakeFiles/megaglest.dir/facilities/game_util.cpp.o CMakeFiles/megaglest.dir/facilities/logger.cpp.o CMakeFiles/megaglest.dir/game/chat_manager.cpp.o CMakeFiles/megaglest.dir/game/commander.cpp.o CMakeFiles/megaglest.dir/game/console.cpp.o CMakeFiles/megaglest.dir/game/game.cpp.o CMakeFiles/megaglest.dir/game/game_camera.cpp.o CMakeFiles/megaglest.dir/game/script_manager.cpp.o CMakeFiles/megaglest.dir/game/stats.cpp.o CMakeFiles/megaglest.dir/global/config.cpp.o CMakeFiles/megaglest.dir/global/core_data.cpp.o CMakeFiles/megaglest.dir/global/lang.cpp.o CMakeFiles/megaglest.dir/global/metrics.cpp.o CMakeFiles/megaglest.dir/graphics/particle_type.cpp.o CMakeFiles/megaglest.dir/graphics/renderer.cpp.o CMakeFiles/megaglest.dir/graphics/unit_particle_type.cpp.o CMakeFiles/megaglest.dir/gui/display.cpp.o CMakeFiles/megaglest.dir/gui/gui.cpp.o CMakeFiles/megaglest.dir/gui/selection.cpp.o CMakeFiles/megaglest.dir/main/battle_end.cpp.o CMakeFiles/megaglest.dir/main/intro.cpp.o CMakeFiles/megaglest.dir/main/main.cpp.o CMakeFiles/megaglest.dir/main/program.cpp.o CMakeFiles/megaglest.dir/menu/main_menu.cpp.o CMakeFiles/megaglest.dir/menu/menu_background.cpp.o CMakeFiles/megaglest.dir/menu/menu_state_about.cpp.o CMakeFiles/megaglest.dir/menu/menu_state_connected_game.cpp.o CMakeFiles/megaglest.dir/menu/menu_state_custom_game.cpp.o CMakeFiles/megaglest.dir/menu/menu_state_graphic_info.cpp.o CMakeFiles/megaglest.dir/menu/menu_state_join_game.cpp.o CMakeFiles/megaglest.dir/menu/menu_state_keysetup.cpp.o CMakeFiles/megaglest.dir/menu/menu_state_load_game.cpp.o CMakeFiles/megaglest.dir/menu/menu_state_masterserver.cpp.o CMakeFiles/megaglest.dir/menu/menu_state_mods.cpp.o CMakeFiles/megaglest.dir/menu/menu_state_new_game.cpp.o CMakeFiles/megaglest.dir/menu/menu_state_options.cpp.o CMakeFiles/megaglest.dir/menu/menu_state_options_graphics.cpp.o CMakeFiles/megaglest.dir/menu/menu_state_options_network.cpp.o CMakeFiles/megaglest.dir/menu/menu_state_options_sound.cpp.o CMakeFiles/megaglest.dir/menu/menu_state_root.cpp.o CMakeFiles/megaglest.dir/menu/menu_state_scenario.cpp.o CMakeFiles/megaglest.dir/menu/server_line.cpp.o CMakeFiles/megaglest.dir/network/client_interface.cpp.o CMakeFiles/megaglest.dir/network/connection_slot.cpp.o CMakeFiles/megaglest.dir/network/network_interface.cpp.o CMakeFiles/megaglest.dir/network/network_manager.cpp.o CMakeFiles/megaglest.dir/network/network_message.cpp.o CMakeFiles/megaglest.dir/network/network_protocol.cpp.o CMakeFiles/megaglest.dir/network/network_types.cpp.o CMakeFiles/megaglest.dir/network/server_interface.cpp.o CMakeFiles/megaglest.dir/sound/sound_container.cpp.o CMakeFiles/megaglest.dir/sound/sound_renderer.cpp.o CMakeFiles/megaglest.dir/type_instances/command.cpp.o CMakeFiles/megaglest.dir/type_instances/faction.cpp.o CMakeFiles/megaglest.dir/type_instances/object.cpp.o CMakeFiles/megaglest.dir/type_instances/resource.cpp.o CMakeFiles/megaglest.dir/type_instances/unit.cpp.o CMakeFiles/megaglest.dir/type_instances/upgrade.cpp.o CMakeFiles/megaglest.dir/types/command_type.cpp.o CMakeFiles/megaglest.dir/types/damage_multiplier.cpp.o CMakeFiles/megaglest.dir/types/element_type.cpp.o CMakeFiles/megaglest.dir/types/faction_type.cpp.o CMakeFiles/megaglest.dir/types/object_type.cpp.o CMakeFiles/megaglest.dir/types/projectile_type.cpp.o CMakeFiles/megaglest.dir/types/resource_type.cpp.o CMakeFiles/megaglest.dir/types/skill_type.cpp.o CMakeFiles/megaglest.dir/types/tech_tree.cpp.o CMakeFiles/megaglest.dir/types/tileset_model_type.cpp.o CMakeFiles/megaglest.dir/types/unit_type.cpp.o CMakeFiles/megaglest.dir/types/upgrade_type.cpp.o CMakeFiles/megaglest.dir/world/map.cpp.o CMakeFiles/megaglest.dir/world/minimap.cpp.o CMakeFiles/megaglest.dir/world/scenario.cpp.o CMakeFiles/megaglest.dir/world/surface_atlas.cpp.o CMakeFiles/megaglest.dir/world/tileset.cpp.o CMakeFiles/megaglest.dir/world/time_flow.cpp.o CMakeFiles/megaglest.dir/world/unit_updater.cpp.o CMakeFiles/megaglest.dir/world/water_effects.cpp.o CMakeFiles/megaglest.dir/world/world.cpp.o -o megaglest  ../shared_lib/sources/streflop/libstreflop.a ../shared_lib/liblibmegaglest.a -Wl,-Bstatic -lSDL2main -Wl,-Bdynamic -lSDL2 -lGL -lGLU -lopenal -lvorbisfile -llua5.1 -lm -ljpeg -lpng -lcurl -lssl -lcrypto -lGLEW ../shared_lib/sources/streflop/libstreflop.a -lX11 -lSM -lICE -lX11 -lXext -lopenal -ldl -lfontconfig -lftgl -lfreetype -lminiupnpc -lircclient -llua5.1 -lm -ljpeg -lpng -lGLEW 
/var/tmp/portage/games-strategy/megaglest-3.13.0-r1/work/megaglest-3.13.0/source/glest_game/menu/menu_state_connected_game.h:42:6: error: type ‘Glest::Game::FTPMessageType’ violates the C++ One Definition Rule [-Werror=odr]
   42 | enum FTPMessageType {
      |      ^
/var/tmp/portage/games-strategy/megaglest-3.13.0-r1/work/megaglest-3.13.0/source/glest_game/menu/menu_state_mods.h:24:6: note: an enum with different value name is defined in another translation unit
   24 | enum FTPMessageType {
      |      ^
/var/tmp/portage/games-strategy/megaglest-3.13.0-r1/work/megaglest-3.13.0/source/glest_game/menu/menu_state_connected_game.h:43:5: note: name ‘ftpmsg_MissingNone’ differs from name ‘ftpmsg_None’ defined in another translation unit
   43 |     ftpmsg_MissingNone,
      |     ^
/var/tmp/portage/games-strategy/megaglest-3.13.0-r1/work/megaglest-3.13.0/source/glest_game/menu/menu_state_mods.h:25:5: note: mismatching definition
   25 |     ftpmsg_None,
      |     ^
lto1: some warnings being treated as errors
lto-wrapper: fatal error: /usr/bin/x86_64-pc-linux-gnu-g++ returned 1 exit status
compilation terminated.
/usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status
make[2]: *** [source/glest_game/CMakeFiles/megaglest.dir/build.make:1472: source/glest_game/megaglest] Error 1

Downstream report: https://bugs.gentoo.org/926143 Full build log: build.log

andy5995 commented 1 month ago

Thanks for the report @eli-schwartz. I've opened draft #289 and will try to fix it.

Not surprisingly, I can reproduce this on Manjaro.

eli-schwartz commented 1 month ago

Note that in general I would suggest using -flto in CI, not in CMakeLists.txt ;) although the addition of the flags that are pure warning flags can definitely be enforced.

Also: -flto=4 says to use 4 parallel jobs, this is a setting that is well tuned to my laptop but not necessarily anyone else's machine. But I copy-pasted my flags verbatim as part of the report. :P

andy5995 commented 1 month ago

Note that in general I would suggest using -flto in CI, not in CMakeLists.txt ;)

I was about to ask! I appreciate your suggestion.

So far I've moved the -flto to the CI and the 3 other Werror flags I moved into CMake. Is that what you meant?

Now I'm gonna do some hunting to actually find the problem

eli-schwartz commented 1 month ago

So far I've moved the -flto to the CI and the 3 other Werror flags I moved into CMake. Is that what you meant?

Yeah.