CMake handling: generate CMakeLists.txt, build with CMake from the designer, open/build existing CMake projects

- Generator emits CMakeLists.txt alongside Makefile (cmake_minimum_required
  3.10, project(<Class> CXX), C++17, PkgConfig gtk+-3.0, explicit source
  list, add_executable(app ...), GTK include/link wiring); exe named app
  to match the Makefile target.
- New src/cmake_build.h/.cpp: cmake_lists_text, cmake_build_project
  (configure + build, captured output), cmake_project_exists,
  CMakeSettings persisted to ~/.config/fdesigner/cmake.conf
  (FDESIGNER_CMAKE_CONF override for tests).
- Designer File menu: Build with CMake... (last generated project, or
  pick a folder), Open CMake Project... (any dir with a CMakeLists.txt),
  CMake Settings...; Generate now offers Build with CMake (the default
  build path for generated projects) and reports the log + exe path.
- Self-test gains a CMake phase (settings round-trip, CMakeLists.txt
  markers, real console build, broken-file failure, cmake build of the
  generated GTK project); file count 4+2N -> 5+2N.
- New tests/cmake_tests.sh + tests/cmake_probe.cpp (31 checks); matrix
  and widget test harnesses link cmake_build.cpp; tests/out/ gitignored.
- DESIGN.md: CMake handling section.
This commit is contained in:
root committed 2026-11-25 06:59:14 -05:00
1 parent 7828ced3d6
commit cd5f07f619
12 files changed
+837 -12

No files matched your search

