Skip to content

Commit a245aaf

Browse files
authored
Added ios support (Kenix3#491)
1 parent 54a387a commit a245aaf

17 files changed

+240
-74
lines changed

.github/workflows/build-validation.yml

+19
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ jobs:
2828
name: soh-mac
2929
path: build-cmake/src/*.a
3030
if-no-files-found: error
31+
build-ios:
32+
runs-on: macos-12
33+
steps:
34+
- uses: actions/checkout@v2
35+
- name: ccache
36+
uses: hendrikmuhs/[email protected]
37+
with:
38+
key: ${{ runner.os }}-ccache
39+
- name: Build libultraship
40+
run: |
41+
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
42+
cmake --no-warn-unused-cli -H. -Bbuild-cmake -GXcode -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_DEPLOYMENT_TARGET=14.0 -DCMAKE_BUILD_TYPE:STRING=Release
43+
cmake --build build-cmake --config Release --parallel 10
44+
- name: Upload build
45+
uses: actions/upload-artifact@v3
46+
with:
47+
name: soh-ios
48+
path: build-cmake/src/*/*.a
49+
if-no-files-found: error
3150
build-linux:
3251
runs-on: ubuntu-latest
3352
steps:

.gitignore

+19
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,22 @@ ZAPDUtils.lib
346346
.vs/
347347
.idea/
348348
cmake-build-**
349+
cmake_install.cmake
350+
*.a
351+
*.o
352+
*.pc
353+
*.vcxproj
354+
*.sln
355+
*.filters
356+
*.stamp
357+
*.depend
358+
*.lib
359+
*.tlog
360+
*.recipe
361+
src/install_config.h
362+
# Removed jsonConfig and jsonConfigVersion
363+
extern/nlohmann-json/*_jsonConfig*
364+
*.pbxproj
365+
*.xcworkspace
366+
*.xcsettings
367+
build*

CMakeLists.txt

+29-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ cmake_minimum_required(VERSION 3.24.0)
22

33
option(NON_PORTABLE "Build a non-portable version" OFF)
44

5+
if(CMAKE_SYSTEM_NAME STREQUAL "iOS")
6+
option(SIGN_LIBRARY "Enable xcode signing" OFF)
7+
option(BUNDLE_ID "Bundle ID for xcode signing" "com.example.libultraship")
8+
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
9+
endif()
10+
511
project(libultraship LANGUAGES C CXX)
6-
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
12+
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
713
enable_language(OBJCXX)
814
set(CMAKE_OBJC_FLAGS "${CMAKE_OBJC_FLAGS} -fobjc-arc")
915
set(CMAKE_OBJCXX_FLAGS "${CMAKE_OBJCXX_FLAGS} -fobjc-arc")
@@ -14,15 +20,34 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
1420
endif()
1521

1622
include(cmake/Utils.cmake)
23+
set(ADDITIONAL_LIB_INCLUDES "")
1724

1825
include(cmake/dependencies/common.cmake)
1926
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
2027
include(cmake/dependencies/android.cmake)
28+
elseif (CMAKE_SYSTEM_NAME STREQUAL "iOS")
29+
include(cmake/dependencies/ios.cmake)
30+
elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
31+
include(cmake/dependencies/windows.cmake)
2132
endif()
22-
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
23-
include(cmake/dependencies/windows.cmake)
33+
34+
if(CMAKE_SYSTEM_NAME STREQUAL "iOS")
35+
set(PLATFORM "OS64COMBINED")
36+
include(FetchContent)
37+
FetchContent_Declare(iostoolchain
38+
GIT_REPOSITORY https://github.com/leetal/ios-cmake
39+
GIT_TAG 06465b27698424cf4a04a5ca4904d50a3c966c45
40+
CONFIGURE_COMMAND ""
41+
BUILD_COMMAND ""
42+
)
43+
FetchContent_GetProperties(iostoolchain)
44+
if(NOT iostoolchain_POPULATED)
45+
FetchContent_Populate(iostoolchain)
46+
endif()
47+
set(CMAKE_IOS_TOOLCHAIN_FILE ${iostoolchain_SOURCE_DIR}/ios.toolchain.cmake)
48+
set_property(GLOBAL PROPERTY IOS_TOOLCHAIN_FILE ${CMAKE_IOS_TOOLCHAIN_FILE})
49+
include(${CMAKE_IOS_TOOLCHAIN_FILE})
2450
endif()
2551

2652
add_subdirectory("extern")
2753
add_subdirectory("src")
28-

cmake/dependencies/ios.cmake

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#=================== SDL2 ===================
2+
find_package(SDL2 QUIET)
3+
if (NOT ${SDL2_FOUND})
4+
include(FetchContent)
5+
FetchContent_Declare(
6+
SDL2
7+
GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
8+
GIT_TAG release-2.28.1
9+
OVERRIDE_FIND_PACKAGE
10+
)
11+
FetchContent_MakeAvailable(SDL2)
12+
endif()
13+
14+
#=================== nlohmann-json ===================
15+
find_package(nlohmann_json QUIET)
16+
if (NOT ${nlohmann_json_FOUND})
17+
FetchContent_Declare(
18+
nlohmann_json
19+
GIT_REPOSITORY https://github.com/nlohmann/json.git
20+
GIT_TAG v3.11.3
21+
OVERRIDE_FIND_PACKAGE
22+
)
23+
FetchContent_MakeAvailable(nlohmann_json)
24+
endif()
25+
26+
#=================== libzip ===================
27+
find_package(libzip QUIET)
28+
if (NOT ${libzip_FOUND})
29+
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
30+
set(BUILD_TOOLS OFF)
31+
set(BUILD_REGRESS OFF)
32+
set(BUILD_EXAMPLES OFF)
33+
set(BUILD_DOC OFF)
34+
set(BUILD_OSSFUZZ OFF)
35+
set(BUILD_SHARED_LIBS OFF)
36+
FetchContent_Declare(
37+
libzip
38+
GIT_REPOSITORY https://github.com/nih-at/libzip.git
39+
GIT_TAG v1.10.1
40+
OVERRIDE_FIND_PACKAGE
41+
)
42+
FetchContent_MakeAvailable(libzip)
43+
list(APPEND ADDITIONAL_LIB_INCLUDES ${libzip_SOURCE_DIR}/lib ${libzip_BINARY_DIR})
44+
endif()

extern/CMakeLists.txt

+5-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
6262
${IMGUI_DIR}/backends/imgui_impl_dx11.cpp
6363
${IMGUI_DIR}/backends/imgui_impl_win32.cpp
6464
)
65-
elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
65+
elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
6666
target_sources(ImGui
6767
PRIVATE
6868
${IMGUI_DIR}/backends/imgui_impl_metal.mm
@@ -77,13 +77,15 @@ endif()
7777

7878
target_include_directories(ImGui PUBLIC ${IMGUI_DIR} ${IMGUI_DIR}/backends PRIVATE ${SDL2_INCLUDE_DIRS})
7979

80-
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
80+
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
8181
target_include_directories(ImGui PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/metal-cpp)
8282
target_compile_definitions(ImGui PUBLIC IMGUI_IMPL_METAL_CPP)
8383
endif()
8484

8585
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
8686
target_link_libraries(ImGui PUBLIC SDL2::SDL2 SDL2::SDL2main)
87+
elseif (CMAKE_SYSTEM_NAME STREQUAL "iOS")
88+
target_link_libraries(ImGui PUBLIC SDL2::SDL2-static SDL2::SDL2main)
8789
elseif (CMAKE_SYSTEM_NAME STREQUAL "CafeOS")
8890
target_link_libraries(ImGui PUBLIC SDL2::SDL2-static)
8991
else()
@@ -97,7 +99,7 @@ elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
9799
find_package(GLEW REQUIRED)
98100
target_link_libraries(ImGui PUBLIC ${OPENGL_opengl_LIBRARY} GLEW::GLEW)
99101
set_target_properties(ImGui PROPERTIES
100-
XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC YES
102+
XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC YES
101103
)
102104
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
103105
if (USE_OPENGLES)

include/libultraship/classes.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#endif
3737
#include "audio/SDLAudioPlayer.h"
3838
#ifdef __APPLE__
39-
#include "utils/OSXFolderManager.h"
39+
#include "utils/AppleFolderManager.h"
4040
#endif
4141
#ifdef __SWITCH__
4242
#include "port/switch/SwitchImpl.h"

src/CMakeLists.txt

+53-21
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ target_sources(libultraship PRIVATE ${Source_Files__Window__Gui})
101101

102102
file(GLOB Source_Files__Utils RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "utils/*.h" "utils/*.cpp" "utils/*.c")
103103

104-
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
105-
list(APPEND Source_Files__Utils ${CMAKE_CURRENT_SOURCE_DIR}/utils/OSXFolderManager.mm)
106-
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/utils/OSXFolderManager.mm PROPERTIES COMPILE_FLAGS -fno-objc-arc)
104+
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
105+
list(APPEND Source_Files__Utils ${CMAKE_CURRENT_SOURCE_DIR}/utils/AppleFolderManager.mm)
106+
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/utils/AppleFolderManager.mm PROPERTIES COMPILE_FLAGS -fno-objc-arc)
107107
else()
108-
list(REMOVE_ITEM Header_Files__include ${CMAKE_CURRENT_SOURCE_DIR}/utils/OSXFolderManager.h)
108+
list(REMOVE_ITEM Header_Files__include ${CMAKE_CURRENT_SOURCE_DIR}/utils/AppleFolderManager.h)
109109
endif()
110110

111111
source_group("utils" FILES ${Source_Files__Utils})
@@ -131,10 +131,10 @@ elseif (CMAKE_SYSTEM_NAME STREQUAL "NintendoSwitch")
131131
${CMAKE_CURRENT_SOURCE_DIR}/port/switch/SwitchImpl.cpp
132132
${CMAKE_CURRENT_SOURCE_DIR}/port/switch/SwitchPerformanceProfiles.h
133133
)
134-
elseif (CMAKE_SYSTEM_NAME STREQUAL "Android")
134+
elseif (CMAKE_SYSTEM_NAME STREQUAL "Android" OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
135135
set(Source_Files__Port
136-
${CMAKE_CURRENT_SOURCE_DIR}/port/android/AndroidImpl.h
137-
${CMAKE_CURRENT_SOURCE_DIR}/port/android/AndroidImpl.cpp
136+
${CMAKE_CURRENT_SOURCE_DIR}/port/mobile/MobileImpl.h
137+
${CMAKE_CURRENT_SOURCE_DIR}/port/mobile/MobileImpl.cpp
138138
)
139139
endif()
140140

@@ -167,10 +167,14 @@ if (NOT CMAKE_SYSTEM_NAME STREQUAL "Windows")
167167
list(FILTER Source_Files__Graphic EXCLUDE REGEX "graphic/Fast3D/dxsdk/*")
168168
endif()
169169

170-
if (NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
170+
if (NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND NOT CMAKE_SYSTEM_NAME STREQUAL "iOS")
171171
list(FILTER Source_Files__Graphic EXCLUDE REGEX "graphic/Fast3D/gfx_metal*")
172172
endif()
173173

174+
if (CMAKE_SYSTEM_NAME STREQUAL "iOS")
175+
list(FILTER Source_Files__Graphic EXCLUDE REGEX "graphic/Fast3D/gfx_opengl*")
176+
endif()
177+
174178
if (NOT CMAKE_SYSTEM_NAME STREQUAL "CafeOS")
175179
list(FILTER Source_Files__Graphic EXCLUDE REGEX "graphic/Fast3D/gfx_wiiu*")
176180
list(FILTER Source_Files__Graphic EXCLUDE REGEX "graphic/Fast3D/gx2*")
@@ -184,7 +188,7 @@ target_sources(libultraship PRIVATE ${Source_Files__Graphic})
184188

185189
#=================== metal-cpp ===================
186190

187-
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
191+
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
188192
target_include_directories(libultraship PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../extern/metal-cpp)
189193
endif()
190194

@@ -205,7 +209,7 @@ endif()
205209

206210
target_include_directories(libultraship
207211
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../extern ${CMAKE_CURRENT_BINARY_DIR} $<$<BOOL:${GFX_DEBUG_DISASSEMBLER}>:${CMAKE_CURRENT_SOURCE_DIR}/../../ZAPDTR/lib/libgfxd>
208-
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../extern/spdlog/include ${CMAKE_CURRENT_SOURCE_DIR}/../extern/stb
212+
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../extern/spdlog/include ${CMAKE_CURRENT_SOURCE_DIR}/../extern/stb ${ADDITIONAL_LIB_INCLUDES}
209213
)
210214

211215
if (CMAKE_SYSTEM_NAME STREQUAL "CafeOS")
@@ -248,7 +252,7 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "NintendoS
248252
target_link_libraries(libultraship PRIVATE Threads::Threads)
249253
endif()
250254

251-
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
255+
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
252256
find_Library(OSX_FOUNDATION Foundation)
253257
find_Library(OSX_AVFOUNDATION AVFoundation)
254258
find_library(METAL Metal)
@@ -277,30 +281,57 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
277281
)
278282
endif()
279283

280-
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
284+
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
281285
set_target_properties(${PROJECT_NAME} PROPERTIES
282286
XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC YES
283287
)
284288
endif()
285289

286-
if (NOT CMAKE_SYSTEM_NAME STREQUAL "CafeOS")
290+
if (CMAKE_SYSTEM_NAME STREQUAL "CafeOS")
291+
target_compile_definitions(libultraship PRIVATE
292+
$<$<CONFIG:Debug>:_DEBUG>
293+
$<$<NOT:$<CONFIG:Debug>>:NDEBUG>
294+
SPDLOG_NO_THREAD_ID
295+
SPDLOG_NO_TLS
296+
STBI_NO_THREAD_LOCALS
297+
SPDLOG_ACTIVE_LEVEL=3
298+
)
299+
elseif (CMAKE_SYSTEM_NAME STREQUAL "iOS")
287300
target_compile_definitions(libultraship PRIVATE
288-
ENABLE_OPENGL
289301
$<$<CONFIG:Debug>:_DEBUG>
290302
$<$<NOT:$<CONFIG:Debug>>:NDEBUG>
291303
$<$<CONFIG:Debug>:SPDLOG_ACTIVE_LEVEL=0>
292304
$<$<NOT:$<CONFIG:Debug>>:SPDLOG_ACTIVE_LEVEL=1>
293-
$<$<BOOL:${USE_OPENGLES}>:USE_OPENGLES>
294-
$<$<BOOL:${GFX_DEBUG_DISASSEMBLER}>:GFX_DEBUG_DISASSEMBLER>
305+
__IOS__
295306
)
296-
else ()
307+
308+
set_xcode_property(${PROJECT_NAME} PRODUCT_BUNDLE_IDENTIFIER ${BUNDLE_ID} All)
309+
310+
if(NOT SIGN_LIBRARY)
311+
set_target_properties(${PROJECT_NAME} PROPERTIES
312+
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY ""
313+
XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO"
314+
)
315+
316+
set_target_properties(SDL2 PROPERTIES
317+
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY ""
318+
XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO"
319+
)
320+
321+
set_target_properties(zip PROPERTIES
322+
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY ""
323+
XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO"
324+
)
325+
endif()
326+
else()
297327
target_compile_definitions(libultraship PRIVATE
328+
ENABLE_OPENGL
298329
$<$<CONFIG:Debug>:_DEBUG>
299330
$<$<NOT:$<CONFIG:Debug>>:NDEBUG>
300-
SPDLOG_NO_THREAD_ID
301-
SPDLOG_NO_TLS
302-
STBI_NO_THREAD_LOCALS
303-
SPDLOG_ACTIVE_LEVEL=3
331+
$<$<CONFIG:Debug>:SPDLOG_ACTIVE_LEVEL=0>
332+
$<$<NOT:$<CONFIG:Debug>>:SPDLOG_ACTIVE_LEVEL=1>
333+
$<$<BOOL:${USE_OPENGLES}>:USE_OPENGLES>
334+
$<$<BOOL:${GFX_DEBUG_DISASSEMBLER}>:GFX_DEBUG_DISASSEMBLER>
304335
)
305336
endif()
306337

@@ -341,6 +372,7 @@ if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU|Clang")
341372
-Wno-parentheses
342373
-Wno-narrowing
343374
-Wno-missing-field-initializers
375+
-Wno-implicit-function-declaration
344376
$<$<COMPILE_LANGUAGE:C,OBJC>:-Wno-int-conversion>
345377
)
346378
endif()

src/Context.cpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#endif
1313

1414
#ifdef __APPLE__
15-
#include "utils/OSXFolderManager.h"
15+
#include "utils/AppleFolderManager.h"
1616
#elif defined(__SWITCH__)
1717
#include "port/switch/SwitchImpl.h"
1818
#elif defined(__WIIU__)
@@ -216,6 +216,10 @@ void Context::InitResourceManager(const std::vector<std::string>& otrFiles,
216216
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "OTR file not found",
217217
"Main OTR file not found. Please generate one", nullptr);
218218
SPDLOG_ERROR("Main OTR file not found!");
219+
#ifdef __IOS__
220+
// We need this exit to close the app when we dismiss the dialog
221+
exit(0);
222+
#endif
219223
#endif
220224
return;
221225
}
@@ -334,6 +338,12 @@ std::string Context::GetAppBundlePath() {
334338
return externaldir;
335339
}
336340
#endif
341+
342+
#ifdef __IOS__
343+
const char* home = getenv("HOME");
344+
return std::string(home) + "/Documents";
345+
#endif
346+
337347
#ifdef NON_PORTABLE
338348
return CMAKE_INSTALL_PREFIX;
339349
#else
@@ -370,6 +380,11 @@ std::string Context::GetAppDirectoryPath(std::string appName) {
370380
}
371381
#endif
372382

383+
#ifdef __IOS__
384+
const char* home = getenv("HOME");
385+
return std::string(home) + "/Documents";
386+
#endif
387+
373388
#if defined(__linux__) || defined(__APPLE__)
374389
char* fpath = std::getenv("SHIP_HOME");
375390
if (fpath != NULL) {

0 commit comments

Comments
 (0)