Hi
This is the problem I'm facing, running on a VPS with 1GB of ram and 1GB of swap:

and it sticks everytime! When I refer to htop I see lots of memory and virtual memory is being used by gcc (though I use ccache). Making seems never end, however after a long time it crashes.

You need more RAM
https://github.com/tdlib/td/issues/6
But let's wait for the answers of professionals.
Yes, this will not be enough for gcc, but 2GB of RAM should be enough for clang.
Thanks for your help, but are you sure? Because I set up building project on a Ubuntu18.04 machine with 4GBs of RAM running via HyperV (Type1 you know) on my own PC, and it's still draining all of RAM I assigned demands much more. Without making swap file available to VM it will be a nightmare and VM becomes completely unresponsive. Honestly, I think it's better you optimize cmake process, draining so much RAM is weird.
I hotplugged more 2GB of RAM and it's going a little bit smoother.
Assuming we use tdlib inside a telegram bot project which should be running in a VPS, 6GB of RAM is much expensive while running a telegram bot is possible even with 1GB of RAM (on top of tdlib ofcourse).
It's unfair, to get a 6GB RAM VPS to only compile tdlib and then use 1~2GB of it.
@HellScre4m It isn't a cmake fault. The problem is in gcc, which needs more than 4GB of RAM to compile some source files.
As I've said you can use clang if you have less than 2 GB of RAM, but you don't really need it. You can compile the library once on a server with enough RAM and then use it on any compatible server, even it has only a few dozen megabytes of RAM.
Clang definitely likely will not help. Everything will hang with him.
You can automate the assembly on a computer with 8GB RAM. Then automate sending to VPS with 1GB RAM or less. By the way, try on 1Megabyte RAM on VPS (I mean launching the assembled library. Interesting to see the result)
It would also be interesting to see the script for sending tdlib to VPS.
I'm using ssd and 4gb ram on vps
TDLib 1.4.0 includes a lot of compilation memory usage optimizations and a new script SplitSource.php, which can be used to split some source code files before building the library to reduce maximum RAM usage per file at the expense of increased build time. In our tests clang 6.0 with libc++ required less than 500 MB of RAM per file and GCC 4.9/6.3 used less than 1 GB of RAM per file.