| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577 |
- /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
- file Copyright.txt or https://cmake.org/licensing for details. */
- #ifndef cmQtAutoMocUic_h
- #define cmQtAutoMocUic_h
- #include "cmConfigure.h" // IWYU pragma: keep
- #include "cmFileTime.h"
- #include "cmQtAutoGen.h"
- #include "cmQtAutoGenerator.h"
- #include "cmWorkerPool.h"
- #include "cm_string_view.hxx"
- #include "cmsys/RegularExpression.hxx"
- #include <atomic>
- #include <cstddef>
- #include <map>
- #include <memory>
- #include <set>
- #include <string>
- #include <unordered_map>
- #include <unordered_set>
- #include <utility>
- #include <vector>
- class cmMakefile;
- /** \class cmQtAutoMocUic
- * \brief AUTOMOC and AUTOUIC generator
- */
- class cmQtAutoMocUic : public cmQtAutoGenerator
- {
- public:
- cmQtAutoMocUic();
- ~cmQtAutoMocUic() override;
- cmQtAutoMocUic(cmQtAutoMocUic const&) = delete;
- cmQtAutoMocUic& operator=(cmQtAutoMocUic const&) = delete;
- public:
- // -- Types
- /**
- * Search key plus regular expression pair
- */
- struct KeyExpT
- {
- KeyExpT() = default;
- KeyExpT(const char* key, const char* exp)
- : Key(key)
- , Exp(exp)
- {
- }
- KeyExpT(std::string key, std::string const& exp)
- : Key(std::move(key))
- , Exp(exp)
- {
- }
- std::string Key;
- cmsys::RegularExpression Exp;
- };
- /**
- * Include string with sub parts
- */
- struct IncludeKeyT
- {
- IncludeKeyT(std::string const& key, std::size_t basePrefixLength);
- std::string Key; // Full include string
- std::string Dir; // Include directory
- std::string Base; // Base part of the include file name
- };
- /**
- * Source file parsing cache
- */
- class ParseCacheT
- {
- public:
- // -- Types
- /**
- * Entry of the file parsing cache
- */
- struct FileT
- {
- void Clear();
- struct MocT
- {
- std::string Macro;
- struct IncludeT
- {
- std::vector<IncludeKeyT> Underscore;
- std::vector<IncludeKeyT> Dot;
- } Include;
- std::vector<std::string> Depends;
- } Moc;
- struct UicT
- {
- std::vector<IncludeKeyT> Include;
- std::vector<std::string> Depends;
- } Uic;
- };
- using FileHandleT = std::shared_ptr<FileT>;
- using GetOrInsertT = std::pair<FileHandleT, bool>;
- public:
- ParseCacheT();
- ~ParseCacheT();
- void Clear();
- bool ReadFromFile(std::string const& fileName);
- bool WriteToFile(std::string const& fileName);
- //! Might return an invalid handle
- FileHandleT Get(std::string const& fileName) const;
- //! Always returns a valid handle
- GetOrInsertT GetOrInsert(std::string const& fileName);
- private:
- std::unordered_map<std::string, FileHandleT> Map_;
- };
- /**
- * Source file data
- */
- class SourceFileT
- {
- public:
- SourceFileT(std::string fileName)
- : FileName(std::move(fileName))
- {
- }
- public:
- std::string FileName;
- cmFileTime FileTime;
- ParseCacheT::FileHandleT ParseData;
- std::string BuildPath;
- bool Moc = false;
- bool Uic = false;
- };
- using SourceFileHandleT = std::shared_ptr<SourceFileT>;
- using SourceFileMapT = std::map<std::string, SourceFileHandleT>;
- /**
- * Meta compiler file mapping information
- */
- struct MappingT
- {
- SourceFileHandleT SourceFile;
- std::string OutputFile;
- std::string IncludeString;
- std::vector<SourceFileHandleT> IncluderFiles;
- };
- using MappingHandleT = std::shared_ptr<MappingT>;
- using MappingMapT = std::map<std::string, MappingHandleT>;
- /**
- * Common settings
- */
- class BaseSettingsT
- {
- public:
- // -- Constructors
- BaseSettingsT();
- ~BaseSettingsT();
- BaseSettingsT(BaseSettingsT const&) = delete;
- BaseSettingsT& operator=(BaseSettingsT const&) = delete;
- // -- Attributes
- // - Config
- bool MultiConfig = false;
- bool IncludeProjectDirsBefore = false;
- unsigned int QtVersionMajor = 4;
- // - Directories
- std::string ProjectSourceDir;
- std::string ProjectBinaryDir;
- std::string CurrentSourceDir;
- std::string CurrentBinaryDir;
- std::string AutogenBuildDir;
- std::string AutogenIncludeDir;
- // - Files
- std::string CMakeExecutable;
- cmFileTime CMakeExecutableTime;
- std::string ParseCacheFile;
- std::vector<std::string> HeaderExtensions;
- };
- /**
- * Shared common variables
- */
- class BaseEvalT
- {
- public:
- // -- Parse Cache
- bool ParseCacheChanged = false;
- cmFileTime ParseCacheTime;
- ParseCacheT ParseCache;
- // -- Sources
- SourceFileMapT Headers;
- SourceFileMapT Sources;
- };
- /**
- * Moc settings
- */
- class MocSettingsT
- {
- public:
- // -- Constructors
- MocSettingsT();
- ~MocSettingsT();
- MocSettingsT(MocSettingsT const&) = delete;
- MocSettingsT& operator=(MocSettingsT const&) = delete;
- // -- Const methods
- bool skipped(std::string const& fileName) const;
- std::string MacrosString() const;
- // -- Attributes
- bool Enabled = false;
- bool SettingsChanged = false;
- bool RelaxedMode = false;
- cmFileTime ExecutableTime;
- std::string Executable;
- std::string CompFileAbs;
- std::string PredefsFileRel;
- std::string PredefsFileAbs;
- std::unordered_set<std::string> SkipList;
- std::vector<std::string> IncludePaths;
- std::vector<std::string> Includes;
- std::vector<std::string> Definitions;
- std::vector<std::string> Options;
- std::vector<std::string> AllOptions;
- std::vector<std::string> PredefsCmd;
- std::vector<KeyExpT> DependFilters;
- std::vector<KeyExpT> MacroFilters;
- cmsys::RegularExpression RegExpInclude;
- };
- /**
- * Moc shared variables
- */
- class MocEvalT
- {
- public:
- // -- predefines file
- cmFileTime PredefsTime;
- // -- Mappings
- MappingMapT HeaderMappings;
- MappingMapT SourceMappings;
- MappingMapT Includes;
- // -- Discovered files
- SourceFileMapT HeadersDiscovered;
- // -- Mocs compilation
- bool CompUpdated = false;
- std::vector<std::string> CompFiles;
- };
- /**
- * Uic settings
- */
- class UicSettingsT
- {
- public:
- UicSettingsT();
- ~UicSettingsT();
- UicSettingsT(UicSettingsT const&) = delete;
- UicSettingsT& operator=(UicSettingsT const&) = delete;
- // -- Const methods
- bool skipped(std::string const& fileName) const;
- // -- Attributes
- bool Enabled = false;
- bool SettingsChanged = false;
- cmFileTime ExecutableTime;
- std::string Executable;
- std::unordered_set<std::string> SkipList;
- std::vector<std::string> TargetOptions;
- std::map<std::string, std::vector<std::string>> Options;
- std::vector<std::string> SearchPaths;
- cmsys::RegularExpression RegExpInclude;
- };
- /**
- * Uic shared variables
- */
- class UicEvalT
- {
- public:
- SourceFileMapT UiFiles;
- MappingMapT Includes;
- };
- /**
- * Abstract job class for concurrent job processing
- */
- class JobT : public cmWorkerPool::JobT
- {
- protected:
- /**
- * @brief Protected default constructor
- */
- JobT(bool fence = false)
- : cmWorkerPool::JobT(fence)
- {
- }
- //! Get the generator. Only valid during Process() call!
- cmQtAutoMocUic* Gen() const
- {
- return static_cast<cmQtAutoMocUic*>(UserData());
- };
- // -- Accessors. Only valid during Process() call!
- Logger const& Log() const { return Gen()->Log(); }
- BaseSettingsT const& BaseConst() const { return Gen()->BaseConst(); }
- BaseEvalT& BaseEval() const { return Gen()->BaseEval(); }
- MocSettingsT const& MocConst() const { return Gen()->MocConst(); }
- MocEvalT& MocEval() const { return Gen()->MocEval(); }
- UicSettingsT const& UicConst() const { return Gen()->UicConst(); }
- UicEvalT& UicEval() const { return Gen()->UicEval(); }
- // -- Error logging with automatic abort
- void LogError(GenT genType, cm::string_view message) const;
- void LogFileError(GenT genType, cm::string_view filename,
- cm::string_view message) const;
- void LogCommandError(GenT genType, cm::string_view message,
- std::vector<std::string> const& command,
- std::string const& output) const;
- /**
- * @brief Run an external process. Use only during Process() call!
- */
- bool RunProcess(GenT genType, cmWorkerPool::ProcessResultT& result,
- std::vector<std::string> const& command,
- std::string* infoMessage = nullptr);
- };
- /**
- * Fence job utility class
- */
- class JobFenceT : public JobT
- {
- public:
- JobFenceT()
- : JobT(true)
- {
- }
- void Process() override{};
- };
- /**
- * Generate moc_predefs.h
- */
- class JobMocPredefsT : public JobFenceT
- {
- void Process() override;
- bool Update(std::string* reason) const;
- };
- /**
- * File parse job base class
- */
- class JobParseT : public JobT
- {
- public:
- JobParseT(SourceFileHandleT fileHandle)
- : FileHandle(std::move(fileHandle))
- {
- }
- protected:
- bool ReadFile();
- void CreateKeys(std::vector<IncludeKeyT>& container,
- std::set<std::string> const& source,
- std::size_t basePrefixLength);
- void MocMacro();
- void MocDependecies();
- void MocIncludes();
- void UicIncludes();
- protected:
- SourceFileHandleT FileHandle;
- std::string Content;
- };
- /**
- * Header file parse job
- */
- class JobParseHeaderT : public JobParseT
- {
- public:
- using JobParseT::JobParseT;
- void Process() override;
- };
- /**
- * Source file parse job
- */
- class JobParseSourceT : public JobParseT
- {
- public:
- using JobParseT::JobParseT;
- void Process() override;
- };
- /**
- * Evaluate parsed files
- */
- class JobEvaluateT : public JobFenceT
- {
- void Process() override;
- // -- Moc
- bool MocEvalHeader(SourceFileHandleT source);
- bool MocEvalSource(SourceFileHandleT const& source);
- SourceFileHandleT MocFindIncludedHeader(
- std::string const& includerDir, std::string const& includeBase) const;
- SourceFileHandleT MocFindHeader(std::string const& basePath) const;
- std::string MocMessageTestHeaders(cm::string_view fileBase) const;
- bool MocRegisterIncluded(std::string const& includeString,
- SourceFileHandleT includerFileHandle,
- SourceFileHandleT sourceFileHandle,
- bool sourceIsHeader) const;
- void MocRegisterMapping(MappingHandleT mappingHandle,
- bool sourceIsHeader) const;
- // -- Uic
- bool UicEval(SourceFileMapT const& fileMap);
- bool UicEvalFile(SourceFileHandleT const& sourceFileHandle);
- SourceFileHandleT UicFindIncludedUi(std::string const& sourceFile,
- std::string const& sourceDir,
- IncludeKeyT const& incKey) const;
- bool UicRegisterMapping(std::string const& includeString,
- SourceFileHandleT uiFileHandle,
- SourceFileHandleT includerFileHandle);
- };
- /**
- * Generates moc/uic jobs
- */
- class JobGenerateT : public JobFenceT
- {
- void Process() override;
- // -- Moc
- bool MocGenerate(MappingHandleT const& mapping, bool compFile) const;
- bool MocUpdate(MappingT const& mapping, std::string* reason) const;
- std::pair<std::string, cmFileTime> MocFindDependency(
- std::string const& sourceDir, std::string const& includeString) const;
- // -- Uic
- bool UicGenerate(MappingHandleT const& mapping) const;
- bool UicUpdate(MappingT const& mapping, std::string* reason) const;
- };
- /**
- * File compiling base job
- */
- class JobCompileT : public JobT
- {
- public:
- JobCompileT(MappingHandleT uicMapping, std::unique_ptr<std::string> reason)
- : Mapping(std::move(uicMapping))
- , Reason(std::move(reason))
- {
- }
- protected:
- MappingHandleT Mapping;
- std::unique_ptr<std::string> Reason;
- };
- /**
- * moc compiles a file
- */
- class JobMocT : public JobCompileT
- {
- public:
- using JobCompileT::JobCompileT;
- void Process() override;
- };
- /**
- * uic compiles a file
- */
- class JobUicT : public JobCompileT
- {
- public:
- using JobCompileT::JobCompileT;
- void Process() override;
- };
- /// @brief Generate mocs_compilation.cpp
- ///
- class JobMocsCompilationT : public JobFenceT
- {
- private:
- void Process() override;
- };
- /// @brief The last job
- ///
- class JobFinishT : public JobFenceT
- {
- private:
- void Process() override;
- };
- // -- Const settings interface
- BaseSettingsT const& BaseConst() const { return this->BaseConst_; }
- BaseEvalT& BaseEval() { return this->BaseEval_; }
- MocSettingsT const& MocConst() const { return this->MocConst_; }
- MocEvalT& MocEval() { return this->MocEval_; }
- UicSettingsT const& UicConst() const { return this->UicConst_; }
- UicEvalT& UicEval() { return this->UicEval_; }
- // -- Parallel job processing interface
- cmWorkerPool& WorkerPool() { return WorkerPool_; }
- void AbortError() { Abort(true); }
- void AbortSuccess() { Abort(false); }
- // -- Utility
- std::string AbsoluteBuildPath(cm::string_view relativePath) const;
- std::string AbsoluteIncludePath(cm::string_view relativePath) const;
- template <class JOBTYPE>
- void CreateParseJobs(SourceFileMapT const& sourceMap);
- private:
- // -- Utility accessors
- Logger const& Log() const { return Logger_; }
- // -- Abstract processing interface
- bool Init(cmMakefile* makefile) override;
- void InitJobs();
- bool Process() override;
- // -- Settings file
- void SettingsFileRead();
- bool SettingsFileWrite();
- // -- Parse cache
- void ParseCacheRead();
- bool ParseCacheWrite();
- // -- Thread processing
- void Abort(bool error);
- // -- Generation
- bool CreateDirectories();
- private:
- // -- Utility
- Logger Logger_;
- // -- Settings
- BaseSettingsT BaseConst_;
- BaseEvalT BaseEval_;
- MocSettingsT MocConst_;
- MocEvalT MocEval_;
- UicSettingsT UicConst_;
- UicEvalT UicEval_;
- // -- Settings file
- std::string SettingsFile_;
- std::string SettingsStringMoc_;
- std::string SettingsStringUic_;
- // -- Worker thread pool
- std::atomic<bool> JobError_ = ATOMIC_VAR_INIT(false);
- cmWorkerPool WorkerPool_;
- };
- #endif
|