I have been working on LLVM, which uses CMake as a build system. I can create an out-of-source build as follows:
mkdir build
cd build
cmake ..
make
This works reasonably well when I am working on one branch, since Make can reuse build artifacts.
However, when I switch branches, a CMakeLists.txt file may have changed. To be sure I am running the corresponding binary of my current source-code, I run:
rm -rf build
mkdir build
cd build
cmake ..
make
This is inefficient, because I loose all build artifacts. Ideally, CMake would only rebuild what has changed.
How can I achieve incremental builds using CMake whilst switching branches and also be guaranteed build correctness?
This is how I use it and I am happy with it (for simplicity assume that I have 1 CMakeLists.txt) in entire repo:
/
/src
/src/repo1 (on branch1)
/build
/build/repo1_branch1
/build/repo1_branch2
so when I want to generate cmake files from branch1
cd /src/repo1
git checkout branch1
cd /build/repo1_branch1
cmake /src/repo1
for the other branch I'd simple replace two middle lines...
In addition you can make more folders for Release and Debug configurations etc.
I realize that this way is not ideal but it's clear consistent and could be automatized. So script would get repo name (root folder name), branch name, create a folder in fixed location with name repo_name+branch_name (like /build, or ok /home/user/builds) go there and run cmake.
Related
I have a project that needs to be built like this:
./meson.py build
./ninja -C build install
This works well. The only thing is: the binaries are stored in (on Linux): /usr/local/bin. This would require me to input root password because the binaries are being written to a root-access folder, aka /usr/local/bin.
Is there a way to install the binaries in some folder in the /user/home directory, so that no passwords are required?
The thing is that everytime I debug and change something, the rebuilding process forces the binaries to be rewritten, which asks for password everytime.
This is what I tried:
Create a folder in home: mkdir ~/projectbin
Use ninja with --prefix option: ./ninja -C --prefix=~/projectbin install
This throws an error of unrecognized option --prefix.
I am new to ninja and meson, please let me know how to resolve this.
The way to pass an option to Meson is using the -D option. So to set the prefix, you should use meson -Dprefix=$HOME/projectbin build.
Note that you set this at configure time (ie when calling meson), not at build time (when calling ninja).
What is exactly the command line (process and arguments) used by CLion when invoking CMake? I'm trying to use the same directory for manual builds using the terminal and for building using the IDE, but it seems that one is interacting badly with the other.
I have no problem with using CLion only to handle CMake configurations (to avoid slight configuration mismatch triggering another CMake execution), but it seems that even standard builds using make on the command line trigger cmake again.
I've seen that CLion prints it's "call" to CMake, but I don't see where it references the current working directory. And since on the GUI you configure paths relative to the project root folder (where CMakeLists.txt live), instead of relative to the build folder. I was hoping that this detail is the culprit here.
Usually in the command line I'd do it like this:
$ cd project
$ mkdir -p builds/debug
$ cd builds/debug
$ cmake $MY_CMAKE_OPTS -DSPECIAL_FILE=../../file.ext ../..
On CLion, though, I have to configure it like this:
CMake options: $MY_CMAKE_OPTS -DSPECIAL_FILE=file.ext
Generation path: builds/debug
The rest I've used the default
This special file is used on the configuration phase, so using paths other than relative to project root or absolute paths won't work.
Configuration step command line is shown in CMake view when you load/reload a CMake project: View - Tool Windows - CMake. The view has no default hotkey.
Example: /Users/vic/bin/cmake_ninja_wrapper.py -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" /Users/user/src/helloworld.
Depending on configuration, the current directory can be PROJECT_SOURCE_DIR/cmake-build-debug (where the build files were generated for me), PROJECT_SOURCE_DIR/cmake-build-release, or other.
Build step command line is shown in Messages - Build view. It opens when you invoke build from Build menu. I don't think the current directory matters for it, as all the build files are already generated.
Example: /Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/user/src/helloworld/cmake-build-debug --target helloworld -- -j 6
Then the view can be opened with Cmd-0 on Mac, or through menu: View -
Tool Windows - Messages.
To work with relative paths, you can refer to PROJECT_SOURCE_DIR variable in your CMakeLists.txt.
I am developing a C++ library and I use a Makefile to compile and create a build. I wish to maintain multiple builds for different gcc compiler versions. What is the best way to do this?
If you are ready to go with autotools, autoconf/automake let you do this through out of tree builds. You can configure as many build you want if you separate each build into its own directory (not the source directory).
Configuring compiler and compiler options is usually a simple matter of bash command line.
mkdir mingw-build
cd mingw-build
CXX=mingw32-g++ ../myproject/configure ...
cd ..
mkdir native-build
cd native-build
CXX=g++ ../myproject/configure ...
cd ..
I got the Box2D project source and want to compile the testbed portion of it.
The project folder contains folders like: freeglu glui testbed(a demo) helloword(a demo)
Box2D Build CMakeFiles
There are many CMakeLists.txt in all the different folders.
I was thinking that I should cmake all those files so that make files are created in all places required.
I read this (as instructions to do do want I want) :
wget http://box2d.googlecode.com/files/Box2D_v2.2.1.zip
unzip Box2D_v2.2.1.zip
cd Box2D_v2.2.1/Build
cmake ..
make
What does the cmake .. do?
There is no CMakeLists.txt in the build folder.
cmake is a Makefile generator.
When you call cmake [path], you ask it to generate a Makefile in the current directory following instructions given in [path]/CMakeLists.txt
Usually cmake output some messages while it is working, and after it is done without errors, you can type "make" to execute your newly created Makefile.
CMakeLists.txt files can reference other CMakeLists.txt file in sub-directories, so you are usually only interested by the CMakeLists.txt of the top directory, not the other ones.
Using an empty "build" directory is a technique called "out-of-source build", in which all your generated files (.o, executable, Makefile, .anything) are generated in the separate "build" directory and not mixed with source files. If you want to clean all, you can delete all the content of the build directory.
In fact, you can put your "build" directory in any place, as long as you give cmake the correct path of the top CMakeLists.txt. You can even have several build directories. It is very useful if you need several different builds at the same time (with different options, different versions of gcc, etc.)
In old programs, you generate the Makefile too, but using ./configure (this is called auto-tools. You may have encountered that already). cmake is considered a successor of the auto-tools.
cmake .. generates makefiles in the current directory, using ../CMakeLists.txt file as starting point. make command, executed after this, builds the program, using generated makefile(s) as an input. This is convenient to keep a source code and build results in different folders. General syntax is: cmake source-dir (of course, there are a lot of other switches).
Well, .. is shorthand for the parent folder, so it will presumably act upon whatever it finds in Box2D_v2.2.1.
I am pretty new to cmake and wonder how I can do this is cmake. I want to place all the intermediate files (like the .o files) to be placed in a certain directory (say "build") and then once the build is done I want copy certain files I need (e.g., the .exe, .dll like the end product) from build directory to "stage" directory. how can I specify the path to these two directories (build and stage)? Cmake will also check if the build/stage directory exists or not. If does not exist, it will create the directories for me.
Any help will be appreciated.
What are you asking is the most often CMake use case.
To make whole build process to occur in arbitary dir, you should run cmake /path/to/your/project from that dir (or use cmake-gui).
Your source dir would be untouched until you explicitly tell CMake to output some files there.
As for point 2:
You should place install() invocations into your CMakeLists.txt (see documentation on install()) and set CMAKE_INSTALL_PREFIX to the dir where you wish files to be copied. After that you can run make install or cmake -P cmake_install.cmake from your build dir to install these files.
I would suggest another approach.
make out-of-source builds see here, so that all your build stuff is in an own folder
copy your executables from your build directory in an own folder with explicit copying see here
Or instead of choosing step 2 you also can provide an install routine where the needed executables are installed in a give path. see here