aboutsummaryrefslogtreecommitdiff
path: root/tools/mhmake/src/fileinfo.h
blob: 9be40bdbe74719b9f10a8c6240931f03ce2fc341 (plain)
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
/*  This file is part of mhmake.
 *
 *  Copyright (C) 2001-2009 Marc Haesen
 *
 *  Mhmake is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Mhmake is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Mhmake.  If not, see <http://www.gnu.org/licenses/>.
*/

/* $Rev$ */

#ifndef __FILEINFO_H
#define __FILEINFO_H

#include "curdir.h"
#include "rule.h"
#include "md5.h"

#ifdef WIN32
#define OSPATHSEP '\\'
#define OSPATHSEPSTR "\\"
#define OSPATHENVSEP      ';'
#define OSPATHENVSEPSTR   ";"
#else
#define OSPATHSEP '/'
#define OSPATHSEPSTR "/"
#define OSPATHENVSEP      ':'
#define OSPATHENVSEPSTR   ":"
#endif

extern bool g_DumpOnError;
extern bool g_PrintVarsAndRules;
extern bool g_DoNotExecute;
extern bool g_GenProjectTree;
extern bool g_Quiet;
extern bool g_RebuildAll;
extern bool g_PrintAdditionalInfo;
extern bool g_pPrintDependencyCheck;
extern bool g_CheckCircularDeps;
extern bool g_ForceAutoDepRescan;
extern bool g_PrintLexYacc;
extern bool g_Clean;
extern bool g_StopCompiling;
extern bool g_PrintMultipleDefinedRules;

extern const string g_EmptyString;
extern const string g_SpaceString;
extern const string g_QuoteString;

string QuoteFileName(const string &Filename);
string UnquoteFileName(const string &Filename);

template<typename T>
inline string stringify(const T& x)
{
  ostringstream o;
  o << x;
  return o.str();
}

#define TIMESAFETY 3
class mh_time
{
  enum
  {
    DATENOTVALID=0,
    NOTEXISTTIME=1,
    DIRTIME     =2+TIMESAFETY
  };
  unsigned long m_Time;

  bool operator < (const mh_time &Src);
public:
  mh_time(){m_Time=DATENOTVALID;}
#ifdef WIN32
  mh_time(unsigned __int64 Time) : m_Time((unsigned long)(Time&0xffffffff)) {}
  mh_time(__int64 Time) : m_Time((unsigned long)(Time&0xffffffff)) {}
#endif
  mh_time(unsigned long Time) : m_Time(Time) {}
  mh_time(const mh_time &Time) : m_Time(Time.m_Time) {}

  void SetDir(void)
  {
    m_Time=DIRTIME;
  }
  bool IsDir(void) const
  {
    return m_Time==DIRTIME;
  }
  void SetNotExist(void)
  {
    m_Time=NOTEXISTTIME;
  }
  bool IsExistingFile(void) const
  {
    return m_Time>DIRTIME;
  }
  bool DoesExist(void) const
  {
    return m_Time!=NOTEXISTTIME;
  }
  void Invalidate(void)
  {
    m_Time=DATENOTVALID;
  }
  bool IsDateValid(void) const
  {
    return m_Time!=DATENOTVALID;
  }
  friend ostream& operator<<(ostream& out,const mh_time &Src);

  mh_time &operator = (const mh_time &Src) { m_Time=Src.m_Time; return *this;}

  bool IsOlder(const mh_time &Src) const { return m_Time<Src.m_Time-TIMESAFETY; }

  bool IsNewer(const mh_time &Src) const { return m_Time>Src.m_Time+TIMESAFETY; }
  bool IsNewerOrSame(const mh_time &Src) const { return m_Time>=Src.m_Time-TIMESAFETY; }
};
typedef mh_time mh_time_t;

inline ostream& operator<<(ostream& out,const mh_time &Src)
{
  out << hex << (unsigned long)Src.m_Time;
  return out;
}

class fileinfo : public refbase
{
  string m_AbsFileName;
  bool m_IsPhony;
  bool m_IsBuild;
  refptr<rule> m_pRule;
  vector< refptr<fileinfo> > m_Deps;
  mh_time_t m_Date;
  uint32 m_CommandsMd5_32;  // 32-bit Md5 checksum of commands to build this target

  fileinfo(const fileinfo &Src);
  fileinfo(void);
public:

