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

Provide BAZEL_CURRENT_REPOSITORY local define in cc_* rules #16216

Closed
wants to merge 1 commit into from

Conversation

fmeum
Copy link
Collaborator

@fmeum fmeum commented Sep 4, 2022

BAZEL_CURRENT_REPOSITORY contains the canonical name of the repository
containing the current target and is required to look up runfiles using
apparent repository names when repository mappings are used, e.g. with
Bzlmod.

Work towards #16124

@fmeum fmeum changed the title Add BAZEL_CURRENT_REPOSITORY to runfiles library dependents Define BAZEL_CURRENT_REPOSITORY for runfiles library dependents Sep 4, 2022
@fmeum fmeum force-pushed the 16124-cc-rules branch 3 times, most recently from 4b2b45c to 98dc6a4 Compare September 4, 2022 18:11
@fmeum
Copy link
Collaborator Author

fmeum commented Sep 4, 2022

@oquenchil I went with the slightly less verbose BAZEL_CURRENT_REPOSITORY instead of BAZEL_CURRENT_REPOSITORY_NAME as suggested in the proposal. Which name do you like best?

Do you happen to have an idea where I need to add cc_runfiles_library.bzl to fix the bootstrap dist test on Windows? Fixed.

@fmeum fmeum marked this pull request as ready for review September 4, 2022 18:29
@sgowroji sgowroji added team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. awaiting-review PR is awaiting review from an assigned reviewer labels Sep 5, 2022
@fmeum fmeum marked this pull request as draft September 7, 2022 14:43
@fmeum
Copy link
Collaborator Author

fmeum commented Sep 7, 2022

Converted to Draft due to the possible changes to the proposal: bazelbuild/proposals#274

Copy link
Contributor

@oquenchil oquenchil left a comment

Choose a reason for hiding this comment

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

LGTM

@oquenchil
Copy link
Contributor

Converted to Draft due to the possible changes to the proposal: bazelbuild/proposals#274

Saw this too late. Let me know if I need to take another look.

@fmeum
Copy link
Collaborator Author

fmeum commented Sep 9, 2022

@oquenchil After a discussion with @lberki, we decided to ditch RunfilesLibraryInfo. That greatly simplifies the PR - please take another look. Do you see a problem with having additional local_defines for all cc_* targets?

@fmeum fmeum marked this pull request as ready for review September 9, 2022 11:07
@fmeum fmeum marked this pull request as draft September 9, 2022 11:08
@fmeum
Copy link
Collaborator Author

fmeum commented Sep 9, 2022

I will flesh out the test a bit more.

@oquenchil
Copy link
Contributor

For literally all cc_* targets? Do you mean adding the attribute to all cc_* rules and the targets that need it would use it?

@fmeum
Copy link
Collaborator Author

fmeum commented Sep 9, 2022

For literally all cc_* targets? Do you mean adding the attribute to all cc_* rules and the targets that need it would use it?

Yes, literally all of them. But by implicitly adding to the local_defines parameter of cc_common.compile rather than literally adding to the local_defines rule attribute.

@oquenchil
Copy link
Contributor

Ah then the latter is totally fine.

@fmeum fmeum force-pushed the 16124-cc-rules branch 2 times, most recently from 8a62946 to 70f189f Compare September 9, 2022 11:43
@fmeum
Copy link
Collaborator Author

fmeum commented Sep 9, 2022

@oquenchil I improved the test. Since it relies on running an external test, I temporarily stacked this on #16214. When that has been merged, I will rebase on master and mark as ready. But you can already review.

`BAZEL_CURRENT_REPOSITORY` contains the canonical name of the repository
containing the current target and is required to look up runfiles using
apparent repository names when repository mappings are used, e.g. with
Bzlmod.

Work towards bazelbuild#16124
@fmeum
Copy link
Collaborator Author

fmeum commented Sep 14, 2022

@oquenchil Test failures are fixed and this PR is no longer stacked.

