Compiling software in a restricted environment requires changing essential environment variables and finding a place where to install the final product. This is a tedious task that can be mitigated by a simple shell script that sets up the appropriate directory and overrides the path variables:
$ cenv foo $ (foo) ./configure --prefix=$CENV && make && make install
Because this script only knows about a limited set of environment variables, you can add more by running
$ cenv --edit foo
This will open a local file that is sourced right before starting the new subshell and in which you can define more variables to be exported.
To make it work even more like a Python virtualenv, you could alias common build
tools in your
.bashrc to include the prefix implicitly:
alias cmake="cmake -DCMAKE_INSTALL_PREFIX=$CENV" alias configure="configure --prefix=$CENV"
This little script works for my use case but there are of course alternatives: lightweight containers (either raw or using Docker) or virtual machines being some of them.