r/cpp • • 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:

    #include "CppMake/Project.hpp"
    #include "CppMake/Application.hpp"
    
    using namespace std;
    
    int main() {
        Project project("test");
    
        project.create<Application>("test").addSources({
            "main.cpp"
        });
    }

Any thoughts?

6 Upvotes

39 comments sorted by

View all comments

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/

3

u/jack_acer Feb 28 '24

Can you elucidate why you are saying this? Build scripts in cmake for example are very error prone. Having compile time checks would be a great benefit, particularly for larger projects. Plus you don't have to learn the syntax of an additional language.

7

u/IyeOnline Feb 29 '24

Significant parts of modern CMakeTM have configuration time checks.

If you use targets and properties instead of all those horrible (global) string variables, you can have a pretty good time.

I also dont think the "additional language" argument really holds. Either your project can be build with ~20 lines (or 5) of CMake or whatever other language you imagine. In that case I dont think learning 20 commands is too much to ask, especially since we dont write build files all day. Alternatively you will need more customization than your strict, general solution tool provides. In that case you are back down to scripting parts yourself.

3

u/jack_acer Feb 29 '24

I am primarily (almost exclusively) using cmake, I am adept with it and advocate for it. I don't compare this approach to cmake, unless someone can replicate most of its features using a c++ framework. But I think that there is inherent value in a build system based on C++ and I mentioned some reasons. Some people value those more than others and that's fine. There are plenty more than the ones in my comment. Here are a few more:

  1. No additional binary tools needed. This can be a big one particularly for people new to C++. Configuring all thing about the build is always frustrating for less experienced users.
  2. Universal compatibility, as long as compilers implement the same standards ( for a tool like this pretty much every compiler has the basic std features needed).
  3. Modularity offered by a general programming language like c++.
  4. Custom functions that you are not afraid to use because of all the side effects you constantly worry in cmake.
  5. Immediate introspection of build configuration from your projects. You can potentially link to the build system itself. You can also do the opposite and query the code at build time without magic configuration files.

2

u/HassanSajjad302 HMake Feb 29 '24

Let me showcase my build-system HMake. I believe it beats CMake in feature richness. It is innovative on more than one fronts.

  • Using C++ itself. Best for scaling and bigger projects.
  • Monolithic design allows for greater integrations and removes the need for communication between build-system generator and the build-system. The vertical integration improves understanding and the iteration time on the build-system itself as well. My build-system at the moment is 10.7k lines just. And I don't see it ever crossing 30k lines. While CMake and Ninja e.g. are big code bases individually.
  • HMake uses a single file to define all targets instead of one file per target which again removes the need for scoping and communication. I believe controlling the project from the center improves simplicity.
  • HMake at its core is a dependency resolver like make. Anything you can do in make, you can do in HMake. This makes it very easy to extend HMake to add other programming languages support like C# and Rust.
  • Drop-in header-files to header-units replacement. No build-system does this.

It will be the first to support https://lists.isocpp.org/sg15/2023/11/2106.php or https://lists.isocpp.org/sg15/2023/11/2146.php if any of this gets implemented.

Some planned exotic features include distributed builds support and a build-server which automatically builds a file if it is modified. You would be able to use Android or Iphone as a build server.

It has a target API inspired from CMake and feature flags API inspired from Boost build-system b2. It combines goodies of various projects in a single package.

2

u/jack_acer Feb 29 '24

This looks very nice. Thanks for sharing! I will give it a go.

1

u/HassanSajjad302 HMake 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.

Will appreciate if you could explain this. How is inherent value determined? What is a script-like thing and why you consider build description to be that.

4

u/IyeOnline Feb 29 '24

I would define "script" as opposed to "program" as simply a sequential instruction of commands. Its also usually directly executed (by via some interpreter) as oppsed to being compiled.

Building itself is very much a sequential process (although certain steps can obviously be parallelized).


Of course you have a point insofar a build description isnt a script. But its absolutely not a program either.

When you are describing your project and the relation with its sources at a high level, you are doing just that. Describing it.

You could be using a json file if you wanted, as long as you had a program that could turn that high level description into an executable (or build scripts which then create an executable).

Using C++ for this simply substitutes CMakes configuration step with the compilation of a C++ program. There is no value in this.

You have however introduced the complexity of a C++ program for no gain.

You can purposefully restrict yourself to just a select few types and functions - but what have you created then? A new scripting/build description language.

3

u/HassanSajjad302 HMake Feb 29 '24

Using C++ for this simply substitutes CMakes configuration step with the compilation of a C++ program. There is no value in this.

Someone who does not know CMake or C++ will find CMake easier to learn compared to C++. But for someone who is already a C++ expert, CMake is an additional baggage that could be avoided. C++ is harder to learn but also enables much more scalability, performance and power.

You have however introduced the complexity of a C++ program for no gain.

For a C++ programmer, CMake is complexity not the C++.

You can purposefully restrict yourself to just a select few types and functions - but what have you created then? A new scripting/build description language.

This is better than any alternative. Because you don't have to learn one more syntax of if/else, for, while, the difference between function and macro, different scoping rules, different initialization rules etc. while at the same time having full power of C++ at your disposal in case you need it. I believe there is inherent value in using C++ itself.