Issue 167325
Summary `clang-cl` with `CMAKE_SYSTEM_PROCESSOR=x86` completely ignores toolchain/env and fails
Labels new issue
Assignees
Reporter zeroflag0
    I'm trying to build our gtests for win-x86 (reason being 32bit Windows is the closest to our target-32bit-processor).
(the build is running in conan==1.66, all dependencies/tools are managed in conan-packages, developers/ci don't have vs_BuildTools installed)

I control the target-architecture with `CMAKE_SYSTEM_PROCESSOR=x86` and even added some hacky flags-overriding to force `-m32` and `/machine:x86` everywhere:
```#toolchain.cmake:
message(STATUS "CMAKE_SYSTEM_PROCESSOR=${CMAKE_SYSTEM_PROCESSOR}")
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86")
	message(STATUS "building for ${CMAKE_SYSTEM_PROCESSOR} with -m32 and /machine:x86")
	#sometimes there is nonsense in the build-environment, make sure it is linked as 32bit
	SET(variables
		LINK_OPTIONS
		STATIC_LIBRARY_OPTIONS
		CMAKE_EXE_LINKER_FLAGS_INIT
		CMAKE_EXE_LINKER_FLAGS
		CMAKE_MODULE_LINKER_FLAGS_INIT
		CMAKE_MODULE_LINKER_FLAGS
		CMAKE_SHARED_LINKER_FLAGS_INIT
		CMAKE_SHARED_LINKER_FLAGS
		CMAKE_STATIC_LINKER_FLAGS_INIT
		CMAKE_STATIC_LINKER_FLAGS
		CMAKE_AR_FLAGS_INIT
		CMAKE_AR_FLAGS
		CMAKE_C_LINK_FLAGS
		CMAKE_CXX_LINK_FLAGS
		CMAKE_C_LINKER_WRAPPER_FLAG
		CMAKE_CXX_LINKER_WRAPPER_FLAG
		)
	foreach(variable ${variables})
		if(${variable} MATCHES "/machine:x64")
			message(STATUS "replacing in ${variable}=${${variable}}")
			string(REGEX REPLACE "/machine:x64" "/machine:x86 -m32" ${variable} "${${variable}}")
			elseif(NOT ${variable} MATCHES "/machine:x86")
			SET(${variable} "${${variable}} /machine:x86 -m32")
			message(STATUS "setting in ${variable}=${${variable}}")
		endif()
		#message(STATUS "${variable}=${${variable}}")
	endforeach()
	add_compile_definitions(
		_PLATFORM_X86
	)
	add_compile_options(
		-m32 # compile as 32bit, that's the closest we can get to aurix-behavior
	)
	add_link_options(
		-fexceptions
		-m32 # compile as 32bit, that's the closest we can get to aurix-behavior
		/machine:x86
	)
endif()
```

which seems to work with output:
```
-- CMAKE_SYSTEM_PROCESSOR=x86
-- building for x86 with -m32 and /machine:x86
-- setting in LINK_OPTIONS= /machine:x86 -m32
-- setting in STATIC_LIBRARY_OPTIONS= /machine:x86 -m32
-- setting in CMAKE_EXE_LINKER_FLAGS_INIT=/subsystem:console /machine:x86 -m32
-- setting in CMAKE_EXE_LINKER_FLAGS= /machine:x86 -m32
-- setting in CMAKE_MODULE_LINKER_FLAGS_INIT= /machine:x86 -m32
-- setting in CMAKE_MODULE_LINKER_FLAGS= /machine:x86 -m32
-- setting in CMAKE_SHARED_LINKER_FLAGS_INIT= /machine:x86 -m32
-- setting in CMAKE_SHARED_LINKER_FLAGS= /machine:x86 -m32
-- setting in CMAKE_STATIC_LINKER_FLAGS_INIT= /machine:x86 -m32
-- setting in CMAKE_STATIC_LINKER_FLAGS= /machine:x86 -m32
-- setting in CMAKE_AR_FLAGS_INIT= /machine:x86 -m32
-- setting in CMAKE_AR_FLAGS= /machine:x86 -m32
-- setting in CMAKE_C_LINK_FLAGS= /machine:x86 -m32
-- setting in CMAKE_CXX_LINK_FLAGS= /machine:x86 -m32
-- setting in CMAKE_C_LINKER_WRAPPER_FLAG= /machine:x86 -m32
-- setting in CMAKE_CXX_LINKER_WRAPPER_FLAG= /machine:x86 -m32
```

the tools are provided from the environment:
```
env_info:
        CC = c:\clang-18.1.8\bin\clang-cl.exe
        CXX = c:\clang-18.1.8\bin\clang-cl.exe
        CMAKE_C_COMPILER = c:\clang-18.1.8\bin\clang-cl.exe
        CMAKE_CPP_COMPILER = c:\clang-18.1.8\bin\clang-cl.exe
        CMAKE_LINKER = C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.44.35207\bin\Hostx64\x86\link.exe
 CMAKE_AR = C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.44.35207\bin\Hostx64\x86\lib.exe
 CMAKE_MT = C:\Program Files (x86)\Windows Kits\10\bin\10.0.26100.0\x86\bin\x86\mt.exe
        CMAKE_RC_COMPILER = C:\Program Files (x86)\Windows Kits\10\bin\10.0.26100.0\x86\bin\x86\rc.exe
```

Compiler-checks will reproducible fail:
```
-- Check for working C compiler: c:/clang-18.1.8/bin/clang-cl.exe
-- Check for working C compiler: c:/clang-18.1.8/bin/clang-cl.exe - broken
CMake Error at c:/cmake/share/cmake-4.1/Modules/CMakeTestCCompiler.cmake:67 (message):
 The C compiler

    "c:/clang-18.1.8/bin/clang-cl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: 'c:/src/gtest/CMakeFiles/CMakeScratch/TryCompile-v7hwns'

    Run Build Command(s): c:/cmake/bin/ninja.exe -v cmTC_389e7
    [1/2] c:\clang-18.1.8\clang-cl.exe  /nologo @CMakeFiles\cmTC_389e7.dir\testCCompiler.c.obj.rsp /showIncludes /FoCMakeFiles\cmTC_389e7.dir\testCCompiler.c.obj /FdCMakeFiles\cmTC_389e7.dir\cmTC_389e7.pdb -c -- c:\src\gtest\CMakeFiles\CMakeScratch\TryCompile-v7hwns\testCCompiler.c
 [2/2] C:\WINDOWS\system32\cmd.exe /C "cd . && c:\clang-18.1.8\llvm-lib.exe /nologo /machine:x64 /out:cmTC_389e7.lib @CMakeFiles\cmTC_389e7.rsp && cd ."
    FAILED: [code=1] cmTC_389e7.lib
 C:\WINDOWS\system32\cmd.exe /C "cd . && c:\clang-18.1.8\llvm-lib.exe /nologo /machine:x64 /out:cmTC_389e7.lib @CMakeFiles\cmTC_389e7.rsp && cd ."
    CMakeFiles\cmTC_389e7.dir\testCCompiler.c.obj: file machine type x86 conflicts with library machine type x64 (from '/machine:x64' flag)
 ninja: build stopped: subcommand failed.





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
 CMakeLists.txt:14 (project)


-- Configuring incomplete, errors occurred!
```

When I `CMAKE_CXX_COMPILER_WORKS 1` gtest 1.10 builds sucessfully, so my toolchain.cmake isn't that far off...
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to