Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing CMake build directory after running, within CMakeLists.txt

Tags:

cmake

Is there anyway of changing the CMake build directory from CMakeLists.txt file? I was looking at the CMAKE_BINARY_DIR variable but it seems to be unchangeable. The reason is that my project can be compiled for different platforms and I want for the user to define one build directory and the CMake will decide on the build location according to the platform: if (x86): CMAKE_BINARY_DIR = CMAKE_BINARY_DIR/x86 if (x64): CMAKE_BINARY_DIR = CMAKE_BINARY_DIR/x64

In short, to dismiss the user from configuring a build directory for each of the platform he wants to compile the project for.

Thanks, Aner

like image 383
anerwolf Avatar asked Nov 26 '17 09:11

anerwolf


1 Answers

Don't do this.

In CMake, it is up to the user to specify which binary directory they want to use for building. This is done at the initial configure run of CMake. For instance, the command line tool will use the current working directory as the root build directory. Changing this directory requires you to re-perform the configure step from scratch. It is not desirable to attempt to change the binary directory in any other way.

What you can do is change the output directory for individual build artifacts. For instance, you could have all libraries be placed in lib/x86 for 32 bit builds and in lib/x64 for 64 bit builds (through the LIBRARY_OUTPUT_DIRECTORY target property). This will not solve your problem here though.

Changing architectures still requires you to re-run the CMake configuration from scratch, which you should always do into a separate directory. You should avoid re-configuring into a dirty binary directory, as this can break in subtle ways and is just asking for trouble in the long run.

So your directory structure would look something like this:

<build_32>
 +-lib
  +-x86
<build_64>
 +-lib
  +-x64

Where <build_32> and <build_64> are folders chosen by the user. If you instead want a single directory tree like this:

<prefix>
 +-lib
  +-x86
  +-x64

The cleanest way to get this is through installation. From the two separate build directories above, the user can perform an installation to the same install prefix to retrieve a directory tree like the one shown above. While you still must take care that you don't accidentally overwrite vital build artifacts from the other configuration during installation, managing this is usually much simpler than ensuring the same for sharing build directories.

like image 94
ComicSansMS Avatar answered Dec 13 '22 01:12

ComicSansMS