Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for external generators in RTE Model #1309

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions libs/crossplatform/include/CrossPlatformUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ class CrossPlatformUtils
*/
static bool CanExecute(const std::string& file);

/**
* @brief execute shell command
* @param cmd string shell command to be executed
* @return command execution result <string, error_code>
*/
static const std::pair<std::string, int> ExecCommand(const std::string& cmd);

enum class REG_STATUS {
ENABLED,
DISABLED,
Expand Down
24 changes: 24 additions & 0 deletions libs/crossplatform/src/CrossPlatformUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include "CrossPlatformUtils.h"
#include "CrossPlatform.h"
#include "constants.h"

#include <time.h>
#include <array>
#include <functional>

std::string CrossPlatformUtils::GetEnv(const std::string& name)
{
Expand Down Expand Up @@ -64,4 +67,25 @@ const std::string& CrossPlatformUtils::GetHostType()
return os_name;
}


const std::pair<std::string, int> CrossPlatformUtils::ExecCommand(const std::string& cmd)
{
std::array<char, 128> buffer;
std::string result;
int ret_code = -1;
std::function<int(FILE*)> close = _pclose;
std::function<FILE* (const char*, const char*)> open = _popen;

auto deleter = [&close, &ret_code](FILE* cmd) { ret_code = close(cmd); };
{
const std::unique_ptr<FILE, decltype(deleter)> pipe(open(cmd.c_str(), "r"), deleter);
if (pipe) {
while (fgets(buffer.data(), static_cast<int>(buffer.size()), pipe.get()) != nullptr) {
result += buffer.data();
}
}
}
return std::make_pair(result, ret_code);
}

// end of CrossplatformUtils.cpp
15 changes: 15 additions & 0 deletions libs/crossplatform/test/src/UnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,19 @@ TEST(CrossPlatformUnitTests, GetLongPathRegStatus) {
EXPECT_EQ(CrossPlatformUtils::REG_STATUS::NOT_SUPPORTED, status);
}
}

TEST(CrossPlatformUnitTests, ExecCommand) {
auto result = CrossPlatformUtils::ExecCommand("invalid command");
EXPECT_EQ(false, (0 == result.second) ? true : false) << result.first;

string testdir = "mkdir_test_dir";
error_code ec;
if (filesystem::exists(testdir)) {
filesystem::remove(testdir);
}
result = CrossPlatformUtils::ExecCommand("mkdir " + testdir);
EXPECT_TRUE(filesystem::exists(testdir));
EXPECT_EQ(true, (0 == result.second) ? true : false) << result.first;
}

// end of UnitTests.cpp
12 changes: 10 additions & 2 deletions libs/rtefsutils/include/RteFsUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
/******************************************************************************/
/*
* Copyright (c) 2020-2021 Arm Limited. All rights reserved.
* Copyright (c) 2020-2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -66,7 +66,7 @@ class RteFsUtils
* @param content string to be stored in the created file
* @return true if file is created
*/
static bool CreateFile(const std::string& file, const std::string& content);
static bool CreateTextFile(const std::string& file, const std::string& content);
/**
* @brief copy string to file in binary mode. Previous content of file is destroyed.
* @param fileName name of file
Expand Down Expand Up @@ -377,6 +377,14 @@ class RteFsUtils
* @return absolute file name if found, empty string otherwise
*/
static std::string FindFileInEtc(const std::string& fileName, const std::string& baseDir);

/**
* @brief get file category according to file extension
* @param filename with extension
* @return string category
*/
static const std::string& FileCategoryFromExtension(const std::string& file);

};

#endif // RteFsUtils_H
28 changes: 25 additions & 3 deletions libs/rtefsutils/src/RteFsUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ string RteFsUtils::BackupFile(const string& fileName, bool bDeleteExisting) {
return RteUtils::ERROR_STRING;
}

bool RteFsUtils::CreateFile(const string& file, const string& content) {
bool RteFsUtils::CreateTextFile(const string& file, const string& content) {
// Create file and directories
error_code ec;
fs::create_directories(fs::path(file).parent_path(), ec);
Expand Down Expand Up @@ -233,7 +233,7 @@ bool RteFsUtils::CopyBufferToFile(const string& fileName, const string& buffer,
}
}

return CreateFile(fileName, buffer);
return CreateTextFile(fileName, buffer);
}

bool RteFsUtils::CopyMergeFile(const string& src, const string& dst, int nInstance, bool backup) {
Expand Down Expand Up @@ -729,7 +729,8 @@ void RteFsUtils::GetMatchingFiles(list<string>& files, const string& extension,
const fs::path& p = entry.path();
string filename = p.filename().generic_string();
if (fs::is_regular_file(p)) {
if (p.extension() == extension) {
auto pos = filename.rfind(extension);
if (pos != string::npos && pos == (filename.size() - extension.size())) {
files.push_back(p.generic_string()); // insert full absolute path
bFound = true;
}
Expand Down Expand Up @@ -840,4 +841,25 @@ std::string RteFsUtils::FindFileInEtc(const std::string& fileName, const std::st
return FindFile(fileName, baseDir, relSearchOrder);
}

const string& RteFsUtils::FileCategoryFromExtension(const string& file) {
static const string& OTHER = "other";
static const map<string, vector<string>> CATEGORIES = {
{"sourceC", {".c", ".C"}},
{"sourceCpp", {".cpp", ".c++", ".C++", ".cxx", ".cc", ".CC"}},
{"sourceAsm", {".asm", ".s", ".S"}},
{"header", {".h", ".hpp"}},
{"library", {".a", ".lib"}},
{"object", {".o"}},
{"linkerScript", {".sct", ".scf", ".ld", ".icf", ".src"}},
{"doc", {".txt", ".md", ".pdf", ".htm", ".html"}},
};
fs::path ext((fs::path(file)).extension());
for (const auto& category : CATEGORIES) {
if (find(category.second.begin(), category.second.end(), ext) != category.second.end()) {
return category.first;
}
}
return OTHER;
}

// End of RteFsUtils.cpp
Loading
Loading