cmFilePathUuid.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmFilePathUuid_h
  4. #define cmFilePathUuid_h
  5. #include <cmConfigure.h> // IWYU pragma: keep
  6. #include <stddef.h>
  7. #include <string>
  8. #include <utility>
  9. class cmMakefile;
  10. /** \class cmFilePathUuid
  11. * @brief Generates a unique pathless file name with a checksum component
  12. * calculated from the file path.
  13. *
  14. * The checksum is calculated from the relative file path to the
  15. * closest known project directory. This guarantees reproducibility
  16. * when source and build directory differ e.g. for different project
  17. * build directories.
  18. */
  19. class cmFilePathUuid
  20. {
  21. public:
  22. /// Maximum number of characters to use from the file name
  23. static const size_t partLengthName = 14;
  24. /// Maximum number of characters to use from the path checksum
  25. static const size_t partLengthCheckSum = 14;
  26. /// @brief Initilizes the parent directories from a makefile
  27. cmFilePathUuid(cmMakefile* makefile);
  28. /// @brief Initilizes the parent directories manually
  29. cmFilePathUuid(const std::string& currentSrcDir,
  30. const std::string& currentBinDir,
  31. const std::string& projectSrcDir,
  32. const std::string& projectBinDir);
  33. /* @brief Calculates and returns the uuid for a file path
  34. *
  35. * @arg outputPrefix optional string to prepend to the result
  36. * @arg outputSuffix optional string to append to the result
  37. */
  38. std::string get(const std::string& filePath,
  39. const char* outputPrefix = CM_NULLPTR,
  40. const char* outputSuffix = CM_NULLPTR);
  41. private:
  42. void initParentDirs(const std::string& currentSrcDir,
  43. const std::string& currentBinDir,
  44. const std::string& projectSrcDir,
  45. const std::string& projectBinDir);
  46. /// Returns the relative path and the parent directory key string (seed)
  47. void GetRelPathSeed(const std::string& filePath, std::string& sourceRelPath,
  48. std::string& sourceRelSeed);
  49. std::string GetChecksumString(const std::string& sourceFilename,
  50. const std::string& sourceRelPath,
  51. const std::string& sourceRelSeed);
  52. /// Size of the parent directory list
  53. static const size_t numParentDirs = 4;
  54. /// List of (directory name, seed name) pairs
  55. std::pair<std::string, std::string> parentDirs[numParentDirs];
  56. };
  57. #endif