diff --git a/CMakeLists.txt b/CMakeLists.txt index 522bf5ae8..22f757f56 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,9 @@ project(dunelegacy VERSION 0.96.4 LANGUAGES CXX C ) +if(APPLE) + enable_language(OBJC) +endif() set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -53,6 +56,10 @@ if(MSVC) include(setup/msvc-setup) endif() +if(APPLE) + include(setup/macos-setup) +endif() + if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") include(setup/clang-gcc-setup) endif() diff --git a/CMakePresets.json b/CMakePresets.json index ecce8c377..7e0c0fe2a 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -294,7 +294,14 @@ "hidden": true, "inherits": "default", "displayName": "Linux default", - "description": "Default Linux build" + "description": "Default Linux build", + "vendor": { + "microsoft.com/VisualStudioSettings/CMake/1.0": { + "hostOS": [ + "Linux" + ] + } + } }, { "name": "linux-debug", @@ -352,17 +359,40 @@ } }, { - "name": "macos-x64-release", - "inherits": "linux-base", - "displayName": "macOS x64 Release", + "name": "macos-base", + "hidden": true, + "inherits": "default", + "displayName": "macOS default", + "description": "Default macOS build", "cacheVariables": { - "CMAKE_BUILD_TYPE": "Release", - "VCPKG_TARGET_TRIPLET": "x64-osx-release" + "VCPKG_MANIFEST_DIR": "${sourceDir}/external/vcpkg/config-macos" + }, + "vendor": { + "microsoft.com/VisualStudioSettings/CMake/1.0": { + "hostOS": [ + "macOS" + ] + } + } + }, + { + "name": "macos-xcode", + "inherits": "macos-base", + "displayName": "macOS Xcode", + "generator": "Xcode" + }, + { + "name": "macos-arm64-debug", + "inherits": "macos-base", + "displayName": "macOS arm64 Debug", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug", + "VCPKG_TARGET_TRIPLET": "arm64-osx" } }, { "name": "macos-arm64-release", - "inherits": "linux-base", + "inherits": "macos-base", "displayName": "macOS arm64 Release", "cacheVariables": { "CMAKE_BUILD_TYPE": "Release", @@ -379,10 +409,6 @@ "name": "linux-release", "configurePreset": "linux-release" }, - { - "name": "macos-x64-release", - "configurePreset": "macos-x64-release" - }, { "name": "macos-arm64-release", "configurePreset": "macos-arm64-release" @@ -411,17 +437,6 @@ "stopOnFailure": true } }, - { - "name": "macos-x64-release", - "configurePreset": "macos-x64-release", - "output": { - "outputOnFailure": true - }, - "execution": { - "noTestsAction": "error", - "stopOnFailure": true - } - }, { "name": "macos-arm64-release", "configurePreset": "macos-arm64-release", diff --git a/MacOSX/MacFunctions.h b/MacOSX/MacFunctions.h new file mode 100644 index 000000000..12f0ffe20 --- /dev/null +++ b/MacOSX/MacFunctions.h @@ -0,0 +1,16 @@ +#ifndef MACFUNCTIONS_H +#define MACFUNCTIONS_H + +#if __cplusplus +extern "C" { +#endif + +const char* getMacLanguage(); + +void getMacApplicationSupportFolder(char* buffer, int len); + +#if __cplusplus +} +#endif + +#endif //MACFUNCTIONS_H diff --git a/MacOSX/MacFunctions.m b/MacOSX/MacFunctions.m new file mode 100644 index 000000000..7315874c1 --- /dev/null +++ b/MacOSX/MacFunctions.m @@ -0,0 +1,34 @@ + +#import +#import "MacFunctions.h" + +static char macLanguage[3] = " "; + +const char* getMacLanguage() { + NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; + NSArray * languageArray = [userDefaults objectForKey:@"AppleLanguages"]; + NSString * firstLanguage = [languageArray objectAtIndex:0]; + + macLanguage[0] = [firstLanguage characterAtIndex:0]; + macLanguage[1] = [firstLanguage characterAtIndex:1]; + + return macLanguage; +} + +void getMacApplicationSupportFolder(char* buffer, int len) { + FSRef appSupportFolder; + OSErr error = noErr; + NSString* appSupportFolderString; + + error = FSFindFolder(kUserDomain, kApplicationSupportFolderType, true, &appSupportFolder); + + if(error == noErr) { + CFURLRef url = CFURLCreateFromFSRef(kCFAllocatorDefault, &appSupportFolder); + appSupportFolderString = [(NSURL*) url path]; + } else { + appSupportFolderString = [@"~/Library/Application Support" stringByExpandingTildeInPath]; + } + + [appSupportFolderString getCString:buffer maxLength:len-1 encoding:NSUTF8StringEncoding]; +} + diff --git a/cmake/setup/macos-setup.cmake b/cmake/setup/macos-setup.cmake new file mode 100644 index 000000000..8b798a811 --- /dev/null +++ b/cmake/setup/macos-setup.cmake @@ -0,0 +1,3 @@ +message(STATUS "Configuring macOS") +include_directories(AFTER "${CMAKE_CURRENT_LIST_DIR}../../../MacOSX") + diff --git a/external/vcpkg/config-macos/vcpkg-configuration.json b/external/vcpkg/config-macos/vcpkg-configuration.json new file mode 100644 index 000000000..ffac774a2 --- /dev/null +++ b/external/vcpkg/config-macos/vcpkg-configuration.json @@ -0,0 +1,8 @@ +{ + "overlay-ports": [ + "../config/ports/sdl2" + ], + "overlay-triplets": [ + "../config/triplets" + ] +} diff --git a/external/vcpkg/config-macos/vcpkg.json b/external/vcpkg/config-macos/vcpkg.json new file mode 100644 index 000000000..e40c157b3 --- /dev/null +++ b/external/vcpkg/config-macos/vcpkg.json @@ -0,0 +1,13 @@ +{ + "dependencies": [ + "fmt", + "gtest", + "lodepng", + "ms-gsl", + "sdl2", + "sdl2-mixer", + "sdl2-ttf", + "soxr", + "openssl" + ] +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 78f99b13f..a56a458b6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -29,7 +29,7 @@ find_package(fmt CONFIG REQUIRED) find_package(lodepng CONFIG REQUIRED) find_package(Microsoft.GSL CONFIG REQUIRED) -if(UNIX) +if(UNIX AND NOT APPLE) find_package(X11 REQUIRED) endif() @@ -73,7 +73,7 @@ if(WIN32) list(APPEND EXE_SOURCES ../Windows/DuneLegacy.exe.manifest ../resource.rc) endif() -if(UNIX) +if(UNIX AND NOT APPLE) list(APPEND LIBS ${X11_LIBRARIES}) endif() @@ -142,6 +142,11 @@ add_executable(dunelegacy ${EXE_SOURCES} ${EXE_HEADERS}) target_compile_options(dunelegacy PRIVATE ${dune_flags}) target_link_libraries(dunelegacy PRIVATE dune SDL2::SDL2main harden_interface) +if(APPLE) + target_sources(dunelegacy PRIVATE ../MacOSX/MacFunctions.m) + set_source_files_properties(../MacOSX/MacFunctions.m PROPERTIES SKIP_PRECOMPILE_HEADERS ON) +endif() + if(TARGET dune_gitversion) target_link_libraries(dunelegacy PRIVATE "$") endif()