Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: support lib.cc #1035

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ba22565
initial library building support
Nov 14, 2024
aef5ee2
remove debug print
Nov 14, 2024
8026e26
fmt
Nov 14, 2024
d7963cf
Merge pull request #1 from poac-dev/main
Lazauya Nov 14, 2024
739474c
Merge remote-tracking branch 'refs/remotes/origin/main' into lib-buil…
Nov 14, 2024
8765dcc
merge master
Nov 14, 2024
3cb0dc7
rollback formatting
Nov 14, 2024
bac7f94
rollback something
Nov 14, 2024
47f8ec8
use format for lib build command
Nov 15, 2024
9cc8c6e
Merge branch 'poac-dev:main' into main
Lazauya Nov 15, 2024
d75857a
Merge remote-tracking branch 'origin/main' into lib-building
Nov 15, 2024
f12e007
formatting, fix uninitialized vars
Nov 15, 2024
3967984
formatting, fix uninitialized vars
Nov 15, 2024
7fc1152
fix lint errors
Nov 15, 2024
a32a4a6
change to fmt::format
Nov 15, 2024
b822f85
format
Nov 15, 2024
b00868f
Merge branch 'poac-dev:main' into main
Lazauya Nov 16, 2024
344be4c
Merge remote-tracking branch 'origin/main' into lib-building
Nov 16, 2024
911d6e0
format
Nov 16, 2024
b28f9a9
fix build
Nov 16, 2024
9e58bf4
refactor build command into function
Nov 16, 2024
0ed1788
preallocate commands vec and make const
Nov 16, 2024
a2bebc9
change var names, introduce library name variables
Nov 21, 2024
e2da2be
Merge branch 'poac-dev:main' into main
Lazauya Nov 23, 2024
48c07e4
Merge remote-tracking branch 'refs/remotes/origin/main' into lib-buil…
Nov 23, 2024
1bc082f
move libname to BuildConfig and fix formatting
Nov 23, 2024
1452d25
combine defineLib/LinkTarget into single func, rename executable to b…
Nov 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 81 additions & 21 deletions src/BuildConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ operator<<(std::ostream& os, VarType type) {

BuildConfig::BuildConfig(const std::string& packageName, const bool isDebug)
: packageName{ packageName }, isDebug{ isDebug } {
Comment on lines 65 to 66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now this line looks awkward. Can you also please move this to the constructor body?

Suggested change
BuildConfig::BuildConfig(const std::string& packageName, const bool isDebug)
: packageName{ packageName }, isDebug{ isDebug } {
BuildConfig::BuildConfig(const std::string& packageName, const bool isDebug) {
packageName = packageName;
isDebug = isDebug;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clang tidy throws an error if I do that.

libName = fmt::format("lib{}.a", packageName);
const fs::path projectBasePath = getProjectBasePath();
if (isDebug) {
outBasePath = projectBasePath / "poac-out" / "debug";
Expand Down Expand Up @@ -486,12 +487,21 @@ BuildConfig::defineCompileTarget(
}

void
BuildConfig::defineLinkTarget(
const std::string& binTarget, const std::unordered_set<std::string>& deps
BuildConfig::defineOutputTarget(
const std::unordered_set<std::string>& buildObjTargets,
const std::string& targetInputPath,
const std::vector<std::string>& commands,
const std::string& targetOutputPath
) {
std::vector<std::string> commands;
commands.emplace_back("$(CXX) $(CXXFLAGS) $^ $(LIBS) -o $@");
defineTarget(binTarget, commands, deps);
// Project binary target.
std::unordered_set<std::string> projTargetDeps = { targetInputPath };
collectBinDepObjs(
projTargetDeps, "",
targets.at(targetInputPath).remDeps, // we don't need sourceFile
buildObjTargets
);

defineTarget(targetOutputPath, commands, projTargetDeps);
}

// Map a path to header file to the corresponding object file.
Expand Down Expand Up @@ -760,7 +770,11 @@ BuildConfig::processUnittestSrc(
);

// Test binary target.
defineLinkTarget(testTarget, testTargetDeps);
const std::vector<std::string> commands = {
"$(CXX) $(CXXFLAGS) $^ $(LIBS) -o $@"
};
defineTarget(testTarget, commands, testTargetDeps);
// defineLinkTarget(testTarget, testTargetDeps);

testTargets.insert(testTarget);
if (mtx) {
Expand Down Expand Up @@ -791,6 +805,9 @@ BuildConfig::configureBuild() {
const auto isMainSource = [](const fs::path& file) {
return file.filename().stem() == "main";
};
const auto isLibSource = [](const fs::path& file) {
return file.filename().stem() == "lib";
};
fs::path mainSource;
for (const auto& entry : fs::directory_iterator(srcDir)) {
const fs::path& path = entry.path();
Expand All @@ -800,15 +817,33 @@ BuildConfig::configureBuild() {
if (!isMainSource(path)) {
continue;
}
if (mainSource.empty()) {
mainSource = path;
} else {
if (!mainSource.empty()) {
throw PoacError("multiple main sources were found");
}
mainSource = path;
hasBinaryTarget = true;
}

fs::path libSource;
for (const auto& entry : fs::directory_iterator(srcDir)) {
const fs::path& path = entry.path();
if (!SOURCE_FILE_EXTS.contains(path.extension())) {
continue;
}
if (!isLibSource(path)) {
continue;
}
if (!libSource.empty()) {
throw PoacError("multiple lib sources were found");
}
libSource = path;
hasLibraryTarget = true;
}

if (mainSource.empty()) {
throw PoacError(fmt::format("src/main{} was not found", SOURCE_FILE_EXTS));
if (!hasBinaryTarget && !hasLibraryTarget) {
throw PoacError(
fmt::format("src/(main|lib){} was not found", SOURCE_FILE_EXTS)
);
}

if (!fs::exists(outBasePath)) {
Expand All @@ -817,8 +852,16 @@ BuildConfig::configureBuild() {

setVariables();

std::unordered_set<std::string> all = {};
if (hasBinaryTarget) {
all.insert(packageName);
}
if (hasLibraryTarget) {
all.insert(libName);
}

// Build rules
setAll({ packageName });
setAll(all);
addPhony("all");

std::vector<fs::path> sourceFilePaths = listSourceFilePaths(srcDir);
Expand All @@ -832,7 +875,16 @@ BuildConfig::configureBuild() {
"Move it directly to 'src/' if intended as such.",
sourceFilePath.string()
);
} else if (sourceFilePath != libSource && isLibSource(sourceFilePath)) {
logger::warn(
"source file `{}` is named `lib` but is not located directly in the "
"`src/` directory. "
"This file will not be treated as a hasLibraryTarget. "
"Move it directly to 'src/' if intended as such.",
sourceFilePath.string()
);
}

srcs += ' ' + sourceFilePath.string();
}

Expand All @@ -842,16 +894,24 @@ BuildConfig::configureBuild() {
const std::unordered_set<std::string> buildObjTargets =
processSources(sourceFilePaths);

// Project binary target.
const std::string mainObjTarget = buildOutPath / "main.o";
std::unordered_set<std::string> projTargetDeps = { mainObjTarget };
collectBinDepObjs(
projTargetDeps, "",
targets.at(mainObjTarget).remDeps, // we don't need sourceFile
buildObjTargets
);
if (hasBinaryTarget) {
const std::vector<std::string> commands = {
"$(CXX) $(CXXFLAGS) $^ $(LIBS) -o $@"
};
defineOutputTarget(
buildObjTargets, buildOutPath / "main.o", commands,
outBasePath / packageName
);
}

defineLinkTarget(outBasePath / packageName, projTargetDeps);
if (hasLibraryTarget) {
const std::vector<std::string> commands = {
fmt::format("ar rcs {} $^", libName)
};
defineOutputTarget(
buildObjTargets, buildOutPath / "lib.o", commands, outBasePath / libName
);
}

// Test Pass
std::unordered_set<std::string> testTargets;
Expand Down
26 changes: 24 additions & 2 deletions src/BuildConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ struct BuildConfig {

private:
std::string packageName;
std::string libName;
fs::path buildOutPath;
fs::path unittestOutPath;
bool isDebug;

// if we are building an binary
bool hasBinaryTarget{ false };
// if we are building a hasLibraryTarget
bool hasLibraryTarget{ false };

std::unordered_map<std::string, Variable> variables;
std::unordered_map<std::string, std::vector<std::string>> varDeps;
std::unordered_map<std::string, Target> targets;
Expand All @@ -67,6 +73,16 @@ struct BuildConfig {
public:
explicit BuildConfig(const std::string& packageName, bool isDebug = true);

bool hasBinTarget() const {
return hasBinaryTarget;
}
bool hasLibTarget() const {
return hasLibraryTarget;
}
std::string getLibName() const {
return this->libName;
}

void defineVar(
const std::string& name, const Variable& value,
const std::unordered_set<std::string>& dependsOn = {}
Expand All @@ -77,12 +93,14 @@ struct BuildConfig {
varDeps[dep].push_back(name);
}
}

void defineSimpleVar(
const std::string& name, const std::string& value,
const std::unordered_set<std::string>& dependsOn = {}
) {
defineVar(name, { .value = value, .type = VarType::Simple }, dependsOn);
}

void defineCondVar(
const std::string& name, const std::string& value,
const std::unordered_set<std::string>& dependsOn = {}
Expand Down Expand Up @@ -142,8 +160,12 @@ struct BuildConfig {
const std::string& objTarget, const std::string& sourceFile,
const std::unordered_set<std::string>& remDeps, bool isTest = false
);
void defineLinkTarget(
const std::string& binTarget, const std::unordered_set<std::string>& deps

void defineOutputTarget(
const std::unordered_set<std::string>& buildObjTargets,
const std::string& targetInputPath,
const std::vector<std::string>& commands,
const std::string& targetOutputPath
);

void collectBinDepObjs( // NOLINT(misc-no-recursion)
Expand Down
38 changes: 27 additions & 11 deletions src/Cmd/Build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,45 @@ const Subcmd BUILD_CMD =
.setMainFn(buildMain);

int
buildImpl(std::string& outDir, const bool isDebug) {
const auto start = std::chrono::steady_clock::now();

const BuildConfig config = emitMakefile(isDebug, /*includeDevDeps=*/false);
outDir = config.outBasePath;

const std::string& packageName = getPackageName();
runBuildCommand(
const std::string& outDir, const BuildConfig& config,
const std::string& targetName
) {
const Command makeCmd = getMakeCommand().addArg("-C").addArg(outDir).addArg(
(config.outBasePath / packageName).string()
(config.outBasePath / targetName).string()
);
Command checkUpToDateCmd = makeCmd;
checkUpToDateCmd.addArg("--question");

int exitCode = execCmd(checkUpToDateCmd);
if (exitCode != EXIT_SUCCESS) {
// If packageName binary is not up-to-date, compile it.
// If `targetName` is not up-to-date, compile it.
logger::info(
"Compiling", "{} v{} ({})", packageName, getPackageVersion().toString(),
"Compiling", "{} v{} ({})", targetName, getPackageVersion().toString(),
getProjectBasePath().string()

);
exitCode = execCmd(makeCmd);
}
return exitCode;
}

int
buildImpl(std::string& outDir, const bool isDebug) {
const auto start = std::chrono::steady_clock::now();

const BuildConfig config = emitMakefile(isDebug, /*includeDevDeps=*/false);
outDir = config.outBasePath;

const std::string& packageName = getPackageName();
int exitCode = 0;
if (config.hasBinTarget()) {
exitCode = runBuildCommand(outDir, config, packageName);
}

if (config.hasLibTarget() && exitCode == 0) {
const std::string libName = config.getLibName();
exitCode = runBuildCommand(outDir, config, libName);
}

const auto end = std::chrono::steady_clock::now();
const std::chrono::duration<double> elapsed = end - start;
Expand Down