aboutsummaryrefslogtreecommitdiff
path: root/tools/mhmake/src/mhmake.cpp
blob: 7e9b117b2e284f20e8b7f4e0a08dd87fc0d9bb37 (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
/*  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 "mhmakeparser.h"
#include "rule.h"
#include "util.h"

bool g_Clean=false;

int __CDECL main(int argc, char* argv[])
{
  //__int64 Freq;
  //__int64 Start;

  //QueryPerformanceFrequency((LARGE_INTEGER*)&Freq);
  //QueryPerformanceCounter((LARGE_INTEGER*)&Start);

  #if defined(_DEBUG) && defined(_MSC_VER)
  int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );

  // Turn on leak-checking bit
  tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
  // Turn on defer freeing
  //tmpFlag |= _CRTDBG_DELAY_FREE_MEM_DF;
  // Turn on heap checking
  //tmpFlag |= _CRTDBG_CHECK_ALWAYS_DF;

  // Set flag to the new value
  _CrtSetDbgFlag( tmpFlag );

  //_CrtSetBreakAlloc(44);
  #endif

  try
  {
    mhmakefileparser::InitBuildTime();

    putenv("PLATFORM="PLATFORM);

    vector<string> CmdLineArgs;
    for (int i=1; i<argc; i++)
    {
      CmdLineArgs.push_back(argv[i]);
    }

    refptr<loadedmakefile> pFirstMakefile(new loadedmakefile(CmdLineArgs,"makefile"));
    // For the first makefile we add the defines passed on the command line to the
    // environment so that the other load_makefile see the same variables
    pFirstMakefile->AddCommandLineVarsToEnvironment();

    g_LoadedMakefiles.push_back(pFirstMakefile);

    pFirstMakefile->LoadMakefile();

    #ifdef _DEBUG
    if (g_PrintVarsAndRules)
    {
      DumpVarsAndRules();
    }
    #endif

    // Make sure that the included makefiles that have rules are build

    LOADEDMAKEFILES::iterator LoadMakIt=g_LoadedMakefiles.begin();
    while (LoadMakIt!=g_LoadedMakefiles.end())
    {
      (*LoadMakIt)->m_pParser->BuildIncludedMakefiles();
      (*LoadMakIt)->m_pParser->ParseBuildedIncludeFiles();
      LoadMakIt++;
    }

    if (pFirstMakefile->m_CommandLineTargets.size())
    {
      vector<string>::iterator It=pFirstMakefile->m_CommandLineTargets.begin();
      while (It!=pFirstMakefile->m_CommandLineTargets.end())
      {
        if (*It=="clean" || *It=="cleanall")
        {
          g_Clean=true;
        }
        pFirstMakefile->m_pParser->BuildTarget(GetFileInfo(*It,pFirstMakefile->m_MakeDir));
        It++;
      }
    }
    else
    {
      refptr<fileinfo> FirstTarget=pFirstMakefile->m_pParser->GetFirstTarget();
      if (FirstTarget)
        pFirstMakefile->m_pParser->BuildTarget(FirstTarget);
      else
        cout << "Warning: no targets in makefile. Nothing to be build.\nMHMAKECONF defined?\n";
    }

  }
  catch (int Error)
  {
    printf("Error occured %d\n",Error);
    #ifdef _DEBUG
    if (g_DumpOnError)
    {
      DumpVarsAndRules();
    }
    #endif
    return 1;
  }

  //__int64 Stop;
  //QueryPerformanceCounter((LARGE_INTEGER*)&Stop);
  //cout << (Stop-Start)*1000/Freq << endl;

  return 0;
}