Update references to hapi_src to hapi_impl and revert hapiRegisterCallbacks
[charm.git] / src / util / ckdll.C
blobaa6bdb410701ca45ed6b56587d190eaa9f0f6a0d
1 /*
2 Portable Dynamically Linked Libraries (DLL) interface
4 Orion Sky Lawlor, olawlor@acm.org, 7/26/2002
5 */
6 #include "converse.h" //For CMK_ symbols
7 #include "ckdll.h"
8 #include <stdio.h> //For fopen
9 #include <stdlib.h> //For system
10 #include <string.h>
13 /*#include the appropriate CkDll implementation: */
15 #if CMK_DLL_USE_DLOPEN  /*********** UNIX .so/dlopen Version ******/
16 #include "ckdll_dlopen.C"
18 static void deleteFile(const char *fileName) {
19         unlink(fileName);
21 #define CMK_SCRATCH_PATH "/tmp"
23 #elif CMK_DLL_USE_WIN32 /*********** Win32 .dll/GetProcAddress Version ******/
24 #include "ckdll_win32.C"
26 static void deleteFile(const char *fileName) {
27         DeleteFile(fileName);
29 #define CMK_SCRATCH_PATH ""
31 #else 
32 /********* It ain't UNIX, it ain't win32-- what *is* it? */
33 CkDll::CkDll(const char *name) {
34         handle=0; /*DLL's not supported here.*/
36 void *CkDll::lookup(const char *name) {
37         return 0;
39 CkDll::~CkDll() {
40         ;
43 const char *CkDll::extension=0;
44 static void deleteFile(const char *fileName) { }
46 #define CMK_SCRATCH_PATH ""
47 #endif
49 /****************************************************************
50 CkCppInterpreter interface:
51         Call the C++ compiler on a string, then use CkDll to link the
52 resulting dll into the running program.
54         This depends on conv-mach.h or conv-mach-opt.h setting the symbol
55 CMK_DLL_CC to the correct invokation of the C++ compiler to generate a shared
56 library.  CMK_DLL_CC will immediately be followed by the output library name,
57 so it should end with, e.g., "-o " on UNIX platforms.
59         CMK_DLL_LINK is an optional extra link step (required on HP machines);
60 and CMK_DLL_INC is the compiler flag to change the #include path.
63 /* 
64 Command-line compilers for various platforms (now in conv-mach.h files)
65 #if CMK_DLL_VIA_SUN_CC
66 #  define CMK_DLL_CC  "CC -G -O3 -o "
67 #elif CMK_DLL_VIA_SGI_CC
68 #  define CMK_DLL_CC  "CC -shared -64 -LANG:std -O3 -o "
69 #elif CMK_DLL_VIA_CXX
70 #  define CMK_DLL_CC  "cxx -shared -O3 -o "
71 #elif CMK_DLL_VIA_HP_CC
72 #  define CMK_DLL_CC  "CC +z -O -c -o "
73 #  define CMK_DLL_LINK "CC -b -o "
74 #else
75 //Default: try g++
76 #  define CMK_DLL_CC  "g++ -shared -O3 -o "
77 #endif
81 #ifdef CMK_DLL_CC //We have a command-line dynamic-link library compiler:
83 #ifndef CMK_DLL_INC
84 #  define CMK_DLL_INC "-I" /*Assume unix-style command-line flags*/
85 #endif
87 /*Return 1 if this file exists*/
88 static int fileExists(const char *fileName) {
89         FILE *f=fopen(fileName,"r");
90         if (f==NULL) return 0;
91         else {
92                 fclose(f);
93                 return 1;
94         }
98 #ifdef CMK_SIGSAFE_SYSTEM
99 #  include "ckdll_system.C"
100 #else
101 /*No need for a signal-safe system call*/
102 static int CkSystem (const char *command) {
103         system(command);
105 #endif
107 //Compile "cppCode", making available the includes at inclPath
108 CkCppInterpreter::CkCppInterpreter(const char *cppCode,const char *inclPath)
109         :library(NULL)
111         int verbose=0;
112         int randA=CrnRand();
113         int randB=CmiMyPe();
115 /*Write the c++ code to a temporary file:*/
116         char sourceFile[256];
117         sprintf(sourceFile,"%s/ckSharedLib_%d_%d_%p.%s",
118                 CMK_SCRATCH_PATH,randA,randB,(void*)this,"cpp");
119         FILE *f=fopen(sourceFile,"w"); if (f==NULL) return;
120         fputs(cppCode,f);
121         fclose(f);
123 /*Allocate a spot for the library file:*/
124         sprintf(libraryFile,"%s/ckSharedLib_%d_%d_%p%s",
125                 CMK_SCRATCH_PATH,randA,randB,(void*)this,CkDll::extension);
126         
127 //Compile the .cpp file into a .dll:
128         char compilerCmd[1024];
129         sprintf(compilerCmd,"%s%s %s %s%s",
130                 CMK_DLL_CC, libraryFile, sourceFile,
131                 inclPath!=NULL?CMK_DLL_INC:"",inclPath!=NULL?inclPath:"");
132         
133         if (verbose) CmiPrintf("Executing: '%s'\n",compilerCmd);
134         int compilerRet=CkSystem(compilerCmd);
135         deleteFile(sourceFile);
136         if (compilerRet!=0) { //!fileExists(libraryFile)) {
137                 CmiPrintf("Compilation error! Cmd='%s', err=%d, src='%s'\n",
138                         compilerCmd,compilerRet,cppCode);
139                 return; /*with library set to NULL*/
140         }
141         
142 #ifdef CMK_DLL_LINK
143 //Link the .so into a ".sop"
144         // HIDEOUS HACK: playing silly games with filename:
145         //    CC source -o foo.so
146         //    CC foo.so -o foo.sop
147         sprintf(compilerCmd,"%s%sp %s",
148                 CMK_DLL_LINK, libraryFile, libraryFile);
149         compilerRet=CkSystem(compilerCmd);
150         unlink(libraryFile);
151         strcat(libraryFile,"p");
152         if (compilerRet!=0) { //!fileExists(libraryFile)) {
153                 CmiPrintf("Link error! Cmd='%s', err=%d, src='%s'\n",
154                         compilerCmd,compilerRet,cppCode);
155                 return; 
156         }
157 #endif
158         
159 /*Link the library into the program: */ 
160         library=new CkDll(libraryFile);
163 //Remove "cppCode" from the program.
164 //  This invalidates any function pointers created with lookup
165 CkCppInterpreter::~CkCppInterpreter()
167         if (library) {
168                 delete library;
169                 deleteFile(libraryFile);
170         }
173 #endif