feat(cli): create a build directory next to the Lua sources

The generated files landed in a luacpp-build folder named after the script, so
the executable could not be found where a build directory is usually expected.
The default layout is now build/<script> for the binary and build/src for the
generated sources, and the path is printed relative to the working directory.
This commit is contained in:
BlackAngelTV committed 2026-11-18 08:48:05 +01:00
1 parent fbdb3e73ba
commit 1bfbe79cba
3 files changed
+38 -16

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
# Build output
build/
luacpp-build/
Demo/build/
# Compiled objects
*.o
+14 -3
View File
@@ -34,8 +34,19 @@ cd build
`luacpp run` transpile le script **et tous les `.lua` du même dossier** (pour que
`require` fonctionne), génère un `main` C++, appelle le compilateur C++ et lance
l'exécutable. Tu n'écris que du Lua, aucun C++ : les fichiers générés restent dans
`luacpp-build/` (ou `-o <dir>`), à côté de l'exécutable.
l'exécutable. Tu n'écris que du Lua, aucun C++ : tout est rangé dans un dossier
`build/` créé à côté des sources Lua (ou dans `-o <dir>`) :
```
build/main l'exécutable, nommé d'après le script
build/src/ les .cpp/.hpp générés
```
Après un `luacpp run Demo/main.lua`, le programme est donc `Demo/build/main` :
```bash
cd Demo/build && ./main
```
Pour produire un exécutable sans le lancer : `./luacpp run --only-build script.lua`.
Pour utiliser un module depuis du C++ : `./luacpp module.lua -n demo -o gen`
@@ -48,7 +59,7 @@ luacpp run <script.lua> [args...] # transpile + compile + exécute
luacpp [options] <fichier.lua>... # génère seulement les packages .hpp/.cpp
-r, --root <dir> racine servant à dériver le nom du module Lua
-o, --output <dir> dossier des .hpp/.cpp générés (défaut : generated)
-o, --output <dir> dossier de sortie de `run` (défaut : build/ près des sources)
-n, --namespace <ns> préfixe de namespace C++, préfixe aussi le nom du module
--cxx <compilateur> compilateur C++ utilisé par `run` (défaut : c++)
--only-build `run` compile l'exécutable sans l'exécuter
+23 -12
View File
@@ -66,7 +66,7 @@ void usage() {
"\n"
"Options:\n"
" -r, --root <dir> root directory used to derive Lua module names\n"
" -o, --output <dir> output directory (run: ./luacpp-build/<name>)\n"
" -o, --output <dir> output directory (run: <root>/build)\n"
" -n, --namespace <ns> C++ namespace prefix, also prefixes module names\n"
" --cxx <compiler> C++ compiler used by `run` (default: c++)\n"
" --only-build `run` compiles the executable but does not execute it\n"
@@ -99,6 +99,13 @@ void write_file(const fs::path& path, const std::string& data) {
std::string quoted(const std::string& s) { return "\"" + s + "\""; }
// Path shown to the user: relative to the working directory when possible.
std::string display(const fs::path& path) {
std::error_code ec;
fs::path rel = fs::relative(path, fs::current_path(), ec);
return ec ? path.generic_string() : rel.generic_string();
}
std::string cpp_string(const std::string& s) {
std::string out = "\"";
for (unsigned char c : s) {
@@ -204,7 +211,7 @@ void generate_job(const Job& job, bool quiet) {
write_file(job.hpp, generated.header);
write_file(job.cpp, generated.source);
if (!quiet) {
std::cout << "luacpp: " << job.source.string() << " -> " << job.cpp.generic_string() << " ("
std::cout << "luacpp: " << display(job.source) << " -> " << display(job.cpp) << " ("
<< job.moduleName << " / " << job.ns << ")\n";
}
}
@@ -261,7 +268,8 @@ std::vector<fs::path> collect_lua_files(const fs::path& root) {
const fs::path& path = it->path();
if (it->is_directory(ec)) {
std::string name = path.filename().string();
if (name == "luacpp-build" || (!name.empty() && name[0] == '.')) it.disable_recursion_pending();
if (name == "build" || name == "luacpp-build" || (!name.empty() && name[0] == '.'))
it.disable_recursion_pending();
continue;
}
if (path.extension() == ".lua") files.push_back(path);
@@ -278,7 +286,11 @@ int run_script(const Options& opt) {
}
fs::path root = opt.root.empty() ? fs::absolute(entry).parent_path() : fs::absolute(opt.root);
fs::path outDir = opt.output.empty() ? fs::path("luacpp-build") / entry.stem() : opt.output;
// `run` writes a conventional build directory next to the Lua sources:
// build/<script> the executable
// build/src/ the generated .cpp/.hpp files
fs::path outDir = opt.output.empty() ? root / "build" : fs::absolute(opt.output);
fs::path srcDir = outDir / "src";
std::vector<fs::path> files = collect_lua_files(root);
if (files.empty()) {
@@ -289,7 +301,7 @@ int run_script(const Options& opt) {
std::vector<Job> jobs;
std::set<std::string> seen;
for (const auto& file : files) {
Job job = plan_job(file, root, outDir, opt.nsPrefix);
Job job = plan_job(file, root, srcDir, opt.nsPrefix);
if (!seen.insert(job.moduleName).second) continue;
jobs.push_back(job);
}
@@ -299,7 +311,7 @@ int run_script(const Options& opt) {
std::string entryModule = derive_module_name(entry, root);
if (!opt.nsPrefix.empty()) entryModule = opt.nsPrefix + "." + entryModule;
fs::path driver = outDir / "luacpp-main.cpp";
fs::path driver = srcDir / "luacpp-main.cpp";
write_file(driver, driver_source(entryModule));
fs::path exe = outDir / entry.stem();
@@ -310,22 +322,21 @@ int run_script(const Options& opt) {
std::string command = opt.cxx + " -std=c++17 -O2 -o " + quoted(exe.string());
for (const auto& job : jobs) command += " " + quoted(job.cpp.string());
command += " " + quoted(driver.string());
command += " -I" + quoted(outDir.string());
command += " -I" + quoted(srcDir.string());
command += " -I" + quoted(LUACPP_RUNTIME_INCLUDE_DIR);
command += " " + lua_link_flags();
if (opt.verbose) std::cerr << "luacpp: compile: " << command << "\n";
if (!opt.quiet) std::cout << "luacpp: building " << exe.string() << "\n";
if (!opt.quiet) std::cout << "luacpp: building " << display(exe) << "\n";
int compileStatus = std::system(command.c_str());
if (LUACPP_EXIT_STATUS(compileStatus) != 0) {
std::cerr << "luacpp: C++ compilation failed\n";
return 1;
}
if (opt.onlyBuild) {
std::cout << exe.string() << "\n";
return 0;
}
if (!opt.quiet) std::cout << "luacpp: executable: " << display(exe) << "\n";
if (opt.onlyBuild) return 0;
std::string runCommand = quoted(exe.string());
for (const auto& arg : opt.programArgs) runCommand += " " + quoted(arg);