diff options
author | Marc Chevrier <marc.chevrier@gmail.com> | 2021-04-20 16:46:26 (GMT) |
---|---|---|
committer | Craig Scott <craig.scott@crascit.com> | 2021-04-21 22:08:47 (GMT) |
commit | e4b793c614f2a499d4d235ec2267ad6bff56a697 (patch) | |
tree | 1a1d878ab438d0983bbf4905473cf916b96c6bb2 /Source/cmFileCommand.cxx | |
parent | 63ffe210365ce2d1dd67fcabcc67e20913f320a8 (diff) | |
download | CMake-e4b793c614f2a499d4d235ec2267ad6bff56a697.zip CMake-e4b793c614f2a499d4d235ec2267ad6bff56a697.tar.gz CMake-e4b793c614f2a499d4d235ec2267ad6bff56a697.tar.bz2 |
file(REAL_PATH): add option EXPAND_TILDE
This option enables the replacement of any leading tilde with the path
to the user's home directory.
Diffstat (limited to 'Source/cmFileCommand.cxx')
-rw-r--r-- | Source/cmFileCommand.cxx | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx index 05ebcca..a943258 100644 --- a/Source/cmFileCommand.cxx +++ b/Source/cmFileCommand.cxx @@ -1246,9 +1246,12 @@ bool HandleRealPathCommand(std::vector<std::string> const& args, struct Arguments { std::string BaseDirectory; + bool ExpandTilde = false; }; - static auto const parser = cmArgumentParser<Arguments>{}.Bind( - "BASE_DIRECTORY"_s, &Arguments::BaseDirectory); + static auto const parser = + cmArgumentParser<Arguments>{} + .Bind("BASE_DIRECTORY"_s, &Arguments::BaseDirectory) + .Bind("EXPAND_TILDE"_s, &Arguments::ExpandTilde); std::vector<std::string> unparsedArguments; std::vector<std::string> keywordsMissingValue; @@ -1270,7 +1273,21 @@ bool HandleRealPathCommand(std::vector<std::string> const& args, arguments.BaseDirectory = status.GetMakefile().GetCurrentSourceDirectory(); } - cmCMakePath path(args[1]); + auto input = args[1]; + if (arguments.ExpandTilde && !input.empty()) { + if (input[0] == '~' && (input.length() == 1 || input[1] == '/')) { + std::string home; + if ( +#if defined(_WIN32) && !defined(__CYGWIN__) + cmSystemTools::GetEnv("USERPROFILE", home) || +#endif + cmSystemTools::GetEnv("HOME", home)) { + input.replace(0, 1, home); + } + } + } + + cmCMakePath path(input, cmCMakePath::auto_format); path = path.Absolute(arguments.BaseDirectory).Normal(); auto realPath = cmSystemTools::GetRealPath(path.GenericString()); |