From c4be6be29d0ea5bafcf75a9082d1054958de4e9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=9F=E6=88=98?= <40414978+PatriceJiang@users.noreply.github.com> Date: Thu, 9 Mar 2023 09:45:22 +0800 Subject: [PATCH] Fixes for GCC compatibility and Linux platform warnings (#14511) * fix compile error on linux * ignore more warnning * move StringPool & StringEqual into StringPool --- .github/workflows/native-compile-linux.yml | 2 +- native/cmake/predefine.cmake | 8 +- .../audio/common/utils/include/tinysndfile.h | 4 +- native/cocos/audio/include/AudioMacros.h | 2 +- native/cocos/base/StringPool.h | 31 ++-- native/cocos/bindings/sebind/intl/common.h | 172 ++++-------------- native/cocos/core/assets/TextureBase.h | 7 +- native/cocos/plugins/bus/EventBus.h | 21 ++- templates/linux/CMakeLists.txt | 6 +- 9 files changed, 83 insertions(+), 170 deletions(-) diff --git a/.github/workflows/native-compile-linux.yml b/.github/workflows/native-compile-linux.yml index e14046f2c6..4bd5a5b11b 100644 --- a/.github/workflows/native-compile-linux.yml +++ b/.github/workflows/native-compile-linux.yml @@ -37,7 +37,7 @@ jobs: sudo apt install ninja-build -y - name: Compile env: - COCOS_ENGINE_DEV: 0 + COCOS_ENGINE_DEV: 1 run: | NATIVE_ROOT=$GITHUB_WORKSPACE/native echo "Compiling Linux ... " diff --git a/native/cmake/predefine.cmake b/native/cmake/predefine.cmake index bca18a34dd..e9799f7da5 100644 --- a/native/cmake/predefine.cmake +++ b/native/cmake/predefine.cmake @@ -116,7 +116,13 @@ if("$ENV{COCOS_ENGINE_DEV}" EQUAL "1") set(WERROR_FLAGS "-Werror -Werror=return-type") # -Wshorten-64-to-32 -Werror=return-type if(APPLE) - set(WERROR_FLAGS "${WERROR_FLAGS} -Wno-deprecated-declarations") + set(WERROR_FLAGS " ${WERROR_FLAGS} -Wno-deprecated-declarations") + elseif(LINUX) + set(WERROR_FLAGS " ${WERROR_FLAGS} -Wno-nullability-completeness -Wno-deprecated-declarations") + endif() + + if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") + set(WERROR_FLAGS " ${WERROR_FLAGS} -Wno-invalid-offsetof") endif() if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") diff --git a/native/cocos/audio/common/utils/include/tinysndfile.h b/native/cocos/audio/common/utils/include/tinysndfile.h index 6fc39eee2d..bdf3705e2a 100644 --- a/native/cocos/audio/common/utils/include/tinysndfile.h +++ b/native/cocos/audio/common/utils/include/tinysndfile.h @@ -35,7 +35,7 @@ namespace sf { // visible to clients using sf_count_t = int; -using SF_INFO = struct { +struct SF_INFO { sf_count_t frames; int samplerate; int channels; @@ -55,7 +55,7 @@ using SNDFILE = struct SNDFILE_; #define SF_FORMAT_PCM_32 8 #define SF_FORMAT_PCM_24 10 -using snd_callbacks = struct { +struct snd_callbacks { void *(*open)(const char *path, void *user); size_t (*read)(void *ptr, size_t size, size_t nmemb, void *datasource); int (*seek)(void *datasource, long offset, int whence); //NOLINT(google-runtime-int) diff --git a/native/cocos/audio/include/AudioMacros.h b/native/cocos/audio/include/AudioMacros.h index 1dda79f342..e7564c8f23 100644 --- a/native/cocos/audio/include/AudioMacros.h +++ b/native/cocos/audio/include/AudioMacros.h @@ -73,4 +73,4 @@ if (!!(condition)) { \ CC_LOG_DEBUG("(" QUOTEME(condition) ") failed, message: " fmt, ##__VA_ARGS__); \ break; \ - } \ No newline at end of file + } diff --git a/native/cocos/base/StringPool.h b/native/cocos/base/StringPool.h index 3e807f2574..bbd30f55ce 100644 --- a/native/cocos/base/StringPool.h +++ b/native/cocos/base/StringPool.h @@ -35,24 +35,23 @@ namespace cc { -namespace { -class StringHasher final { -public: - ccstd::hash_t operator()(const char *str) const noexcept { - return ccstd::hash_range(str, str + strlen(str)); - } -}; - -class StringEqual final { -public: - bool operator()(const char *c1, const char *c2) const noexcept { - return strcmp(c1, c2) == 0; - } -}; -} // namespace - template class StringPool final { +private: + class StringHasher final { + public: + ccstd::hash_t operator()(const char *str) const noexcept { + return ccstd::hash_range(str, str + strlen(str)); + } + }; + + class StringEqual final { + public: + bool operator()(const char *c1, const char *c2) const noexcept { + return strcmp(c1, c2) == 0; + } + }; + public: StringPool() = default; ~StringPool(); diff --git a/native/cocos/bindings/sebind/intl/common.h b/native/cocos/bindings/sebind/intl/common.h index 82e48acae6..7c800b7d2d 100644 --- a/native/cocos/bindings/sebind/intl/common.h +++ b/native/cocos/bindings/sebind/intl/common.h @@ -330,7 +330,7 @@ ResultType mapTupleArguments(se::Object *self, TupleIn &input, std::index_sequen using map_tuple = typename Mapping::mapping_list::tuple_type; using result_type = typename Mapping::result_types_tuple_mutable; static_assert(std::is_same::value, "result_type mismatch"); - //if constexpr (std::tuple_size::value > 0) { + // if constexpr (std::tuple_size::value > 0) { return result_type(ArgumentFilter::forward(self, input)...); //} // return result_type(); @@ -465,40 +465,26 @@ struct StaticAttribute; template struct Constructor> : ConstructorBase { // no `if constexpr`, more code is needed... - template - struct ConstructFn; - template <> - struct ConstructFn { - template - static auto invoke(Self *self, se::Object *thisObj, std::tuple &args, std::index_sequence n) { - using type_mapping = TypeMapping>; - using map_list_type = typename type_mapping::mapping_list; - using map_tuple_type = typename type_mapping::result_types_tuple_mutable; - - static_assert(map_list_type::COUNT == sizeof...(ARGS), "type mapping incorrect"); - - map_tuple_type remapArgs = mapTupleArguments(thisObj, args, std::make_index_sequence{}); - - return self->constructWithTuple(remapArgs, n); - } - }; - template <> - struct ConstructFn { - template - static auto invoke(Self *self, se::Object * /*thisObj*/, std::tuple &args, std::index_sequence n) { - return self->constructWithTupleValue(args, n); - } - }; - bool construct(se::State &state) override { using type_mapping = TypeMapping>; using args_holder_type = typename MapTypeListToTuple::tuple; + constexpr auto indexes = std::make_index_sequence{}; se::PrivateObjectBase *self{nullptr}; se::Object *thisObj = state.thisObject(); args_holder_type args{}; const auto &jsArgs = state.args(); convert_js_args_to_tuple(jsArgs, args, thisObj, std::make_index_sequence()); - self = ConstructFn::invoke(this, thisObj, args, std::make_index_sequence{}); + if constexpr (type_mapping::NEED_REMAP) { + using type_mapping = TypeMapping>; + using map_list_type = typename type_mapping::mapping_list; + using map_tuple_type = typename type_mapping::result_types_tuple_mutable; + static_assert(map_list_type::COUNT == sizeof...(ARGS), "type mapping incorrect"); + + map_tuple_type remapArgs = mapTupleArguments(thisObj, args, std::make_index_sequence{}); + self = constructWithTuple(remapArgs, indexes); + } else { + self = constructWithTupleValue(args, indexes); + } state.thisObject()->setPrivateObject(self); return true; } @@ -557,23 +543,6 @@ struct InstanceMethod : InstanceMethodBase { type func{nullptr}; - template - struct Invoker; - template <> - struct Invoker { - template - static void invoke(S *method, T *self, se::State & /*state*/, std::tuple &args, std::index_sequence idx) { - method->callWithTuple(self, args, idx); - } - }; - template <> - struct Invoker { - template - static void invoke(S *method, T *self, se::State &state, std::tuple &args, std::index_sequence idx) { - nativevalue_to_se(method->callWithTuple(self, args, idx), state.rval(), state.thisObject()); - } - }; - template R callWithTuple(T *self, std::tuple &args, std::index_sequence /*unused*/) const { return ((reinterpret_cast(self))->*func)(std::get(args).value()...); @@ -590,7 +559,11 @@ struct InstanceMethod : InstanceMethodBase { } std::tuple::value>...> args{}; convert_js_args_to_tuple(jsArgs, args, thisObject, indexes); - Invoker::invoke(this, self, state, args, indexes); + if constexpr (RETURN_VOID) { + callWithTuple(self, args, indexes); + } else { + nativevalue_to_se(callWithTuple(self, args, indexes), state.rval(), state.thisObject()); + } return true; } }; @@ -605,23 +578,6 @@ struct InstanceMethod : InstanceMethodBase { type func{nullptr}; - template - struct Invoker; - template <> - struct Invoker { - template - static void invoke(S *method, T *self, se::State & /*state*/, std::tuple &args, std::index_sequence idx) { - method->callWithTuple(self, args, idx); - } - }; - template <> - struct Invoker { - template - static void invoke(S *method, T *self, se::State &state, std::tuple &args, std::index_sequence idx) { - nativevalue_to_se(method->callWithTuple(self, args, idx), state.rval(), state.thisObject()); - } - }; - template R callWithTuple(T *self, std::tuple &args, std::index_sequence /*unused*/) const { return ((reinterpret_cast(self))->*func)(std::get(args).value()...); @@ -638,7 +594,11 @@ struct InstanceMethod : InstanceMethodBase { } std::tuple::value>...> args{}; convert_js_args_to_tuple(jsArgs, args, thisObject, indexes); - Invoker::invoke(this, self, state, args, indexes); + if constexpr (RETURN_VOID) { + callWithTuple(self, args, indexes); + } else { + nativevalue_to_se(callWithTuple(self, args, indexes), state.rval(), state.thisObject()); + } return true; } }; @@ -653,23 +613,6 @@ struct InstanceMethod : InstanceMethodBase { type func{nullptr}; - template - struct Invoker; - template <> - struct Invoker { - template - static void invoke(S *method, T *self, se::State & /*state*/, std::tuple &args, std::index_sequence idx) { - method->callWithTuple(self, args, idx); - } - }; - template <> - struct Invoker { - template - static void invoke(S *method, T *self, se::State &state, std::tuple &args, std::index_sequence idx) { - nativevalue_to_se(method->callWithTuple(self, args, idx), state.rval(), state.thisObject()); - } - }; - template R callWithTuple(T *self, std::tuple &args, std::index_sequence /*unused*/) const { return (*func)(reinterpret_cast(self), std::get(args).value()...); @@ -686,7 +629,11 @@ struct InstanceMethod : InstanceMethodBase { } std::tuple::value>...> args{}; convert_js_args_to_tuple(jsArgs, args, thisObject, indexes); - Invoker::invoke(this, self, state, args, indexes); + if constexpr (RETURN_VOID) { + callWithTuple(self, args, indexes); + } else { + nativevalue_to_se(callWithTuple(self, args, indexes), state.rval(), state.thisObject()); + } return true; } }; @@ -815,29 +762,15 @@ struct InstanceAttribute> : InstanceAttribu setter_type setterPtr; getter_type getterPtr; - template - struct Invoker; - - template <> - struct Invoker { - static bool invoke(getter_type getterPtr, se::State &state) { + bool get(se::State &state) const override { + if constexpr (HAS_GETTER) { T *self = reinterpret_cast(state.nativeThisObject()); se::Object *thisObject = state.thisObject(); using func_type = FunctionWrapper; static_assert(!std::is_void::value, "should return a value"); return nativevalue_to_se(func_type::invoke(getterPtr, self), state.rval(), thisObject); } - }; - - template <> - struct Invoker { - static bool invoke(getter_type /*getterPtr*/, se::State & /*state*/) { - return false; - } - }; - - bool get(se::State &state) const override { - return Invoker::invoke(getterPtr, state); + return false; } bool set(se::State &state) const override { @@ -865,23 +798,6 @@ struct StaticMethod : StaticMethodBase { type func{nullptr}; - template - struct Invoker; - template <> - struct Invoker { - template - static void invoke(T *self, se::State & /*state*/, std::tuple &args, std::index_sequence idxs) { - self->callWithTuple(args, idxs); - } - }; - template <> - struct Invoker { - template - static void invoke(T *self, se::State &state, std::tuple &args, std::index_sequence idxs) { - nativevalue_to_se(self->callWithTuple(args, idxs), state.rval(), nullptr); - } - }; - template R callWithTuple(std::tuple &args, std::index_sequence /*unused*/) const { return (*func)(std::get(args).value()...); @@ -896,7 +812,11 @@ struct StaticMethod : StaticMethodBase { } std::tuple::value>...> args{}; convert_js_args_to_tuple(jsArgs, args, nullptr, indexes); - Invoker::invoke(this, state, args, indexes); + if constexpr (RETURN_VOID) { + callWithTuple(args, indexes); + } else { + nativevalue_to_se(callWithTuple(args, indexes), state.rval(), nullptr); + } return true; } }; @@ -975,27 +895,13 @@ struct StaticAttribute> : StaticAttributeB setter_type setterPtr; getter_type getterPtr; - template - struct Invoker; - - template <> - struct Invoker { - static bool invoke(getter_type getterPtr, se::State &state) { + bool get(se::State &state) const override { + if constexpr (HAS_GETTER) { using func_type = StaticFunctionWrapper; static_assert(!std::is_void::value, "should return a value"); return nativevalue_to_se(func_type::invoke(getterPtr), state.rval(), nullptr); } - }; - - template <> - struct Invoker { - static bool invoke(getter_type /*getterPtr*/, se::State & /*state*/) { - return false; - } - }; - - bool get(se::State &state) const override { - return Invoker::invoke(getterPtr, state); + return false; } bool set(se::State &state) const override { diff --git a/native/cocos/core/assets/TextureBase.h b/native/cocos/core/assets/TextureBase.h index b0592f00ef..ffde4bb1a4 100644 --- a/native/cocos/core/assets/TextureBase.h +++ b/native/cocos/core/assets/TextureBase.h @@ -29,6 +29,7 @@ #include "core/assets/Asset.h" #include "core/assets/AssetEnum.h" #include "renderer/gfx-base/GFXDef.h" +#include "base/std/container/unordered_map.h" #include "base/std/any.h" @@ -55,19 +56,19 @@ public: * @en The pixel format enum. * @zh 像素格式枚举类型 */ - using PixelFormat = PixelFormat; + using PixelFormat = cc::PixelFormat; /** * @en The wrap mode enum. * @zh 环绕模式枚举类型 */ - using WrapMode = WrapMode; + using WrapMode = cc::WrapMode; /** * @en The texture filter mode enum * @zh 纹理过滤模式枚举类型 */ - using Filter = Filter; + using Filter = cc::Filter; TextureBase(); // NOTE: Editor needs to invoke 'new TextureBase' in JS, so we need to make the constructor public. ~TextureBase() override; diff --git a/native/cocos/plugins/bus/EventBus.h b/native/cocos/plugins/bus/EventBus.h index d505d9fa5d..10f8b923c1 100644 --- a/native/cocos/plugins/bus/EventBus.h +++ b/native/cocos/plugins/bus/EventBus.h @@ -51,18 +51,19 @@ struct EventParam : std::conditional::value, std::true_type, std using type = typename std::conditional::type; }; +template +struct Char; +template <> +struct Char { + static constexpr char VALUE{'E'}; +}; +template <> +struct Char { + static constexpr char VALUE{'?'}; +}; + template struct Signature { - template - struct Char; - template <> - struct Char { - static constexpr char VALUE{'E'}; - }; - template <> - struct Char { - static constexpr char VALUE{'?'}; - }; constexpr static char name() { return Char::value>::VALUE; } }; diff --git a/templates/linux/CMakeLists.txt b/templates/linux/CMakeLists.txt index 6c7e76bce5..b6dd590a5e 100644 --- a/templates/linux/CMakeLists.txt +++ b/templates/linux/CMakeLists.txt @@ -1,8 +1,8 @@ cmake_minimum_required(VERSION 3.8) set(APP_NAME "game" CACHE STRING "Project Name") -set(CMAKE_C_COMPILER "/usr/bin/clang") -set(CMAKE_CXX_COMPILER "/usr/bin/clang++") +#set(CMAKE_C_COMPILER "/usr/bin/clang") +#set(CMAKE_CXX_COMPILER "/usr/bin/clang++") project(${APP_NAME} CXX) @@ -16,4 +16,4 @@ set(EXECUTABLE_NAME ${APP_NAME}) cc_linux_before_target(${EXECUTABLE_NAME}) add_executable(${EXECUTABLE_NAME} ${CC_ALL_SOURCES}) -cc_linux_after_target(${EXECUTABLE_NAME}) \ No newline at end of file +cc_linux_after_target(${EXECUTABLE_NAME})