-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
Export LLVM_VERSION_MAJOR
CMake variable as a directory property
#83346
Conversation
44dd7c1
to
6848a19
Compare
Just tagged 3 tentative reviewers... feel free to reassign! |
6848a19
to
f188a23
Compare
LLVM_VERSION_*
CMake variables to PARENT_SCOPE
LLVM_VERSION_MAJOR
CMake variable to PARENT_SCOPE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally the recommended guidance has been to either include LLVM as an ExternalProject or to import the CMake package. I mention this purely as a caution. I don't have any philosophical objection to fixing the issues related to including LLVM as a subdirectory, and generally speaking those issues are bugs.
I have two concrete suggestions below to make the code easier to read and more robust for users.
llvm/CMakeLists.txt
Outdated
# Export a few LLVM version identifiers for users who use LLVM as a subdir. | ||
get_directory_property(_LLVM_HAS_PARENT_DIRECTORY PARENT_DIRECTORY) | ||
if (_LLVM_HAS_PARENT_DIRECTORY) | ||
set(LLVM_VERSION_MAJOR "${LLVM_VERSION_MAJOR}" PARENT_SCOPE) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be more idiomatic in CMake to set those values as directory or global properties, which would allow them to be accessed at any directory scope above using the get_property
function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: If you set these as properties instead of variables, I don't think there is any need to only set them conditionally, they can just always be set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for teaching me better CMake! Now this PR is down to 1 line with no control flow and I have verified that my downstream project is able to read the variable like this,
get_directory_property(LLVM_VERSION_MAJOR DIRECTORY "third_party/llvm-project/llvm" LLVM_VERSION_MAJOR)
f188a23
to
502e3de
Compare
LLVM_VERSION_MAJOR
CMake variable to PARENT_SCOPE
LLVM_VERSION_MAJOR
CMake variable as a directory property
Thanks, I didn't know about https://cmake.org/cmake/help/latest/module/ExternalProject.html . TIL. FYI @ScottTodd @stellaraccident . |
Neither of those are particularly attractive options when depending on many targets in the subproject (as we do with MLIR targets). If we were just using tools like lld from LLVM as black boxes and were rarely interacting with individual libraries, header files, etc. then they might work. What's sort of tricky in our (IREE's) use of LLVM/MLIR is that we use both the individual libraries in MLIR and the linking and code generation from LLVM -- and in both opinionated and general/flexible ways. |
Yep. In the past the guidance has been for projects like that to nest as LLVM subprojects. I understand reasons why that is undesirable and welcome any contributions to make your preferred workflow better supported. I just wanted to make sure you were aware that the way you're doing things is historically fraught with peril. |
This supersedes #16606 as the way that we stop relying on `CLANG_EXECUTABLE_VERSION` which, as a cached variable, is bound to eventually give an incorrect value and cause builds to fail. llvm/llvm-project#83346 was merged so we can now query `LLVM_VERSION_MAJOR` as a directory property on the `llvm/` directory.
This allows users who include
llvm-project
as a subrepository to access theLLVM_VERSION_MAJOR
variable on par with if they were relying on an installed LLVM andFindLLVM.cmake
. They just need to do something like:Context: iree-org/iree#16606 -- like other projects with similar needs that I found by some googling, our work-around had been to rely on the CMake cached variable
CLANG_EXECUTABLE_VERSION
. Being cached, it over time inevitably ended up having a wrong value.