File: compiler_command_line_parser.cpp

package info (click to toggle)
codelite 6.1.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 48,992 kB
  • ctags: 43,502
  • sloc: cpp: 334,263; ansic: 18,441; xml: 4,713; yacc: 2,653; lex: 2,449; python: 1,188; sh: 385; makefile: 40
file content (312 lines) | stat: -rw-r--r-- 10,034 bytes parent folder | download
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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 The CodeLite Team
// file name            : compiler_command_line_parser.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program 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 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

#include "compiler_command_line_parser.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <wx/string.h>
#include <wx/filename.h>
#include <wx/ffile.h>

//////////////////////////////////////////////////////
// Helper C functions
//////////////////////////////////////////////////////

#ifndef EOS
#define EOS '\0'
#endif

#ifdef __WXMSW__
#include <malloc.h>
#endif

#define ISBLANK(ch) ((ch) == ' ' || (ch) == '\t')
#define INITIAL_MAXARGC 8	/* Number of args + NULL in initial argv */

static void freeargv (char **vector)
{
    register char **scan;

    if (vector != NULL) {
        for (scan = vector; *scan != NULL; scan++) {
            free (*scan);
        }
        free (vector);
    }
}


//////////////////////////////////////////////////////
//////////////////////////////////////////////////////

char **buildargv (const char *input, int &argc)
{
    char *arg;
    char *copybuf;
    int squote = 0;
    int dquote = 0;
    int bsquote = 0;
    int maxargc = 0;
    char **argv = NULL;
    char **nargv;

    if (input != NULL) {
        copybuf = (char *) alloca (strlen (input) + 1);
        /* Is a do{}while to always execute the loop once.  Always return an
        argv, even for null strings.  See NOTES above, test case below. */
        do {
            /* Pick off argv[argc] */
            while (ISBLANK(*input)) {
                input++;
            }
            if ((maxargc == 0) || (argc >= (maxargc - 1))) {
                /* argv needs initialization, or expansion */
                if (argv == NULL) {
                    maxargc = INITIAL_MAXARGC;
                    nargv = (char **) malloc (maxargc * sizeof (char *));
                } else {
                    maxargc *= 2;
                    nargv = (char **) realloc (argv, maxargc * sizeof (char *));
                }
                if (nargv == NULL) {
                    if (argv != NULL) {
                        freeargv (argv);
                        argv = NULL;
                    }
                    break;
                }
                argv = nargv;
                argv[argc] = NULL;
            }
            /* Begin scanning arg */
            arg = copybuf;
            while (*input != EOS) {
                if (ISBLANK (*input) && !squote && !dquote && !bsquote) {
                    break;
                } else {
                    if (bsquote) {
                        bsquote = 0;
                        *arg++ = *input;
                    } else if (*input == '\\') {
                        bsquote = 1;
                    } else if (squote) {
                        if (*input == '\'') {
                            squote = 0;
                        } else {
                            *arg++ = *input;
                        }
                    } else if (dquote) {
                        if (*input == '"') {
                            dquote = 0;
                        } else {
                            *arg++ = *input;
                        }
                    } else {
                        if (*input == '\'') {
                            squote = 1;
                        } else if (*input == '"') {
                            dquote = 1;
                        } else {
                            *arg++ = *input;
                        }
                    }
                    input++;
                }
            }
            *arg = EOS;
            argv[argc] = strdup (copybuf);
            if (argv[argc] == NULL) {
                freeargv (argv);
                argv = NULL;
                break;
            }
            argc++;
            argv[argc] = NULL;

            while (ISBLANK (*input)) {
                input++;
            }
        } while (*input != EOS);
    }
    return (argv);
}

//////////////////////////////////////////////////////
//////////////////////////////////////////////////////

CompilerCommandLineParser::CompilerCommandLineParser(const wxString& cmdline, const wxString& workingDirectory)
{
    m_argc = 0;
    m_argv = NULL;

    wxString c = cmdline;

    // HACK
    // Since our code does not handle \ properly when its part of a directory name
    // we replace it with forward slash
    c.Replace(wxT("\\\""), wxT("@@GERESH@@"));
    c.Replace(wxT("\\"), wxT("/"));
    c.Replace(wxT("@@GERESH@@"), wxT("\\\""));

    // Check for makefile directory changes lines
    if(cmdline.Contains(wxT("Entering directory `"))) {
        wxString currentDir = cmdline.AfterFirst(wxT('`'));
        m_diretory = currentDir.BeforeLast(wxT('\''));

    } else {

        m_argv = buildargv(c.mb_str(wxConvUTF8).data(), m_argc);

        for(int i=0; i<m_argc; i++) {
            wxString opt = wxString(m_argv[i], wxConvUTF8);
            opt.Trim().Trim(false);

            wxString rest;
            
            if ( opt.StartsWith("@") && (opt.Contains("includes_C.rsp") || opt.Contains("includes_CXX.rsp") ) ) {
                
                // The include folders are inside the file - read the file and process its content
                wxFileName fnIncludes( workingDirectory + "/" + opt.Mid(1) );
                if ( fnIncludes.Exists() ) {
                    AddIncludesFromFile( fnIncludes );
                }

            } else if ( opt == "-isystem" && (i+1 < m_argc) ) {
                
                // the next arg is the include folder
                wxString include_path = m_argv[i+1];
                include_path.Trim().Trim(false);
                
                m_includes.Add( include_path );
                m_includesWithPrefix.Add( wxString() << "-I" << include_path );
                ++i;

            } else if( opt.StartsWith(wxT("-I"), &rest)) {
                m_includes.Add(rest);
                m_includesWithPrefix.Add(opt);
            }

            else if(opt.StartsWith(wxT("-D"), &rest)) {
                m_macros.Add(rest);
                m_macrosWithPrefix.Add(opt);
            }

            else if(opt.StartsWith(wxT("-include-path"), &rest)) {
                m_includesWithPrefix.Add(opt);

                rest.Trim().Trim(false);
                m_pchFile = rest;
            }

            // Support for Apple's Framework include paths
            else if(opt.StartsWith(wxT("-F"), &rest)) {

                m_includesWithPrefix.Add(opt);
                rest.Trim().Trim(false);
                m_framworks.Add(rest);

            }

            // Support for Apple's Framework include paths
            else if(opt.StartsWith(wxT("-std"), &rest)) {
                wxString stds = rest.AfterFirst(wxT('='));
                stds.Trim().Trim(false);

                if ( stds.IsEmpty() == false ) {
                    m_standard = stds;
                }
            } else {
                m_otherOptions.Add( opt );
            }
        }
    }
}

CompilerCommandLineParser::~CompilerCommandLineParser()
{
    freeargv(m_argv);
    m_argv = NULL;
    m_argc = 0;
}

wxString CompilerCommandLineParser::GetCompileLine() const
{
    wxString s;
    for(size_t i=0; i<m_includes.GetCount(); i++) {
        s << wxT("-I") << m_includes.Item(i) << wxT(" ");
    }

    for(size_t i=0; i<m_macros.GetCount(); i++) {
        s << wxT("-D") << m_macros.Item(i) << wxT(" ");
    }

    s.Trim().Trim(false);
    return s;
}

wxString CompilerCommandLineParser::GetStandardWithPrefix() const
{
    if(m_standard.IsEmpty())
        return wxT("");
        
    return wxT("-std=") + m_standard;
}

void CompilerCommandLineParser::MakeAbsolute(const wxString &path)
{
    wxArrayString incls;
    for(size_t i=0; i<m_includes.GetCount(); ++i) {
        wxFileName fn(m_includes.Item(i), wxT(""));
        fn.MakeAbsolute(path);
        incls.Add(fn.GetFullPath());
    }
    
    m_includes.Clear();
    m_includes = incls;
    
    m_includesWithPrefix.Clear();
    for(size_t i=0; i<m_framworks.GetCount(); ++i) {
        m_includesWithPrefix.Add(wxT("-F") + m_framworks.Item(i));
    }
    
    for(size_t i=0; i<m_includes.GetCount(); ++i) {
        m_includesWithPrefix.Add(wxT("-I") + m_includes.Item(i));
    }
}

void CompilerCommandLineParser::AddIncludesFromFile(const wxFileName& includeFile)
{
    wxFFile fp( includeFile.GetFullPath(), "rb" );
    if ( fp.IsOpened() ) {
        wxString content;
        fp.ReadAll( &content );
        content.Replace("\n", " ");
        CompilerCommandLineParser cclp( content );
        m_includes.insert(m_includes.end(), cclp.GetIncludes().begin(), cclp.GetIncludes().end());
        m_includesWithPrefix.insert(m_includesWithPrefix.end(), cclp.GetIncludesWithPrefix().begin(), cclp.GetIncludesWithPrefix().end());
        fp.Close();
    }
}