diff --git a/Makefile b/Makefile index 8f34051b..6dbf70f8 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,7 @@ SRCS += src/helpers/ActionManager.cpp SRCS += src/helpers/FSUtils.cpp SRCS += src/helpers/Languages.cpp SRCS += src/helpers/Logger.cpp +SRCS += src/helpers/MakeFileHandler.cpp SRCS += src/helpers/GSettings.cpp SRCS += src/helpers/GrepThread.cpp SRCS += src/helpers/PipeImage.cpp diff --git a/src/helpers/MakeFileHandler.cpp b/src/helpers/MakeFileHandler.cpp new file mode 100644 index 00000000..34a782b8 --- /dev/null +++ b/src/helpers/MakeFileHandler.cpp @@ -0,0 +1,107 @@ +/* + * Copyright 2024, the Genio team + * All rights reserved. Distributed under the terms of the MIT license. + */ + +// TODO: Very basic, it only reads the "NAME" from haiku makefiles. +// Should implement a real makefile reader + +#include "MakeFileHandler.h" + +#include +#include + +#include "TextUtils.h" + +MakeFileHandler::MakeFileHandler() +{ +} + + +MakeFileHandler::MakeFileHandler(const char* path) +{ + SetTo(path); +} + + +MakeFileHandler::~MakeFileHandler() +{ +} + + +status_t +MakeFileHandler::SetTo(const char* path) +{ + std::ifstream inFile(path); + std::string line; + bool foundName = false; + bool foundDir = false; + while (std::getline(inFile, line)) { + if (!foundName) { + size_t pos = line.find("NAME"); + if (pos != std::string::npos) { + line = line.substr(pos); + pos = line.find("="); + if (pos != std::string::npos) { + std::string str = line.substr(pos + 1).c_str(); + Trim(str); + fTargetName = str.c_str(); + foundName = true; + } + } + } + if (!foundDir) { + size_t pos = line.find("TARGET_DIR"); + if (pos != std::string::npos) { + line = line.substr(pos); + pos = line.find("="); + if (pos != std::string::npos) { + std::string str = line.substr(pos + 1).c_str(); + Trim(str); + fTargetDir = str.c_str(); + foundDir = true; + } + } + } + } + return B_OK; +} + + +void +MakeFileHandler::GetTargetName(BString& outName) const +{ + outName = fTargetName; +} + + +void +MakeFileHandler::SetTargetName(const BString& inName) +{ + fTargetName = inName; +} + + +void +MakeFileHandler::GetTargetDirectory(BString& outDir) const +{ + outDir = fTargetDir; +} + + +void +MakeFileHandler::SetTargetDirectory(const BString& inDir) +{ + fTargetDir = inDir; +} + + +void +MakeFileHandler::GetFullTargetName(BString &fullName) const +{ + if (!fTargetDir.IsEmpty()) { + fullName = fTargetDir; + fullName.Append("/"); + } + fullName.Append(fTargetName); +} diff --git a/src/helpers/MakeFileHandler.h b/src/helpers/MakeFileHandler.h new file mode 100644 index 00000000..a174376e --- /dev/null +++ b/src/helpers/MakeFileHandler.h @@ -0,0 +1,29 @@ +/* + * Copyright 2024, the Genio team + * All rights reserved. Distributed under the terms of the MIT license. + */ + +#pragma once + +#include + +class MakeFileHandler { +public: + MakeFileHandler(); + MakeFileHandler(const char* path); + virtual ~MakeFileHandler(); + + status_t SetTo(const char* path); + + void GetTargetName(BString &name) const; + void SetTargetName(const BString& name); + + void GetTargetDirectory(BString& dir) const; + void SetTargetDirectory(const BString& dir); + + void GetFullTargetName(BString& fullName) const; + +private: + BString fTargetName; + BString fTargetDir; +}; diff --git a/src/helpers/TextUtils.cpp b/src/helpers/TextUtils.cpp index ec1297ba..85edf641 100644 --- a/src/helpers/TextUtils.cpp +++ b/src/helpers/TextUtils.cpp @@ -5,8 +5,10 @@ #include "TextUtils.h" + #include + const std::string kWordCharacters ("_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); const std::string kWhiteSpaces (" \n\t\r"); @@ -27,8 +29,18 @@ const BString EscapeQuotesWrap(const BString& path) { // trim from start (in place) void LeftTrim(std::string &s) { - s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { - return !std::isspace(ch); - })); + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { + return !std::isspace(ch); + })); +} + +void RightTrim(std::string& s) { + s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { + return !std::isspace(ch); + }).base(), s.end()); } +void Trim(std::string& s) { + LeftTrim(s); + RightTrim(s); +} \ No newline at end of file diff --git a/src/helpers/TextUtils.h b/src/helpers/TextUtils.h index 0570d55f..cdcec23d 100644 --- a/src/helpers/TextUtils.h +++ b/src/helpers/TextUtils.h @@ -20,6 +20,8 @@ const BString EscapeQuotesWrap(const BString& string); // trim from start (in place) void LeftTrim(std::string &s); +void RightTrim(std::string &s); +void Trim(std::string &s); #endif // TextUtils_H diff --git a/src/project/ProjectFolder.cpp b/src/project/ProjectFolder.cpp index 801b8aec..b5e2b7d6 100644 --- a/src/project/ProjectFolder.cpp +++ b/src/project/ProjectFolder.cpp @@ -15,9 +15,12 @@ #include "ConfigManager.h" #include "LSPProjectWrapper.h" #include "LSPServersManager.h" +#include "MakeFileHandler.h" #include "GenioNamespace.h" #include "GSettings.h" +#include + #undef B_TRANSLATION_CONTEXT #define B_TRANSLATION_CONTEXT "ProjectSettingsWindow" @@ -237,15 +240,23 @@ ProjectFolder::GuessBuildCommand() // guess builder type // TODO: move this away from here, into a specialized class // and maybe into plugins - if (strcasecmp(entry.Name(), "makefile") == 0) { + if (::strcasecmp(entry.Name(), "makefile") == 0) { // builder: make SetBuildCommand("make", BuildMode::ReleaseMode); SetCleanCommand("make clean", BuildMode::ReleaseMode); SetBuildCommand("make DEBUGGER=1", BuildMode::DebugMode); SetCleanCommand("make DEBUGGER=1 clean", BuildMode::DebugMode); LogInfo("Guessed builder: make"); + BPath makeFilePath; + if (entry.GetPath(&makeFilePath) == B_OK) { + MakeFileHandler handler(makeFilePath.Path()); + BString targetName; + handler.GetFullTargetName(targetName); + SetTarget(targetName, BuildMode::ReleaseMode); + SetTarget(targetName, BuildMode::DebugMode); + } break; - } else if (strcasecmp(entry.Name(), "jamfile") == 0) { + } else if (::strcasecmp(entry.Name(), "jamfile") == 0) { // builder: jam SetBuildCommand("jam", BuildMode::ReleaseMode); SetCleanCommand("jam clean", BuildMode::ReleaseMode);