fips icon indicating copy to clipboard operation
fips copied to clipboard

Add config file for MSVC 2022, and support for MSVC ClangCL

Open mattiasljungstrom opened this issue 3 years ago • 4 comments

Added configuration files for Visual Studio 2022, and support for ClangCL which ships with Visual Studio. ClangCL uses the same flags as normal MSVC cl. My changes will detect MSVC frontend and sets FIPS_MSVC flag.

Any cmake script that checks for MSVC should work, but some that checks for FIPS_CLANG without checking for !FIPS_MSVC might setup incorrect flags for ClangCL. Normal Clang should work with these changes.

ClangCL accepts some of clangs flags, for instance "-Wno-unused-function". I haven't found a good overview of this yet. But it means you have to do things like this:

    if (FIPS_MSVC)
        # used by CL and ClangCL
        target_compile_options(${target_name} PUBLIC /GR- /GS- /std:c++20 /std:c17 /fp:fast)
    endif()
    if (FIPS_CLANG)
        if (!FIPS_MSVC)
            # clang
            target_compile_options(${target_name} PUBLIC -ffast-math -fno-rtti)
        endif()
        # used by both Clang and ClangCL
        target_compile_options(${target_name} PUBLIC -Wno-unused-function)
    endif()

Maybe there's another option, or maybe there should be a FIPS_CLANG_CL flag?

mattiasljungstrom avatar Apr 28 '22 08:04 mattiasljungstrom

Just found out about the "-fno-ms-compatibility" flag which makes ClangCL behave like normal clang. Going to experiment a bit more with this and maybe needs a different solution.

mattiasljungstrom avatar Apr 29 '22 12:04 mattiasljungstrom

Ok let me know when the PR is ready to merge :)

Would it make sense to split the "vanilla" VS2022 build config files into a separate PR?

Also, as a side note, I had tinkered with Clang for Windows (not the clang-cl variant) a while ago and had added a toolchain file and build configs for Ninja and VSCode:

Build configs:

https://github.com/floooh/fips/blob/master/configs/win64-clang-ninja-debug.yml

https://github.com/floooh/fips/blob/master/configs/win64-clang-ninja-release.yml

https://github.com/floooh/fips/blob/master/configs/win64-clang-vscode-debug.yml

https://github.com/floooh/fips/blob/master/configs/win64-clang-vscode-release.yml

...and the toolchain file looks like this:

https://github.com/floooh/fips/blob/master/cmake-toolchains/windows-clang.cmake

...but I guess the point is to make Clang available in a Visual Studio project :) (but maybe the toolchain file helps somehow - this is using the regular clang command line args)

floooh avatar Apr 29 '22 16:04 floooh

I experimented a bit with the windows-clang.cmake toolchain, but I found it hard to get it to work correctly with ClangCL. It feels like it's better to keep ClangCL with the MS compatibility mode. Linking, and for instance turning off exceptions gets a bit messy otherwise.

I changed the setup I had originally to define a FIPS_CLANGCL flag instead of defining FIPS_MSVC. The fips cmake script picks windows.cmake based on FIPS_HOST_WINDOWS.

In user cmake files you need to handle the FIPS_CLANGCL, in a simple setup you can probably do:

if (FIPS_CLANGCL) 
  set (FIPS_MSVC 1)
endif()

or do

if (FIPS_MSVC OR FIPS_CLANGCL)
...

The reasoning behind not defining FIPS_MSVC directly is that there are MSVC flags that are not allowed in ClangCL, but mostly more obscure stuff like "/Zc:tlsGuards-".

If you think it's better to have FIPS_MSVC defined too directly I can add that, I'm sort of 50/50 on this.

mattiasljungstrom avatar May 03 '22 09:05 mattiasljungstrom

Notes:

  • ClangCL disables a lot of default flags in clang to try to get warnings and configuration to be as similar to MSVC as possible. If you want to have specific warnings you need to enable them back. (This was my main reason to compile with clang.)

  • Debug information is broken in MSs static libraries (libcpmt, libvcruntime, ..). (There's a fix coming in next version of LLVM [https://developercommunity.visualstudio.com/t/lots-of-lld-link-warnings-about-pdb-with-vs2022-17/1667797])

mattiasljungstrom avatar May 03 '22 09:05 mattiasljungstrom

FYI, in the wip cmake-presets branch I'm going to add configs for VS2022, but not the ClangCL configs. I'll close this PR. If ClangCL is still important a separate PR would be better :)

floooh avatar Nov 29 '22 18:11 floooh

ClangCL is a bit messy, and I can enable it manually if I need again.

mattiasljungstrom avatar Nov 30 '22 10:11 mattiasljungstrom