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

shims/super/cc: handle double dash in args #19082

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alebcay
Copy link
Member

@alebcay alebcay commented Jan 12, 2025

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes? Here's an example.
  • Have you successfully run brew style with your changes locally?
  • Have you successfully run brew typecheck with your changes locally?
  • Have you successfully run brew tests with your changes locally?

The cc shim currently does not handle invocations where -- is passed as an argument, which (conventionally) indicates that all following arguments be treated as positional (non-optional) arguments. As a result, any refurbished args may get appended after the -- where they are erroneously treated as input file paths:

clang: error: no such file or directory: '-isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk'
clang: error: no such file or directory: '--sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk'; did you mean '--sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk'?
clang: error: no such file or directory: '-isystem/opt/homebrew/include'
clang: error: no such file or directory: '-isystem/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk/System/Library/Frameworks/OpenGL.framework/Versions/Current/Headers'
clang: error: no such file or directory: '-L/opt/homebrew/lib'
clang: error: no such file or directory: '-L/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk/System/Library/Frameworks/OpenGL.framework/Versions/Current/Libraries'
clang: error: no such file or directory: '-Wl,-headerpad_max_install_names'

This kind of error was encountered in the wild in some core PRs, e.g. Homebrew/homebrew-core#203929, Homebrew/homebrew-core#203934, Homebrew/homebrew-core#203932, Homebrew/homebrew-core#203944.

This pull request ensures that any modifications to the arguments are applied before the -- (if -- is present).

Before:

clang called with: -- test.c
superenv added:    -isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk --sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk -isystem/opt/homebrew/include -isystem/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk/System/Library/Frameworks/OpenGL.framework/Versions/Current/Headers -L/opt/homebrew/lib -L/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk/System/Library/Frameworks/OpenGL.framework/Versions/Current/Libraries -Wl,-headerpad_max_install_names
superenv executed: clang -- test.c -isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk --sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk -isystem/opt/homebrew/include -isystem/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk/System/Library/Frameworks/OpenGL.framework/Versions/Current/Headers -L/opt/homebrew/lib -L/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk/System/Library/Frameworks/OpenGL.framework/Versions/Current/Libraries -Wl,-headerpad_max_install_names

After:

clang called with: -- test.c
superenv added:    -isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk --sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk -isystem/opt/homebrew/include -isystem/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk/System/Library/Frameworks/OpenGL.framework/Versions/Current/Headers -L/opt/homebrew/lib -L/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk/System/Library/Frameworks/OpenGL.framework/Versions/Current/Libraries -Wl,-headerpad_max_install_names
superenv executed: clang -isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk --sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk -isystem/opt/homebrew/include -isystem/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk/System/Library/Frameworks/OpenGL.framework/Versions/Current/Headers -L/opt/homebrew/lib -L/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk/System/Library/Frameworks/OpenGL.framework/Versions/Current/Libraries -Wl,-headerpad_max_install_names -- test.c

Copy link
Member

@carlocab carlocab left a comment

Choose a reason for hiding this comment

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

Thanks; nice work!

def split_args(args)
double_dash_index = args.find_index("--")
if double_dash_index
[args[0...double_dash_index], args[double_dash_index..-1]]
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 the start/end indices can be omitted:

Suggested change
[args[0...double_dash_index], args[double_dash_index..-1]]
[args[...double_dash_index], args[double_dash_index..]]

or, at the very least:

Suggested change
[args[0...double_dash_index], args[double_dash_index..-1]]
[args[0...double_dash_index], args[double_dash_index..]]

Comment on lines 144 to 159
case mode
when :ccld
cflags + args + cppflags + ldflags
cflags + args + cppflags + ldflags + @positional_args
when :cxxld
cxxflags + args + cppflags + ldflags
cxxflags + args + cppflags + ldflags + @positional_args
when :cc
cflags + args + cppflags
cflags + args + cppflags + @positional_args
when :cxx
cxxflags + args + cppflags
cxxflags + args + cppflags + @positional_args
when :ccE
args + cppflags
args + cppflags + @positional_args
when :cpp
args + cppflags
args + cppflags + @positional_args
when :ld
ldflags + args
ldflags + args + @positional_args
end
Copy link
Member

Choose a reason for hiding this comment

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

Would be nice to deduplicate all these ... + @positional_args.

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.

2 participants