Creating an archive with CMake at a location that I choose - shell

I'm trying to learn how to create archives with CMake. I wrote this piece of code:
cmake_minimum_required(VERSION 3.5.1)
project(hello)
message("Creating archieve in: " ${CMAKE_CURRENT_BINARY_DIR})
message("Source dir is: " ${CMAKE_CURRENT_SOURCE_DIR})
add_executable(hello main.cpp)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_custom_target(create_tar ALL COMMAND
${CMAKE_COMMAND} -E tar "cfvz" "archieve.tgz")
add_dependencies(create_tar hello)
My project structure is like this:
I have a main folder in which I have a CMakeLists.txt, a main.cpp and a build folder. I go into build and run the cmake file above by 'cmake ..' and then make. My archive is created in the build folder but as you can see I have specified that I want it in the CMAKE_CURRENT_SOURCE_DIR which is along with the main, cmakelists and build folder not in the actual build folder.
Please explain to me why is that happening and how can I make the archive be created in the CMAKE_CURRENT_SOURCE_DIR and not CMAKE_CURRENT_BINARY_DIR. Thanks.

Turning my comment into an answer
That would be WORKING_DIRECTORY parameter of the add_custom_target() command:
add_custom_target(
create_tar ALL
COMMAND ${CMAKE_COMMAND} -E tar cfvz "archieve.tgz"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
The CMAKE_ARCHIVE_OUTPUT_DIRECTORY is for changing the output directory of add_library(... STATIC ...)` targets.
And you can either run the cmake -E tar either the correct (sub-)directory by changing the WORKING_DIRECTORY accordingly (like "${CMAKE_CURRENT_SOURCE_DIR}/Dir") or give a list of files after -- command line option (see documentation of CMake Command Line Tool Mode).

Related

CMake include resources in build [duplicate]

I've got some config files (xml, ini, ...) in the config directory next to the source files. How can I copy all the files in the config directory into the build directory (next to the executable file) each time I make the project?
You can use add_custom_command.
Say your target is called MyTarget, then you can do this:
add_custom_command(TARGET MyTarget PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/config/ $<TARGET_FILE_DIR:MyTarget>)
This executes every time you build MyTarget and copies the contents of "/config" into the directory where the target exe/lib will end up.
As Mark Lakata points out in a comment below, replacing PRE_BUILD with POST_BUILD in the add_custom_command ensures that copying will only happen if the build succeeds.
Explanation
${CMAKE_COMMAND} is the path to CMake
-E makes CMake run commands instead of building
copy_directory is a Command-Line Tool
config is the directory (that falls under the root of the project) whose contents will be copied into the build target
$<TARGET_FILE_DIR:MyTarget> is a generator expression, described in the add_custom_command documentation.
In addition to the top answer,
To copy the directory itself instead of the contents, you can add /${FOLDER_NAME} to the end of the second parameter.
Like this:
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/config $<TARGET_FILE_DIR:${PROJECT_NAME}>/config)
CMake supports a shell type file copy. This link should be helpful for you - How to copy directory from source tree to binary tree?
Use symbolic links
CMake enables symbolic links via create_symlink:
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink
${CMAKE_SOURCE_DIR}/config $<TARGET_FILE_DIR:${PROJECT_NAME}>/config)
It ensures that when you make a change to the files in the directory, build folder would subsequently be updated.
In my project i use INSTALL to specify in CMake, what and where i move my binary with conf file. After execution of cmake, use "make install".

Error fatal - No such file or directory

I have installed the cds library with command ./build.sh -b 64 -z '-std=c++0x' -l '-L /usr/lib/x86_64-linux-gnu' --with-boost /usr/include/boost --amd64-use-128bit at build folder.
After I tried to compile the example init.cpp of src folder, I typed this in terminal: g++ init.cpp -o init, and terminal showed: fatal error: cds/init.h: No such file or directory.
What should I do for compilation command in this case?
Thanks.
For general troubleshooting in cases like this, i would recommend finding where on the system the file got installed (if your build.sh actually installed the file). You would be able to find the missing header file using
find / -path '*/cds/init.h' 2>/dev/null
Then you need to supply two parameters to g++:
First one gets the compiler to know about the include files from the install directory
-I path_to_folder_one_step_above_cds_folder
Second one gets the linker to know about the librarys location. If the library file is called libcds.so, you can find it by running
find / -name libcds.so 2>/dev/null
So for linking, you supply the flag
-L path_to_folder_one_step_above_libcds.so
In your case you might not need the -L flag, since most of your library supposedly is header only.
UPDATE: the build.sh script is printing out important information at the top, starting with "Building with the following options:". The important bits will be "Compile options:" and "Link options:". Those should be enough to solve your specific option.
UPDATE2: build.sh also exports some flags which might include more options. You can print them out directly after running build.sh by running
echo LDFLAGS=$LDFLAGS
echo CFLAGS=$CFLAGS
echo CXXFLAGS=$CXXFLAGS
you are likely to need to pass all these options to g++ when compiling and linking against that library. LDFLAGS are specific to the linker only. Both the other ones are needed for compiling c++ files.

Running a bash command via CMake

I'm trying to have CMake either run three bash commands or a bash script. However, I can't seem to get it to work.
The bash commands are:
cd ${CMAKE_SOURCE_DIR}/dependencies/library
make
cd ${CMAKE_BINARY_DIR}
Essentially, I would like CMake to build the library in that directory if it does not already exist.
Here's the CMake code I tried:
if(NOT "${CMAKE_SOURCE_DIR}/dependencies/library/lib.o")
execute_process(COMMAND cd ${CMAKE_SOURCE_DIR}/dependencies/library)
execute_process(COMMAND make)
execute_process(COMMAND cd ${CMAKE_BINARY_DIR})
endif(NOT "${CMAKE_SOURCE_DIR}/dependencies/library/lib.o")
However, it's not building anything. What am I doing wrong?
Also, while I'm here asking this: should the third command, to move to the binary folder, be included?
Thanks!
execute_process() is executed during configure time. But you want this to run at build time, thus add_custom_command() and add_custom_target() is what you're looking for.
In this special case you want to generate an output file, so you should go for add_custom_command() (both are essentially the same, but command produces one or multiple output files, while target does not.
The cmake snippet for this should look something like the following:
add_custom_command(
OUTPUT ${CMAKE_SOURCE_DIR}/dependencies/library/lib.o
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/dependencies/library
COMMAND make
)
You then have to add the output file in another target as dependency, and everything should (hopefully) work as expected.
You can also add DEPENDS statements to the add_custom_command() call to rebuild the object file in case some input sources have changed.

Prevent running cmake in the root directory

Sometimes I forget I'm in the build folder and I run the command in the root directory. Well, maybe some of you have experienced that this creates a mess in the entire file hierarchy, as you have to delete CMakeFiles/ folder, config files, and files related to CPack and CTest. Is there a way I can add something to the Makefile at the root directory that prevents me from running cmake accidentally? I tried to add a target 'cmake' but this didn't work.
aa
UPDATE
I found the same question posted in here. I ended up adopting to put a function in my .bashrc file as suggested in that page:
function cmake() {
# Don't invoke cmake from the top-of-tree
if [ -e "CMakeLists.txt" ]
then
echo "CMakeLists.txt file present, cowardly refusing to invoke cmake..."
else
/usr/bin/cmake $*
fi
}
You can check in your CMakeLists.txt if the source and binary directories are the same. Put something like this as the very first thing in your CMakeLists.txt:
if (CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR)
message(FATAL_ERROR "Source and build directories cannot be the same.")
endif()

How can CMake be used to generate Makefiles with personalized commands?

I like to keep my Makefiles flexible and multifunctional. One of the tasks I usually add to make command is tar, for example the following instruction does the job:
tar:
tar -cvf $(PROGNAME).tar $(SRCS) Makefile
My question is: How can CMake be used to generate personalized commands like tar?
I would like to see some code samples.
For the full functionality it would be useful to create project's components and be able to use them as parameters.
(Exempli gratia: archive only header files or some specific library).
Thanks in advance for your answers!
The literal translation of your tar example would be:
ADD_CUSTOM_TARGET(tar
tar -cvf ${CMAKE_CURRENT_BINARY_DIR}/${PROGNAME}.tar ${SRCS} Makefile
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
This adds a new target "tar" that always executes the given command whenever it is requested as a command line target, i.e. whenever you run make tar it will create a new tar file. The WORKING_DIRECTORY argument will ensure that the source files are taken from the source directory, while CMAKE_CURRENT_BINARY_DIR ensures the output goes in the current build directory.
A slightly better iteration would be to replace tar with ${CMAKE_COMMAND} -E tar, as this doesn't depend on the command line tar program being available. So something like this would tar up all the header files when you run make tar:
SET(HEADER_FILES my.h another.h)
SET(PROGNAME myprog)
ADD_CUSTOM_TARGET(tar ${CMAKE_COMMAND} -E tar -czvf
${CMAKE_CURRENT_BINARY_DIR}/${PROGNAME}.tar.gz ${HEADER_FILES}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
An even better iteration would be to use the CPack features to create source or binary tar files, but that's quite a bit more work and may not be what you need anyway.

Resources