Skip to content

Commit 17f3212

Browse files
authored
Builder: add BuildProfile & debug -> dev (#1151)
1 parent 342d363 commit 17f3212

18 files changed

+170
-108
lines changed

Diff for: .github/workflows/cpp.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ jobs:
102102
run: ./build/cabin --verbose run test --verbose
103103

104104
- name: Stage 2 - Print version
105-
run: ./cabin-out/debug/cabin version --verbose
105+
run: ./cabin-out/dev/cabin version --verbose
106106

107107
- name: Stage 2 - Integration Test
108108
run: find tests -maxdepth 1 -name '[0-9]*.sh' -print0 | xargs -0 -I {} sh -c 'sh {} -v'
109109
env:
110-
CABIN: ${{ github.workspace }}/cabin-out/debug/cabin
110+
CABIN: ${{ github.workspace }}/cabin-out/dev/cabin
111111
CABIN_TERM_COLOR: auto
112112

113113
# - name: Print coverage

Diff for: docs/hello-world.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ you:~$ cd hello_world
1818
you:~/hello_world$ cabin run
1919
Compiling src/main.cc
2020
Linking hello_world
21-
Finished debug target(s) in 0.45386s
22-
Running cabin-out/debug/hello_world
21+
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.45386s
22+
Running cabin-out/dev/hello_world
2323
Hello, world!
2424
```

Diff for: docs/usage.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ In most cases, you want to execute the generated binary as well as build the pro
2727
you:~/hello_world$ cabin run
2828
Compiling src/main.cc
2929
Linking hello_world
30-
Finished debug target(s) in 0.45386s
31-
Running cabin-out/debug/hello_world
30+
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.45386s
31+
Running cabin-out/dev/hello_world
3232
Hello, world!
3333
```
3434
3535
If you just want to build it, run the `build` command:
3636

3737
```console
3838
you:~/hello_world$ cabin build
39-
Finished debug target(s) in 0.00866317s
39+
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00866317s
4040
```
4141

4242
Cabin uses a cache since we executed the command with no changes.
@@ -111,7 +111,7 @@ you:~/hello_world$ cabin build
111111
Downloaded ToruNiina/toml11 846abd9a49082fe51440aa07005c360f13a67bbf
112112
Compiling src/main.cc
113113
Linking hello_world
114-
Finished debug target(s) in 0.70s
114+
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.70s
115115
```
116116
117117
### Remove dependencies

Diff for: src/BuildConfig.cc

+14-25
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ operator<<(std::ostream& os, VarType type) {
6161
}
6262

6363
Result<BuildConfig>
64-
BuildConfig::init(const Manifest& manifest, const bool isDebug) {
64+
BuildConfig::init(const Manifest& manifest, BuildProfile buildProfile) {
6565
using std::string_view_literals::operator""sv;
6666

6767
std::string libName;
@@ -71,20 +71,17 @@ BuildConfig::init(const Manifest& manifest, const bool isDebug) {
7171
libName = fmt::format("lib{}.a", manifest.package.name);
7272
}
7373

74-
fs::path outBasePath;
7574
const fs::path projectBasePath = manifest.path.parent_path();
76-
if (isDebug) {
77-
outBasePath = projectBasePath / "cabin-out" / "debug";
78-
} else {
79-
outBasePath = projectBasePath / "cabin-out" / "release";
80-
}
75+
fs::path outBasePath =
76+
projectBasePath / "cabin-out" / fmt::format("{}", buildProfile);
8177
fs::path buildOutPath = outBasePath / (manifest.package.name + ".d");
8278
fs::path unittestOutPath = outBasePath / "unittests";
8379

8480
Project project = Try(Project::init(fs::current_path()));
8581
return Ok(BuildConfig(
86-
manifest, isDebug, std::move(libName), std::move(outBasePath),
87-
std::move(buildOutPath), std::move(unittestOutPath), std::move(project)
82+
manifest, std::move(buildProfile), std::move(libName),
83+
std::move(outBasePath), std::move(buildOutPath),
84+
std::move(unittestOutPath), std::move(project)
8885
));
8986
}
9087

@@ -501,7 +498,7 @@ BuildConfig::installDeps(const bool includeDevDeps) {
501498

502499
void
503500
BuildConfig::setVariables() {
504-
project.setBuildProfile(isDebug);
501+
project.setBuildProfile(buildProfile);
505502

506503
defineSimpleVar("CXX", project.compiler.cxx);
507504
defineSimpleVar(
@@ -814,10 +811,11 @@ BuildConfig::configureBuild() {
814811

815812
Result<BuildConfig>
816813
emitMakefile(
817-
const Manifest& manifest, const bool isDebug, const bool includeDevDeps
814+
const Manifest& manifest, const BuildProfile& buildProfile,
815+
const bool includeDevDeps
818816
) {
819-
const Profile& profile = manifest.profiles.at(modeToProfile(isDebug));
820-
auto config = Try(BuildConfig::init(manifest, isDebug));
817+
const Profile& profile = manifest.profiles.at(buildProfile);
818+
auto config = Try(BuildConfig::init(manifest, buildProfile));
821819

822820
// When emitting Makefile, we also build the project. So, we need to
823821
// make sure the dependencies are installed.
@@ -860,9 +858,10 @@ emitMakefile(
860858
/// @returns the directory where the compilation database is generated.
861859
Result<std::string>
862860
emitCompdb(
863-
const Manifest& manifest, const bool isDebug, const bool includeDevDeps
861+
const Manifest& manifest, const BuildProfile& buildProfile,
862+
const bool includeDevDeps
864863
) {
865-
auto config = Try(BuildConfig::init(manifest, isDebug));
864+
auto config = Try(BuildConfig::init(manifest, buildProfile));
866865

867866
// compile_commands.json also needs INCLUDES, but not LIBS.
868867
Try(config.installDeps(includeDevDeps));
@@ -879,16 +878,6 @@ emitCompdb(
879878
return Ok(config.outBasePath);
880879
}
881880

882-
std::string_view
883-
modeToString(const bool isDebug) {
884-
return isDebug ? "debug" : "release";
885-
}
886-
887-
const char*
888-
modeToProfile(const bool isDebug) {
889-
return isDebug ? "dev" : "release";
890-
}
891-
892881
Command
893882
getMakeCommand() {
894883
Command makeCommand("make");

Diff for: src/BuildConfig.hpp

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

3-
#include "./Builder/Project.hpp"
3+
#include "Builder/BuildProfile.hpp"
4+
#include "Builder/Project.hpp"
45
#include "Command.hpp"
56
#include "Manifest.hpp"
67

@@ -59,7 +60,7 @@ class BuildConfig {
5960
std::string libName;
6061
fs::path buildOutPath;
6162
fs::path unittestOutPath;
62-
bool isDebug;
63+
BuildProfile buildProfile;
6364

6465
// if we are building an binary
6566
bool hasBinaryTarget{ false };
@@ -81,18 +82,18 @@ class BuildConfig {
8182
) const;
8283

8384
explicit BuildConfig(
84-
const Manifest& manifest, bool isDebug, std::string libName,
85+
const Manifest& manifest, BuildProfile buildProfile, std::string libName,
8586
fs::path outBasePath, fs::path buildOutPath, fs::path unittestOutPath,
8687
Project project
8788
)
8889
: outBasePath(std::move(outBasePath)), manifest(manifest),
8990
libName(std::move(libName)), buildOutPath(std::move(buildOutPath)),
90-
unittestOutPath(std::move(unittestOutPath)), isDebug(isDebug),
91-
project(std::move(project)) {}
91+
unittestOutPath(std::move(unittestOutPath)),
92+
buildProfile(std::move(buildProfile)), project(std::move(project)) {}
9293

9394
public:
9495
static Result<BuildConfig>
95-
init(const Manifest& manifest, bool isDebug = true);
96+
init(const Manifest& manifest, BuildProfile buildProfile = BuildProfile::Dev);
9697

9798
bool hasBinTarget() const {
9899
return hasBinaryTarget;
@@ -212,12 +213,14 @@ class BuildConfig {
212213
Result<void> configureBuild();
213214
};
214215

215-
Result<BuildConfig>
216-
emitMakefile(const Manifest& manifest, bool isDebug, bool includeDevDeps);
217-
Result<std::string>
218-
emitCompdb(const Manifest& manifest, bool isDebug, bool includeDevDeps);
219-
std::string_view modeToString(bool isDebug);
220-
const char* modeToProfile(bool isDebug);
216+
Result<BuildConfig> emitMakefile(
217+
const Manifest& manifest, const BuildProfile& buildProfile,
218+
bool includeDevDeps
219+
);
220+
Result<std::string> emitCompdb(
221+
const Manifest& manifest, const BuildProfile& buildProfile,
222+
bool includeDevDeps
223+
);
221224
Command getMakeCommand();
222225

223226
} // namespace cabin

Diff for: src/Builder/BuildProfile.hpp

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#pragma once
2+
3+
#include <fmt/format.h>
4+
#include <string>
5+
#include <utility>
6+
#include <variant>
7+
8+
namespace cabin {
9+
10+
class BuildProfile {
11+
friend struct fmt::formatter<BuildProfile>;
12+
friend struct std::hash<BuildProfile>;
13+
14+
public:
15+
enum Type : uint8_t {
16+
Dev,
17+
Release,
18+
};
19+
20+
private:
21+
using TypeT = std::variant<Type, std::string>;
22+
TypeT type;
23+
24+
public:
25+
BuildProfile() : type(Type::Dev) {}
26+
BuildProfile(Type type) : type(type) {} // NOLINT
27+
explicit BuildProfile(std::string type) : type(std::move(type)) {}
28+
29+
bool operator==(const BuildProfile& other) const {
30+
return type == other.type;
31+
}
32+
};
33+
34+
} // namespace cabin
35+
36+
template <>
37+
struct std::hash<cabin::BuildProfile> {
38+
std::size_t operator()(const cabin::BuildProfile& bp) const noexcept {
39+
return std::hash<cabin::BuildProfile::TypeT>{}(bp.type);
40+
}
41+
};
42+
43+
template <>
44+
struct fmt::formatter<cabin::BuildProfile> {
45+
// NOLINTNEXTLINE(*-static)
46+
constexpr auto parse(fmt::format_parse_context& ctx) {
47+
return ctx.begin();
48+
}
49+
50+
template <typename FormatContext>
51+
auto
52+
format(const cabin::BuildProfile& buildProfile, FormatContext& ctx) const {
53+
if (std::holds_alternative<cabin::BuildProfile::Type>(buildProfile.type)) {
54+
switch (std::get<cabin::BuildProfile::Type>(buildProfile.type)) {
55+
case cabin::BuildProfile::Dev:
56+
return fmt::format_to(ctx.out(), "dev");
57+
case cabin::BuildProfile::Release:
58+
return fmt::format_to(ctx.out(), "release");
59+
}
60+
__builtin_unreachable();
61+
} else {
62+
return fmt::format_to(
63+
ctx.out(), "{}", std::get<std::string>(buildProfile.type)
64+
);
65+
}
66+
}
67+
};

Diff for: src/Builder/Project.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "../Git2.hpp"
55
#include "../Rustify/Result.hpp"
66
#include "../TermColor.hpp"
7+
#include "BuildProfile.hpp"
78

89
#include <filesystem>
910
#include <spdlog/spdlog.h>
@@ -98,8 +99,8 @@ getEnvFlags(const char* name) {
9899
}
99100

100101
void
101-
Project::setBuildProfile(const bool isDebug) {
102-
const Profile& profile = manifest.profiles.at(isDebug ? "dev" : "release");
102+
Project::setBuildProfile(const BuildProfile& buildProfile) {
103+
const Profile& profile = manifest.profiles.at(buildProfile);
103104
if (profile.debug) {
104105
compiler.opts.cFlags.others.emplace_back("-g");
105106
compiler.opts.cFlags.macros.emplace_back("DEBUG", "");
@@ -154,7 +155,7 @@ Project::setBuildProfile(const bool isDebug) {
154155
defines.emplace("COMMIT_HASH", commitHash);
155156
defines.emplace("COMMIT_SHORT_HASH", commitShortHash);
156157
defines.emplace("COMMIT_DATE", commitDate);
157-
defines.emplace("PROFILE", std::string(isDebug ? "dev" : "release"));
158+
defines.emplace("PROFILE", fmt::format("{}", buildProfile));
158159

159160
const auto quote = [](auto&& val) {
160161
if constexpr (std::is_same_v<std::decay_t<decltype(val)>, std::string>) {

Diff for: src/Builder/Project.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "../Manifest.hpp"
44
#include "../Rustify/Result.hpp"
5+
#include "BuildProfile.hpp"
56
#include "Compiler.hpp"
67

78
#include <filesystem>
@@ -21,7 +22,7 @@ class Project {
2122

2223
static Result<Project> init(const fs::path& rootDir);
2324

24-
void setBuildProfile(bool isDebug);
25+
void setBuildProfile(const BuildProfile& buildProfile);
2526
};
2627

2728
} // namespace cabin

0 commit comments

Comments
 (0)