RenatoUtsch
RenatoUtsch

Reputation: 1559

Doubts regarding config_setting with Bazel

I'm trying to understand config_setting for detecting the underlying platform and had some doubts. Could you help me clarify them?

  1. What is the difference between x64_windows and x64_windows_(msvc|msys) cpus? If I create config_setting's for all of them, will only one of them trigger? Should I just ignore x64_windows?

  2. To detect Windows, what is the recommended way? Currently I'm doing:

    config_setting(
        name = "windows",
        values = {"crosstool_top": "//crosstools/windows"},
    )
    
    config_setting(
        name = "windows_msvc",
        values = {
            "crosstool_top": "//crosstools/windows",
            "cpu": "x64_windows_msvc",
        },
    )
    
    config_setting(
        name = "windows_msys",
        values = {
            "crosstool_top": "//crosstools/windows",
            "cpu": "x64_windows_msys",
        },
    )
    

    By using this I want to use :windows to match all Windows versions and :windows_msvc, for example, to match only MSVC. Is this the best way to do it?

  3. What is the difference between darwin and darwin_x86_64 cpus? I know they match macOS, but do I need to always specify both when selecting something for macOS? If not, is there a better way to detect macOS with only one config_setting? Like using //crosstools with Windows?

  4. How do detect Linux? I know you can detect the operating systems you care about first and then use //conditions:default, but it'd be nice to have a way to detect specifically Linux and not leave it as the default.

  5. What are k8, piii, etc? Is there any documentation somewhere describing all the possible cpu values and what they mean?

  6. If I wanted to use //crosstools to detect each platform, is there somewhere I can look up all available crosstools?

Thanks!

Upvotes: 2

Views: 827

Answers (1)

Greg E
Greg E

Reputation: 46

Great questions, all. Let me tackle them one by one:

  1. --cpu=x64_windows_msys triggers the C++ toolchain that relies on MSYS/Cygwin. --cpu=x64_windows_msvc triggers the Windows-native (MSVC) toolchain. -cpu=x64_windows triggers the default, which is still MSYS but being converted to MSVC.

    Which ones you want to support is up to you, but it's probably safest to support all for generality (and if one is just an alias for the other it doesn't require very complicated logic).

    Only one config_setting can trigger at a time.

  2. Unless you'e using a custom -crosstool_top= flag to specify Windows builds, you'll probably want to trigger on --cpu, e.g:

    config_setting(
        name = "windows",
        values = {"cpu": "x64_windows"}
    

    There's not a great way now to define all Windows. This is a current deficiency in Bazel's ability to recognize platforms, which settings like --cpu and --crosstool_top don't quite model the right way. Ongoing work to create a first-class concept of platform will provide the best solution to what you want. But for now --cpu is probably your best option.

  3. This would basically be the same story as Windows. But to my knowledge there's only darwin for default crosstools, no darwin_x86_64.

  4. For the time being it's probably best to use the //conditions:default approach you'd rather not do. Once first-class platforms are available that'll give you the fidelity you want.

  5. k8 and piii are pseudonyms for 86 64-bit and 32-bit CPUs, respectively. They also tend to be associated with "Linux" by convention, although this is not a guaranteed 1-1 match.

    There is no definitive set of "all possible CPU values". Basically, --cpu is just a string that gets resolved in CROSSTOOL files to toolchains with identifiers that match that string. This allows you to write new CROSSTOOL files for new CPU types you want to encode yourself. So the exact set of available CPUs depends on who's using Bazel and how they have their workspace set up.

  6. For the same reasons as 5., there is no definitive list. See Bazel's github tools/ directory for references to defaults.

Upvotes: 1

Related Questions