aboutsummaryrefslogtreecommitdiff
path: root/tools/mhmake/src/fileinfo.cpp
blob: d63724882202753d9c3603f86dfee2e074d3f7f8 (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
/*  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$ */

#include "stdafx.h"

#include "fileinfo.h"
#include "rule.h"
#include "util.h"
#include "mhmakeparser.h"

#ifndef S_ISDIR
#define S_ISDIR(val) ((val)&_S_IFDIR)
#endif

const string NullString;
refptr<fileinfo> NullFileInfo;

///////////////////////////////////////////////////////////////////////////////
string QuoteFileName(const string &Filename)
{
  string Ret(Filename);
#if OSPATHSEP=='\\'
  /* Put quotes around the string if there are spaces in it */
  if (Ret.find_first_of(' ')!=string::npos && Ret[0]!='"')
  {
    Ret=g_QuoteString+Ret+g_QuoteString;
  }
#else
  int Pos=0;
  /* Escape the spaces with a backslash */
  while ((Pos=Ret.find_first_of(' ',Pos))!=string::npos)
  {
    Ret=Ret.replace(Pos,1,"\\ ");
    Pos+=2;
  }
#endif
  return Ret;
}

///////////////////////////////////////////////////////////////////////////////
string UnquoteFileName(const string &Filename)
{
  size_t Pos=0;
  string Name(Filename);
#if OSPATHSEP=='\\'
  /* Remove all the quotes from the filename */
  while ((Pos=Name.find_first_of('"',Pos))!=string::npos)
  {
    Name=Name.replace(Pos,1,"");
  }
#else
  /* Remove the escaped spaces */
  while ((Pos=Name.find_first_of("\\",Pos))!=string::npos)
  {
    if (Name[Pos+1]==' ')
      Name=Name.replace(Pos,2," ");
    Pos+=1;
  }
#endif
  return Name;
}

///////////////////////////////////////////////////////////////////////////////
refptr<fileinfo> fileinfo::GetDir() const
{
  return GetAbsFileInfo(m_AbsFileName.substr(0,m_AbsFileName.find_last_of(OSPATHSEP)));
}

///////////////////////////////////////////////////////////////////////////////
string fileinfo::GetName() const
{
  return m_AbsFileName.substr(m_AbsFileName.find_last_of(OSPATHSEP)+1);
}

///////////////////////////////////////////////////////////////////////////////
mh_time_t fileinfo::realGetDate()
{
  struct stat Buf;
  if (-1==stat(m_AbsFileName.c_str(),&Buf))
    m_Date.SetNotExist();
  else if (S_ISDIR(Buf.st_mode))
    m_Date.SetDir();
  else
    m_Date=Buf.st_mtime;
  return m_Date;
}

///////////////////////////////////////////////////////////////////////////////
bool fileinfo::IsDir() const
{
#ifdef WIN32
  WIN32_FIND_DATA FindData;
  HANDLE hFind=FindFirstFile(m_AbsFileName.c_str(),&FindData);
  if (hFind==INVALID_HANDLE_VALUE)
  {
    return false;
  }
  else
  {
    FindClose(hFind);
    if (FindData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
#else
  struct stat Buf;
  if (-1==stat(m_AbsFileName.c_str(),&Buf))
    return false; // File does not exist, so consider this as not a directory
  else
    return 0!=S_ISDIR (Buf.st_mode);
  return true;
#endif
}

///////////////////////////////////////////////////////////////////////////////
void fileinfo::SetDateToNow()
{
  m_Date=time(NULL);
}

///////////////////////////////////////////////////////////////////////////////
string fileinfo::GetPrerequisits() const
{
  // Build a string with all prerequisits, but make sure that every dependency
  // is only in there once (we do this be building a set in parallel
  vector< refptr<fileinfo> >::const_iterator DepIt=m_Deps.begin();
  deps_t Deps;
  bool first=true;
  string Ret=g_EmptyString;
  while (DepIt!=m_Deps.end())
  {
    deps_t::iterator pFound=Deps.find(*DepIt);
    if (pFound==Deps.end())
    {
      if (first)
      {
        first=false;
      }
      else
      {
        Ret+=g_SpaceString;
      }
      Ret+=(*DepIt)->GetQuotedFullFileName();
    }
    Deps.insert(*DepIt);
    DepIt++;
  }
  return Ret;
}

///////////////////////////////////////////////////////////////////////////////
void fileinfo::AddDeps(vector< refptr<fileinfo> > &Deps)
{
  vector< refptr<fileinfo> >::iterator It=Deps.begin();
  vector< refptr<fileinfo> >::iterator ItEnd=Deps.end();
  while (It!=ItEnd)
  {
    AddDep(*It++);
  }
}

///////////////////////////////////////////////////////////////////////////////
bool fileinfo::IsAutoDepExtention(void) const
{
  const char *pName=GetFullFileName().c_str();
  const char *pExt=strrchr(pName,'.');
  if (!pExt)
    return false;
  pExt++;
  if (m_pRule)
  {
    string ObjExt=m_pRule->GetMakefile()->ExpandVar(OBJEXTVAR);
    return ((0==strcmp(pExt,ObjExt.c_str()+1)) || (0==strcmp(pExt,"h")));
  }
  else
    return ((0==strcmp(pExt,"obj")) || (0==strcmp(pExt,"doj")) || (0==strcmp(pExt,"o")) || (0==strcmp(pExt,"h")));
}

#ifdef _DEBUG
///////////////////////////////////////////////////////////////////////////////
string fileinfo::GetErrorMessageDuplicateRule(const refptr<rule>&pRule)
{
  string Ret;
  Ret = GetQuotedFullFileName() + ": rule is defined multiple times\n";
  Ret += "First (" + m_pRule->GetMakefile()->GetMakeDir()->GetQuotedFullFileName() + ") :\n";

  vector<string>::const_iterator It=m_pRule->GetCommands().begin();
  while (It!=m_pRule->GetCommands().end())
  {
    Ret+= "    " + m_pRule->GetMakefile()->ExpandExpression(*It) + "\n";
    It++;
  }
  Ret += "Second (" + pRule->GetMakefile()->GetMakeDir()->GetQuotedFullFileName() + ") :\n";
  It=pRule->GetCommands().begin();
  while (It!=pRule->GetCommands().end())
  {
    Ret += "    " + pRule->GetMakefile()->ExpandExpression(*It) +"\n";
    It++;
  }
  return Ret;
}
#endif

///////////////////////////////////////////////////////////////////////////////
string &NormalizePathName(string &Name)
{
  const char *pPtr=Name.c_str();
  const char *pBeg=pPtr;
  const char *pLastSlash=NULL;
  char *pWr=(char*)pBeg;
  char Char=*pPtr++;

  while (Char)
  {
    if (Char=='\\' || Char=='/')
    {
      char Char2=pPtr[0];
      if (Char2=='.')
      {
        if (pPtr[1]=='.')
        {
          pPtr+=2;
          while ((pPtr[0]=='\\' || pPtr[0]=='/')  && pPtr[1]=='.' && pPtr[2]=='.')
          {
            pLastSlash--;
            while (*pLastSlash!='\\' && *pLastSlash!='/') pLastSlash--;
            if (pLastSlash<pBeg)
              pLastSlash=pBeg;  // This is a fault in the file name, just put it back at the beginning
            pPtr+=3;
          }
          if (pLastSlash)
            pWr=(char*)pLastSlash;
        }
        else
        {
          if (pPtr[1]=='\\' || pPtr[1]=='/')
          {
            pPtr++;
          }
          else
          {
            pLastSlash=pWr;
            *pWr++ = OSPATHSEP;
          }
        }
      }
      else if (Char2=='\\' || Char2=='/')
      {
      }
      else
      {
        pLastSlash=pWr;
        *pWr++ = OSPATHSEP;
      }
    }
    else
    {
      #ifdef WIN32
      *pWr++ = tolower(Char);
      #else
      *pWr++ = Char;
      #endif
    }
    Char=*pPtr++;
  }
  *pWr=0;
  Name.resize(pWr-pBeg);

  return Name;
}


///////////////////////////////////////////////////////////////////////////////
const refptr<fileinfo> &GetFileInfo(const string &NameIn,const refptr<fileinfo> &RelDir)
{
  string Name=UnquoteFileName(NameIn);
  bool DoesExist=true;

  //Only concatenate if szName is not already a full name
#ifdef WIN32
  if (!Name.empty() && (Name.length()==1 || Name[1]!=':'))
#endif
  {
    if (Name[0]!=OSPATHSEP)
    {
      Name=RelDir->GetFullFileName()+OSPATHSEPSTR+Name;
      if (!RelDir->Exists())  /* if the directory does not exist, the file will not exist either */
        DoesExist=false;
    }
    #ifdef WIN32
    else
    {
      /* The filename is absolute but does not contain a driver letter. So add it (only on windows) */
      Name=RelDir->GetFullFileName().substr(0,2)+Name;
    }
    #endif
  }
  const refptr<fileinfo> &Ret=GetAbsFileInfo(NormalizePathName(Name));
  if (!DoesExist)
    Ret->SetNotExist();
  return Ret;
}

#ifdef _DEBUG
///////////////////////////////////////////////////////////////////////////////
void PrintFileInfos()
{
  set<refptr<fileinfo>,less_refptrfileinfo>::iterator pIt=g_FileInfos.begin();
  while (pIt!=g_FileInfos.end())
  {
    cout<<(*pIt)->GetQuotedFullFileName()<<" :";
    if ((*pIt)->IsPhony())
      cout<<" (phony)";
    vector< refptr<fileinfo> > &Deps=(*pIt)->GetDeps();
    vector< refptr<fileinfo> >::iterator pDepIt=Deps.begin();
    while (pDepIt!=Deps.end())
    {
      cout<<g_SpaceString<<(*pDepIt)->GetQuotedFullFileName();
      pDepIt++;
    }
    cout<<endl;
    // Write the commands
    refptr<rule> pRule=(*pIt)->GetRule();
    if (pRule)
    {
      cout<<g_SpaceString<<"Run in: "<<pRule->GetMakefile()->GetMakeDir()->GetQuotedFullFileName()<<endl;
      pRule->PrintCommands();
    }
    pIt++;
  }

}
#endif