  fileinfo(const string &AbsFileName,uint32 Md5_32)
  {
    m_IsPhony=false;
    m_IsBuild=false;
    m_AbsFileName=UnquoteFileName(AbsFileName);
    InvalidateDate();
    m_CommandsMd5_32=Md5_32;
    #ifdef _DEBUG
    if (g_PrintAdditionalInfo)
      cout << "Initialising Md5 of "<<GetQuotedFullFileName().c_str()<<" to 0x"<<hex<<Md5_32<<endl;
    #endif
  }
  fileinfo(const string &AbsFileName)
  {
    new (this) fileinfo(AbsFileName,0);
  }
  /* The following constructor is only used for name comparisons, and should only be used for that */
  fileinfo(int Dummy,const string &AbsFileName)
  {
    m_AbsFileName=UnquoteFileName(AbsFileName);
  }

  fileinfo(const char *szFile)
  {
    new (this) fileinfo(string(szFile));
  }
  fileinfo(const char *szFile,uint32 Md5_32)
  {
    new (this) fileinfo(string(szFile),Md5_32);
  }
  ~fileinfo()
  {
  }
  const string &GetFullFileName(void) const
  {
    return m_AbsFileName;
  }
  string GetQuotedFullFileName(void) const
  {
    return QuoteFileName(m_AbsFileName);
  }
  void SetFullFileName(const string &strAbsName)
  {
    m_AbsFileName=UnquoteFileName(strAbsName);
    // If the last char is path sep strip it
    if (!m_AbsFileName.empty() && m_AbsFileName[m_AbsFileName.length()-1]==OSPATHSEP)
      m_AbsFileName.resize(m_AbsFileName.length()-1);
  }
  fileinfo &operator = (const fileinfo &Src)
  {
    new (this) fileinfo(Src);
    return *this;
  }

  refptr<fileinfo> GetDir(void) const;
  string GetName() const;
  bool IsDir() const;

  string GetErrorMessageDuplicateRule(const refptr<rule> &pRule);

  void SetRule(refptr<rule> &pRule)
  {
    #if defined(_DEBUG) && defined(_MSC_VER)
    if (m_pRule && m_pRule->GetCommands().size()) {
      _asm int 3;
    }
    #endif
    m_pRule=pRule;
  }

  void SetRuleIfNotExist(refptr<rule> &pRule)
  {
    if (pRule)
    {
      if (!m_pRule)
      {
        SetRule(pRule);
        pRule->AddTarget(this);
      }
      #ifdef _DEBUG
      else
      {
        if (*m_pRule!=*pRule)
        {
          throw(GetErrorMessageDuplicateRule(pRule));
        }
        else if (g_PrintMultipleDefinedRules)
        {
          cerr<<GetErrorMessageDuplicateRule(pRule);
        }
      }
      #endif
    }
  }

  refptr<rule> GetRule(void)
  {
    return m_pRule;
  }
  void AddDep(const refptr<fileinfo> &Dep)
  {
    if (&*Dep==this)
    {
      #ifdef _DEBUG
      cout << GetQuotedFullFileName()<<" is directly dependent on itself\n";
      #endif
      return;
    }
    m_Deps.push_back(Dep);
  }
  void AddDeps(vector< refptr<fileinfo> > &Deps);

  void InsertDeps(vector< refptr<fileinfo> > &Deps)
  {
    vector< refptr<fileinfo> > NewDeps;
    vector< refptr<fileinfo> >::const_iterator It=Deps.begin();
    vector< refptr<fileinfo> >::const_iterator ItEnd=Deps.end();
    while (It!=ItEnd)
    {
      if (&**It==this)
      {
        #ifdef _DEBUG
        cout << GetQuotedFullFileName()<<" is directly dependent on itself\n";
        #endif
      }
      else
        NewDeps.push_back(*It);
      It++;
    }
    if (NewDeps.size())
      m_Deps.insert(m_Deps.begin(),NewDeps.begin(),NewDeps.end());
  }
  void AddMainDep(refptr<fileinfo> &MainDep)
  {
    if (&*MainDep==this)
    {
      #ifdef _DEBUG
      cout << GetQuotedFullFileName()<<" is directly dependent on itself\n";
      #endif
      return;
    }
    m_Deps.insert(m_Deps.begin(),MainDep);
  }
  vector< refptr<fileinfo> > &GetDeps(void)
  {
    return m_Deps;
  }
  string GetPrerequisits(void) const;
  void SetPhony(void)
  {
    m_IsPhony=true;
    m_Date.SetNotExist(); // This will sure that this target will always be build (even if a corresponding file exists)
  }
  bool IsPhony(void)
  {
    return m_IsPhony;
  }
  mh_time_t realGetDate(void);
  void SetDateToNow(void);

  void SetDate(mh_time_t Date)
  {
    m_Date=Date;
  }

  bool IsDateValid() const
  {
    return m_Date.IsDateValid();
  }
  void InvalidateDate(void)
  {
    m_Date.Invalidate();
  }

