Skip to content
Draft
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
10 changes: 9 additions & 1 deletion lute/cli/include/lute/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include "Luau/FileUtils.h"

#include <filesystem>
#include <optional>

struct AppendedBytecodeResult
{
bool found = false;
Expand All @@ -10,4 +13,9 @@ struct AppendedBytecodeResult

AppendedBytecodeResult checkForAppendedBytecode(const std::string& executablePath);

int compileScript(const std::string& inputFilePath, const std::string& outputFilePath, const std::string& currentExecutablePath);
std::string getBinaryName(const std::string& target);
std::filesystem::path getCachePath(const std::string& version, const std::string& target);
bool downloadBinary(const std::string& url, const std::filesystem::path& outputPath);
std::optional<std::filesystem::path> ensureBinaryAvailable(const std::string& target);

int compileScript(const std::string& inputFilePath, const std::string& outputFilePath, const std::string& currentExecutablePath, const std::optional<std::string>& target = std::nullopt);
70 changes: 69 additions & 1 deletion lute/cli/src/climain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#include <string>
#include <filesystem>
#include <vector>
#include <optional>
#include <array>
#include <algorithm>

static int program_argc = 0;
static char** program_argv = nullptr;
Expand Down Expand Up @@ -193,6 +196,10 @@ static void displayCompileHelp(const char* argv0)
printf("Usage: lute compile <script.luau> [output_executable]\n");
printf("\n");
printf("Compile Options:\n");
printf(" --target <platform> Target platform for cross-compilation.\n");
printf(" Supported: linux-aarch64, linux-x86_64,\n");
printf(" macos-aarch64, windows-x86_64\n");
printf(" If not specified, uses current platform binary.\n");
printf(" output_executable Optional name for the compiled executable.\n");
printf(" Defaults to '<script_name>_compiled'.\n");
printf(" -h, --help Display this usage message.\n");
Expand Down Expand Up @@ -319,6 +326,18 @@ int handleCompileCommand(int argc, char** argv, int argOffset)
{
std::string inputFilePath;
std::string outputFilePath;
std::optional<std::string> target;

const std::array<std::string, 4> supportedTargets = {
"linux-aarch64",
"linux-x86_64",
"macos-aarch64",
"windows-x86_64"
};

auto ensureSupportedTarget = [&](const std::string& value) -> bool {
return std::find(supportedTargets.begin(), supportedTargets.end(), value) != supportedTargets.end();
};

for (int i = argOffset; i < argc; ++i)
{
Expand All @@ -329,6 +348,49 @@ int handleCompileCommand(int argc, char** argv, int argOffset)
displayCompileHelp(argv[0]);
return 0;
}
else if (strncmp(currentArg, "--target=", 9) == 0)
{
if (target)
{
fprintf(stderr, "Error: Duplicate '--target' option provided.\n\n");
displayCompileHelp(argv[0]);
return 1;
}

std::string value = currentArg + 9;
if (value.empty())
{
fprintf(stderr, "Error: '--target' option requires a value.\n\n");
displayCompileHelp(argv[0]);
return 1;
}

target = value;
}
else if (strcmp(currentArg, "--target") == 0)
{
if (target)
{
fprintf(stderr, "Error: Duplicate '--target' option provided.\n\n");
displayCompileHelp(argv[0]);
return 1;
}

if (i + 1 >= argc)
{
fprintf(stderr, "Error: '--target' option requires a value.\n\n");
displayCompileHelp(argv[0]);
return 1;
}

target = argv[++i];
}
else if (currentArg[0] == '-')
{
fprintf(stderr, "Error: Unrecognized option '%s' for 'compile' command.\n\n", currentArg);
displayCompileHelp(argv[0]);
return 1;
}
else if (inputFilePath.empty())
{
inputFilePath = currentArg;
Expand All @@ -352,6 +414,12 @@ int handleCompileCommand(int argc, char** argv, int argOffset)
return 1;
}

if (target && !ensureSupportedTarget(*target))
{
fprintf(stderr, "Unsupported target platform: %s. Supported: linux-aarch64, linux-x86_64, macos-aarch64, windows-x86_64\n", target->c_str());
return 1;
}

if (outputFilePath.empty())
{
std::string inputBase = inputFilePath;
Expand All @@ -378,7 +446,7 @@ int handleCompileCommand(int argc, char** argv, int argOffset)
#endif
}

return compileScript(inputFilePath, outputFilePath, argv[0]);
return compileScript(inputFilePath, outputFilePath, argv[0], target);
}

int handleCliCommand(CliCommandResult result)
Expand Down
Loading
Loading