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

[feature] Design an internal macro to detect arch setting, _conan_detect_arch(). #457

Open
hwhsu1231 opened this issue Oct 31, 2022 · 2 comments

Comments

@hwhsu1231
Copy link
Contributor

hwhsu1231 commented Oct 31, 2022

Feature Request

In the conan_cmake_autodetect() macro (so far 1.18.1), there are some internal macros to detect the required settings:

  • _conan_detect_build_type()
  • _conan_check_system_name()
  • _conan_check_language()
  • _conan_detect_compiler()
  • _collect_settings()

As we can see, there isn't a macro to detect arch setting. Only when we use MSVC compilers, _conan_detect_compiler() will detect it specifically. Since arch is one of the required settings, I suggest that cmake-conan design an internal macro called _conan_detect_arch() to do so.

@hwhsu1231 hwhsu1231 changed the title [feature] Design an internal macro _conan_detect_arch() to detect arch setting. [feature] Design an internal macro to detect arch setting, _conan_detect_arch(). Oct 31, 2022
@hwhsu1231
Copy link
Contributor Author

This issue is related to: #448

@hwhsu1231
Copy link
Contributor Author

hwhsu1231 commented Nov 4, 2022

In fact, CMake already defined a variable called CMAKE_LANG_COMPILER_ARCHITECTURE_ID, which should be able to detect the architecture of compilers currently used. However, this variable is NOT computed everywhere for now. Here are the links of related issues in CMake repository:

According to my experiments on Windows, the following compilers is computed:

  • MSVC:

    %VCToolsInstallDir%\bin\Hostx64\x64\cl.exe
    %VCToolsInstallDir%\bin\Hostx64\x86\cl.exe
    %VCToolsInstallDir%\bin\Hostx86\x64\cl.exe
    %VCToolsInstallDir%\bin\Hostx86\x86\cl.exe
    
  • MSVC-based Clang:

    C:\Program Files\LLVM\bin\clang.exe
    C:\Program Files\LLVM\bin\clang-cl.exe
    C:\Program Files (x86)\LLVM\bin\clang.exe
    C:\Program Files (x86)\LLVM\bin\clang-cl.exe
    %VCINSTALLDIR%\Tools\Llvm\x64\bin\clang.exe
    %VCINSTALLDIR%\Tools\Llvm\x64\bin\clang-cl.exe
    %VCINSTALLDIR%\Tools\Llvm\bin\clang.exe
    %VCINSTALLDIR%\Tools\Llvm\bin\clang-cl.exe
    

Therefore, the implementation of _conan_detect_arch() can be like:

macro(_conan_detect_arch)
    conan_parse_arguments(${ARGV})
    
    if (ARGUMENTS_ARCH)
        set(_CONAN_SETTING_ARCH ${ARGUMENTS_ARCH})
    else ()
        if ("${CMAKE_${LANGUAGE}_COMPILER_ID}" STREQUAL "MSVC" 
            OR ("${CMAKE_${LANGUAGE}_COMPILER_ID}" STREQUAL "Clang" 
                AND "${CMAKE_${LANGUAGE}_SIMULATE_ID}" STREQUAL "MSVC"))
            # using MSVC 
            # using MSVC-based Clang
            if (CMAKE_${LANGUAGE}_COMPILER_ARCHITECTURE_ID MATCHES "64")
                set(_CONAN_SETTING_ARCH x86_64)
            elseif (CMAKE_${LANGUAGE}_COMPILER_ARCHITECTURE_ID MATCHES "^ARM")
                set(_CONAN_SETTING_ARCH armv6)
            elseif (CMAKE_${LANGUAGE}_COMPILER_ARCHITECTURE_ID MATCHES "86")
                set(_CONAN_SETTING_ARCH x86)
            else ()
                # Other architectures...
            endif()
        else ()
            # Other C/C++ compilers...
            # Use other mechanism to detect its architecture 
        endif ()
    endif ()
endmacro()

Theoratically, once CMAKE_LANG_COMPILER_ARCHITECTURE_ID can be computed everywhere, the implementation should be able to reduce to:

macro(_conan_detect_arch)
    conan_parse_arguments(${ARGV})
    
    if (ARGUMENTS_ARCH)
        set(_CONAN_SETTING_ARCH ${ARGUMENTS_ARCH})
    else ()
        if (CMAKE_${LANGUAGE}_COMPILER_ARCHITECTURE_ID MATCHES "64")
            set(_CONAN_SETTING_ARCH x86_64)
        elseif (CMAKE_${LANGUAGE}_COMPILER_ARCHITECTURE_ID MATCHES "^ARM")
            set(_CONAN_SETTING_ARCH armv6)
        elseif (CMAKE_${LANGUAGE}_COMPILER_ARCHITECTURE_ID MATCHES "86")
            set(_CONAN_SETTING_ARCH x86)
        else ()
            # Other architectures...
        endif()
    endif ()
endmacro()

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

No branches or pull requests

1 participant