Skip to content

Commit 97e360d

Browse files
committed
fix(build): separate user project and CMake source paths
2 parents fe2f3e8 + e511265 commit 97e360d

4 files changed

Lines changed: 111 additions & 27 deletions

File tree

include/vix/cli/process/Process.hpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,48 @@ namespace vix::cli::process
223223
struct Plan
224224
{
225225
/**
226-
* @brief Root project directory containing the main CMakeLists.txt.
226+
* @brief User project root directory.
227+
*
228+
* This is the real directory where the user runs Vix from.
229+
* Build directories, exported binaries, local build state, and project
230+
* input snapshots should be based on this path.
231+
*
232+
* For normal CMake projects, this is the same as cmakeSourceDir.
233+
* For vix.app projects, this is the directory containing vix.app.
234+
*/
235+
fs::path userProjectDir;
236+
237+
/**
238+
* @brief CMake source directory passed to `cmake -S`.
239+
*
240+
* For normal CMake projects, this is the same as userProjectDir.
241+
* For vix.app projects, this points to the generated internal CMake
242+
* directory under `.vix/generated/app`.
243+
*/
244+
fs::path cmakeSourceDir;
245+
246+
/**
247+
* @brief Root project directory.
248+
*
249+
* Kept for backward compatibility with existing build code.
250+
* New code should prefer userProjectDir or cmakeSourceDir depending on
251+
* the operation being performed.
227252
*/
228253
fs::path projectDir;
229254

255+
/**
256+
* @brief Default target name used when --build-target is not provided.
257+
*
258+
* For normal CMake projects, this is usually the project directory name.
259+
* For vix.app projects, this is the manifest `name`.
260+
*/
261+
std::string defaultTargetName;
262+
263+
/**
264+
* @brief True when the active CMake project was generated from vix.app.
265+
*/
266+
bool generatedFromVixApp{false};
267+
230268
/**
231269
* @brief Resolved embedded preset.
232270
*/

src/build/BuildContext.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ namespace vix::cli::build
6161
if (!opt.buildTarget.empty())
6262
return opt.buildTarget;
6363

64+
if (!plan.defaultTargetName.empty())
65+
return plan.defaultTargetName;
66+
6467
return plan.projectDir.filename().string();
6568
}
6669

@@ -71,9 +74,11 @@ namespace vix::cli::build
7174
if (!opt.buildTarget.empty())
7275
return opt.buildTarget;
7376

77+
if (!plan.defaultTargetName.empty())
78+
return plan.defaultTargetName;
79+
7480
return plan.projectDir.filename().string();
7581
}
76-
7782
fs::path default_project_executable_path(
7883
const process::Options &opt,
7984
const process::Plan &plan)

src/cmake/CMakeBuild.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,16 @@ namespace vix::cli::build
138138
std::vector<std::string> argv;
139139
argv.reserve(32);
140140

141+
const fs::path cmakeSourceDir =
142+
!plan.cmakeSourceDir.empty()
143+
? plan.cmakeSourceDir
144+
: plan.projectDir;
145+
141146
argv.push_back("cmake");
142147
argv.push_back(opt.cmakeVerbose ? "--log-level=VERBOSE" : "--log-level=WARNING");
143148

144149
argv.push_back("-S");
145-
argv.push_back(plan.projectDir.string());
150+
argv.push_back(cmakeSourceDir.string());
146151
argv.push_back("-B");
147152
argv.push_back(plan.buildDir.string());
148153
argv.push_back("-G");
@@ -175,7 +180,12 @@ namespace vix::cli::build
175180
std::string target = opt.buildTarget;
176181

177182
if (target.empty())
178-
target = plan.projectDir.filename().string();
183+
{
184+
if (!plan.defaultTargetName.empty())
185+
target = plan.defaultTargetName;
186+
else
187+
target = plan.projectDir.filename().string();
188+
}
179189

180190
if (!target.empty())
181191
{

src/commands/BuildCommand.cpp

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,10 @@ namespace vix::commands::BuildCommand
292292
const std::string &toolchainContent)
293293
{
294294
artifact_cache::Artifact a;
295-
a.package = sanitize_cache_component(plan.projectDir.filename().string());
295+
a.package = sanitize_cache_component(
296+
!plan.defaultTargetName.empty()
297+
? plan.defaultTargetName
298+
: plan.projectDir.filename().string());
296299
a.version = "local";
297300
a.target = sanitize_cache_component(
298301
opt.targetTriple.empty() ? detect_native_target_triple() : opt.targetTriple);
@@ -896,6 +899,9 @@ namespace vix::commands::BuildCommand
896899
userProjectDir = fs::absolute(userProjectDir).lexically_normal();
897900

898901
fs::path cmakeSourceDir = userProjectDir;
902+
std::string defaultTargetName = userProjectDir.filename().string();
903+
bool generatedFromVixApp = false;
904+
899905
const fs::path cmakeListsPath = userProjectDir / "CMakeLists.txt";
900906
const fs::path appManifestPath = userProjectDir / "vix.app";
901907

@@ -927,6 +933,8 @@ namespace vix::commands::BuildCommand
927933
}
928934

