aboutsummaryrefslogtreecommitdiff
path: root/tools/mhmake/src/mhmake.cpp
blob: db9a1368296bbf0e0658e3e29527145bf82f22d8 (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
/*  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;
bool g_StopCompiling=false;

#ifdef WIN32
BOOL WINAPI ControlCHandler(DWORD dwCtrlType)
{
  g_StopCompiling=true;
  return TRUE;
}
#endif

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

  #ifdef WIN32
  /* Remove the VS_UNICODE_OUTPUT environment variable. This variable is set when running from
   * the Visual Studio IDE and is causing the output of cl.exe to send the output directly to the IDE instead
   * of sending it to stdout. This is causing all scripts that are calling cl.exe and intercept the
   * output to fail.
   */
  putenv("VS_UNICODE_OUTPUT=");

  SetConsoleCtrlHandler(ControlCHandler, TRUE);

  #endif

  try
  {
    mhmakefileparser::InitBuildTime();

    char PlatformEnv[]="PLATFORM="PLATFORM;
    putenv(PlatformEnv);

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

    refptr<loadedmakefile> pFirstMakefile(new loadedmakefile(curdir::GetCurDir(),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)->m_pParser->CheckEnv();
      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;
        }
        refptr<fileinfo> pTarget=GetFileInfo(*It,pFirstMakefile->m_MakeDir);
        pFirstMakefile->m_pParser->BuildTarget(pTarget);
        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 (string Message)
  {
    cerr << "Error occured: " << Message << endl;
    #ifdef _DEBUG
    if (g_DumpOnError)
    {
      DumpVarsAndRules();
    }
    #endif
    return 1;
  }

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

  return 0;
}