Skip to content

Commit 923ea2b

Browse files
committed
feat(cmake): make generated app cmake registry-safe and extensible
1 parent 71c6e85 commit 923ea2b

1 file changed

Lines changed: 95 additions & 25 deletions

File tree

src/commands/NewCommand.cpp

Lines changed: 95 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,14 +1250,16 @@ int main()
12501250
static std::string make_cmakelists_app(const std::string &projectName, const FeaturesSelection &f)
12511251
{
12521252
std::string s;
1253-
s.reserve(12000);
1253+
s.reserve(16000);
12541254

12551255
s += "cmake_minimum_required(VERSION 3.20)\n";
12561256
s += "project(" + projectName + " LANGUAGES CXX)\n\n";
1257+
12571258
s += "set(CMAKE_CXX_STANDARD 20)\n";
1258-
s += "set(CMAKE_CXX_STANDARD_REQUIRED ON)\n\n";
1259+
s += "set(CMAKE_CXX_STANDARD_REQUIRED ON)\n";
1260+
s += "set(CMAKE_CXX_EXTENSIONS OFF)\n\n";
12591261

1260-
// Options only for selected features (strict)
1262+
// Options only for selected features
12611263
if (f.orm)
12621264
s += "option(VIX_USE_ORM \"Enable Vix ORM (requires vix::orm in install)\" ON)\n";
12631265
if (f.sanitizers)
@@ -1269,31 +1271,40 @@ int main()
12691271
if (f.orm || f.sanitizers || f.static_rt || f.full_static)
12701272
s += "\n";
12711273

1274+
s += "# ------------------------------------------------------\n";
1275+
s += "# Core Vix runtime\n";
1276+
s += "# ------------------------------------------------------\n";
12721277
s += "find_package(vix QUIET CONFIG)\n";
12731278
s += "if (NOT vix_FOUND)\n";
12741279
s += " find_package(Vix CONFIG REQUIRED)\n";
12751280
s += "endif()\n\n";
12761281

1277-
s += "add_executable(" + projectName + " src/main.cpp)\n";
1278-
s += "target_link_libraries(" + projectName + " PRIVATE vix::vix)\n\n";
1279-
1280-
s += "if (MSVC)\n";
1281-
s += " target_compile_options(" + projectName + " PRIVATE /W4 /permissive-)\n";
1282-
s += "else()\n";
1283-
s += " target_compile_options(" + projectName + " PRIVATE -Wall -Wextra -Wpedantic)\n";
1282+
s += "# ------------------------------------------------------\n";
1283+
s += "# Local registry packages installed with: vix install\n";
1284+
s += "# ------------------------------------------------------\n";
1285+
s += "# If you add packages from the Vix registry, they are wired\n";
1286+
s += "# through .vix/vix_deps.cmake. This file creates the imported\n";
1287+
s += "# targets for local project dependencies.\n";
1288+
s += "#\n";
1289+
s += "# Example:\n";
1290+
s += "# vix add @cnerium/app\n";
1291+
s += "# vix install\n";
1292+
s += "#\n";
1293+
s += "# Then uncomment the links you need below.\n";
1294+
s += "if (EXISTS \"${CMAKE_CURRENT_SOURCE_DIR}/.vix/vix_deps.cmake\")\n";
1295+
s += " include(\"${CMAKE_CURRENT_SOURCE_DIR}/.vix/vix_deps.cmake\")\n";
12841296
s += "endif()\n\n";
12851297

1286-
if (f.orm)
1287-
{
1288-
s += "if (VIX_USE_ORM)\n";
1289-
s += " if (TARGET vix::orm)\n";
1290-
s += " target_link_libraries(" + projectName + " PRIVATE vix::orm)\n";
1291-
s += " target_compile_definitions(" + projectName + " PRIVATE VIX_USE_ORM=1)\n";
1292-
s += " else()\n";
1293-
s += " message(FATAL_ERROR \"VIX_USE_ORM=ON but vix::orm target is not available in this Vix install\")\n";
1294-
s += " endif()\n";
1295-
s += "endif()\n\n";
1296-
}
1298+
s += "# ------------------------------------------------------\n";
1299+
s += "# Helpers\n";
1300+
s += "# ------------------------------------------------------\n";
1301+
s += "function(vix_link_optional_targets tgt)\n";
1302+
s += " foreach(dep IN LISTS ARGN)\n";
1303+
s += " if (TARGET ${dep})\n";
1304+
s += " target_link_libraries(${tgt} PRIVATE ${dep})\n";
1305+
s += " endif()\n";
1306+
s += " endforeach()\n";
1307+
s += "endfunction()\n\n";
12971308

12981309
if (f.static_rt || f.full_static)
12991310
{
@@ -1316,9 +1327,46 @@ int main()
13161327
s += " endif()\n";
13171328
}
13181329
s += "endfunction()\n\n";
1319-
s += "vix_apply_static_link_flags(" + projectName + ")\n\n";
13201330
}
13211331