929935
cmakeSourceDir = generateResult.sourceDir;
936+
defaultTargetName = loadResult.manifest.name;
937+
generatedFromVixApp = true;
930938
}
931939

932940
const auto presetOpt = build::resolve_builtin_preset(opt.preset);
@@ -935,13 +943,17 @@ namespace vix::commands::BuildCommand
935943
return std::nullopt;
936944

937945
process::Plan plan;
938-
plan.projectDir = cmakeSourceDir;
946+
plan.userProjectDir = userProjectDir;
947+
plan.cmakeSourceDir = cmakeSourceDir;
948+
plan.projectDir = userProjectDir;
949+
plan.defaultTargetName = defaultTargetName;
950+
plan.generatedFromVixApp = generatedFromVixApp;
939951
plan.preset = *presetOpt;
940952

941953
plan.launcher = detect_launcher(opt);
942954
plan.fastLinkerFlag = detect_fast_linker_flag(opt);
943955
plan.projectFingerprint =
944-
util::compute_cmake_config_fingerprint(plan.projectDir);
956+
util::compute_cmake_config_fingerprint(plan.cmakeSourceDir);
945957

946958
if (!opt.targetTriple.empty())
947959
plan.buildDir = userProjectDir / (plan.preset.buildDirName + "-" + opt.targetTriple);
@@ -1031,12 +1043,15 @@ namespace vix::commands::BuildCommand
10311043
static std::optional<fs::path> resolve_main_executable(
10321044
const fs::path &buildDir,
10331045
const fs::path &projectDir,
1034-
const std::string &buildTarget)
1046+
const std::string &buildTarget,
1047+
const std::string &defaultTargetName)
10351048
{
10361049
const std::string preferredBase =
10371050
!buildTarget.empty()
10381051
? buildTarget
1039-
: projectDir.filename().string();
1052+
: (!defaultTargetName.empty()
1053+
? defaultTargetName
1054+
: projectDir.filename().string());
10401055

10411056
const std::string preferredName = platform_executable_name(preferredBase);
10421057

@@ -1258,7 +1273,12 @@ namespace vix::commands::BuildCommand
12581273
if (!opt.outPath.empty())
12591274
return fs::absolute(fs::path(opt.outPath));
12601275

1261-
return plan.buildDir / platform_executable_name(plan.projectDir.filename().string());
1276+
const std::string target =
1277+
!plan.defaultTargetName.empty()
1278+
? plan.defaultTargetName
1279+
: plan.projectDir.filename().string();
1280+
1281+
return plan.buildDir / platform_executable_name(target);
12621282
}
12631283

12641284
static bool file_exists_regular(const fs::path &path)
@@ -1608,7 +1628,7 @@ namespace vix::commands::BuildCommand
16081628

