|
4 | 4 | #include "config.h" |
5 | 5 | #include "terminal.h" |
6 | 6 | #include <regex> |
| 7 | +#include "compile_commands.h" |
7 | 8 |
|
8 | 9 | CMake::CMake(const boost::filesystem::path &path) { |
9 | 10 | const auto find_cmake_project=[this](const boost::filesystem::path &cmake_path) { |
@@ -110,29 +111,69 @@ bool CMake::update_debug_build(const boost::filesystem::path &debug_build_path, |
110 | 111 | return false; |
111 | 112 | } |
112 | 113 |
|
113 | | -boost::filesystem::path CMake::get_executable(const boost::filesystem::path &file_path) { |
114 | | - auto executables = get_functions_parameters("add_executable"); |
| 114 | +boost::filesystem::path CMake::get_executable(const boost::filesystem::path &build_path, const boost::filesystem::path &file_path) { |
| 115 | + auto cmake_executables = get_functions_parameters("add_executable"); |
115 | 116 |
|
116 | | - //Attempt to find executable based add_executable files and opened tab |
117 | | - boost::filesystem::path executable_path; |
| 117 | + //Attempt to find executable based on add_executable in cmake files |
118 | 118 | if(!file_path.empty()) { |
119 | | - for(auto &executable: executables) { |
| 119 | + for(auto &executable: cmake_executables) { |
120 | 120 | if(executable.second.size()>1) { |
121 | 121 | for(size_t c=1;c<executable.second.size();c++) { |
122 | | - if(executable.second[c]==file_path.filename()) { |
123 | | - executable_path=executable.first.parent_path()/executable.second[0]; |
124 | | - break; |
| 122 | + if(executable.second[c]==file_path.filename() && |
| 123 | + executable.second[0].size()>0 && cmake_executables[0].second[0].substr(0, 2)!="${") { |
| 124 | + auto found_executable=(executable.first.parent_path()/executable.second[0]).string(); |
| 125 | + size_t pos=found_executable.find(project_path.string()); |
| 126 | + if(pos!=std::string::npos) |
| 127 | + found_executable.replace(pos, project_path.string().size(), build_path.string()); |
| 128 | + return found_executable; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + if(cmake_executables.size()>0 && cmake_executables[0].second.size()>0 && cmake_executables[0].second[0].size()>0 && |
| 135 | + cmake_executables[0].second[0].substr(0, 2)!="${") { |
| 136 | + auto found_executable=(cmake_executables[0].first.parent_path()/cmake_executables[0].second[0]).string(); |
| 137 | + size_t pos=found_executable.find(project_path.string()); |
| 138 | + if(pos!=std::string::npos) |
| 139 | + found_executable.replace(pos, project_path.string().size(), build_path.string()); |
| 140 | + return found_executable; |
| 141 | + } |
| 142 | + |
| 143 | + //Attempt to find executable based on compile_commands.json |
| 144 | + CompileCommands compile_commands(build_path); |
| 145 | + size_t compile_commands_best_match_size=-1; |
| 146 | + boost::filesystem::path compile_commands_best_match_executable; |
| 147 | + for(auto &command: compile_commands.commands) { |
| 148 | + boost::system::error_code ec; |
| 149 | + auto command_file=boost::filesystem::canonical(command.file, ec); |
| 150 | + if(!ec) { |
| 151 | + auto values=command.paramter_values("-o"); |
| 152 | + if(!values.empty()) { |
| 153 | + size_t pos; |
| 154 | + values[0].erase(0, 11); |
| 155 | + if((pos=values[0].find(".dir"))!=std::string::npos) { |
| 156 | + boost::filesystem::path executable=values[0].substr(0, pos); |
| 157 | + auto relative_path=filesystem::get_relative_path(command_file, project_path); |
| 158 | + if(!relative_path.empty()) { |
| 159 | + executable=build_path/relative_path.parent_path()/executable; |
| 160 | + if(command_file==file_path) |
| 161 | + return executable; |
| 162 | + auto command_file_directory=command_file.parent_path(); |
| 163 | + if(filesystem::file_in_path(file_path, command_file_directory)) { |
| 164 | + auto size=command_file_directory.string().size(); |
| 165 | + if(compile_commands_best_match_size==static_cast<size_t>(-1) || compile_commands_best_match_size<size) { |
| 166 | + compile_commands_best_match_size=size; |
| 167 | + compile_commands_best_match_executable=executable; |
| 168 | + } |
| 169 | + } |
125 | 170 | } |
126 | 171 | } |
127 | 172 | } |
128 | | - if(!executable_path.empty()) |
129 | | - break; |
130 | 173 | } |
131 | 174 | } |
132 | | - if(executable_path.empty() && executables.size()>0 && executables[0].second.size()>0) |
133 | | - executable_path=executables[0].first.parent_path()/executables[0].second[0]; |
134 | 175 |
|
135 | | - return executable_path; |
| 176 | + return compile_commands_best_match_executable; |
136 | 177 | } |
137 | 178 |
|
138 | 179 | void CMake::read_files() { |
|
0 commit comments