2 Portable Dynamically Linked Libraries (DLL) interface
4 Orion Sky Lawlor, olawlor@acm.org, 7/26/2002
6 #include "converse.h" //For CMK_ symbols
8 #include <stdio.h> //For fopen
9 #include <stdlib.h> //For system
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) {
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) {
29 #define CMK_SCRATCH_PATH ""
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) {
43 const char *CkDll::extension=0;
44 static void deleteFile(const char *fileName) { }
46 #define CMK_SCRATCH_PATH ""
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.
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 "
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 "
76 # define CMK_DLL_CC "g++ -shared -O3 -o "
81 #ifdef CMK_DLL_CC //We have a command-line dynamic-link library compiler:
84 # define CMK_DLL_INC "-I" /*Assume unix-style command-line flags*/
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;
98 #ifdef CMK_SIGSAFE_SYSTEM
99 # include "ckdll_system.C"
101 /*No need for a signal-safe system call*/
102 static int CkSystem (const char *command) {
107 //Compile "cppCode", making available the includes at inclPath
108 CkCppInterpreter::CkCppInterpreter(const char *cppCode,const char *inclPath)
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;
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);
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:"");
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*/
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);
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);
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()
169 deleteFile(libraryFile);