blob: 6a667c8040fd0eae5d4a0dbee4c2edf28eca72b5 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cmake_parse_arguments &mdash; CMake 3.8.2 Documentation</title>
<link rel="stylesheet" href="../_static/cmake.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '3.8.2',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="shortcut icon" href="../_static/cmake-favicon.ico"/>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="cmake_policy" href="cmake_policy.html" />
<link rel="prev" title="cmake_minimum_required" href="cmake_minimum_required.html" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="cmake_policy.html" title="cmake_policy"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="cmake_minimum_required.html" title="cmake_minimum_required"
accesskey="P">previous</a> |</li>
<li>
<img src="../_static/cmake-logo-16.png" alt=""
style="vertical-align: middle; margin-top: -2px" />
</li>
<li>
<a href="https://cmake.org/">CMake</a> &#187;
</li>
<li>
<a href="../index.html">3.8.2 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="../manual/cmake-commands.7.html" accesskey="U">cmake-commands(7)</a> &#187;</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="cmake-parse-arguments">
<span id="command:cmake_parse_arguments"></span><h1>cmake_parse_arguments<a class="headerlink" href="#cmake-parse-arguments" title="Permalink to this headline">ΒΆ</a></h1>
<p><code class="docutils literal"><span class="pre">cmake_parse_arguments</span></code> is intended to be used in macros or functions for
parsing the arguments given to that macro or function. It processes the
arguments and defines a set of variables which hold the values of the
respective options.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">cmake_parse_arguments</span><span class="p">(</span><span class="o">&lt;</span><span class="n">prefix</span><span class="o">&gt;</span> <span class="o">&lt;</span><span class="n">options</span><span class="o">&gt;</span> <span class="o">&lt;</span><span class="n">one_value_keywords</span><span class="o">&gt;</span>
<span class="o">&lt;</span><span class="n">multi_value_keywords</span><span class="o">&gt;</span> <span class="n">args</span><span class="o">...</span><span class="p">)</span>
<span class="n">cmake_parse_arguments</span><span class="p">(</span><span class="n">PARSE_ARGV</span> <span class="n">N</span> <span class="o">&lt;</span><span class="n">prefix</span><span class="o">&gt;</span> <span class="o">&lt;</span><span class="n">options</span><span class="o">&gt;</span> <span class="o">&lt;</span><span class="n">one_value_keywords</span><span class="o">&gt;</span>
<span class="o">&lt;</span><span class="n">multi_value_keywords</span><span class="o">&gt;</span><span class="p">)</span>
</pre></div>
</div>
<p>The first signature reads processes arguments passed in the <code class="docutils literal"><span class="pre">args...</span></code>.
This may be used in either a <span class="target" id="index-0-command:macro"></span><a class="reference internal" href="macro.html#command:macro" title="macro"><code class="xref cmake cmake-command docutils literal"><span class="pre">macro()</span></code></a> or a <span class="target" id="index-0-command:function"></span><a class="reference internal" href="function.html#command:function" title="function"><code class="xref cmake cmake-command docutils literal"><span class="pre">function()</span></code></a>.</p>
<p>The <code class="docutils literal"><span class="pre">PARSE_ARGV</span></code> signature is only for use in a <span class="target" id="index-1-command:function"></span><a class="reference internal" href="function.html#command:function" title="function"><code class="xref cmake cmake-command docutils literal"><span class="pre">function()</span></code></a>
body. In this case the arguments that are parsed come from the
<code class="docutils literal"><span class="pre">ARGV#</span></code> variables of the calling function. The parsing starts with
the Nth argument, where <code class="docutils literal"><span class="pre">N</span></code> is an unsigned integer. This allows for
the values to have special characters like <code class="docutils literal"><span class="pre">;</span></code> in them.</p>
<p>The <code class="docutils literal"><span class="pre">&lt;options&gt;</span></code> argument contains all options for the respective macro,
i.e. keywords which can be used when calling the macro without any value
following, like e.g. the <code class="docutils literal"><span class="pre">OPTIONAL</span></code> keyword of the <span class="target" id="index-0-command:install"></span><a class="reference internal" href="install.html#command:install" title="install"><code class="xref cmake cmake-command docutils literal"><span class="pre">install()</span></code></a>
command.</p>
<p>The <code class="docutils literal"><span class="pre">&lt;one_value_keywords&gt;</span></code> argument contains all keywords for this macro
which are followed by one value, like e.g. <code class="docutils literal"><span class="pre">DESTINATION</span></code> keyword of the
<span class="target" id="index-1-command:install"></span><a class="reference internal" href="install.html#command:install" title="install"><code class="xref cmake cmake-command docutils literal"><span class="pre">install()</span></code></a> command.</p>
<p>The <code class="docutils literal"><span class="pre">&lt;multi_value_keywords&gt;</span></code> argument contains all keywords for this
macro which can be followed by more than one value, like e.g. the
<code class="docutils literal"><span class="pre">TARGETS</span></code> or <code class="docutils literal"><span class="pre">FILES</span></code> keywords of the <span class="target" id="index-2-command:install"></span><a class="reference internal" href="install.html#command:install" title="install"><code class="xref cmake cmake-command docutils literal"><span class="pre">install()</span></code></a> command.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">All keywords shall be unique. I.e. every keyword shall only be specified
once in either <code class="docutils literal"><span class="pre">&lt;options&gt;</span></code>, <code class="docutils literal"><span class="pre">&lt;one_value_keywords&gt;</span></code> or
<code class="docutils literal"><span class="pre">&lt;multi_value_keywords&gt;</span></code>. A warning will be emitted if uniqueness is
violated.</p>
</div>
<p>When done, <code class="docutils literal"><span class="pre">cmake_parse_arguments</span></code> will have defined for each of the
keywords listed in <code class="docutils literal"><span class="pre">&lt;options&gt;</span></code>, <code class="docutils literal"><span class="pre">&lt;one_value_keywords&gt;</span></code> and
<code class="docutils literal"><span class="pre">&lt;multi_value_keywords&gt;</span></code> a variable composed of the given <code class="docutils literal"><span class="pre">&lt;prefix&gt;</span></code>
followed by <code class="docutils literal"><span class="pre">&quot;_&quot;</span></code> and the name of the respective keyword. These
variables will then hold the respective value from the argument list.
For the <code class="docutils literal"><span class="pre">&lt;options&gt;</span></code> keywords this will be <code class="docutils literal"><span class="pre">TRUE</span></code> or <code class="docutils literal"><span class="pre">FALSE</span></code>.</p>
<p>All remaining arguments are collected in a variable
<code class="docutils literal"><span class="pre">&lt;prefix&gt;_UNPARSED_ARGUMENTS</span></code>, this can be checked afterwards to see
whether your macro was called with unrecognized parameters.</p>
<p>As an example here a <code class="docutils literal"><span class="pre">my_install()</span></code> macro, which takes similar arguments
as the real <span class="target" id="index-3-command:install"></span><a class="reference internal" href="install.html#command:install" title="install"><code class="xref cmake cmake-command docutils literal"><span class="pre">install()</span></code></a> command:</p>
<div class="highlight-cmake"><div class="highlight"><pre><span></span><span class="nb">function</span><span class="p">(</span><span class="s">MY_INSTALL</span><span class="p">)</span>
<span class="nb">set</span><span class="p">(</span><span class="s">options</span> <span class="s">OPTIONAL</span> <span class="s">FAST</span><span class="p">)</span>
<span class="nb">set</span><span class="p">(</span><span class="s">oneValueArgs</span> <span class="s">DESTINATION</span> <span class="s">RENAME</span><span class="p">)</span>
<span class="nb">set</span><span class="p">(</span><span class="s">multiValueArgs</span> <span class="s">TARGETS</span> <span class="s">CONFIGURATIONS</span><span class="p">)</span>
<span class="nb">cmake_parse_arguments</span><span class="p">(</span><span class="s">MY_INSTALL</span> <span class="s2">&quot;${options}&quot;</span> <span class="s2">&quot;${oneValueArgs}&quot;</span>
<span class="s2">&quot;${multiValueArgs}&quot;</span> <span class="o">${</span><span class="nv">ARGN</span><span class="o">}</span> <span class="p">)</span>
<span class="c"># ...</span>
</pre></div>
</div>
<p>Assume <code class="docutils literal"><span class="pre">my_install()</span></code> has been called like this:</p>
<div class="highlight-cmake"><div class="highlight"><pre><span></span><span class="nb">my_install</span><span class="p">(</span><span class="s">TARGETS</span> <span class="s">foo</span> <span class="s">bar</span> <span class="s">DESTINATION</span> <span class="s">bin</span> <span class="s">OPTIONAL</span> <span class="s">blub</span><span class="p">)</span>
</pre></div>
</div>
<p>After the <code class="docutils literal"><span class="pre">cmake_parse_arguments</span></code> call the macro will have set the
following variables:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">MY_INSTALL_OPTIONAL</span> <span class="o">=</span> <span class="n">TRUE</span>
<span class="n">MY_INSTALL_FAST</span> <span class="o">=</span> <span class="n">FALSE</span> <span class="p">(</span><span class="n">was</span> <span class="ow">not</span> <span class="n">used</span> <span class="ow">in</span> <span class="n">call</span> <span class="n">to</span> <span class="n">my_install</span><span class="p">)</span>
<span class="n">MY_INSTALL_DESTINATION</span> <span class="o">=</span> <span class="s2">&quot;bin&quot;</span>
<span class="n">MY_INSTALL_RENAME</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span> <span class="p">(</span><span class="n">was</span> <span class="ow">not</span> <span class="n">used</span><span class="p">)</span>
<span class="n">MY_INSTALL_TARGETS</span> <span class="o">=</span> <span class="s2">&quot;foo;bar&quot;</span>
<span class="n">MY_INSTALL_CONFIGURATIONS</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span> <span class="p">(</span><span class="n">was</span> <span class="ow">not</span> <span class="n">used</span><span class="p">)</span>
<span class="n">MY_INSTALL_UNPARSED_ARGUMENTS</span> <span class="o">=</span> <span class="s2">&quot;blub&quot;</span> <span class="p">(</span><span class="n">nothing</span> <span class="n">expected</span> <span class="n">after</span> <span class="s2">&quot;OPTIONAL&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>You can then continue and process these variables.</p>
<p>Keywords terminate lists of values, e.g. if directly after a
one_value_keyword another recognized keyword follows, this is
interpreted as the beginning of the new option. E.g.
<code class="docutils literal"><span class="pre">my_install(TARGETS</span> <span class="pre">foo</span> <span class="pre">DESTINATION</span> <span class="pre">OPTIONAL)</span></code> would result in
<code class="docutils literal"><span class="pre">MY_INSTALL_DESTINATION</span></code> set to <code class="docutils literal"><span class="pre">&quot;OPTIONAL&quot;</span></code>, but as <code class="docutils literal"><span class="pre">OPTIONAL</span></code>
is a keyword itself <code class="docutils literal"><span class="pre">MY_INSTALL_DESTINATION</span></code> will be empty and
<code class="docutils literal"><span class="pre">MY_INSTALL_OPTIONAL</span></code> will therefore be set to <code class="docutils literal"><span class="pre">TRUE</span></code>.</p>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="cmake_minimum_required.html"
title="previous chapter">cmake_minimum_required</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="cmake_policy.html"
title="next chapter">cmake_policy</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/command/cmake_parse_arguments.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="cmake_policy.html" title="cmake_policy"
>next</a> |</li>
<li class="right" >
<a href="cmake_minimum_required.html" title="cmake_minimum_required"
>previous</a> |</li>
<li>
<img src="../_static/cmake-logo-16.png" alt=""
style="vertical-align: middle; margin-top: -2px" />
</li>
<li>
<a href="https://cmake.org/">CMake</a> &#187;
</li>
<li>
<a href="../index.html">3.8.2 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="../manual/cmake-commands.7.html" >cmake-commands(7)</a> &#187;</li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2000-2017 Kitware, Inc. and Contributors.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.2.
</div>
</body>
</html>