  mh_time_t GetDate(void)
  {
    if (m_Date.IsDateValid())
      return m_Date;
    else
      return realGetDate();
  }
  void SetNotExist(void)
  {  // this is used to make sure that this item is rebuild, even if it really exists
    m_Date.SetNotExist();
  }
  bool Exists(void)
  {
    return GetDate().DoesExist();
  }
  bool IsBuild(void) const
  {
    return m_IsBuild;
  }
  void SetBuild(void)
  {
    m_IsBuild=true;
  }
  void ClearBuild(void)
  {
    m_IsBuild=false;
  }
  bool IsAutoDepExtention(void) const;

  void SetCommandsMd5_32(uint32 Md5_32)
  {
    #ifdef _DEBUG
    if (g_PrintAdditionalInfo)
      cout << "Setting Md5 of "<<GetQuotedFullFileName()<<" to 0x"<<hex<<Md5_32<<endl;
    #endif
    m_CommandsMd5_32=Md5_32;
  }
#ifdef _DEBUG
  uint32 GetCommandsMd5_32(void) const
  {
    return m_CommandsMd5_32;
  }
#endif
  bool CompareMd5_32(uint32 Md5_32) const
  {
    return m_CommandsMd5_32==Md5_32;
  }
  void WriteMd5_32(FILE *pFile) const
  {
    fwrite(&m_CommandsMd5_32,sizeof(m_CommandsMd5_32),1,pFile);
  }
};

struct less_refptrfileinfo : public binary_function <refptr<fileinfo>, refptr<fileinfo>, bool>
{
  bool operator()(const refptr<fileinfo>& _Left, const refptr<fileinfo>& _Right) const
  {
    return less<string>().operator ()(_Left->GetFullFileName(),_Right->GetFullFileName());
  }
};

struct less_fileinfo : public binary_function <const fileinfo*, const fileinfo*, bool>
{
  bool operator()(const fileinfo *_Left, const fileinfo *_Right) const
  {
    return less<string>().operator ()(_Left->GetFullFileName(),_Right->GetFullFileName());
  }
};

extern const string NullString;
extern refptr<fileinfo> NullFileInfo;

const refptr<fileinfo> &GetFileInfo(const string &szName,const refptr<fileinfo> &pRelDir=curdir::GetCurDir());

extern set<refptr<fileinfo>,less_refptrfileinfo > g_FileInfos;

inline const refptr<fileinfo> &GetAbsFileInfo(const string &strAbsName)
{
  static refptr<fileinfo> SearchFileInfo(new fileinfo(""));
  SearchFileInfo->SetFullFileName(strAbsName);
  /* Using find is just an optimalisation, you could use insert immediately */
  set<refptr<fileinfo>,less_refptrfileinfo >::const_iterator pFind=g_FileInfos.find(SearchFileInfo);
  if (pFind==g_FileInfos.end())
  {
    pair <set<refptr<fileinfo>,less_refptrfileinfo >::iterator, bool> pPair=g_FileInfos.insert(new fileinfo(SearchFileInfo->GetFullFileName()));
    return *(pPair.first);
  }
  else
    return *pFind;
}


inline const refptr<fileinfo> &GetFileInfo(const string &szName,const string &RelDir)
{
  return GetFileInfo(szName,GetFileInfo(RelDir));
}

inline const refptr<fileinfo> &GetFileInfo(const char *szName,const char *RelDir)
{
  return GetFileInfo(string(szName),string(RelDir));
}

inline const refptr<fileinfo> &GetFileInfo(const char *szName,const refptr<fileinfo> &RelDir=curdir::GetCurDir())
{
  return GetFileInfo(string(szName),RelDir);
}

void PrintFileInfos();

#ifdef WIN32
class ZEROTIME
{
  __int64 m_ZeroTime;
public:
  ZEROTIME()
  {
    SYSTEMTIME SystemTime;

    memset(&SystemTime,0,sizeof(SystemTime));
    SystemTime.wYear=1970;
    SystemTime.wMonth=1;
    SystemTime.wDay=1;
    SystemTime.wDayOfWeek=4;

    SystemTimeToFileTime(&SystemTime,(FILETIME*)&m_ZeroTime);
  }

  __int64 GetZeroTime()
  {
    return m_ZeroTime;
  }

  mh_time_t ConvertTime(FILETIME *pFileTime)
  {
    return (mh_time_t)((*(__int64*)pFileTime-m_ZeroTime)/10000000);  /* filetime is in nano seconds*/
  }
};

extern ZEROTIME g_ZeroTime;
#endif

#endif