From d5d39d3adba9f68b7c2e83920230102adb172c23 Mon Sep 17 00:00:00 2001
From: root <root@localhost.localdomain>
Date: Thu, 3 Dec 2020 17:03:47 +0800
Subject: [PATCH] cmake aarch64-ilp32 support

Reference:https://build.opensuse.org/package/view_file/devel:ARM:Factory:Contrib:ILP32/cmake/aarch64-ilp32.patch?expand=1
Conflict:Contextual adaptation

---
 Modules/CMakeCompilerABI.h       |  5 +++++
 Modules/FindGTK2.cmake           |  2 ++
 Modules/FindJNI.cmake            |  4 ++++
 Modules/FindPkgConfig.cmake      |  4 ++++
 Modules/GNUInstallDirs.cmake     |  5 +++++
 Modules/Platform/UnixPaths.cmake |  3 ++-
 Source/cmFindLibraryCommand.cxx  |  6 ++++++
 Source/cmFindPackageCommand.cxx  | 11 +++++++++++
 Source/cmFindPackageCommand.h    |  1 +
 Source/cmMakefile.cxx            | 13 +++++++++++++
 Source/cmMakefile.h              |  3 +++
 11 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/Modules/CMakeCompilerABI.h b/Modules/CMakeCompilerABI.h
index 45532af..38545c9 100644
--- a/Modules/CMakeCompilerABI.h
+++ b/Modules/CMakeCompilerABI.h
@@ -24,6 +24,11 @@ const char info_sizeof_dptr[] = {
   defined(__ILP32__)
 #  define ABI_ID "ELF X32"
 
+#elif defined(__ELF__) && defined(__aarch64__) && defined(__LP64__)
+#  define ABI_ID "ELF LP64"
+#elif defined(__ELF__) && defined(__aarch64__) && defined(__ILP32__)
+#  define ABI_ID "ELF ILP32"
+
 #elif defined(__ELF__)
 #  define ABI_ID "ELF"
 #endif
diff --git a/Modules/FindGTK2.cmake b/Modules/FindGTK2.cmake
index 83091f3..9ed1f4d 100644
--- a/Modules/FindGTK2.cmake
+++ b/Modules/FindGTK2.cmake
@@ -293,9 +293,11 @@ function(_GTK2_FIND_INCLUDE_DIR _var _hdr)
         PATHS
             ${_gtk2_arch_dir}
             /usr/local/libx32
+	    /usr/local/libilp32
             /usr/local/lib64
             /usr/local/lib
             /usr/libx32
+	    /usr/libilp32
             /usr/lib64
             /usr/lib
             /opt/gnome/include
diff --git a/Modules/FindJNI.cmake b/Modules/FindJNI.cmake
index 3a5bd31..04fe966 100644
--- a/Modules/FindJNI.cmake
+++ b/Modules/FindJNI.cmake
@@ -64,6 +64,8 @@ macro(java_append_library_directories _var)
     elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
         # Subdir is "arm" for both big-endian (arm) and little-endian (armel).
         set(_java_libarch "arm" "aarch32")
+    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch64")
+        set(_java_libarch "aarch64" "aarch64_ilp32")
     elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^mips")
         # mips* machines are bi-endian mostly so processor does not tell
         # endianness of the underlying system.
@@ -216,6 +218,8 @@ set(_JNI_JAVA_DIRECTORIES_BASE
   # SuSE specific paths for default JVM
   /usr/lib64/jvm/java
   /usr/lib64/jvm/jre
+  /usr/libilp32/jvm/java
+  /usr/libilp32/jvm/jre
   )
 
 set(_JNI_JAVA_AWT_LIBRARY_TRIES)
diff --git a/Modules/FindPkgConfig.cmake b/Modules/FindPkgConfig.cmake
index bd1bc7c..4773d99 100644
--- a/Modules/FindPkgConfig.cmake
+++ b/Modules/FindPkgConfig.cmake
@@ -317,6 +317,10 @@ macro(_pkg_set_path_internal)
         if(uselibx32 AND CMAKE_INTERNAL_PLATFORM_ABI STREQUAL "ELF X32")
           list(APPEND _lib_dirs "libx32/pkgconfig")
         endif()
+        get_property(uselibilp32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIBILP32_PATHS)
+        if(uselibilp32 AND CMAKE_INTERNAL_PLATFORM_ABI STREQUAL "ELF ILP32")
+          list(APPEND _lib_dirs "libilp32/pkgconfig")
+        endif()
       endif()
     endif()
     if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND NOT CMAKE_CROSSCOMPILING)