16091629
if (opt.exportBin)
16101630
{
1611-
const fs::path dest = plan.projectDir / outputBinary.filename();
1631+
const fs::path dest = plan.userProjectDir / outputBinary.filename();
16121632

16131633
if (!export_built_binary(outputBinary, dest, opt.quiet))
16141634
return 1;
@@ -1672,7 +1692,7 @@ namespace vix::commands::BuildCommand
16721692
{
16731693
try
16741694
{
1675-
clean_local_build_dirs(plan_.projectDir, opt_.targetTriple, opt_.quiet);
1695+
clean_local_build_dirs(plan_.userProjectDir, opt_.targetTriple, opt_.quiet);
16761696
}
16771697
catch (const std::exception &)
16781698
{
@@ -1766,7 +1786,7 @@ namespace vix::commands::BuildCommand
17661786

17671787
std::vector<artifact_cache::ProjectInput> projectInputs =
17681788
artifact_cache::ArtifactCache::snapshot_project_inputs(
1769-
plan_.projectDir,
1789+
plan_.userProjectDir,
17701790
previousState ? &previousState->inputs : nullptr);
17711791

17721792
const bool buildStateHit =
@@ -1787,14 +1807,14 @@ namespace vix::commands::BuildCommand
17871807
}
17881808

17891809
build::BuildGraphConfig graphConfig;
1790-
graphConfig.projectDir = plan_.projectDir;
1810+
graphConfig.projectDir = plan_.userProjectDir;
17911811
graphConfig.buildDir = plan_.buildDir;
17921812
graphConfig.objectDir = plan_.buildDir / ".vix" / "obj";
17931813
graphConfig.compiler = "c++";
17941814
graphConfig.buildFingerprint = plan_.signature;
17951815

1796-
graphConfig.includeDirs.push_back((plan_.projectDir / "include").string());
1797-
graphConfig.includeDirs.push_back((plan_.projectDir / "src").string());
1816+
graphConfig.includeDirs.push_back((plan_.userProjectDir / "include").string());
1817+
graphConfig.includeDirs.push_back((plan_.userProjectDir / "src").string());
17981818

17991819
graphConfig.flags.push_back("-Wall");
18001820
graphConfig.flags.push_back("-Wextra");
@@ -1927,7 +1947,15 @@ namespace vix::commands::BuildCommand
19271947
if (debug_build_details_enabled() && !opt_.quiet)
19281948
{
19291949
out.print(" Using project directory:\n");
1930-
out.print("" + plan_.projectDir.string() + "\n\n");
1950+
out.print("" + plan_.userProjectDir.string() + "\n");
1951+
1952+
if (plan_.generatedFromVixApp)
1953+
{
1954+
out.print(" Using generated CMake source:\n");
1955+
out.print("" + plan_.cmakeSourceDir.string() + "\n");
1956+
}
1957+
1958+
out.print("\n");
19311959
}
19321960

19331961
if (!opt_.targetTriple.empty())
@@ -1952,7 +1980,7 @@ namespace vix::commands::BuildCommand
19521980

19531981
if (verboseMode && !opt_.quiet)
19541982
{
1955-
out.print("Configuring " + plan_.projectDir.filename().string() +
1983+
out.print("Configuring " + build::default_build_target_name(opt_, plan_) +
19561984
" (" + display_build_profile(plan_) + ")\n");
19571985

19581986
if (debug_build_details_enabled())
@@ -1988,7 +2016,7 @@ namespace vix::commands::BuildCommand
19882016
const std::string log = util::read_text_file_or_empty(plan_.configureLog);
19892017
const bool handled = vix::cli::ErrorHandler::printBuildErrors(
19902018
log,
1991-
plan_.projectDir / "CMakeLists.txt",
2019+
plan_.cmakeSourceDir / "CMakeLists.txt",
19922020
"CMake configure failed");
19932021

19942022
if (!opt_.quiet && !handled)
@@ -2044,8 +2072,9 @@ namespace vix::commands::BuildCommand
20442072

20452073
const auto exeOpt = resolve_main_executable(
20462074
plan_.buildDir,
2047-
plan_.projectDir,
2048-
opt_.buildTarget);
2075+
plan_.userProjectDir,
2076+
opt_.buildTarget,
2077+
plan_.defaultTargetName);
20492078

20502079
if (exeOpt)
20512080
lastBinary = exeOpt->string();
@@ -2142,7 +2171,7 @@ namespace vix::commands::BuildCommand
21422171
const std::string log = util::read_text_file_or_empty(plan_.buildLog);
21432172
const bool handled = vix::cli::ErrorHandler::printBuildErrors(
21442173
log,
2145-
plan_.projectDir / "CMakeLists.txt",
2174+
plan_.cmakeSourceDir / "CMakeLists.txt",
21462175
"Build failed");
21472176

21482177
if (!opt_.quiet && !handled)
@@ -2161,8 +2190,9 @@ namespace vix::commands::BuildCommand
21612190

21622191
const auto exeOpt = resolve_main_executable(
21632192
plan_.buildDir,
2164-
plan_.projectDir,
2165-
opt_.buildTarget);
2193+
plan_.userProjectDir,
2194+
opt_.buildTarget,
2195+
plan_.defaultTargetName);
21662196

21672197
if (exeOpt)
21682198
lastBinary = exeOpt->string();
@@ -2230,8 +2260,9 @@ namespace vix::commands::BuildCommand
22302260
{
22312261
const auto exeOpt = resolve_main_executable(
22322262
plan_.buildDir,
2233-
plan_.projectDir,
2234-
opt_.buildTarget);
2263+
plan_.userProjectDir,
2264+
opt_.buildTarget,
2265+
plan_.defaultTargetName);
22352266

22362267
if (!exeOpt)
22372268
{
@@ -2243,7 +2274,7 @@ namespace vix::commands::BuildCommand
22432274
fs::path dest;
22442275
if (opt_.exportBin)
22452276
{
2246-
dest = plan_.projectDir / exeOpt->filename();
2277+
dest = plan_.userProjectDir / exeOpt->filename();
22472278
}
22482279
else
22492280
{

0 commit comments

Comments
 (0)