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

don't inject -Wl,-rpath options when '-x c++-header' compiler option is used #3424

Merged
merged 2 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
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
30 changes: 19 additions & 11 deletions easybuild/scripts/rpath_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
rpath_include = sys.argv[3]
args = sys.argv[4:]

# wheter or not to use -Wl to pass options to the linker
# determine whether or not to use -Wl to pass options to the linker based on name of command
if cmd in ['ld', 'ld.gold', 'ld.bfd']:
flag_prefix = ''
else:
Expand All @@ -58,7 +58,7 @@
else:
rpath_include = []

version_mode = False
add_rpath_args = True
cmd_args, cmd_args_rpath = [], []

# process list of original command line arguments
Expand All @@ -69,7 +69,16 @@

# if command is run in 'version check' mode, make sure we don't include *any* -rpath arguments
if arg in ['-v', '-V', '--version', '-dumpversion']:
version_mode = True
add_rpath_args = False
cmd_args.append(arg)

# compiler options like "-x c++header" imply no linking is done (similar to -c),
# so then we must not inject -Wl,-rpath option since they *enable* linking;
# see https://github.com/easybuilders/easybuild-framework/issues/3371
elif arg == '-x':
idx_next = idx + 1
if idx_next < len(args) and args[idx_next] in ['c-header', 'c++-header']:
add_rpath_args = False
cmd_args.append(arg)

# FIXME: also consider $LIBRARY_PATH?
Expand Down Expand Up @@ -110,16 +119,15 @@

idx += 1

# add -rpath flags in front
cmd_args = cmd_args_rpath + cmd_args
if add_rpath_args:
# try to make sure that RUNPATH is not used by always injecting --disable-new-dtags
cmd_args_rpath.insert(0, flag_prefix + '--disable-new-dtags')

cmd_args_rpath = [flag_prefix + '-rpath=%s' % inc for inc in rpath_include]
# add -rpath options for paths listed in rpath_include
cmd_args_rpath = [flag_prefix + '-rpath=%s' % inc for inc in rpath_include] + cmd_args_rpath

if not version_mode:
cmd_args = cmd_args_rpath + [
# try to make sure that RUNPATH is not used by always injecting --disable-new-dtags
flag_prefix + '--disable-new-dtags',
] + cmd_args
# add -rpath flags in front
cmd_args = cmd_args_rpath + cmd_args

# wrap all arguments into single quotes to avoid further bash expansion
cmd_args = ["'%s'" % a.replace("'", "''") for a in cmd_args]
Expand Down
24 changes: 20 additions & 4 deletions test/framework/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1925,10 +1925,26 @@ def test_rpath_args_script(self):
self.assertEqual(out.strip(), "CMD_ARGS=(%s)" % ' '.join(cmd_args))

# verify that no -rpath arguments are injected when command is run in 'version check' mode
cmd = "%s g++ '' '%s' -v" % (script, rpath_inc)
out, ec = run_cmd(cmd, simple=False)
self.assertEqual(ec, 0)
self.assertEqual(out.strip(), "CMD_ARGS=('-v')")
for extra_args in ["-v", "-V", "--version", "-dumpversion", "-v -L/test/lib"]:
cmd = "%s g++ '' '%s' %s" % (script, rpath_inc, extra_args)
out, ec = run_cmd(cmd, simple=False)
self.assertEqual(ec, 0)
self.assertEqual(out.strip(), "CMD_ARGS=(%s)" % ' '.join(["'%s'" % x for x in extra_args.split(' ')]))

# if a compiler command includes "-x c++-header" or "-x c-header" (which imply no linking is done),
# we should *not* inject -Wl,-rpath options, since those enable linking as a side-effect;
# see https://github.com/easybuilders/easybuild-framework/issues/3371
test_cases = [
"-x c++-header",
"-x c-header",
"-L/test/lib -x c++-header",
]
for extra_args in test_cases:
cmd = "%s g++ '' '%s' foo.c -O2 %s" % (script, rpath_inc, extra_args)
out, ec = run_cmd(cmd, simple=False)
self.assertEqual(ec, 0)
cmd_args = ["'foo.c'", "'-O2'"] + ["'%s'" % x for x in extra_args.split(' ')]
self.assertEqual(out.strip(), "CMD_ARGS=(%s)" % ' '.join(cmd_args))

def test_toolchain_prepare_rpath(self):
"""Test toolchain.prepare under --rpath"""
Expand Down