Cmake function parameter parsing and cmake function Parsing

Source: Internet
Author: User
Tags keyword list types of functions

Cmake function parameter parsing and cmake function Parsing

Recently, when migrating the company's make system to cmake, we found that the function parameter of cmake is very strange. For example, if we pass a list as a parameter to a function, the form parameter in the function will become the following:

set(SRC)list(APPEND SRC a.cpp b.cpp)list(APPEND SRC c.cpp d.cpp)function(tst_arguments src_list)message("src_list = "${src_list})endfunction()message("SRC = "${SRC})<pre name="code" class="plain">tst_arguments(${SRC})==== output ====SRC = a.cppb.cppc.cppd.cppsrc_list = a.cpp

It is strange that $ {SRC} here is a complete 4 elements outside function, in function, only the first element is left (probably related to the fixed length of list ). If we want to pass the function to a list consisting of n source files, this obviously won't work. 

A simple solution is to use ARGV and ARGC in combination. Their meanings are as follows: main's argv and argc in C/C ++, representing the number of parameters and the number of parameters respectively, parse parameters using the following method:

function(tst_arguments src_list)message("ARGC = "${ARGC})message("ARGV = "${ARGV})        set(INDEX 0)    while(INDEX LESS ${ARGC})message("ARG = "${ARGV${INDEX}})math(EXPR INDEX "${INDEX} + 1")endwhile()endfunction()<pre name="code" class="plain"><pre name="code" class="plain">tst_arguments(${SRC})

 
<pre name="code" class="plain">==== output ====ARGC = 4ARGV = a.cppb.cppc.cppd.cpp
ARG = a. cppARG = B. cppARG = c. cppARG = d. cpp 
Of course, you can also use the foreach loop traversal parameter of cmake. This method is very effective when there is only one list parameter, but multiple parameters appear, as follows: 

# Assume that the function link_lib links the source files in the src_list to a library and determines whether the library is a static library or a dynamic library based on the type. # function (link_lib src_list type) <pre name = "code" class = "plain"> message ("ARGC =" $ {ARGC}) message ("ARGV =" $ {ARGV })

# The following operations are performed based on the actual conditions of the parameters to ensure that src_list and typeset (INDEX 0) math (expr max "$ {ARGC}-1") are obtained correctly ") while (index less $ {MAX}) # do something to linkmath (expr index "$ {INDEX} + 1") endwhile () endfunction ()

link_lib(${SRC} , so)<pre name="code" class="plain"><pre name="code" class="plain"><pre name="code" class="plain">==== output ====

 
ARGC = 5 

ARGV = a. cppb. cppc. cppd. cppso
 
Originally, ARG mixed the two parameters. Although we used while for special processing later, it was not universal for the cmake function and it was very troublesome to transplant it. 

The final version of solution uses cmake's cmake_parse_arguments to parse function parameters. It is a bit like parsing a map key-value pair. First, let's look at its function prototype:

Include (CMakeParseArguments) # <span class = "highlighted"> cmake_parse_arguments </span> values (<prefix> <options> <one_value_keywords> <multi_value_keywords> args...) must contain this cmake file ...)
First, prefix is a prefix, which will be mentioned later when you reference the parameter. <option> is a list that contains some KeyWord you are interested in, then you can use it to check whether the KeyWord you need is set. <One_value_keywords> is a single-value parameter KeyWord list. <Multi_value_keywords> is a multi-value parameter KeyWord list (such as list). The following example shows how to use them. First, define the required functions, because the parameters are parsed by CMAKE_PARSE_ARGUMENTS, you do not need to define the parameters in the function declaration:

function(tst_arguments)  CMAKE_PARSE_ARGUMENTS(    TEST "" "NAME;COMMAND;BASELINE"       "ARGSLIST"       ${ARGN}  )  message("TEST_DEFAULT_ARGS is ${TEST_DEFAULT_ARGS} from ${ARGN}")  message("TEST_NAME is ${TEST_NAME}")  message("TEST_COMMAND is ${TEST_COMMAND}")  message("TEST_ARGSLIST is ${TEST_ARGSLIST}")  message("TEST_BASELINE is ${TEST_BASELINE}")endfunction(tst_arguments)
The prefix here is TEST. <one_value_keywords> we set the KeyWord (NAME; COMMAND; BASELINE) of the single-Value parameter. This will indicate the relationship between KeyWord and Value in subsequent function calls, <multi_value_keywords> we set the KeyWord ("ARGSLIST") of the multi-value parameter and call the function:

TEST_ARGUMENT(    NAME      testiso    COMMAND      "RunMe"    ARGSLIST      ${SRC}    BASELINE      "/home/sakaue/iWork")==== output ====TEST_DEFAULT_ARGS is  from NAME;testiso;COMMAND;RunMe;ARGSLIST;a.cpp;b.cpp;c.cpp;d.cpp;BASELINE;/home/sakaue/iWorkTEST_NAME is testisoTEST_COMMAND is RunMeTEST_ARGSLIST is a.cpp;b.cpp;c.cpp;d.cppTEST_BASELINE is /home/sakaue/iWork

 
 
 
 
As you can see, the parameter transfer here is the same as map <NAME, testiso _ $ {datafile} >,< COMMAND, "RunMe" >,< ARGSLIST, $ {SRC}>. In the function, the prefix + KeyWord is used to call the Value, which is much more convenient than self-parsing parameters, it also does not mix the list parameter with other types of functions. 





Every form parameter of A function is A expression B constant C variable D function call

C

Main () parameters of the main function

The parameters of the main function are the parameters of the input command line. The main function cannot be called by other functions.

Int main (int argc, char * argv []);
Int main (int argc, char ** argv );
Int main (void );

Void main (int argc, char * argv []);
Void main (int argc, char ** argv );
Void main (void );

Supplementary answer:
------------------------------------
The third parameter of WinMain () is LPSTR lpCmdLine, which is a command line parameter.
This is equivalent to argv. You need to parse each parameter by yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.