1332+
s += "# ------------------------------------------------------\n";
1333+
s += "# Main executable\n";
1334+
s += "# ------------------------------------------------------\n";
1335+
s += "add_executable(" + projectName + " src/main.cpp)\n";
1336+
s += "target_link_libraries(" + projectName + " PRIVATE vix::vix)\n\n";
1337+
1338+
s += "# Add local registry libraries here.\n";
1339+
s += "# Keep them in this block so the build stays stable even if\n";
1340+
s += "# some packages are not installed yet.\n";
1341+
s += "#\n";
1342+
s += "# Example:\n";
1343+
s += "# vix_link_optional_targets(" + projectName + "\n";
1344+
s += "# cnerium::app\n";
1345+
s += "# cnerium::http\n";
1346+
s += "# cnerium::json\n";
1347+
s += "# )\n\n";
1348+
1349+
s += "if (MSVC)\n";
1350+
s += " target_compile_options(" + projectName + " PRIVATE /W4 /permissive-)\n";
1351+
s += "else()\n";
1352+
s += " target_compile_options(" + projectName + " PRIVATE -Wall -Wextra -Wpedantic)\n";
1353+
s += "endif()\n\n";
1354+
1355+
if (f.orm)
1356+
{
1357+
s += "if (VIX_USE_ORM)\n";
1358+
s += " if (TARGET vix::orm)\n";
1359+
s += " target_link_libraries(" + projectName + " PRIVATE vix::orm)\n";
1360+
s += " target_compile_definitions(" + projectName + " PRIVATE VIX_USE_ORM=1)\n";
1361+
s += " else()\n";
1362+
s += " message(FATAL_ERROR \"VIX_USE_ORM=ON but vix::orm target is not available in this Vix install\")\n";
1363+
s += " endif()\n";
1364+
s += "endif()\n\n";
1365+
}
1366+
1367+
if (f.static_rt || f.full_static)
1368+
s += "vix_apply_static_link_flags(" + projectName + ")\n\n";
1369+
13221370
if (f.sanitizers)
13231371
{
13241372
s += "if (VIX_ENABLE_SANITIZERS AND NOT MSVC)\n";
@@ -1328,14 +1376,37 @@ int main()
13281376
s += "endif()\n\n";
13291377
}
13301378

1379+
s += "# ------------------------------------------------------\n";
1380+
s += "# Tests\n";
1381+
s += "# ------------------------------------------------------\n";
13311382
s += "include(CTest)\n";
13321383
s += "enable_testing()\n\n";
1384+
13331385
s += "add_executable(" + projectName + "_basic_test tests/test_basic.cpp)\n";
1334-
s += "target_link_libraries(" + projectName + "_basic_test PRIVATE vix::vix)\n";
1386+
s += "target_link_libraries(" + projectName + "_basic_test PRIVATE vix::vix)\n\n";
1387+
1388+
s += "# Add the same local registry libraries to tests when needed.\n";
1389+
s += "# Example:\n";
1390+
s += "# vix_link_optional_targets(" + projectName + "_basic_test\n";
1391+
s += "# cnerium::app\n";
1392+
s += "# cnerium::http\n";
1393+
s += "# cnerium::json\n";
1394+
s += "# )\n\n";
1395+
1396+
s += "if (MSVC)\n";
1397+
s += " target_compile_options(" + projectName + "_basic_test PRIVATE /W4 /permissive-)\n";
1398+
s += "else()\n";
1399+
s += " target_compile_options(" + projectName + "_basic_test PRIVATE -Wall -Wextra -Wpedantic)\n";
1400+
s += "endif()\n\n";
1401+
13351402
if (f.static_rt || f.full_static)
1336-
s += "vix_apply_static_link_flags(" + projectName + "_basic_test)\n";
1403+
s += "vix_apply_static_link_flags(" + projectName + "_basic_test)\n\n";
1404+
13371405
s += "add_test(NAME " + projectName + ".basic COMMAND " + projectName + "_basic_test)\n\n";
13381406

1407+
s += "# ------------------------------------------------------\n";
1408+
s += "# Convenience target\n";
1409+
s += "# ------------------------------------------------------\n";
13391410
s += "add_custom_target(run\n";
13401411
s += " COMMAND $<TARGET_FILE:" + projectName + ">\n";
13411412
s += " DEPENDS " + projectName + "\n";
@@ -1344,7 +1415,6 @@ int main()
13441415

13451416
return s;
13461417
}
1347-
13481418
static std::string make_cmakelists_lib(const std::string &name)
13491419
{
13501420
std::string s;

0 commit comments

Comments
 (0)