Llvm/clang 9, Windows, __m128 not defined

Howdy!

So, I tend to want to just use CMake, Ninja, and Clang to build stuff and as recent as llvm 8 I didn’t have to use clang-cl.exe or work from the visual studdio developer shell.

So far everything I’ve wanted to build has worked just fine and dandy, but presently I’ve discovered that I can’t build SDL 2.0.10 this way.

There appears to be a problem with how SSE intrinsics are being detected.

The rough sequence of events that could lead to the following errors:

scoop install llvm
… download SDL2 sources and unpack them somewhere …
… then from the unpacked source root …
cmake -B.build -DCMAKE_BUILD_TYPE=Release -DCMAKE_RC_COMPILER=llvm-rc -GNinja
ninja -C .build SDL2-static

First error for me happens in SDL_audiocvt.c but I know other sources have similar issues:

[7/150] Building C object CMakeFiles/SDL2-static.dir/src/audio/SDL_audiocvt.c.obj
FAILED: CMakeFiles/SDL2-static.dir/src/audio/SDL_audiocvt.c.obj
C:\Users\phote\scoop\shims\clang.exe -DUSING_GENERATED_CONFIG_H -Iinclude -I../include -idirafter C:/projects/slowgames/allyerfault/vendor/SDL2/2.0.10-src/src/video/khronos  -IC:/projects/slowgames/allyerfault/vendor/SDL2/2.0.10-src/src/hidapi/hidapi -msse3 -msse2 -msse -m3dnow -mmmx -Wshadow -Wdeclaration-after-statement -Werror=declaration-after-statement -fno-strict-aliasing -Wall  -O3 -DNDEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrt -MD -MT CMakeFiles/SDL2-static.dir/src/audio/SDL_audiocv
t.c.obj -MF CMakeFiles\SDL2-static.dir\src\audio\SDL_audiocvt.c.obj.d -o CMakeFiles/SDL2-static.dir/src/audio/SDL_audiocvt.c.obj   -c ../src/audio/SDL_audiocvt.c
../src/audio/SDL_audiocvt.c:59:15: error: unknown type name '__m128'
        const __m128 divby2 = _mm_set1_ps(0.5f);
              ^
../src/audio/SDL_audiocvt.c:59:31: warning: implicit declaration of function '_mm_set1_ps' is invalid in C99 [-Wimplicit-function-declaration]
        const __m128 divby2 = _mm_set1_ps(0.5f);
                              ^
../src/audio/SDL_audiocvt.c:61:13: warning: implicit declaration of function '_mm_store_ps' is invalid in C99 [-Wimplicit-function-declaration]
            _mm_store_ps(dst, _mm_mul_ps(_mm_hadd_ps(_mm_load_ps(src), _mm_load_ps(src+4)), divby2));
            ^
../src/audio/SDL_audiocvt.c:61:31: warning: implicit declaration of function '_mm_mul_ps' is invalid in C99 [-Wimplicit-function-declaration]
            _mm_store_ps(dst, _mm_mul_ps(_mm_hadd_ps(_mm_load_ps(src), _mm_load_ps(src+4)), divby2));
                              ^
../src/audio/SDL_audiocvt.c:61:42: warning: implicit declaration of function '_mm_hadd_ps' is invalid in C99 [-Wimplicit-function-declaration]
            _mm_store_ps(dst, _mm_mul_ps(_mm_hadd_ps(_mm_load_ps(src), _mm_load_ps(src+4)), divby2));
                                         ^
../src/audio/SDL_audiocvt.c:61:54: warning: implicit declaration of function '_mm_load_ps' is invalid in C99 [-Wimplicit-function-declaration]
            _mm_store_ps(dst, _mm_mul_ps(_mm_hadd_ps(_mm_load_ps(src), _mm_load_ps(src+4)), divby2));
                                                     ^
5 warnings and 1 error generated.

I see these functions and that type defined in the llvm installation so I feel pretty sure that there is a pretty simple configuration issue in the CMake build that isn’t handling this particular toolchain arrangement.

I know I can just run this run a developer shell and everything will be hunky dory but I’m really curious to figure out why this isn’t just working. I’d love it if someone more experienced in this matter could help me dig around.

Yep, it’s seem’s that clang-cl and clang on Windows are not detected properly and separated as we need to do

I think that the problem is the include header of __m128 is not included correctly

I too have encountered this problem. Although not using clang, you can build SDL2.0.10 with the mingw-w64 toolchain (https://sourceforge.net/projects/mingw-w64/) on windows, which does not require microsoft headers or compilers. Once installed, run the following batch file (should be placed in root sdl source folder):

@ECHO OFF

IF NOT EXIST windows-build MKDIR windows-build
PUSHD windows-build

REM IMPORTANT(Ryan): Cannot be built with clang
cmake.exe -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE="Debug"^
  -DCMAKE_C_COMPILER_ID="GNU" -D_CMAKE_C_COMPILER="gcc.exe"^
  -DCMAKE_CXX_COMPILER_ID="GNU" -D_CMAKE_C_COMPILER="g++.exe"^
  -DDIRECTX=OFF^
  -DSDL_TEST=OFF -DSDL_SHARED=OFF -DSDL_STATIC=ON ..

cmake.exe --build . -- -j %NUMBER_OF_PROCESSORS%

POPD

Of course you can change what you want the library to be built with, e.g for this I’m building with opengl and nor directx. You can then build your own code with clang and link with your dynamic or static SDL2.0.10 library you just compiled.