mirror of https://github.com/Kitware/CMake.git
Browse Source
find_package: Optionally sort globbed directories in a meaningful order
find_package: Optionally sort globbed directories in a meaningful order
Add `CMAKE_FIND_PACKAGE_SORT_{ORDER,DIRECTION}` variables to specify sort order and direction. When multiple package with the same name have been found in the same location sorting option can be used to force a specific version to be loaded (e.g. libA_1.12.0 instead of libA_1.1.0). Currently sorting by NAME and by NATURAL order have been implemented. Natural ordering makes use of the `strverscmp(3)` ordering.pull/274/head

committed by
Brad King

14 changed files with 341 additions and 17 deletions
-
18Help/command/find_package.rst
-
2Help/manual/cmake-variables.7.rst
-
13Help/release/dev/find_package-dir-sort.rst
-
16Help/variable/CMAKE_FIND_PACKAGE_SORT_DIRECTION.rst
-
36Help/variable/CMAKE_FIND_PACKAGE_SORT_ORDER.rst
-
118Source/cmFindPackageCommand.cxx
-
26Source/cmFindPackageCommand.h
-
1Tests/CMakeLib/CMakeLists.txt
-
76Tests/CMakeLib/testFindPackageCommand.cxx
-
30Tests/FindPackageTest/CMakeLists.txt
-
2Tests/FindPackageTest/SortLib-3.1.1/SortLibConfig.cmake
-
9Tests/FindPackageTest/SortLib-3.1.1/SortLibConfigVersion.cmake
-
2Tests/FindPackageTest/SortLib-3.10.1/SortLibConfig.cmake
-
9Tests/FindPackageTest/SortLib-3.10.1/SortLibConfigVersion.cmake
@ -0,0 +1,13 @@ |
|||
find_package-dir-sort |
|||
--------------------- |
|||
|
|||
* The :command:`find_package` command gained the possibility of |
|||
sorting compatible libraries by ``NAME`` or by ``NATURAL`` sorting by |
|||
setting the two new variables :variable:`CMAKE_FIND_PACKAGE_SORT_ORDER` |
|||
and :variable:`CMAKE_FIND_PACKAGE_SORT_DIRECTION`. |
|||
|
|||
* Variable :variable:`CMAKE_FIND_PACKAGE_SORT_ORDER` was added to control |
|||
the sorting mode of the :command:`find_package` command. |
|||
|
|||
* Variable :variable:`CMAKE_FIND_PACKAGE_SORT_DIRECTION` was added to control |
|||
the sorting direction the :command:`find_package` command. |
@ -0,0 +1,16 @@ |
|||
CMAKE_FIND_PACKAGE_SORT_DIRECTION |
|||
--------------------------------- |
|||
|
|||
The sorting direction used by :variable:`CMAKE_FIND_PACKAGE_SORT_ORDER`. |
|||
It can assume one of the following values: |
|||
|
|||
``DEC`` |
|||
Default. Ordering is done in descending mode. |
|||
The highest folder found will be tested first. |
|||
|
|||
``ASC`` |
|||
Ordering is done in ascending mode. |
|||
The lowest folder found will be tested first. |
|||
|
|||
If :variable:`CMAKE_FIND_PACKAGE_SORT_ORDER` is not set or is set to ``NONE`` |
|||
this variable has no effect. |
@ -0,0 +1,36 @@ |
|||
CMAKE_FIND_PACKAGE_SORT_ORDER |
|||
----------------------------- |
|||
|
|||
The default order for sorting packages found using :command:`find_package`. |
|||
It can assume one of the following values: |
|||
|
|||
``NONE`` |
|||
Default. No attempt is done to sort packages. |
|||
The first valid package found will be selected. |
|||
|
|||
``NAME`` |
|||
Sort packages lexicographically before selecting one. |
|||
|
|||
``NATURAL`` |
|||
Sort packages using natural order (see ``strverscmp(3)`` manual), |
|||
i.e. such that contiguous digits are compared as whole numbers. |
|||
|
|||
Natural sorting can be employed to return the highest version when multiple |
|||
versions of the same library are found by :command:`find_package`. For |
|||
example suppose that the following libraries have been found: |
|||
|
|||
* libX-1.1.0 |
|||
* libX-1.2.9 |
|||
* libX-1.2.10 |
|||
|
|||
By setting ``NATURAL`` order we can select the one with the highest |
|||
version number ``libX-1.2.10``. |
|||
|
|||
.. code-block:: cmake |
|||
|
|||
set(CMAKE_FIND_PACKAGE_SORT_ORDER NATURAL) |
|||
find_package(libX CONFIG) |
|||
|
|||
The sort direction can be controlled using the |
|||
:variable:`CMAKE_FIND_PACKAGE_SORT_DIRECTION` variable |
|||
(by default decrescent, e.g. lib-B will be tested before lib-A). |
@ -0,0 +1,76 @@ |
|||
/*============================================================================
|
|||
CMake - Cross Platform Makefile Generator |
|||
Copyright 2000-2011 Kitware, Inc., Insight Software Consortium |
|||
|
|||
Distributed under the OSI-approved BSD License (the "License"); |
|||
see accompanying file Copyright.txt for details. |
|||
|
|||
This software is distributed WITHOUT ANY WARRANTY; without even the |
|||
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
See the License for more information. |
|||
============================================================================*/ |
|||
#include "cmFindPackageCommand.h"
|
|||
|
|||
#include <iostream>
|
|||
#include <string>
|
|||
|
|||
#define cmPassed(m) std::cout << "Passed: " << (m) << "\n"
|
|||
#define cmFailed(m) \
|
|||
std::cout << "FAILED: " << (m) << "\n"; \ |
|||
failed = 1 |
|||
|
|||
int testFindPackageCommand(int /*unused*/, char* /*unused*/ []) |
|||
{ |
|||
int failed = 0; |
|||
|
|||
// ----------------------------------------------------------------------
|
|||
// Test cmFindPackage::Sort
|
|||
std::vector<std::string> testString; |
|||
testString.push_back("lib-0.0"); |
|||
testString.push_back("lib-1.2"); |
|||
testString.push_back("lib-2.0"); |
|||
testString.push_back("lib-19.0.1"); |
|||
testString.push_back("lib-20.01.1"); |
|||
testString.push_back("lib-20.2.2a"); |
|||
|
|||
cmFindPackageCommand::Sort(testString.begin(), testString.end(), |
|||
cmFindPackageCommand::Natural, |
|||
cmFindPackageCommand::Asc); |
|||
if (!(testString[0] == "lib-0.0" && testString[1] == "lib-1.2" && |
|||
testString[2] == "lib-2.0" && testString[3] == "lib-19.0.1" && |
|||
testString[4] == "lib-20.01.1" && testString[5] == "lib-20.2.2a")) { |
|||
cmFailed("cmSystemTools::Sort fail with Natural ASC"); |
|||
} |
|||
|
|||
cmFindPackageCommand::Sort(testString.begin(), testString.end(), |
|||
cmFindPackageCommand::Natural, |
|||
cmFindPackageCommand::Dec); |
|||
if (!(testString[5] == "lib-0.0" && testString[4] == "lib-1.2" && |
|||
testString[3] == "lib-2.0" && testString[2] == "lib-19.0.1" && |
|||
testString[1] == "lib-20.01.1" && testString[0] == "lib-20.2.2a")) { |
|||
cmFailed("cmSystemTools::Sort fail with Natural ASC"); |
|||
} |
|||
|
|||
cmFindPackageCommand::Sort(testString.begin(), testString.end(), |
|||
cmFindPackageCommand::Name_order, |
|||
cmFindPackageCommand::Dec); |
|||
if (!(testString[5] == "lib-0.0" && testString[4] == "lib-1.2" && |
|||
testString[3] == "lib-19.0.1" && testString[2] == "lib-2.0" && |
|||
testString[1] == "lib-20.01.1" && testString[0] == "lib-20.2.2a")) { |
|||
cmFailed("cmSystemTools::Sort fail with Name DEC"); |
|||
} |
|||
|
|||
cmFindPackageCommand::Sort(testString.begin(), testString.end(), |
|||
cmFindPackageCommand::Name_order, |
|||
cmFindPackageCommand::Asc); |
|||
if (!(testString[0] == "lib-0.0" && testString[1] == "lib-1.2" && |
|||
testString[2] == "lib-19.0.1" && testString[3] == "lib-2.0" && |
|||
testString[4] == "lib-20.01.1" && testString[5] == "lib-20.2.2a")) { |
|||
cmFailed("cmSystemTools::Sort fail with Natural ASC"); |
|||
} |
|||
|
|||
if (!failed) { |
|||
cmPassed("cmSystemTools::Sort working"); |
|||
} |
|||
return failed; |
|||
} |
@ -0,0 +1,2 @@ |
|||
set(SORT_LIB_VERSION 3.1.1) |
|||
message("SortLib 3.1.1 config reached") |
@ -0,0 +1,9 @@ |
|||
set(PACKAGE_VERSION 3.1.1) |
|||
if(PACKAGE_FIND_VERSION_MAJOR EQUAL 3) |
|||
if(PACKAGE_FIND_VERSION_MINOR EQUAL 1) |
|||
set(PACKAGE_VERSION_COMPATIBLE 1) |
|||
if(PACKAGE_FIND_VERSION_PATCH EQUAL 1) |
|||
set(PACKAGE_VERSION_EXACT 1) |
|||
endif() |
|||
endif() |
|||
endif() |
@ -0,0 +1,2 @@ |
|||
set(SORT_LIB_VERSION 3.10.1) |
|||
message("SortLib 3.10.1 config reached") |
@ -0,0 +1,9 @@ |
|||
set(PACKAGE_VERSION 3.10.1) |
|||
if(PACKAGE_FIND_VERSION_MAJOR EQUAL 3) |
|||
if(PACKAGE_FIND_VERSION_MINOR EQUAL 10) |
|||
set(PACKAGE_VERSION_COMPATIBLE 1) |
|||
if(PACKAGE_FIND_VERSION_PATCH EQUAL 1) |
|||
set(PACKAGE_VERSION_EXACT 1) |
|||
endif() |
|||
endif() |
|||
endif() |
Write
Preview
Loading…
Cancel
Save
Reference in new issue