conan-io / cmake-conan

CMake wrapper for conan C and C++ package manager
MIT License
823 stars 250 forks source link

[bug] `conan_cmake_autodetect()` doesn't specify `arch` setting for MinGW compilers. #448

Open hwhsu1231 opened 1 year ago

hwhsu1231 commented 1 year ago

Problem Desciprtion

It seems that conan_cmake_autodetect() doesn't specify arch setting for MinGW compilers. Therefore, some errors will occur when using MinGW x86 compiler since Conan will set it to arch=x86_64 by default.

Environments and Versions

NOTE: Since GCC version 12.2 doesn't add to settings.yml in Conan 1.53.0 by default, I manually modified the settings.yml like this.

img_001_2022-10-16_19-28-12

hwhsu1231 commented 1 year ago

Demonstration

First of all, I installed MinGW x86 and MinGW x64 compilers by running the following commands on MSYS2.

pacman -S mingw-w64-i686-toolchain
pacman -S mingw-w64-x64_86-toolchain

And I append the bin directory into PATH env when configure the preset, like:

{
  "environment": {
    "PATH": "C:/msys64/mingw32/bin;$penv{PATH}"
  }
}

The following are my example CMakePresets.json, CMakeLists.txt, and conanfile.py.

Click to expand CMakePresets.json ```json { "version": 3, "configurePresets": [ { "name": "win32-msvc-x64-nmake-debug", "displayName": "Windows MSVC x64 (NMake Makefiles) Debug", "description": "Using MSVC x64 compilers with \"NMake Makefiles\" generator on Windows - Debug", "generator": "NMake Makefiles", "toolset": { "value": "host=x64", "strategy": "external" }, "architecture": { "value": "x64", "strategy": "external" }, "binaryDir": "${sourceDir}/build/${presetName}", "cacheVariables": { "CMAKE_C_COMPILER": "cl.exe", "CMAKE_CXX_COMPILER": "cl.exe", "CMAKE_BUILD_TYPE": "Debug" } }, { "name": "win32-msvc-x86-nmake-debug", "displayName": "Windows MSVC x86 (NMake Makefiles) Debug", "description": "Using MSVC x86 compilers with \"NMake Makefiles\" generator on Windows - Debug", "generator": "NMake Makefiles", "toolset": { "value": "host=x86", "strategy": "external" }, "architecture": { "value": "x86", "strategy": "external" }, "binaryDir": "${sourceDir}/build/${presetName}", "cacheVariables": { "CMAKE_C_COMPILER": "cl.exe", "CMAKE_CXX_COMPILER": "cl.exe", "CMAKE_BUILD_TYPE": "Debug" } }, { "name": "win32-gcc-x64-mingw-debug", "displayName": "Windows GCC x64 (MinGW Makefiles) Debug", "description": "Using GCC x64 compiler with \"MinGW Makefiles\" geneartor on Windows - Debug", "generator": "MinGW Makefiles", "environment": { "PATH": "C:/msys64/mingw64/bin;$penv{PATH}" }, "binaryDir": "${sourceDir}/build/${presetName}", "cacheVariables": { "CMAKE_C_COMPILER": "gcc.exe", "CMAKE_CXX_COMPILER": "g++.exe", "CMAKE_BUILD_TYPE": "Debug" } }, { "name": "win32-gcc-x86-mingw-debug", "displayName": "Windows GCC x86 (MinGW Makefiles) Debug", "description": "Using GCC x86 compiler with \"MinGW Makefiles\" geneartor on Windows - Debug", "generator": "MinGW Makefiles", "environment": { "PATH": "C:/msys64/mingw32/bin;$penv{PATH}" }, "binaryDir": "${sourceDir}/build/${presetName}", "cacheVariables": { "CMAKE_C_COMPILER": "gcc.exe", "CMAKE_CXX_COMPILER": "g++.exe", "CMAKE_BUILD_TYPE": "Debug" } } ] } ```
Click to expand CMakeLists.txt ```cmake cmake_minimum_required(VERSION 3.21) get_filename_component(folder_name "${CMAKE_CURRENT_SOURCE_DIR}" NAME) project(${folder_name} LANGUAGES C CXX) message("========== ${PROJECT_NAME} ==========") # Download 'conan.cmake' from "https://github.com/conan-io/cmake-conan" if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake") message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan") file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/0.18.1/conan.cmake" "${CMAKE_BINARY_DIR}/conan.cmake" TLS_VERIFY ON) endif() include("${CMAKE_BINARY_DIR}/conan.cmake") conan_cmake_autodetect(settings) message("settings = ${settings}") ```
Click to expand conanfile.py ```python from conans import ConanFile, CMake class ConsumerConan(ConanFile): settings = [ "os", "compiler", "build_type", "arch" ] generators = [ "CMakeDeps", "CMakeToolchain" ] requires = [ "fmt/8.1.1@" ] ```

Prepare the above three files in the same folder, and then configure the corresponding preset:

As we can see, there isn't arch=x86 or arch=x86_64 in settings variable when using MinGW x86 or MinGW x64 compiler.

czoido commented 1 year ago

Hi @hwhsu1231 ,

Thanks for the question. conan_cmake_autodetect() just implements basic autodetection, for more advanced cases like using Windows subsystems and different x86 and x64 compilers I would recommend you to pass the ARCH argument to conan_cmake_autodetect() because as you say, cmake-conan is not clever enough to support those cases.

hwhsu1231 commented 1 year ago

@czoido Then how about improving the autodetection of conan_cmake_autodetect()?

czoido commented 1 year ago

@czoido Then how about improving the autodetection of conan_cmake_autodetect()?

Yes, we could check in the future if there is a demand by users but I'm afraid that right now is not a priority as the intention of that helper was to cover basic use cases. We may also accept contributions for that if they don't complicate too much the code or have the risk of breaking cases that currently work.

hwhsu1231 commented 1 year ago

Maybe we can use gcc -dumpmachine to check the architecture.

image