00001
00002
00003
00004
00005
00006 #include "converse.h"
00007 #include "ckdll.h"
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011
00012
00013
00014
00015 #if CMK_DLL_USE_DLOPEN
00016 #include "ckdll_dlopen.C"
00017
00018 static void deleteFile(const char *fileName) {
00019 unlink(fileName);
00020 }
00021 #define CMK_SCRATCH_PATH "/tmp"
00022
00023 #elif CMK_DLL_USE_WIN32
00024 #include "ckdll_win32.C"
00025
00026 static void deleteFile(const char *fileName) {
00027 DeleteFile(fileName);
00028 }
00029 #define CMK_SCRATCH_PATH ""
00030
00031 #else
00032
00033 CkDll::CkDll(const char *name) {
00034 handle=0;
00035 }
00036 void *CkDll::lookup(const char *name) {
00037 return 0;
00038 }
00039 CkDll::~CkDll() {
00040 ;
00041 }
00042
00043 const char *CkDll::extension=0;
00044 static void deleteFile(const char *fileName) { }
00045
00046 #define CMK_SCRATCH_PATH ""
00047 #endif
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 #ifdef CMK_DLL_CC //We have a command-line dynamic-link library compiler:
00082
00083 #ifndef CMK_DLL_INC
00084 # define CMK_DLL_INC "-I"
00085 #endif
00086
00087
00088 static int fileExists(const char *fileName) {
00089 FILE *f=fopen(fileName,"r");
00090 if (f==NULL) return 0;
00091 else {
00092 fclose(f);
00093 return 1;
00094 }
00095 }
00096
00097
00098 #ifdef CMK_SIGSAFE_SYSTEM
00099 # include "ckdll_system.C"
00100 #else
00101
00102 static int CkSystem (const char *command) {
00103 system(command);
00104 }
00105 #endif
00106
00107
00108 CkCppInterpreter::CkCppInterpreter(const char *cppCode,const char *inclPath)
00109 :library(NULL)
00110 {
00111 int verbose=0;
00112 int randA=CrnRand();
00113 int randB=CmiMyPe();
00114
00115
00116 char sourceFile[256];
00117 sprintf(sourceFile,"%s/ckSharedLib_%d_%d_%p.%s",
00118 CMK_SCRATCH_PATH,randA,randB,(void*)this,"cpp");
00119 FILE *f=fopen(sourceFile,"w"); if (f==NULL) return;
00120 fputs(cppCode,f);
00121 fclose(f);
00122
00123
00124 sprintf(libraryFile,"%s/ckSharedLib_%d_%d_%p%s",
00125 CMK_SCRATCH_PATH,randA,randB,(void*)this,CkDll::extension);
00126
00127
00128 char compilerCmd[1024];
00129 sprintf(compilerCmd,"%s%s %s %s%s",
00130 CMK_DLL_CC, libraryFile, sourceFile,
00131 inclPath!=NULL?CMK_DLL_INC:"",inclPath!=NULL?inclPath:"");
00132
00133 if (verbose) CmiPrintf("Executing: '%s'\n",compilerCmd);
00134 int compilerRet=CkSystem(compilerCmd);
00135 deleteFile(sourceFile);
00136 if (compilerRet!=0) {
00137 CmiPrintf("Compilation error! Cmd='%s', err=%d, src='%s'\n",
00138 compilerCmd,compilerRet,cppCode);
00139 return;
00140 }
00141
00142 #ifdef CMK_DLL_LINK
00143
00144
00145
00146
00147 sprintf(compilerCmd,"%s%sp %s",
00148 CMK_DLL_LINK, libraryFile, libraryFile);
00149 compilerRet=CkSystem(compilerCmd);
00150 unlink(libraryFile);
00151 strcat(libraryFile,"p");
00152 if (compilerRet!=0) {
00153 CmiPrintf("Link error! Cmd='%s', err=%d, src='%s'\n",
00154 compilerCmd,compilerRet,cppCode);
00155 return;
00156 }
00157 #endif
00158
00159
00160 library=new CkDll(libraryFile);
00161 }
00162
00163
00164
00165 CkCppInterpreter::~CkCppInterpreter()
00166 {
00167 if (library) {
00168 delete library;
00169 deleteFile(libraryFile);
00170 }
00171 }
00172
00173 #endif