From 7ec0fd6fc02815d6093cdff97599226045657053 Mon Sep 17 00:00:00 2001 From: Timothy Collett Date: Sat, 26 May 2012 16:08:15 -0400 Subject: [PATCH] Trying to set up an autorelease pool, but not yet succeeding. --- library/CMakeLists.txt | 32 +++++++++++++++++++++++++++++++- library/Core.cpp | 13 +++++++++++++ library/Hooks-darwin.cpp | 9 +++++---- library/MacPool.h | 12 ++++++++++++ library/MacPool.mm | 22 ++++++++++++++++++++++ library/include/Console.h | 7 +++++++ 6 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 library/MacPool.h create mode 100644 library/MacPool.mm diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 4b7ffa7d1..1e664b812 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -49,6 +49,10 @@ SET(MAIN_HEADERS_WINDOWS include/wdirent.h ) +SET(MAIN_HEADERS_DARWIN +MacPool.h +) + SET(MAIN_SOURCES Core.cpp ColorText.cpp @@ -94,7 +98,10 @@ Console-darwin.cpp Hooks-darwin.cpp PlugLoad-darwin.cpp Process-darwin.cpp -#MacPool.m +) + +SET(OBJC_SOURCES_DARWIN +MacPool.mm ) SET(MAIN_SOURCES_LINUX_EGGY @@ -165,6 +172,11 @@ IF(UNIX) LIST(APPEND PROJECT_SOURCES ${MAIN_SOURCES_LINUX_EGGY}) ELSEIF(APPLE) LIST(APPEND PROJECT_SOURCES ${MAIN_SOURCES_DARWIN}) + SET(CMAKE_CXX_FLAGS "-fvisibility=hidden -m32 -march=i686 -mtune=generic") + SET(CPP_SOURCES) + LIST(APPEND CPP_SOURCES ${PROJECT_SOURCES}) + LIST(APPEND PROJECT_SOURCES ${OBJC_SOURCES_DARWIN}) + LIST(APPEND PROJECT_HEADERS ${MAIN_HEADERS_DARWIN}) ELSE() LIST(APPEND PROJECT_SOURCES ${MAIN_SOURCES_LINUX}) ENDIF() @@ -219,6 +231,23 @@ ELSE(WIN32) PROPERTIES COMPILE_FLAGS "/O1 /bigobj") ENDIF() +if(APPLE) + SET(CMAKE_CXX_FLAGS "-fvisibility=hidden -m32 -mtune=generic" ) + foreach(f ${PROJECT_SOURCES}) + if(f MATCHES MacPool.mm) + MESSAGE(STATUS "Not setting properties for ${f}") + SET_SOURCE_FILES_PROPERTIES(${f} PROPERTIES COMPILE_FLAGS "-arch i386 -framework Foundation -x objective-c") + else() + if(f MATCHES MacPool.h) + else() + SET_SOURCE_FILES_PROPERTIES(${f} PROPERTIES COMPILE_FLAGS "-std=c++0x -march=i686") + MESSAGE(STATUS "Setting properties for ${f}") + endif() + endif() + endforeach() + SET_SOURCE_FILES_PROPERTIES(dfhack-run.cpp PROPERTIES COMPILE_FLAGS "-std=c++0x -march=i686") + SET(CMAKE_SHARED_LINKER_FLAGS "-arch i386") +endif() # Compilation @@ -271,6 +300,7 @@ SET_TARGET_PROPERTIES(dfhack PROPERTIES DEBUG_POSTFIX "-debug" ) IF(APPLE) SET(SDL_LIBRARY ${CMAKE_INSTALL_PREFIX}/libs/SDL.framework) TARGET_LINK_LIBRARIES(dfhack ${SDL_LIBRARY}) + TARGET_LINK_LIBRARIES(dfhack /System/Library/Frameworks/Foundation.framework) SET_TARGET_PROPERTIES(dfhack PROPERTIES VERSION 1.0.0) SET_TARGET_PROPERTIES(dfhack PROPERTIES SOVERSION 1.0.0) ENDIF() diff --git a/library/Core.cpp b/library/Core.cpp index 63add0242..94d408b80 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -279,21 +279,29 @@ static command_result runLuaScript(color_ostream &out, std::string filename, vec command_result Core::runCommand(color_ostream &out, const std::string &command) { + fprintf(stderr,"Inside runCommand"); + fprintf(stderr," with command %s\n",command.c_str()); if (!command.empty()) { + fprintf(stderr,"Command is not empty, tokenizing\n"); vector parts; Core::cheap_tokenise(command,parts); + fprintf(stderr,"Tokenized, got %d parts\n",parts.size()); if(parts.size() == 0) return CR_NOT_IMPLEMENTED; string first = parts[0]; + fprintf(stderr,"Erasing beginning\n"); parts.erase(parts.begin()); + + fprintf(stderr,"I think we're about there\n"); if (first[0] == '#') return CR_OK; cerr << "Invoking: " << command << endl; + fprintf(stderr,"Returning with the next recursion\n"); return runCommand(out, first, parts); } else @@ -674,6 +682,7 @@ void fIOthread(void * iodata) { string command = ""; int ret = con.lineedit("[DFHack]# ",command, main_history); + fprintf(stderr,"Command: [%s]\n",command.c_str()); if(ret == -2) { cerr << "Console is shutting down properly." << endl; @@ -687,9 +696,13 @@ void fIOthread(void * iodata) else if(ret) { // a proper, non-empty command was entered + fprintf(stderr,"Adding command to history\n"); main_history.add(command); + fprintf(stderr,"Saving history\n"); main_history.save("dfhack.history"); } + + fprintf(stderr,"Running command\n"); auto rv = core->runCommand(con, command); diff --git a/library/Hooks-darwin.cpp b/library/Hooks-darwin.cpp index 59539c79b..19f30f198 100644 --- a/library/Hooks-darwin.cpp +++ b/library/Hooks-darwin.cpp @@ -49,8 +49,6 @@ distribution. #include "Hooks.h" #include -#include "MacPool.h" - /*static const interpose_t interposers[] __attribute__ ((section("__DATA, __interpose"))) = { { (void *)DFH_SDL_Init, (void *)SDL_Init }, @@ -60,6 +58,9 @@ distribution. };*/ +extern "C" int create_pool(); +extern "C" int destroy_pool(); + /******************************************************************************* * SDL part starts here * *******************************************************************************/ @@ -81,7 +82,7 @@ DFhackCExport void SDL_Quit(void) _SDL_Quit(); }*/ -// destroy_pool(); + destroy_pool(); _SDL_Quit(); } @@ -140,7 +141,7 @@ DFhackCExport int SDL_Init(uint32_t flags) // we don't reroute stdout until we figure out if this should be done at all // See: Console-linux.cpp -// create_pool(); + create_pool(); // find real functions fprintf(stderr,"dfhack: saving real SDL functions\n"); diff --git a/library/MacPool.h b/library/MacPool.h new file mode 100644 index 000000000..f3b5dbebb --- /dev/null +++ b/library/MacPool.h @@ -0,0 +1,12 @@ +/* + * MacPool.h + * Handles creation and destruction of autorelease pool for DFHack on the Mac + */ + +#ifndef MACPOOL_H +#define MACPOOL_H + +int create_pool(); +int destroy_pool(); + +#endif \ No newline at end of file diff --git a/library/MacPool.mm b/library/MacPool.mm new file mode 100644 index 000000000..2453aca29 --- /dev/null +++ b/library/MacPool.mm @@ -0,0 +1,22 @@ +/* + * MacPool.m + * + */ + +#import +#import "MacPool.h" + +NSAutoreleasePool *thePool; + +int create_pool() { + fprintf(stderr,"Creating autorelease pool\n"); + thePool = [[NSAutoreleasePool alloc] init]; + return 1; +} + +int destroy_pool() { + fprintf(stderr,"Draining and releasing autorelease pool\n"); + [thePool drain]; + [thePool release]; + return 0; +} \ No newline at end of file diff --git a/library/include/Console.h b/library/include/Console.h index a0a098861..196a1c27d 100644 --- a/library/include/Console.h +++ b/library/include/Console.h @@ -65,13 +65,20 @@ namespace DFHack bool save (const char * filename) { std::ofstream outfile (filename); + fprintf(stderr,"Save: Initialized stream\n"); if(outfile.bad()) return false; + fprintf(stderr,"Save: Iterating...\n"); for(auto iter = history.begin();iter < history.end(); iter++) { + fprintf(stderr,"Save: Dumping %s\n",(*iter).c_str()); outfile << *iter << std::endl; + fprintf(stderr,"Save: Flushing\n"); + outfile.flush(); } + fprintf(stderr,"Save: Closing\n"); outfile.close(); + fprintf(stderr,"Save: Done\n"); return true; } /// add a command to the history