[compiler-rt][cmake] Factor out extend_install_path function
authorJohn Ericson <John.Ericson@Obsidian.Systems>
Tue, 14 Dec 2021 19:52:02 +0000 (14:52 -0500)
committerJohn Ericson <John.Ericson@Obsidian.Systems>
Thu, 30 Dec 2021 06:19:30 +0000 (06:19 +0000)
It is likely to become used again, if other projects want their own per-project
install directory variables. `install` is removed from the name since it is not inherently about installing.

Reviewed By: stephenneuendorffer

Differential Revision: https://reviews.llvm.org/D115746

cmake/Modules/ExtendPath.cmake [new file with mode: 0644]
compiler-rt/cmake/base-config-ix.cmake

diff --git a/cmake/Modules/ExtendPath.cmake b/cmake/Modules/ExtendPath.cmake
new file mode 100644 (file)
index 0000000..5db393a
--- /dev/null
@@ -0,0 +1,19 @@
+# Extend the path in `base_path` with the path in `current_segment`, returning
+# the result in `joined_path`. If `current_segment` is an absolute path then
+# just return it, in effect overriding `base_path`, and issue a warning.
+#
+# Note that the code returns a relative path (avoiding introducing leading
+# slashes) if `base_path` is empty.
+function(extend_path joined_path base_path current_segment)
+  if("${current_segment}" STREQUAL "")
+    set(temp_path "${base_path}")
+  elseif("${base_path}" STREQUAL "")
+    set(temp_path "${current_segment}")
+  elseif(IS_ABSOLUTE "${current_segment}")
+    message(WARNING "Since \"${current_segment}\" is absolute, it overrides install path: \"${base_path}\".")
+    set(temp_path "${current_segment}")
+  else()
+    set(temp_path "${base_path}/${current_segment}")
+  endif()
+  set(${joined_path} "${temp_path}" PARENT_SCOPE)
+endfunction()
index 447232e..1ada0ab 100644 (file)
@@ -5,6 +5,7 @@
 
 include(CheckIncludeFile)
 include(CheckCXXSourceCompiles)
+include(ExtendPath)
 
 check_include_file(unwind.h HAVE_UNWIND_H)
 
@@ -85,20 +86,6 @@ else()
   set(COMPILER_RT_TEST_COMPILER_ID GNU)
 endif()
 
-function(extend_install_path joined_path current_segment)
-  if("${current_segment}" STREQUAL "")
-    set(temp_path "${COMPILER_RT_INSTALL_PATH}")
-  elseif("${COMPILER_RT_INSTALL_PATH}" STREQUAL "")
-    set(temp_path "${current_segment}")
-  elseif(IS_ABSOLUTE "${current_segment}")
-    message(WARNING "Since \"${current_segment}\" is absolute, it overrides COMPILER_RT_INSTALL_PATH: \"${COMPILER_RT_INSTALL_PATH}\".")
-    set(temp_path "${current_segment}")
-  else()
-    set(temp_path "${COMPILER_RT_INSTALL_PATH}/${current_segment}")
-  endif()
-  set(${joined_path} "${temp_path}" PARENT_SCOPE)
-endfunction()
-
 if(NOT DEFINED COMPILER_RT_OS_DIR)
   if(ANDROID)
     # The CMAKE_SYSTEM_NAME for Android is Android, but the OS is Linux and the
@@ -111,23 +98,23 @@ endif()
 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
   set(COMPILER_RT_OUTPUT_LIBRARY_DIR
     ${COMPILER_RT_OUTPUT_DIR}/lib)
-  extend_install_path(default_install_path lib)
+  extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" lib)
   set(COMPILER_RT_INSTALL_LIBRARY_DIR "${default_install_path}" CACHE PATH
     "Path where built compiler-rt libraries should be installed.")
 else(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
   set(COMPILER_RT_OUTPUT_LIBRARY_DIR
     ${COMPILER_RT_OUTPUT_DIR}/lib/${COMPILER_RT_OS_DIR})
-  extend_install_path(default_install_path "lib/${COMPILER_RT_OS_DIR}")
+  extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" "lib/${COMPILER_RT_OS_DIR}")
   set(COMPILER_RT_INSTALL_LIBRARY_DIR "${default_install_path}" CACHE PATH
     "Path where built compiler-rt libraries should be installed.")
 endif()
-extend_install_path(default_install_path bin)
+extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" bin)
 set(COMPILER_RT_INSTALL_BINARY_DIR "${default_install_path}" CACHE PATH
   "Path where built compiler-rt executables should be installed.")
-extend_install_path(default_install_path include)
+extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" include)
 set(COMPILER_RT_INSTALL_INCLUDE_DIR "${default_install_path}" CACHE PATH
   "Path where compiler-rt headers should be installed.")
-extend_install_path(default_install_path share)
+extend_path(default_install_path "${COMPILER_RT_INSTALL_PATH}" share)
 set(COMPILER_RT_INSTALL_DATA_DIR "${default_install_path}" CACHE PATH
   "Path where compiler-rt data files should be installed.")