1
1

Add some new support functions to the unit test library -- for

opening, examining, and closing components.

This commit was SVN r5139.
Этот коммит содержится в:
Jeff Squyres 2005-04-01 22:13:05 +00:00
родитель 9cbb698ada
Коммит 842a4e904e
3 изменённых файлов: 274 добавлений и 0 удалений

Просмотреть файл

@ -16,6 +16,12 @@
include $(top_srcdir)/config/Makefile.options
AM_CFLAGS = -g
AM_CPPFLAGS = \
-I$(top_srcdir)/test/support -DOMPI_ENABLE_DEBUG_OVERRIDE=1 \
-DBUILDDIR=\"$(OMPI_TOP_BUILDDIR)\" \
-DSRCDIR=\"$(OMPI_TOP_SRCDIR)\"
#
# This should be libsupport.a, not libsupport.la. Automake doesn't
# support check_LTLIBRARIES, as technically you have to install a
@ -24,5 +30,7 @@ include $(top_srcdir)/config/Makefile.options
check_LIBRARIES = libsupport.a
libsupport_a_SOURCES = \
components.h \
components.c \
support.c \
support.h

181
test/support/components.c Обычный файл
Просмотреть файл

@ -0,0 +1,181 @@
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University.
* All rights reserved.
* Copyright (c) 2004-2005 The Trustees of the University of Tennessee.
* All rights reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "ompi_config.h"
#include "include/constants.h"
#include "libltdl/ltdl.h"
#include "components.h"
#include "mca/mca.h"
/*
* Local functions
*/
static bool try_dlopen(const char *dir, const char *fw, const char *comp,
test_component_handle_t *comp_handle,
test_component_sym_t *comp_symbol);
static char *dir_concat(const char *a, const char *b);
int test_component_open(const char *framework, const char *component,
test_component_handle_t *comp_handle,
mca_base_component_t **mca)
{
test_component_sym_t sym;
if (NULL == framework || NULL == component || NULL == comp_handle ||
NULL == mca) {
return OMPI_ERR_BAD_PARAM;
}
comp_handle->tch_handle = NULL;
sym.tcs_variable = NULL;
/* It's ok to call lt_dlinit() multiple times; it ref counts for
lt_dlexit() */
lt_dlinit();
/* Try to open */
if (try_dlopen(".", framework, component, comp_handle, &sym) ||
try_dlopen(dir_concat(BUILDDIR, "src/mca"), framework,
component, comp_handle, &sym) ||
try_dlopen(dir_concat(SRCDIR, "src/mca"), framework,
component, comp_handle, &sym)) {
/* Ok, dlopen'ed it. Now call the component's open
function. */
*mca = (mca_base_component_t*) sym.tcs_variable;
if (NULL == (*mca)->mca_open_component ||
(NULL != (*mca)->mca_open_component &&
OMPI_SUCCESS == (*mca)->mca_open_component())) {
return OMPI_SUCCESS;
}
/* Badness occurred, so dlclose the component */
test_component_close(comp_handle);
}
/* Didn't find it / unable to open it */
return OMPI_ERROR;
}
int test_component_find_symbol(const char *name,
test_component_handle_t *handle,
test_component_sym_t *sym)
{
if (NULL == handle || NULL == sym) {
return OMPI_ERR_BAD_PARAM;
}
sym->tcs_function = (void (*)(void)) lt_dlsym(handle->tch_handle, name);
return OMPI_SUCCESS;
}
int test_component_close(test_component_handle_t *handle)
{
/* Note that lt_dlinit() refcounts, so ld_dlexit() only actually
shuts down if we've called it as many times as we've called
lt_dlinit() */
if (NULL != handle && NULL != handle->tch_handle) {
lt_dlclose(handle->tch_handle);
lt_dlexit();
handle->tch_handle = NULL;
}
return OMPI_SUCCESS;
}
static bool try_dlopen(const char *dir, const char *fw, const char *comp,
test_component_handle_t *comp_handle,
test_component_sym_t *comp_symbol)
{
char component_name[BUFSIZ];
char file_name[BUFSIZ];
#ifdef WIN32
char dirsep = '\\';
#else
char dirsep = '/';
#endif
component_name[BUFSIZ - 1] = '\0';
snprintf(component_name, BUFSIZ - 1, "mca_%s_%s_component", fw, comp);
if ('\0' != component_name[BUFSIZ - 1]) {
return false;
}
/* First, look for the component symbol statically */
comp_handle->tch_handle = lt_dlopen(NULL);
if (NULL != comp_handle->tch_handle) {
comp_symbol->tcs_variable =
lt_dlsym(comp_handle->tch_handle, component_name);
if (NULL != comp_symbol->tcs_variable) {
return true;
}
lt_dlclose(comp_handle->tch_handle);
}
/* Not in self -- look for a real component */
file_name[BUFSIZ - 1] = '\0';
snprintf(file_name, BUFSIZ - 1, "%s%c%s%c%s%cmca_%s_%s", dir, dirsep,
fw, dirsep, comp, dirsep, fw, comp);
if ('\0' != file_name[BUFSIZ - 1]) {
return false;
}
comp_handle->tch_handle = lt_dlopenext(file_name);
if (NULL != comp_handle->tch_handle) {
comp_symbol->tcs_variable =
lt_dlsym(comp_handle->tch_handle, component_name);
if (NULL != comp_symbol->tcs_variable) {
return true;
}
lt_dlclose(comp_handle->tch_handle);
}
/* Nope, didn't find it */
return false;
}
static char *dir_concat(const char *a, const char *b)
{
static char name[BUFSIZ];
name[0] = '\0';
if (NULL != a) {
strcat(name, a);
#ifdef WIN32
strcat(name, "\\");
#else
strcat(name, "/");
#endif
}
if (NULL != b) {
strcat(name, b);
}
return name;
}

85
test/support/components.h Обычный файл
Просмотреть файл

@ -0,0 +1,85 @@
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University.
* All rights reserved.
* Copyright (c) 2004-2005 The Trustees of the University of Tennessee.
* All rights reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
/** @file */
#ifndef OMPI_SUPPORT_COMPONENTS_H
#define OMPI_SUPPORT_COMPONENTS_H
#include "libltdl/ltdl.h"
#include "mca/mca.h"
#if defined(c_plusplus) || defined(__cplusplus)
extern "C" {
#endif
/**
* Generic function pointer, suitable for casting
*/
typedef void (*test_component_fn_t)(void);
/**
* Type to hold both a variable and a function pointer, especially
* for platforms where they are different sizes.
*/
union test_component_sym_t {
void *tcs_variable;
test_component_fn_t tcs_function;
};
/** Convenience typedef */
typedef union test_component_sym_t test_component_sym_t;
/**
* Type for masking the real/underlying type of opened components
*/
struct test_component_handle_t {
lt_dlhandle tch_handle;
};
/**
* Convenience typedef
*/
typedef struct test_component_handle_t test_component_handle_t;
/**
* Open a specific MCA component in the build tree
*
* void *test_component_open()
*/
int test_component_open(const char *framework, const char *component,
test_component_handle_t *comp_handle,
mca_base_component_t **mca);
/**
* Find a symbol in an MCA component
*
* ompi_test_component_sym_t test_component_find_symbol()
*/
int test_component_find_symbol(const char *name,
test_component_handle_t *handle,
test_component_sym_t *sym);
/**
* Close a specific MCA component
*
* int test_component_close()
*/
int test_component_close(test_component_handle_t *handle);
#if defined(c_plusplus) || defined(__cplusplus)
}
#endif
#endif /* OMPI_SUPPORT_COMPONENTS_H */