[CLANG-CL] Initial commit that allows us to compile ReactOS with clang-cl.
authorAmine Khaldi <amine.khaldi@reactos.org>
Fri, 27 Oct 2017 21:18:01 +0000 (22:18 +0100)
committerAmine Khaldi <amine.khaldi@reactos.org>
Thu, 23 Nov 2017 13:09:57 +0000 (14:09 +0100)
18 files changed:
CMakeLists.txt
base/setup/usetup/CMakeLists.txt
boot/freeldr/freeldr/CMakeLists.txt
configure.cmd
dll/win32/CMakeLists.txt
dll/win32/browseui/CMakeLists.txt
dll/win32/browseui/shellbars/CMakeLists.txt
dll/win32/riched20/CMakeLists.txt
dll/win32/winmm/CMakeLists.txt
drivers/filesystems/ext2/CMakeLists.txt
ntoskrnl/ntos.cmake
sdk/cmake/Platform/Windows-MSVC.cmake
sdk/cmake/config.cmake
sdk/cmake/host-tools.cmake
sdk/cmake/msvc.cmake
sdk/lib/crt/crt.cmake
subsystems/mvdm/asm16.cmake
toolchain-msvc.cmake

index f4c38c8..bcf4c18 100644 (file)
@@ -97,7 +97,10 @@ if(NOT CMAKE_CROSSCOMPILING)
         if(MSVC_VERSION GREATER 1699)
             add_definitions(/D_ALLOW_KEYWORD_MACROS)
         endif()
-        add_definitions(/Dinline=__inline)
+        if(NOT USE_CLANG_CL)
+            # FIXME: Inspect
+            add_definitions(/Dinline=__inline)
+        endif()
     endif()
 
     include_directories(sdk/include/host)
@@ -182,7 +185,10 @@ else()
 
     # Arch Options
     if(ARCH STREQUAL "i386")
-        add_definitions(-D_M_IX86 -D_X86_ -D__i386__ -Di386)
+        if(NOT USE_CLANG_CL)
+            add_definitions(-D_M_IX86)
+        endif()
+        add_definitions(-D_X86_ -D__i386__ -Di386)
     elseif(ARCH STREQUAL "amd64")
         add_definitions(-D_M_AMD64 -D_AMD64_ -D__x86_64__ -D_WIN64)
     elseif(ARCH STREQUAL "arm")
index d78f9d7..d90103f 100644 (file)
@@ -34,11 +34,6 @@ list(APPEND SOURCE
     settings.c
     usetup.h)
 
-if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
-    #FIXME: Investigate Clang "illegal character encoding in string literal" warning
-    set_property(SOURCE mui.c APPEND_STRING PROPERTY COMPILE_FLAGS " -Wno-invalid-source-encoding")
-endif()
-
 add_executable(usetup ${SOURCE} usetup.rc)
 target_link_libraries(usetup zlib inflib ext2lib vfatlib)
 set_module_type(usetup nativecui)
