浏览代码

Source: Add cm_fileno wrapper

And use it where appropriate.
Kyle Edwards 2 年之前
父节点
当前提交
fbdb1fd843
共有 5 个文件被更改,包括 34 次插入14 次删除
  1. 2 0
      Source/CMakeLists.txt
  2. 15 0
      Source/cm_fileno.cxx
  3. 7 0
      Source/cm_fileno.hxx
  4. 6 13
      Source/cmake.cxx
  5. 4 1
      Tests/CMakeLib/testUVProcessChain.cxx

+ 2 - 0
Source/CMakeLists.txt

@@ -738,6 +738,8 @@ add_library(
   cm_utf8.c
   cm_codecvt.hxx
   cm_codecvt.cxx
+  cm_fileno.hxx
+  cm_fileno.cxx
 
   cmDuration.h
   cmDuration.cxx

+ 15 - 0
Source/cm_fileno.cxx

@@ -0,0 +1,15 @@
+/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
+   file Copyright.txt or https://cmake.org/licensing for details.  */
+#if !defined(_POSIX_C_SOURCE) && !defined(_WIN32) && !defined(__sun) &&       \
+  !defined(__OpenBSD__)
+/* POSIX APIs are needed */
+// NOLINTNEXTLINE(bugprone-reserved-identifier)
+#  define _POSIX_C_SOURCE 200809L
+#endif
+
+#include "cm_fileno.hxx"
+
+int cm_fileno(FILE* f)
+{
+  return fileno(f);
+}

+ 7 - 0
Source/cm_fileno.hxx

@@ -0,0 +1,7 @@
+/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
+   file Copyright.txt or https://cmake.org/licensing for details.  */
+#pragma once
+
+#include <cstdio>
+
+int cm_fileno(FILE* f);

+ 6 - 13
Source/cmake.cxx

@@ -23,14 +23,11 @@
 #include <cmext/algorithm>
 #include <cmext/string_view>
 
-#if !defined(CMAKE_BOOTSTRAP) && !defined(_WIN32)
-#  include <unistd.h>
-#endif
-
 #include "cmsys/FStream.hxx"
 #include "cmsys/Glob.hxx"
 #include "cmsys/RegularExpression.hxx"
 
+#include "cm_fileno.hxx"
 #include "cm_sys_stat.h"
 
 #include "cmBuildOptions.h"
@@ -3913,15 +3910,11 @@ std::function<int()> cmake::BuildWorkflowStep(
   const std::vector<std::string>& args)
 {
   cmUVProcessChainBuilder builder;
-  builder
-    .AddCommand(args)
-#  ifdef _WIN32
-    .SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, _fileno(stdout))
-    .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, _fileno(stderr));
-#  else
-    .SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, STDOUT_FILENO)
-    .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, STDERR_FILENO);
-#  endif
+  builder.AddCommand(args)
+    .SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT,
+                       cm_fileno(stdout))
+    .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR,
+                       cm_fileno(stderr));
   return [builder]() -> int {
     auto chain = builder.Start();
     chain.Wait();

+ 4 - 1
Tests/CMakeLib/testUVProcessChain.cxx

@@ -12,6 +12,8 @@
 
 #include <cm3p/uv.h>
 
+#include "cm_fileno.hxx"
+
 #include "cmGetPipes.h"
 #include "cmStringAlgorithms.h"
 #include "cmUVHandlePtr.h"
@@ -630,7 +632,8 @@ bool testUVProcessChainInputFile(const char* helperCommand)
 
   cmUVProcessChainBuilder builder;
   builder.AddCommand({ helperCommand, "dedup" })
-    .SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT, fileno(f.get()))
+    .SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT,
+                       cm_fileno(f.get()))
     .SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT);
 
   auto chain = builder.Start();