From 7be64d0df72f1c5e7f73362b4f4dceed11a3f0ad Mon Sep 17 00:00:00 2001 From: VisualGMQ <2142587070@qq.com> Date: Mon, 29 Jan 2024 20:48:21 +0800 Subject: [PATCH] fix compile bug under clang(backend MSVC) --- CMakeLists.txt | 19 +++++++++++++++---- cutils.c | 1 + quickjs.c | 6 ++---- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 103e9a3..a2c260f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,17 @@ endif() message(STATUS "Building in ${CMAKE_BUILD_TYPE} mode") message(STATUS "Building with ${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION} on ${CMAKE_SYSTEM}") +function(IsMSVC var) + if (MSVC OR (CMAKE_C_COMPILER_ID STREQUAL "Clang" AND CMAKE_C_SIMULATE_ID STREQUAL "MSVC")) + set(${var} 1 PARENT_SCOPE) + else() + set(${var} 0 PARENT_SCOPE) + endif() +endfunction() + +set(is_msvc 0) +IsMSVC(is_msvc) + macro(xcheck_add_c_compiler_flag FLAG) string(REPLACE "-" "" FLAG_NO_HYPHEN ${FLAG}) check_c_compiler_flag(${FLAG} COMPILER_SUPPORTS_${FLAG_NO_HYPHEN}) @@ -30,7 +41,7 @@ macro(xcheck_add_c_compiler_flag FLAG) endmacro() xcheck_add_c_compiler_flag(-Wall) -if(NOT MSVC) +if(NOT is_msvc) xcheck_add_c_compiler_flag(-Werror) xcheck_add_c_compiler_flag(-Wextra) endif() @@ -45,7 +56,7 @@ xcheck_add_c_compiler_flag(-Wno-format-truncation) xcheck_add_c_compiler_flag(-funsigned-char) # ClangCL is command line compatible with MSVC, so 'MSVC' is set. -if(MSVC) +if(${is_msvc}) xcheck_add_c_compiler_flag(-Wno-unsafe-buffer-usage) xcheck_add_c_compiler_flag(-Wno-sign-conversion) xcheck_add_c_compiler_flag(-Wno-nonportable-system-include-path) @@ -54,7 +65,7 @@ if(MSVC) xcheck_add_c_compiler_flag(-Wno-reserved-macro-identifier) xcheck_add_c_compiler_flag(-Wno-reserved-identifier) xcheck_add_c_compiler_flag(-Wdeprecated-declarations) - add_compile_definitions(WIN32_LEAN_AND_MEAN) + add_compile_definitions(WIN32_LEAN_AND_MEAN _CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATE) endif() if(CMAKE_BUILD_TYPE MATCHES "Debug") @@ -146,7 +157,7 @@ if(BUILD_QJS_LIBC) endif() list(APPEND qjs_defines _GNU_SOURCE) list(APPEND qjs_libs qjs ${CMAKE_DL_LIBS}) -if(NOT MSVC) +if(NOT is_msvc) list(APPEND qjs_libs m pthread) endif() diff --git a/cutils.c b/cutils.c index 088e75e..6c9f1c9 100644 --- a/cutils.c +++ b/cutils.c @@ -612,6 +612,7 @@ void rqsort(void *base, size_t nmemb, size_t size, cmp_f cmp, void *opaque) } #if defined(_MSC_VER) +struct timezone; // From: https://stackoverflow.com/a/26085827 static int gettimeofday_msvc(struct timeval *tp, struct timezone *tzp) { diff --git a/quickjs.c b/quickjs.c index 2ea44cc..1d29ce1 100644 --- a/quickjs.c +++ b/quickjs.c @@ -211,8 +211,6 @@ typedef enum { JS_GC_PHASE_REMOVE_CYCLES, } JSGCPhaseEnum; -typedef enum OPCodeEnum OPCodeEnum; - struct JSRuntime { JSMallocFunctions mf; JSMallocState malloc_state; @@ -977,7 +975,7 @@ typedef enum OPCodeFormat { #undef FMT } OPCodeFormat; -enum OPCodeEnum { +typedef enum OPCodeEnum { #define FMT(f) #define DEF(id, size, n_pop, n_push, f) OP_ ## id, #define def(id, size, n_pop, n_push, f) @@ -997,7 +995,7 @@ enum OPCodeEnum { #undef DEF #undef FMT OP_TEMP_END, -}; +} OPCodeEnum; static int JS_InitAtoms(JSRuntime *rt); static JSAtom __JS_NewAtomInit(JSRuntime *rt, const char *str, int len,