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
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2014 Eran Ifrah
// 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 "StringUtils.h"
#include "procutils.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <wx/ffile.h>
#include <wx/filename.h>
#include <wx/string.h>
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
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("\\\"", "@@GERESH@@");
c.Replace("\\", "/");
c.Replace("@@GERESH@@", "\\\"");
// Check for makefile directory changes lines
if(cmdline.Contains("Entering directory `")) {
wxString currentDir = cmdline.AfterFirst('`');
m_diretory = currentDir.BeforeLast('\'');
} else {
m_argv = StringUtils::BuildArgv(c, 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.EndsWith("`")) {
// backtick
opt = opt.Mid(1);
opt.RemoveLast();
// Execute the command
wxString result = ProcUtils::SafeExecuteCommand(opt);
// Parse the result
CompilerCommandLineParser cclp(result, workingDirectory);
m_includes.insert(m_includes.end(), cclp.GetIncludes().begin(), cclp.GetIncludes().end());
m_includesWithPrefix.insert(m_includesWithPrefix.end(), cclp.GetIncludesWithPrefix().begin(),
cclp.GetIncludesWithPrefix().end());
m_macros.insert(m_macros.end(), cclp.GetMacros().begin(), cclp.GetMacros().end());
m_macrosWithPrefix.insert(m_macrosWithPrefix.end(), cclp.GetMacrosWithPrefix().begin(),
cclp.GetMacrosWithPrefix().end());
m_framworks.insert(m_framworks.end(), cclp.GetFramworks().begin(), cclp.GetFramworks().end());
} else 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("-I", &rest)) {
rest.Replace("\"", wxEmptyString);
wxFileName path(rest, wxEmptyString);
m_includes.Add(path.GetPath());
m_includesWithPrefix.Add("-I" + path.GetPath());
} else if(opt.StartsWith("/I", &rest)) {
rest.Replace("\"", wxEmptyString);
wxFileName path(rest, wxEmptyString);
m_includes.Add(path.GetPath());
m_includesWithPrefix.Add("/I" + path.GetPath());
}
else if(opt.StartsWith("-D", &rest) || opt.StartsWith("/D", &rest)) {
m_macros.Add(rest);
m_macrosWithPrefix.Add(opt);
}
else if(opt.StartsWith("-include-path ", &rest)) {
m_includesWithPrefix.Add(rest);
rest.Trim().Trim(false);
m_pchFile = rest;
}
else if((opt == "-include") && (i + 1 < m_argc)) {
wxString include_path = m_argv[i + 1];
include_path.Trim().Trim(false);
m_pchFile = include_path;
++i;
} else if((opt == "-isysroot") && (i + 1 < m_argc)) {
wxString include_path = m_argv[i + 1];
include_path.Trim().Trim(false);
m_sysroots.Add(include_path);
++i;
}
else if(opt.StartsWith("/FI", &rest)) {
rest.Trim().Trim(false);
m_pchFile = rest;
}
// Support for Apple's Framework include paths
else if(opt.StartsWith("-F", &rest)) {
m_includesWithPrefix.Add(opt);
rest.Trim().Trim(false);
m_framworks.Add(rest);
}
// std
else if(opt.StartsWith("-std=", &rest) || opt.StartsWith("/std:", &rest)) {
rest.Trim().Trim(false);
if(!rest.IsEmpty()) {
m_standard = rest;
}
// keep the std as an option as well
m_otherOptions.Add(opt);
} else {
m_otherOptions.Add(opt);
}
}
}
}
CompilerCommandLineParser::~CompilerCommandLineParser()
{
StringUtils::FreeArgv(m_argv, m_argc);
m_argv = NULL;
m_argc = 0;
}
wxString CompilerCommandLineParser::GetCompileLine() const
{
wxString s;
for(size_t i = 0; i < m_includes.GetCount(); i++) {
s << "-I" << m_includes.Item(i) << " ";
}
for(size_t i = 0; i < m_macros.GetCount(); i++) {
s << "-D" << m_macros.Item(i) << " ";
}
for(size_t i = 0; i < m_sysroots.size(); ++i) {
s << "-isysroot " << m_sysroots.Item(i) << " ";
}
s.Trim().Trim(false);
return s;
}
wxString CompilerCommandLineParser::GetStandardWithPrefix() const
{
if(m_standard.IsEmpty()) {
return "";
}
return "-std=" + m_standard;
}
void CompilerCommandLineParser::MakeAbsolute(const wxString& path)
{
wxArrayString incls;
incls.reserve(m_includes.size());
for(size_t i = 0; i < m_includes.GetCount(); ++i) {
wxFileName fn(m_includes.Item(i), "");
fn.MakeAbsolute(path);
incls.Add(fn.GetPath());
}
m_includes.swap(incls);
m_includesWithPrefix.Clear();
for(size_t i = 0; i < m_framworks.GetCount(); ++i) {
m_includesWithPrefix.Add("-F" + m_framworks.Item(i));
}
for(size_t i = 0; i < m_includes.GetCount(); ++i) {
m_includesWithPrefix.Add("-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();
}
}
|