Skip to content

Commit

Permalink
Resolve more cases of 'C compiler cannot compile executables' (#26167)
Browse files Browse the repository at this point in the history
Sometimes when building Chapel the build system will report something
like 'C compiler cannot compile executable'. This can indicate the users
system is missing some dependencies, or it can mean that extra arguments
are needed to work. This PR improves the ability of chplenv to provide
those arguments when needed.

Specifically this PR infers when `--sysroot` or `-Wl,-dynamic-linker`
need to be passed to clang on linux systems.

Testing
- [x] tested that MacOS is unaffected
- [x] tested on various Linux systems that the output is correct (4
systems: two different linux clusters, hpe-cray-ex, and hpe-apollo)
- [x] tested that this patch resolves the issues on the original system
that motivated this

[Reviewed by @mppf]
  • Loading branch information
jabraham17 authored Oct 30, 2024
2 parents cd1ddf6 + 5492986 commit 46c2561
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions util/chplenv/chpl_llvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,9 +742,8 @@ def get_gcc_install_dir():
@memoize
def get_sysroot_resource_dir_args():
args = [ ]
target_platform = chpl_platform.get('target')
llvm_val = get()
if target_platform == "darwin" and llvm_val == "bundled":
if llvm_val == "bundled":
# Add -isysroot and -resourcedir based upon what 'clang' uses
cfile = os.path.join(get_chpl_home(),
"runtime", "include", "sys_basic.h")
Expand All @@ -767,6 +766,42 @@ def get_sysroot_resource_dir_args():

return args

# On some systems, it may be necessary to provide a --sysroot and/or a explicit dynamic linker
@memoize
def get_sysroot_linux_args():
args = [ ]
# try invoking the system gcc to see if it needs various extra flags
dummy_main = '#include <stdio.h>\nint main() { return 0; }\n'
_, _, stdout, _ = try_run_command(
["gcc", "-v", "-x", "c", "-", "-o", "/dev/null"],
combine_output=True, cmd_input=dummy_main
)
if stdout:
# on some platforms, it may be necessary to provide a --sysroot.
# note: this regex does not handle paths with spaces
found = re.search(r'--(?:with-)sysroot(?:=|\s+)([^ ]+)', stdout)
if found:
args.append('--sysroot')
args.append(found.group(1).strip())

# on some platforms, it may be necessary to provide -dynamic-linker explicitly
# note: this regex does not handle paths with spaces
found = re.search(r'-dynamic-linker(?:=|\s+)([^ ]+)', stdout)
if found:
dyn_linker = found.group(1).strip()
# if this is path starts with '/lib/' or '/lib64/' then its the
# default and we don't need to override it
if not (
dyn_linker.startswith('/lib/') or dyn_linker.startswith('/lib64/')
):
# this linker flags needs some tricks to be passed to the compiler
# wrap in '--[start|end]-no-unused-arguments' to prevent compiler warnings
# pass as '-Wl,-dynamic-linker,<path>'
args.append('--start-no-unused-arguments')
args.append('-Wl,-dynamic-linker,' + dyn_linker)
args.append('--end-no-unused-arguments')
return args

# When a system LLVM is installed with Homebrew, it's very important
# to use the same Mac OS X libraries as what the Homebrew LLVM used.
# This function helps us to do that.
Expand Down Expand Up @@ -830,7 +865,12 @@ def get_clang_basic_args():
if gcc_prefix:
clang_args.append('--gcc-toolchain=' + gcc_prefix)

sysroot_args = get_sysroot_resource_dir_args()
target_platform = chpl_platform.get('target')
sysroot_args = []
if target_platform == "darwin":
sysroot_args = get_sysroot_resource_dir_args()
else:
sysroot_args = get_sysroot_linux_args()
if sysroot_args:
clang_args.extend(sysroot_args)

Expand Down

0 comments on commit 46c2561

Please sign in to comment.