Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I define a structure data type in CMake?

Tags:

cmake

The dominant data type in CMake is string, and almost all the variables in CMake is based on string. I was wondering whether it is possible to create a structure similar to C/C++ structure. I give the following example to illustrate my question:

with C/C++ , we can define structure in this way:

struct targetProperty
{
   std::string folder_name;
   std::string lib_name;
   std::string exe_name;
   std::string lib_type;
};

In CMake, we can emulate this structure by using LIST:

set(targetProperty "the location for the folder")
list(APPEND targetProperty "the name of the lib")
list(APPEND targetProperty "the name of the executable")
list(APPEND targetProperty "the type of the lib")

But it is not as clear as struct targetProperty in C/C++, and I was wondering whether there are other smart alternatives. Thanks.

like image 571
feelfree Avatar asked Aug 11 '15 13:08

feelfree


People also ask

What is ${} in CMake?

You access a variable by using ${} , such as ${MY_VARIABLE} . 1. CMake has the concept of scope; you can access the value of the variable after you set it as long as you are in the same scope. If you leave a function or a file in a sub directory, the variable will no longer be defined.

How do I print a variable in CMake?

Run CMake and have a look at the cache with the ccmake GUI tool. Then you'll get all the variables. Or run CMake with -LH then you will get all variables printed after configuration.

What languages does CMake support?

Enables support for the named languages in CMake. This is the same as the project() command but does not create any of the extra variables that are created by the project command. Example languages are CXX , C , CUDA , OBJC , OBJCXX , Fortran , HIP , ISPC , and ASM .


2 Answers

If you need to associate structure with CMake targets(executables, libraries, custom ones), the simplest way is to use CMake properties:

define_property(TARGET PROPERTY folder_name
    BRIEF_DOCS "The location for the folder"
    FULL_DOCS "The location for the folder"
)
define_property(TARGET PROPERTY lib_name
    BRIEF_DOCS "The name of the lib"
    FULL_DOCS "The name of the lib"
)

... # Define other structure field as properties


# Add some CMake target
add_custom_target(my_target1 ...)

# Associate structure with target
set_target_properties(my_target1 PROPERTIES
    folder_name "dir1"
    ... # set other properties
)

# Use properties
get_target_property(my_target1_folder my_target1 folder_name)
message("folder_name for my_target1 is ${my_target1_folder}")

CMake properties can also be associated with source directories, which allows some sort of inheritance.

For more info see define_property command desctiption. Or ask more specific question.

like image 76
Tsyvarev Avatar answered Sep 27 '22 18:09

Tsyvarev


You can emulate structures with the variable name postfixed with the field name:

set(targetProperty_folder_name "the location for the folder")
set(targetProperty_lib_name "the name of the lib")

Another approach is what CMake uses to emulate named parameters in the commands:

list(APPEND targetProperty FOLDER_NAME "the location for the folder")
list(APPEND targetProperty LIB_NAME "the name of the lib")

then you can parse the list with the CMakeParseArguments module.

In both cases you can write setter and getter macros to automate the operations.

For a sophisticated framework solution see cmakepp/Objects.

like image 45
tamas.kenez Avatar answered Sep 27 '22 19:09

tamas.kenez