cmFilePathChecksum.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 cmFilePathChecksum_h
  4. #define cmFilePathChecksum_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <array>
  7. #include <stddef.h>
  8. #include <string>
  9. #include <utility>
  10. class cmMakefile;
  11. /** \class cmFilePathChecksum
  12. * @brief Generates a checksum for the parent directory of a file
  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 cmFilePathChecksum
  20. {
  21. public:
  22. /// Maximum number of characters to use from the path checksum
  23. static const size_t partLengthDefault = 10;
  24. /// @brief Parent directories are empty
  25. cmFilePathChecksum();
  26. /// @brief Initializes the parent directories manually
  27. cmFilePathChecksum(std::string const& currentSrcDir,
  28. std::string const& currentBinDir,
  29. std::string const& projectSrcDir,
  30. std::string const& projectBinDir);
  31. /// @brief Initializes the parent directories from a makefile
  32. cmFilePathChecksum(cmMakefile* makefile);
  33. /// @brief Allows parent directories setup after construction
  34. ///
  35. void setupParentDirs(std::string const& currentSrcDir,
  36. std::string const& currentBinDir,
  37. std::string const& projectSrcDir,
  38. std::string const& projectBinDir);
  39. /* @brief Calculates the path checksum for the parent directory of a file
  40. *
  41. */
  42. std::string get(std::string const& filePath) const;
  43. /* @brief Same as get() but returns only the first length characters
  44. *
  45. */
  46. std::string getPart(std::string const& filePath,
  47. size_t length = partLengthDefault) const;
  48. private:
  49. /// List of (directory name, seed name) pairs
  50. std::array<std::pair<std::string, std::string>, 4> parentDirs;
  51. };
  52. #endif