1# Find the prefix from the `*Config.cmake` file being generated.
2#
3# When generating an installed `*Config.cmake` file, we often want to be able
4# to refer to the ancestor directory which contains all the installed files.
5#
6# We want to do this without baking in an absolute path when the config file is
7# generated, in order to allow for a "relocatable" binary distribution that
8# doesn't need to know what path it ends up being installed at when it is
9# built.
10#
11# The solution that we know the relative path that the config file will be at
12# within that prefix, like `"${prefix_var}/lib/cmake/${project}"`, so we count
13# the number of components in that path to figure out how many parent dirs we
14# need to traverse from the location of the config file to get to the prefix
15# dir.
16#
17# out_var:
18#   variable to set the "return value" of the function, which is the code to
19#   include in the config file under construction.
20#
21# prefix_var:
22#   Name of the variable to define in the returned code (not directory for the
23#   faller!) that will contain the prefix path.
24#
25# path_to_leave:
26#   Path from the prefix to the config file, a relative path which we wish to
27#   go up and out from to find the prefix directory.
28function(find_prefix_from_config out_var prefix_var path_to_leave)
29  set(config_code
30    "# Compute the installation prefix from this LLVMConfig.cmake file location."
31    "get_filename_component(${prefix_var} \"\${CMAKE_CURRENT_LIST_FILE}\" PATH)")
32  # Construct the proper number of get_filename_component(... PATH)
33  # calls to compute the installation prefix.
34  string(REGEX REPLACE "/" ";" _count "${path_to_leave}")
35  foreach(p ${_count})
36    list(APPEND config_code
37      "get_filename_component(${prefix_var} \"\${${prefix_var}}\" PATH)")
38  endforeach(p)
39  string(REPLACE ";" "\n" config_code "${config_code}")
40  set("${out_var}" "${config_code}" PARENT_SCOPE)
41endfunction()
42