-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathdmemakefile.cpp
More file actions
628 lines (518 loc) · 18.5 KB
/
dmemakefile.cpp
File metadata and controls
628 lines (518 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//====== Copyright © 1996-2004, Valve Corporation, All rights reserved. =======
//
// Describes an asset: something that is compiled from sources,
// in potentially multiple steps, to a compiled resource
//
//=============================================================================
#include "movieobjects/dmemdlmakefile.h"
#include "movieobjects/idmemakefileutils.h"
#include "datamodel/dmelementfactoryhelper.h"
#include "tier2/fileutils.h"
#include "tier3/tier3.h"
#include "filesystem.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// Hook into element factories
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT_FACTORY( DmeSource, CDmeSource );
//-----------------------------------------------------------------------------
// Construction/destruction
//-----------------------------------------------------------------------------
void CDmeSource::OnConstruction()
{
m_DependentMakefile = NULL;
}
void CDmeSource::OnDestruction()
{
}
//-----------------------------------------------------------------------------
// Sets/gets the makefile that was used to build this source
//-----------------------------------------------------------------------------
void CDmeSource::SetDependentMakefile( CDmeMakefile *pMakeFile )
{
m_DependentMakefile = pMakeFile;
}
CDmeMakefile *CDmeSource::GetDependentMakefile()
{
return m_DependentMakefile.Get();
}
//-----------------------------------------------------------------------------
// Call this to open the source file in an editor
//-----------------------------------------------------------------------------
void CDmeSource::OpenEditor()
{
if ( g_pDmeMakefileUtils )
{
g_pDmeMakefileUtils->PerformOpenEditor( this );
}
}
//-----------------------------------------------------------------------------
// Hook into element factories
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT_FACTORY( DmeMakefile, CDmeMakefile );
//-----------------------------------------------------------------------------
// Construction/destruction
//-----------------------------------------------------------------------------
void CDmeMakefile::OnConstruction()
{
m_Sources.Init( this, "sources" );
m_hOutput = NULL;
m_hCompileProcess = NULL;
m_bIsDirty = false;
}
void CDmeMakefile::OnDestruction()
{
DestroyOutputElement( m_hOutput.Get() );
m_hOutput = NULL;
}
//-----------------------------------------------------------------------------
// Performs pre-compilation step
//-----------------------------------------------------------------------------
void CDmeMakefile::PreCompile( )
{
// Make all outputs writeable
MakeOutputsWriteable();
// Destroy the current output object; we'll need to reload it
// NOTE: Don't check for m_hOutput == 0; we always need to call DestroyOutputElement
// Sometimes makefiles have to do stuff even if m_hOutput == NULL
DestroyOutputElement( m_hOutput );
m_hOutput = NULL;
}
void CDmeMakefile::PostCompile( )
{
}
//-----------------------------------------------------------------------------
// Gets the output element created by compilation of this makefile
//-----------------------------------------------------------------------------
CDmElement *CDmeMakefile::GetOutputElement( bool bCreateIfNecessary )
{
if ( m_hOutput.Get() )
return m_hOutput.Get();
if ( !bCreateIfNecessary )
return NULL;
if ( !g_pDmeMakefileUtils || !g_pDmeMakefileUtils->IsCurrentlyCompiling() )
{
m_hOutput = CreateOutputElement();
}
return m_hOutput.Get();
}
//-----------------------------------------------------------------------------
// Gets the path of the makefile
//-----------------------------------------------------------------------------
void CDmeMakefile::GetMakefilePath( char *pFullPath, int nBufLen )
{
DmFileId_t fileId = GetFileId();
const char *pFileName = ( fileId != DMFILEID_INVALID ) ? g_pDataModel->GetFileName( fileId ) : "";
Assert( !pFileName[0] || Q_IsAbsolutePath( pFileName ) );
Q_ExtractFilePath( pFileName, pFullPath, nBufLen );
}
//-----------------------------------------------------------------------------
// Returns the output directory we expect to compile files into
//-----------------------------------------------------------------------------
bool CDmeMakefile::GetOutputDirectory( char *pFullPath, int nBufLen )
{
return GetDefaultDirectory( GetOutputDirectoryID(), pFullPath, nBufLen );
}
//-----------------------------------------------------------------------------
// Returns the output name (output directory + filename, no extension)
//-----------------------------------------------------------------------------
bool CDmeMakefile::GetOutputName( char *pFullPath, int nBufLen )
{
pFullPath[0] = 0;
char pOutputPath[MAX_PATH];
if ( !GetDefaultDirectory( GetOutputDirectoryID(), pOutputPath, sizeof(pOutputPath) ) )
return false;
DmFileId_t fileId = GetFileId();
const char *pFileName = ( fileId != DMFILEID_INVALID ) ? g_pDataModel->GetFileName( fileId ) : "";
if ( !pFileName || !pFileName[0] )
return false;
Q_ComposeFileName( pOutputPath, Q_UnqualifiedFileName(pFileName), pFullPath, nBufLen );
Q_RemoveDotSlashes( pFullPath );
return true;
}
//-----------------------------------------------------------------------------
// Converts the m_pDefaultDirectoryID field of the DmeMakefileType_t to a full path
//-----------------------------------------------------------------------------
bool CDmeMakefile::GetDefaultDirectory( const char *pDefaultDirectoryID, char *pFullPath, int nBufLen )
{
if ( StringHasPrefix( pDefaultDirectoryID, "contentdir:" ) )
{
pDefaultDirectoryID += 11;
GetModContentSubdirectory( pDefaultDirectoryID, pFullPath, nBufLen );
Q_RemoveDotSlashes( pFullPath );
return true;
}
if ( StringHasPrefix( pDefaultDirectoryID, "gamedir:" ) )
{
pDefaultDirectoryID += 8;
GetModSubdirectory( pDefaultDirectoryID, pFullPath, nBufLen );
Q_RemoveDotSlashes( pFullPath );
return true;
}
if ( StringHasPrefix( pDefaultDirectoryID, "makefiledir:" ) )
{
char pMakefilePath[MAX_PATH];
GetMakefilePath( pMakefilePath, sizeof(pMakefilePath) );
pDefaultDirectoryID += 12;
Q_ComposeFileName( pMakefilePath, pDefaultDirectoryID, pFullPath, nBufLen );
Q_RemoveDotSlashes( pFullPath );
return true;
}
if ( StringHasPrefix( pDefaultDirectoryID, "makefilegamedir:" ) )
{
char pMakefilePath[MAX_PATH];
GetMakefilePath( pMakefilePath, sizeof(pMakefilePath) );
char pModContentDirectory[MAX_PATH];
GetModContentSubdirectory( NULL, pModContentDirectory, sizeof(pModContentDirectory) );
char pRelativePath[MAX_PATH];
if ( !Q_MakeRelativePath( pMakefilePath, pModContentDirectory, pRelativePath, sizeof(pRelativePath) ) )
{
pFullPath[0] = 0;
return false;
}
char pModDirectory[MAX_PATH];
GetModSubdirectory( NULL, pModDirectory, sizeof(pModDirectory) );
char pMakefileGamePath[MAX_PATH];
Q_ComposeFileName( pModDirectory, pRelativePath, pMakefileGamePath, sizeof(pMakefileGamePath) );
pDefaultDirectoryID += 16;
Q_ComposeFileName( pMakefileGamePath, pDefaultDirectoryID, pFullPath, nBufLen );
Q_RemoveDotSlashes( pFullPath );
return true;
}
// Assume it's a content subdir
GetModContentSubdirectory( pDefaultDirectoryID, pFullPath, nBufLen );
Q_RemoveDotSlashes( pFullPath );
return true;
}
//-----------------------------------------------------------------------------
// Relative path to full path
//-----------------------------------------------------------------------------
void CDmeMakefile::RelativePathToFullPath( const char *pRelativePath, char *pFullPath, int nBufLen )
{
if ( !pRelativePath[0] )
{
pFullPath[0] = 0;
return;
}
char pRootDir[ MAX_PATH ];
GetMakefilePath( pRootDir, sizeof(pRootDir) );
Q_ComposeFileName( pRootDir, pRelativePath, pFullPath, nBufLen );
Q_RemoveDotSlashes( pFullPath );
}
//-----------------------------------------------------------------------------
// Fullpath to relative path
//-----------------------------------------------------------------------------
void CDmeMakefile::FullPathToRelativePath( const char *pFullPath, char *pRelativePath, int nBufLen )
{
if ( !pFullPath[0] )
{
pRelativePath[0] = 0;
return;
}
char pRootDir[ MAX_PATH ];
GetMakefilePath( pRootDir, sizeof(pRootDir) );
if ( pRootDir[0] )
{
Q_MakeRelativePath( pFullPath, pRootDir, pRelativePath, nBufLen );
}
else
{
Q_strncpy( pRelativePath, pFullPath, nBufLen );
Q_FixSlashes( pRelativePath );
}
}
//-----------------------------------------------------------------------------
// Adds a single source
//-----------------------------------------------------------------------------
CDmeSource *CDmeMakefile::AddSource( const char *pSourceType, const char *pFullPath )
{
if ( pFullPath[0] && FindSource( pSourceType, pFullPath ) )
{
Warning( "Attempted to add the same source twice %s!\n", pFullPath );
return NULL;
}
CDmElement *pElement = GetElement< CDmElement >( g_pDataModel->CreateElement( pSourceType, "", GetFileId() ) );
CDmeSource *pSource = CastElement< CDmeSource >( pElement );
Assert( pSource );
if ( !pSource )
{
Warning( "Invalid source type name %s!\n", pSourceType );
if ( pElement )
{
DestroyElement( pElement );
}
return NULL;
}
char pRelativePath[MAX_PATH];
FullPathToRelativePath( pFullPath, pRelativePath, sizeof( pRelativePath ) );
pSource->SetRelativeFileName( pRelativePath );
m_Sources.AddToTail( pSource );
return pSource;
}
//-----------------------------------------------------------------------------
// Removes a single source
//-----------------------------------------------------------------------------
CDmeSource *CDmeMakefile::FindSource( const char *pSourceType, const char *pFullPath )
{
char pRelativePath[MAX_PATH];
FullPathToRelativePath( pFullPath, pRelativePath, sizeof( pRelativePath ) );
int nCount = m_Sources.Count();
for ( int i = 0; i < nCount; ++i )
{
if ( Q_stricmp( pSourceType, m_Sources[i]->GetTypeString() ) )
continue;
if ( !Q_stricmp( pRelativePath, m_Sources[i]->GetRelativeFileName() ) )
return m_Sources[i];
}
return NULL;
}
//-----------------------------------------------------------------------------
// Sets a source to be a single source
//-----------------------------------------------------------------------------
CDmeSource *CDmeMakefile::SetSingleSource( const char *pSourceType, const char *pFullPath )
{
// FIXME: we maybe shouldn't remove everything if the source can't be created for some reason?
RemoveAllSources( pSourceType );
return AddSource( pSourceType, pFullPath );
}
//-----------------------------------------------------------------------------
// Changes a source
//-----------------------------------------------------------------------------
void CDmeMakefile::SetSourceFullPath( CDmeSource *pSource, const char *pFullPath )
{
char pRelativePath[MAX_PATH];
FullPathToRelativePath( pFullPath, pRelativePath, sizeof( pRelativePath ) );
if ( Q_stricmp( pRelativePath, pSource->GetRelativeFileName() ) )
{
pSource->SetRelativeFileName( pRelativePath );
// FIXME: Should we delete the dependent makefile?
pSource->SetDependentMakefile( NULL );
}
}
//-----------------------------------------------------------------------------
// Returns the full path of a source
//-----------------------------------------------------------------------------
void CDmeMakefile::GetSourceFullPath( CDmeSource *pSource, char *pFullPath, int nBufLen )
{
const char *pRelativePath = pSource->GetRelativeFileName( );
RelativePathToFullPath( pRelativePath, pFullPath, nBufLen );
}
//-----------------------------------------------------------------------------
// Returns a list of sources
//-----------------------------------------------------------------------------
void CDmeMakefile::GetSources( const char *pSourceType, CUtlVector< CDmeHandle< CDmeSource > > &sources )
{
int nCount = m_Sources.Count();
sources.EnsureCapacity( nCount );
for ( int i = 0; i < nCount; ++i )
{
if ( m_Sources[i]->IsA( pSourceType ) )
{
int j = sources.AddToTail();
sources[j] = m_Sources[i];
}
}
}
//-----------------------------------------------------------------------------
// Gets a list of all sources, regardless of type
//-----------------------------------------------------------------------------
int CDmeMakefile::GetSourceCount()
{
return m_Sources.Count();
}
CDmeSource *CDmeMakefile::GetSource( int nIndex )
{
return m_Sources[nIndex];
}
//-----------------------------------------------------------------------------
// Removes a single source
//-----------------------------------------------------------------------------
void CDmeMakefile::RemoveSource( CDmeSource *pSource )
{
int nCount = m_Sources.Count();
for ( int i = 0; i < nCount; ++i )
{
if ( m_Sources[i] == pSource )
{
m_Sources.Remove( i );
break;
}
}
}
void CDmeMakefile::RemoveSource( const char *pSourceType, const char *pFullPath )
{
char pRelativePath[MAX_PATH];
FullPathToRelativePath( pFullPath, pRelativePath, sizeof( pRelativePath ) );
int nCount = m_Sources.Count();
for ( int i = 0; i < nCount; ++i )
{
if ( Q_stricmp( pSourceType, m_Sources[i]->GetTypeString() ) )
continue;
if ( !Q_stricmp( pRelativePath, m_Sources[i]->GetRelativeFileName() ) )
{
m_Sources.Remove( i );
break;
}
}
}
//-----------------------------------------------------------------------------
// Removes all sources of a particular type
//-----------------------------------------------------------------------------
void CDmeMakefile::RemoveAllSources( const char *pSourceType )
{
int nCount = m_Sources.Count();
for ( int i = nCount; --i >= 0; )
{
if ( !Q_stricmp( pSourceType, m_Sources[i]->GetTypeString() ) )
{
// NOTE: This works because we're iterating backward
m_Sources.Remove( i );
}
}
}
//-----------------------------------------------------------------------------
// Source iteration
//-----------------------------------------------------------------------------
bool CDmeMakefile::HasSourceOfType( const char *pSourceType )
{
int nCount = m_Sources.Count();
for ( int i = nCount; --i >= 0; )
{
if ( !Q_stricmp( pSourceType, m_Sources[i]->GetTypeString() ) )
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Updates the source names to be relative to a particular path
//-----------------------------------------------------------------------------
bool CDmeMakefile::UpdateSourceNames( const char *pOldRootDir, const char *pNewRootDir, bool bApplyChanges )
{
char pOldSourcePath[ MAX_PATH ];
char pNewSourcePath[ MAX_PATH ];
int nCount = m_Sources.Count();
for ( int i = 0; i < nCount; ++i )
{
const char *pOldRelativePath = m_Sources[i]->GetRelativeFileName();
if ( pOldRelativePath[0] )
{
Q_ComposeFileName( pOldRootDir, pOldRelativePath, pOldSourcePath, sizeof(pOldSourcePath) );
Q_RemoveDotSlashes( pOldSourcePath );
if ( !Q_MakeRelativePath( pOldSourcePath, pNewRootDir, pNewSourcePath, sizeof(pNewSourcePath) ) )
{
Assert( !bApplyChanges );
return false;
}
}
else
{
pNewSourcePath[0] = 0;
}
if ( !bApplyChanges )
continue;
m_Sources[i]->SetRelativeFileName( pNewSourcePath );
}
return true;
}
//-----------------------------------------------------------------------------
// Returns the filename
//-----------------------------------------------------------------------------
const char *CDmeMakefile::GetFileName() const
{
DmFileId_t fileId = GetFileId();
return g_pDataModel->GetFileName( fileId );
}
//-----------------------------------------------------------------------------
// Call this to change the file the makefile is stored in
// Will make all sources be relative to this path
//-----------------------------------------------------------------------------
bool CDmeMakefile::SetFileName( const char *pFileName )
{
if ( !Q_IsAbsolutePath( pFileName ) )
return false;
char pOldRootDir[ MAX_PATH ];
char pNewRootDir[ MAX_PATH ];
GetMakefilePath( pOldRootDir, sizeof(pOldRootDir) );
Q_ExtractFilePath( pFileName, pNewRootDir, sizeof(pNewRootDir) );
// Gotta do this twice; once to check for validity, once to actually do it
if ( !UpdateSourceNames( pOldRootDir, pNewRootDir, false ) )
return false;
UpdateSourceNames( pOldRootDir, pNewRootDir, true );
DmFileId_t fileId = GetFileId();
if ( fileId == DMFILEID_INVALID )
{
fileId = g_pDataModel->FindOrCreateFileId( pFileName );
SetFileId( fileId, TD_DEEP );
}
else
{
g_pDataModel->SetFileName( fileId, pFileName );
}
return true;
}
//-----------------------------------------------------------------------------
// Make all outputs writeable
//-----------------------------------------------------------------------------
void CDmeMakefile::MakeOutputsWriteable( )
{
// When we publish, we'll check them out.
CUtlVector<CUtlString> outputs;
GetOutputs( outputs );
int nCount = outputs.Count();
for ( int i = 0; i < nCount; ++i )
{
g_pFullFileSystem->SetFileWritable( outputs[i], true );
}
}
//-----------------------------------------------------------------------------
// Sets a makefile/source association
//-----------------------------------------------------------------------------
void CDmeMakefile::SetAssociation( CDmeSource *pSource, CDmeMakefile *pSourceMakefile )
{
if ( !pSource )
return;
int nCount = m_Sources.Count();
for ( int i = 0; i < nCount; ++i )
{
if ( m_Sources[i] != pSource )
continue;
CDmeMakefile *pDependentMakeFile = m_Sources[i]->GetDependentMakefile();
if ( pSourceMakefile != pDependentMakeFile )
{
// FIXME: Should I recursively delete pDependentMakeFile ?
m_Sources[i]->SetDependentMakefile( pSourceMakefile );
}
return;
}
}
//-----------------------------------------------------------------------------
// Finds a dependent makefile
//-----------------------------------------------------------------------------
CDmeMakefile *CDmeMakefile::FindDependentMakefile( CDmeSource *pSource )
{
if ( !pSource )
return NULL;
int nCount = m_Sources.Count();
for ( int i = 0; i < nCount; ++i )
{
if ( m_Sources[i] == pSource )
return m_Sources[i]->GetDependentMakefile();
}
return NULL;
}
//-----------------------------------------------------------------------------
// Finds the associated source
//-----------------------------------------------------------------------------
CDmeSource *CDmeMakefile::FindAssociatedSource( CDmeMakefile *pChildMakefile )
{
if ( !pChildMakefile )
return NULL;
int nCount = m_Sources.Count();
for ( int i = 0; i < nCount; ++i )
{
if ( m_Sources[i]->GetDependentMakefile() == pChildMakefile )
return m_Sources[i];
}
return NULL;
}