This is an archive of the discontinued LLVM Phabricator instance.

[CRT][LIT] split target_cflags using shlex
ClosedPublic

Authored by jsji on Aug 18 2021, 2:01 PM.

Details

Reviewers
phosek
MaskRay
Group Reviewers
Restricted Project
Commits
rG0541ce4ef9ca: [CRT][LIT] build the target_cflags for Popen properly
Summary

We recently enabled crt for powerpc in
https://reviews.llvm.org/rGb7611ad0b16769d3bf172e84fa9296158f8f1910.

And we started to see some unexpected error message when running
check-runtimes.

eg:
https://lab.llvm.org/buildbot/#/builders/57/builds/9488/steps/6/logs/stdio
line 100 - 103:

"
clang-14: error: unknown argument: '-m64 -fno-function-sections'
clang-14: error: unknown argument: '-m64 -fno-function-sections'
clang-14: error: unknown argument: '-m64 -fno-function-sections'
clang-14: error: unknown argument: '-m64 -fno-function-sections'
"

Looks like we shouldn't strip the space at the beginning,
or else the command line passed to subprocess won't work well.

Diff Detail

Event Timeline

jsji created this revision.Aug 18 2021, 2:01 PM
jsji requested review of this revision.Aug 18 2021, 2:01 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 18 2021, 2:01 PM
Herald added a subscriber: Restricted Project. · View Herald Transcript
MaskRay added inline comments.Aug 18 2021, 2:32 PM
compiler-rt/test/crt/lit.cfg.py
25

Thank you for working on this.

Do you know what are config.clang and config.target_cflags?

rstrip() works while strip() doesn't looks weird.

jsji added inline comments.Aug 18 2021, 2:37 PM
compiler-rt/test/crt/lit.cfg.py
25

The cflags are passing down from compiler.

compiler-rt/test/crt/lit.site.cfg.py.in:config.target_cflags = "@CRT_TEST_TARGET_CFLAGS@"

ompiler-rt/test/crt/CMakeLists.txt:    get_test_cc_for_arch(${arch} CRT_TEST_TARGET_CC CRT_TEST_TARGET_CFLAGS)
jsji added inline comments.Aug 18 2021, 2:46 PM
compiler-rt/test/crt/lit.cfg.py
25

the target_cflags itself is OK.

Actually , you should be able to reproduce the same problem with following:

$ cat t.py
import subprocess

target_cflags= " -m64 -v " 
 
cmd = subprocess.Popen(['clang',
                            target_cflags.strip(),
                            '-print-file-name=t'],
                           stdout=subprocess.PIPE,
                           universal_newlines=True)
$ python t.py
$ clang-14: error: unknown argument: '-m64 -v'
MaskRay added inline comments.Aug 18 2021, 3:01 PM
compiler-rt/test/crt/lit.cfg.py
25

The code appears to make use of a fragile Popen behavior.

The proper fix should be import shlex; ... ['clang', '-print-file-name=t'] + shlex.split(target_cflags)

25

Perhaps even better to make sure target_cflags is a list in the first place. I haven't investigated further.

jsji updated this revision to Diff 367383.Aug 18 2021, 7:00 PM

Address comments.

jsji retitled this revision from [CRT][LIT] don't strip leading space for target_cflags to [CRT][LIT] split target_cflags using shlex.Aug 18 2021, 7:00 PM
jsji added inline comments.
compiler-rt/test/crt/lit.cfg.py
25

You are right. While it works with previous patch, it is better to use shlex for better readability and maintenance.

MaskRay accepted this revision.Aug 18 2021, 7:39 PM

Thanks!

This revision is now accepted and ready to land.Aug 18 2021, 7:39 PM

It isn't easy to make config.target_cflags a list, so shlex is good.

phosek accepted this revision.Aug 18 2021, 11:14 PM

LGTM

Please make sure to test on Windows (or check if some windows buildbotbis
running those tests and check it out). Shlex is, IIRC, using UNIX rules to
do the split.

Thank you,
Filipe

jsji added a comment.Aug 19 2021, 7:16 AM

Please make sure to test on Windows (or check if some windows buildbotbis
running those tests and check it out). Shlex is, IIRC, using UNIX rules to
do the split.

Thank you,
Filipe

Good point Filipe! Unfortunately, there are no Windows buildbot running check-runtimes as far as I can tell.

jsji updated this revision to Diff 367493.Aug 19 2021, 7:38 AM

Avoid using shlex due to win32.

jsji added a comment.Aug 19 2021, 7:40 AM

Switch to the way as we used in build_invocation, let me know if you have better solution. Thanks.

Looks ok, I’ll check it out when I can and submit a patch if needed.

Thanks!
Filipe

This revision was automatically updated to reflect the committed changes.

shlex works perfectly fine on Windows.

Please make sure to test on Windows (or check if some windows buildbotbis
running those tests and check it out). Shlex is, IIRC, using UNIX rules to
do the split.

Thank you,
Filipe

shlex.split works perfectly fine. We don't do crazy space and double quotes on Windows command lines. (https://stackoverflow.com/questions/33560364/python-windows-parsing-command-lines-with-shlex)

The committed version [config.target_cflags] is inferior IMO

jsji added a comment.EditedAug 19 2021, 1:33 PM

shlex.split works perfectly fine. We don't do crazy space and double quotes on Windows command lines. (https://stackoverflow.com/questions/33560364/python-windows-parsing-command-lines-with-shlex)

The committed version [config.target_cflags] is inferior IMO

:), since shlex.split is using posix and the cflags are actually passing down from user input.
So there is still some chance that user use space in path or something? eg: -isystem C:\\.... Program Files \...

Anyway, I can do a following commit with something like following if it is OK for everyone.

-                           [config.target_cflags],
+                           shlex.split(config.target_cflags, posix='win' not in sys.platform),

It seems that the posix argument doesn't mean POSIX/Windows distinction. Using shlex.split only for *NIX platforms seems fine.

I suspect if sys.platform in ['win32'] is likely irrelevant here because we have

if config.host_os not in ['Linux']:
    config.unsupported = True
jsji added a comment.Aug 19 2021, 6:47 PM
if config.host_os not in ['Linux']:
    config.unsupported = True

Good point. I committed the version using shlex.split in 337bd67d836b7cb7a18fb08df8f3a7902338c7fa.