main.cpp: Add emscripten support to the "--output" argument

Signed-off-by: Username404 <w.iron.zombie@gmail.com>
This commit is contained in:
Username404 2023-06-16 12:08:54 +02:00
parent c1b0184b64
commit ade66e8343
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
3 changed files with 41 additions and 27 deletions

View File

@ -175,7 +175,7 @@ set(CPACK_PACKAGE_INSTALL_DIRECTORY "${PROJECT_NAME} ${CMAKE_PROJECT_VERSION_MAJ
set(CPACK_PACKAGE_FILE_NAME "${LOWERCASE_PROJECT_NAME}-${PROJECT_VERSION}-${TIME}.${CMAKE_SYSTEM_PROCESSOR}")
include_directories(${CMAKE_CURRENT_LIST_DIR})
add_executable(${EXENAME} src/main.cpp ${CMAKE_CURRENT_BINARY_DIR}/processed/${PROJECT_NAME}.rc src/etc/filefuncs.cpp src/etc/lexer.cpp src/headers/lex.hpp src/headers/misc.hpp src/headers/parsing/ParseComponents.hpp src/headers/transpiler/Target.hpp src/headers/transpiler/implementations/Lua.hpp src/headers/transpiler/implementations/Js.hpp src/headers/transpiler/implementations/Py.hpp src/headers/parsing/Parser.hpp src/headers/arguments.hpp src/headers/parsing/ReservedIdentifiers.hpp)
add_executable(${EXENAME} src/main.cpp ${CMAKE_CURRENT_BINARY_DIR}/processed/${PROJECT_NAME}.rc src/etc/filefuncs.cpp src/etc/lexer.cpp src/headers/lex.hpp src/headers/misc.hpp src/headers/parsing/ParseComponents.hpp src/headers/transpiler/Target.hpp src/headers/transpiler/implementations/Lua.hpp src/headers/transpiler/implementations/Js.hpp src/headers/transpiler/implementations/Py.hpp src/headers/parsing/Parser.hpp src/headers/arguments.hpp src/headers/parsing/ReservedIdentifiers.hpp src/headers/emscripten_compatibility.h)
target_compile_definitions(${EXENAME} PRIVATE YBCON_VERSION="${CODENAME} ${PROJECT_VERSION}")
target_precompile_headers(${EXENAME} PRIVATE src/headers/Yerbacon.hpp)
if (Threads_FOUND)

View File

@ -0,0 +1,36 @@
#ifndef YERBACON_EMSCRIPTEN_COMPATIBILITY_H
#define YERBACON_EMSCRIPTEN_COMPATIBILITY_H
#include <string_view>
#include <string>
#include <filesystem>
filesystem::path mount(const std::string_view& element_path) {
filesystem::path file_path = element_path;
#ifdef EMSCRIPTEN
if (element_path.size() >= 2 and element_path[1] == ':') {
auto path = string(element_path.begin() + 2, element_path.end());
replace(path.begin(), path.end(), '\\', '/');
file_path = path;
}
const string directory = filesystem::path(file_path).remove_filename();
cout << "Directory: " << directory << endl;
for (unsigned int character = 1; character < directory.size(); ++character) {
const string sub_directory = directory.substr(0, character);
if (is_node and (directory[character] == '/') and not filesystem::is_directory(sub_directory)) {
MAIN_THREAD_EM_ASM({
const sub_directory = UTF8ToString($0);
if (!FS.isMountpoint(sub_directory)) {
try {
FS.mkdir(sub_directory);
FS.mount(NODEFS, {root: sub_directory}, sub_directory);
} catch (exception) {}
};
}, sub_directory.c_str());
}
}
#endif
return file_path;
}
#endif //YERBACON_EMSCRIPTEN_COMPATIBILITY_H

View File

@ -8,6 +8,7 @@ using namespace std;
#include "headers/misc.hpp"
#include "headers/arguments.hpp"
#include "headers/transpiler/Target.hpp"
#include "headers/emscripten_compatibility.h"
int main(int argc, char* argv[]) {
setlocale(LC_ALL, "");
@ -44,38 +45,15 @@ int main(int argc, char* argv[]) {
else if (currentArgument == ArgumentShort("text")) printResult = text_provided = true;
else if (currentArgument == ArgumentShort("output"))
if ((not output_directory.has_value()) and Units.empty() and (i + 1) != argc) {
mount(argv[i + 1]);
output_directory = argv[i + 1]; ++i;
} else goto invalid_argument;
else if (text_provided || currentArgument.ends_with(".ybcon")) {
#ifdef _OPENMP
if (not parallel) omp_set_num_threads(1);
#endif
optional file_path = make_optional<filesystem::path>();
if (not text_provided) {
file_path = filesystem::path(currentArgument);
#ifdef EMSCRIPTEN
if (currentArgument.size() >= 2 and currentArgument[1] == ':') {
auto path = string(currentArgument).erase(0, 2);
replace(path.begin(), path.end(), '\\', '/');
file_path = path;
}
const string directory = filesystem::path(file_path.value()).remove_filename();
for (unsigned int character = 1; character < directory.size(); ++character) {
const string sub_directory = directory.substr(0, character);
if (is_node and (directory[character] == '/') and not filesystem::is_directory(sub_directory)) {
MAIN_THREAD_EM_ASM({
const sub_directory = UTF8ToString($0);
if (!FS.isMountpoint(sub_directory)) {
try {
FS.mkdir(sub_directory);
FS.mount(NODEFS, {root: sub_directory}, sub_directory);
} catch (exception) {}
};
}, sub_directory.c_str());
}
}
#endif
}
optional<filesystem::path> file_path;
if (not text_provided) file_path = mount(currentArgument);
Units.insert_or_assign(currentArgument, async(not parallel ? launch::deferred : launch::async, [&, currentArgument, file_path, index = Units.size() + 1]() {
unit_result resultingPair;
try {