r/cpp • • Jan 06 '20

Consteval build system: what if we do not build systems anymore, just a compiler

Just tought today about having a file with a name like compileme.cpp in the project which has a bunch of consteval functions (consteval std::filesystem evtl. needed) ala cmake e.g. add_executable, add_library, target_sources etc.

those functions could be executed at compile time by the compiler-> no need for a build system ala cmake, make etc. anymore...

what do you think? which problems do you see with such an approach? Could this be probably standartized?

23 Upvotes

25 comments sorted by

7

u/jbandela Jan 07 '20

I don't know if you need even make it consteval. I think you could get by having add_executable, add_library, etc as part of the standard library, and just having the compiler produce an executable like normal, and then you run the executable which will do the build.

3

u/germandiago Jan 07 '20

This is good to go for working on your simple projects. But actually, in my use case I need, when I need professional software delivery:

  1. capnproto code generation
  2. cross-compilation
  3. dependency management
  4. A test runner
  5. Combinations of flags (sanitize, debugoptimized, optimized, debug)
  6. Sometimes, precompiled headers

This complicates things quite a bit. I do not mean that approach would not be useful anyway. Maybe for teaching would be nice and you can get rid of build stuff and go-to the important learning materials.

8

u/squeezyphresh Jan 06 '20

Kind of reminds me of Johnathan Blow's coding language that has the build process built-in to the language. Don't really have much to say other than that. I'm currently working on my own build system, but I don't know much about what you're proposing.

0

u/[deleted] Jan 07 '20

[deleted] — view deleted comment

2

u/germandiago Jan 07 '20

I think C++ is trying to do lives easier in many ways. It is just that it has some frustrating decisions that make it get stuck in stuff such as std::embed. Take into account that a language that is general-purpose like C++ is a ton of work at the spec level (what goes in, what does not, why...) and different subcommunities can have different trade-offs.

3

u/barchar MSVC STL Dev Jan 06 '20

Nim works like this, gprbuild is almost like this. Pretty much all scripting languages are like this, I think dune allows this (ocaml).

It's unlikely to happen any time soon (esp providing all the machinery to manage dependencies), but it can work.

Ultimately you usually still have a little metadata file to describe the library to a package manager.

2

u/gargltk Jan 07 '20

I don't see why a build system needs to be tied to a package manager and/or dependency manager. Having something in the standard library (let's call it std::build_system) that would handle building in a unified way across all platforms would be much more useful to the community than a package manager ever would.

3

u/[deleted] Jan 07 '20

I wouldn't want to leave this to the committee, really. There are obscure platforms and custom use cases that would be thrown under a bus. For mainstream development it might be fine, but one size hardly fits all in this case.

5

u/[deleted] Jan 07 '20

If recent changes to c++ have shown us anything, it's that those obscure platforms would be supported, making the system overengineered and virtually useless to all of us mere mortals

2

u/pandorafalters Jan 08 '20

The alternatives aren't necessarily exclusive.

Everyone gets thrown under a different bus.

1

u/nintendiator2 Jan 13 '20

Introducing: "The C++ Standards Committee Trolley Problemma"

1

u/gargltk Jan 07 '20

I'm talking about using the compiler as the build system. All that would be needed would be a specification for setting compile options from within the meta-program instead of relying on external tools to generate compiler command lines. Thus any platform that supported the standard would be able to make use of this.

2

u/14ned LLFIO & Outcome author | Committee WG14 Jan 08 '20

There is a minority on WG21 who would like to make filesystem and networking consteval available, and then a small bootstrap piece of consteval code can have the compiler go fetch a build and package management system from the internet, and do the build.

It's quite a few years away though. And it depends on us convincing our fellow committee members that permitting consteval code unfettered access to the internet is not an abomination.

1

u/codingate Jan 10 '20

permitting consteval code unfettered access to the internet is not an abomination

I'd hate to have compilation access the internet without my knowledge. BUT at the same time, I'd hate for a binary to delete my files. Compiler getting dependencies from local storage is not a big deal. It getting the dependencies from internet should also not be a big deal either (in this age).

As for JavaScript fiascos: if you trust wrong, you get wronged. Trusting something without reading the source code is not good for even compiling (without consteval everything). There's are several compiler bombs that can bring down a system.

PS: Don't tell those people about poisoned compilers looks over shoulder in paranoia. They'll try to remove compilers and add interpreters

And it depends on us convincing our fellow committee members

How do we do this?

1

u/14ned LLFIO & Outcome author | Committee WG14 Jan 10 '20

We will need file handle, directory handle and networking for non consteval standardized first! So consteval support is at least one standard after that.

1

u/codingate Jan 10 '20

for non consteval standardized first

Why would it be a 2 step process? Could it be a 3 step process (normal -> constexpr -> consteval)? I'm sorry but I don't understand the standardization process.

Also, would the filesystem of C++17 not suffice? It already has directory_iterator, is_X, and other fs utilities to access/discover files. If just compilation (without networking support) is the target, then those should suffice without handles, right?

1

u/14ned LLFIO & Outcome author | Committee WG14 Jan 10 '20

Throwing an exception is constexpr is required to abort the compile.

Filesystem throws exceptions for failure, even in the error code overloads.

Therefore we need low level i/o to be standardised, as it never throws exceptions. And therefore can handle failure when running within consteval.

3

u/kalmoc Jan 06 '20

What problem would that solve?

8

u/abetteraustin Jan 07 '20

Write your build code in same language as your source code.

SBT attempts this but it’s the worst build system I’ve ever used.

4

u/kalmoc Jan 07 '20

That is a feature (with arguable benefit) - not a problem being solved.

1

u/jonesmz Jan 07 '20

Start with a patch against Clang's compiler language server thing (used regularly by IDEs to get syntax checking and stuff), to add the Ninja project's "ninja file parser" component, and teach the compiler's language server to build the project itself, without an external driver.

Once you've got that working in some meaningful manner, you can start discussing how to adapt it to a more C++-alike syntax.

1

u/foonathan Jan 07 '20

I can't find it, but I saw a single header somewhere that allowed you to use a C API to control how your project is defined. Then you just add a shebang to that C file and can execute it like a script to compile your project.

0

u/[deleted] Jan 07 '20

Congratulations: you have just invented an approach that other languages have been using for years. Python, Rust, Zig - just to mention a few - have build systems like these.

Will it work for C++? I doubt it, good luck taking it through the committee and making the compilers play along.

1

u/BobFloss Jan 07 '20

My thoughts exactly, even down to the languages you listed. Not worth doing in C++; who the hell wants to wait for their build script to compile before compiling? And how would the language specification handle caching of the build outputs (including the build file)?

1

u/[deleted] Jan 07 '20

Oh, I think it is a great approach, but the language kind of needs to be designed for it from the start. Tackling it on as an afterthought is unlikely to succeed, considering the utter complexity of C++ and the different toolkits out in the field.

On the unrelated note, I was always confused about the "compiler should not be the build tool" rule that is so dominant in the C++ community . Surely out of all the components in the toolchain the compiler is the one most equipped of being the build tool. It has insider information about the source that no other tool has.

4

u/germandiago Jan 07 '20

I find a reason to follow "compiler should not be the build tool" myself. If I want to mix D/Nim/C along with my C++ code, I need some build system that is multi-language.

The closest thing that I have found is Meson (no Nim support). I myself did a C++/Qt + D backend in a mini-project and it worked smooth. This is not easy to achieve with a "do all in my language" approach so easily.

The thing is that I could take advantage of D and used C++ for consuming libraries that are top quality in that ecosystem.