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

fix(clion): Add Xcode information to Cpp compiler wrapper #4651

Merged
merged 3 commits into from
May 19, 2023

Conversation

blorente
Copy link
Collaborator

Checklist

  • I have filed an issue about this change and discussed potential changes with the maintainers.
  • I have received the approval from the maintainers to make this change.
  • This is not a stylistic, refactoring, or cleanup change.

Please note that the maintainers will not be reviewing this change until all checkboxes are ticked. See
the Contributions section in the README for more
details.

Discussion thread for this change

Issue number: N/A (sorry, I didn't open one).

Description of this change

In CLion, the Bazel plugin has to pass a C compiler to the rest of the CLion machinery.
Currently, on macOS, if using the standard cc toolchains, it does so by wrapping the clang_wrapper.sh we get from each toolchain implementation. On macOS, that wrapper expects the environment variables DEVELOPER_DIR and SDKROOT to be set. In Bazel, they are set on every action. Currently, in CLion, they are not, which makes a lot of the smarts of CLion inaccessible to macOS users.

This PR sets those variables by mirroring Bazel's logic:

  • When resolving Compiler Settings, cquery for the xcode version in @bazel_tools//tools/osx:current_xcode_config.
  • Then, use the xcode locator that Bazel uses to translate the DEVELOPER_DIR that corresponds with that version. (This is where Bazel does it).
  • The rest is just piping those values so that they end up as environment variables in the compiler wrapper.

Note that we could have gotten the xcode version from the aspect, and saved ourselves a potentially expensive cquery. But, due to this issue, we cannot forward those values to the rest of the build.

@blorente blorente added product: CLion CLion plugin lang: c++ C++ rules integration awaiting-review Awaiting review from Bazel team on PRs labels Mar 29, 2023
@tpasternak tpasternak added the P1 I'll work on this now. (Assignee required) label Apr 14, 2023
@raoarun
Copy link
Collaborator

raoarun commented Apr 20, 2023

ECD 27 April 2023 to be merged as per @tpasternak

Copy link
Contributor

@tpasternak tpasternak left a comment

Choose a reason for hiding this comment

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

Thank you, good job!

The only major issue I see here is that we ignore stderr output in both query calls, which can lead to some hard-to-spot silent crashes, so personally I'd prefer to pipe stderr to the Bazel tool window

}

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errStream = new ByteArrayOutputStream();
Copy link
Contributor

Choose a reason for hiding this comment

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

We lose error stream here if the query result is 0. I think this is the reason for which it was possible to skip != None check in QUERY_XCODE_VERSION_STARLARK_EXPR - the error message was silenced

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Great point! Piped it through the context.

cpp/src/META-INF/blaze-cpp.xml Outdated Show resolved Hide resolved
@blorente
Copy link
Collaborator Author

@tpasternak If CI passes, I think this is ready for another review.

TargetMapBuilder.builder()
.addTarget(createCcToolchain())
.build();
XcodeCompilerSettings expected = new XcodeCompilerSettings(Path.of("/tmp/dev_dir"), Path.of("/tmp/dev_dir/sdk"));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
XcodeCompilerSettings expected = new XcodeCompilerSettings(Path.of("/tmp/dev_dir"), Path.of("/tmp/dev_dir/sdk"));
XcodeCompilerSettings expected = XcodeCompilerSettings.create(Path.of("/tmp/dev_dir"), Path.of("/tmp/dev_dir/sdk"));

Otherwise, it won't compile as XcodeCompilerSettings is now abstract

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks! After a small fire at work I finally got time to make the tests pass.
There was this thing and another small thing related to turning it into a project service, then it's all done :D

@tpasternak tpasternak merged commit 3c68f0b into bazelbuild:master May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-review Awaiting review from Bazel team on PRs lang: c++ C++ rules integration P1 I'll work on this now. (Assignee required) product: CLion CLion plugin
Projects
3 participants