Skip to content

Commit

Permalink
Merge pull request #3866 from Sonicadvance1/ArgumentLoader_atexit_remove
Browse files Browse the repository at this point in the history
ArgumentLoader: Removes static fextl::vector usage
  • Loading branch information
Sonicadvance1 authored Jul 18, 2024
2 parents d385e49 + a5d3692 commit f81fc4e
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 59 deletions.
28 changes: 12 additions & 16 deletions Source/Common/ArgumentLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
#include <stdint.h>

namespace FEX::ArgLoader {
fextl::vector<fextl::string> RemainingArgs;
fextl::vector<fextl::string> ProgramArguments;

static fextl::string Version = "FEX-Emu (" GIT_DESCRIBE_STRING ") ";
void FEX::ArgLoader::ArgLoader::Load() {
RemainingArgs.clear();
ProgramArguments.clear();
if (Type == LoadType::WITHOUT_FEXLOADER_PARSER) {
LoadWithoutArguments();
return;
}

optparse::OptionParser Parser {};
Parser.version(Version);
Parser.version("FEX-Emu (" GIT_DESCRIBE_STRING ") ");
optparse::OptionGroup CPUGroup(Parser, "CPU Core options");
optparse::OptionGroup EmulationGroup(Parser, "Emulation options");
optparse::OptionGroup DebugGroup(Parser, "Debug options");
Expand Down Expand Up @@ -45,21 +48,14 @@ void FEX::ArgLoader::ArgLoader::Load() {
ProgramArguments = Parser.parsed_args();
}

void LoadWithoutArguments(int _argc, char** _argv) {
void FEX::ArgLoader::ArgLoader::LoadWithoutArguments() {
// Skip argument 0, which will be the interpreter
for (int i = 1; i < _argc; ++i) {
RemainingArgs.emplace_back(_argv[i]);
for (int i = 1; i < argc; ++i) {
RemainingArgs.emplace_back(argv[i]);
}

// Put the interpreter in ProgramArguments
ProgramArguments.emplace_back(_argv[0]);
}

fextl::vector<fextl::string> Get() {
return RemainingArgs;
}
fextl::vector<fextl::string> GetParsedArgs() {
return ProgramArguments;
ProgramArguments.emplace_back(argv[0]);
}

} // namespace FEX::ArgLoader
36 changes: 28 additions & 8 deletions Source/Common/ArgumentLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,39 @@
namespace FEX::ArgLoader {
class ArgLoader final : public FEXCore::Config::Layer {
public:
explicit ArgLoader(int _argc, char** _argv)
enum class LoadType {
WITH_FEXLOADER_PARSER,
WITHOUT_FEXLOADER_PARSER,
};

explicit ArgLoader(LoadType Type, int argc, char** argv)
: FEXCore::Config::Layer(FEXCore::Config::LayerType::LAYER_ARGUMENTS)
, argc {_argc}
, argv {_argv} {}
, Type {Type}
, argc {argc}
, argv {argv} {
Load();
}

void Load() override;
void LoadWithoutArguments();
fextl::vector<fextl::string> Get() {
return RemainingArgs;
}
fextl::vector<fextl::string> GetParsedArgs() {
return ProgramArguments;
}

void Load();
LoadType GetLoadType() const {
return Type;
}

private:
LoadType Type;
int argc {};
char** argv;
char** argv {};

fextl::vector<fextl::string> RemainingArgs {};
fextl::vector<fextl::string> ProgramArguments {};
};

void LoadWithoutArguments(int _argc, char** _argv);
fextl::vector<fextl::string> Get();
fextl::vector<fextl::string> GetParsedArgs();
} // namespace FEX::ArgLoader
43 changes: 23 additions & 20 deletions Source/Common/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,40 +326,27 @@ fextl::string RecoverGuestProgramFilename(fextl::string Program, bool ExecFDInte
return Program;
}

ApplicationNames
LoadConfig(bool NoFEXArguments, bool LoadProgramConfig, int argc, char** argv, char** const envp, bool ExecFDInterp, int ProgramFDFromEnv) {
ApplicationNames LoadConfig(fextl::unique_ptr<FEX::ArgLoader::ArgLoader> ArgsLoader, bool LoadProgramConfig, char** const envp,
bool ExecFDInterp, int ProgramFDFromEnv) {
FEX::Config::InitializeConfigs();
FEXCore::Config::Initialize();
FEXCore::Config::AddLayer(CreateGlobalMainLayer());
FEXCore::Config::AddLayer(CreateMainLayer());

if (NoFEXArguments) {
FEX::ArgLoader::LoadWithoutArguments(argc, argv);
} else {
FEXCore::Config::AddLayer(fextl::make_unique<FEX::ArgLoader::ArgLoader>(argc, argv));
}

const char* AppConfig = getenv("FEX_APP_CONFIG");
if (AppConfig && FHU::Filesystem::Exists(AppConfig)) {
FEXCore::Config::AddLayer(CreateUserOverrideLayer(AppConfig));
}

FEXCore::Config::AddLayer(CreateEnvironmentLayer(envp));
FEXCore::Config::Load();

auto Args = FEX::ArgLoader::Get();
auto Args = ArgsLoader->Get();

fextl::string Program {};
fextl::string ProgramName {};
if (LoadProgramConfig) {
if (Args.empty()) {
// Early exit if we weren't passed an argument
return {};
}

Args[0] = RecoverGuestProgramFilename(std::move(Args[0]), ExecFDInterp, ProgramFDFromEnv);
fextl::string& Program = Args[0];
Program = Args[0];

bool Wine = false;
fextl::string ProgramName;
for (size_t CurrentProgramNameIndex = 0; CurrentProgramNameIndex < Args.size(); ++CurrentProgramNameIndex) {
auto CurrentProgramName = FHU::Filesystem::GetFilename(Args[CurrentProgramNameIndex]);

Expand Down Expand Up @@ -404,10 +391,26 @@ LoadConfig(bool NoFEXArguments, bool LoadProgramConfig, int argc, char** argv, c
FEXCore::Config::AddLayer(CreateAppLayer(SteamAppName, FEXCore::Config::LayerType::LAYER_GLOBAL_STEAM_APP));
FEXCore::Config::AddLayer(CreateAppLayer(SteamAppName, FEXCore::Config::LayerType::LAYER_LOCAL_STEAM_APP));
}
}

if (ArgsLoader->GetLoadType() == FEX::ArgLoader::ArgLoader::LoadType::WITH_FEXLOADER_PARSER) {
FEXCore::Config::AddLayer(std::move(ArgsLoader));
}

const char* AppConfig = getenv("FEX_APP_CONFIG");
if (AppConfig && FHU::Filesystem::Exists(AppConfig)) {
FEXCore::Config::AddLayer(CreateUserOverrideLayer(AppConfig));
}

FEXCore::Config::AddLayer(CreateEnvironmentLayer(envp));
FEXCore::Config::Load();


if (LoadProgramConfig) {
return ApplicationNames {std::move(Program), std::move(ProgramName)};
} else {
return {};
}
return {};
}

#ifndef _WIN32
Expand Down
11 changes: 6 additions & 5 deletions Source/Common/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include <FEXCore/Config/Config.h>
#include <FEXCore/fextl/string.h>

namespace FEX::ArgLoader {
class ArgLoader;
}
/**
* @brief This is a singleton for storing global configuration state
*/
Expand All @@ -28,18 +31,16 @@ struct ApplicationNames {
/**
* @brief Loads the FEX and application configurations for the application that is getting ready to run.
*
* @param NoFEXArguments Do we want to parse FEXLoader arguments, Or is this FEXInterpreter?
* @param ArgLoader Argument loader for argument based config options
* @param LoadProgramConfig Do we want to load application specific configurations?
* @param argc The `argc` passed to main(...)
* @param argv The `argv` passed to main(...)
* @param envp The `envp` passed to main(...)
* @param ExecFDInterp If FEX was executed with binfmt_misc FD argument
* @param ProgramFDFromEnv The execveat FD argument passed through FEX
*
* @return The application name and path structure
*/
ApplicationNames
LoadConfig(bool NoFEXArguments, bool LoadProgramConfig, int argc, char** argv, char** const envp, bool ExecFDInterp, int ProgramFDFromEnv);
ApplicationNames LoadConfig(fextl::unique_ptr<FEX::ArgLoader::ArgLoader> ArgLoader, bool LoadProgramConfig, char** const envp,
bool ExecFDInterp, int ProgramFDFromEnv);

const char* GetHomeDirectory();

Expand Down
4 changes: 2 additions & 2 deletions Source/Tools/FEXBash/FEXBash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ int main(int argc, char** argv, char** const envp) {
FEXCore::Config::Initialize();
FEXCore::Config::AddLayer(FEX::Config::CreateGlobalMainLayer());
FEXCore::Config::AddLayer(FEX::Config::CreateMainLayer());
FEX::ArgLoader::LoadWithoutArguments(argc, argv);
FEX::ArgLoader::ArgLoader ArgsLoader(FEX::ArgLoader::ArgLoader::LoadType::WITHOUT_FEXLOADER_PARSER, argc, argv);
FEXCore::Config::AddLayer(FEX::Config::CreateEnvironmentLayer(envp));
FEXCore::Config::Load();

// Reload the meta layer
FEXCore::Config::ReloadMetaLayer();

auto Args = FEX::ArgLoader::Get();
auto Args = ArgsLoader.Get();

// Ensure FEXServer is setup before config options try to pull CONFIG_ROOTFS
if (!FEXServerClient::SetupClient(argv[0])) {
Expand Down
10 changes: 6 additions & 4 deletions Source/Tools/FEXLoader/FEXLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,18 @@ int main(int argc, char** argv, char** const envp) {
LogMan::Throw::InstallHandler(AssertHandler);
LogMan::Msg::InstallHandler(MsgHandler);

auto Program = FEX::Config::LoadConfig(IsInterpreter, true, argc, argv, envp, ExecutedWithFD, FEXFD);
auto ArgsLoader = fextl::make_unique<FEX::ArgLoader::ArgLoader>(
IsInterpreter ? FEX::ArgLoader::ArgLoader::LoadType::WITHOUT_FEXLOADER_PARSER : FEX::ArgLoader::ArgLoader::LoadType::WITH_FEXLOADER_PARSER,
argc, argv);
auto Args = ArgsLoader->Get();
auto ParsedArgs = ArgsLoader->GetParsedArgs();
auto Program = FEX::Config::LoadConfig(std::move(ArgsLoader), true, envp, ExecutedWithFD, FEXFD);

if (Program.ProgramPath.empty() && FEXFD == -1) {
// Early exit if we weren't passed an argument
return 0;
}

auto Args = FEX::ArgLoader::Get();
auto ParsedArgs = FEX::ArgLoader::GetParsedArgs();

// Reload the meta layer
FEXCore::Config::ReloadMetaLayer();
FEXCore::Config::Set(FEXCore::Config::CONFIG_IS_INTERPRETER, IsInterpreter ? "1" : "0");
Expand Down
4 changes: 3 additions & 1 deletion Source/Tools/FEXRootFSFetcher/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,9 @@ bool ExtractEroFS(const fextl::string& Path, const fextl::string& RootFS, const

int main(int argc, char** argv, char** const envp) {
CheckTTY();
FEX::Config::LoadConfig(true, false, argc, argv, envp, false, {});

auto ArgsLoader = fextl::make_unique<FEX::ArgLoader::ArgLoader>(FEX::ArgLoader::ArgLoader::LoadType::WITHOUT_FEXLOADER_PARSER, argc, argv);
FEX::Config::LoadConfig(std::move(ArgsLoader), false, envp, false, {});

// Reload the meta layer
FEXCore::Config::ReloadMetaLayer();
Expand Down
4 changes: 3 additions & 1 deletion Source/Tools/FEXServer/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "PipeScanner.h"
#include "ProcessPipe.h"
#include "SquashFS.h"
#include "Common/ArgumentLoader.h"
#include "Common/Config.h"
#include "Common/FEXServerClient.h"

Expand Down Expand Up @@ -115,7 +116,8 @@ int main(int argc, char** argv, char** const envp) {
DeparentSelf();
}

FEX::Config::LoadConfig(true, false, argc, argv, envp, false, {});
auto ArgsLoader = fextl::make_unique<FEX::ArgLoader::ArgLoader>(FEX::ArgLoader::ArgLoader::LoadType::WITHOUT_FEXLOADER_PARSER, argc, argv);
FEX::Config::LoadConfig(std::move(ArgsLoader), false, envp, false, {});

// Reload the meta layer
FEXCore::Config::ReloadMetaLayer();
Expand Down
5 changes: 3 additions & 2 deletions Source/Tools/TestHarnessRunner/TestHarnessRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,12 @@ int main(int argc, char** argv, char** const envp) {

FEX::Config::InitializeConfigs();
FEXCore::Config::Initialize();
FEXCore::Config::AddLayer(fextl::make_unique<FEX::ArgLoader::ArgLoader>(argc, argv));
auto ArgsLoader = fextl::make_unique<FEX::ArgLoader::ArgLoader>(FEX::ArgLoader::ArgLoader::LoadType::WITH_FEXLOADER_PARSER, argc, argv);
auto Args = ArgsLoader->Get();
FEXCore::Config::AddLayer(std::move(ArgsLoader));
FEXCore::Config::AddLayer(FEX::Config::CreateEnvironmentLayer(envp));
FEXCore::Config::Load();

auto Args = FEX::ArgLoader::Get();

if (Args.size() < 2) {
LogMan::Msg::EFmt("Not enough arguments");
Expand Down

0 comments on commit f81fc4e

Please sign in to comment.