How can I pass arguments to ranlib using cmake? - cmake

How can I pass an argument to ranlib when compiling a static library with CMake?
I tried:
set_target_properties(myLibrary STATIC_LIBRARY_FLAGS "--plugin /usr/lib/gcc/x86_64-linux-gnu/4.9/liblto_plugin.so")
and this worked for ar but not for the subsequent ranlib command.

Have you tried this?
SET(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
SET(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
On the Mac, this is how I pass the "-no_warning_for_no_symbols" flag to ranlib.
Note: The SET commands don't modify the ranlib command used as part of an installation by running "make install." CMake's installer code does not generate installation scripts that allow for options to be added to ranlib.

Adding
set_property(
TARGET myLibrary
APPEND
PROPERTY STATIC_LIBRARY_FLAGS "-no_warning_for_no_symbols"
)
worked for me.

Related

How to add and remove some flag to cmake in commandline?

In the command line ubuntu, I want to directly delete a series of flags contained in the source code of the HHVM when using cmake for install HHVM, if available, and add another set of flags to it.
What should I do about this?
please guide me.
for use linker -Wl,--emit-relocs should don't use -E , -s and -c.
example :
$ cmake + (-O3 -fno-reorder-blocks-and-partition -Wl,--emit-relocs)
and
$ cmake - (-E ,-s,-c)

How to use CMake cached variables inside subprocess called by custom target?

My project contains a custom target which generates some output via .cmake script. It looks like this:
add_custom_target(TargetName
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_SOURCE_DIR}/cmake/script.cmake
BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/generated/output
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
VERBATIM
)
But now I want to set come cache variables inside the script. I tried doing like that:
message("MY_CACHE_VARIABLE = ${MY_CACHE_VARIABLE}")
set(MY_CACHE_VARIABLE "VALUE" CACHE INTERNAL "")
And I faced with the problem that cache variables are not saved. It always prints me empty output:
MY_CACHE_VARIABLE =
I already tried setting working directory as CMAKE_BINARY_DIR, or passing CMAKE_BINARY_DIR of the last argument of cmake command, or passing -B ${CMAKE_BINARY_DIR} or -C ${CMAKE_BINARY_DIR}/CMakeCache.txt as arguments and etc. None of these worked.
So is there any way to reuse existing cache inside CMake subprocess or I just should write my own cache inside the script?
You have to distinguish between running CMake to generate build files (for Make, Ninja, etc.) and running CMake in script mode:
Script mode simply runs the commands in the given CMake Language source file and does not generate a build system. It does not allow CMake commands that define build targets or actions.
-- cmake-language(7)
No configure or generate step is performed and the cache is not modified.
-- cmake(1)
So in script mode (-P), CMake is not aware of the cache or any variable/target/etc. defined in your regular CMakeLists.txt files. It is more similar to executing a bash/shell script than to processing a "usual" CMakeLists.txt.
But don't worry, there is still a solution to your problem. You can simply pass your arguments as -D options to your script:
add_custom_target(TargetName
COMMAND ${CMAKE_COMMAND}
-DMY_VAR="..."
-DANOTHER_VAR="..."
-P ${CMAKE_SOURCE_DIR}/cmake/script.cmake
...
)
Note however:
If variables are defined using -D, this must be done before the -P argument.
-- cmake(1)

CMAKE_CTEST_COMMAND not defined

I am using cmake 3.0.2 (on debian 8). I am trying to add some tests using a custom check target like this:
ADD_CUSTOM_TARGET(check
COMMAND ${CMAKE_COMMAND} -E env CTEST_OUTPUT_ON_FAILURE=1 ${CMAKE_CTEST_COMMAND}
DEPENDS test_1 test_2)
I am using the built-in CMAKE_CTEST_COMMAND which is supposedly available in cmake However, the variable is undefined and the command does not work. My questions are the following:
1: Am I doing it wrong? It seems to work with cmake 3.3.2, when was this variable added?
2: How can I get this to work in cmake 3.0.2, should I just replace CMAKE_CTEST_COMMAND with "ctest"?

What does the `-H.` option means for CMake?

This answer to a former question on CMake shows this command line:
cmake -H. -Bbuild -G "MSYS Makefiles"
What task does the -H. option perform here? cmake --help says that -H prints the help...
I am using CMake 3.2.3.
As mentioned in the linked answer, it is an undocumented option, but looking at the source code reveals its effect:
In cmake::SetArgs():
if(arg.find("-H",0) == 0)
{
directoriesSet = true;
std::string path = arg.substr(2);
path = cmSystemTools::CollapseFullPath(path);
cmSystemTools::ConvertToUnixSlashes(path);
this->SetHomeDirectory(path);
The last call, SetHomeDirectory actually sets the source directory for the project. The -B option (also undocumented) in turn sets the binary directory.
If these options are not set, the binary directory will be the current folder where cmake is executed, and the source directory can be given as a positional argument (if not found, the source folder will also be the current working directory).
The Hitchhiker’s Guide to the CMake explains both, the legacy and new in CMake 3.13 options:
-H
This internal option is not documented but widely used by community.
and
Has been replaced in 3.13 with the official source directory flag of -S.
-B
Starting with CMake 3.13, -B is an officially supported flag,
can handle spaces correctly and can be used independently of the -S or -H options.

How to set PATH environment variable in CMake script?

I want to build my sources by Mingw compiler which in not placed on my system PATH.
I tried this in the beginning of my script:
set(Env{PATH} "c:/MyProject/Tools/mingw/bin/" "c:/MyProject/Tools/mingw/msys/1.0/bin/")
And this:
set(CMAKE_PROGRAM_PATH "c:/MyProject/Tools/mingw/bin/" "c:/MyProject/Tools/mingw/msys/1.0/bin/")
set(CMAKE_LIBRARY_PATH "c:/MyProject/Tools/mingw/bin/" "c:/MyProject/Tools/mingw/msys/1.0/bin/")
set(CMAKE_SYSTEM_PROGRAM_PATH "c:/MyProject/Tools/mingw/bin/" "c:/MyProject/Tools/mingw/msys/1.0/bin/")
set(CMAKE_SYSTEM_PREFIX_PATH "c:/MyProject/Tools/mingw/bin/" "c:/MyProject/Tools/mingw/msys/1.0/bin/")
The first variant doesn't work at all. A suggest that I can't overwrite the value of the environment variable in CMake script.
The second script finds my mingw compiler, but catches the error while running gcc (can't find libgmp-10.dll which needs by gcc). This is because the PATH variable is not set to my Mingw.
CMAKE_SYSTEM_PROGRAM_PATH is not meant to be modified, use
LIST(APPEND CMAKE_PROGRAM_PATH "c:/MyProject/Tools/mingw/bin/" ...)
You might approach it as if it were a cross compiling toolchain, even if you're not cross compiling from Linux to Windows as in this example:
http://www.vtk.org/Wiki/CmakeMingw
After you follow that guide you set the mingw toolchain at the command line when calling cmake:
~/src/helloworld/ $ mkdir build
~/src/helloworld/ $ cd build
~/src/helloworld/build/ $ cmake -DCMAKE_TOOLCHAIN_FILE=~/Toolchain-mingw32.cmake
then if you're using this a whole lot you can make an alias to limit typing in that ugly -D every time you want to regenerate makefiles:
alias mingw-cmake='cmake -DCMAKE_TOOLCHAIN_FILE=~/Toolchain-mingw32.cmake'
Write a script file to start CMake.
On Windows make a batch file:
#echo off
set path=c:\MyProject\Tools\mingw\bin;c:\MyProject\Tools\mingw\msys\1.0\bin
"C:\Program Files\CMake 2.8\bin\cmake-gui.exe"
On Linux make a bash script:
export PATH=$PATH:/your/path