mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-01 11:22:21 -06:00
@@ -44,12 +44,6 @@ Paths are returned with forward slashes and have no trailing slashes. If the
|
||||
optional ``CACHE`` argument is specified, the result variable is added to the
|
||||
cache.
|
||||
|
||||
.. note::
|
||||
|
||||
All previous sub-commands has been superseded by
|
||||
:command:`cmake_path` command, except ``REALPATH`` now offered by
|
||||
:ref:`file(REAL_PATH) <REAL_PATH>` command.
|
||||
|
||||
.. code-block:: cmake
|
||||
|
||||
get_filename_component(<var> <FileName> PROGRAM [PROGRAM_ARGS <arg_var>] [CACHE])
|
||||
@@ -59,3 +53,9 @@ left as a full path. If ``PROGRAM_ARGS`` is present with ``PROGRAM``, then
|
||||
any command-line arguments present in the ``<FileName>`` string are split
|
||||
from the program name and stored in ``<arg_var>``. This is used to
|
||||
separate a program name from its arguments in a command line string.
|
||||
|
||||
.. note::
|
||||
|
||||
This command been superseded by :command:`cmake_path` command, except
|
||||
``REALPATH`` now offered by :ref:`file(REAL_PATH) <REAL_PATH>` command and
|
||||
``PROGRAM`` now available in :command:`separate_arguments(PROGRAM)` command.
|
||||
|
||||
@@ -5,7 +5,7 @@ Parse command-line arguments into a semicolon-separated list.
|
||||
|
||||
.. code-block:: cmake
|
||||
|
||||
separate_arguments(<variable> <mode> <args>)
|
||||
separate_arguments(<variable> <mode> [PROGRAM [SEPARATE_ARGS]] <args>)
|
||||
|
||||
Parses a space-separated string ``<args>`` into a list of items,
|
||||
and stores this list in semicolon-separated standard form in ``<variable>``.
|
||||
@@ -35,6 +35,36 @@ be one of the following keywords:
|
||||
Proceeds as in ``WINDOWS_COMMAND`` mode if the host system is Windows.
|
||||
Otherwise proceeds as in ``UNIX_COMMAND`` mode.
|
||||
|
||||
``PROGRAM``
|
||||
The first item in ``<args>`` is assumed to be an executable and will be
|
||||
search in the system search path or left as a full path. If not found,
|
||||
``<variable>`` will be empty. Otherwise, ``<variable>`` is a list of 2
|
||||
elements:
|
||||
|
||||
0. Absolute path of the program
|
||||
1. Any command-line arguments present in ``<args>`` as a string
|
||||
|
||||
For example:
|
||||
|
||||
.. code-block:: cmake
|
||||
|
||||
separate_arguments (out UNIX_COMMAND PROGRAM "cc -c main.c")
|
||||
|
||||
* First element of the list: ``/path/to/cc``
|
||||
* Second element of the list: ``" -c main.c"``
|
||||
|
||||
``SEPARATE_ARGS``
|
||||
When this sub-option of ``PROGRAM`` option is specified, command-line
|
||||
arguments will be split as well and stored in ``<variable>``.
|
||||
|
||||
For example:
|
||||
|
||||
.. code-block:: cmake
|
||||
|
||||
separate_arguments (out UNIX_COMMAND PROGRAM SEPARATE_ARGS "cc -c main.c")
|
||||
|
||||
The contents of ``out`` will be: ``/path/to/cc;-c;main.c``
|
||||
|
||||
.. _`Parsing C Command-Line Arguments`: https://msdn.microsoft.com/library/a1y7w461.aspx
|
||||
|
||||
.. code-block:: cmake
|
||||
|
||||
5
Help/release/dev/separate_arguments-PROGRAM.rst
Normal file
5
Help/release/dev/separate_arguments-PROGRAM.rst
Normal file
@@ -0,0 +1,5 @@
|
||||
separate_arguments-PROGRAM
|
||||
--------------------------
|
||||
|
||||
* The :command:`separate_arguments` command gained new ``PROGRAM`` option to
|
||||
search program.
|
||||
@@ -41,13 +41,17 @@ bool cmSeparateArgumentsCommand(std::vector<std::string> const& args,
|
||||
bool UnixCommand = false;
|
||||
bool WindowsCommand = false;
|
||||
bool NativeCommand = false;
|
||||
bool Program = false;
|
||||
bool SeparateArgs = false;
|
||||
};
|
||||
|
||||
static auto const parser =
|
||||
cmArgumentParser<Arguments>{}
|
||||
.Bind("UNIX_COMMAND"_s, &Arguments::UnixCommand)
|
||||
.Bind("WINDOWS_COMMAND"_s, &Arguments::WindowsCommand)
|
||||
.Bind("NATIVE_COMMAND"_s, &Arguments::NativeCommand);
|
||||
.Bind("NATIVE_COMMAND"_s, &Arguments::NativeCommand)
|
||||
.Bind("PROGRAM"_s, &Arguments::Program)
|
||||
.Bind("SEPARATE_ARGS"_s, &Arguments::SeparateArgs);
|
||||
|
||||
std::vector<std::string> unparsedArguments;
|
||||
Arguments arguments =
|
||||
@@ -66,6 +70,10 @@ bool cmSeparateArgumentsCommand(std::vector<std::string> const& args,
|
||||
"are mutually exclusive");
|
||||
return false;
|
||||
}
|
||||
if (arguments.SeparateArgs && !arguments.Program) {
|
||||
status.SetError("`SEPARATE_ARGS` option requires `PROGRAM' option");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (unparsedArguments.size() > 1) {
|
||||
status.SetError("given unexpected argument(s)");
|
||||
@@ -79,6 +87,35 @@ bool cmSeparateArgumentsCommand(std::vector<std::string> const& args,
|
||||
return true;
|
||||
}
|
||||
|
||||
if (arguments.Program && !arguments.SeparateArgs) {
|
||||
std::string program;
|
||||
std::string programArgs;
|
||||
|
||||
// First assume the path to the program was specified with no
|
||||
// arguments and with no quoting or escaping for spaces.
|
||||
// Only bother doing this if there is non-whitespace.
|
||||
if (!cmTrimWhitespace(command).empty()) {
|
||||
program = cmSystemTools::FindProgram(command);
|
||||
}
|
||||
|
||||
// If that failed then assume a command-line string was given
|
||||
// and split the program part from the rest of the arguments.
|
||||
if (program.empty()) {
|
||||
if (cmSystemTools::SplitProgramFromArgs(command, program, programArgs)) {
|
||||
if (!cmSystemTools::FileExists(program)) {
|
||||
program = cmSystemTools::FindProgram(program);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!program.empty()) {
|
||||
program += cmStrCat(';', programArgs);
|
||||
}
|
||||
|
||||
status.GetMakefile().AddDefinition(var, program);
|
||||
return true;
|
||||
}
|
||||
|
||||
// split command given
|
||||
std::vector<std::string> values;
|
||||
|
||||
@@ -96,6 +133,18 @@ bool cmSeparateArgumentsCommand(std::vector<std::string> const& args,
|
||||
cmSystemTools::ParseWindowsCommandLine(command.c_str(), values);
|
||||
}
|
||||
|
||||
if (arguments.Program) {
|
||||
// check program exist
|
||||
if (!cmSystemTools::FileExists(values.front())) {
|
||||
auto result = cmSystemTools::FindProgram(values.front());
|
||||
if (result.empty()) {
|
||||
values.clear();
|
||||
} else {
|
||||
values.front() = result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// preserve semicolons in arguments
|
||||
std::for_each(values.begin(), values.end(), [](std::string& value) {
|
||||
std::string::size_type pos = 0;
|
||||
|
||||
48
Tests/RunCMake/separate_arguments/ProgramCommand.cmake
Normal file
48
Tests/RunCMake/separate_arguments/ProgramCommand.cmake
Normal file
@@ -0,0 +1,48 @@
|
||||
|
||||
separate_arguments (out UNIX_COMMAND PROGRAM "xx a b c")
|
||||
if (out)
|
||||
message (SEND_ERROR "unexpected result with nonexistent program")
|
||||
endif()
|
||||
|
||||
set (TEST_EXE_DIR "${CMAKE_CURRENT_BINARY_DIR}/TestExe")
|
||||
file(MAKE_DIRECTORY "${TEST_EXE_DIR}")
|
||||
file(COPY "${CMAKE_COMMAND}" DESTINATION "${TEST_EXE_DIR}")
|
||||
cmake_path (GET CMAKE_COMMAND FILENAME cmake_exe)
|
||||
|
||||
set (ENV{PATH} "${TEST_EXE_DIR}")
|
||||
|
||||
|
||||
separate_arguments (out UNIX_COMMAND PROGRAM "${cmake_exe}")
|
||||
list (LENGTH out length)
|
||||
if (length EQUAL 0)
|
||||
message(FATAL_ERROR "existent program not found")
|
||||
endif()
|
||||
if (NOT length EQUAL 2)
|
||||
message(FATAL_ERROR "unexpected arguments")
|
||||
endif()
|
||||
list(GET out 0 cmake)
|
||||
list(GET out 1 args)
|
||||
if (NOT cmake STREQUAL "${TEST_EXE_DIR}/${cmake_exe}")
|
||||
message (SEND_ERROR "bad path for program: '${cmake}' instead of '${TEST_EXE_DIR}/${cmake_exe}'")
|
||||
endif()
|
||||
if (NOT args STREQUAL "")
|
||||
message (SEND_ERROR "bad value for args: '${args}' instead of ''")
|
||||
endif()
|
||||
|
||||
|
||||
separate_arguments (out UNIX_COMMAND PROGRAM "${cmake_exe} a b c")
|
||||
list (LENGTH out length)
|
||||
if (length EQUAL 0)
|
||||
message(FATAL_ERROR "existent program not found")
|
||||
endif()
|
||||
if (NOT length EQUAL 2)
|
||||
message(FATAL_ERROR "unexpected arguments")
|
||||
endif()
|
||||
list(GET out 0 cmake)
|
||||
list(GET out 1 args)
|
||||
if (NOT cmake STREQUAL "${TEST_EXE_DIR}/${cmake_exe}")
|
||||
message (SEND_ERROR "bad path for program: '${cmake}' instead of '${TEST_EXE_DIR}/${cmake_exe}'")
|
||||
endif()
|
||||
if (NOT args STREQUAL " a b c")
|
||||
message (SEND_ERROR "bad value for args: '${args}' instead of ' a b c'")
|
||||
endif()
|
||||
@@ -0,0 +1,28 @@
|
||||
|
||||
separate_arguments (out UNIX_COMMAND PROGRAM SEPARATE_ARGS "xx a b c")
|
||||
if (out)
|
||||
message (SEND_ERROR "unexpected result with nonexistent program")
|
||||
endif()
|
||||
|
||||
set (TEST_EXE_DIR "${CMAKE_CURRENT_BINARY_DIR}/TestExe")
|
||||
file(MAKE_DIRECTORY "${TEST_EXE_DIR}")
|
||||
file(COPY "${CMAKE_COMMAND}" DESTINATION "${TEST_EXE_DIR}")
|
||||
cmake_path (GET CMAKE_COMMAND FILENAME cmake_exe)
|
||||
|
||||
set (ENV{PATH} "${TEST_EXE_DIR}")
|
||||
|
||||
separate_arguments (out UNIX_COMMAND PROGRAM SEPARATE_ARGS "${cmake_exe} a b c")
|
||||
list (LENGTH out length)
|
||||
if (length EQUAL 0)
|
||||
message(FATAL_ERROR "existent program not found")
|
||||
endif()
|
||||
if (NOT length EQUAL 4)
|
||||
message(FATAL_ERROR "unexpected arguments")
|
||||
endif()
|
||||
list(POP_FRONT out cmake)
|
||||
if (NOT cmake STREQUAL "${TEST_EXE_DIR}/${cmake_exe}")
|
||||
message (SEND_ERROR "bad path for program: '${cmake}' instead of '${TEST_EXE_DIR}/${cmake_exe}'")
|
||||
endif()
|
||||
if (NOT out STREQUAL "a;b;c")
|
||||
message (SEND_ERROR "bad path for args: '${out}' instead of 'a;b;c'")
|
||||
endif()
|
||||
1
Tests/RunCMake/separate_arguments/ProgramOnly-result.txt
Normal file
1
Tests/RunCMake/separate_arguments/ProgramOnly-result.txt
Normal file
@@ -0,0 +1 @@
|
||||
1
|
||||
5
Tests/RunCMake/separate_arguments/ProgramOnly-stderr.txt
Normal file
5
Tests/RunCMake/separate_arguments/ProgramOnly-stderr.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
CMake Error at ProgramOnly.cmake:[0-9]+ \(separate_arguments\):
|
||||
separate_arguments missing required option: 'UNIX_COMMAND' or
|
||||
'WINDOWS_COMMAND' or 'NATIVE_COMMAND'
|
||||
Call Stack \(most recent call first\):
|
||||
CMakeLists.txt:[0-9]+ \(include\)
|
||||
2
Tests/RunCMake/separate_arguments/ProgramOnly.cmake
Normal file
2
Tests/RunCMake/separate_arguments/ProgramOnly.cmake
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
separate_arguments (var PROGRAM arg)
|
||||
@@ -2,9 +2,14 @@ include(RunCMake)
|
||||
|
||||
run_cmake(MultipleCommands)
|
||||
run_cmake(MultipleArguments)
|
||||
run_cmake(ProgramOnly)
|
||||
run_cmake(SeparateArgsOnly)
|
||||
|
||||
run_cmake(EmptyCommand)
|
||||
run_cmake(PlainCommand)
|
||||
run_cmake(UnixCommand)
|
||||
run_cmake(WindowsCommand)
|
||||
run_cmake(NativeCommand)
|
||||
|
||||
run_cmake(ProgramCommand)
|
||||
run_cmake(ProgramCommandWithSeparateArgs)
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
1
|
||||
@@ -0,0 +1,4 @@
|
||||
CMake Error at SeparateArgsOnly.cmake:[0-9]+ \(separate_arguments\):
|
||||
separate_arguments `SEPARATE_ARGS` option requires `PROGRAM' option
|
||||
Call Stack \(most recent call first\):
|
||||
CMakeLists.txt:[0-9]+ \(include\)
|
||||
2
Tests/RunCMake/separate_arguments/SeparateArgsOnly.cmake
Normal file
2
Tests/RunCMake/separate_arguments/SeparateArgsOnly.cmake
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
separate_arguments (var UNIX_COMMAND SEPARATE_ARGS arg)
|
||||
Reference in New Issue
Block a user