Permalink
Browse files

Add a --get-compile flag for getting the command line for a file

This is an attempt to make it easier to use other tools, many of
which can use either a compilation database or the full command
line.
  • Loading branch information...
1 parent 6f22050 commit db01a9dd38842b5b136a721fa5af9dcec673e519 @brucestephens committed Nov 20, 2013
Showing with 141 additions and 0 deletions.
  1. +1 −0 src/CMakeLists.txt
  2. +1 −0 src/ClientMessage.h
  3. +32 −0 src/GetCompileMessage.cpp
  4. +47 −0 src/GetCompileMessage.h
  5. +1 −0 src/QueryMessage.h
  6. +22 −0 src/RClient.cpp
  7. +36 −0 src/Server.cpp
  8. +1 −0 src/Server.h
View
@@ -64,6 +64,7 @@ set(RTAGS_SHARED_SOURCES
CompileMessage.cpp
CompletionMessage.cpp
CreateOutputMessage.cpp
+ GetCompileMessage.cpp
Location.cpp
QueryMessage.cpp
RTags.cpp)
View
@@ -27,6 +27,7 @@ class ClientMessage : public Message
QueryId,
CompileId,
CreateOutputId,
+ GetCompileId,
VisitFileId,
IndexerMessageId
};
View
@@ -0,0 +1,32 @@
+/* This file is part of RTags.
+
+RTags 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 3 of the License, or
+(at your option) any later version.
+
+RTags is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with RTags. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "GetCompileMessage.h"
+#include <rct/Serializer.h>
+
+GetCompileMessage::GetCompileMessage(const Path &cwd, const String &args)
+ : ClientMessage(MessageId), mWorkingDirectory(cwd), mArgs(args)
+{
+}
+
+void GetCompileMessage::encode(Serializer &serializer) const
+{
+ serializer << mRaw << mWorkingDirectory << mArgs << mProjects;
+}
+
+void GetCompileMessage::decode(Deserializer &deserializer)
+{
+ deserializer >> mRaw >> mWorkingDirectory >> mArgs >> mProjects;
+}
View
@@ -0,0 +1,47 @@
+/* This file is part of RTags.
+
+RTags 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 3 of the License, or
+(at your option) any later version.
+
+RTags is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with RTags. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef GetCompileMessage_h
+#define GetCompileMessage_h
+
+#include <rct/List.h>
+#include <rct/String.h>
+#include "ClientMessage.h"
+#include "RTags.h"
+
+class GetCompileMessage : public ClientMessage
+{
+public:
+ enum { MessageId = GetCompileId };
+
+ GetCompileMessage(const Path &cwd = Path(), const String &args = String());
+
+ Path workingDirectory() const { return mWorkingDirectory; }
+
+ String arguments() const { return mArgs; }
+ void setArguments(const String &arguments) { mArgs = arguments; }
+
+ void setProjects(const List<String> &projects) { mProjects = projects; }
+ List<String> projects() const { return mProjects; }
+
+ virtual void encode(Serializer &serializer) const;
+ virtual void decode(Deserializer &deserializer);
+private:
+ Path mWorkingDirectory;
+ List<String> mProjects;
+ String mArgs;
+};
+
+#endif
View
@@ -39,6 +39,7 @@ class QueryMessage : public ClientMessage
FindSymbols,
FixIts,
FollowLocation,
+ GetCompile,
HasFileManager,
Invalid,
IsIndexed,
View
@@ -55,6 +55,7 @@ enum OptionType {
FindVirtuals,
FixIts,
FollowLocation,
+ GetCompile,
HasFileManager,
Help,
IMenu,
@@ -150,6 +151,7 @@ struct Option opts[] = {
{ CodeComplete, "code-complete", 0, no_argument, "Get code completion from stream written to stdin." },
{ FixIts, "fixits", 0, required_argument, "Get fixits for file." },
{ Compile, "compile", 'c', required_argument, "Pass compilation arguments to rdm." },
+ { GetCompile, "get-compile", 0, required_argument, "Get compilation arguments to rdm." },
{ RemoveFile, "remove", 'D', required_argument, "Remove file from project." },
{ FindProjectRoot, "find-project-root", 0, required_argument, "Use to check behavior of find-project-root." },
{ JSON, "json", 0, optional_argument, "Dump json about files matching arg or whole project if no argument." },
@@ -986,6 +988,26 @@ bool RClient::parse(int &argc, char **argv)
}
addCompile(Path::pwd(), args);
break; }
+ case GetCompile: {
+ Path p;
+ if (optarg) {
+ p = optarg;
+ } else if (optind < argc && argv[optind][0] != '-') {
+ p = argv[optind++];
+ }
+ if (!p.isEmpty()) {
+ if (p == "clear" && !p.exists()) {
+
+ } else {
+ p.resolve(Path::MakeAbsolute);
+ if (!p.isFile() && p != "clear") {
+ fprintf(stderr, "%s is not a file\n", optarg);
+ return false;
+ }
+ }
+ }
+ addQuery(QueryMessage::GetCompile, p);
+ break; }
case IsIndexing:
addQuery(QueryMessage::IsIndexing);
break;
View
@@ -370,6 +370,9 @@ void Server::handleQueryMessage(const QueryMessage &message, Connection *conn)
case QueryMessage::SuspendFile:
suspendFile(message, conn);
break;
+ case QueryMessage::GetCompile:
+ getCompileCommand(message, conn);
+ break;
case QueryMessage::IsIndexing:
isIndexing(message, conn);
break;
@@ -1398,6 +1401,39 @@ void Server::suspendFile(const QueryMessage &query, Connection *conn)
conn->finish();
}
+void Server::getCompileCommand(const QueryMessage &query, Connection *conn)
+{
+ std::shared_ptr<Project> project;
+ const Match match = query.match();
+ if (match.isEmpty()) {
+ project = currentProject();
+ } else {
+ project = updateProjectForLocation(match);
+ }
+ if (!project) {
+ conn->write("No project");
+ } else if (project->state() != Project::Loaded) {
+ conn->write("Project loading");
+ } else {
+ const Path p = query.match().pattern();
+ if (!p.isFile()) {
+ conn->write<512>("%s doesn't seem to exist", p.constData());
+ } else {
+ const uint32_t fileId = Location::insertFile(p);
+ const SourceInformationMap& infoMap = project->sources();
+ SourceInformationMap::const_iterator it = infoMap.find(fileId);
+ if (it == infoMap.end() || it->second.isJS()) {
+ conn->write<512>("No command known for %s", p.constData());
+ } else {
+ String command = it->second.compiler + " " +
+ String::join(it->second.args, ' ');
+ conn->write<512>("%s", command.data());
+ }
+ }
+ }
+ conn->finish();
+}
+
void Server::handleCompletionMessage(const CompletionMessage &message, Connection *conn)
{
updateProject(message.projects(), 0);
View
@@ -143,6 +143,7 @@ class Server
void shutdown(const QueryMessage &query, Connection *conn);
void builds(const QueryMessage &query, Connection *conn);
void suspendFile(const QueryMessage &query, Connection *conn);
+ void getCompileCommand(const QueryMessage &query, Connection *conn);
int nextId();
void reindex(const QueryMessage &query, Connection *conn);
std::shared_ptr<Project> updateProjectForLocation(const Match &match);

0 comments on commit db01a9d

Please sign in to comment.