Skip to content

Commit 1b2cddc

Browse files
authored
feat: allow adding cpp1 module third party deps in build.cpp2 (#16)
1 parent 41b9339 commit 1b2cddc

File tree

6 files changed

+624
-69
lines changed

6 files changed

+624
-69
lines changed

.clang-format

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,100 @@
1-
UseTab: Always
2-
IndentWidth: 4
3-
TabWidth: 4
1+
AccessModifierOffset: -2
2+
AlignAfterOpenBracket: BlockIndent
3+
AlignConsecutiveAssignments: false
4+
AlignConsecutiveDeclarations: true
5+
AlignEscapedNewlines: Left
6+
AlignOperands: DontAlign
7+
AlignTrailingComments: false
8+
AllowAllArgumentsOnNextLine: false
9+
AllowAllParametersOfDeclarationOnNextLine: false
10+
AllowShortBlocksOnASingleLine: false
11+
AllowShortCaseLabelsOnASingleLine: false
12+
AllowShortFunctionsOnASingleLine: None
13+
AllowShortIfStatementsOnASingleLine: WithoutElse
14+
AllowShortLoopsOnASingleLine: false
15+
AlwaysBreakAfterDefinitionReturnType: None
16+
AlwaysBreakAfterReturnType: None
17+
AlwaysBreakBeforeMultilineStrings: true
18+
AlwaysBreakTemplateDeclarations: Yes
19+
BinPackArguments: false
20+
BinPackParameters: false
21+
BraceWrapping:
22+
AfterClass: false
23+
AfterControlStatement: false
24+
AfterEnum: false
25+
AfterFunction: false
26+
AfterNamespace: false
27+
AfterStruct: false
28+
AfterUnion: false
29+
AfterExternBlock: false
30+
BeforeCatch: false
31+
BeforeElse: false
32+
IndentBraces: false
33+
SplitEmptyFunction: false
34+
SplitEmptyRecord: false
35+
SplitEmptyNamespace: false
36+
BreakAfterJavaFieldAnnotations: false
37+
BreakBeforeBinaryOperators: None
38+
BreakBeforeBraces: Custom
39+
BreakBeforeInheritanceComma: false
40+
BreakBeforeTernaryOperators: true
41+
BreakConstructorInitializers: BeforeComma
42+
BreakConstructorInitializersBeforeComma: false
43+
BreakStringLiterals: true
44+
ColumnLimit: 80
45+
CompactNamespaces: false
46+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
47+
ConstructorInitializerIndentWidth: 2
48+
ContinuationIndentWidth: 2
49+
Cpp11BracedListStyle: true
50+
DerivePointerAlignment: false
51+
DisableFormat: false
52+
ExperimentalAutoDetectBinPacking: false
53+
FixNamespaceComments: true
54+
IncludeBlocks: Preserve
55+
IncludeIsMainRegex: "(Test)?$"
56+
IndentCaseLabels: true
57+
IndentPPDirectives: AfterHash
58+
IndentWrappedFunctionNames: false
59+
InsertBraces: true
60+
JavaScriptQuotes: Leave
61+
JavaScriptWrapImports: true
62+
KeepEmptyLinesAtTheStartOfBlocks: false
63+
MacroBlockBegin: ""
64+
MacroBlockEnd: ""
65+
MaxEmptyLinesToKeep: 1
66+
NamespaceIndentation: None
67+
PackConstructorInitializers: NextLine
68+
PenaltyBreakAssignment: 10
69+
PenaltyBreakBeforeFirstCallParameter: 30
70+
PenaltyBreakComment: 10
71+
PenaltyBreakFirstLessLess: 0
72+
PenaltyBreakString: 10
73+
PenaltyExcessCharacter: 400
74+
PenaltyReturnTypeOnItsOwnLine: 350
75+
PointerAlignment: Left
76+
ReflowComments: true
77+
RequiresClausePosition: OwnLine
78+
SortIncludes: false
79+
SortUsingDeclarations: true
80+
SpaceAfterCStyleCast: false
81+
SpaceAfterLogicalNot: false
82+
SpaceAfterTemplateKeyword: false
83+
SpaceBeforeAssignmentOperators: true
84+
SpaceBeforeCtorInitializerColon: true
85+
SpaceBeforeInheritanceColon: true
86+
SpaceBeforeParens: Never
87+
SpaceBeforeRangeBasedForLoopColon: true
88+
SpaceInEmptyParentheses: false
89+
SpacesBeforeTrailingComments: 1
90+
SpacesInAngles: Never
91+
SpacesInContainerLiterals: false
92+
SpacesInCStyleCastParentheses: false
93+
SpacesInParentheses: false
94+
SpacesInSquareBrackets: false
95+
SeparateDefinitionBlocks: Always
96+
Standard: Latest
97+
UseTab: Never
98+
IndentWidth: 2
99+
TabWidth: 2
100+

.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ insert_final_newline = true
66
indent_style = tab
77
indent_size = 4
88

9+
[*.{cppm.tpl}]
10+
indent_style = space
11+
indent_size = 2
12+
913
[*.{sh,cmd}]
1014
indent_size = 4
1115
indent_style = space

build.cpp2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import cpp2b.build;
22

33
build: (inout b: cpp2b::build) -> void = {
4+
_ = b.cpp1_module().source_path("src/dylib.cppm");
5+
_ = b.cpp1_module().source_path("src/nlohmann.json.cppm");
46
b.binary_name("src/main", "cpp2b");
57
}

examples/donut/build.cpp2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import cpp2b.build;
22

33
build: (inout b: cpp2b::build) -> void = {
4+
_ = b.cpp1_module().source_path("gotoxy.cppm");
45
}

share/cpp2b/cpp2b.build.cppm.tpl

Lines changed: 201 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,220 @@ export module cpp2b.build;
1111
import std;
1212

1313
struct cpp2b_detail_build_impl;
14-
CPP2B_BUILD_API void (*cpp2b_detail_build_binary_name)(
15-
cpp2b_detail_build_impl *, std::filesystem::path,
16-
std::string_view) = nullptr;
14+
struct cpp2b_detail_git_repo_impl;
15+
struct cpp2b_detail_cpp1_module_impl;
16+
17+
template<typename Signature>
18+
using func_ptr = Signature*;
19+
20+
#define CPP2B_BUILD_DECL_FN(name, signature) \
21+
CPP2B_BUILD_API func_ptr<signature> name = nullptr
22+
23+
#define CPP2B_BUILD_FN_CHECK(name) \
24+
if(name == nullptr) { \
25+
std::println("\033[0;31mINTERNAL ERROR\033[0m: {} is unset", #name); \
26+
std::exit(1); \
27+
} \
28+
static_assert(true, "macro needs semicolon")
29+
30+
CPP2B_BUILD_DECL_FN(
31+
cpp2b_detail_build_binary_name,
32+
void(
33+
cpp2b_detail_build_impl* impl,
34+
std::filesystem::path p,
35+
std::string_view name
36+
)
37+
);
38+
39+
CPP2B_BUILD_DECL_FN(
40+
cpp2b_detail_git_clone,
41+
std::shared_ptr<cpp2b_detail_git_repo_impl>(
42+
cpp2b_detail_build_impl* impl,
43+
std::string_view clone_url,
44+
std::string_view commitish
45+
)
46+
);
47+
48+
CPP2B_BUILD_DECL_FN(
49+
cpp2b_detail_git_repo_path,
50+
std::filesystem::path(const cpp2b_detail_git_repo_impl* impl)
51+
);
52+
53+
CPP2B_BUILD_DECL_FN(
54+
cpp2b_detail_create_cpp1_module,
55+
std::shared_ptr<cpp2b_detail_cpp1_module_impl>(cpp2b_detail_build_impl* impl)
56+
);
57+
58+
CPP2B_BUILD_DECL_FN(
59+
cpp2b_detail_cpp1_module_source_path,
60+
void(
61+
cpp2b_detail_cpp1_module_impl* impl,
62+
std::filesystem::path p,
63+
std::source_location caller_srcloc
64+
)
65+
);
66+
67+
CPP2B_BUILD_DECL_FN(
68+
cpp2b_detail_cpp1_module_include_directory,
69+
void(
70+
cpp2b_detail_cpp1_module_impl* impl,
71+
std::filesystem::path p,
72+
std::source_location caller_srcloc
73+
)
74+
);
75+
76+
CPP2B_BUILD_DECL_FN(
77+
cpp2b_detail_cpp1_module_system_include_directory,
78+
void(
79+
cpp2b_detail_cpp1_module_impl* impl,
80+
std::filesystem::path p,
81+
std::source_location caller_srcloc
82+
)
83+
);
84+
85+
CPP2B_BUILD_DECL_FN(
86+
cpp2b_detail_cpp1_module_define,
87+
void(
88+
cpp2b_detail_cpp1_module_impl* impl,
89+
std::string_view name,
90+
std::string_view value,
91+
std::source_location caller_srcloc
92+
)
93+
);
1794

1895
export namespace cpp2b {
96+
class git_repo {
97+
std::shared_ptr<cpp2b_detail_git_repo_impl> impl;
98+
99+
public:
100+
inline git_repo(std::shared_ptr<cpp2b_detail_git_repo_impl> impl)
101+
: impl(impl) {
102+
}
103+
104+
inline git_repo(const git_repo& other) : impl(other.impl) {
105+
}
106+
107+
inline git_repo(git_repo&& other) : impl(other.impl) {
108+
other.impl = nullptr;
109+
}
110+
111+
inline ~git_repo() = default;
112+
113+
inline auto path() const -> std::filesystem::path {
114+
CPP2B_BUILD_FN_CHECK(cpp2b_detail_git_repo_path);
115+
return (*cpp2b_detail_git_repo_path)(impl.get());
116+
}
117+
};
118+
119+
class cpp1_module {
120+
std::shared_ptr<cpp2b_detail_cpp1_module_impl> impl;
121+
122+
public:
123+
inline cpp1_module(std::shared_ptr<cpp2b_detail_cpp1_module_impl> impl)
124+
: impl(impl) {
125+
}
126+
127+
inline cpp1_module(const cpp1_module& other) : impl(other.impl) {
128+
}
129+
130+
inline cpp1_module(cpp1_module&& other) : impl(other.impl) {
131+
other.impl = nullptr;
132+
}
133+
134+
inline ~cpp1_module() = default;
135+
136+
inline auto source_path(
137+
std::filesystem::path p,
138+
std::source_location caller_srcloc = std::source_location::current()
139+
) -> cpp1_module {
140+
CPP2B_BUILD_FN_CHECK(cpp2b_detail_cpp1_module_source_path);
141+
(*cpp2b_detail_cpp1_module_source_path)(impl.get(), p, caller_srcloc);
142+
return *this;
143+
}
144+
145+
inline auto include_directory(
146+
std::filesystem::path p,
147+
std::source_location caller_srcloc = std::source_location::current()
148+
) -> cpp1_module {
149+
CPP2B_BUILD_FN_CHECK(cpp2b_detail_cpp1_module_include_directory);
150+
(*cpp2b_detail_cpp1_module_include_directory)(impl.get(), p, caller_srcloc);
151+
return *this;
152+
}
153+
154+
inline auto system_include_directory(
155+
std::filesystem::path p,
156+
std::source_location caller_srcloc = std::source_location::current()
157+
) -> cpp1_module {
158+
CPP2B_BUILD_FN_CHECK(cpp2b_detail_cpp1_module_system_include_directory);
159+
(*cpp2b_detail_cpp1_module_system_include_directory)(
160+
impl.get(),
161+
p,
162+
caller_srcloc
163+
);
164+
return *this;
165+
}
166+
167+
inline auto define(
168+
std::string_view name,
169+
std::string_view value,
170+
std::source_location caller_srcloc = std::source_location::current()
171+
) -> cpp1_module {
172+
CPP2B_BUILD_FN_CHECK(cpp2b_detail_cpp1_module_define);
173+
(*cpp2b_detail_cpp1_module_define)(impl.get(), name, value, caller_srcloc);
174+
return *this;
175+
}
176+
};
177+
19178
class build {
20-
cpp2b_detail_build_impl *impl;
179+
cpp2b_detail_build_impl* impl;
21180
22181
public:
23-
inline build(cpp2b_detail_build_impl *impl) : impl(impl) {}
24-
inline build(const build &other) : impl(other.impl) {}
25-
inline build(build &&other) : impl(other.impl) { other.impl = nullptr; }
182+
inline build(cpp2b_detail_build_impl* impl) : impl(impl) {
183+
}
184+
185+
inline build(const build& other) : impl(other.impl) {
186+
}
187+
188+
inline build(build&& other) : impl(other.impl) {
189+
other.impl = nullptr;
190+
}
191+
26192
inline ~build() = default;
193+
27194
/**
28-
* Renames a binary. By default binary names are the same name as their source
29-
* file path with the extension replaced with the target platforms executable
30-
* extension.
195+
* Renames a binary. By default binary names are the same name as their
196+
* source file path with the extension replaced with the target platforms
197+
* executable extension.
31198
*/
32-
inline auto binary_name(std::filesystem::path binary_source_path,
33-
std::string_view new_binary_name) -> void {
34-
return (*cpp2b_detail_build_binary_name)(impl, binary_source_path,
35-
new_binary_name);
199+
inline auto binary_name(
200+
std::filesystem::path binary_source_path,
201+
std::string_view new_binary_name
202+
) -> void {
203+
CPP2B_BUILD_FN_CHECK(cpp2b_detail_build_binary_name);
204+
return (*cpp2b_detail_build_binary_name)(
205+
impl,
206+
binary_source_path,
207+
new_binary_name
208+
);
209+
}
210+
211+
inline auto git_clone(std::string_view clone_url, std::string_view commitish)
212+
-> git_repo {
213+
CPP2B_BUILD_FN_CHECK(cpp2b_detail_git_clone);
214+
return git_repo{(*cpp2b_detail_git_clone)(impl, clone_url, commitish)};
215+
}
216+
217+
inline auto cpp1_module() -> cpp1_module {
218+
CPP2B_BUILD_FN_CHECK(cpp2b_detail_create_cpp1_module);
219+
return ::cpp2b::cpp1_module{(*cpp2b_detail_create_cpp1_module)(impl)};
36220
}
37221
};
222+
38223
} // namespace cpp2b
39224

40-
extern "C" void build(cpp2b::build &b);
225+
extern "C" void build(cpp2b::build& b);
41226

42-
CPP2B_BUILD_API void cpp2b_detail_build(cpp2b_detail_build_impl *impl) {
227+
CPP2B_BUILD_API void cpp2b_detail_build(cpp2b_detail_build_impl* impl) {
43228
cpp2b::build b(impl);
44229
::build(b);
45230
}

0 commit comments

Comments
 (0)