Debian Codegraph @

cmake/tribits/core/utils/TribitsFilepathHelpers.cmake

raw
1 include(MessageWrapper)
2 include(Split)
3
4
5 # @FUNCTION: tribits_dir_is_basedir()
6 #
7 # Function to determine if a given path is a base dir of another path.
8 #
9 # Usage::
10 #
11 # tribits_dir_is_basedir(<absBaseDir> <absFullDir> <isBaseDirVarOut>)
12 #
13 # If the absolute path ``<absBaseDir>`` is a subdir of the absolute path
14 # ``<absFullDir>``, then the variable ``<isBaseDirVarOut>`` is set to
15 # ``TRUE``. Otherwise, ``<isBaseDirVarOut>`` is set to ``FALSE``.
16 #
17 # For example, the output var ``isBaseDir`` would be set to ``TRUE`` in the
18 # following examples::
19 #
20 # tribits_dir_is_basedir(/some/base/path /some/base/path/more isBaseDir)
21 #
22 # tribits_dir_is_basedir(/some/base/path /some/base/path isBaseDir)
23 #
24 # However, in the following examples, ``isBaseDir`` would be set to ``FALSE``::
25 #
26 # tribits_dir_is_basedir(/some/base/path/more /some/base/path isBaseDir)
27 #
28 # tribits_dir_is_basedir(/some/base/path /some/other/path isBaseDir)
29 #
30 function(tribits_dir_is_basedir absBaseDir absFullDir isBaseDirVarOut)
31
32 # Assume not base dir by default unless we find it is
33 set(isBaseDir FALSE)
34
35 string(LENGTH "${absBaseDir}" absBaseDirLen)
36 string(LENGTH "${absFullDir}" absFullDirLen)
37
38 if (absBaseDir STREQUAL absFullDir)
39 set(isBaseDir TRUE)
40 elseif (NOT absBaseDirLen GREATER absFullDirLen)
41 string(FIND "${absFullDir}" "${absBaseDir}/" baseDirIdx)
42 if (baseDirIdx EQUAL 0)
43 set(isBaseDir TRUE)
44 endif()
45 endif()
46
47 set(${isBaseDirVarOut} ${isBaseDir} PARENT_SCOPE)
48
49 endfunction()
50
51
52 # @FUNCTION: tribits_get_dir_array_below_base_dir()
53 #
54 # Returns the array of directories below a base directory.
55 #
56 # Usage::
57 #
58 # tribits_get_dir_array_below_base_dir(<absBaseDir> <absFullDir>
59 # <trailingDirArrayVarOut>)
60 #
61 # The following show examples of what this returns:
62 #
63 # tribits_get_dir_array_below_base_dir("/a/b/c" "/a/b/c", dirArray)
64 # => dirArray = ""
65 #
66 # tribits_get_dir_array_below_base_dir("/a/b/c" "/a/b/c/d", dirArray)
67 # => dirArray = "d"
68 #
69 # tribits_get_dir_array_below_base_dir("/a/b/c" "/a/b/c/d/e", dirArray)
70 # => dirArray = "d;e"
71 #
72 function(tribits_get_dir_array_below_base_dir absBaseDir absFullDir
73 trailingDirArrayVarOut
74 )
75
76 tribits_dir_is_basedir("${absBaseDir}" "${absFullDir}" isBaseDir)
77 if (NOT isBaseDir)
78 message_wrapper(FATAL_ERROR
79 "ERROR: '${absBaseDir}' is not a base dir of '${absFullDir}'")
80 endif()
81
82 string(LENGTH "${absBaseDir}" absBaseDirLen)
83 string(LENGTH "${absFullDir}" absFullDirLen)
84
85 if (absBaseDirLen EQUAL absFullDirLen)
86 set(trailingDirArray "")
87 else()
88 math(EXPR trailingDirsStrStartIdx "${absBaseDirLen}+1")
89 string(SUBSTRING "${absFullDir}" ${trailingDirsStrStartIdx} -1 trailingDirsStr)
90 split("${trailingDirsStr}" "/" trailingDirArray)
91 endif()
92
93 set(${trailingDirArrayVarOut} "${trailingDirArray}" PARENT_SCOPE)
94
95 endfunction()