Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
9.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Interface for makefiles to build differently depending on where they are run from
  4. //
  5. //===========================================================================//
  6. #ifndef DMEMAKEFILEUTILS_H
  7. #define DMEMAKEFILEUTILS_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "movieobjects/idmemakefileutils.h"
  12. #include "datamodel/dmehandle.h"
  13. #include "tier1/utlsymbol.h"
  14. #include "tier3/tier3.h"
  15. //-----------------------------------------------------------------------------
  16. // Forward declarations
  17. //-----------------------------------------------------------------------------
  18. class CDmeMakefileUtils;
  19. class CDmeMDLMakefile;
  20. class CDmeMayaMakefile;
  21. class CDmeSourceMayaFile;
  22. class CDmeMakefile;
  23. //-----------------------------------------------------------------------------
  24. //
  25. // This glue code here is to make it easy to create methods using various DmElement types
  26. //
  27. //-----------------------------------------------------------------------------
  28. //-----------------------------------------------------------------------------
  29. // Compilation steps
  30. //-----------------------------------------------------------------------------
  31. enum CompilationStep_t
  32. {
  33. BUILDING_STANDARD_DEPENDENCIES = 0,
  34. BUILDING_ALL_DEPENDENCIES,
  35. BEFORE_COMPILATION,
  36. PERFORMING_COMPILATION,
  37. AFTER_COMPILATION_FAILED,
  38. AFTER_COMPILATION_SUCCEEDED,
  39. NOT_COMPILING,
  40. };
  41. //-----------------------------------------------------------------------------
  42. // Utility adapter class to hook compile funcs into the map
  43. //-----------------------------------------------------------------------------
  44. class CCompileFuncAdapterBase
  45. {
  46. public:
  47. virtual void InitializeAdapter( ) = 0;
  48. virtual bool PerformCompilationStep( CDmElement *pElement, CompilationStep_t step ) = 0;
  49. protected:
  50. // Constructor, protected because these should never be instanced directly
  51. CCompileFuncAdapterBase( ) {}
  52. public:
  53. CUtlSymbol m_ElementType;
  54. CCompileFuncAdapterBase *m_pNext;
  55. };
  56. template< class U, class T >
  57. class CCompileFuncAdapter : public CCompileFuncAdapterBase
  58. {
  59. typedef CCompileFuncAdapterBase BaseClass;
  60. public:
  61. CCompileFuncAdapter( )
  62. {
  63. // Hook into the list
  64. m_pNext = U::m_CompileFuncTree.m_pFirstAdapter;
  65. U::m_CompileFuncTree.m_pFirstAdapter = this;
  66. }
  67. virtual void InitializeAdapter( )
  68. {
  69. m_ElementType = T::GetStaticTypeSymbol();
  70. if ( m_pNext )
  71. {
  72. m_pNext->InitializeAdapter();
  73. }
  74. }
  75. virtual bool PerformCompilationStep( CDmElement *pElement, CompilationStep_t step )
  76. {
  77. T *pConverted = CastElement< T >( pElement );
  78. if ( pConverted )
  79. return U::m_pSingleton->PerformCompilationStep( pConverted, step );
  80. return false;
  81. }
  82. };
  83. //-----------------------------------------------------------------------------
  84. // Utility adapter class to hook editor opening funcs into the map
  85. //-----------------------------------------------------------------------------
  86. class COpenEditorFuncAdapterBase
  87. {
  88. public:
  89. virtual void InitializeAdapter( ) = 0;
  90. virtual void OpenEditor( CDmElement *pElement ) = 0;
  91. protected:
  92. // Constructor, protected because these should never be instanced directly
  93. COpenEditorFuncAdapterBase( ) {}
  94. public:
  95. CUtlSymbol m_ElementType;
  96. COpenEditorFuncAdapterBase *m_pNext;
  97. };
  98. template< class U, class T >
  99. class COpenEditorFuncAdapter : public COpenEditorFuncAdapterBase
  100. {
  101. typedef COpenEditorFuncAdapterBase BaseClass;
  102. public:
  103. COpenEditorFuncAdapter( )
  104. {
  105. // Hook into the list
  106. m_pNext = U::m_OpenEditorFuncTree.m_pFirstAdapter;
  107. U::m_OpenEditorFuncTree.m_pFirstAdapter = this;
  108. }
  109. virtual void InitializeAdapter( )
  110. {
  111. m_ElementType = T::GetStaticTypeSymbol();
  112. if ( m_pNext )
  113. {
  114. m_pNext->InitializeAdapter();
  115. }
  116. }
  117. virtual void OpenEditor( CDmElement *pElement )
  118. {
  119. T *pConverted = CastElement< T >( pElement );
  120. if ( pConverted )
  121. {
  122. U::m_pSingleton->OpenEditor( pConverted );
  123. }
  124. }
  125. };
  126. #define DECLARE_DMEMAKEFILE_UTIL_CLASS_BASE( _className ) \
  127. protected: \
  128. typedef _className ThisClass; \
  129. static CompileFuncTree_t m_CompileFuncTree; \
  130. static OpenEditorFuncTree_t m_OpenEditorFuncTree; \
  131. static _className *m_pSingleton; \
  132. template< typename U, typename T > friend class CCompileFuncAdapter; \
  133. template< typename U, typename T > friend class COpenEditorFuncAdapter; \
  134. virtual CompileFuncTree_t* GetCompileTree() \
  135. { \
  136. return &m_CompileFuncTree; \
  137. } \
  138. virtual OpenEditorFuncTree_t* GetOpenEditorTree() \
  139. { \
  140. return &m_OpenEditorFuncTree; \
  141. } \
  142. #define DECLARE_DMEMAKEFILE_UTIL_CLASS( _className, _baseClass ) \
  143. DECLARE_DMEMAKEFILE_UTIL_CLASS_BASE( _className ) \
  144. typedef _baseClass BaseClass; \
  145. protected: \
  146. virtual void InitializeFuncMaps() \
  147. { \
  148. m_pSingleton = this; \
  149. m_CompileFuncTree.m_pBaseAdapterTree = &BaseClass::m_CompileFuncTree; \
  150. m_CompileFuncTree.m_pFirstAdapter->InitializeAdapter( ); \
  151. m_OpenEditorFuncTree.m_pBaseAdapterTree = &BaseClass::m_OpenEditorFuncTree; \
  152. m_OpenEditorFuncTree.m_pFirstAdapter->InitializeAdapter( ); \
  153. BaseClass::InitializeFuncMaps(); \
  154. } \
  155. #define DECLARE_DMEMAKEFILE_UTIL_CLASS_ROOT( _className ) \
  156. DECLARE_DMEMAKEFILE_UTIL_CLASS_BASE( _className ) \
  157. protected: \
  158. virtual void InitializeFuncMaps() \
  159. { \
  160. m_pSingleton = this; \
  161. m_CompileFuncTree.m_pBaseAdapterTree = NULL; \
  162. m_CompileFuncTree.m_pFirstAdapter->InitializeAdapter( ); \
  163. m_OpenEditorFuncTree.m_pBaseAdapterTree = NULL; \
  164. m_OpenEditorFuncTree.m_pFirstAdapter->InitializeAdapter( ); \
  165. } \
  166. #define IMPLEMENT_DMEMAKEFILE_UTIL_CLASS( _className ) \
  167. CDmeMakefileUtils::CompileFuncTree_t _className::m_CompileFuncTree; \
  168. CDmeMakefileUtils::OpenEditorFuncTree_t _className::m_OpenEditorFuncTree; \
  169. _className *_className::m_pSingleton; \
  170. #define DECLARE_COMPILEFUNC( _className ) \
  171. bool PerformCompilationStep( _className *pClassName, CompilationStep_t step ); \
  172. CCompileFuncAdapter< ThisClass, _className > m_##_className##CompileAdapter
  173. #define DECLARE_OPENEDITORFUNC( _className ) \
  174. void OpenEditor( _className *pClassName ); \
  175. COpenEditorFuncAdapter< ThisClass, _className > m_##_className##OpenEditorAdapter
  176. //-----------------------------------------------------------------------------
  177. // Interface for makefiles to build differently depending on where they are run from
  178. //-----------------------------------------------------------------------------
  179. class CDmeMakefileUtils : public CTier3AppSystem<IDmeMakefileUtils>
  180. {
  181. protected:
  182. struct CompileFuncTree_t
  183. {
  184. CCompileFuncAdapterBase *m_pFirstAdapter;
  185. CompileFuncTree_t *m_pBaseAdapterTree;
  186. };
  187. struct OpenEditorFuncTree_t
  188. {
  189. COpenEditorFuncAdapterBase *m_pFirstAdapter;
  190. OpenEditorFuncTree_t *m_pBaseAdapterTree;
  191. };
  192. typedef CTier3AppSystem< IDmeMakefileUtils > BaseClass;
  193. DECLARE_DMEMAKEFILE_UTIL_CLASS_ROOT( CDmeMakefileUtils );
  194. public:
  195. // Constructor, destructor
  196. CDmeMakefileUtils();
  197. virtual ~CDmeMakefileUtils();
  198. // Inherited from IAppSystem
  199. virtual void *QueryInterface( const char *pInterfaceName );
  200. virtual InitReturnVal_t Init();
  201. // Inherited from IDmeMakefileUtils
  202. virtual void PerformCompile( CDmElement *pElement, bool bBuildAllDependencies );
  203. virtual bool IsCurrentlyCompiling( );
  204. virtual int GetCompileOutputSize();
  205. virtual CompilationState_t UpdateCompilation( char *pOutputBuf, int nBufLen );
  206. virtual void AbortCurrentCompilation();
  207. virtual void PerformOpenEditor( CDmElement *pElement );
  208. virtual int GetExitCode();
  209. protected:
  210. // Compile functions + editor functions
  211. DECLARE_COMPILEFUNC( CDmElement );
  212. DECLARE_COMPILEFUNC( CDmeMakefile );
  213. DECLARE_COMPILEFUNC( CDmeMDLMakefile );
  214. DECLARE_COMPILEFUNC( CDmeMayaMakefile );
  215. DECLARE_OPENEDITORFUNC( CDmeSourceMayaFile );
  216. // Queues up a compilation task
  217. // ( Call only in BUILDING_STANDARD_DEPENDENCIES or BUILDING_ALL_DEPENDENCIES )
  218. void AddCompilationTask( CDmElement* pElement );
  219. // Sets the compilation process handle
  220. // ( Call only in PERFORMING_COMPILATION )
  221. void SetCompileProcess( ProcessHandle_t hProcess );
  222. private:
  223. struct CompileInfo_t
  224. {
  225. CDmeHandle< CDmElement > m_hElement;
  226. CCompileFuncAdapterBase *m_pAdapter;
  227. };
  228. // Finds the adapter class associated with a particular element type
  229. CCompileFuncAdapterBase *DetermineCompileAdapter( CDmElement *pElement );
  230. COpenEditorFuncAdapterBase *DetermineOpenEditorAdapter( CDmElement *pElement );
  231. // Dequeue the first compile task and start it up
  232. void StartNextCompileTask();
  233. // Performs the compilation step on all elements
  234. bool PerformCompilationStep( CompilationStep_t step );
  235. // Queues up a compilation task
  236. void AddCompilationTask( CDmElement* pElement, CCompileFuncAdapterBase *pAdapter );
  237. // Default implementatations for compile dependencies
  238. bool AddCompileDependencies( CDmeMakefile *pMakefile, bool bBuildAllDependencies );
  239. CUtlVector< CompileInfo_t > m_CompileTasks;
  240. ProcessHandle_t m_hCompileProcess;
  241. int m_nCurrentCompileTask;
  242. int m_nExitCode;
  243. CompilationStep_t m_CompilationStep;
  244. };
  245. #endif // DMEMAKEFILEUTILS_H