From 73857703b8df3fcccd9dad28f0734d1cd9dec38b Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Wed, 15 Mar 2023 09:21:12 +0100 Subject: [PATCH] Added a check that libslic3r/I18N.hpp is not included on the frontend by mistake, also added a check that L macro is not defined when including libslic3r/I18N.hpp --- src/libslic3r/I18N.hpp | 25 +++++++++++++++++++------ src/slic3r/CMakeLists.txt | 3 +++ src/slic3r/GUI/GUI_App.cpp | 4 ++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/libslic3r/I18N.hpp b/src/libslic3r/I18N.hpp index 3bf286b192..51d41a3ea1 100644 --- a/src/libslic3r/I18N.hpp +++ b/src/libslic3r/I18N.hpp @@ -3,6 +3,12 @@ #include +#ifdef SLIC3R_CURRENTLY_COMPILING_GUI_MODULE + #ifndef SLIC3R_ALLOW_LIBSLIC3R_I18N_IN_SLIC3R + #error You included libslic3r/I18N.hpp into a file belonging to slic3r module. + #endif +#endif + namespace Slic3r { namespace I18N { @@ -15,11 +21,18 @@ namespace I18N { } // namespace Slic3r -namespace { - const char* L(const char* s) { return s; } - std::string _u8L(const char* s) { return Slic3r::I18N::translate(s); } - std::string _utf8(const char* s) { return Slic3r::I18N::translate(s); } - std::string _utf8(const std::string& s) { return Slic3r::I18N::translate(s); } -} +// When this is included from slic3r, better do not define the translation functions. +// Macros from slic3r/GUI/I18N.hpp should be used there. +#ifndef SLIC3R_CURRENTLY_COMPILING_GUI_MODULE + #ifdef L + #error L macro is defined where it shouldn't be. Didn't you include slic3r/GUI/I18N.hpp in libslic3r by mistake? + #endif + namespace { + const char* L(const char* s) { return s; } + std::string _u8L(const char* s) { return Slic3r::I18N::translate(s); } + std::string _utf8(const char* s) { return Slic3r::I18N::translate(s); } + std::string _utf8(const std::string& s) { return Slic3r::I18N::translate(s); } + } +#endif #endif /* slic3r_I18N_hpp_ */ diff --git a/src/slic3r/CMakeLists.txt b/src/slic3r/CMakeLists.txt index 2ca1998e31..db8cefa995 100644 --- a/src/slic3r/CMakeLists.txt +++ b/src/slic3r/CMakeLists.txt @@ -344,3 +344,6 @@ if (UNIX AND NOT APPLE) target_include_directories(libslic3r_gui PRIVATE ${GTK${SLIC3R_GTK}_INCLUDE_DIRS}) target_link_libraries(libslic3r_gui ${GTK${SLIC3R_GTK}_LIBRARIES} fontconfig) endif () + +# Add a definition so that we can tell we are compiling slic3r. +target_compile_definitions(libslic3r_gui PRIVATE SLIC3R_CURRENTLY_COMPILING_GUI_MODULE) diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index 5d3b65632c..4be6de6e1e 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -8,7 +8,11 @@ // Localization headers: include libslic3r version first so everything in this file // uses the slic3r/GUI version (the macros will take precedence over the functions). +// Also, there is a check that the former is not included from slic3r module. +// This is the only place where we want to allow that, so define an override macro. +#define SLIC3R_ALLOW_LIBSLIC3R_I18N_IN_SLIC3R #include "libslic3r/I18N.hpp" +#undef SLIC3R_ALLOW_LIBSLIC3R_I18N_IN_SLIC3R #include "slic3r/GUI/I18N.hpp" #include