tools-windows-msvc icon indicating copy to clipboard operation
tools-windows-msvc copied to clipboard

Enable libdispatch on x86

Open triplef opened this issue 3 years ago • 5 comments

triplef avatar Jun 02 '21 14:06 triplef

@davidchisnall could you do me a favor when you get a chance and take a look at these linker errors I’m getting for libdispatch when building it for 32-bit with the MSVC toolchain?

This is being built in a x86 Developer Command Prompt, but for some reason the linker command from CMake ends up with a bunch of extra LIBPATH options referencing the x64 toolchain. Can you think of anything that might cause this? I checked the CMake files of libdispatch but don’t see anything in that direction.

The CMake command should be the following, and the same setup works fine building libobjc2:

cmake .. ^
  -G Ninja ^
  -D CMAKE_BUILD_TYPE=Debug ^
  -D CMAKE_INSTALL_PREFIX="%INSTALL_PREFIX%" ^
  -D CMAKE_C_COMPILER=clang-cl ^
  -D CMAKE_CXX_COMPILER=clang-cl ^
  -D BUILD_SHARED_LIBS=YES ^
  -D INSTALL_PRIVATE_HEADERS=YES ^
  -D CMAKE_CXX_FLAGS_RELWITHDEBINFO="/Zi" ^
  -D CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO="/INCREMENTAL:NO /DEBUG /OPT:REF /OPT:ICF" ^
  -D BlocksRuntime_INCLUDE_DIR=%INSTALL_PREFIX%\include ^
  -D BlocksRuntime_LIBRARIES=%INSTALL_PREFIX%\lib\objc.lib ^

I’d appreciate your thoughts on this. Thank you!

triplef avatar Aug 05 '21 07:08 triplef

All of those errors look like they're from the CRT. If you're using a recent CMake (3.20 or later?), it provides a cleaner way of specifying the target than depending on ambient bits of environment. If you specify -A {Win32,x64} then you can tell CMake which architecture to target. With -T ClangCL then you will use the clang toolchain that shipped with VS 2019 (this may need to be an optional install, but it's present on the GitHub-managed action runners, we're using it for snmalloc.

davidchisnall avatar Aug 05 '21 11:08 davidchisnall

Thanks for the suggestion using the VS generator with -T ClangCL. Unfortunately though that is giving the exact same linker errors with -A Win32.

Btw. I also tried using that generator with libobjc2, but I’m getting the error "File not found - objc.pdb" when installing with the following command (even though the file is there in the Debug directory): cmake --build . --config Debug --target install

triplef avatar Aug 06 '21 20:08 triplef

The libobjc2 CMake contains numerous hacks because it requires a version of CMake that predates CMake's official support for Objective-C. Riccardo objected to moving it forward. At some point I will make a modern CMakeLists.txt and move the current one to CMakeLists.Riccardo for him to use. Most platforms now provide a mechanism for installing the latest cmake (it's the default everywhere except Ubuntu and it's in a snap there), so maintaining support for old versions is not particularly useful.

davidchisnall avatar Aug 07 '21 10:08 davidchisnall

Feedback in the libobjc2 issue:

There was a time where the x86 build of libdispatch didn't work, but I believe all the issues have been resolved. Yes, this is a bit trickier to get right, but the following should give you a working build: https://github.com/compnerd/swift-build/blob/master/build.cmd#L502 https://github.com/compnerd/swift-build/blob/master/build.cmd#L620-L639 with appropriate modifications of course.

FTR, the undefined symbol __DllMainCRTStartup is going to be fulfilled by msvcrt.lib.

From the above are a couple CMake options we’re not setting, e.g.:

  -D CMAKE_C_COMPILER_TARGET=i686-unknown-windows-msvc                          ^
  -D CMAKE_CXX_COMPILER_TARGET=i686-unknown-windows-msvc                        ^
  -D CMAKE_SYSTEM_NAME=Windows                                                  ^
  -D CMAKE_SYSTEM_PROCESSOR=i686                                                ^
  -D CMAKE_MT=mt                                                                ^

triplef avatar Oct 04 '22 13:10 triplef