[CMAKE:MSVC]
[reactos.git] / reactos / cmake / msvc.cmake
index 952403e..53fe600 100644 (file)
@@ -20,16 +20,24 @@ endif()
 
 add_definitions(/Dinline=__inline /D__STDC__=1)
 
-add_compile_flags("/X /GR- /GS- /Zl /W3")
+add_compile_flags("/X /GR- /EHs-c- /GS- /Zl /W3")
 
 # HACK: for VS 11+ we need to explicitly disable SSE, which is off by
-# default for older compilers. See bug #7174
+# default for older compilers. See CORE-6507
 if (MSVC_VERSION GREATER 1699 AND ARCH STREQUAL "i386")
     add_compile_flags("/arch:IA32")
 endif ()
 
-# C4700 is almost always likely to result in broken code, so mark it as an error
-add_compile_flags("/we4700")
+# C++ exception specification ignored... yeah we don't care
+add_compile_flags("/wd4290")
+
+# The following warnings are treated as errors:
+# - C4047: different level of indirection
+# - C4090: different 'modifier' qualifiers (for C programs only;
+#          for C++ programs, the compiler error C2440 is issued)
+# - C4098: void function returning a value
+# - C4700: uninitialized variable usage
+add_compile_flags("/we4047 /we4090 /we4098 /we4700")
 
 # Debugging
 #if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
@@ -43,23 +51,24 @@ elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
     add_compile_flags("/Ob2 /D NDEBUG")
 endif()
 
-if(${_MACHINE_ARCH_FLAG} MATCHES X86)
-    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO /NODEFAULTLIB /RELEASE")
-    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SAFESEH:NO /NODEFAULTLIB /RELEASE")
-    set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /SAFESEH:NO /NODEFAULTLIB /RELEASE")
+if(MSVC_IDE)
+    add_compile_flags("/MP")
+    if(NOT DEFINED USE_FOLDER_STRUCTURE)
+        set(USE_FOLDER_STRUCTURE FALSE)
+    endif()
 endif()
 
-set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> <FLAGS> <DEFINES> ${I18N_DEFS} /fo<OBJECT> <SOURCE>")
+if(${_MACHINE_ARCH_FLAG} MATCHES X86)
+    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE")
+    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE")
+    set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE")
+endif()
 
-if(MSVC_IDE)
-    # Asm source files are not supported in VS generators yet. As a result, <DEFINES> isn't recognized.
-    # We may temporarily use just the global defines, but this is not a solution as some modules (minihal for example) apply additional definitions to source files, so we get an incorrect build of such targets.
-    get_directory_property(definitions DEFINITIONS)
-    set(CMAKE_ASM_COMPILE_OBJECT
-        "cl /nologo /X /I${REACTOS_SOURCE_DIR}/include/asm /I${REACTOS_BINARY_DIR}/include/asm <FLAGS> ${definitions} /D__ASM__ /D_USE_ML /EP /c <SOURCE> > <OBJECT>.tmp"
-        "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <OBJECT>.tmp")
+if(MSVC_IDE AND (CMAKE_VERSION MATCHES "ReactOS"))
+    # for VS builds we'll only have en-US in resource files
+    add_definitions(/DLANGUAGE_EN_US)
 else()
-    # NMake Makefiles
+    set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> /nologo <FLAGS> <DEFINES> ${I18N_DEFS} /fo<OBJECT> <SOURCE>")
     set(CMAKE_ASM_COMPILE_OBJECT
         "cl /nologo /X /I${REACTOS_SOURCE_DIR}/include/asm /I${REACTOS_BINARY_DIR}/include/asm <FLAGS> <DEFINES> /D__ASM__ /D_USE_ML /EP /c <SOURCE> > <OBJECT>.tmp"
         "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <OBJECT>.tmp")
@@ -102,7 +111,11 @@ function(set_entrypoint _module _entrypoint)
 endfunction()
 
 function(set_subsystem MODULE SUBSYSTEM)
-    add_target_link_flags(${MODULE} "/subsystem:${SUBSYSTEM}")
+    if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 17)
+        add_target_link_flags(${MODULE} "/SUBSYSTEM:${SUBSYSTEM},5.01")
+    else()
+        add_target_link_flags(${MODULE} "/SUBSYSTEM:${SUBSYSTEM}")
+    endif()
 endfunction()
 
 function(set_image_base MODULE IMAGE_BASE)
@@ -110,23 +123,37 @@ function(set_image_base MODULE IMAGE_BASE)
 endfunction()
 
 function(set_module_type_toolchain MODULE TYPE)