@fmeum fmeum changed the title Define BAZEL_CURRENT_REPOSITORY for runfiles library dependents Provide BAZEL_CURRENT_REPOSITORY local define in cc_* rules Sep 15, 2022
@Wyverald
Copy link
Member

Looks good :) Is this ready for merge?

@fmeum
Copy link
Collaborator Author

fmeum commented Sep 15, 2022

Looks good :) Is this ready for merge?

Yes, it's ready.

@Wyverald Wyverald added awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally and removed awaiting-review PR is awaiting review from an assigned reviewer labels Sep 15, 2022
@sgowroji sgowroji removed the awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally label Sep 16, 2022
aiuto pushed a commit to aiuto/bazel that referenced this pull request Oct 12, 2022
`BAZEL_CURRENT_REPOSITORY` contains the canonical name of the repository
containing the current target and is required to look up runfiles using
apparent repository names when repository mappings are used, e.g. with
Bzlmod.

Work towards bazelbuild#16124

Closes bazelbuild#16216.

PiperOrigin-RevId: 474770711
Change-Id: Icfe179607abfb405b3a0bfb81fac18c21f744333
@BoleynSu
Copy link
Contributor

Wouldn't this violate ODR for some cc_libaray?

Suppose we have cc_library(hdr) that use this macro through runfiles API and it then is depended in two different repo?

To resolve this, we need to ask users to never use runfiles API in the header.

@BoleynSu
Copy link
Contributor

@fmeum for visibility

@fmeum
Copy link
Collaborator Author

fmeum commented Mar 12, 2023

I would have said no: As this is a local_define, it's only set when compiling the sources of the current cc_library and not exposed transitively.

@BoleynSu Could you provide an example of a situation you are worried about?

@BoleynSu
Copy link
Contributor

BoleynSu commented Mar 12, 2023 via email

@BoleynSu
Copy link
Contributor

BoleynSu commented Mar 12, 2023 via email

@BoleynSu
Copy link
Contributor

@fmeum friendly ping

@fmeum
Copy link
Collaborator Author

fmeum commented Mar 14, 2023

@BoleynSu I agree that that situation would be UB, but is there more to it than "exposing macros via inline functions is potentially dangerous"? Wouldn't the same apply to e.g. the __FILE__ macro?

@BoleynSu
Copy link
Contributor

BoleynSu commented Mar 14, 2023 via email

@fmeum fmeum deleted the 16124-cc-rules branch March 14, 2023 12:54
@fmeum
Copy link
Collaborator Author

fmeum commented Mar 14, 2023

True. Maybe we should prominently explain in the runfiles library documentation that BAZEL_CURRENT_REPOSITORY takes different values depending on the cc_library that includes it? Or do you see a way to improve the way the current repository name is passed in so that it would work in the body of an inline function?

@BoleynSu
Copy link
Contributor

The goal is more to implement a function that can return the current repo name.

Seems like it can be safely implemented with std::source_location (lets assume its avalible for now). Let me try if I could make it work first.

@BoleynSu
Copy link
Contributor

BoleynSu commented Mar 14, 2023

std::string_view current_repo_name(std::string_view file=__builtin_FILE()) {
   if (file.startswith("external/")) return x; // where file = external/x/whatever
   else return REPO_NAME; // where REPO_NAME is the name of the main repo. Thus, the macro will be the same across different repos.
}

We could check if __builtin_FILE exists, if not we could fall back to other solutions.

@fmeum
Copy link
Collaborator Author

fmeum commented Mar 15, 2023

Something like that looks pretty good as a modern C++ replacement for what we are doing here. If it requires C++20, we probably have to hide it behind std level checks.

@BoleynSu Since you seem to have this mostly figured out, would you be willing to submit a PR adding this to https://github.com/bazelbuild/bazel/blob/master/tools/cpp/runfiles/runfiles_src.h?

@BoleynSu
Copy link
Contributor

Sure. Will do.

@BoleynSu
Copy link
Contributor

@fmeum Please check #17887

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants