File: includepathlocator.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 (243 lines) | stat: -rw-r--r-- 7,144 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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 The CodeLite Team
// file name            : includepathlocator.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 "includepathlocator.h"
#include <wx/dir.h>
#include "environmentconfig.h"
#include <wx/utils.h>
#include "procutils.h"
#include <wx/fileconf.h>
#include <wx/tokenzr.h>
#include "editor_config.h"
#include "globals.h"

// Helper method
static wxArrayString ExecCommand(const wxString &cmd) {
	wxArrayString outputArr;
	EnvSetter setter;
	ProcUtils::SafeExecuteCommand(cmd, outputArr);
	return outputArr;
}

IncludePathLocator::IncludePathLocator(IManager *mgr)
	: m_mgr(mgr) {
}

IncludePathLocator::~IncludePathLocator() {
}

void IncludePathLocator::Locate(wxArrayString& paths, wxArrayString &excludePaths, bool thirdPartyLibs, const wxString &tool) {
	// Common compiler paths - should be placed at top of the include path!
	wxString tmpfile1 = wxFileName::CreateTempFileName(wxT("codelite"));
	wxString command;
	wxString tmpfile = tmpfile1;
	tmpfile += wxT(".cpp");

	wxString bin = tool;
	if(bin.IsEmpty()) {
		bin = wxT("gcc");
	}

	wxRenameFile(tmpfile1, tmpfile);

	// GCC prints parts of its output to stdout and some to stderr
	// redirect all output to stdout
#if defined(__WXMAC__) || defined(__WXGTK__)
	// Mac does not like the standard command
	command = wxString::Format(wxT("%s -v -x c++ /dev/null -fsyntax-only"), bin.c_str());
#else
	command = wxString::Format(wxT("%s -v -x c++ %s -fsyntax-only"), bin.c_str(), tmpfile.c_str());
#endif

	wxString outputStr = wxShellExec(command, wxEmptyString);
	wxRemoveFile( tmpfile );

	wxArrayString outputArr = wxStringTokenize(outputStr, wxT("\n\r"), wxTOKEN_STRTOK);
	// Analyze the output
	bool collect(false);
	for(size_t i=0; i<outputArr.GetCount(); i++) {
		if(outputArr[i].Contains(wxT("#include <...> search starts here:"))) {
			collect = true;
			continue;
		}

		if(outputArr[i].Contains(wxT("End of search list."))) {
			break;
		}

		if(collect) {

			wxString file = outputArr.Item(i).Trim().Trim(false);

			// on Mac, (framework directory) appears also,
			// but it is harmless to use it under all OSs
			file.Replace(wxT("(framework directory)"), wxT(""));
			file.Trim().Trim(false);

			wxFileName includePath(file, wxT(""));
			includePath.Normalize();

			paths.Add( includePath.GetPath() );
		}
	}

	if(thirdPartyLibs) {
		// try to locate QMAKE
		wxFileConfig  qmakeConf(wxEmptyString, wxEmptyString, m_mgr->GetStartupDirectory() + wxT("/config/qmake.ini"));
		wxString      groupName;
		long          index(0);
		wxArrayString out;
		wxString      qmake(wxT("qmake"));

		if (qmakeConf.GetFirstGroup(groupName, index)) {
			// we got qmake configuration, use it instead of the default qmake command
			qmake = qmakeConf.Read(groupName + wxT("/qmake"));
		}

		// Run: qmake -query QT_INSTALL_PREFIX
		wxString cmd;
		cmd << qmake << wxT(" -query QT_INSTALL_PREFIX");

#ifdef __WXGTK__
		cmd << wxT(" 2>/dev/null");
#endif

		out = ExecCommand(cmd);

		if (out.IsEmpty() == false ) {

			wxString qt_output (out.Item(0));
			qt_output.Trim().Trim(false);

#if defined(__WXGTK__)||defined(__WXMAC__)
			wxString pathQt4, pathQt3, pathQt;
			pathQt4 << qt_output << wxFileName::GetPathSeparator() << wxT("include") << wxFileName::GetPathSeparator() << wxT("qt4");
			pathQt3 << qt_output << wxFileName::GetPathSeparator() << wxT("include") << wxFileName::GetPathSeparator() << wxT("qt3");
			pathQt  << qt_output << wxFileName::GetPathSeparator() << wxT("include");

			if (wxDir::Exists( pathQt4 )) {
				wxString tmpPath;

				tmpPath = pathQt4 + wxT("/QtCore");
				if(wxFileName::DirExists(tmpPath))
					paths.Add( tmpPath );

				tmpPath = pathQt4 + wxT("/QtGui");
				if(wxFileName::DirExists(tmpPath))
					paths.Add( tmpPath );

				tmpPath = pathQt4 + wxT("/QtXml");
				if(wxFileName::DirExists(tmpPath))
					paths.Add( tmpPath );

			} else if (wxDir::Exists( pathQt3 ) ) {

				wxString tmpPath;

				tmpPath = pathQt3 + wxT("/QtCore");
				if(wxFileName::DirExists(tmpPath))
					paths.Add( tmpPath );

				tmpPath = pathQt3 + wxT("/QtGui");
				if(wxFileName::DirExists(tmpPath))
					paths.Add( tmpPath );

				tmpPath = pathQt3 + wxT("/QtXml");
				if(wxFileName::DirExists(tmpPath))
					paths.Add( tmpPath );

			} else if (wxDir::Exists( pathQt ) ) {

				wxString tmpPath;

				tmpPath = pathQt + wxT("/QtCore");
				if(wxFileName::DirExists(tmpPath))
					paths.Add( tmpPath );

				tmpPath = pathQt + wxT("/QtGui");
				if(wxFileName::DirExists(tmpPath))
					paths.Add( pathQt );

				tmpPath = pathQt + wxT("/QtXml");
				if(wxFileName::DirExists(tmpPath))
					paths.Add( tmpPath );
			}

#else // __WXMSW__
			wxString pathWin;
			pathWin << qt_output << wxFileName::GetPathSeparator() << wxT("include") << wxFileName::GetPathSeparator();
			if (wxDir::Exists( pathWin )) {

				wxString tmpPath;

				tmpPath = pathWin + wxT("QtCore");
				if(wxFileName::DirExists(tmpPath)) {
					wxFileName fn(tmpPath, wxT(""));
					paths.Add( fn.GetPath() );
				}

				tmpPath = pathWin + wxT("QtGui");
				if(wxFileName::DirExists(tmpPath)) {
					wxFileName fn(tmpPath, wxT(""));
					paths.Add( fn.GetPath() );
				}

				tmpPath = pathWin + wxT("QtXml");
				if(wxFileName::DirExists(tmpPath)) {
					wxFileName fn(tmpPath, wxT(""));
					paths.Add( fn.GetPath() );
				}
			}
#endif
		}

		// Try wxWidgets
#ifdef __WXMSW__
		// On Windows, just read the content of the WXWIN environment variable
		wxString wxwin;
		if (wxGetEnv(wxT("WX_INCL_HOME"), &wxwin)) {
			// we got the path to the installation of wxWidgets
			if (wxDir::Exists(wxwin)) {
				paths.Add( wxwin );
				excludePaths.Add( wxwin + wxT("\\univ") );
				excludePaths.Add( wxwin + wxT("\\unix") );
			}
		}
#else
		// run wx-config and parse the output
		out.Clear();
		out = ExecCommand(wxT("wx-config --cxxflags 2>/dev/null"));
		if (out.IsEmpty() == false) {
			wxString line ( out.Item(0) );
			int where = line.Find(wxT(" -I"));
			while (where != wxNOT_FOUND) {
				line = line.Mid(where + 3);
				paths.Add( line.BeforeFirst(wxT(' ')) );
				where = line.Find(wxT(" -I"));
			}
		}
#endif
	}
}