CMake space delimited list of objects - cmake

$<TARGET_OBJECTS:objLib> this CMake genex gives semi-colon
separated list of objects in an object library.
I need to set space delimited list of objects in a variable and use the variable in add_custom_target
I have tried using string replace in a separate cmake file, call that cmake by passing $<TARGET_OBJECTS:objLib> as an argument. But I don't know how to return the new string.
I tried setting the new string as ENV variable and use it in add_custom_target but it didnt work.
can some one please help me resolve this ?

Related

How can i override a default variable from CMakeLists.txt on CMake command line?

I have a .cmake file containing default values for variables. The .cmake file is called from CMakeLists.txt using 'include'.
One of the variables is a version number.
What is the best practice or setup to overrule that variable version number from CMake command line?
In gnu make you could use
var ?= value
where you can set var value on make command line. I do not see something similar in C-Make.
For boolean values, you can use option:
option(CUSTOMIZABLE_VAR "This variable do stuff" "default-value")
Also GUI apps exposing options such as CMake GUI or QtCreator you'll get a the description and a field to edit it.
For value of type string, you can set a cache value with a help string:
set(CUSTOMIZABLE_VAR "8" CACHE STRING "This option is a string")

How do I list cmake user-definable variables?

I want to list things that I can set with -D at configure time, like with ccmake or cmake-gui, but non-interactively.
How do I do it, apart from trying to capture ccmake's output or parsing cmake's files myself?
You can call the following in your binary output directory:
cmake -LH .
See CMake's documentation:
-L[A][H] List non-advanced cached variables.
List cache variables will run CMake and list all the variables from the CMake cache that are not marked as INTERNAL or ADVANCED. This will effectively display current CMake settings, which can then be changed with -D option. Changing some of the variables may result in more variables being created.
If A is specified, then it will display also advanced variables.
If H is specified, it will also display help for each variable.
If you want to list variables from CMakeLists.txt file itself, you may iterate over CACHE_VARIABLES property of the directory.
For each cache variable you may check its TYPE (using get_property(CACHE)). Types
BOOL
PATH
FILEPATH
STRING
correspond to variable, which is suitable for being adjusted by the user.
Also you may check cache variable's ADVANCED property.

Specify multiple paths in XXX_INCLUDE_DIRS or XXX_LIBRARIES of FindXXX.cmake

Consider the following example of a FindXXX.cmake:
find_path(XXX_INCLUDE_DIR NAMES XXX/XXX.h)
find_path(XXXYYY_INCLUDE_DIR NAMES YYY.h)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(XXX DEFAULT_MSG
XXX_INCLUDE_DIR XXXYYY_INCLUDE_DIR)
set(XXX_INCLUDE_DIRS "${XXX_INCLUDE_DIR} ${XXXYYY_INCLUDE_DIR}")
As it is in this example, XXX_INCLUDE_DIRS is a string with a space in the middle, and, thus, when added in CMakeLists.txt using
target_include_directories(a PRIVATE ${XXX_INCLUDE_DIRS})
it is invoked by the compiler as
-I"XXXpath XXXYYYpath"
How should I modify the line
set(XXX_INCLUDE_DIRS ${XXX_INCLUDE_DIR} ${XXXYYY_INCLUDE_DIR})
which sets the value for the variable XXX_INCLUDE_DIRS?
The same question arises for multiple library paths in XXX_LIBRARIES.
You provide multiple entries for variables like XXX_LIBRARIES and XXX_INCLUDE_DIRS as a semicolon-separated list.
set(XXX_INCLUDE_DIRS "${XXX_INCLUDE_DIR};${XXXYYY_INCLUDE_DIR}")

preserve whitespace in preprocessor macro value taken from environment variable

In an Objective-C project I am attempting to take a file path from an environment variable and set it as a location to write generated files. This is used to run test code using xcodebuild in an automated testing environment where the file path is not determined until xcodebuild is called.
In attempt to do this I am entering a preprocessor macro in the Build Settings that references the variable:
BUILDSERVER_WORKSPACE=\#\"$(WORKSPACE)\"
and then setting the value of a string using that macro
NSString *workspaceLocation = BUILDSERVER_WORKSPACE;
in cases where the (string value of) the path for $WORKSPACE does not contain spaces it works fine but in cases where the path has spaces, the macro preprocessor sees the whitespaces as a macro separator and attempts to process them as separate macro definitions.
for example:
$WORKSPACE=/foo/bar/thudblat
will set the value of workspacelocation as #"/foo/bar/thudblat"
but
$WORKSPACE="/foo/bar/thud blat"
ends up creating multiple preprocessor definitions:
#define BUILDSERVER_WORKSPACE #"/foo/bar/thud
#define blat"
I have attempted to stringify the path, but since the presence or absence of whitespace only happens when i call xcodebuild to build and then run and so I cannot get that to work.
In the end, what I want is to simply take the path at $WORKSPACE and set its value to the NSString *workspaceLocation
so that workspaceLocation could potentially be "\foo\bar\thud blat"
I thought I had tried every scheme of quoting and escaping but, the one thing I had not tried was quoting the entire thing as suggested by #nielsbot
BUILDSERVER_WORKSPACE="\#\"$(WORKSPACE)\""
with an unescaped quote at the beginning and end of the entire value statement. Thad did the trick and gave me the string: #"/foo/bar/thud blat" when calling xcodebuild.
You can achieve that with double stringize trick:
#define STRINGIZE_NX(A) #A
#define STRINGIZE(A) STRINGIZE_NX(A)
static NSString *kWorkspace = #( STRINGIZE(BUILDSERVER_WORKSPACE) );
The way it works is very well explained in here: https://stackoverflow.com/a/2751891/351305
If you wish to really read the environment variable at runtime then you can simply obtain it from NSProcessInfo:
NSString *workspaceLocation = NSProcessInfo.processInfo.environment[#"WORKSPACE"];
That will give you the current value of the environment variables, spaces and all.
HTH

CMake error with string sub-command STRIP "requires two arguments"

I am trying to compile a library with CMake. This library uses CMake with the pods build system.
During configuring I get the following error:
CMake Error at cmake/pods.cmake:257 (string):
string sub-command STRIP requires two arguments.
In the specific file pods.cmake the command looks like this:
execute_process(COMMAND
${PKG_CONFIG_EXECUTABLE} --cflags-only-I ${ARGN}
OUTPUT_VARIABLE _pods_pkg_include_flags)
string(STRIP ${_pods_pkg_include_flags} _pods_pkg_include_flags)
which looks fine to me. Any ideas why this error occurs? I don't understand why cmake complains that it needs two arguments for the STRIP command when it clearly has two.
Note: I use cmake 2.8.12.2, but according to the documentation this should be valid.
While your CMake file does syntactically contain two arguments, ${_pods_pkg_include_flags} can be empty. If so, it is not an argument semantically and never reaches string(), which then sees just one. If it's possible for a string to be empty (and you want to treat it as an empty string in such case instead of skipping it), quote it:
string(STRIP "${_pods_pkg_include_flags}" _pods_pkg_include_flags)