Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hwhsu1231 opened this issue Oct 16, 2022 · 5 comments
Labels

Comments

@hwhsu1231
Copy link
Contributor

hwhsu1231 commented Oct 16, 2022

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

  • OS version: Windows 11
  • Compiler version: GCC 12.2.0
  • CMake version: CMake 3.24.2
  • Conan version: Conan 1.53.0
  • conan-cmake: 1.8.1

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
Copy link
Contributor Author

hwhsu1231 commented Oct 16, 2022

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
{
  "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_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
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:

  • For MSVC x64 compiler, configure with win32-msvc-x64-nmake-debug preset.

    Click to expand
    • Type the following commands:

      vcvarsall x64
      cmake --preset win32-msvc-x64-nmake-debug
    • Here is the result of settings variable:

      settings = arch=x86_64;build_type=Debug;compiler=Visual Studio;compiler.version=16;compiler.runtime=MDd
  • For MSVC x86 compiler, configure with win32-msvc-x86-nmake-debug preset.

    Click to expand
    • Type the following commands:

      vcvarsall x86
      cmake --preset win32-msvc-x86-nmake-debug
    • Here is the result of settings variable:

      settings = arch=x86;build_type=Debug;compiler=Visual Studio;compiler.version=16;compiler.runtime=MDd
  • For MinGW x64 compiler, configure with win32-msvc-x64-mingw-debug preset.

    Click to expand
    • Type the following commands:

      cmake --preset win32-msvc-x64-mingw-debug
      
    • Here is the result of settings variable:

      settings = build_type=Debug;compiler=gcc;compiler.version=12.2;compiler.libcxx=libstdc++11
  • For MinGW x86 compiler, configure with win32-msvc-x86-mingw-debug preset.

    Click to expand
    • Type the following commands:

      cmake --preset win32-msvc-x86-mingw-debug
      
    • Here is the result of settings variable:

      settings = build_type=Debug;compiler=gcc;compiler.version=12.2;compiler.libcxx=libstdc++11

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
Copy link
Contributor

czoido commented Oct 17, 2022

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
Copy link
Contributor Author

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

@czoido
Copy link
Contributor

czoido commented Oct 17, 2022

@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
Copy link
Contributor Author

hwhsu1231 commented Oct 17, 2022

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

  • For GCC/MinGW x64 compiler, it will print x86_64-w64-mingw32.
  • For GCC/MinGW x86 compiler, it will print i686-w64-mingw32.
  • For GCC/MSYS compiler, it will print x86_64-pc-msys.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants