diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f36d6126..13c06c0e4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,6 +116,9 @@ mark_as_advanced(TORQUE_ADVANCED_LIGHTING) option(TORQUE_BASIC_LIGHTING "Basic Lighting" ON) mark_as_advanced(TORQUE_APP_PASSWORD) +option(TORQUE_DYNAMIC_LIBRARY "Whether or not to build Torque as a dynamic library." OFF) +mark_as_advanced(TORQUE_DYNAMIC_LIBRARY) + set(TORQUE_APP_PASSWORD "changeme" CACHE STRING "zip file password") if(WIN32) diff --git a/Engine/source/CMakeLists.txt b/Engine/source/CMakeLists.txt index 308ae6f16..e8c406adc 100644 --- a/Engine/source/CMakeLists.txt +++ b/Engine/source/CMakeLists.txt @@ -63,9 +63,15 @@ endif (TORQUE_BASIC_LIGHTING) # On Windows we disable CRT Security warnings - this comes from recommendations to use non-portable functions. if (WIN32) - set(TORQUE_COMPILE_DEFINITIONS ${TORQUE_COMPILE_DEFINITIONS} _CRT_SECURE_NO_WARNINGS) + set(TORQUE_COMPILE_DEFINITIONS ${TORQUE_COMPILE_DEFINITIONS} _CRT_SECURE_NO_WARNINGS WIN32) endif (WIN32) +if (APPLE) + set(TORQUE_COMPILE_DEFINITIONS ${TORQUE_COMPILE_DEFINITIONS} __MACOSX__) +elseif (UNIX) + set(TORQUE_COMPILE_DEFINITIONS ${TORQUE_COMPILE_DEFINITIONS} __linux__) +endif (APPLE) + ################# Set Engine Linkages ################### # When on Windows, we need to link against winsock and windows codecs @@ -95,8 +101,6 @@ endif (UNIX AND NOT APPLE) ################# Collect Source Files ################### -torqueAddSourceDirectories("main") - # Handle app torqueAddSourceDirectories("app" "app/net") @@ -324,6 +328,22 @@ endif (APPLE) ################# Executable Generation ################### +if (TORQUE_DYNAMIC_LIBRARY) + set(TORQUE_COMPILE_DEFINITIONS ${TORQUE_COMPILE_DEFINITIONS} TORQUE_SHARED) + + # Build the main engine library + add_library(TorqueEngine SHARED ${TORQUE_SOURCE_FILES}) + target_compile_definitions(TorqueEngine PUBLIC ${TORQUE_COMPILE_DEFINITIONS}) + target_link_libraries(TorqueEngine ${TORQUE_LINK_LIBRARIES}) + target_include_directories(TorqueEngine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} "${CMAKE_BINARY_DIR}/temp" ${TORQUE_INCLUDE_DIRECTORIES}) + target_compile_features(TorqueEngine PRIVATE cxx_std_17) + + set(TORQUE_SOURCE_FILES "main/main.cpp") + set(TORQUE_LINK_LIBRARIES TorqueEngine) +else() + set(TORQUE_SOURCE_FILES "main/main.cpp" ${TORQUE_SOURCE_FILES}) +endif (TORQUE_DYNAMIC_LIBRARY) + if (APPLE) add_executable(${TORQUE_APP_NAME} MACOSX_BUNDLE ${TORQUE_SOURCE_FILES}) set_target_properties(${TORQUE_APP_NAME} PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_BINARY_DIR}/temp/Info.plist") diff --git a/Engine/source/main/main.cpp b/Engine/source/main/main.cpp index 6ebd6e5dc..961de278e 100644 --- a/Engine/source/main/main.cpp +++ b/Engine/source/main/main.cpp @@ -100,121 +100,44 @@ int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdL } #endif // WIN32 - #ifdef __MACOSX__ #include #include #include +#include // for dirname +#include +#include // for _NSGetExecutablePath #include extern "C" { int (*torque_macmain)(int argc, const char **argv) = 0; -} - -void GetBasePath(const char** cpath, const char** cname) -{ - static char path[2049]; - static char name[2049]; - - ProcessSerialNumber PSN; - ProcessInfoRec pinfo; - FSSpec pspec; - FSRef fsr; - OSStatus err; - - path[0] = 0; - name[0] = 0; - - *cpath = path; - *cname = name; - - // set up process serial number - PSN.highLongOfPSN = 0; - PSN.lowLongOfPSN = kCurrentProcess; - - // set up info block - pinfo.processInfoLength = sizeof(pinfo); - pinfo.processName = NULL; - pinfo.processAppSpec = &pspec; - - // grab the vrefnum and directory - err = GetProcessInformation(&PSN, &pinfo); - if (! err ) { - - FSSpec fss2; - - strcpy(name, &pspec.name[1]); - - err = FSMakeFSSpec(pspec.vRefNum, pspec.parID, 0, &fss2); - - if ( ! err ) { - err = FSpMakeFSRef(&fss2, &fsr); - if ( ! err ) { - err = (OSErr)FSRefMakePath(&fsr, (UInt8*)path, 2048); - } - } - } -} +} int main(int argc, const char **argv) { - void *gameBundle = 0; - char gameBundleFilename[2049]; + char path[PATH_MAX]; + uint32_t pathLen = sizeof(path); + int err = _NSGetExecutablePath(path, &pathLen); - const char* basePath; - const char* appName; + char* executableDirectory = dirname(path); - // Get the path to our app binary and the app name + // Once the executable directory is determined, we search two possibilities: The frameworks and next to the app bundle + chdir(executableDirectory); + chdir("../Frameworks"); - GetBasePath(&basePath, &appName); - - if (!basePath[0] || !appName[0]) - return; - - char appNameNoDebug[2049]; - - strcpy(appNameNoDebug, appName); - - int i = strlen(appName); - while (i > 0) + void *gameLib = dlopen("libTorqueEngine.dylib", RTLD_LAZY | RTLD_LOCAL); + if (!gameLib) { - if (!strcmp(&appName[i], "_DEBUG")) - { - appNameNoDebug[i] = 0; - break; - } - - i--; - } - - sprintf(gameBundleFilename, "%s.app/Contents/Frameworks/%s Bundle.bundle/Contents/MacOS/%s Bundle", appName, appNameNoDebug, appNameNoDebug); - - // first see if the current directory is set properly - gameBundle = dlopen(gameBundleFilename, RTLD_LAZY | RTLD_LOCAL); - - if (!gameBundle) - { - // Couldn't load the game bundle... so, using the path to the bundle binary fix up the cwd - - if (basePath[0]) { - chdir( basePath ); - chdir( "../../../" ); - } - - // and try again - gameBundle = dlopen( gameBundleFilename, RTLD_LAZY | RTLD_LOCAL); - } - - if (!gameBundle) return -1; + } - torque_macmain = (int (*)(int argc, const char **argv)) dlsym(gameBundle, "torque_macmain"); + torque_macmain = (int (*)(int argc, const char **argv)) dlsym(gameLib, "torque_macmain"); if (!torque_macmain) - return -1; + return -2; return torque_macmain(argc, argv); }