Merge topic 'source-with-and-without-extension'

321c647640 Include source file without an extension after the same name with an extension

Acked-by: Kitware Robot <kwrobot@kitware.com>
Tested-by: buildbot <buildbot@kitware.com>
Merge-request: !10271
This commit is contained in:
Brad King 2025-02-16 16:10:50 +00:00 committed by Kitware Robot
commit ad13d2f99a
15 changed files with 113 additions and 3 deletions

View File

@ -98,6 +98,7 @@ Policies Introduced by CMake 4.1
.. toctree::
:maxdepth: 1
CMP0187: Include source file without an extension after the same name with an extension. </policy/CMP0187>
CMP0186: Regular expressions match ^ at most once in repeated searches. </policy/CMP0186>
Policies Introduced by CMake 4.0

33
Help/policy/CMP0187.rst Normal file
View File

@ -0,0 +1,33 @@
CMP0187
-------
.. versionadded:: 4.1
Include source file without an extension after the same name with an extension.
In CMake 4.0 and below, if two source files have the same filename and only one
file has a file extension and the file with the extension is listed first, the
file without the extension is omitted from the target.
For example, the following library target only include ``hello.c`` in the
target, but omits the file ``hello``.
.. code-block:: cmake
add_library(library hello.c hello)
If the file without the extension is listed before the file with the extension,
both files are included in the target.
Starting in CMake 4.1, CMake includes both files in the library target.
This policy has no effect if :policy:`CMP0115` uses the ``OLD`` behavior.
The ``OLD`` behavior for this policy is to omit the file without the extension.
The ``NEW`` behavior for this policy is to include it.
.. |INTRODUCED_IN_CMAKE_VERSION| replace:: 4.1
.. |WARNS_OR_DOES_NOT_WARN| replace:: does *not* warn
.. include:: STANDARD_ADVICE.txt
.. include:: DEPRECATED.txt

View File

@ -558,7 +558,11 @@ class cmMakefile;
WARN) \
SELECT(POLICY, CMP0186, \
"Regular expressions match ^ at most once in repeated searches.", 4, \
1, 0, WARN)
1, 0, WARN) \
SELECT(POLICY, CMP0187, \
"Include source file without an extension after the same name with " \
"an extension.", \
4, 1, 0, WARN)
#define CM_SELECT_ID(F, A1, A2, A3, A4, A5, A6) F(A1)
#define CM_FOR_EACH_POLICY_ID(POLICY) \

View File

@ -9,11 +9,19 @@
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmMessageType.h"
#include "cmPolicies.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmake.h"
cmSourceFileLocation::cmSourceFileLocation() = default;
// if CMP0187 and CMP0115 are NEW, then we assume that source files that do not
// include a file extension are not ambiguous but intentionally do not have an
// extension.
bool NoAmbiguousExtensions(cmMakefile const& makefile)
{
return makefile.GetPolicyStatus(cmPolicies::CMP0115) == cmPolicies::NEW &&
makefile.GetPolicyStatus(cmPolicies::CMP0187) == cmPolicies::NEW;
}
cmSourceFileLocation::cmSourceFileLocation(cmSourceFileLocation const& loc)
: Makefile(loc.Makefile)
@ -30,7 +38,12 @@ cmSourceFileLocation::cmSourceFileLocation(cmMakefile const* mf,
: Makefile(mf)
{
this->AmbiguousDirectory = !cmSystemTools::FileIsFullPath(name);
this->AmbiguousExtension = true;
// If ambiguous extensions are allowed then the extension is assumed to be
// ambiguous unless the name has an extension, in which case
// `UpdateExtension` will update this. If ambiguous extensions are not
// allowed, then set this to false as the file extension must be provided or
// the file doesn't have an extension.
this->AmbiguousExtension = !NoAmbiguousExtensions(*mf);
this->Directory = cmSystemTools::GetFilenamePath(name);
if (cmSystemTools::FileIsFullPath(this->Directory)) {
this->Directory = cmSystemTools::CollapseFullPath(this->Directory);

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1,3 @@
^CMake Deprecation Warning at CMP0187-NEW-CMP0115-OLD\.cmake:[0-9]+ \(cmake_policy\):
The OLD behavior for policy CMP0115 will be removed from a future version
of CMake\.

View File

@ -0,0 +1,4 @@
cmake_policy(SET CMP0115 OLD)
cmake_policy(SET CMP0187 NEW)
include(CMP0187.cmake)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,3 @@
cmake_policy(SET CMP0187 NEW)
include(CMP0187.cmake)

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1,3 @@
cmake_policy(SET CMP0187 OLD)
include(CMP0187.cmake)

View File

@ -0,0 +1,4 @@
add_custom_command(OUTPUT z.h COMMAND ${CMAKE_COMMAND} -E true)
add_custom_command(OUTPUT z COMMAND ${CMAKE_COMMAND} -E false)
add_library(lib INTERFACE z z.h)

View File

@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 4.0)
project(${RunCMake_TEST} NONE)
include(${RunCMake_TEST}.cmake)

View File

@ -0,0 +1,34 @@
include(RunCMake)
block()
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CMP0187-NEW-build)
run_cmake_with_options(CMP0187-NEW "-DCMAKE_POLICY_DEFAULT_CMP0187=NEW")
if(RunCMake_GENERATOR MATCHES "Ninja.*")
set(RunCMake_TEST_NO_CLEAN 1)
# -n: dry-run to avoid actually compiling, -v: verbose to capture executed command
run_cmake_command(CMP0187-NEW-build ${CMAKE_COMMAND} --build .)
endif()
endblock()
block()
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CMP0187-OLD-build)
run_cmake_with_options(CMP0187-OLD "-DCMAKE_POLICY_DEFAULT_CMP0187=OLD")
if(RunCMake_GENERATOR MATCHES "Ninja.*")
set(RunCMake_TEST_NO_CLEAN 1)
# -n: dry-run to avoid actually compiling, -v: verbose to capture executed command
run_cmake_command(CMP0187-OLD-build ${CMAKE_COMMAND} --build .)
endif()
endblock()
block()
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CMP0187-NEW-CMP0115-OLD-build)
run_cmake(CMP0187-NEW-CMP0115-OLD)
if(RunCMake_GENERATOR MATCHES "Ninja.*")
set(RunCMake_TEST_NO_CLEAN 1)
# -n: dry-run to avoid actually compiling, -v: verbose to capture executed command
run_cmake_command(CMP0187-NEW-CMP0115-OLD-build ${CMAKE_COMMAND} --build .)
endif()
endblock()

View File

@ -179,6 +179,7 @@ add_RunCMake_test(CMP0169)
add_RunCMake_test(CMP0170)
add_RunCMake_test(CMP0171)
add_RunCMake_test(CMP0173)
add_RunCMake_test(CMP0187)
# The test for Policy 65 requires the use of the
# CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS variable, which both the VS and Xcode