Skip to content

Conversation

@the-horo
Copy link
Contributor

When cross-compiling on a non-Apple Posix build machine pick the C cross compiler (when not specified through $CC or -gcc=) from:

  • <triple>-gcc
  • <triple>-clang
  • clang --target=<triple>

This should be a better UX when cross-compiling with simpler build systems like dub or even without one at all, offering a out-of-the-box functional setup, on most systems.

The advantage to performing this check in the compiler code as opposed to hardcoding a default value in the config file is that:

  1. Multiple programs can be searched, instead of just one
  2. The $CC value continues to be respected, which keeps more advanced cross-compiling setups working (for example Meson)

A sample implementation of #4978 (comment), I haven't rigorously tested this but I'm interested in comments on the approach/fallback rules.

@the-horo the-horo force-pushed the cross-cc-default branch 3 times, most recently from 25265fc to 57a6d72 Compare September 13, 2025 18:44
@the-horo
Copy link
Contributor Author

I have no idea how this fails with LLVM-15 but works with all others, given that the C++ compiler is the same

FAILED: [code=1] CMakeFiles/LDCShared.dir/driver/tool.cpp.o 
/usr/bin/c++ -DLDC_ENABLE_PLUGINS -DLDC_JITRT_USE_JITLINK -DLDC_LLVM_SUPPORTED_TARGET_AArch64=1 -DLDC_LLVM_SUPPORTED_TARGET_AMDGPU=1 -DLDC_LLVM_SUPPORTED_TARGET_ARM=1 -DLDC_LLVM_SUPPORTED_TARGET_AVR=1 -DLDC_LLVM_SUPPORTED_TARGET_BPF=1 -DLDC_LLVM_SUPPORTED_TARGET_Hexagon=1 -DLDC_LLVM_SUPPORTED_TARGET_Lanai=1 -DLDC_LLVM_SUPPORTED_TARGET_M68k=1 -DLDC_LLVM_SUPPORTED_TARGET_MSP430=1 -DLDC_LLVM_SUPPORTED_TARGET_Mips=1 -DLDC_LLVM_SUPPORTED_TARGET_NVPTX=1 -DLDC_LLVM_SUPPORTED_TARGET_PowerPC=1 -DLDC_LLVM_SUPPORTED_TARGET_RISCV=1 -DLDC_LLVM_SUPPORTED_TARGET_Sparc=1 -DLDC_LLVM_SUPPORTED_TARGET_SystemZ=1 -DLDC_LLVM_SUPPORTED_TARGET_VE=1 -DLDC_LLVM_SUPPORTED_TARGET_WebAssembly=1 -DLDC_LLVM_SUPPORTED_TARGET_X86=1 -DLDC_LLVM_SUPPORTED_TARGET_XCore=1 -I/home/runner/work/ldc/ldc/. -I/home/runner/work/ldc/ldc/dmd -DDMDV2 -O3 -DNDEBUG -I/usr/lib/llvm-15/include -std=c++14   -fno-exceptions -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -fno-rtti  -Wall -Wextra -Wno-unused-parameter -Wno-comment -Wno-missing-field-initializers -Wno-non-virtual-dtor -Wno-pedantic -DLDC_POSIX  -DIN_LLVM -DOPAQUE_VTBLS "-DLDC_INSTALL_PREFIX=R\"(/usr/local)\"" -DLDC_LLVM_VER=1500 "-DLDC_LIBDIR_SUFFIX=R\"(64)\"" -DLDC_HOST_DigitalMars=1 -DLDC_HOST_FE_VER=2111 "-DLDC_LLVM_LIBDIR=R\"(/usr/lib/llvm-15/lib)\"" -DNDEBUG -MD -MT CMakeFiles/LDCShared.dir/driver/tool.cpp.o -MF CMakeFiles/LDCShared.dir/driver/tool.cpp.o.d -o CMakeFiles/LDCShared.dir/driver/tool.cpp.o -c /home/runner/work/ldc/ldc/driver/tool.cpp
/home/runner/work/ldc/ldc/driver/tool.cpp: In function ‘llvm::SmallVector<std::__cxx11::basic_string<char>, 2> {anonymous}::findCCFallback()’:
/home/runner/work/ldc/ldc/driver/tool.cpp:68:14: error: missing template arguments before ‘choices’
   68 |   std::array choices = {
      |              ^~~~~~~
/home/runner/work/ldc/ldc/driver/tool.cpp:77:22: error: ‘choices’ was not declared in this scope; did you mean ‘choice’?
   77 |   for (auto choice : choices) {
      |                      ^~~~~~~
      |                      choice
/home/runner/work/ldc/ldc/driver/tool.cpp:90:97: error: ‘choices’ was not declared in this scope
   90 |   tip("Make sure you have a compiler like `%s` installed and set $CC or pass -gcc accordingly", choices[0][0].c_str());
      |                                                                                                 ^~~~~~~

@JohanEngelen
Copy link
Member

I have no idea how this fails with LLVM-15 but works with all others, given that the C++ compiler is the same

LLVM 16 and onwards is compiled with -std=c++17. Probably it is because of: https://en.cppreference.com/w/cpp/container/array/deduction_guides.html (not supported in -std=c++14 mode).

@the-horo
Copy link
Contributor Author

Oh, I guess that makes sense.

@the-horo the-horo force-pushed the cross-cc-default branch 2 times, most recently from 5859087 to 74a4062 Compare September 17, 2025 07:21
#ifdef _WIN32
choices = {
{ "clang-cl.exe" },
{ "cl.exe" },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, this should actually work, because of the preceding windows::MsvcEnvironmentScope in runCPreprocessor() - cl.exe should be in PATH by then, so that the later findProgramByName() succeeds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I wasn't sure if I wanted to move it inside findCCFallback because it looked like that setup included environment needed for compilation & preprocessing and, at the end of the function, they would be rollback.

Copy link
Member

@kinke kinke Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine as-is in that regard. But what's more problematic is that native Windows builds would now have -v output and potential errors mentioning a 'C cross-compiler', while that's not the case in this #ifdef _WIN32 block. So analogous to the Posix-native-build case, we'd need an early-return / common helper function / or simply using choices for the Posix-native-build case too, and then adapting the strings based on isNativeBuild.

@the-horo the-horo force-pushed the cross-cc-default branch 4 times, most recently from 321f9c4 to 0326b59 Compare September 23, 2025 05:05
@kinke
Copy link
Member

kinke commented Sep 26, 2025

Looks pretty ready to me, just 2 nits. - If this lands after #4978, you could remove the FIXME comment for the Android x86[_64] .confs, no need to inject -gcc anymore.

Try to pick a better default instead of `cc` when
cross-compiling. When targeting Linux on a non-Linux build machine,
for example, try to pick `cc` from:
- `<triple>-gcc`
- `<triple>-clang`
- `clang --target=<triple>`

This should be a better UX when cross-compiling with simpler build
systems like `dub` or even without one at all, offering a
out-of-the-box functional setup, on most systems.

The advantage to performing this check in the compiler code as opposed
to hardcoding a default value in the config file is that:
1. Multiple programs can be searched, instead of just one
2. The `$CC` value continues to be respected, which keeps more
advanced cross-compiling setups working (for example Meson)

Signed-off-by: Andrei Horodniceanu <a.horodniceanu@proton.me>
@the-horo the-horo marked this pull request as ready for review September 27, 2025 13:33
@kinke
Copy link
Member

kinke commented Sep 27, 2025

Cheers, LGTM, modulo error strings starting in uppercase. Let me fix the changelog merge conflict and convert to lowercase.

@kinke kinke enabled auto-merge (squash) September 27, 2025 14:58
@kinke kinke merged commit 9b65296 into ldc-developers:master Sep 27, 2025
21 checks passed
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

Successfully merging this pull request may close these issues.

3 participants