ianmcook commented on a change in pull request #9553:
URL: https://github.com/apache/arrow/pull/9553#discussion_r587870368



##########
File path: cpp/cmake_modules/Usevcpkg.cmake
##########
@@ -0,0 +1,214 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+message(STATUS "Using vcpkg to find dependencies")
+
+# ----------------------------------------------------------------------
+# Define macros
+
+# macro to list subdirectirectories (non-recursive)
+macro(list_subdirs SUBDIRS DIR)
+  file(GLOB children_ RELATIVE ${DIR} ${DIR}/*)
+  set(subdirs_ "")
+  foreach(child_ ${children_})
+    if(IS_DIRECTORY "${DIR}/${child_}")
+      list(APPEND subdirs_ ${child_})
+    endif()
+  endforeach()
+  set("${SUBDIRS}" ${subdirs_})
+  unset(children_)
+  unset(subdirs_)
+endmacro()
+
+# ----------------------------------------------------------------------
+# Get VCPKG_ROOT
+
+if(DEFINED CMAKE_TOOLCHAIN_FILE)
+  # Get it from the CMake variable CMAKE_TOOLCHAIN_FILE
+  get_filename_component(_VCPKG_DOT_CMAKE "${CMAKE_TOOLCHAIN_FILE}" NAME)
+  if(EXISTS "${CMAKE_TOOLCHAIN_FILE}" AND _VCPKG_DOT_CMAKE STREQUAL 
"vcpkg.cmake")
+    get_filename_component(_VCPKG_BUILDSYSTEMS_DIR "${CMAKE_TOOLCHAIN_FILE}" 
DIRECTORY)
+    get_filename_component(VCPKG_ROOT "${_VCPKG_BUILDSYSTEMS_DIR}/../.." 
ABSOLUTE)
+  else()
+    message(
+      FATAL_ERROR
+        "vcpkg toolchain file not found at path specified in 
-DCMAKE_TOOLCHAIN_FILE")
+  endif()
+else()
+  if(DEFINED VCPKG_ROOT)
+    # Get it from the CMake variable VCPKG_ROOT
+    find_program(_VCPKG_BIN vcpkg PATHS "${VCPKG_ROOT}" NO_DEFAULT_PATH)
+    if(NOT _VCPKG_BIN)
+      message(FATAL_ERROR "vcpkg not found in directory specified in 
-DVCPKG_ROOT")
+    endif()
+  elseif(DEFINED ENV{VCPKG_ROOT})
+    # Get it from the environment variable VCPKG_ROOT
+    set(VCPKG_ROOT ENV{VCPKG_ROOT})
+    find_program(_VCPKG_BIN vcpkg PATHS "${VCPKG_ROOT}" NO_DEFAULT_PATH)
+    if(NOT _VCPKG_BIN)
+      message(
+        FATAL_ERROR "vcpkg not found in directory in environment variable 
VCPKG_ROOT")
+    endif()
+  else()
+    # Get it from the file vcpkg.path.txt
+    find_program(_VCPKG_BIN vcpkg)
+    if(_VCPKG_BIN)
+      get_filename_component(_VCPKG_REAL_BIN "${_VCPKG_BIN}" REALPATH)
+      get_filename_component(VCPKG_ROOT "${_VCPKG_REAL_BIN}" DIRECTORY)
+    else()
+      if(CMAKE_HOST_WIN32)
+        set(_VCPKG_PATH_TXT "$ENV{LOCALAPPDATA}/vcpkg/vcpkg.path.txt")
+      else()
+        set(_VCPKG_PATH_TXT "$ENV{HOME}/.vcpkg/vcpkg.path.txt")
+      endif()
+      if(EXISTS "${_VCPKG_PATH_TXT}")
+        file(STRINGS "${_VCPKG_PATH_TXT}" VCPKG_ROOT)
+      else()
+        message(
+          FATAL_ERROR
+            "vcpkg not found. Install vcpkg if not installed, "
+            "then run vcpkg integrate install or set environment variable 
VCPKG_ROOT.")
+      endif()
+      find_program(_VCPKG_BIN vcpkg PATHS "${VCPKG_ROOT}" NO_DEFAULT_PATH)
+      if(NOT _VCPKG_BIN)
+        message(FATAL_ERROR "vcpkg not found. Re-run vcpkg integrate install "
+                            "or set environment variable VCPKG_ROOT.")
+      endif()
+    endif()
+  endif()
+  set(CMAKE_TOOLCHAIN_FILE
+      "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
+      CACHE STRING "Path to vcpkg CMake toolchain file")

Review comment:
       `CACHE` is necessary here and and in many of the other places where I 
used it in this file. I initially attempted to cache none of the variables, but 
that caused numerous errors in the CMake scripts, in vcpkg, and in the build. I 
experimented with using `PARENT_SCOPE` but that did not solve most of the 
problems, so I used `CACHE` for all the variables whose scope of interaction 
extends beyond this one script.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to