opal_wrapper.c
来自「MPI stands for the Message Passing Inter」· C语言 代码 · 共 759 行 · 第 1/2 页
C
759 行
/* * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana * University Research and Technology * Corporation. All rights reserved. * Copyright (c) 2004-2005 The University of Tennessee and The University * of Tennessee Research Foundation. 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 (c) 2007 Los Alamos National Security, LLC. All rights * reserved. * $COPYRIGHT$ * * Additional copyrights may follow * * $HEADER$ */#include "opal_config.h"#include <stdio.h>#include <errno.h>#ifdef HAVE_STDLIB_H#include <stdlib.h>#endif /* HAVE_STDLIB_H */#ifdef HAVE_SYS_STAT_H#include <sys/stat.h>#endif#ifdef HAVE_LIBGEN_H#include <libgen.h>#endif#ifdef HAVE_SYS_TYPES_H#include <sys/types.h>#endif#ifdef HAVE_SYS_WAIT_H#include <sys/wait.h>#endif#ifdef HAVE_STRING_H#include <string.h>#endif /* HAVE_STRING_H */#include "opal/mca/installdirs/installdirs.h"#include "opal/runtime/opal.h"#include "opal/constants.h"#include "opal/util/argv.h"#include "opal/util/error.h"#include "opal/util/keyval_parse.h"#include "opal/util/opal_environ.h"#include "opal/util/show_help.h"#include "opal/util/path.h"#include "opal/util/few.h"#include "opal/util/basename.h"#include "opal/util/os_path.h"#if !defined(__WINDOWS__)#define OPAL_INCLUDE_FLAG "-I"#define OPAL_LIBDIR_FLAG "-L"#else#define OPAL_INCLUDE_FLAG "/I"#define OPAL_LIBDIR_FLAG "/LIBPATH:"#endif /* !defined(__WINDOWS__) */struct options_data_t { char **compiler_args; char *language; char *project; char *project_short; char *version; char *compiler_env; char *compiler_flags_env; char *compiler; char *module_option; char **preproc_flags; char **comp_flags; char **link_flags; char **libs; char *req_file; char *path_includedir; char *path_libdir;};static struct options_data_t *options_data = NULL;/* index used by parser */static int parse_options_idx = -1;/* index of options specified by user */static int user_data_idx = -1;/* index of options to use by default */static int default_data_idx = -1;#define COMP_DRY_RUN 0x001#define COMP_SHOW_ERROR 0x002#define COMP_WANT_COMMAND 0x004#define COMP_WANT_PREPROC 0x008#define COMP_WANT_COMPILE 0x010#define COMP_WANT_LINK 0x020#define COMP_WANT_PMPI 0x040static voidoptions_data_init(struct options_data_t *data){ data->compiler_args = malloc(sizeof(char*)); data->compiler_args[0] = NULL; data->language = NULL; data->compiler = NULL; data->project = NULL; data->project_short = NULL; data->version = NULL; data->compiler_env = NULL; data->compiler_flags_env = NULL; data->module_option = NULL; data->preproc_flags = malloc(sizeof(char*)); data->preproc_flags[0] = NULL; data->comp_flags = malloc(sizeof(char*)); data->comp_flags[0] = NULL; data->link_flags = malloc(sizeof(char*)); data->link_flags[0] = NULL; data->libs = malloc(sizeof(char*)); data->libs[0] = NULL; data->req_file = NULL; data->path_includedir = NULL; data->path_libdir = NULL;}static voidoptions_data_free(struct options_data_t *data){ if (NULL != data->compiler_args) { opal_argv_free(data->compiler_args); } if (NULL != data->language) free(data->language); if (NULL != data->compiler) free(data->compiler); if (NULL != data->project) free(data->project); if (NULL != data->project_short) free(data->project_short); if (NULL != data->version) free(data->version); if (NULL != data->compiler_env) free(data->compiler_env); if (NULL != data->compiler_flags_env) free(data->compiler_flags_env); if (NULL != data->module_option) free(data->module_option); opal_argv_free(data->preproc_flags); opal_argv_free(data->comp_flags); opal_argv_free(data->link_flags); opal_argv_free(data->libs); if (NULL != data->req_file) free(data->req_file); if (NULL != data->path_includedir) free(data->path_includedir); if (NULL != data->path_libdir) free(data->path_libdir);}static voidoptions_data_expand(const char *value){ /* make space for the new set of args */ parse_options_idx++; options_data = realloc(options_data, sizeof(struct options_data_t) * (parse_options_idx + 1)); options_data_init(&(options_data[parse_options_idx])); /* if there are values, this is not the default case. Otherwise, it's the default case... */ if (NULL != value && 0 != strcmp(value, "")) { char **values = opal_argv_split(value, ';'); opal_argv_insert(&(options_data[parse_options_idx].compiler_args), opal_argv_count(options_data[parse_options_idx].compiler_args), values); opal_argv_free(values); } else { free(options_data[parse_options_idx].compiler_args); options_data[parse_options_idx].compiler_args = NULL; /* this is a default */ default_data_idx = parse_options_idx; }}static intfind_options_index(const char *arg){ int i, j; for (i = 0 ; i <= parse_options_idx ; ++i) { if (NULL == options_data[i].compiler_args) { continue; } for (j = 0 ; j < opal_argv_count(options_data[i].compiler_args) ; ++j) { /* BWB: If in the future, we want to allow architecture flags to be wildcard specified (like, say, -xarch=amd64*), this strcmp would have to be changed to something that understands wildcards. This is the only change that will have to be made, provided you didn't want to add some extra logic to have a general -xarch=amd64* rule and another one that was an exact match (like, say, -xarch=amd64a). Then this entire loop will have to be modified, but no other parts of this file would have to change. */ if (0 == strcmp(arg, options_data[i].compiler_args[j])) { return i; } } } return -1;}static voiddata_callback(const char *key, const char *value){ /* handle case where text file does not contain any special compiler options field */ if (parse_options_idx < 0 && 0 != strcmp(key, "compiler_args")) { options_data_expand(NULL); } if (0 == strcmp(key, "compiler_args")) { options_data_expand(value); } else if (0 == strcmp(key, "language")) { if (NULL != value) options_data[parse_options_idx].language = strdup(value); } else if (0 == strcmp(key, "compiler")) { if (NULL != value) options_data[parse_options_idx].compiler = strdup(value); } else if (0 == strcmp(key, "project")) { if (NULL != value) options_data[parse_options_idx].project = strdup(value); } else if (0 == strcmp(key, "version")) { if (NULL != value) options_data[parse_options_idx].version = strdup(value); } else if (0 == strcmp(key, "module_option")) { if (NULL != value) options_data[parse_options_idx].module_option = strdup(value); } else if (0 == strcmp(key, "extra_includes")) { /* this is the hard one - need to put it together... */ int i; char **values = opal_argv_split(value, ' '); for (i = 0 ; i < opal_argv_count(values) ; ++i) { char *line, *include_directory; include_directory = opal_os_path( false, opal_install_dirs.includedir, values[i], NULL );#if defined(__WINDOWS__) asprintf(&line, OPAL_INCLUDE_FLAG"\"%s\"", include_directory);#else asprintf(&line, OPAL_INCLUDE_FLAG"%s", include_directory);#endif /* defined(__WINDOWS__) */ opal_argv_append_nosize(&options_data[parse_options_idx].preproc_flags, line); free(include_directory); free(line); } } else if (0 == strcmp(key, "preprocessor_flags")) { char **values = opal_argv_split(value, ' '); opal_argv_insert(&options_data[parse_options_idx].preproc_flags, opal_argv_count(options_data[parse_options_idx].preproc_flags), values); opal_argv_free(values); } else if (0 == strcmp(key, "compiler_flags")) { char **values = opal_argv_split(value, ' '); opal_argv_insert(&options_data[parse_options_idx].comp_flags, opal_argv_count(options_data[parse_options_idx].comp_flags), values); opal_argv_free(values); } else if (0 == strcmp(key, "linker_flags")) { char **values = opal_argv_split(value, ' '); opal_argv_insert(&options_data[parse_options_idx].link_flags, opal_argv_count(options_data[parse_options_idx].link_flags), values); opal_argv_free(values); } else if (0 == strcmp(key, "libs")) { char **values = opal_argv_split(value, ' '); opal_argv_insert(&options_data[parse_options_idx].libs, opal_argv_count(options_data[parse_options_idx].libs), values); opal_argv_free(values); } else if (0 == strcmp(key, "required_file")) { if (NULL != value) options_data[parse_options_idx].req_file = strdup(value); } else if (0 == strcmp(key, "project_short")) { if (NULL != value) options_data[parse_options_idx].project_short = strdup(value); } else if (0 == strcmp(key, "compiler_env")) { if (NULL != value) options_data[parse_options_idx].compiler_env = strdup(value); } else if (0 == strcmp(key, "compiler_flags_env")) { if (NULL != value) options_data[parse_options_idx].compiler_flags_env = strdup(value); } else if (0 == strcmp(key, "includedir")) { if (NULL != value) options_data[parse_options_idx].path_includedir = opal_install_dirs_expand(value); if (0 != strcmp(options_data[parse_options_idx].path_includedir, "/usr/include")) { char *line;#if defined(__WINDOWS__) asprintf(&line, OPAL_INCLUDE_FLAG"\"%s\"", options_data[parse_options_idx].path_includedir);#else asprintf(&line, OPAL_INCLUDE_FLAG"%s", options_data[parse_options_idx].path_includedir);#endif /* defined(__WINDOWS__) */ opal_argv_append_nosize(&options_data[parse_options_idx].preproc_flags, line); free(line); } } else if (0 == strcmp(key, "libdir")) { if (NULL != value) options_data[parse_options_idx].path_libdir = opal_install_dirs_expand(value);#if defined(__WINDOWS__) opal_argv_append_nosize( &options_data[parse_options_idx].link_flags, "/link" );#endif /* defined(__WINDOWS__) */ if (0 != strcmp(options_data[parse_options_idx].path_libdir, "/usr/lib")) { char *line;#if defined(__WINDOWS__) asprintf(&line, OPAL_LIBDIR_FLAG"\"%s\"", options_data[parse_options_idx].path_libdir);#else asprintf(&line, OPAL_LIBDIR_FLAG"%s", options_data[parse_options_idx].path_libdir);#endif /* defined(__WINDOWS__) */ opal_argv_append_nosize(&options_data[parse_options_idx].link_flags, line); free(line); } }}static intdata_init(const char *appname) { int ret; char *datafile; /* now load the data */ asprintf(&datafile, "%s%s%s-wrapper-data.txt", opal_install_dirs.pkgdatadir, OPAL_PATH_SEP, appname); if (NULL == datafile) return OPAL_ERR_TEMP_OUT_OF_RESOURCE; ret = opal_util_keyval_parse(datafile, data_callback); if( OPAL_SUCCESS != ret ) { fprintf(stderr, "Cannot open configuration file %s\n", datafile ); } free(datafile); return ret;}static intdata_finalize(void){ int i; for (i = 0 ; i <= parse_options_idx ; ++i) { options_data_free(&(options_data[i])); } free(options_data); return OPAL_SUCCESS;}static voidprint_flags(char **args, char *pattern){ int i; bool found = false; for (i = 0 ; args[i] != NULL ; ++i) { if (0 == strncmp(args[i], pattern, strlen(pattern))) { if (found) printf(" "); printf("%s", args[i] + strlen(pattern)); found = true; } } if (found) printf("\n");}static voidload_env_data(const char *project, const char *flag, char **data){ char *envname; char *envvalue; if (NULL == project || NULL == flag) return; asprintf(&envname, "%s_MPI%s", project, flag); if (NULL == (envvalue = getenv(envname))) { free(envname); asprintf(&envname, "%s_%s", project, flag); if (NULL == (envvalue = getenv(envname))) { free(envname); return; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?