Skip to content

Commit 4c1847c

Browse files
committed
Extract the install prefix from the shared library.
If we detect a static build don't try to provide a prefix. This is part of a multi-project effort, a similar PR will be created in OpenPMIX and OMPI. The goal of each of these changes is the same: instead of using build-time generated prefix that ignore a project rebase, take the prefix from the shared library of each project and derive the necessary paths from it. The user can however overwrite this using the environment variables, and the configuration files. Signed-off-by: George Bosilca <[email protected]>
1 parent 9fbd849 commit 4c1847c

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#
2+
# Copyright (c) 2025 NVIDIA Corporation. All rights reserved.
3+
# $COPYRIGHT$
4+
#
5+
# Additional copyrights may follow
6+
#
7+
# $HEADER$
8+
#
9+
10+
noinst_LTLIBRARIES = libmca_installdirs_runtime.la
11+
12+
libmca_installdirs_runtime_la_SOURCES = \
13+
opal_installdirs_runtime.c
14+
libmca_installdirs_runtime_la_CPPFLAGS = -DOPAL_LIB_NAME=\"@OPAL_LIB_NAME@\"
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- shell-script -*-
2+
#
3+
# Copyright (c) 2025 NVIDIA Corporation. All rights reserved.
4+
# $COPYRIGHT$
5+
#
6+
# Additional copyrights may follow
7+
#
8+
# $HEADER$
9+
#
10+
11+
AC_DEFUN([MCA_opal_installdirs_runtime_PRIORITY], [5])
12+
13+
AC_DEFUN([MCA_opal_installdirs_runtime_COMPILE_MODE], [
14+
AC_MSG_CHECKING([for MCA component $2:$3 compile mode])
15+
$4="static"
16+
AC_MSG_RESULT([$$4])
17+
])
18+
19+
# MCA_installdirs_config_CONFIG(action-if-can-compile,
20+
# [action-if-cant-compile])
21+
# ------------------------------------------------
22+
AC_DEFUN([MCA_opal_installdirs_runtime_CONFIG], [
23+
# Check if we are building a shared library or not. Disable if static
24+
AC_MSG_CHECKING([if shared libraries are enabled])
25+
AS_IF([test "$enable_shared" != "yes"],
26+
[installdirs_runtime_happy="no"],
27+
[installdirs_runtime_happy="yes"])
28+
AC_MSG_RESULT([$installdirs_runtime_happy])
29+
30+
# Check if dladdr is available
31+
AS_IF([test "$installdirs_runtime_happy" = "yes"],
32+
[AC_CHECK_HEADERS([dlfcn.h],
33+
[],
34+
[installdirs_runtime_happy="no"])])
35+
AS_IF([test "$installdirs_runtime_happy" = "yes"],
36+
[AC_CHECK_LIB([dl], [dladdr],
37+
[],
38+
[installdirs_runtime_happy="no"])
39+
])
40+
#
41+
AS_IF([test "$installdirs_runtime_happy" = "yes"],
42+
[AC_CONFIG_FILES([opal/mca/installdirs/runtime/Makefile])
43+
$1], [$2])
44+
])
45+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2006-2007 Los Alamos National Security, LLC. All rights
3+
* reserved.
4+
* Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
5+
* $COPYRIGHT$
6+
*
7+
* Additional copyrights may follow
8+
*
9+
* $HEADER$
10+
*/
11+
12+
#include "opal_config.h"
13+
14+
#include <stdlib.h>
15+
#include <string.h>
16+
17+
#include "opal/constants.h"
18+
#include "opal/mca/installdirs/installdirs.h"
19+
#include "opal/runtime/opal.h"
20+
21+
static int installdirs_runtime_open(void);
22+
23+
opal_installdirs_base_component_t mca_installdirs_runtime_component = {
24+
/* First, the mca_component_t struct containing meta information
25+
about the component itself */
26+
{OPAL_INSTALLDIRS_BASE_VERSION_2_0_0,
27+
28+
/* Component name and version */
29+
"runtime", OPAL_MAJOR_VERSION, OPAL_MINOR_VERSION, OPAL_RELEASE_VERSION,
30+
31+
/* Component open and close functions */
32+
installdirs_runtime_open, NULL},
33+
{/* This component is checkpointable */
34+
MCA_BASE_METADATA_PARAM_CHECKPOINT},
35+
36+
/* Next the opal_install_dirs_t install_dirs_data information */
37+
{
38+
NULL,
39+
},
40+
};
41+
42+
#include <dlfcn.h>
43+
#include "opal/util/basename.h"
44+
45+
/**
46+
* We are trying to solve a particular use case here, when the entire install tree
47+
* of Open MPI (and its dependencies) has been moved into another location. Nothing
48+
* fancy, the entire install tree has maintained his shape but changed the prefix.
49+
*/
50+
static int installdirs_runtime_open(void)
51+
{
52+
Dl_info info;
53+
void* opal_fct;
54+
55+
/* Casting from void* to fct pointer according to POSIX.1-2001 and POSIX.1-2008 */
56+
*(void **)&opal_fct = dlsym(RTLD_DEFAULT, "opal_init_util");
57+
58+
if( 0 == dladdr(opal_fct, &info) ) {
59+
/* Can't find the symbol */
60+
return OPAL_ERROR;
61+
}
62+
63+
/* If this build was both static and shared then this compoenent will be build and will exists
64+
* even in the static library. We need to prevent setting a prefix for the OMPI library that
65+
* is actually the application path. Check, if the name points to a library.
66+
*/
67+
char* libname = opal_basename(info.dli_fname);
68+
if( strncmp(libname, "lib", 3)) { /* not a shared library */
69+
free(libname);
70+
return OPAL_ERROR;
71+
}
72+
#if defined(OPAL_LIB_NAME)
73+
/* Extra check using the installed name of the OPAL library */
74+
if( strncmp(libname+3, OPAL_LIB_NAME, strlen(OPAL_LIB_NAME)) ) { /* not a shared library */
75+
free(libname);
76+
return OPAL_ERROR;
77+
}
78+
#endif /* defined(OPAL_LIB_NAME) */
79+
/* Remove the shared library name and it's first dirname to obtain a prefix. This
80+
* is true in most cases, especially when the install directory was just moved
81+
* moved around, but it is not necessarily always true.
82+
*/
83+
char* dname = opal_dirname(info.dli_fname);
84+
char* prefix = opal_dirname(dname);
85+
86+
free(libname);
87+
free(dname);
88+
89+
mca_installdirs_runtime_component.install_dirs_data.prefix = prefix;
90+
91+
return OPAL_SUCCESS;
92+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#
2+
# owner/status file
3+
# owner: institution that is responsible for this package
4+
# status: e.g. active, maintenance, unmaintained
5+
#
6+
owner: NVIDIA
7+
status: active

0 commit comments

Comments
 (0)