cmake - How to change compile_commands.json output location - cmake

Here's an example C++ project structure:
- {root}
- CMakeLists.txt
- build/
- compile_commands.json <-- This file gets generated
- src/
- CMakeLists.txt
- files here
- compile_commands.json <-- This is where I want compile_commands.json to be built to
If I build the project, it creates a "compile_commands.json" file in the "build" folder. But I actually want it underneath "{root}". Is there a way to specify the location of compile_commands.json? Or do I have to just copy it manually?
These are the command(s) that I typically use to build a project
(If cd'ed into {root})
cmake . -B build
alternative:
cd {root}/build && cmake ..
In both cases, compile_commands.json is added to the build folder

Is there a way to specify the location of compile_commands.json?
There is no way. I believe the path for compile_commands.json generated when CMAKE_EXPORT_COMPILE_COMMANDS is set is hardcoded to be inside CMAKE_BINARY_DIR.
Just create a symlink compile_commands.json -> ./build/compile_commands.json.

Related

How to create a custom target that copies a library?

I am trying to create a custom target that just copies a library to the LIBRARY_OUTPUT_PATH. Below is my CMakeLists.txt.
The contents of the MYCOMPONENT folder is mylib.so and CMakeLists.txt.
cmake_minimum_required(VERSION 2.8)
project( MYCOMPONENT )
add_custom_target( MYTARGET
COMMAND ${CMAKE_COMMAND} -E copy ./mylib.so ${LIBRARY_OUTPUT_PATH} )
I do the following:
cd MYCOMPFOLDER
mkdir build_debug
cd build_debug
cmake -DLIBRARY_OUTPUT_PATH=<mycompfolderfullpath>/build_debug/bin ..
Now I do an ls of build_debug and I get:
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
ls CMakeFiles/
3.5.1 CMakeTmp Makefile2
cmake.check_cache feature_tests.bin Makefile.cmake
CMakeDirectoryInformation.cmake feature_tests.c progress.marks
CMakeOutput.log feature_tests.cxx TargetDirectories.txt
CMakeRuleHashes.txt MYCOMPONENT.dir
ls CMakeFiles/MYCOMPONENT.dir/
build.make cmake_clean.cmake DependInfo.cmake progress.make
From build_debug, "find . -name mylib.so" is nowhere, so of course the "make MYTARGET" fails. How do I get cmake to properly handle mylib.so? This is a third party library, we don't build it, the target is to make sure it gets copied to the LIBRARY_OUTPUT_PATH from this folder so other components can link against it from there.
Don't use relative paths in -E copy part. Variables CMAKE_SOURCE_DIR, CMAKE_CURRENT_SOURCE_DIR, CMAKE_BINARY_DIR and CMAKE_CURRENT_BINARY_DIR are at your disposal.

cmake, how to specify the output directory

I'm using cmake to generate a VS2017 solution (and projects...), I try to generate everything in a different folder.
I used both the command line and different variables, but no way, it generate in the "source" folder !
Here are some examples of what I tried:
cd source
cmake -B../build ...
cd build
cmake ../source
cmake --build "../build"
cmake -Dxxx=../build
Any idea ? all theses solutions are expected to generate in the build folder !
Once you have performed in-source build (in source directory), it is impossible to build the project out-of-source: every such attempt will modify in-source build.
You need to clear build files (CMakeCache.txt, probably some other ones) in source directory before using out-of-source builds.
I fixed with:
cd VSBuild
cmake ../
instead of
cd VSBuild
cmake ..
Have you tried this? This will put all the build files under "out"
$ cmake -H. -Bout
To execute:
$ cmake --build out

Wrong CMAKE_BINARY_DIR?

I have the following trivial CMakeLists.txt (FreeBSD 9.1, CMake 2.8.11.2):
project(temp_proj CXX)
message(${CMAKE_BINARY_DIR})
message(${CMAKE_SOURCE_DIR})
This file is located in /root/trunk/temp. Since I want to perform an out-of-source build, I create a sibling directory temp2 and invoke cmake from there:
root#:/root/trunk # mkdir temp2
root#:/root/trunk # cd temp2
root#:/root/trunk/temp2 # cmake ../temp
/root/trunk/temp
/root/trunk/temp
-- Configuring done
-- Generating done
-- Build files have been written to: /root/trunk/temp
How comes CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR are the same?
What am I doing wrong?
As #Fraser suggested, the issue occurred because of CMakeCache.txt file, which was left in /root/trunk/temp.

How do I change the directory to which CMake outputs solution and project files?

I have a build/ directory where I'd like CMake to put the *.sln, *.proj, etc. files it generates when I type cmake CMakeLists.txt. How do I do this?
You run cmake in the directory you want to build in with the path from the build directory to the source directory. So say your parent directory is called project and it contains src and build you would:
cd build
cmake ../src
That will put the makefiles and the objects in build while leaving the CMakeLists.txt files in src. The one pitfall is that if there is already a CMakeCache.txt in src then you must delete before running cmake.

CMake with "standard" directory layout (Linux)

Let's say I have a simple hello project with the pseudo-standard directory layout
helloworld/
src/
main.c
say.c
say-helper.c
include/
say.h
say-helper.h
build/
and after running
cd ~/helloworld/build
cmake ..
make
I would expect the following
helloworld/
build/lib/
libsay.a
libsay.so
libsay.so.1.0.0
tmp/obj/
main.o
say.o
build/bin/
hello
and after make install I would expect
/usr/local/lib/
libsay.a
libsay.so
libsay.so.1.0.0
/usr/local/bin/
hello
What would the CMakeLists.txt look like for this setup?
I've been looking around for examples, but the only one I've found that shows how to add a library and an executable didn't work.
Basic commands to describe the project:
INCLUDE_DIRECTORIES(include)
ADD_LIBRARY(say src/say.c src/say-helper.c)
ADD_EXECUTABLE(hello src/main.c)
TARGET_LINK_LIBRARIES(hello say)
This is for placing the libs and the executable in the build directory, put that in your CMakeLists.txt:
SET (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
SET (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
SET (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
For install you specify
install(TARGETS say hello
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
in your CMakeLists.txt and set CMAKE_INSTALL_PREFIX to /usr/local in your configuration.
I'm not sure if you can build static and dynamic libraries simultaneously with the same name, though. And I don't know how to tell CMake to put the obj files in some specific location.