Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Prefer /usr/bin/clang as default CC on darwin #10823

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,24 @@ AS_CASE(["${build_os}"],
# Clang on Solaris is largely untested.
# https://bugs.ruby-lang.org/issues/17949
AC_CHECK_TOOLS([CC], [cc gcc])
],
[darwin*], [
# /usr/bin/clang is always available as long as Command Line Tools or Xcode is installed.
# Prefer absolute path to avoid PATH lookup, which may lead to unaligned tool selection.
rb_darwin_default_CC=/usr/bin/clang
AC_MSG_CHECKING([for darwin default CC $rb_darwin_default_CC])
AS_IF([test -z "$CC" -a -x "$rb_darwin_default_CC"],
[
AC_MSG_RESULT([yes])
CC=$rb_darwin_default_CC
],
[
AC_MSG_RESULT([no])
# Fall back to PATH lookup just in case the /usr/bin/clang is not
# available for some reason.
AC_CHECK_TOOLS([CC], [clang cc gcc c99])
]
)
Comment on lines +189 to +205
Copy link
Member

Choose a reason for hiding this comment

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

What about expand rb_CC absolute path, only if the found CC is Xcode?

    AS_IF([$rb_CC -E -dM -xc - < /dev/null | grep -F __apple_build_version__ > /dev/null],
          [llvm_prefix= rb_CC=`command -v $rb_CC`], [llvm_prefix=llvm-])

Copy link
Member Author

@kateinoigakukun kateinoigakukun May 23, 2024

Choose a reason for hiding this comment

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

What we are trying to solve is unaligned toolset selection in the situation where PATH contains mixed toolchains (e.g. LLVM from Homebrew, Conda toolchain?, and Xcode).

e.g.

$ ls /path/to/conda/bin
clang # but no nm
$ ls /usr/bin
clang nm ...
$ export PATH=/path/to/conda:/usr/bin

If we continue looking up CC from PATH and align toolset only when CC comes from Xcode, the problem won't be mitigated for the above situation.
That's why I prefer fixed /usr/bin/clang as the default decision.

Copy link

@rubyFeedback rubyFeedback May 23, 2024

Choose a reason for hiding this comment

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

Not disagreeing with you as such, but it should be pointed out that at the least on Linux, the default assumption is that the entries in $PATH coming first, have higher precedence. So, for instance, if a user would have a PATH that first has /opt/bin/, and /usr/bin/ as second entry, then /opt/bin/ should be searched first. This will probably happen only very, very rarely, so it does not invalidate your statement, but I wanted to mention it in regards to (possible) user's expectations.

If we continue looking up CC from PATH and align toolset only when CC comes from Xcode, the problem won't be mitigated for the above situation.

Right, but it should also pointed out that this appears to be an incorrect assumption by conda then, if it assumes that the bin/ it searches for must be in the same directory (e. g. assuming /usr/bin/). My clang binary, for instance, is under /home/Programs/Llvm/18.1.2/bin/, for instance. (I follow a symlink-version approach similar to GoboLinux, but I also only use /usr/bin/ and just update the current symlink in use, for all programs on my linux machine, just as info; obviously darwin follows another scheme.)

Copy link
Member

Choose a reason for hiding this comment

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

I think we should abort in that case, if not [test -x "${rb_prog}"], rather than searching from $PATH.

Copy link
Member Author

Choose a reason for hiding this comment

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

It makes sense to me. It sounds well consistent behavior.

Copy link
Member

Choose a reason for hiding this comment

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

I think rb_darwin_default_CC is not needed anymore.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, do you mean we should find CC from PATH instead of having the default value?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, only for Xcode clang, as commented above.

], [
# OpenBSD wants to prefer cc over gcc.
# See https://github.com/ruby/ruby/pull/2443
Expand All @@ -207,7 +225,31 @@ set rb_dummy ${CC}
rb_CC=$2
AC_DEFUN([RUBY_CHECK_PROG_FOR_CC], [
rb_prog=`echo "${rb_CC}" | sed "$2"`
AC_CHECK_PROG([$1], [$rb_prog], [$rb_prog])
rb_prog_optional="$3"
rb_prog_found=no
# If the tool is not explicitly specified and the compiler
# is absolute, check the one next to the compiler.
AS_IF([test -z "$$1"], [
AS_CASE(["${rb_prog}"],
[/*], [
AC_MSG_CHECKING([for ${rb_prog}])
AS_IF([test -x "${rb_prog}"], [
rb_prog_found=yes
$1=${rb_prog}
], [
# Abort if the tool is required and expected to be next
# to the compiler but not found.
AS_IF([test "x${rb_prog_optional}" != xyes], [
AC_MSG_ERROR("executable ${rb_prog} is not found")
])
])
AC_MSG_RESULT([$rb_prog_found])
], [
# Otherwise, try to find it in PATH with given program name
rb_prog=`echo "${rb_prog}" | sed 's,.*/,,'`
AC_CHECK_PROG([$1], [$rb_prog], [$rb_prog])
])
])
])
AS_CASE(["/${rb_CC} "],
[*@<:@\ /@:>@"cc "*], [
Expand Down Expand Up @@ -245,7 +287,7 @@ AS_CASE(["/${rb_CC} "],
# RUBY_CHECK_PROG_FOR_CC([AS], [s/clang/${llvm_prefix}as/])
RUBY_CHECK_PROG_FOR_CC([CXX], [s/clang/clang++/])
RUBY_CHECK_PROG_FOR_CC([NM], [s/clang/${llvm_prefix}nm/])
RUBY_CHECK_PROG_FOR_CC([OBJCOPY], [s/clang/${llvm_prefix}objcopy/])
RUBY_CHECK_PROG_FOR_CC([OBJCOPY], [s/clang/${llvm_prefix}objcopy/], [yes]) # optional
RUBY_CHECK_PROG_FOR_CC([OBJDUMP], [s/clang/${llvm_prefix}objdump/])
RUBY_CHECK_PROG_FOR_CC([RANLIB], [s/clang/${llvm_prefix}ranlib/])
RUBY_CHECK_PROG_FOR_CC([STRIP], [s/clang/${llvm_prefix}strip/])
Expand Down
Loading