Repository: mesonbuild/meson

Issue:
Automatically create build directory if it does not exist
Currently, invoking meson in a project root folder with a non created build folder leads to:

```
% meson build
Error during basic setup:

[Errno 2] No such file or directory: '/some/path/build'
```

Since meson can find that the current folder contains a `meson.build` file, and that it probably also knows that `build` is my expected build directory. Should not it create it itself? 
I personally don't see any value invoking `mkdir build && meson build` while `meson build` could do the trick. I can submit a PR for that if wished but I'd like to know your opinion on that first.

(in a second time I would also love to have some `meson --build` argument which would invoke backend automatically, so that you finally simply type `meson --build <buildfolder>`; bypassing always-the-same `mkdir` and `cd` commands). 



Discussion:
I can see why this would be useful but on the other hand our command line/path detector already does a fair bit of magic and we don't want to end up like Git, which has a gazillion options for absolutely everything which is very confusing for everyone.

Yeah, I'm fine with no `--build` option (I'll do an alias anyway), but concerning the create directory issue I think it is _standard_ to expect `meson` to build it instead of to warn that is does not exist.

I have been paying attention this for a while and at least for me I have fatfingered this several times (usually by typing `meson typodi<tab> + enter`. In this case you _don't_ want the build system to create the directory but instead error out.

What's the harm if meson does create an incorrectly-named build directory though? The fix is a trivial `mv` command, and people usually just want any unused directory to be used for the configuration.

I literally just keep this mess in my shell history and keep calling it `rm -r meson-build; and mkdir meson-build; and meson meson-build`

I would _love_ if I could do that in a single command like `meson --force-clean meson-build`

Why do you need to do this so often? Meson should always be able to reconfigure itself just by running `ninja`. Every now and then nuking the build dir is fine but if you are doing it often, then that is probably the symptom of a bug.

This is necessary when you do Meson development or if you're changing environment outside of what Ninja keeps track of; specifically things on the system that affect the configuration step.

I do: `mkdir -p build && rm -rf build/* && meson build && ninja-build -C build`

It's particularly annoying on Windows when I want to test inside `cmd.exe`.

Yea I was doing it because I was hacking on Meson itself.

There's also the case of switching between branches with the `build` directory inside the top `srcdir`.

An interesting side effect of adding a `--builddir` argument would be that it allows things like:

```
$ cd ~/sources/project
$ meson --builddir ~/builds/project
$ ninja -C ~/builds/project test
```

which allows keeping the sources tree completely pristine.

You can already do that:

```
$ cd ~/sources/project && mkdir -p ~/builds/project
$ meson ~/builds/project
$ ninja -C ~/builds/project test
```

The `test` target will build all its dependencies before running the tests. However, implicitly creating the builddir would indeed make it less rote work, and less rote work = more productivity. ;)


Resolving patch:
diff --git a/mesonbuild/mesonmain.py b/mesonbuild/mesonmain.py
--- a/mesonbuild/mesonmain.py
+++ b/mesonbuild/mesonmain.py
@@ -89,6 +89,10 @@ def has_build_file(self, dirname):
     def validate_core_dirs(self, dir1, dir2):
         ndir1 = os.path.abspath(dir1)
         ndir2 = os.path.abspath(dir2)
+        if not os.path.exists(ndir1):
+            os.makedirs(ndir1)
+        if not os.path.exists(ndir2):
+            os.makedirs(ndir2)
         if not stat.S_ISDIR(os.stat(ndir1).st_mode):
             raise RuntimeError('%s is not a directory' % dir1)
         if not stat.S_ISDIR(os.stat(ndir2).st_mode):


Test patch: