@@ -56,33 +56,52 @@ if(CMAKE_SYSTEM_NAME MATCHES "Linux")
5656 endif ()
5757endif ()
5858
59+ set (FLB_EXTRA_CMAKE_CXX_FLAGS)
60+ set (FLB_EXTRA_CMAKE_CXX_LINK_FLAGS)
61+
62+ # For consistency with MSVC debug C/C++ runtime (/MTd and /MDd options),
63+ # which is used for the Debug build and which automatically adds _DEBUG.
64+ add_compile_definitions ($<$<CONFIG :Debug >:_DEBUG >)
65+
5966# Update CFLAGS
6067if (MSVC )
68+ # Use static C runtime
69+ if (NOT (CMAKE_VERSION VERSION_LESS "3.15" ))
70+ set (CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG :Debug >:Debug >" )
71+ endif ()
72+ add_compile_options (/MT$<$<CONFIG:Debug>:d> )
73+ # Replace (/|-)MD(d?) with (/|-)MT(d?) to avoid D9025 warning
74+ foreach (config_type ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE} Debug Release RelWithDebInfo MinSizeRel)
75+ string (TOUPPER ${config_type} upper_config_type)
76+ set (flag_var "CMAKE_C_FLAGS_${upper_config_type} " )
77+ string (REGEX REPLACE "(^| |\\ t|\\ r|\\ n)(-|/)(MD)(d?)($| |\\ t|\\ r|\\ n)" "\\ 1\\ 2MT\\ 4\\ 5" ${flag_var} "${${flag_var} }" )
78+ endforeach ()
79+ set (flag_var)
80+ set (upper_config_type)
81+ set (config_type)
82+
83+ # Make compiler aware of source code using UTF-8 encoding
84+ add_compile_options (/utf-8 )
85+
6186 add_definitions (-D_CRT_SECURE_NO_WARNINGS )
6287 add_definitions (-D_CRT_NONSTDC_NO_WARNINGS )
6388
6489 # Use custom CFLAGS for MSVC
6590 #
6691 # /Zi ...... Generate pdb files.
67- # /MT ...... Static link C runtimes.
6892 # /wd4711 .. C4711 (function selected for inline expansion)
6993 # /wd4100 .. C4100 (unreferenced formal parameter)
7094 # /wd5045 .. C5045 (Spectre mitigation)
7195 #
7296 # Restore /OPT:REF after setting /Debug, enable /OPT:ICF for 64-bit
7397 # builds per Microsoft recommended best practices.
74- set (CMAKE_C_FLAGS "/DWIN32 /D_WINDOWS /DNDEBUG /O2 / Zi /wd4100 /wd4711 /wd5045" )
98+ set (CMAKE_C_FLAGS "/DWIN32 /D_WINDOWS /Zi /wd4100 /wd4711 /wd5045" )
7599 set (CMAKE_EXE_LINKER_FLAGS "/DEBUG /OPT:REF /INCREMENTAL:NO" )
76100 set (CMAKE_SHARED_LINKER_FLAGS "/DEBUG /OPT:REF /INCREMENTAL:NO" )
77- set (CMAKE_BUILD_TYPE None)
78101 if (CMAKE_SIZEOF_VOID_P EQUAL 8)
79102 set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /OPT:ICF" )
80103 set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /OPT:ICF" )
81104 endif ()
82-
83- # Use add_compile_options() to set /MT since Visual Studio
84- # Generator does not notice /MT in CMAKE_C_FLAGS.
85- add_compile_options (/MT )
86105else ()
87106 set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall" )
88107 if (CMAKE_SYSTEM_NAME STREQUAL "Linux" )
@@ -104,17 +123,23 @@ else()
104123 endif ()
105124endif ()
106125
107- set ( CMAKE_C_FLAGS " ${CMAKE_C_FLAGS} -D__FLB_FILENAME__ =__FILE__" )
126+ add_compile_definitions ( __FLB_FILENAME__ =__FILE__ )
108127
109128if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv7l" )
110129 set (CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -latomic" )
111- set (CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -latomic" )
130+ # CMAKE_CXX_LINK_FLAGS will be extended with FLB_EXTRA_CMAKE_CXX_LINK_FLAGS,
131+ # when / if C++ language support will be enabled.
132+ # Until C++ language support is enabled, CMAKE_CXX_LINK_FLAGS is empty / undefined.
133+ set (FLB_EXTRA_CMAKE_CXX_LINK_FLAGS "${FLB_EXTRA_CMAKE_CXX_LINK_FLAGS} -latomic" )
112134endif ()
113135if (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" )
114136 set (FLB_SYSTEM_FREEBSD On )
115137 add_definitions (-DFLB_SYSTEM_FREEBSD )
116138 set (CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -lutil" )
117- set (CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -lutil" )
139+ # CMAKE_CXX_LINK_FLAGS will be extended with FLB_EXTRA_CMAKE_CXX_LINK_FLAGS,
140+ # when / if C++ language support will be enabled.
141+ # Until C++ language support is enabled, CMAKE_CXX_LINK_FLAGS is empty / undefined.
142+ set (FLB_EXTRA_CMAKE_CXX_LINK_FLAGS "${FLB_EXTRA_CMAKE_CXX_LINK_FLAGS} -lutil" )
118143endif ()
119144
120145# *BSD is not supported platform for wasm-micro-runtime except for FreeBSD.
@@ -525,9 +550,12 @@ if(FLB_UNICODE_ENCODER)
525550endif ()
526551
527552if (FLB_COVERAGE)
528- set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 --coverage -fprofile-arcs -ftest-coverage" )
553+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 --coverage -fprofile-arcs -ftest-coverage -fprofile-update=atomic " )
529554 if (FLB_UNICODE_ENCODER)
530- set (CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -g -O0 --coverage -fprofile-arcs -ftest-coverage" )
555+ # CMAKE_CXX_FLAGS will be extended with FLB_EXTRA_CMAKE_CXX_FLAGS,
556+ # when / if C++ language support will be enabled.
557+ # Until C++ language support is enabled, CMAKE_CXX_FLAGS is empty / undefined.
558+ set (FLB_EXTRA_CMAKE_CXX_FLAGS "${FLB_EXTRA_CMAKE_CXX_FLAGS} -g -O0 --coverage -fprofile-arcs -ftest-coverage -fprofile-update=atomic" )
531559 endif ()
532560 set (CMAKE_BUILD_TYPE "Debug" )
533561endif ()
@@ -544,9 +572,7 @@ if(FLB_COMPILER_STRICT_POINTER_TYPES)
544572endif ()
545573
546574# Enable Debug symbols if specified
547- if (MSVC )
548- set (CMAKE_BUILD_TYPE None) # Avoid flag conflicts (See CMakeList.txt:L18)
549- elseif (FLB_RELEASE)
575+ if (FLB_RELEASE)
550576 set (CMAKE_BUILD_TYPE "RelWithDebInfo" )
551577elseif (FLB_DEBUG)
552578 set (CMAKE_BUILD_TYPE "Debug" )
@@ -665,6 +691,12 @@ endif()
665691
666692# MPack
667693add_definitions (-DMPACK_EXTENSIONS=1 )
694+ # MPack read / write tracking seems to have issues with nested types,
695+ # so it needs to be disabled explicitly, because otherwise MPack
696+ # read / write tracking is automatically enabled for the Debug build
697+ # (when _DEBUG macro is defined) with some compilers, like MSVC
698+ # (refer to https://learn.microsoft.com/en-us/cpp/c-runtime-library/debug).
699+ add_definitions (-DMPACK_READ_TRACKING=0 -DMPACK_WRITE_TRACKING=0 )
668700add_subdirectory (${FLB_PATH_LIB_MPACK} EXCLUDE_FROM_ALL )
669701
670702# Miniz (zip)
@@ -752,13 +784,30 @@ if(FLB_UTF8_ENCODER)
752784 add_subdirectory (${FLB_PATH_LIB_TUTF8E} EXCLUDE_FROM_ALL )
753785endif ()
754786
787+ # All cases when C++ language support is needed should be checked here
788+ if ((FLB_UNICODE_ENCODER AND FLB_USE_SIMDUTF) OR FLB_RIPSER)
789+ enable_language (CXX )
790+
791+ # Perform delayed modification / extension of CMAKE_CXX_* variables.
792+ if (MSVC )
793+ # Replace (/|-)MD(d?) with (/|-)MT(d?) to avoid D9025 warning
794+ foreach (config_type ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE} Debug Release RelWithDebInfo MinSizeRel)
795+ string (TOUPPER ${config_type} upper_config_type)
796+ set (flag_var "CMAKE_CXX_FLAGS_${upper_config_type} " )
797+ string (REGEX REPLACE "(^| |\\ t|\\ r|\\ n)(-|/)(MD)(d?)($| |\\ t|\\ r|\\ n)" "\\ 1\\ 2MT\\ 4\\ 5" ${flag_var} "${${flag_var} }" )
798+ endforeach ()
799+ set (flag_var)
800+ set (upper_config_type)
801+ set (config_type)
802+ endif ()
803+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}${FLB_EXTRA_CMAKE_CXX_FLAGS} " )
804+ set (CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS}${FLB_EXTRA_CMAKE_CXX_LINK_FLAGS} " )
805+
806+ set (CMAKE_CXX_STANDARD 11)
807+ endif ()
808+
755809# simdutf
756810if (FLB_UNICODE_ENCODER AND FLB_USE_SIMDUTF)
757- if (NOT FLB_USE_SIMDUTF)
758- message (FATAL_ERROR "FLB_UNICODE_ENCODER requires FLB_USE_SIMDUTF" )
759- endif ()
760- enable_language (CXX )
761- set (CMAKE_CXX_STANDARD 11)
762811 add_subdirectory (${FLB_PATH_LIB_SIMDUTF} EXCLUDE_FROM_ALL )
763812 FLB_DEFINITION (FLB_HAVE_UNICODE_ENCODER )
764813endif ()
@@ -818,7 +867,9 @@ macro(MK_SET_OPTION option value)
818867 set (${option} ${value} CACHE INTERNAL "" FORCE )
819868endmacro ()
820869MK_SET_OPTION (MK_SYSTEM_MALLOC ON )
821- MK_SET_OPTION (MK_DEBUG ON )
870+ if (NOT MSVC )
871+ MK_SET_OPTION (MK_DEBUG ON )
872+ endif ()
822873
823874# Monkey backend event loop
824875if (FLB_EVENT_LOOP_EPOLL)
@@ -915,8 +966,6 @@ endif()
915966
916967# ripser
917968if (FLB_RIPSER)
918- enable_language (CXX )
919- set (CMAKE_CXX_STANDARD 11)
920969 add_subdirectory (${FLB_PATH_LIB_RIPSER} EXCLUDE_FROM_ALL )
921970 FLB_DEFINITION (FLB_HAVE_RIPSER )
922971endif ()
0 commit comments