Skip to content

Commit

Permalink
Read the target name from Makefile (#468)
Browse files Browse the repository at this point in the history
* Read the target name and target dir from Makefile and set it in settings
  • Loading branch information
jackburton79 authored Dec 23, 2024
1 parent b8d5526 commit 03bdffa
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 5 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
107 changes: 107 additions & 0 deletions src/helpers/MakeFileHandler.cpp
Original file line number Diff line number Diff line change
@@ -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 <fstream>
#include <iostream>

#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);
}
29 changes: 29 additions & 0 deletions src/helpers/MakeFileHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2024, the Genio team
* All rights reserved. Distributed under the terms of the MIT license.
*/

#pragma once

#include <String.h>

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;
};
18 changes: 15 additions & 3 deletions src/helpers/TextUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@


#include "TextUtils.h"

#include <algorithm>


const std::string kWordCharacters ("_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
const std::string kWhiteSpaces (" \n\t\r");

Expand All @@ -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);
}
2 changes: 2 additions & 0 deletions src/helpers/TextUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 13 additions & 2 deletions src/project/ProjectFolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
#include "ConfigManager.h"
#include "LSPProjectWrapper.h"
#include "LSPServersManager.h"
#include "MakeFileHandler.h"
#include "GenioNamespace.h"
#include "GSettings.h"

#include <iostream>

#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "ProjectSettingsWindow"

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 03bdffa

Please sign in to comment.