177 lines
6.2 KiB
C++
177 lines
6.2 KiB
C++
// LightGUI CMake handling — see cmake_build.h.
|
|
#include "cmake_build.h"
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
// ---------------------------------------------------------------- settings
|
|
|
|
static std::string home_dir() {
|
|
const char* h = std::getenv("HOME");
|
|
return h ? h : "";
|
|
}
|
|
|
|
std::string CMakeSettings::default_path() {
|
|
const char* e = std::getenv("LIGHTGUI_CMAKE_CONF");
|
|
if (e && *e) return e;
|
|
return home_dir() + "/.config/lightgui/cmake.conf";
|
|
}
|
|
|
|
static bool ensure_parent_dir(const std::string& path) {
|
|
std::error_code ec;
|
|
size_t slash = path.rfind('/');
|
|
if (slash == std::string::npos) return true;
|
|
fs::create_directories(path.substr(0, slash), ec);
|
|
return !ec;
|
|
}
|
|
|
|
static std::string trim_s(const std::string& s) {
|
|
size_t a = s.find_first_not_of(" \t\r\n");
|
|
if (a == std::string::npos) return "";
|
|
size_t b = s.find_last_not_of(" \t\r\n");
|
|
return s.substr(a, b - a + 1);
|
|
}
|
|
|
|
bool CMakeSettings::save(const std::string& path) const {
|
|
std::string p = path.empty() ? default_path() : path;
|
|
ensure_parent_dir(p);
|
|
std::ofstream f(p);
|
|
if (!f) return false;
|
|
f << "# LightGUI CMake settings\n";
|
|
f << "cmake_path=" << cmake_path << "\n";
|
|
f << "generator=" << generator << "\n";
|
|
f << "build_type=" << build_type << "\n";
|
|
f << "build_dir=" << build_dir << "\n";
|
|
return (bool)f;
|
|
}
|
|
|
|
bool CMakeSettings::load(const std::string& path) {
|
|
std::string p = path.empty() ? default_path() : path;
|
|
std::ifstream f(p);
|
|
if (!f) return false;
|
|
std::string line;
|
|
while (std::getline(f, line)) {
|
|
line = trim_s(line);
|
|
if (line.empty() || line[0] == '#') continue;
|
|
size_t eq = line.find('=');
|
|
if (eq == std::string::npos) continue;
|
|
std::string key = trim_s(line.substr(0, eq));
|
|
std::string val = trim_s(line.substr(eq + 1));
|
|
if (key == "cmake_path") cmake_path = val;
|
|
else if (key == "generator") generator = val;
|
|
else if (key == "build_type") build_type = val;
|
|
else if (key == "build_dir") build_dir = val;
|
|
// Unknown keys are ignored, so newer settings files stay readable.
|
|
}
|
|
if (cmake_path.empty()) cmake_path = "cmake";
|
|
if (build_type.empty()) build_type = "Release";
|
|
if (build_dir.empty()) build_dir = "build";
|
|
return true;
|
|
}
|
|
|
|
// ---------------------------------------------------------------- emitter
|
|
|
|
std::string cmake_lists_text(const std::string& project_name,
|
|
const std::vector<std::string>& sources) {
|
|
std::ostringstream o;
|
|
o << "# Generated by LightGUI (C++/GTK). Build with:\n"
|
|
<< "# cmake -S . -B build && cmake --build build\n"
|
|
<< "# (or use File -> Build with CMake in the designer).\n"
|
|
<< "cmake_minimum_required(VERSION 3.10)\n"
|
|
<< "project(" << project_name << " CXX)\n\n"
|
|
<< "set(CMAKE_CXX_STANDARD 17)\n"
|
|
<< "set(CMAKE_CXX_STANDARD_REQUIRED ON)\n\n"
|
|
<< "find_package(PkgConfig REQUIRED)\n"
|
|
<< "pkg_check_modules(GTK3 REQUIRED gtk+-3.0)\n\n"
|
|
<< "add_executable(app\n";
|
|
for (const std::string& s : sources) o << " " << s << "\n";
|
|
o << ")\n\n"
|
|
<< "target_include_directories(app PRIVATE ${GTK3_INCLUDE_DIRS})\n"
|
|
<< "target_compile_options(app PRIVATE ${GTK3_CFLAGS_OTHER})\n"
|
|
<< "target_link_libraries(app PRIVATE ${GTK3_LIBRARIES})\n";
|
|
return o.str();
|
|
}
|
|
|
|
// ---------------------------------------------------------------- building
|
|
|
|
bool cmake_project_exists(const std::string& dir) {
|
|
std::error_code ec;
|
|
return fs::is_regular_file(fs::path(dir) / "CMakeLists.txt", ec);
|
|
}
|
|
|
|
std::string cmake_shell_quote(const std::string& s) {
|
|
std::string out = "'";
|
|
for (char c : s) {
|
|
if (c == '\'') out += "'\\''";
|
|
else out += c;
|
|
}
|
|
out += "'";
|
|
return out;
|
|
}
|
|
|
|
std::string cmake_exe_path(const std::string& dir,
|
|
const CMakeSettings& settings) {
|
|
fs::path d(dir);
|
|
if (d.is_relative()) d = fs::absolute(d);
|
|
return (d / settings.build_dir / "app").string();
|
|
}
|
|
|
|
// Run `cmd`, appending its combined stdout+stderr to `out`; true on exit 0.
|
|
static bool run_capture(const std::string& cmd, std::string& out) {
|
|
std::string full = cmd + " 2>&1";
|
|
FILE* p = ::popen(full.c_str(), "r");
|
|
if (!p) {
|
|
out += "cannot run: " + cmd + "\n";
|
|
return false;
|
|
}
|
|
char buf[4096];
|
|
size_t n;
|
|
while ((n = std::fread(buf, 1, sizeof buf, p)) > 0) out.append(buf, n);
|
|
int rc = ::pclose(p);
|
|
return rc == 0;
|
|
}
|
|
|
|
CMakeBuildResult cmake_build_project(const std::string& dir,
|
|
const CMakeSettings& settings) {
|
|
CMakeBuildResult r;
|
|
fs::path d(dir);
|
|
if (d.is_relative()) d = fs::absolute(d);
|
|
r.build_dir = (d / settings.build_dir).string();
|
|
r.exe_path = cmake_exe_path(dir, settings);
|
|
if (!cmake_project_exists(d.string())) {
|
|
r.output = "no CMakeLists.txt in " + d.string() + "\n";
|
|
return r;
|
|
}
|
|
std::string cmake = settings.cmake_path.empty()
|
|
? std::string("cmake")
|
|
: settings.cmake_path;
|
|
std::string gen;
|
|
if (!settings.generator.empty())
|
|
gen = " -G " + cmake_shell_quote(settings.generator);
|
|
std::string cfg = cmake_shell_quote(cmake) + " -S " +
|
|
cmake_shell_quote(d.string()) + " -B " +
|
|
cmake_shell_quote(r.build_dir) + gen +
|
|
" -DCMAKE_BUILD_TYPE=" +
|
|
cmake_shell_quote(settings.build_type.empty()
|
|
? std::string("Release")
|
|
: settings.build_type);
|
|
r.output += "$ " + cfg + "\n";
|
|
r.configured = run_capture(cfg, r.output);
|
|
if (!r.configured) return r;
|
|
std::string bld = cmake_shell_quote(cmake) + " --build " +
|
|
cmake_shell_quote(r.build_dir);
|
|
r.output += "$ " + bld + "\n";
|
|
bool built = run_capture(bld, r.output);
|
|
std::error_code ec;
|
|
r.ok = built && fs::is_regular_file(r.exe_path, ec);
|
|
if (built && !r.ok)
|
|
r.output += "build reported success but " + r.exe_path +
|
|
" is missing\n";
|
|
return r;
|
|
}
|