CMAKE install command post process the file - linux

I am installing static file using CMake's INSTALL command.
I want to post process the output file using CMake.
For example, static files have a string like
v={{VERSION}}
I want to replace {{VERSION}} in the output files.
Is this possible with CMake?

Yes, but preprocessing is usually done at cmake invocation step, not during install. This is done using configure_file() command.
Note that configure_file() supports substituting values only in ${} or ##, so if you really need to configure a file with {{}}, you might end up writing your own function using the CMake command file().
Finally, you need to install your configured file. Be sure to
install(FILES ${CMAKE_BINARY_DIR}/your.file)
and not just
install(FILES your.file)
since the latter command would install your source file.

Related

Running cscope from CMake at build time

I'm using Ubuntu Linux.
I've been trying to get the following cscope command to run when I run "make" from my project directory, so it recompiles cscope and gets updated name information when I make my project.
cscope -b -q -U -R
Per my research and a bit of reading, I should be able to get CMake to run a command when you do 'make' by using the add_custom_command function in CMakeLists.txt.
However, many attempts and variations of it, have not been successful. Is it possible to run this as I want it with add_custom_command?
Simply doing this doesn't seem to work:
add_custom_command(OUTPUT
${CMAKE_CURRENT_BINARY_DIR}/cscope.in.out
${CMAKE_CURRENT_BINARY_DIR}/cscope.out
${CMAKE_CURRENT_BINARY_DIR}/cscope.po.out
COMMAND cscope -b -q -U -R)
I've tried using the TARGET overload of add_custom_command as well, and making a custom target with a dependency on either ALL or the main output file of the project, but that doesn't do anything either.
Ideally this would run after the project has been built, if could tell me what I'm doing wrong or if this is at all the way to do this, I'd be grateful?
I've tried using the TARGET overload of add_custom_command as well, and making a custom target with a dependency on either ALL or the main output file of the project, but that doesn't do anything either.
This seems to be the problem - when a CMake commands requires to be passed a target, they refer to the name of a target you've created previously by using any of add_executable, add_library or add_custom_target, which doesn't necessarily map to an actual artifact file generated by the command.
Here's my take on the problem, and it seems to generate the three cscope files in the build directory.
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(te)
add_executable(main main.cpp asdf.cpp)
add_custom_command(TARGET main POST_BUILD COMMAND cscope -b -q -U -R)
Note here that the name of the target here is whatever I've passed as the first argument to the add_executable command.

CMake: How to execute a command before make install?

This is the way I install the config files:
file(GLOB ConfigFiles ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.xml
${CMAKE_CURRENT_SOURCE_DIR}/configs/*.xsd
${CMAKE_CURRENT_SOURCE_DIR}/configs/*.conf)
install(FILES ${ConfigFiles} DESTINATION ${INSTDIR})
But I need to convert one of the xml files before installing it. There is an executable that can do this job for me:
./Convertor a.xml a-converted.xml
How can I automatically convert the xml file before installing it? It should be a custom command or target that installing depends on it, I just don't know how to make the install command depend on it though. Any advice would be appreciated!
Take a look at the SCRIPT version of install:
The SCRIPT and CODE signature:
install([[SCRIPT <file>] [CODE <code>]] [...])
The SCRIPT form will invoke the given CMake script files during
installation. If the script file name is a relative path it will be
interpreted with respect to the current source directory. The CODE
form will invoke the given CMake code during installation. Code is
specified as a single argument inside a double-quoted string.
For example:
install(CODE "execute_process(\"./Convertor a.xml a-converted.xml\")")
install(FILES a-converted.xml DESTINATION ${INSTDIR})
Be sure to checkout the entry for execute_process in the manual. Also be aware that macro expansion inside the CODE parameter can be a bit tricky to get right. Check the generated cmake_install.cmake in your build directory where the generated code will be placed.
I think that your specific case would work better if you were to use a custom command and target like so:
add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/a-converted.xml
COMMAND ./Convertor a.xml a-converted.xml
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/Convertor
)
add_custom_target(run ALL
DEPENDS ${CMAKE_BINARY_DIR}/a-converted.xml
COMMENT "Generating a-converted.xml" VERBATIM
)
install(
FILES ${CMAKE_BINARY_DIR}/a-converted.xml
DESTINATION ${INSTDIR}
)
Note: I don't have all the details, so the directories are probably
not exactly what you'd want in your environment, although it's
a good idea to generate files in the ${CMAKE_BINARY_DIR} area.
That way you can be sure that the file a-converted.xml is built at the time you want to install it. Especially, these two rules make sure that if you make changes to the file, it gets recompiled.

how to generate configure script -- linux

I have downloaded :--
libwebsockets-1.0-chrome25-firefox17
from ;--
http://git.warmcat.com/cgi-bin/cgit/libwebsockets/
I have to install websocket library. But it does not contains configure file.
As per install instruction :--
The file `configure.ac' (or `configure.in') is used to create
`configure' by a program called `autoconf'. You need `configure.ac' if
you want to change it or regenerate `configure' using a newer version
of `autoconf'.
Also it contains file :---
configure.ac
autogen.sh
Please suggest command to generate configure file, in linux ?
Just run the provided autogen.sh script.

Building library from source files in Unix-like OS

When building any library from sources files, is it possible to set the place where I want it to be installed? I have read about some prefix flag, but I was wondering if all aplications have that flag.
For instance, I was building OpenCV from sources and when I made make && make install the contents where installed in /usr/local/opencv but what If I want it to be installed in /other/place/opencv?
If I follow you correctly, that stuff is done during the configure phase with something like:
$ ./configure --prefix=/other/place/opencv
If the project doesn't have a configure script then it's a case of editing the Makefile directly.

Make install, but not to default directories?

I want to run 'make install' so I have everything I need, but I'd like it to install the things in their own folder as opposed to the system's /usr/bin etc. is that possible? even if it references tools in the /usr/bin etc.?
It depends on the package. If the Makefile is generated by GNU autotools (./configure) you can usually set the target location like so:
./configure --prefix=/somewhere/else/than/usr/local
If the Makefile is not generated by autotools, but distributed along with the software, simply open it up in an editor and change it. The install target directory is probably defined in a variable somewhere.
Since don't know which version of automake you can use DESTDIR environment variable.
See Makefile to be sure.
For example:
export DESTDIR="$HOME/Software/LocalInstall" && make -j4 install
make DESTDIR=./new/customized/path install
This quick command worked for me for opencv release 3.2.0 installation on Ubuntu 16. DESTDIR path can be relative as well as absolute.
Such redirection can also be useful in case user does not have admin privileges as long as DESTDIR location has right access for the user. e.g /home//
It could be dependent upon what is supported by the module you are trying to compile. If your makefile is generated by using autotools, use:
--prefix=<myinstalldir>
when running the ./configure
some packages allow you to also override when running:
make prefix=<myinstalldir>
however, if your not using ./configure, only way to know for sure is to open up the makefile and check. It should be one of the first few variables at the top.
If the package provides a Makefile.PL - one can use:
perl Makefile.PL PREFIX=/home/my/local/lib LIB=/home/my/local/lib
make
make test
make install
* further explanation: https://www.perlmonks.org/?node_id=564720
I tried the above solutions. None worked.
In the end I opened Makefile file and manually changed prefix path to desired installation path like below.
PREFIX ?= "installation path"
When I tried --prefix, "make" complained that there is not such command input. However, perhaps some packages accepts --prefix which is of course a cleaner solution.
try using INSTALL_ROOT.
make install INSTALL_ROOT=$INSTALL_DIRECTORY

Resources