|
| 1 | +#=============================================================================# |
| 2 | +# Finds the shallowest path among the given sources, where shallowest is the path having |
| 3 | +# the least nesting level, i.e. The least number of '/' separators in its' path. |
| 4 | +# _sources - List of sources paths to find shallowest path from. |
| 5 | +# _return_var - Name of variable in parent-scope holding the return value. |
| 6 | +# Returns - Shallowest path among given sources (Lowest nesting level). |
| 7 | +#=============================================================================# |
| 8 | +function(get_shallowest_directory_structure_path _sources _return_var) |
| 9 | + |
| 10 | + set(min_nesting_level 9999) |
| 11 | + |
| 12 | + foreach (source ${_sources}) |
| 13 | + |
| 14 | + string(REGEX MATCHALL "/" nesting_regex_match ${source}) |
| 15 | + |
| 16 | + list(LENGTH nesting_regex_match source_nesting_level) |
| 17 | + |
| 18 | + if (${source_nesting_level} LESS ${min_nesting_level}) |
| 19 | + set(min_nested_path ${source}) |
| 20 | + set(min_nesting_level ${source_nesting_level}) |
| 21 | + endif () |
| 22 | + |
| 23 | + endforeach () |
| 24 | + |
| 25 | + set(${_return_var} ${min_nested_path} PARENT_SCOPE) |
| 26 | + |
| 27 | +endfunction() |
| 28 | + |
| 29 | +#=============================================================================# |
| 30 | +# Gets the path of the common root directory of all given sources. |
| 31 | +# It is expected that indeed all sources will have the same, common root directory. |
| 32 | +# E.g. src/foo.h and src/utility/bar.h both have 'src' in common. |
| 33 | +# However, if src/foo.c is a relative path under the C:\ drive (in Windows), and src/bar.c is |
| 34 | +# a relative path under the D:\ drive - This is invalid and the function will misbehave. |
| 35 | +# _sources - List of sources that have a common root directory which needs to be found. |
| 36 | +# _return_var - Name of variable in parent-scope holding the return value. |
| 37 | +# Returns - Path to the common root directory of the given list of sources. |
| 38 | +#=============================================================================# |
| 39 | +function(get_sources_root_directory _sources _return_var) |
| 40 | + |
| 41 | + get_shallowest_directory_structure_path("${_sources}" shallowest_path) |
| 42 | + |
| 43 | + get_filename_component(root_dir ${shallowest_path} DIRECTORY) |
| 44 | + |
| 45 | + if ("${root_dir}" MATCHES ".+src$") # 'src' directory has been retrieved as shallowest path |
| 46 | + # The actual root directory is one level above 'src' |
| 47 | + get_filename_component(root_dir ${root_dir} DIRECTORY) |
| 48 | + endif () |
| 49 | + |
| 50 | + set(${_return_var} ${root_dir} PARENT_SCOPE) |
| 51 | + |
| 52 | +endfunction() |
0 commit comments