summaryrefslogtreecommitdiffstats
path: root/src/jomlib/makefilefactory.cpp
blob: 6cc05bb666ff5031e33d8dcecb6ce352d769e98c (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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of jom.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "exception.h"
#include "makefilefactory.h"
#include "macrotable.h"
#include "makefile.h"
#include "options.h"
#include "parser.h"
#include "preprocessor.h"

#include <QtCore/QCoreApplication>
#include <QtCore/QDir>

namespace NMakeFile {

MakefileFactory::MakefileFactory()
:   m_makefile(0),
    m_errorType(NoError)
{
}

void MakefileFactory::setEnvironment(const QStringList &env)
{
    for (QStringList::const_iterator it = env.begin(); it != env.end(); ++it) {
        int idx = it->indexOf(QLatin1Char('='));
        if (idx >= 0)
            m_environment.insert(it->left(idx), it->mid(idx + 1));
    }
}

void MakefileFactory::clear()
{
    m_makefile = 0;
    m_errorType = NoError;
    m_errorString.clear();
    m_activeTargets.clear();
}

static void readEnvironment(const ProcessEnvironment &environment, MacroTable *macroTable, bool forceReadOnly)
{
    ProcessEnvironment::const_iterator it = environment.begin();
    for (; it != environment.end(); ++it)
        macroTable->defineEnvironmentMacroValue(it.key().toQString(), it.value(), forceReadOnly);
}

/**
 * Returns true, if the path contains some whitespace.
 */
static bool isComplexPathName(const QString& path)
{
    for (int i=path.length()-1; i > 0; i--) {
        const QChar ch = path.at(i);
        if (ch.isSpace())
            return true;
    }
    return false;
}

/**
 * Enclose the path in double quotes, if its a complex one.
 */
static QString encloseInDoubleQuotesIfNeeded(const QString& path)
{
    if (isComplexPathName(path)) {
        QString result(QLatin1Char('"'));
        result.append(path);
        result.append(QLatin1Char('"'));
        return result;
    }
    return path;
}

bool MakefileFactory::apply(const QStringList& commandLineArguments, Options **outopt)
{
    if (m_makefile)
        clear();

    Options *options = new Options;
    if (outopt)
        *outopt = options;
    MacroTable *macroTable = new MacroTable;
    macroTable->setEnvironment(m_environment);

    QString filename;
    if (!options->readCommandLineArguments(commandLineArguments, filename, m_activeTargets, *macroTable)) {
        m_errorType = CommandLineError;
        return false;
    }
    if (options->showUsageAndExit || options->showVersionAndExit)
        return true;

    if (!options->stderrFile.isEmpty()) {
        // Try to open the file for writing.
        const wchar_t *wszFileName = reinterpret_cast<const wchar_t*>(options->stderrFile.utf16());
        FILE *f = _wfopen(wszFileName, L"w");
        if (!f) {
            m_errorString = QLatin1String("Cannot open stderr file for writing.");
            m_errorType = IOError;
            return false;
        }
        fclose(f);
        if (!_wfreopen(wszFileName, L"w", stderr)) {
            m_errorString = QLatin1String("Cannot reopen stderr handle for writing.");
            m_errorType = IOError;
            return false;
        }
    }

    options->fullAppPath = QDir::toNativeSeparators(QCoreApplication::applicationFilePath());

    readEnvironment(m_environment, macroTable, options->overrideEnvVarMacros);
    if (!options->ignorePredefinedRulesAndMacros) {
        macroTable->predefineValue("MAKE", encloseInDoubleQuotesIfNeeded(options->fullAppPath));
        macroTable->predefineValue("MAKEDIR", encloseInDoubleQuotesIfNeeded(
                                       QDir::toNativeSeparators(QDir::currentPath())));
        macroTable->predefineValue("AS", "ml");       // Macro Assembler
        macroTable->predefineValue("ASFLAGS", QString());
        macroTable->predefineValue("BC", "bc");       // Basic Compiler
        macroTable->predefineValue("BCFLAGS", QString());
        macroTable->predefineValue("CC", "cl");       // C Compiler
        macroTable->predefineValue("CCFLAGS", QString());
        macroTable->predefineValue("COBOL", "cobol"); // COBOL Compiler
        macroTable->predefineValue("COBOLFLAGS", QString());
        macroTable->predefineValue("CPP", "cl");      // C++ Compiler
        macroTable->predefineValue("CPPFLAGS", QString());
        macroTable->predefineValue("CXX", "cl");      // C++ Compiler
        macroTable->predefineValue("CXXFLAGS", QString());
        macroTable->predefineValue("FOR", "fl");      // FORTRAN Compiler
        macroTable->predefineValue("FORFLAGS", QString());
        macroTable->predefineValue("PASCAL", "pl");   // Pascal Compiler
        macroTable->predefineValue("PASCALFLAGS", QString());
        macroTable->predefineValue("RC", "rc");       // Resource Compiler
        macroTable->predefineValue("RCFLAGS", QString());
    }

    try {
        m_makefile = new Makefile(filename);
        m_makefile->setOptions(options);
        m_makefile->setMacroTable(macroTable);
        Preprocessor preprocessor;
        preprocessor.setMacroTable(macroTable);
        preprocessor.openFile(filename);
        Parser parser;
        parser.apply(&preprocessor, m_makefile, m_activeTargets);
    } catch (Exception &e) {
        m_errorType = ParserError;
        m_errorString = e.toString();
    }

    return m_errorType == NoError;
}

} //namespace NMakeFile