summaryrefslogtreecommitdiff
path: root/cmake
diff options
context:
space:
mode:
authorJohn Ericson <John.Ericson@Obsidian.Systems>2022-01-03 02:25:06 +0000
committerJohn Ericson <John.Ericson@Obsidian.Systems>2022-01-07 20:16:18 +0000
commit44e3365775101fec3fd355eda339282258d74415 (patch)
treeab8ebea8266a13bcb3115c3ef0ffcd4405de23e5 /cmake
parentb99833dc745758042f230add35c75faf4070529f (diff)
downloadllvm-44e3365775101fec3fd355eda339282258d74415.tar.gz
[CMake] Factor out config prefix finding logic
See the docs in the new function for details. I think I found every instance of this copy pasted code. Polly could also use it, but currently does something different, so I will save the behavior change for a future revision. We get the shared, non-installed CMake modules following the pattern established in D116472. It might be good to have LLD and Flang also use this, but that would be a functional change and so I leave it as future work. Reviewed By: beanz, lebedev.ri Differential Revision: https://reviews.llvm.org/D116521
Diffstat (limited to 'cmake')
-rw-r--r--cmake/Modules/FindPrefixFromConfig.cmake41
1 files changed, 41 insertions, 0 deletions
diff --git a/cmake/Modules/FindPrefixFromConfig.cmake b/cmake/Modules/FindPrefixFromConfig.cmake
new file mode 100644
index 000000000000..aa9fb0d03413
--- /dev/null
+++ b/cmake/Modules/FindPrefixFromConfig.cmake
@@ -0,0 +1,41 @@
+# Find the prefix from the `*Config.cmake` file being generated.
+#
+# When generating an installed `*Config.cmake` file, we often want to be able
+# to refer to the ancestor directory which contains all the installed files.
+#
+# We want to do this without baking in an absolute path when the config file is
+# generated, in order to allow for a "relocatable" binary distribution that
+# doesn't need to know what path it ends up being installed at when it is
+# built.
+#
+# The solution that we know the relative path that the config file will be at
+# within that prefix, like `"${prefix_var}/lib/cmake/${project}"`, so we count
+# the number of components in that path to figure out how many parent dirs we
+# need to traverse from the location of the config file to get to the prefix
+# dir.
+#
+# out_var:
+# variable to set the "return value" of the function, which is the code to
+# include in the config file under construction.
+#
+# prefix_var:
+# Name of the variable to define in the returned code (not directory for the
+# faller!) that will contain the prefix path.
+#
+# path_to_leave:
+# Path from the prefix to the config file, a relative path which we wish to
+# go up and out from to find the prefix directory.
+function(find_prefix_from_config out_var prefix_var path_to_leave)
+ set(config_code
+ "# Compute the installation prefix from this LLVMConfig.cmake file location."
+ "get_filename_component(${prefix_var} \"\${CMAKE_CURRENT_LIST_FILE}\" PATH)")
+ # Construct the proper number of get_filename_component(... PATH)
+ # calls to compute the installation prefix.
+ string(REGEX REPLACE "/" ";" _count "${path_to_leave}")
+ foreach(p ${_count})
+ list(APPEND config_code
+ "get_filename_component(${prefix_var} \"\${${prefix_var}}\" PATH)")
+ endforeach(p)
+ string(REPLACE ";" "\n" config_code "${config_code}")
+ set("${out_var}" "${config_code}" PARENT_SCOPE)
+endfunction()