| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2014 Eran Ifrah |
| 5 |
// file name : compiler_command_line_parser.cpp |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
|
| 26 |
#include "compiler_command_line_parser.h" |
| 27 |
|
| 28 |
#include "StringUtils.h" |
| 29 |
#include "procutils.h" |
| 30 |
|
| 31 |
#include <ctype.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <string.h> |
| 34 |
#include <wx/ffile.h> |
| 35 |
#include <wx/filename.h> |
| 36 |
#include <wx/string.h> |
| 37 |
|
| 38 |
////////////////////////////////////////////////////// |
| 39 |
////////////////////////////////////////////////////// |
| 40 |
|
| 41 |
CompilerCommandLineParser::CompilerCommandLineParser(const wxString& cmdline, const wxString& workingDirectory) |
| 42 |
{ |
| 43 |
m_argc = 0; |
| 44 |
m_argv = NULL; |
| 45 |
|
| 46 |
wxString c = cmdline; |
| 47 |
|
| 48 |
// HACK |
| 49 |
// Since our code does not handle \ properly when its part of a directory name |
| 50 |
// we replace it with forward slash |
| 51 |
c.Replace("\\\"", "@@GERESH@@"); |
| 52 |
c.Replace("\\", "/"); |
| 53 |
c.Replace("@@GERESH@@", "\\\""); |
| 54 |
|
| 55 |
// Check for makefile directory changes lines |
| 56 |
if(cmdline.Contains("Entering directory `")) { |
| 57 |
wxString currentDir = cmdline.AfterFirst('`'); |
| 58 |
m_diretory = currentDir.BeforeLast('\''); |
| 59 |
|
| 60 |
} else { |
| 61 |
|
| 62 |
m_argv = StringUtils::BuildArgv(c, m_argc); |
| 63 |
|
| 64 |
for(int i = 0; i < m_argc; i++) { |
| 65 |
wxString opt = wxString(m_argv[i], wxConvUTF8); |
| 66 |
opt.Trim().Trim(false); |
| 67 |
|
| 68 |
wxString rest; |
| 69 |
if(opt.StartsWith("`") && opt.EndsWith("`")) { |
| 70 |
// backtick |
| 71 |
opt = opt.Mid(1); |
| 72 |
opt.RemoveLast(); |
| 73 |
|
| 74 |
// Execute the command |
| 75 |
wxString result = ProcUtils::SafeExecuteCommand(opt); |
| 76 |
|
| 77 |
// Parse the result |
| 78 |
CompilerCommandLineParser cclp(result, workingDirectory); |
| 79 |
m_includes.insert(m_includes.end(), cclp.GetIncludes().begin(), cclp.GetIncludes().end()); |
| 80 |
m_includesWithPrefix.insert(m_includesWithPrefix.end(), cclp.GetIncludesWithPrefix().begin(), |
| 81 |
cclp.GetIncludesWithPrefix().end()); |
| 82 |
m_macros.insert(m_macros.end(), cclp.GetMacros().begin(), cclp.GetMacros().end()); |
| 83 |
m_macrosWithPrefix.insert(m_macrosWithPrefix.end(), cclp.GetMacrosWithPrefix().begin(), |
| 84 |
cclp.GetMacrosWithPrefix().end()); |
| 85 |
m_framworks.insert(m_framworks.end(), cclp.GetFramworks().begin(), cclp.GetFramworks().end()); |
| 86 |
} else if(opt.StartsWith("@") && (opt.Contains("includes_C.rsp") || opt.Contains("includes_CXX.rsp"))) { |
| 87 |
|
| 88 |
// The include folders are inside the file - read the file and process its content |
| 89 |
wxFileName fnIncludes(workingDirectory + "/" + opt.Mid(1)); |
| 90 |
if(fnIncludes.Exists()) { |
| 91 |
AddIncludesFromFile(fnIncludes); |
| 92 |
} |
| 93 |
|
| 94 |
} else if(opt == "-isystem" && (i + 1 < m_argc)) { |
| 95 |
|
| 96 |
// the next arg is the include folder |
| 97 |
wxString include_path = m_argv[i + 1]; |
| 98 |
include_path.Trim().Trim(false); |
| 99 |
|
| 100 |
m_includes.Add(include_path); |
| 101 |
m_includesWithPrefix.Add(wxString() << "-I" << include_path); |
| 102 |
++i; |
| 103 |
|
| 104 |
} else if(opt.StartsWith("-I", &rest)) { |
| 105 |
rest.Replace("\"", wxEmptyString); |
| 106 |
wxFileName path(rest, wxEmptyString); |
| 107 |
m_includes.Add(path.GetPath()); |
| 108 |
m_includesWithPrefix.Add("-I" + path.GetPath()); |
| 109 |
|
| 110 |
} else if(opt.StartsWith("/I", &rest)) { |
| 111 |
rest.Replace("\"", wxEmptyString); |
| 112 |
wxFileName path(rest, wxEmptyString); |
| 113 |
m_includes.Add(path.GetPath()); |
| 114 |
m_includesWithPrefix.Add("/I" + path.GetPath()); |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
else if(opt.StartsWith("-D", &rest) || opt.StartsWith("/D", &rest)) { |
| 119 |
m_macros.Add(rest); |
| 120 |
m_macrosWithPrefix.Add(opt); |
| 121 |
} |
| 122 |
|
| 123 |
else if(opt.StartsWith("-include-path ", &rest)) { |
| 124 |
m_includesWithPrefix.Add(rest); |
| 125 |
rest.Trim().Trim(false); |
| 126 |
m_pchFile = rest; |
| 127 |
} |
| 128 |
|
| 129 |
else if((opt == "-include") && (i + 1 < m_argc)) { |
| 130 |
wxString include_path = m_argv[i + 1]; |
| 131 |
include_path.Trim().Trim(false); |
| 132 |
m_pchFile = include_path; |
| 133 |
++i; |
| 134 |
} else if((opt == "-isysroot") && (i + 1 < m_argc)) { |
| 135 |
wxString include_path = m_argv[i + 1]; |
| 136 |
include_path.Trim().Trim(false); |
| 137 |
m_sysroots.Add(include_path); |
| 138 |
++i; |
| 139 |
} |
| 140 |
|
| 141 |
else if(opt.StartsWith("/FI", &rest)) { |
| 142 |
rest.Trim().Trim(false); |
| 143 |
m_pchFile = rest; |
| 144 |
} |
| 145 |
|
| 146 |
// Support for Apple's Framework include paths |
| 147 |
else if(opt.StartsWith("-F", &rest)) { |
| 148 |
|
| 149 |
m_includesWithPrefix.Add(opt); |
| 150 |
rest.Trim().Trim(false); |
| 151 |
m_framworks.Add(rest); |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
// std |
| 156 |
else if(opt.StartsWith("-std=", &rest) || opt.StartsWith("/std:", &rest)) { |
| 157 |
rest.Trim().Trim(false); |
| 158 |
|
| 159 |
if(!rest.IsEmpty()) { |
| 160 |
m_standard = rest; |
| 161 |
} |
| 162 |
// keep the std as an option as well |
| 163 |
m_otherOptions.Add(opt); |
| 164 |
} else { |
| 165 |
m_otherOptions.Add(opt); |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
CompilerCommandLineParser::~CompilerCommandLineParser() |
| 172 |
{ |
| 173 |
StringUtils::FreeArgv(m_argv, m_argc); |
| 174 |
m_argv = NULL; |
| 175 |
m_argc = 0; |
| 176 |
} |
| 177 |
|
| 178 |
wxString CompilerCommandLineParser::GetCompileLine() const |
| 179 |
{ |
| 180 |
wxString s; |
| 181 |
for(size_t i = 0; i < m_includes.GetCount(); i++) { |
| 182 |
s << "-I" << m_includes.Item(i) << " "; |
| 183 |
} |
| 184 |
|
| 185 |
for(size_t i = 0; i < m_macros.GetCount(); i++) { |
| 186 |
s << "-D" << m_macros.Item(i) << " "; |
| 187 |
} |
| 188 |
|
| 189 |
for(size_t i = 0; i < m_sysroots.size(); ++i) { |
| 190 |
s << "-isysroot " << m_sysroots.Item(i) << " "; |
| 191 |
} |
| 192 |
s.Trim().Trim(false); |
| 193 |
return s; |
| 194 |
} |
| 195 |
|
| 196 |
wxString CompilerCommandLineParser::GetStandardWithPrefix() const |
| 197 |
{ |
| 198 |
if(m_standard.IsEmpty()) { |
| 199 |
return ""; |
| 200 |
} |
| 201 |
return "-std=" + m_standard; |
| 202 |
} |
| 203 |
|
| 204 |
void CompilerCommandLineParser::MakeAbsolute(const wxString& path) |
| 205 |
{ |
| 206 |
wxArrayString incls; |
| 207 |
incls.reserve(m_includes.size()); |
| 208 |
|
| 209 |
for(size_t i = 0; i < m_includes.GetCount(); ++i) { |
| 210 |
wxFileName fn(m_includes.Item(i), ""); |
| 211 |
fn.MakeAbsolute(path); |
| 212 |
incls.Add(fn.GetPath()); |
| 213 |
} |
| 214 |
m_includes.swap(incls); |
| 215 |
|
| 216 |
m_includesWithPrefix.Clear(); |
| 217 |
for(size_t i = 0; i < m_framworks.GetCount(); ++i) { |
| 218 |
m_includesWithPrefix.Add("-F" + m_framworks.Item(i)); |
| 219 |
} |
| 220 |
|
| 221 |
for(size_t i = 0; i < m_includes.GetCount(); ++i) { |
| 222 |
m_includesWithPrefix.Add("-I" + m_includes.Item(i)); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
void CompilerCommandLineParser::AddIncludesFromFile(const wxFileName& includeFile) |
| 227 |
{ |
| 228 |
wxFFile fp(includeFile.GetFullPath(), "rb"); |
| 229 |
if(fp.IsOpened()) { |
| 230 |
wxString content; |
| 231 |
fp.ReadAll(&content); |
| 232 |
content.Replace("\n", " "); |
| 233 |
CompilerCommandLineParser cclp(content); |
| 234 |
m_includes.insert(m_includes.end(), cclp.GetIncludes().begin(), cclp.GetIncludes().end()); |
| 235 |
m_includesWithPrefix.insert(m_includesWithPrefix.end(), cclp.GetIncludesWithPrefix().begin(), |
| 236 |
cclp.GetIncludesWithPrefix().end()); |
| 237 |
fp.Close(); |
| 238 |
} |
| 239 |
} |