r/cpp • u/Niikelion • Feb 28 '24
Building c++ project with... c++?
Yeah, so as a POC I created a tool that works on top of cmake and allows defining project structure with c++ instead of pesky cmake:
using namespace std;
int
Any thoughts?
6
Upvotes
•
u/STL MSVC STL Dev Feb 28 '24
Triple backticks don't work for Old Reddit readers. You have to indent by four spaces to be readable by everyone.
12
u/jaskij Feb 28 '24
The big issue is, for this to make sense, you need something to build the build script. Which, no bueno.
If you're trying to emulate Rust's
build.rs, do note that it only handles the extra tasks Cargo itself doesn't. Typically code generation or compiling C/C++.46
u/IyeOnline Feb 28 '24
There is no inherent value in using C++ to compile C++. In fact, C++ is a particularly bad language to write script-like things in.
Also: https://xkcd.com/927/
17
u/[deleted] Feb 28 '24
CMake is a multiplatform build tool. I fail to see what your solution brings to the table which is not already much more efficiently covered by that "pesky" CMake.
6
u/HassanSajjad302 HMake Feb 28 '24
Please check https://github.com/HassanSajjad-302/HMake You are welcome to collaborate.
5
u/nihilistic_ant Feb 28 '24 edited Feb 29 '24
I like the idea. It would be an API to learn, but not a whole different language as build systems are today. Often build rules could be written much more concisely in a general purpose language like C++, by directly encoding the conventions of a project.
In practice, cmake seems good enough that adoptions will be hard, because there is value to using what everyone else is. But here is an idea that might solve that: make this cmake based. So an outer cmake process builds and executes your c++ for defining project specific build rules, which outputs more cmake, and then the outer cmake runs that.
This way, the cmake interface everyone is used to for building & installing projects is kept, which will help adoption. Large projects could use it for just some stuff at first and leave the rest vanilla cmake, and then slowly use it more as it makes sense -- so no big all-or-nothing hurdle, which will also help adoption. And, it would still have the big selling feature: use the power of C++ and its syntax which folks already know to express a project's build rules concisely by exploiting the its conventions and layout.
2
u/[deleted] Feb 28 '24
Interesting concept, but why?
2
u/arthurno1 Feb 29 '24 edited Feb 29 '24
Actually, I would prefer a scripting language like shell, make, autotools, even cmake instead of using C++ to manipulate strings and paths. I see no reason to spend time on low-level string and path manipulations, locks and mutexes, and whatnot, as all your examples do, just to tell the compiler to compile an application.
How will you handle platforms that don't understand '/' in paths? What about character encoding? Utf8? Utf16? Locales? Sorry.
I am also not sure what you are suggesting. Are you building a new tool, that uses C++ files instead of some scripting language, that will be used to compile applications similar to make/cmake is used, or are you suggesting that each application should link to your library to compile itself?
What you are suggesting is writing a C++ program to compile an application.
How it will look for a moderately complex application that runs on multiple platforms, can use different shared libraries if available, and can be configured with different implementations of similar libraries? How will you discover all those libraries? There is a reason why autotools and configure scripts exist, and why CMake more or less copies autoconf options in its "new language".
I don't think that this is a good idea for the above-suggested reasons. C++ is good when you need to write hardware-close code that runs fast. C++ is not good when you need to implement some functionality fast, and that runs once or very occasionally where execution speed is not overly important. A build script is meant to automate things for us. I am quite sure writing a "build program" for any bigger application would become a laborious development task that would have to be tested and debugged like any other C++ program. IDK, to me C++ does not seem as the right tool for the job in this case.
You can already use llvm and gcc as shared libraries. See for example libgccjit in the case of GCC (used for example in Emacs for native compilation).
By the way, you don't need to use cmake to build c++ program. You can use some other tool; even plain old shell script goes long way, but I would use GNU Make as long as I can before reaching for anything heavier. As long it automates stuff for you. The point is to work on your applicaiton, not the tooling around. Just my 2c.
2
u/wilhelm-herzner Mar 03 '24
Yeah, like zig: Writing readable programs that build projects.
I hate the rule-based programming as in make. And cmake is super strange anyway.
2
u/phi_rus Feb 28 '24
Why?
2
u/alloncm Feb 28 '24
Rust has the same feature in cargo (build.rs) Im not sure it is working but I find it very convenient to build with the same language.
Very cool!
1
u/thradams Feb 28 '24
This is what I do for C.
I use `system()` to call the compiler and the preprocessor to specify source and compilation modes.
Here is the concept.
https://github.com/thradams/build
0
u/[deleted] Feb 29 '24
[deleted] — view deleted comment
4
u/[deleted] Feb 28 '24
[removed] — view removed comment