+1
View File
@@ -2,3 +2,4 @@ fdesigner
*.o
tests/gen_matrix
tests/debugger/work/
tests/out/
+42
View File
@@ -257,6 +257,48 @@ panel in the main window). It drives real gdb over the MI protocol
Xvfb, GUI smoke test) — see `tests/debugger/` for the targets and the
headless harness `dbg_harness.cpp`, which drives `GdbSession` directly.
## CMake handling (2026-11-26)
The generator now also emits a `CMakeLists.txt` next to the `Makefile` in
every generated project, so a generated project builds with either tool.
The file sets `cmake_minimum_required(VERSION 3.10)`,
`project(<ClassName> CXX)`, C++17, `find_package(PkgConfig)` /
`pkg_check_modules(GTK3 REQUIRED gtk+-3.0)`, lists every generated `.cpp`
explicitly in `add_executable(app …)`, and wires the GTK include dirs /
compile options / libraries onto that target. The executable is named
`app`, matching the Makefile target. Build it with
`cmake -S . -B build && cmake --build build`.
In the designer, **File → Generate C++/GTK…** now offers **Build with
CMake** right after generating (CMake is the default build path for a
generated project); **File → Build with CMake…** rebuilds the last
generated project (or asks for a folder), and **File → Open CMake
Project…** points the designer at any directory holding a
`CMakeLists.txt` — generated or hand-written — and builds it (configure +
build), reporting success/failure plus the executable path in a log
dialog. A missing or broken `CMakeLists.txt`, or a compile error, is
reported as a failure, never as success.
The logic lives in new files `src/cmake_build.h/.cpp`
(`cmake_lists_text`, `cmake_build_project`, `cmake_project_exists`,
`CMakeSettings`); the generator only gains the emit call, `main.cpp`
only the menu callbacks, and `ui/designer.ui` the three File-menu items.
Settings (cmake command, generator — empty = CMake default, build type,
build directory) persist in `~/.config/fdesigner/cmake.conf` (key=value,
same pattern as `debugger.conf`; `FDESIGNER_CMAKE_CONF` overrides the
path for tests) and are edited via **File → CMake Settings…**.
Tests: `tests/cmake_tests.sh` builds a headless probe
(`tests/cmake_probe.cpp`) that checks the generated `CMakeLists.txt`
contents, configures + builds a generated GTK project and runs it under
Xvfb, builds + runs a hand-written console CMake project and a
hand-written GTK one, verifies settings save/load and custom build
directories, checks failure reporting (missing/broken `CMakeLists.txt`,
compile error), and builds a project in a directory with a space in its
name. The designer self-test gained a CMake phase (settings round-trip,
`CMakeLists.txt` markers, real console build, broken-file failure, and a
cmake build of the generated GTK project).
## Lazarus-style widgets (2026-11-25)
Twenty Lazarus-palette widgets were added alongside the original Tk set.
+2 -2
View File
@@ -3,8 +3,8 @@ CXXFLAGS ?= -O2 -Wall -Wextra -std=c++17
GTKFLAGS := $(shell pkg-config --cflags gtk+-3.0)
GTKLIBS := $(shell pkg-config --libs gtk+-3.0)
fdesigner: src/main.cpp src/generator.cpp src/project_io.cpp src/debugger.cpp src/debugger_ui.cpp src/model.h src/props.h src/generator.h src/project_io.h src/debugger.h src/debugger_ui.h src/indicators.h src/indicators_embedded.h src/drawings.h src/drawings_embedded.h ui/designer.ui
$(CXX) $(CXXFLAGS) -o $@ src/main.cpp src/generator.cpp src/project_io.cpp src/debugger.cpp src/debugger_ui.cpp $(GTKFLAGS) $(GTKLIBS)
fdesigner: src/main.cpp src/generator.cpp src/cmake_build.cpp src/project_io.cpp src/debugger.cpp src/debugger_ui.cpp src/model.h src/props.h src/generator.h src/cmake_build.h src/project_io.h src/debugger.h src/debugger_ui.h src/indicators.h src/indicators_embedded.h src/drawings.h src/drawings_embedded.h ui/designer.ui
$(CXX) $(CXXFLAGS) -o $@ src/main.cpp src/generator.cpp src/cmake_build.cpp src/project_io.cpp src/debugger.cpp src/debugger_ui.cpp $(GTKFLAGS) $(GTKLIBS)
src/indicators_embedded.h: src/indicators.h tools/embed_indicators.sh
sh tools/embed_indicators.sh src/indicators.h $@
+176
View File
@@ -0,0 +1,176 @@
// FDesigner 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("FDESIGNER_CMAKE_CONF");
if (e && *e) return e;
return home_dir() + "/.config/fdesigner/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 << "# FDesigner 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 FDesigner (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;
}
+52
View File
@@ -0,0 +1,52 @@
// FDesigner CMake handling — no GTK dependency.
// Emits CMakeLists.txt text for generated projects, builds any directory
// containing one (configure + build), and persists the CMake settings
// (generator / build type / build directory) in
// ~/.config/fdesigner/cmake.conf, following the debugger.conf pattern
// (key=value; FDESIGNER_CMAKE_CONF overrides the path, used by tests).
#pragma once
#include <string>
#include <vector>
struct CMakeSettings {
std::string cmake_path = "cmake"; // cmake binary (PATH lookup or path)
std::string generator; // "" = CMake default generator
std::string build_type = "Release";
std::string build_dir = "build"; // relative to the project directory
static std::string default_path(); // ~/.config/fdesigner/cmake.conf
bool save(const std::string& path = "") const;
bool load(const std::string& path = "");
};
// CMakeLists.txt text for a generated project: project <name>, GTK 3 via
// PkgConfig, C++17, and add_executable(app <sources>). `sources` are the
// generated .cpp file names (e.g. main.cpp, MyWindow.cpp, Button_001.cpp).
std::string cmake_lists_text(const std::string& project_name,
const std::vector<std::string>& sources);
// True when <dir>/CMakeLists.txt exists.
bool cmake_project_exists(const std::string& dir);
// Shell-quote one argument for /bin/sh.
std::string cmake_shell_quote(const std::string& s);
struct CMakeBuildResult {
bool ok = false; // configure AND build both succeeded
bool configured = false; // configure step succeeded
std::string output; // combined stdout+stderr of both steps
std::string build_dir; // absolute build directory used
std::string exe_path; // <build_dir>/app when it exists after build
};
// Configure + build the CMake project in `dir` (which must contain a
// CMakeLists.txt). The executable is <dir>/<build_dir>/app, matching the
// generated Makefile's `app` target. Blocking; captures all output.
CMakeBuildResult cmake_build_project(const std::string& dir,
const CMakeSettings& settings);
// Absolute path of the executable a successful cmake_build_project leaves
// behind for `dir` under `settings`.
std::string cmake_exe_path(const std::string& dir,
const CMakeSettings& settings);
+16
View File
@@ -12,6 +12,7 @@
#include <set>
#include <sstream>
#include "cmake_build.h"
#include "drawings_embedded.h"
#include "indicators_embedded.h"
#include "props.h"
@@ -2071,6 +2072,18 @@ static std::string emit_main(const std::string& cls) {
return o.str();
}
// ---- CMake (cmake_build.h) ----
// CMakeLists.txt for the generated project: the same sources the Makefile
// builds, as an explicit list, with the executable named `app` to match.
static std::string emit_cmake_lists(
const std::string& cls, const std::vector<Widget>& widgets) {
std::vector<std::string> sources;
sources.push_back("main.cpp");
sources.push_back(cls + ".cpp");
for (const Widget& w : widgets) sources.push_back(w.name + ".cpp");
return cmake_lists_text(cls, sources);
}
static std::string emit_makefile(const std::string& cls) {
std::ostringstream o;
o << "# Generated by FDesigner (C++/GTK). One class per widget:\n"
@@ -2142,6 +2155,9 @@ GenResult generate_project(const Project& project,
return r;
if (!write_file(dir / "main.cpp", emit_main(cls), r)) return r;
if (!write_file(dir / "Makefile", emit_makefile(cls), r)) return r;
if (!write_file(dir / "CMakeLists.txt", emit_cmake_lists(cls, widgets),
r))
return r;
r.ok = true;
return r;
}
+266 -8
View File
@@ -33,6 +33,7 @@
#include "props.h"
#include "drawings.h"
#include "generator.h"
#include "cmake_build.h"
#include "indicators.h"
#include "project_io.h"
#include "debugger.h"
@@ -190,6 +191,8 @@ static GtkCssProvider* g_canvas_css = nullptr;
static std::vector<Item*> g_items;
static Item* g_selected = nullptr;
static std::string g_current_file; // .fdj path, "" = untitled
// ---- CMake (cmake_build.h) ----
static std::string g_last_generated_dir; // folder of the last Generate
static std::map<std::string, int> g_counters; // per-type name counters
static int g_cascade = 0;
@@ -2372,6 +2375,10 @@ static Project snapshot_project() {
return p;
}
// ---- CMake (cmake_build.h) ----
// Forward declaration: defined just below on_generate.
static bool cmake_build_dir(const std::string& dir);
static void on_generate(GtkMenuItem*, gpointer) {
GtkWidget* dlg = gtk_file_chooser_dialog_new(
"Generate C++/GTK — choose a folder", GTK_WINDOW(g_window),
@@ -2385,22 +2392,181 @@ static void on_generate(GtkMenuItem*, gpointer) {
gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlg));
gtk_widget_destroy(dlg);
if (!folder) return;
Project p = snapshot_project();
GenResult r = generate_project(p, p.widgets, folder);
std::string folder_s = folder;
g_free(folder);
Project p = snapshot_project();
GenResult r = generate_project(p, p.widgets, folder_s);
if (!r.ok) {
status("Generate failed — " + r.error);
return;
}
// ---- CMake (cmake_build.h) ----
g_last_generated_dir = folder_s;
std::string list;
for (const std::string& f : r.files) list += f + "\n";
status("Generated " + std::to_string(r.files.size()) +
" files — build with make in that folder");
GtkWidget* msg = gtk_message_dialog_new(
GTK_WINDOW(g_window), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO,
GTK_BUTTONS_OK, "Generated C++/GTK project:\n\n%s", list.c_str());
gtk_dialog_run(GTK_DIALOG(msg));
" files — build with make, or CMake, in that folder");
GtkWidget* msg = gtk_dialog_new_with_buttons(
"Generated C++/GTK project", GTK_WINDOW(g_window),
GTK_DIALOG_MODAL, "_Build with CMake", GTK_RESPONSE_ACCEPT, "_Close",
GTK_RESPONSE_CLOSE, nullptr);
GtkWidget* label = gtk_label_new(
("Generated C++/GTK project:\n\n" + list).c_str());
gtk_label_set_selectable(GTK_LABEL(label), TRUE);
gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(
GTK_DIALOG(msg))),
label);
gtk_widget_show_all(msg);
// CMake is the default build path for a generated project: one click
// here configures + builds it (File -> Build with CMake… does the
// same later, for the last generated folder).
bool build_now = gtk_dialog_run(GTK_DIALOG(msg)) == GTK_RESPONSE_ACCEPT;
gtk_widget_destroy(msg);
if (build_now) cmake_build_dir(folder_s);
}
// ------------------------------------------------------- CMake
// (cmake_build.h) Builds a project directory with CMake instead of the
// plain g++ / make command. Settings (generator, build type, build
// directory) live in ~/.config/fdesigner/cmake.conf.
static void show_build_result(const std::string& title,
const CMakeBuildResult& r) {
status(r.ok ? ("CMake build OK — " + r.exe_path)
: "CMake build FAILED — see log");
GtkWidget* dlg = gtk_dialog_new_with_buttons(
title.c_str(), GTK_WINDOW(g_window), GTK_DIALOG_MODAL, "_Close",
GTK_RESPONSE_CLOSE, nullptr);
gtk_window_set_default_size(GTK_WINDOW(dlg), 700, 420);
std::string head =
r.ok ? std::string("Build succeeded.\nExecutable: ") + r.exe_path +
"\n\n"
: std::string("Build FAILED.\n\n");
// Keep the dialog readable: show the tail of the log.
std::string log = r.output;
if (log.size() > 6000) log = "[…]\n" + log.substr(log.size() - 6000);
GtkWidget* view = gtk_text_view_new();
gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
gtk_text_view_set_monospace(GTK_TEXT_VIEW(view), TRUE);
GtkTextBuffer* buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
gtk_text_buffer_set_text(buf, (head + log).c_str(), -1);
GtkWidget* scroll = gtk_scrolled_window_new(nullptr, nullptr);
gtk_container_add(GTK_CONTAINER(scroll), view);
gtk_container_add(
GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dlg))), scroll);
gtk_widget_show_all(dlg);
gtk_dialog_run(GTK_DIALOG(dlg));
gtk_widget_destroy(dlg);
}
// Configure + build `dir` with the saved CMake settings and report the
// result. Returns false when `dir` holds no CMakeLists.txt.
static bool cmake_build_dir(const std::string& dir) {
if (!cmake_project_exists(dir)) {
status("No CMakeLists.txt in " + dir);
GtkWidget* msg = gtk_message_dialog_new(
GTK_WINDOW(g_window), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK, "No CMakeLists.txt in:\n%s", dir.c_str());
gtk_dialog_run(GTK_DIALOG(msg));
gtk_widget_destroy(msg);
return false;
}
CMakeSettings s;
s.load();
status("Building with CMake in " + dir + " …");
CMakeBuildResult r = cmake_build_project(dir, s);
show_build_result("CMake build — " + dir, r);
return r.ok;
}
// File -> Build with CMake… : rebuilds the last generated project, or
// asks for a project folder when there is none yet.
static void on_cmake_build(GtkMenuItem*, gpointer) {
std::string dir = g_last_generated_dir;
if (dir.empty() || !cmake_project_exists(dir)) {
GtkWidget* dlg = gtk_file_chooser_dialog_new(
"Build with CMake — choose a project folder",
GTK_WINDOW(g_window), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
"_Cancel", GTK_RESPONSE_CANCEL, "_Build", GTK_RESPONSE_ACCEPT,
nullptr);
if (gtk_dialog_run(GTK_DIALOG(dlg)) != GTK_RESPONSE_ACCEPT) {
gtk_widget_destroy(dlg);
return;
}
gchar* folder = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlg));
gtk_widget_destroy(dlg);
if (!folder) return;
dir = folder;
g_free(folder);
}
cmake_build_dir(dir);
}
// File -> Open CMake Project… : point the designer at any directory
// holding a CMakeLists.txt (generated or hand-written) and build it.
static void on_cmake_open(GtkMenuItem*, gpointer) {
GtkWidget* dlg = gtk_file_chooser_dialog_new(
"Open CMake Project — choose a folder with a CMakeLists.txt",
GTK_WINDOW(g_window), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
"_Cancel", GTK_RESPONSE_CANCEL, "_Open & Build", GTK_RESPONSE_ACCEPT,
nullptr);
if (gtk_dialog_run(GTK_DIALOG(dlg)) != GTK_RESPONSE_ACCEPT) {
gtk_widget_destroy(dlg);
return;
}
gchar* folder = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlg));
gtk_widget_destroy(dlg);
if (!folder) return;
std::string dir = folder;
g_free(folder);
g_last_generated_dir = dir; // so Build with CMake… rebuilds it
cmake_build_dir(dir);
}
// File -> CMake Settings… : generator / build type / build directory /
// cmake command, persisted to ~/.config/fdesigner/cmake.conf.
static void on_cmake_settings(GtkMenuItem*, gpointer) {
CMakeSettings s;
s.load();
GtkWidget* dlg = gtk_dialog_new_with_buttons(
"CMake Settings", GTK_WINDOW(g_window), GTK_DIALOG_MODAL, "_Cancel",
GTK_RESPONSE_CANCEL, "_Save", GTK_RESPONSE_ACCEPT, nullptr);
GtkWidget* grid = gtk_grid_new();
gtk_grid_set_column_spacing(GTK_GRID(grid), 8);
gtk_grid_set_row_spacing(GTK_GRID(grid), 6);
gtk_container_set_border_width(GTK_CONTAINER(grid), 12);
auto add_row = [&](int row, const char* label, GtkWidget* entry) {
gtk_grid_attach(GTK_GRID(grid), gtk_label_new(label), 0, row, 1, 1);
gtk_grid_attach(GTK_GRID(grid), entry, 1, row, 1, 1);
};
GtkWidget* e_cmake = gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(e_cmake), s.cmake_path.c_str());
add_row(0, "CMake command:", e_cmake);
GtkWidget* e_gen = gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(e_gen), s.generator.c_str());
gtk_entry_set_placeholder_text(GTK_ENTRY(e_gen),
"empty = CMake default");
add_row(1, "Generator:", e_gen);
GtkWidget* e_type = gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(e_type), s.build_type.c_str());
add_row(2, "Build type:", e_type);
GtkWidget* e_dir = gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(e_dir), s.build_dir.c_str());
add_row(3, "Build directory:", e_dir);
gtk_container_add(
GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dlg))), grid);
gtk_widget_show_all(dlg);
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_ACCEPT) {
s.cmake_path = gtk_entry_get_text(GTK_ENTRY(e_cmake));
s.generator = gtk_entry_get_text(GTK_ENTRY(e_gen));
s.build_type = gtk_entry_get_text(GTK_ENTRY(e_type));
s.build_dir = gtk_entry_get_text(GTK_ENTRY(e_dir));
if (s.save())
status("CMake settings saved to " + CMakeSettings::default_path());
else
status("CMake settings save FAILED");
}
gtk_widget_destroy(dlg);
}
// ------------------------------------------------------- file operations
@@ -2573,9 +2739,30 @@ static gboolean selftest_step(gpointer) {
Project snap = snapshot_project();
GenResult gr = generate_project(snap, snap.widgets,
"/tmp/fdesigner_gen_test");
check(gr.ok && gr.files.size() == 4 + 2 * snap.widgets.size());
check(gr.ok && gr.files.size() == 5 + 2 * snap.widgets.size());
check(!g_file_test("/tmp/fdesigner_gen_test/mywindow.ui",
G_FILE_TEST_EXISTS)); // all code, no .ui
// ---- CMake (cmake_build.h) ----
// The generator also emits a CMakeLists.txt for the project.
gchar* cmake_text = nullptr;
check(g_file_get_contents("/tmp/fdesigner_gen_test/CMakeLists.txt",
&cmake_text, nullptr, nullptr) &&
cmake_text != nullptr);
if (cmake_text) {
std::string cm = cmake_text;
check(cm.find("cmake_minimum_required(VERSION 3.10)") !=
std::string::npos);
check(cm.find("project(MyWindow CXX)") != std::string::npos);
check(cm.find("pkg_check_modules(GTK3 REQUIRED gtk+-3.0)") !=
std::string::npos);
check(cm.find("add_executable(app") != std::string::npos);
check(cm.find("main.cpp") != std::string::npos &&
cm.find("MyWindow.cpp") != std::string::npos &&
cm.find("Button_001.cpp") != std::string::npos);
check(cm.find("target_link_libraries(app PRIVATE "
"${GTK3_LIBRARIES})") != std::string::npos);
g_free(cmake_text);
}
gchar* code_text = nullptr;
check(g_file_get_contents("/tmp/fdesigner_gen_test/MyWindow.cpp",
&code_text, nullptr, nullptr) &&
@@ -3025,6 +3212,62 @@ static gboolean selftest_step(gpointer) {
sess2.breakpoints()[0].location == "main");
}
// ---- CMake (cmake_build.h) ---- Phase 8: settings round-trip,
// CMakeLists.txt text, and a real configure+build of a small CMake
// project (console, plus a generated GTK project built by cmake).
{
CMakeSettings cs;
cs.generator = "Unix Makefiles";
cs.build_type = "Debug";
cs.build_dir = "out";
cs.cmake_path = "cmake";
check(cs.save("/tmp/fdesigner_cmake.conf"));
CMakeSettings cs2;
check(cs2.load("/tmp/fdesigner_cmake.conf"));
check(cs2.generator == "Unix Makefiles" &&
cs2.build_type == "Debug" && cs2.build_dir == "out" &&
cs2.cmake_path == "cmake");
check(cmake_lists_text("Demo", {"main.cpp", "Foo.cpp"}).find(
"add_executable(app") != std::string::npos);
check(!cmake_project_exists("/tmp/fdesigner_cmake_nodir_xyz"));
// Hand-written console CMake project: configure + build + run.
std::system("rm -rf /tmp/fdesigner_cmake_console && mkdir -p "
"/tmp/fdesigner_cmake_console");
{
std::ofstream f("/tmp/fdesigner_cmake_console/CMakeLists.txt");
f << "cmake_minimum_required(VERSION 3.10)\n"
<< "project(console_demo CXX)\n"
<< "add_executable(app main.cpp)\n";
}
{
std::ofstream f("/tmp/fdesigner_cmake_console/main.cpp");
f << "#include <cstdio>\nint main() { std::printf(\"hi\\n\"); "
"return 0; }\n";
}
CMakeSettings def;
check(cmake_project_exists("/tmp/fdesigner_cmake_console"));
CMakeBuildResult cr =
cmake_build_project("/tmp/fdesigner_cmake_console", def);
check(cr.ok && cr.configured);
check(std::system("/tmp/fdesigner_cmake_console/build/app "
"> /tmp/fdesigner_cmake_console/run.log 2>&1") ==
0);
// A broken CMakeLists.txt must fail cleanly, not claim success.
std::system("rm -rf /tmp/fdesigner_cmake_broken && mkdir -p "
"/tmp/fdesigner_cmake_broken");
{
std::ofstream f("/tmp/fdesigner_cmake_broken/CMakeLists.txt");
f << "this is not valid cmake (\n";
}
CMakeBuildResult bad =
cmake_build_project("/tmp/fdesigner_cmake_broken", def);
check(!bad.ok);
// The generated GTK project from Phase 3 builds with cmake too.
CMakeBuildResult gr_cmake =
cmake_build_project("/tmp/fdesigner_gen_test", def);
check(gr_cmake.ok && gr_cmake.configured);
}
std::printf("SELFTEST %s items=%zu\n", ok ? "PASS" : "FAIL",
g_items.size());
std::fflush(stdout);
@@ -3111,6 +3354,21 @@ int main(int argc, char** argv) {
if (mi_save)
g_signal_connect(mi_save, "activate", G_CALLBACK(on_save), nullptr);
// ---- CMake (cmake_build.h) ----
// File -> Build with CMake… / Open CMake Project… / CMake Settings…
GObject* mi_cb = gtk_builder_get_object(builder, "mi_cmake_build");
if (mi_cb)
g_signal_connect(mi_cb, "activate", G_CALLBACK(on_cmake_build),
nullptr);
GObject* mi_co = gtk_builder_get_object(builder, "mi_cmake_open");
if (mi_co)
g_signal_connect(mi_co, "activate", G_CALLBACK(on_cmake_open),
nullptr);
GObject* mi_cs = gtk_builder_get_object(builder, "mi_cmake_settings");
if (mi_cs)
g_signal_connect(mi_cs, "activate", G_CALLBACK(on_cmake_settings),
nullptr);
// Debug menu -> stepping debugger window / settings.
GObject* mi_dbg = gtk_builder_get_object(builder, "mi_debug_open");
if (mi_dbg)
+220
View File
@@ -0,0 +1,220 @@
// CMake test probe — drives src/cmake_build.h + src/generator.h headlessly
// (no GUI): generates a project, checks the CMakeLists.txt, configures +
// builds it with cmake, runs the built GTK app under Xvfb, builds a
// hand-written console CMake project and a hand-written GTK one, and
// checks settings persistence and failure reporting.
// Usage: cmake_probe <workdir> ; exit 0 = all pass. Prints PASS/FAIL lines.
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <string>
#include <sys/wait.h>
#include "cmake_build.h"
#include "generator.h"
#include "model.h"
namespace fs = std::filesystem;
static int g_pass = 0, g_fail = 0;
static void check(bool ok, const char* name) {
std::printf("%s %s\n", ok ? "PASS" : "FAIL", name);
if (ok) ++g_pass; else ++g_fail;
}
static std::string read_file(const std::string& p) {
std::ifstream f(p);
if (!f) return "";
return std::string(std::istreambuf_iterator<char>(f),
std::istreambuf_iterator<char>());
}
static void write_file(const std::string& p, const std::string& c) {
std::ofstream f(p);
f << c;
}
static int run(const std::string& cmd) {
// std::system returns the wait status, not the exit code.
int rc = std::system(cmd.c_str());
if (rc == -1) return -1;
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
return -1;
}
int main(int argc, char** argv) {
std::string base = argc > 1 ? argv[1] : "/tmp/fdesigner_cmake_tests";
std::error_code ec;
fs::remove_all(base, ec);
fs::create_directories(base, ec);
// 1. Generate a project (Button + Label + Entry) and inspect its
// CMakeLists.txt.
Project p;
p.class_name = "MyWindow";
Widget b;
b.name = "Button_001"; b.type = "Button";
b.props["label"] = "Click me";
Widget l;
l.name = "Label_001"; l.type = "Label";
l.props["label"] = "Hello";
Widget e;
e.name = "Entry_001"; e.type = "Entry";
p.widgets = {b, l, e};
std::string gen_dir = base + "/generated";
GenResult r = generate_project(p, p.widgets, gen_dir);
check(r.ok, "generate-project");
check(r.files.size() == 5 + 2 * p.widgets.size(),
"generate-file-count-includes-cmakelists");
std::string cm = read_file(gen_dir + "/CMakeLists.txt");
check(!cm.empty(), "cmakelists-exists");
check(cm.find("cmake_minimum_required(VERSION 3.10)") !=
std::string::npos,
"cmakelists-minimum-required");
check(cm.find("project(MyWindow CXX)") != std::string::npos,
"cmakelists-project");
check(cm.find("find_package(PkgConfig REQUIRED)") != std::string::npos,
"cmakelists-find-pkgconfig");
check(cm.find("pkg_check_modules(GTK3 REQUIRED gtk+-3.0)") !=
std::string::npos,
"cmakelists-pkg-check-gtk3");
check(cm.find("add_executable(app") != std::string::npos,
"cmakelists-add-executable");
check(cm.find("main.cpp") != std::string::npos &&
cm.find("MyWindow.cpp") != std::string::npos &&
cm.find("Button_001.cpp") != std::string::npos &&
cm.find("Label_001.cpp") != std::string::npos &&
cm.find("Entry_001.cpp") != std::string::npos,
"cmakelists-lists-all-sources");
check(cm.find("target_include_directories(app PRIVATE "
"${GTK3_INCLUDE_DIRS})") != std::string::npos,
"cmakelists-include-dirs");
check(cm.find("target_link_libraries(app PRIVATE ${GTK3_LIBRARIES})") !=
std::string::npos,
"cmakelists-link-libs");
check(cm.find("CMAKE_CXX_STANDARD 17") != std::string::npos,
"cmakelists-cxx17");
check(cmake_project_exists(gen_dir), "cmake-project-detected");
check(!cmake_project_exists(base), "cmake-project-absent-elsewhere");
// 2. Configure + build the generated project with cmake, then run
// the built GTK app briefly under Xvfb (timeout kills it: exit 124).
CMakeSettings def;
CMakeBuildResult br = cmake_build_project(gen_dir, def);
check(br.ok && br.configured, "cmake-configure-build-generated");
check(br.exe_path == gen_dir + "/build/app", "cmake-exe-path");
check(fs::is_regular_file(br.exe_path), "cmake-exe-exists");
check(run("timeout 3 xvfb-run -a '" + br.exe_path +
"' >/dev/null 2>&1") == 124,
"cmake-built-gtk-app-runs");
// Rebuild is a no-op success (idempotent configure+build).
CMakeBuildResult br2 = cmake_build_project(gen_dir, def);
check(br2.ok, "cmake-rebuild-idempotent");
// 3. Hand-written console CMake project (existing project, not
// generated): configure + build + run, output must be exact.
std::string console_dir = base + "/console";
fs::create_directories(console_dir, ec);
write_file(console_dir + "/CMakeLists.txt",
"cmake_minimum_required(VERSION 3.10)\n"
"project(console_demo CXX)\n"
"add_executable(app main.cpp)\n");
write_file(console_dir + "/main.cpp",
"#include <cstdio>\n"
"int main() { std::printf(\"cmake-console-ok\\n\"); "
"return 0; }\n");
CMakeBuildResult cr = cmake_build_project(console_dir, def);
check(cr.ok, "cmake-build-handwritten-console");
check(run("'" + console_dir + "/build/app' > '" + console_dir +
"/run.log' 2>&1") == 0 &&
read_file(console_dir + "/run.log") == "cmake-console-ok\n",
"cmake-console-app-output");
// 4. Hand-written GTK CMake project (uses PkgConfig like generated).
std::string gtk_dir = base + "/gtk";
fs::create_directories(gtk_dir, ec);
write_file(gtk_dir + "/CMakeLists.txt",
"cmake_minimum_required(VERSION 3.10)\n"
"project(gtk_demo CXX)\n"
"find_package(PkgConfig REQUIRED)\n"
"pkg_check_modules(GTK3 REQUIRED gtk+-3.0)\n"
"add_executable(app main.cpp)\n"
"target_include_directories(app PRIVATE "
"${GTK3_INCLUDE_DIRS})\n"
"target_link_libraries(app PRIVATE ${GTK3_LIBRARIES})\n");
write_file(gtk_dir + "/main.cpp",
"#include <gtk/gtk.h>\n"
"int main(int argc, char** argv) {\n"
" gtk_init(&argc, &argv);\n"
" GtkWidget* w = gtk_window_new(GTK_WINDOW_TOPLEVEL);\n"
" gtk_widget_show_all(w);\n"
" gtk_main();\n"
" return 0;\n"
"}\n");
CMakeBuildResult gr = cmake_build_project(gtk_dir, def);
check(gr.ok, "cmake-build-handwritten-gtk");
check(run("timeout 3 xvfb-run -a '" + gtk_dir + "/build/app' "
"> /dev/null 2>&1") == 124,
"cmake-handwritten-gtk-app-runs");
// 5. Settings round-trip (custom path, so ~/.config is untouched).
CMakeSettings s;
s.cmake_path = "cmake";
s.generator = "Unix Makefiles";
s.build_type = "Debug";
s.build_dir = "out";
check(s.save(base + "/cmake.conf"), "cmake-settings-save");
CMakeSettings s2;
check(s2.load(base + "/cmake.conf") &&
s2.generator == "Unix Makefiles" &&
s2.build_type == "Debug" && s2.build_dir == "out" &&
s2.cmake_path == "cmake",
"cmake-settings-load");
// Custom build dir + build type are honoured by a real build.
CMakeBuildResult dbg = cmake_build_project(console_dir, s);
check(dbg.ok && dbg.build_dir == console_dir + "/out" &&
fs::is_regular_file(console_dir + "/out/app"),
"cmake-settings-custom-build-dir");
// Defaults load: unknown keys ignored, empty file loads as defaults.
write_file(base + "/cmake2.conf", "# comment\nunknown_key=1\n");
CMakeSettings s3;
check(s3.load(base + "/cmake2.conf") &&
s3.build_type == "Release" && s3.build_dir == "build",
"cmake-settings-unknown-keys-ignored");
// 6. Failure reporting: missing CMakeLists.txt, broken CMakeLists.txt,
// and a project whose sources fail to compile must all report !ok.
CMakeBuildResult nofile = cmake_build_project(base, def);
check(!nofile.ok && !nofile.configured,
"cmake-missing-cmakelists-fails");
std::string broken_dir = base + "/broken";
fs::create_directories(broken_dir, ec);
write_file(broken_dir + "/CMakeLists.txt", "this is not valid cmake (\n");
CMakeBuildResult bad = cmake_build_project(broken_dir, def);
check(!bad.ok && !bad.configured, "cmake-broken-cmakelists-fails");
std::string badsrc_dir = base + "/badsrc";
fs::create_directories(badsrc_dir, ec);
write_file(badsrc_dir + "/CMakeLists.txt",
"cmake_minimum_required(VERSION 3.10)\n"
"project(bad CXX)\n"
"add_executable(app main.cpp)\n");
write_file(badsrc_dir + "/main.cpp", "int main( { syntax error\n");
CMakeBuildResult badsrc = cmake_build_project(badsrc_dir, def);
check(!badsrc.ok && badsrc.configured,
"cmake-compile-error-fails-after-configure");
// 7. Shell quoting: a project dir with a space in its name builds.
std::string spaced = base + "/dir with space";
fs::create_directories(spaced, ec);
write_file(spaced + "/CMakeLists.txt",
"cmake_minimum_required(VERSION 3.10)\n"
"project(spaced CXX)\n"
"add_executable(app main.cpp)\n");
write_file(spaced + "/main.cpp", "int main() { return 0; }\n");
CMakeBuildResult sp = cmake_build_project(spaced, def);
check(sp.ok, "cmake-dir-with-space-builds");
std::printf("== cmake probe: %d passed, %d failed ==\n", g_pass, g_fail);
return g_fail == 0 ? 0 : 1;
}
+42
View File
@@ -0,0 +1,42 @@
#!/usr/bin/env bash
# CMake tests for FDesigner: generated CMakeLists.txt contents, real
# configure+build of generated and hand-written CMake projects (GTK under
# Xvfb, console run), settings persistence, and failure reporting.
# Usage: tests/cmake_tests.sh (run from the repo root; builds fdesigner
# first so the self-test's CMake phase is exercised too.)
set -u
cd "$(dirname "$0")/.."
pass=0; fail=0
note() { if [ "$1" = 0 ]; then pass=$((pass+1)); else fail=$((fail+1)); fi; }
echo "== building fdesigner =="
make > /tmp/fdesigner_cmake_make.log 2>&1
note $?
if [ $fail -ne 0 ]; then echo "FAIL make — see /tmp/fdesigner_cmake_make.log"; exit 1; fi
echo "PASS make"
echo "== building cmake probe =="
g++ -O2 -Wall -Wextra -std=c++17 -o /tmp/cmake_probe tests/cmake_probe.cpp \
src/generator.cpp src/cmake_build.cpp -I src \
$(pkg-config --cflags --libs gtk+-3.0) > /tmp/cmake_probe_build.log 2>&1
note $?
if [ $fail -ne 0 ]; then echo "FAIL probe build — see /tmp/cmake_probe_build.log"; exit 1; fi
echo "PASS probe build"
echo "== probe (generate + cmake configure/build + run + settings + failures) =="
/tmp/cmake_probe /tmp/fdesigner_cmake_tests
rc=$?
note $rc
echo "== designer self-test (includes CMake Phase 8) =="
FDESIGNER_SELFTEST=1 timeout 120 xvfb-run -a ./fdesigner > /tmp/fdesigner_cmake_selftest.log 2>&1
rc=$?
if [ $rc -eq 0 ] && grep -q "SELFTEST PASS" /tmp/fdesigner_cmake_selftest.log; then
echo "PASS selftest"; pass=$((pass+1))
else
echo "FAIL selftest (rc=$rc) — see /tmp/fdesigner_cmake_selftest.log"; fail=$((fail+1))
fi
echo "== cmake tests: $pass passed, $fail failed =="
[ $fail -eq 0 ]
+1 -1
View File
@@ -7,7 +7,7 @@ set -u
cd "$(dirname "$0")/.."
echo "== building harness =="
g++ -O2 -Wall -std=c++17 -o tests/gen_matrix tests/gen_matrix.cpp src/generator.cpp || exit 1
g++ -O2 -Wall -std=c++17 -o tests/gen_matrix tests/gen_matrix.cpp src/generator.cpp src/cmake_build.cpp || exit 1
rm -rf tests/out
echo "== generating =="
+1 -1
View File
@@ -27,7 +27,7 @@ grep -E "GENERATED7" /tmp/widget_selftest.log || true
echo "== widget generator test (per-widget markers) =="
if g++ -O2 -Wall -Wextra -std=c++17 -o /tmp/widget_tests \
tests/widget_tests.cpp src/generator.cpp src/project_io.cpp; then
tests/widget_tests.cpp src/generator.cpp src/cmake_build.cpp src/project_io.cpp; then
rm -rf /tmp/fdesigner_widgets_gen
if /tmp/widget_tests /tmp/fdesigner_widgets_gen; then
ok widget-markers
+18
View File
@@ -49,6 +49,24 @@
<property name="visible">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="mi_cmake_build">
<property name="label">Build with CMake…</property>
<property name="visible">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="mi_cmake_open">
<property name="label">Open CMake Project…</property>
<property name="visible">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="mi_cmake_settings">
<property name="label">CMake Settings…</property>
<property name="visible">True</property>
</object>
</child>
<child>
<object class="GtkSeparatorMenuItem" id="mi_sep2">
<property name="visible">True</property>