diff --git a/Modules/GNUInstallDirs.cmake b/Modules/GNUInstallDirs.cmake
index f95e6e2..003e8cc 100644
--- a/Modules/GNUInstallDirs.cmake
+++ b/Modules/GNUInstallDirs.cmake
@@ -245,6 +245,11 @@ if(NOT DEFINED CMAKE_INSTALL_LIBDIR OR (_libdir_set
           if(DEFINED _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX)
             set(__LAST_LIBDIR_DEFAULT "lib64")
           endif()
+        elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch64")
+          set(_LIBDIR_DEFAULT "libilp32")
+          if(DEFINED _GNUInstallDirs_LAST_CMAKE_INSTALL_PREFIX)
+            set(__LAST_LIBDIR_DEFAULT "libilp32")
+          endif()
         endif()
       endif()
     endif()
diff --git a/Modules/Platform/UnixPaths.cmake b/Modules/Platform/UnixPaths.cmake
index 97f744d..9acb7b3 100644
--- a/Modules/Platform/UnixPaths.cmake
+++ b/Modules/Platform/UnixPaths.cmake
@@ -60,7 +60,7 @@ list(APPEND CMAKE_SYSTEM_LIBRARY_PATH
   )
 
 list(APPEND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES
-  /lib /lib32 /lib64 /usr/lib /usr/lib32 /usr/lib64
+  /lib /lib32 /lib64 /libilp32 /usr/lib /usr/lib32 /usr/lib64 /usr/libilp32
   )
 
 if(CMAKE_SYSROOT_COMPILE)
@@ -90,3 +90,4 @@ unset(_cmake_sysroot_compile)
 set_property(GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS TRUE)
 set_property(GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS TRUE)
 set_property(GLOBAL PROPERTY FIND_LIBRARY_USE_LIBX32_PATHS TRUE)
+set_property(GLOBAL PROPERTY FIND_LIBRARY_USE_LIBILP32_PATHS TRUE)
diff --git a/Source/cmFindLibraryCommand.cxx b/Source/cmFindLibraryCommand.cxx
index 20221b1..b73b1c1 100644
--- a/Source/cmFindLibraryCommand.cxx
+++ b/Source/cmFindLibraryCommand.cxx
@@ -70,6 +70,12 @@ bool cmFindLibraryCommand::InitialPass(std::vector<std::string> const& argsIn)
              "FIND_LIBRARY_USE_LIBX32_PATHS")) {
     this->AddArchitecturePaths("x32");
   }
+  // add special 32 bit paths if this is an ilp32 compile.
+  else if (this->Makefile->PlatformIsilp32() &&
+           this->Makefile->GetState()->GetGlobalPropertyAsBool(
+             "FIND_LIBRARY_USE_LIBILP32_PATHS")) {
+    this->AddArchitecturePaths("ilp32");
+  }
 
   std::string const library = this->FindLibrary();
   if (!library.empty()) {
diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx
index 2b11b62..deb5f1e 100644
--- a/Source/cmFindPackageCommand.cxx
+++ b/Source/cmFindPackageCommand.cxx
@@ -100,6 +100,7 @@ cmFindPackageCommand::cmFindPackageCommand(cmExecutionStatus& status)
   this->UseLib32Paths = false;
   this->UseLib64Paths = false;
   this->UseLibx32Paths = false;
+  this->UseLibilp32Paths = false;
   this->UseRealPath = false;
   this->PolicyScope = true;
   this->VersionMajor = 0;
@@ -190,6 +191,13 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
     this->UseLibx32Paths = true;
   }
 
+  // Lookup whether libilp32 paths should be used.
+  if (this->Makefile->PlatformIsilp32() &&
+      this->Makefile->GetState()->GetGlobalPropertyAsBool(
+        "FIND_LIBRARY_USE_LIBILP32_PATHS")) {
+    this->UseLibilp32Paths = true;
+  }
+
   // Check if User Package Registry should be disabled
   // The `CMAKE_FIND_USE_PACKAGE_REGISTRY` has
   // priority over the deprecated CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY
@@ -2097,6 +2105,9 @@ bool cmFindPackageCommand::SearchPrefix(std::string const& prefix_in)
   if (this->UseLibx32Paths) {
     common.emplace_back("libx32");
   }
+  if (this->UseLibilp32Paths) {
+    common.emplace_back("libilp32");
+  }
   common.emplace_back("lib");
   common.emplace_back("share");
 
diff --git a/Source/cmFindPackageCommand.h b/Source/cmFindPackageCommand.h
index 85fe7b6..f254581 100644
--- a/Source/cmFindPackageCommand.h
+++ b/Source/cmFindPackageCommand.h
@@ -178,6 +178,7 @@ private:
   bool UseLib32Paths;
   bool UseLib64Paths;
   bool UseLibx32Paths;
+  bool UseLibilp32Paths;
   bool UseRealPath;
   bool PolicyScope;
   std::string LibraryArchitecture;
diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx
index f143ef7..8b5aee4 100644
--- a/Source/cmMakefile.cxx
+++ b/Source/cmMakefile.cxx
@@ -2569,6 +2569,9 @@ bool cmMakefile::PlatformIs32Bit() const
     if (strcmp(plat_abi, "ELF X32") == 0) {
       return false;
     }
+    if (strcmp(plat_abi, "ELF ILP32") == 0) {
+      return false;
+    }
   }
   if (const char* sizeof_dptr = this->GetDefinition("CMAKE_SIZEOF_VOID_P")) {
     return atoi(sizeof_dptr) == 4;
@@ -2595,6 +2598,16 @@ bool cmMakefile::PlatformIsx32() const
   return false;
 }
 
+bool cmMakefile::PlatformIsilp32() const
+{
+  if (const char* plat_abi = this->GetDefinition("CMAKE_INTERNAL_PLATFORM_ABI")) {
+    if (strcmp(plat_abi, "ELF ILP32") == 0) {
+      return true;
+    }
+  }
+  return false;
+}
+
 cmMakefile::AppleSDK cmMakefile::GetAppleSDKType() const
 {
   std::string sdkRoot;
diff --git a/Source/cmMakefile.h b/Source/cmMakefile.h
index 6e59494..615e291 100644
--- a/Source/cmMakefile.h
+++ b/Source/cmMakefile.h
@@ -511,6 +511,9 @@ public:
   /** Return whether the target platform is x32.  */
   bool PlatformIsx32() const;
 
+  /** Return whether the target platform is ilp32.  */
+  bool PlatformIsilp32() const;
+
   /** Apple SDK Type */
   enum class AppleSDK
   {
-- 
2.23.0