+    if(CPP_USE_STL)
+        if((${TYPE} STREQUAL "kernelmodedriver") OR (${TYPE} STREQUAL "wdmdriver"))
+            message(FATAL_ERROR "Use of STL in kernelmodedriver or wdmdriver type module prohibited")
+        endif()
+        target_link_libraries(${MODULE} cpprt stlport oldnames)
+    elseif(CPP_USE_RT)
+        target_link_libraries(${MODULE} cpprt)
+    endif()
     if((${TYPE} STREQUAL "win32dll") OR (${TYPE} STREQUAL "win32ocx") OR (${TYPE} STREQUAL "cpl"))
         add_target_link_flags(${MODULE} "/DLL")
     elseif(${TYPE} STREQUAL "kernelmodedriver")
         add_target_link_flags(${MODULE} "/DRIVER")
+    elseif(${TYPE} STREQUAL "wdmdriver")
+        add_target_link_flags(${MODULE} "/DRIVER:WDM")
     endif()
 endfunction()
 
 #define those for having real libraries
 set(CMAKE_IMPLIB_CREATE_STATIC_LIBRARY "LINK /LIB /NOLOGO <LINK_FLAGS> /OUT:<TARGET> <OBJECTS>")
-set(CMAKE_STUB_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> /Cp /Fo<OBJECT> /c /Ta <SOURCE>")
-macro(add_delay_importlibs MODULE)
-    foreach(LIB ${ARGN})
-        add_target_link_flags(${MODULE} "/DELAYLOAD:${LIB}.dll")
-        target_link_libraries(${MODULE} lib${LIB})
+set(CMAKE_STUB_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <SOURCE>")
+function(add_delay_importlibs _module)
+    get_target_property(_module_type ${_module} TYPE)
+    if(_module_type STREQUAL "STATIC_LIBRARY")
+        message(FATAL_ERROR "Cannot add delay imports to a static library")
+    endif()
+    foreach(_lib ${ARGN})
+        add_target_link_flags(${_module} "/DELAYLOAD:${_lib}.dll")
+        target_link_libraries(${_module} lib${_lib})
     endforeach()
-    target_link_libraries(${MODULE} delayimp)
-endmacro()
+    target_link_libraries(${_module} delayimp)
+endfunction()
 
 function(generate_import_lib _libname _dllname _spec_file)
 
@@ -173,7 +200,7 @@ else()
     set(SPEC2DEF_ARCH i386)
 endif()
 function(spec2def _dllname _spec_file)
-    # do we also want to add impotlib targets?
+    # do we also want to add importlib targets?
     if(${ARGC} GREATER 2)
         if(${ARGN} STREQUAL "ADD_IMPORTLIB")
             set(__add_importlib TRUE)
@@ -243,3 +270,32 @@ endfunction()
 
 function(allow_warnings __module)
 endfunction()
+
+macro(add_asm_files _target)
+    if(MSVC_IDE AND (CMAKE_VERSION MATCHES "ReactOS"))
+        get_defines(_directory_defines)
+        get_includes(_directory_includes)
+        get_directory_property(_defines COMPILE_DEFINITIONS)
+        foreach(_source_file ${ARGN})
+            get_filename_component(_source_file_base_name ${_source_file} NAME_WE)
+            set(_preprocessed_asm_file ${CMAKE_CURRENT_BINARY_DIR}/asm/${_source_file_base_name}_${_target}.tmp)
+            set(_object_file ${CMAKE_CURRENT_BINARY_DIR}/asm/${_source_file_base_name}_${_target}.obj)
+            set(_source_file_full_path ${CMAKE_CURRENT_SOURCE_DIR}/${_source_file})
+            get_source_file_property(_defines_semicolon_list ${_source_file_full_path} COMPILE_DEFINITIONS)
+            unset(_source_file_defines)
+            foreach(_define ${_defines_semicolon_list})
+                if(NOT ${_define} STREQUAL "NOTFOUND")
+                    list(APPEND _source_file_defines -D${_define})
+                endif()
+            endforeach()
+            add_custom_command(
+                OUTPUT ${_preprocessed_asm_file} ${_object_file}
+                COMMAND cl /nologo /X /I${REACTOS_SOURCE_DIR}/include/asm /I${REACTOS_BINARY_DIR}/include/asm ${_directory_includes} ${_source_file_defines} ${_directory_defines} /D__ASM__ /D_USE_ML /EP /c ${_source_file_full_path} > ${_preprocessed_asm_file} && ${CMAKE_ASM_COMPILER} /nologo /Cp /Fo${_object_file} /c /Ta ${_preprocessed_asm_file}
+                DEPENDS ${_source_file_full_path})
+            set_source_files_properties(${_object_file} PROPERTIES EXTERNAL_OBJECT 1)
+            list(APPEND ${_target} ${_object_file})
+        endforeach()
+    else()
+        list(APPEND ${_target} ${ARGN})
+    endif()
+endmacro()