index c4ca5b4..df7cc0a 100644 (file)
@@ -198,6 +198,11 @@ add_library(freeldr_common ${FREELDR_BOOTLIB_COMMON_SOURCE} ${FREELDR_ARC_COMMON
 add_pch(freeldr_common include/freeldr.h FREELDR_COMMON_SOURCE)
 add_dependencies(freeldr_common bugcodes asm xdk)
 
+if(USE_CLANG_CL)
+    # We need to reduce the binary size
+    set_property(SOURCE ${FREELDR_BOOTLIB_COMMON_SOURCE} ${FREELDR_ARC_COMMON_SOURCE} APPEND_STRING PROPERTY COMPILE_FLAGS " /Os")
+endif()
+
 if(ARCH STREQUAL "i386" AND NOT MSVC)
     list(APPEND FREELDR_BASE_SOURCE arch/i386/multiboot.S)
     target_link_libraries(freeldr_common mini_hal)
index 6b0c5df..922a8f1 100755 (executable)
@@ -83,6 +83,7 @@ if not defined ARCH (
 )
 
 set NEW_STYLE_BUILD=1
+set USE_CLANG_CL=0
 
 REM Parse command line parameters
 :repeat
@@ -95,8 +96,6 @@ REM Parse command line parameters
             set CMAKE_GENERATOR="Eclipse CDT4 - MinGW Makefiles"
         ) else if /I "%1" == "Makefiles" (
             set CMAKE_GENERATOR="MinGW Makefiles"
-        ) else if /I "%1" == "clang" (
-            set MINGW_TOOCHAIN_FILE=toolchain-clang.cmake
         ) else (
             goto continue
         )
@@ -107,6 +106,8 @@ REM Parse command line parameters
             set CMAKE_GENERATOR="Eclipse CDT4 - NMake Makefiles"
         ) else if /I "%1" == "Makefiles" (
             set CMAKE_GENERATOR="NMake Makefiles"
+        ) else if /I "%1" == "clang" (
+            set USE_CLANG_CL=1
         ) else if /I "%1" == "VSSolution" (
             set VS_SOLUTION=1
             REM explicitly set VS version for project generator
@@ -239,6 +240,8 @@ if "%NEW_STYLE_BUILD%"=="0" (
 
 if "%BUILD_ENVIRONMENT%" == "MinGW" (
     cmake -G %CMAKE_GENERATOR% -DENABLE_CCACHE:BOOL=0 -DCMAKE_TOOLCHAIN_FILE:FILEPATH=%MINGW_TOOCHAIN_FILE% -DARCH:STRING=%ARCH% %BUILD_TOOLS_FLAG% %* "%REACTOS_SOURCE_DIR%"
+) else if %USE_CLANG_CL% == 1 (
+        cmake -G %CMAKE_GENERATOR% -DCMAKE_TOOLCHAIN_FILE:FILEPATH=toolchain-msvc.cmake -DARCH:STRING=%ARCH% %BUILD_TOOLS_FLAG% -DUSE_CLANG_CL:BOOL=1 -DRUNTIME_CHECKS:BOOL=%VS_RUNTIME_CHECKS% %* "%REACTOS_SOURCE_DIR%"
 ) else (
     cmake -G %CMAKE_GENERATOR% -DCMAKE_TOOLCHAIN_FILE:FILEPATH=toolchain-msvc.cmake -DARCH:STRING=%ARCH% %BUILD_TOOLS_FLAG% -DRUNTIME_CHECKS:BOOL=%VS_RUNTIME_CHECKS% %* "%REACTOS_SOURCE_DIR%"
 )
index c0a16ad..e4639ac 100644 (file)
@@ -42,7 +42,9 @@ add_subdirectory(faultrep)
 add_subdirectory(fltlib)
 add_subdirectory(fmifs)
 add_subdirectory(fontsub)
-add_subdirectory(framedyn)
+if(NOT USE_CLANG_CL)
+    add_subdirectory(framedyn)
+endif()
 add_subdirectory(fusion)
 add_subdirectory(gdiplus)
 add_subdirectory(getuname)
index 30ab6a2..45afa68 100644 (file)
@@ -45,10 +45,7 @@ add_pch(browseui precomp.h SOURCE)
 add_cd_file(TARGET browseui DESTINATION reactos/system32 FOR all)
 
 if(NOT MSVC)
-    if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
-        add_target_compile_flags(browseui "-Wno-unused-but-set-variable")
-    endif()
-
+    add_target_compile_flags(browseui "-Wno-unused-but-set-variable")
     # Binutils linker bug
     if(LTCG)
         add_target_link_flags(browseui "-Wl,--allow-multiple-definition")
index 28c5517..f42c74f 100644 (file)
@@ -20,7 +20,5 @@ add_dependencies(shellbars xdk)
 target_link_libraries(shellbars atlnew)
 
 if(NOT MSVC)
-    if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
-        add_target_compile_flags(shellbars "-Wno-unused-but-set-variable")
-    endif()
+    add_target_compile_flags(shellbars "-Wno-unused-but-set-variable")
 endif()
index b0cf90e..002fc5a 100644 (file)
@@ -33,11 +33,6 @@ else()
     list(APPEND SOURCE txthost.c txtsrv.c)
 endif()
 
-if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
-    #FIXME: http://llvm.org/bugs/show_bug.cgi?id=19027
-    set_property(SOURCE txthost.c txtsrv.c APPEND_STRING PROPERTY COMPILE_FLAGS " -no-integrated-as")
-endif()
-
 list(APPEND ADDITIONAL_SOURCE
     version.rc
     ${CMAKE_CURRENT_BINARY_DIR}/riched20.def)
index c82fda5..eb3cbec 100644 (file)
@@ -27,7 +27,7 @@ add_importlibs(winmm advapi32 user32 msvcrt kernel32 ntdll)
 add_pch(winmm winemm.h SOURCE)
 add_cd_file(TARGET winmm DESTINATION reactos/system32 FOR all)
 
-if((NOT MSVC) AND (NOT CMAKE_C_COMPILER_ID STREQUAL "Clang"))
+if(NOT MSVC)
     add_target_compile_flags(winmm "-Wno-unused-but-set-variable")
 endif()
 
index abc1c24..6627874 100644 (file)
@@ -88,11 +88,13 @@ list(APPEND SOURCE
 
 add_library(ext2fs SHARED ${SOURCE} ext2fs.rc)
 
+if(USE_CLANG_CL)
+    set_property(SOURCE src/create.c src/fileinfo.c src/memory.c src/read.c APPEND_STRING PROPERTY COMPILE_FLAGS " /fallback")
+endif()
+
 if(NOT MSVC)
     add_target_compile_flags(ext2fs "-Wno-pointer-sign -Wno-unused-function")
-    if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
-        add_target_compile_flags(ext2fs "-Wno-unused-but-set-variable -Wno-unused-variable -Wno-missing-braces")
-    endif()
+    add_target_compile_flags(ext2fs "-Wno-unused-but-set-variable -Wno-unused-variable -Wno-missing-braces")
 else()
     #disable warnings: "unreferenced local variable", "initialized, but not used variable", "benign include"
     replace_compile_flags("/we\"4189\"" " ")
@@ -103,5 +105,10 @@ target_link_libraries(ext2fs memcmp ${PSEH_LIB})
 add_definitions(-D__KERNEL__)
 set_module_type(ext2fs kernelmodedriver)
 add_importlibs(ext2fs ntoskrnl hal)
-add_pch(ext2fs inc/ext2fs.h SOURCE)
+
+if(NOT USE_CLANG_CL)
+    # The fallback we have above prevents cl and clang-cl from using the same PCH
+    add_pch(ext2fs inc/ext2fs.h SOURCE)
+endif()
+
 add_cd_file(TARGET ext2fs DESTINATION reactos/system32/drivers NO_CAB FOR all)
index 9affb1e..ceb2876 100644 (file)
@@ -426,12 +426,3 @@ else() # _WINKD_
     endif()
 
 endif()
-
-if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
-    #FIXME: http://llvm.org/bugs/show_bug.cgi?id=19027
-    set_property(SOURCE
-        ${REACTOS_SOURCE_DIR}/ntoskrnl/ke/i386/cpu.c
-        ${REACTOS_SOURCE_DIR}/ntoskrnl/ke/i386/kiinit.c
-        ${REACTOS_SOURCE_DIR}/ntoskrnl/ke/i386/traphdlr.c
-        APPEND_STRING PROPERTY COMPILE_FLAGS " -no-integrated-as")
-endif()
index dc09c74..5c3912c 100644 (file)
@@ -193,8 +193,13 @@ else()
 endif()
 
 if (CMAKE_COMPILER_SUPPORTS_PDBTYPE)
-  set (CMAKE_EXE_LINKER_FLAGS_DEBUG_INIT "/debug /pdbtype:sept")
-  set (CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO_INIT "/debug /pdbtype:sept ${MSVC_INCREMENTAL_YES_FLAG}")
+  if(NOT USE_CLANG_CL)
+    set (CMAKE_EXE_LINKER_FLAGS_DEBUG_INIT "/debug /pdbtype:sept")
+    set (CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO_INIT "/debug /pdbtype:sept ${MSVC_INCREMENTAL_YES_FLAG}")
+  else()
+    set(CMAKE_EXE_LINKER_FLAGS_DEBUG_INIT "/debug")
+    set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO_INIT "/debug")
+  endif()
 else ()
   set (CMAKE_EXE_LINKER_FLAGS_DEBUG_INIT "/debug")
   set (CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO_INIT "/debug ${MSVC_INCREMENTAL_YES_FLAG}")
index 1374cab..3fc9ce2 100644 (file)
@@ -36,7 +36,7 @@ else()
 "Whether to compile for debugging.")
 endif()
 
-if(MSVC)
+if(MSVC AND (NOT USE_CLANG_CL))
     set(KDBG FALSE CACHE BOOL
 "Whether to compile in the integrated kernel debugger.")
     if(CMAKE_BUILD_TYPE STREQUAL "Release")
index 40c4eb1..100df6f 100644 (file)
@@ -23,6 +23,11 @@ foreach(_host_tool ${host_tools_list})
     add_dependencies(native-${_host_tool} host-tools)
 endforeach()
 
+if(USE_CLANG_CL)
+    # FIXME: Fix host tools build with clang
+    #set(USE_CLANG_CL_ARG "-DCMAKE_C_COMPILER=clang-cl;-DCMAKE_CXX_COMPILER=clang-cl")
+endif()
+
 include(ExternalProject)
 
 ExternalProject_Add(host-tools
@@ -32,6 +37,6 @@ ExternalProject_Add(host-tools
     BUILD_ALWAYS 1
     PREFIX host-tools
     EXCLUDE_FROM_ALL 1
-    CMAKE_ARGS "-DNEW_STYLE_BUILD=1;-DARCH:STRING=${ARCH}"
+    CMAKE_ARGS "-DNEW_STYLE_BUILD=1;-DARCH:STRING=${ARCH};${USE_CLANG_CL_ARG}"
     INSTALL_COMMAND ""
     BUILD_BYPRODUCTS ${tools_binaries})
index 4705b3c..19790ed 100644 (file)
@@ -29,7 +29,15 @@ endif()
 
 add_definitions(/Dinline=__inline /D__STDC__=1)
 
-add_compile_flags("/X /GR- /EHs-c- /GS- /Zl /W3")
+if(NOT USE_CLANG_CL)
+    add_compile_flags("/X /Zl")
+endif()
+
+add_compile_flags("/GR- /EHs-c- /GS- /W3")
+
+if(USE_CLANG_CL)
+    set(CMAKE_CL_SHOWINCLUDES_PREFIX "Note: including file: ")
+endif()
 
 # HACK: for VS 11+ we need to explicitly disable SSE, which is off by
 # default for older compilers. See CORE-6507
@@ -88,6 +96,11 @@ endif()
 # - C4115: named type definition in parentheses
 add_compile_flags("/w14115")
 
+if(USE_CLANG_CL)
+    add_compile_flags_language("-nostdinc -Wno-multichar -Wno-char-subscripts -Wno-microsoft-enum-forward-reference -Wno-pragma-pack -Wno-microsoft-anon-tag -Wno-unknown-pragmas" "C")
+    add_compile_flags_language("-nostdinc -Wno-multichar -Wno-char-subscripts -Wno-microsoft-enum-forward-reference -Wno-pragma-pack -Wno-microsoft-anon-tag -Wno-unknown-pragmas" "CXX")
+endif()
+
 # Debugging
 #if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
@@ -101,7 +114,9 @@ endif()
 
 # Hotpatchable images
 if(ARCH STREQUAL "i386")
-    add_compile_flags("/hotpatch")
+    if(NOT USE_CLANG_CL)
+        add_compile_flags("/hotpatch")
+    endif()
     set(_hotpatch_link_flag "/FUNCTIONPADMIN:5")
 elseif(ARCH STREQUAL "amd64")
     set(_hotpatch_link_flag "/FUNCTIONPADMIN:6")
@@ -135,7 +150,8 @@ if(MSVC_IDE AND (CMAKE_VERSION MATCHES "ReactOS"))
     add_definitions(/DLANGUAGE_EN_US)
 else()
     # Only VS 10+ resource compiler supports /nologo
-    if(MSVC_VERSION GREATER 1599)
+    # CMAKE_CXX_SIMULATE_VERSION is a similar check for our clang-cl builds
+    if((MSVC_VERSION GREATER 1599) OR (CMAKE_CXX_SIMULATE_VERSION VERSION_GREATER 15.99))
         set(rc_nologo_flag "/nologo")
     else()
         set(rc_nologo_flag)
@@ -218,12 +234,18 @@ if(PCH)
             set(_pch_path_name_flag "/Fp${_gch}")
         endif()
 
+        if(USE_CLANG_CL)
+            set(_pch_compile_flags "${_cl_lang_flag} /Yc${_pch} /FI${_pch} /Fp${_gch}")
+        else()
+            set(_pch_compile_flags "${_cl_lang_flag} /Yc /Fp${_gch}")
+        endif()
+
         # Build the precompiled header
         # HEADER_FILE_ONLY FALSE: force compiling the header
         set_source_files_properties(${_pch} PROPERTIES
             HEADER_FILE_ONLY FALSE
             LANGUAGE ${_pch_language}
-            COMPILE_FLAGS "${_cl_lang_flag} /Yc /Fp${_gch}"
+            COMPILE_FLAGS ${_pch_compile_flags}
             OBJECT_OUTPUTS ${_gch})
 
         # Prevent a race condition related to writing to the PDB files between the PCH and the excluded list of source files
index 8de8a67..66c5b52 100644 (file)
@@ -581,9 +581,10 @@ endif()
 set_source_files_properties(${CRT_ASM_SOURCE} PROPERTIES COMPILE_DEFINITIONS "__MINGW_IMPORT=extern;USE_MSVCRT_PREFIX;_MSVCRT_LIB_;_MSVCRT_;_MT;CRTDLL")
 add_asm_files(crt_asm ${CRT_ASM_SOURCE})
 
-if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
-    #FIXME: http://llvm.org/bugs/show_bug.cgi?id=19027
-    set_property(SOURCE except/cpp.c APPEND_STRING PROPERTY COMPILE_FLAGS " -no-integrated-as")
+if(USE_CLANG_CL)
+    # clang-cl is missing pragma function support
+    # https://bugs.llvm.org/show_bug.cgi?id=35116
+    set_property(SOURCE stdlib/rot.c APPEND_STRING PROPERTY COMPILE_FLAGS " /fallback")
 endif()
 
 add_library(crt ${CRT_SOURCE} ${crt_asm})
index eb6d05d..c25f676 100644 (file)
@@ -107,9 +107,10 @@ function(add_asm16_bin _target _binary_file _base_address)
         set(_pp_asm16_compile_command ${CMAKE_ASM16_COMPILER} /nologo /Cp /Fo${_object_file} /c /Ta ${_preprocessed_asm_file})
     endif()
 
+    # FIXME: clang-cl can't compile this so use cl here instead of ${CMAKE_C_COMPILER} in the meantime
     add_custom_command(
         OUTPUT ${_preprocessed_asm_file} ${_object_file}
-        COMMAND ${CMAKE_C_COMPILER} /nologo /X /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/include/asm ${_directory_includes} ${_source_file_defines} ${_directory_defines} /D__ASM__ /D_USE_ML /EP /c ${_concatenated_asm_file} > ${_preprocessed_asm_file} && ${_pp_asm16_compile_command}
+        COMMAND cl /nologo /X /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/include/asm ${_directory_includes} ${_source_file_defines} ${_directory_defines} /D__ASM__ /D_USE_ML /EP /c ${_concatenated_asm_file} > ${_preprocessed_asm_file} && ${_pp_asm16_compile_command}
         DEPENDS ${_concatenated_asm_file})
 
     add_custom_command(
index e0f2b2b..6028e61 100644 (file)
@@ -14,13 +14,21 @@ set(CMAKE_SYSTEM_NAME Windows)
 set(CMAKE_SYSTEM_PROCESSOR i686)
 
 # which compilers to use for C and C++
-set(CMAKE_C_COMPILER cl)
+if(USE_CLANG_CL)
+    set(CMAKE_C_COMPILER clang-cl)
+else()
+    set(CMAKE_C_COMPILER cl)
+endif()
 
 if(ARCH STREQUAL "arm")
     include(CMakeForceCompiler)
     CMAKE_FORCE_CXX_COMPILER(cl MSVC)
 else()
-set(CMAKE_CXX_COMPILER cl)
+    if(USE_CLANG_CL)
+        set(CMAKE_CXX_COMPILER clang-cl)
+    else()
+        set(CMAKE_CXX_COMPILER cl)
+    endif()
 endif()
 
 set(CMAKE_MC_COMPILER mc)