diff --git a/src/cmake.cc b/src/cmake.cc index cff8f1b..c24ffb5 100644 --- a/src/cmake.cc +++ b/src/cmake.cc @@ -7,7 +7,12 @@ std::unordered_set CMake::debug_build_needed; -CMake::CMake(const boost::filesystem::path &path) { +CMake::CMake(const boost::filesystem::path &path, bool find_project_root_path) { + if(!find_project_root_path) { + project_path=path; + return; + } + const auto find_cmake_project=[this](const boost::filesystem::path &cmake_path) { for(auto &line: filesystem::read_lines(cmake_path)) { const boost::regex project_regex("^ *project *\\(.*$"); @@ -35,7 +40,7 @@ CMake::CMake(const boost::filesystem::path &path) { } } -boost::filesystem::path CMake::get_default_build_path(const boost::filesystem::path &project_path) { +boost::filesystem::path CMake::get_default_build_path() { boost::filesystem::path default_build_path=Config::get().project.default_build_path; const std::string path_variable_project_directory_name=""; @@ -55,7 +60,7 @@ boost::filesystem::path CMake::get_default_build_path(const boost::filesystem::p return default_build_path; } -boost::filesystem::path CMake::get_debug_build_path(const boost::filesystem::path &project_path) { +boost::filesystem::path CMake::get_debug_build_path() { boost::filesystem::path debug_build_path=Config::get().project.debug_build_path; const std::string path_variable_project_directory_name=""; @@ -86,14 +91,14 @@ boost::filesystem::path CMake::get_debug_build_path(const boost::filesystem::pat return debug_build_path; } -bool CMake::create_default_build(const boost::filesystem::path &project_path, bool force) { +bool CMake::create_default_build(bool force) { if(project_path.empty()) return false; if(!boost::filesystem::exists(project_path/"CMakeLists.txt")) return false; - auto default_build_path=get_default_build_path(project_path); + auto default_build_path=get_default_build_path(); if(default_build_path.empty()) return false; if(!boost::filesystem::exists(default_build_path)) { @@ -135,14 +140,14 @@ bool CMake::create_default_build(const boost::filesystem::path &project_path, bo return false; } -bool CMake::create_debug_build(const boost::filesystem::path &project_path) { +bool CMake::create_debug_build() { if(project_path.empty()) return false; if(!boost::filesystem::exists(project_path/"CMakeLists.txt")) return false; - auto debug_build_path=get_debug_build_path(project_path); + auto debug_build_path=get_debug_build_path(); if(debug_build_path.empty()) return false; if(!boost::filesystem::exists(debug_build_path)) { diff --git a/src/cmake.h b/src/cmake.h index bfa463b..dc665b5 100644 --- a/src/cmake.h +++ b/src/cmake.h @@ -7,14 +7,14 @@ class CMake { public: - CMake(const boost::filesystem::path &path); + CMake(const boost::filesystem::path &path, bool find_project_root_path=false); boost::filesystem::path project_path; std::vector paths; - static boost::filesystem::path get_default_build_path(const boost::filesystem::path &project_path); - static boost::filesystem::path get_debug_build_path(const boost::filesystem::path &project_path); - static bool create_default_build(const boost::filesystem::path &project_path, bool force=false); - static bool create_debug_build(const boost::filesystem::path &project_path); + boost::filesystem::path get_default_build_path(); + boost::filesystem::path get_debug_build_path(); + bool create_default_build(bool force=false); + bool create_debug_build(); boost::filesystem::path get_executable(const boost::filesystem::path &file_path); diff --git a/src/directories.cc b/src/directories.cc index be1d779..6b51587 100644 --- a/src/directories.cc +++ b/src/directories.cc @@ -3,6 +3,7 @@ #include #include #include "source.h" +#include "cmake.h" #include //TODO: remove using namespace std; //TODO: remove @@ -130,12 +131,13 @@ void Directories::open(const boost::filesystem::path& dir_path) { last_write_times.clear(); update_paths.clear(); update_mutex.unlock(); - - cmake=std::unique_ptr(new CMake(dir_path)); - CMake::create_default_build(cmake->project_path); - auto project=cmake->get_functions_parameters("project"); - if(project.size()>0 && project[0].second.size()>0) { - auto title=project[0].second[0]; + + auto cmake=CMake(dir_path, true); + cmake.create_default_build(); + project_path=cmake.project_path; + auto project_name=cmake.get_functions_parameters("project"); + if(project_name.size()>0 && project_name[0].second.size()>0) { + auto title=project_name[0].second[0]; //TODO: report that set_title does not handle '_' correctly? size_t pos=0; while((pos=title.find('_', pos))!=std::string::npos) { diff --git a/src/directories.h b/src/directories.h index 5e5e374..e91b6c6 100644 --- a/src/directories.h +++ b/src/directories.h @@ -5,11 +5,11 @@ #include #include #include "boost/filesystem.hpp" -#include "cmake.h" #include #include #include #include "dispatcher.h" +#include class Directories : public Gtk::TreeView { public: @@ -40,8 +40,9 @@ public: void select(const boost::filesystem::path &path); std::function on_row_activated; - std::unique_ptr cmake; + boost::filesystem::path current_path; + boost::filesystem::path project_path; private: void add_path(const boost::filesystem::path& dir_path, const Gtk::TreeModel::Row &row); diff --git a/src/notebook.cc b/src/notebook.cc index f973961..9545c11 100644 --- a/src/notebook.cc +++ b/src/notebook.cc @@ -99,18 +99,18 @@ void Notebook::open(const boost::filesystem::path &file_path) { auto language=Source::guess_language(file_path); boost::filesystem::path project_path; auto &directories=Directories::get(); - if(directories.cmake && directories.cmake->project_path!="" && file_path.generic_string().substr(0, directories.cmake->project_path.generic_string().size()+1)==directories.cmake->project_path.generic_string()+'/') - project_path=directories.cmake->project_path; + if(!directories.project_path.empty() && file_path.generic_string().substr(0, directories.project_path.generic_string().size()+1)==directories.project_path.generic_string()+'/') + project_path=directories.project_path; else { project_path=file_path.parent_path(); - CMake cmake(project_path); + CMake cmake(project_path, true); if(cmake.project_path!="") { project_path=cmake.project_path; Terminal::get().print("Project path for "+file_path.string()+" set to "+project_path.string()+"\n"); } } if(language && (language->get_id()=="chdr" || language->get_id()=="cpphdr" || language->get_id()=="c" || language->get_id()=="cpp" || language->get_id()=="objc")) { - CMake::create_default_build(project_path); + CMake(project_path).create_default_build(); source_views.emplace_back(new Source::ClangView(file_path, project_path, language)); } else @@ -249,19 +249,19 @@ bool Notebook::save(int page) { boost::filesystem::path project_path; if(view->file_path.filename()=="CMakeLists.txt") { auto &directories=Directories::get(); - if(directories.cmake && directories.cmake->project_path!="" && view->file_path.generic_string().substr(0, directories.cmake->project_path.generic_string().size()+1)==directories.cmake->project_path.generic_string()+'/') { - if(CMake::create_default_build(directories.cmake->project_path, true)) - project_path=directories.cmake->project_path; + if(!directories.project_path.empty() && view->file_path.generic_string().substr(0, directories.project_path.generic_string().size()+1)==directories.project_path.generic_string()+'/') { + if(CMake(directories.project_path).create_default_build(true)) + project_path=directories.project_path; } else { - CMake cmake(view->file_path.parent_path()); - if(CMake::create_default_build(cmake.project_path, true)) + CMake cmake(view->file_path.parent_path(), true); + if(cmake.create_default_build(true)) project_path=cmake.project_path; } if(project_path!="") { - auto debug_project_path=CMake::get_debug_build_path(project_path); + auto debug_project_path=CMake(project_path).get_debug_build_path(); if(!debug_project_path.empty() && boost::filesystem::exists(debug_project_path)) - CMake::create_debug_build(project_path); + CMake(project_path).create_debug_build(); for(auto source_view: source_views) { if(auto source_clang_view=dynamic_cast(source_view)) { if(project_path==source_clang_view->project_path) diff --git a/src/project.cc b/src/project.cc index 0fc11b4..1e1601d 100644 --- a/src/project.cc +++ b/src/project.cc @@ -82,10 +82,10 @@ std::unique_ptr Project::Clang::get_cmake() { path=Directories::get().current_path; if(path.empty()) return nullptr; - auto cmake=std::unique_ptr(new CMake(path)); + auto cmake=std::unique_ptr(new CMake(path, true)); if(cmake->project_path.empty()) return nullptr; - if(!CMake::create_default_build(cmake->project_path)) + if(!cmake->create_default_build()) return nullptr; return cmake; } @@ -96,7 +96,7 @@ std::pair Project::Clang::get_run_arguments() { return {"", ""}; auto project_path=cmake->project_path.string(); - auto run_arguments_it=run_arguments.find(project_path); + auto run_arguments_it=run_arguments.find(cmake->project_path.string()); std::string arguments; if(run_arguments_it!=run_arguments.end()) arguments=run_arguments_it->second; @@ -105,17 +105,16 @@ std::pair Project::Clang::get_run_arguments() { auto executable=cmake->get_executable(Notebook::get().get_current_page()!=-1?Notebook::get().get_current_view()->file_path:"").string(); if(executable!="") { - auto project_path=cmake->project_path; - auto build_path=CMake::get_default_build_path(project_path); + auto build_path=cmake->get_default_build_path(); if(!build_path.empty()) { - size_t pos=executable.find(project_path.string()); + size_t pos=executable.find(project_path); if(pos!=std::string::npos) - executable.replace(pos, project_path.string().size(), build_path.string()); + executable.replace(pos, project_path.size(), build_path.string()); } arguments=filesystem::escape_argument(executable); } else - arguments=filesystem::escape_argument(CMake::get_default_build_path(cmake->project_path)); + arguments=filesystem::escape_argument(cmake->get_default_build_path()); } return {project_path, arguments}; @@ -126,7 +125,7 @@ void Project::Clang::compile() { if(!cmake) return; - auto default_build_path=CMake::get_default_build_path(cmake->project_path); + auto default_build_path=cmake->get_default_build_path(); if(default_build_path.empty()) return; compiling=true; @@ -142,7 +141,7 @@ void Project::Clang::compile_and_run() { return; auto project_path=cmake->project_path; - auto default_build_path=CMake::get_default_build_path(project_path); + auto default_build_path=cmake->get_default_build_path(); if(default_build_path.empty()) return; @@ -194,17 +193,16 @@ std::pair Project::Clang::debug_get_run_arguments() { auto executable=cmake->get_executable(Notebook::get().get_current_page()!=-1?Notebook::get().get_current_view()->file_path:"").string(); if(executable!="") { - auto project_path=cmake->project_path; - auto build_path=CMake::get_debug_build_path(project_path); + auto build_path=cmake->get_debug_build_path(); if(!build_path.empty()) { - size_t pos=executable.find(project_path.string()); + size_t pos=executable.find(project_path); if(pos!=std::string::npos) - executable.replace(pos, project_path.string().size(), build_path.string()); + executable.replace(pos, project_path.size(), build_path.string()); } arguments=filesystem::escape_argument(executable); } else - arguments=filesystem::escape_argument(CMake::get_debug_build_path(cmake->project_path)); + arguments=filesystem::escape_argument(cmake->get_debug_build_path()); } return {project_path, arguments}; @@ -216,10 +214,10 @@ void Project::Clang::debug_start() { return; auto project_path=cmake->project_path; - auto debug_build_path=CMake::get_debug_build_path(project_path); + auto debug_build_path=cmake->get_debug_build_path(); if(debug_build_path.empty()) return; - if(!CMake::create_debug_build(project_path)) + if(!cmake->create_debug_build()) return; auto run_arguments_it=debug_run_arguments.find(project_path.string()); diff --git a/src/source_clang.cc b/src/source_clang.cc index 2130993..cc6c6ee 100644 --- a/src/source_clang.cc +++ b/src/source_clang.cc @@ -178,7 +178,7 @@ void Source::ClangViewParse::soft_reparse() { } std::vector Source::ClangViewParse::get_compilation_commands() { - clang::CompilationDatabase db(CMake::get_default_build_path(project_path).string()); + clang::CompilationDatabase db(CMake(project_path).get_default_build_path().string()); clang::CompileCommands commands(file_path.string(), db); std::vector cmds = commands.get_commands(); std::vector arguments;