r/cpp • HMake • Apr 07 '21

I am trying to replace CMake with c++

I am writing a new build system HMake on top of CMake. Basically, HMake is a C++ library that generates a CMakeLists.txt file. There are very important features missing in it currently. Though I believe they can be added. I was able to generate two trivial CMakeLists.txt files which work for my two projects. One example is the following.

For following c++ code

    executable HMake("HMake", file("main.cpp"));
    for (auto x : fs::directory_iterator("src"))
    {
        HMake.sourceFiles.emplace_back(file(x));
    }
    directory headerDirectory("header");
    //IDD is for includeDirectoryDependency
    IDD headerFolderDependency{headerDirectory, dependencyType::PUBLIC};
    HMake.includeDirectoryDependencies.emplace_back(headerFolderDependency);
    HMake.setRunTimeOutputDirectory(directory("/home/hassan/Projects/HMake"));
    //This is the CMakeLists.txt file
    instructionsFileMain mainFile(HMake, projectDetails(CmakeVersion(), "HMake"));
    std::cout<<mainFile.getScript();

This generates the following CMakeLists.txt file

cmake_minimum_required(VERSION 3.0.0)

project(HMake LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(HMake
        main.cpp
        src/executable.cpp
        src/projectdetails.cpp
        src/file.cpp
        src/sourcefiles.cpp
        src/directory.cpp
        src/instructionsFileMain.cpp
        src/cxx_standard.cpp
        src/cxxStandard.cpp
        src/libraryDependency.cpp
        src/library.cpp
        )

target_include_directories(HMake
        PUBLIC header
        )

set_target_properties(HMake PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY /home/hassan/Projects/HMake)

This is basically the same CMakeLists.txt file that I am using to build the library. Other example is a bit more complex and can be found in my project.

This lacks many important features e.g. I am providing hardcoded paths instead of variables like CMAKE_CURRENT_SOURCE_DIR and CMAKE_CURRENT_BINARY_DIR. These and many other missing features I think can be provided using the c++ interface.

Motivation:

This is proof that c++ can be used instead of CMake to specify instructions for build system generators.

Basically, we will embed a c++ compiler in HMake. Then instead of writing the CMakeLists.txt file in CMake language, you will write CMakeLists.txt in c++. Headers related to the build system will be provided by default. Other functions that help in installation or constants or enums that will be used to specify compiler version, c++ version, platform, platform version, architecture, etc will also be provided. This file will then be compiled and executed. At the end of its execution, we will have a data structure that has information that will be used for generating the build system. e.g. in the example above in the last line, we are printing the contents of the CMakeLists.txt file. Instead of this printing of the CMakeLists.txt file we just generate the build system. This is approach 1.

Or we can just embed the C++ parser and then use constexpr vector and string. This way we won't need to embed the compiler backend.

These are long-term goals. Short term goal is to support the most features of CMake in my build system using the optimal c++ interface. So that real-world projects can use my library to generate CMakeLists.txt file by writing c++ code. Once this is done, we can just embed the compiler and eliminate the middle stage of generating the CMakeLists.txt file.

0 Upvotes

68 comments sorted by

33

u/00jknight Apr 07 '21

on top of CMake

I'm all for replacing CMake but this is not that!

2

u/HassanSajjad302 HMake Apr 07 '21

Why?

13

u/Thorinori Apr 07 '21

Replacing CMake implies that it is a full replacement, that it does the functions of CMake. What your program does is it translates CPP into CMake, so it isn't actually a replacement. It is an additional layer to potentially make CMake easier to use (That is subjective though)

1

u/HassanSajjad302 HMake Apr 07 '21

It is just a concept and a proof of concept. It is by no means useable as a replacement of CMake YET and never ever I said so.

7

u/ydieb Apr 07 '21

What, you dont like meta2 build systems?
We need to go deeper, give me a few months, Ill create MMake, it stands for meta make which generates HMake files which create CMakeLists.txt files which create build.ninja files!

3

u/atatatko Apr 07 '21

Maybe you'd be interested in Meson. C/C++/Rust support, Python-like syntax, automated project conversion from CMake, supported in Qt6.

2

u/HassanSajjad302 HMake Apr 07 '21

The distinction of my build system generator will be that it takes its project specification file in c++.

3

u/atatatko Apr 07 '21

Still requires to be translated in CMake for work

3

u/HassanSajjad302 HMake Apr 07 '21

That's short-term. In long term, we will remove this middle part of generating CMakeLists.txt files and instead will directly generate the cache files and build system files.

1

u/RogerLeigh Scientific Imaging and Embedded Medical Diagnostics Apr 10 '21

What particular value does this add? I don't see anything but unnecessary complexity which is incompatible with cross-compiling.

5

u/germandiago Apr 07 '21

I voted you up but if you pronounce Meson when talking about CMake all fanatics will come to vote you down. Lol.

4

u/jk-jeon Apr 07 '21

OP said this is a proof that C++ can replace CMake. So this might not have been intended to be a software for real use I guess?

1

u/igagis Apr 16 '21

what don't you like in CMake?

Just want to understand what some people may not like in CMake.

I'm personally using my own build system based on GNU make. Trying to justify my choice for myself :D.

2

u/00jknight Apr 16 '21

I use CMake and prefer CMake to many of the alternatives. That being said, CMake files are nigh unreadable, and it is hard to work within them.

CMake is maybe the best we got right now, but it's not good enough. It's too hard to read. It's stupid that it has it's own syntax.

Honestly, I like SCons. Using python is just better, because it's more readable.

1

u/igagis Apr 16 '21

Yeah, I also find CMake syntax unreadable.

The only thing is that my own build system is GNU make-based, so it's syntax is also not easy :P.

Haven't tried scons, though. I need to look at that.

7

u/W00tbeer1 Apr 07 '21

Why though? What situations does this help simplify?

8

u/HassanSajjad302 HMake Apr 07 '21

You won't have to learn a new CMake language. Learning a new library interface in c++ will be easier than learning CMake.

5

u/kalmoc Apr 07 '21

Learning a new library interface in c++ will be easier than learning CMake.

Why should it? Especially when considering:

Short term goal is to support the most features of CMake in my build system using the optimal c++ interface.

the more likely scenario is that you have to learn cmake and in addition learn how your library interface maps to cmake. So at best you'll be able to achieve syntactic convenience.

3

u/HassanSajjad302 HMake Apr 07 '21

You will have to learn concepts like targets, exporting, importing, etc. But not CMake.

6

u/kalmoc Apr 07 '21

Take your example: RuntimeOutputDirectory is a cmake specific concept. Also, importing and exporting are very non-trivial concepts. Imho learning all those concepts is much more difficult than learning the cmake syntax.

But well, I'm certainly willing to be proven wrong.

3

u/kalmoc Apr 07 '21

This is proof that c++ can be used instead of CMake to specify instructions for build system generators.

How is writing something in c++ that relies on X a proof that c++ can replace X?

Of course the statement that c++ can be used to replace cmake itself is trivially true: C++ is turing complete, so you can use it to replace any other "programming language". Whether its practical is a completely different question of course.

2

u/HassanSajjad302 HMake Apr 07 '21

By saying that it is proof I meant that I intend to go in this direction and I will like your thoughts on it and will be glad if you join in. And that I see it as practical and useful. And I see no logical fallacy in it

Indeed proof of my statement will be when HMake is ready.

3

u/almost_useless Apr 07 '21
directory headerDirectory("header");
//IDD is for includeDirectoryDependency
IDD headerFolderDependency{headerDirectory, dependencyType::PUBLIC};
HMake.includeDirectoryDependencies.emplace_back(headerFolderDependency);

When you read that and then see it's the equivalent of this:

target_include_directories(HMake PUBLIC header)

Surely you must think that maybe you are not on the right track here?

2

u/HassanSajjad302 HMake Apr 07 '21

Sorry, but I could not think that why I am not on the right track here. I will really appreciate if you provide more details here.

4

u/almost_useless Apr 07 '21

The CMake line is easy to read and it's intuitive what it means.

Your code is not. This is highlighted by the fact you need a comment to explain what the code is doing.

Even if you have never heard of cmake before you understand the gist of what it is trying to do.

Even if I understand c++ syntax and know what you are trying to do, there is still a big cognitive burden to parse that code.

The easy to read c++ version of that would look like this:

HMake.add_include_directory(dependencyType::PUBLIC, "header")

But then it's just exactly the same as the cmake-version except you need to quote the strings...

1

u/HassanSajjad302 HMake Apr 07 '21

This is a very small library interface problem. This does not prove that HMake will be useless or non-practical or is not possible. Or writing a build specification file in c++ is not possible. In fact, you yourself are giving one example of how to do it.

If you think your interface is better, you are welcome to open a pull request. You are welcome to design an optimal interface for other features such as library installation, export, import, toolchains file, build/install pre/post custom tasks.

4

u/almost_useless Apr 07 '21

Of course it does not prove or disprove anythings viability, but the burden of proof is on you.

It should also be better than cmake. Just doing the same thing but with c++ syntax is probably not going to be enough for a lot of people.

I just have a hard time seeing what things would actually be better with c++ syntax. I can see several minor things that would get worse though.

2

u/HassanSajjad302 HMake Apr 07 '21

You are understating the effort to learn and correctly write CMake. CMake does less on stopping you from writing erroneous code.

e.g. target_link_libraries(HMake PUBLIC abc) is an error if abc is not defined. But it won't be an error in any text editor/ide. However while writing the specification file in c++ you will see error diagnosed right there if you are using an undefined variable. HMake.libraryDependencies.emplace_back(abc);

Until the library is designed, nothing is proven. However, I see this fully practical and useful.

3

u/th3f1nal3 Apr 08 '21

So basically your dissatisfaction with CMake can be solved with a nice intellisense-level code hunter.

I mean, if I’m writing HMake with a basic text editor it will not tell me if abc is defined until I run it. Same thing with CMake.

2

u/RogerLeigh Scientific Imaging and Embedded Medical Diagnostics Apr 10 '21

You are understating the effort to learn and correctly write CMake.

It took me two weeks to go from zero knowledge to making a full release of a project, doing a full conversion of an Autoconf/Automake build system with a lot of custom macros and targets, unit tests, CI testing, the works.

People moan about the documentation and the scripting language. They are not entirely wrong; both could be better. But the documentation is mostly fine for reference material, and there are hundreds of tutorial examples and open-source projects to look at for any points of confusion, as well as several full written books. As for the language, it could be better, but it's serviceable. If you've had to use the Autotools, you'll realise just how much of an improvement it is on all counts.

Error handling could be stricter, but you have to bear in mind it has backward-compatibility requirements. There have been continual improvements with policy additions over the 8+ years I've been using it. So long as errors are correctly flagged when you run CMake, it's doing its job. Expecting the editor to identify invalid targets is asking a bit much; it could be done but requires support for that in multiple places.

6

u/eyes-are-fading-blue Apr 07 '21 edited Apr 07 '21

This would make a good April's fool joke, except you're a bit late.

Assuming you are serious, C++ is a bit too close to hardware to be useful for build systems. I would like to have little to no chance of corrupting my memory while writing build instructions.

5

u/HassanSajjad302 HMake Apr 07 '21

I am fully serious. Considering your logic why use c++ for anything. Why risk memory corruption?

A well-written c++ code does not let you corrupt memory easily.

5

u/almost_useless Apr 07 '21

Usually the reasons for using C++ are hardware/memory access and/or performance.

This does not match that.

2

u/jonesmz Apr 07 '21

Cmake scripting language?

https://github.com/stryku/cmakesl

1

u/HassanSajjad302 HMake Apr 07 '21

Yes, quite close to it.

Instead of using the c++-ish scripting style, I will use c++ itself.

4

u/[deleted] Apr 07 '21

[removed] — view removed comment

2

u/HassanSajjad302 HMake Apr 07 '21

Yes. If there is a distinction between CMake front-end and backend such that cmake front-end after parsing the CMakeLists.txt file feeds the data to libcmake which then generates the build system, our job is a bit easy. We can use this libcmake and feed it data from a c++ parser.

2

u/HassanSajjad302 HMake Apr 08 '21

How do I reach out to CMake maintainers?

1

u/RogerLeigh Scientific Imaging and Embedded Medical Diagnostics Apr 10 '21

There is (or was) some effort to add Lua language support to CMake as an alternative to (and eventual replacement for) the CMake scripting language. I'm not sure where that is at right now, but it looked like a good long-term strategy to move to a simple, robust and well-known language, which can easily wrap all of the CMake "project object model". But keeping it all interoperable with the existing scripting language semantics is a big job.

2

u/JulienVernay Apr 10 '21

I have actually suggested something similar: https://www.reddit.com/r/cpp/comments/j1ebld/draft_proposal_for_compilation_using_c_as_build/ I have put this in pause because it required to know a lot about linking which I am not competent about. That, and actually the toughest part of building c++ is managing external dependencies and cross-compilation.

1

u/HassanSajjad302 HMake Apr 10 '21

I have also put it on pause. I am trying to contact to cmake maintainers to get their opinion on it.

2

u/Possible-Direction-9 Apr 15 '21

I get it. You already know c++ because your application is written in c++. Why not use that same c++ language for your build system? It's not that cmake isn't good enough It's just now one more thing to learn. And yes c++ can be close to the hardware and low level but it doesn't have be and you can write c++ that stays high level and is safe and easy to follow.

1

u/HassanSajjad302 HMake Apr 15 '21

That's the best comment.

12

u/th3f1nal3 Apr 07 '21

Yay, now you can have three page std errors before even starting to build your project.

3

u/be-sc Apr 07 '21

But you’re more likely to get an error instead of some silent weird behaviour – maybe because you assumed a variable to exist were it didn’t.

2

u/th3f1nal3 Apr 07 '21

Pretty sure CMake has error messages for critical cases, like non-existent files. For less important stuff you can use “if not false” which is the same level of failure handling you would do in C++.

2

u/be-sc Apr 07 '21 edited Apr 07 '21

Yes, CMake does have some error detection. But consider a super common pattern in CMake code:

if ("${CMAKE_BULD_TYPE}" STREQUAL "")
    set(CMAKE_BUILD_TYPE "Release")
endif()

Did you catch the mistake? CMake won’t tell you. That’s nowhere near the level of error detection of a C++ compiler.

That said, I’m sceptical that C++ is a good choice of build script language.

Edit: For everybody who didn’t see it: There’s a typo (missing I) in the if condition. But CMake happily expands that non-existing variable to nothing, meaning the condition always compares the empty string to the empty string.

1

u/HassanSajjad302 HMake Apr 07 '21 edited Apr 07 '21

Yes exactly. Now, e.g. if you are doing this in c++. An interface could be like this.

if(Project.buildType == BuildType::None){
    Project.buildType = BuildType::Release;
}

Where BuildType is a class enum that can be inherited from for custom build configurations. While Project is a global variable.

4

u/helloiamsomeone Apr 07 '21

You shouldn't be setting CMAKE_BUILD_TYPE in CMLs anyway. The variable does fiddlesticks when the generator is multi-config and provides nothing of value. If you want a default value, then just use presets.

4

u/evaned Apr 07 '21

I am a CMake expert and definitely 100% see it; but for anyone who just happens to not know CMake, which definitely doesn't include me, what's the problem?

3

u/drjeats Apr 07 '21

It's the usual annoyance with languages that resemble shell script: it doesn't barf when encountering a variable that wasn't defined previously.

3

u/[deleted] Apr 07 '21

That piece of code pisses me off so much. Why is CMake like this?

1

u/th3f1nal3 Apr 08 '21

Yeah, I see your point here and it’s totally valid. It’s kinda solved by CMake linters, yet obviously you can’t enforce their usage.

3

u/helloiamsomeone Apr 07 '21

Don't. Especially not without learning CMake first and after that why even even bother with such a lib?

CMAKE_CXX_STANDARD and CMAKE_CXX_STANDARD_REQUIRED should not be set in a CML. 3.0 is too old, use 3.8 at the very least or 3.14 for header only libraries. You should not hardcode absolute paths into your CML, like in the RUNTIME_OUTPUT_DIRECTORY prop's case.

To learn how to write a CML that is ideally written to be easy for both consumers building the library, developers developing the library and consumers vendoring the library via add_subdirectory (which will be a very very important thing to do in the near future), please check out this educational example repository https://github.com/friendlyanon/cxx-static-shared-example

2

u/sat0ri Apr 08 '21

CMAKE_CXX_STANDARD and CMAKE_CXX_STANDARD_REQUIRED should not be set in a CML.

What's the reasoning behind this? What's the alternative?

2

u/helloiamsomeone Apr 08 '21

Compile features, which propagate to consumers.

1

u/sat0ri Apr 11 '21 edited Apr 11 '21

But this is very different. In my experience, most of the time you need to upgrade the standard for the whole project/repo, not set it for each target specifically (you may have thousands of them). And IMO propagating the standard to the consumers is not a good thing, they're using your ABI-stable headers so their standard of choice doesn't matter.

Using CMAKE_CXX_STANDARD and CMAKE_CXX_STANDARD_REQUIRED is preferable, unless you really need one of the targets to have different standard than the rest (questionable scenario), or you need to propagate your standard to the users (questionable again, better choice would be to design a standard-agnostic ABI).

1

u/helloiamsomeone Apr 11 '21

Who's talking about ABI? This is about propagating your standard requirements. If you are using C++17 features in your headers, then that better be reflected in your install interface.

Compile features also set a lower bound of requirement, meanwhile those variables set an upper bound or restrict to an exact version. That's only useful for passing in from CLI if the consumer has specific requirements (like a library declaring C++11 as lower bound, but consumer wants to build it in C++17 mode).

1

u/sat0ri Apr 11 '21 edited Apr 11 '21

My point is that suggesting

CMAKE_CXX_STANDARD and CMAKE_CXX_STANDARD_REQUIRED should not be set in a CML

without stating pros/cons of the alternative is wrong. CXX_STANDARD property or compile features do not replace CMAKE_CXX_STANDARD/CMAKE_CXX_STANDARD_REQUIRED. CMakeLists.txt writers should not be forced to set CXX_STANDARD for each target individually, especially if it's only in the name of some arbitrary "best practices".

My point re ABI vs standard is if you need to propagate C++ standard to consumers, you're either doing something unsafe (especially if the library is binary distributed with exported cmake targets) or it's a header-only library.

3

u/Pulseamm0 Apr 07 '21 edited Apr 07 '21

I've been trying to transition to cmake recently and I have to say the learning curve is steep.

Also it really doesn't help with the docs are out of date at seemingly random places, as someone new to cmake there's no way to know this stuff.

Case in point: CMAKE_CXX_STANDARD and CMAKE_CXX_STANDARD_REQUIRED are both recommended in the official tutorial for 3.20 (which looks like they haven't been updated since 3.10): https://cmake.org/cmake/help/latest/guide/tutorial/index.html#specify-the-c-standard

So please help a newbie out... can you tell me what the correct way to set that is? I think I've seen another way where you can set compiler flags or something, but it seemed compiler specific and since the CMAKE_CXX_STANDARD way was in the official tutorial I disregarded it.

Edit: Found the answer to that one, seems to be target_compile_features I think my point stands though, cmake documentation is a serious barrier to entry.

Honestly I got lured into thinking cmake might be easy, and it is if you're just using it to maybe declare a library or an executable, build it and be happy, but as soon as you get into packaging and configs and install and exports it's an uphill battle to take it in, they need to do better in this regard, and that's why I can see this guy trying to write a cmake generator. People seem to be missing that in the comments. "Learn CMake" is fine, but I'm not sure it should be this difficult, and it's causing projects like this to happen out of frustration or will to make it somehow easier.

Besides, it's not like this OP's project is the only effort. There's various "cmake template" project githubs it seems trying to make this stuff more user friendly.

2

u/john_wind Apr 07 '21

I would love to see something similar using python instead of C++

3

u/SJC_hacker Apr 09 '21

Its called scons

1

u/john_wind Apr 09 '21

I had no idea! Thanks!

1

u/RogerLeigh Scientific Imaging and Embedded Medical Diagnostics Apr 10 '21

Before you jump down that rabbit hole, let me say from the perspective of someone who converted some projects from SCons to CMake, that SCons was downright terrible: hard to maintain, hard to understand, terrible for portability, and had abysmal performance. CMake solved all of these problems hands down. CMake might not have the nicest syntax or the best quality examples, but it works really well.

3

u/SJC_hacker Apr 09 '21

Please don't.

2

u/zzzthelastuser Apr 10 '21

Don't let them trash talk your efforts!

Fuck CMake, seriously! People who keep defending CMake must have Stockholm syndrome.

1

u/zimnicky Apr 08 '21

There is a great thing from Ubisoft called Sharpmake: https://github.com/ubisoft/Sharpmake

Looks siimilar to what you're trying to do, but without CMake behind and C# as a language for configuration, which, in my opinion, is more friendly for such kind of things.

Maybe, it makes sense to contribute in it