From 19258e2179e15f9cbefaf68c72e1bf44154a4834 Mon Sep 17 00:00:00 2001
From: Ranjit Ranjan <ranjit.sastra@gmail.com>
Date: Mon, 6 May 2024 13:34:30 -0500
Subject: [PATCH] aix support

---
 include/xgboost/collective/socket.h |  2 +-
 python-package/setup.py             |  2 ++
 python-package/xgboost/core.py      |  5 ++++-
 python-package/xgboost/libpath.py   |  2 ++
 tests/cpp/CMakeLists.txt            | 11 +++++++++++
 5 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/include/xgboost/collective/socket.h b/include/xgboost/collective/socket.h
index 33d14fe8c..ae69ba683 100644
--- a/include/xgboost/collective/socket.h
+++ b/include/xgboost/collective/socket.h
@@ -287,7 +287,7 @@ class TCPSocket {
 #elif defined(__APPLE__)
     return domain_;
 #elif defined(__unix__)
-#ifndef __PASE__
+#if defined (__PASE__) && not defined(_AIX)
     std::int32_t domain;
     socklen_t len = sizeof(domain);
     xgboost_CHECK_SYS_CALL(
diff --git a/python-package/setup.py b/python-package/setup.py
index 8a1b1b709..2e7ba8426 100644
--- a/python-package/setup.py
+++ b/python-package/setup.py
@@ -46,6 +46,8 @@ def lib_name() -> str:
         name = 'xgboost.dll'
     elif system() == 'OS400':
         name = 'libxgboost.so'
+    elif system() == 'AIX':
+        name = 'libxgboost.a'
     return name
 
 
diff --git a/python-package/xgboost/core.py b/python-package/xgboost/core.py
index 8a877ec5a..21e0367c3 100644
--- a/python-package/xgboost/core.py
+++ b/python-package/xgboost/core.py
@@ -203,7 +203,10 @@ def _load_lib() -> ctypes.CDLL:
             os.environ["PATH"] = os.pathsep.join(
                 pathBackup + [os.path.dirname(lib_path)]
             )
-            lib = ctypes.cdll.LoadLibrary(lib_path)
+            if sys.platform == 'aix':
+                lib = ctypes.cdll.LoadLibrary(lib_path + '(libxgboost.so)')
+            else:
+                lib = ctypes.cdll.LoadLibrary(lib_path)
             setattr(lib, "path", os.path.normpath(lib_path))
             lib_success = True
         except OSError as e:
diff --git a/python-package/xgboost/libpath.py b/python-package/xgboost/libpath.py
index 1ae77556e..c38e657a7 100644
--- a/python-package/xgboost/libpath.py
+++ b/python-package/xgboost/libpath.py
@@ -49,6 +49,8 @@ def find_lib_path() -> List[str]:
         dll_path = [os.path.join(p, 'libxgboost.dylib') for p in dll_path]
     elif sys.platform == 'cygwin':
         dll_path = [os.path.join(p, 'cygxgboost.dll') for p in dll_path]
+    elif sys.platform == 'aix':
+        dll_path = [os.path.join(p, 'libxgboost.a') for p in dll_path]
     if platform.system() == 'OS400':
         dll_path = [os.path.join(p, 'libxgboost.so') for p in dll_path]
 
diff --git a/tests/cpp/CMakeLists.txt b/tests/cpp/CMakeLists.txt
index 51cdecd9d..874685d42 100644
--- a/tests/cpp/CMakeLists.txt
+++ b/tests/cpp/CMakeLists.txt
@@ -43,6 +43,17 @@ target_link_libraries(testxgboost
   PRIVATE
   ${GTEST_LIBRARIES})
 
+if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
+  target_link_options(testxgboost 
+    PRIVATE 
+    -Wl,-bkeepfile:${CMAKE_CURRENT_BINARY_DIR}/../../CMakeFiles/testxgboost.dir/tests/cpp/test_learner.cc.o  
+    -Wl,-bkeepfile:${CMAKE_CURRENT_BINARY_DIR}/../../CMakeFiles/testxgboost.dir/tests/cpp/objective/test_objective.cc.o  
+    -Wl,-bkeepfile:${CMAKE_CURRENT_BINARY_DIR}/../../CMakeFiles/testxgboost.dir/tests/cpp/metric/test_elementwise_metric.cc.o  
+    -Wl,-bkeepfile:${CMAKE_CURRENT_BINARY_DIR}/../../CMakeFiles/testxgboost.dir/tests/cpp/metric/test_metric.cc.o 
+    -Wl,-bkeepfile:${CMAKE_CURRENT_BINARY_DIR}/../../CMakeFiles/testxgboost.dir/tests/cpp/metric/test_multiclass_metric.cc.o 
+    -Wl,-bkeepfile:${CMAKE_CURRENT_BINARY_DIR}/../../CMakeFiles/testxgboost.dir/tests/cpp/metric/test_rank_metric.cc.o)
+endif()
+
 set_output_directory(testxgboost ${xgboost_BINARY_DIR})
 
 # This grouping organises source files nicely in visual studio
-- 
2.41.0

