cmFilePathChecksum.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 <stddef.h>
  7. #include <string>
  8. #include <utility>
  9. class cmMakefile;
  10. /** \class cmFilePathChecksum
  11. * @brief Generates a checksum for the parent directory of a file
  12. *
  13. * The checksum is calculated from the relative file path to the
  14. * closest known project directory. This guarantees reproducibility
  15. * when source and build directory differ e.g. for different project
  16. * build directories.
  17. */
  18. class cmFilePathChecksum
  19. {
  20. public:
  21. /// Maximum number of characters to use from the path checksum
  22. static const size_t partLengthDefault = 10;
  23. /// @brief Parent directories are empty
  24. cmFilePathChecksum();
  25. /// @brief Initilizes the parent directories manually
  26. cmFilePathChecksum(const std::string& currentSrcDir,
  27. const std::string& currentBinDir,
  28. const std::string& projectSrcDir,
  29. const std::string& projectBinDir);
  30. /// @brief Initilizes the parent directories from a makefile
  31. cmFilePathChecksum(cmMakefile* makefile);
  32. /// @brief Allows parent directories setup after construction
  33. ///
  34. void setupParentDirs(const std::string& currentSrcDir,
  35. const std::string& currentBinDir,
  36. const std::string& projectSrcDir,
  37. const std::string& projectBinDir);
  38. /* @brief Calculates the path checksum for the parent directory of a file
  39. *
  40. */
  41. std::string get(const std::string& filePath) const;
  42. /* @brief Same as get() but returns only the first length characters
  43. *
  44. */
  45. std::string getPart(const std::string& filePath,
  46. size_t length = partLengthDefault) const;
  47. private:
  48. /// Size of the parent directory list
  49. static const size_t numParentDirs = 4;
  50. /// List of (directory name, seed name) pairs
  51. std::pair<std::string, std::string> parentDirs[numParentDirs];
  52. };
  53. #endif