Jelajahi Sumber

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

CMP0115 requires that source files listed in CMake must include their
file extension, but there are cases when projects have different source
files with the same name, but one with an extension and one without.
In the current state, CMake will ignore the file without the extension
an always map it to the file with an extension.

```cmake
add_library(foo bar.c bar)
```

In the above example, the target `foo` will only recognize and depend on
`bar.c` and miss the file `bar` unless `bar` comes before `bar.c` in the
source list.
This issue also affects how custom commands emit files.

This change adds a new policy to recognize files with and without a file
extension as different files, both when building targets, and when they
are being created.

Fixes: #26058
Evan Wilde 1 tahun lalu
induk
melakukan
321c647640

+ 1 - 0
Help/manual/cmake-policies.7.rst

@@ -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 - 0
Help/policy/CMP0187.rst

@@ -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

+ 5 - 1
Source/cmPolicies.h

@@ -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)                                         \

+ 15 - 2
Source/cmSourceFileLocation.cxx

@@ -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);

+ 1 - 0
Tests/RunCMake/CMP0187/CMP0187-NEW-CMP0115-OLD-build-result.txt

@@ -0,0 +1 @@
+0

+ 3 - 0
Tests/RunCMake/CMP0187/CMP0187-NEW-CMP0115-OLD-stderr.txt

@@ -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\.

+ 4 - 0
Tests/RunCMake/CMP0187/CMP0187-NEW-CMP0115-OLD.cmake

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

+ 1 - 0
Tests/RunCMake/CMP0187/CMP0187-NEW-build-result.txt

@@ -0,0 +1 @@
+1

+ 3 - 0
Tests/RunCMake/CMP0187/CMP0187-NEW.cmake

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

+ 1 - 0
Tests/RunCMake/CMP0187/CMP0187-OLD-build-result.txt

@@ -0,0 +1 @@
+0

+ 3 - 0
Tests/RunCMake/CMP0187/CMP0187-OLD.cmake

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

+ 4 - 0
Tests/RunCMake/CMP0187/CMP0187.cmake

@@ -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)

+ 4 - 0
Tests/RunCMake/CMP0187/CMakeLists.txt

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

+ 34 - 0
Tests/RunCMake/CMP0187/RunCMakeTest.cmake

@@ -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()

+ 1 - 0
Tests/RunCMake/CMakeLists.txt

@@ -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