Skip to content

Commit

Permalink
Build and run
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Sep 19, 2023
1 parent a284d29 commit 6c53039
Show file tree
Hide file tree
Showing 238 changed files with 24,605 additions and 1,525 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
cmake-build-debug
.DS_Store
build_switch
112 changes: 103 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ cmake_dependent_option(USE_SYSTEM_TINYXML2 "" OFF "NOT USE_SHARED_LIB" ON)

if (APPLE AND PLATFORM_DESKTOP)
option(BUNDLE_MACOS_APP "Bundle a app for macOS" OFF)
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64" CACHE STRING "" FORCE)
set(CMAKE_OSX_ARCHITECTURES "arm64" CACHE STRING "" FORCE)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15" CACHE STRING "" FORCE)
set(CMAKE_XCODE_ATTRIBUTE_MACOSX_DEPLOYMENT_TARGET[arch=arm64] "11.0" CACHE STRING "" FORCE)
message(STATUS "CMAKE_OSX_ARCHITECTURES: ${CMAKE_OSX_ARCHITECTURES}")
Expand All @@ -24,25 +24,38 @@ include(${EXTERN_PATH}/cmake/toolchain.cmake)

# project info
project(borealis_demo)
set(VERSION_MAJOR "1")
set(VERSION_MINOR "0")
set(VERSION_ALTER "0")
set(VERSION_MAJOR "0")
set(VERSION_MINOR "13")
set(VERSION_ALTER "3")
set(VERSION_BUILD "0")
set(PACKAGE_NAME "com.borealis.demo")
set(VITA_TITLEID "BOREALIS0")
set(VITA_VERSION "01.00")
set(PROJECT_AUTHOR "borealis")
set(PROJECT_ICON ${CMAKE_CURRENT_SOURCE_DIR}/resources/img/demo_icon.jpg)
set(PROJECT_ICON ${CMAKE_CURRENT_SOURCE_DIR}/resources/img/moonlight_icon.jpg)
set(PROJECT_RESOURCES ${CMAKE_CURRENT_SOURCE_DIR}/resources)

add_definitions(
-DAPP_VERSION="${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_ALTER}"
-DUSE_MBEDTLS_CRYPTO
-DMULTICAST_DISABLED
-Dcimg_display=0
-Dcimg_use_png
-DBRLS_I18N_PREFIX=\"main/\")

if (USE_LIBROMFS)
add_libromfs(${PROJECT_NAME} ${PROJECT_RESOURCES})
endif ()

add_subdirectory(extern)

# setting src and include
file(GLOB_RECURSE MAIN_SRC app/*.cpp)
file(GLOB_RECURSE MAIN_SRC app/src/*.cpp)

# Fmt
list(APPEND MAIN_SRC
${BOREALIS_LIBRARY}/lib/extern/fmt/src/format.cc
${BOREALIS_LIBRARY}/lib/extern/fmt/src/os.cc)

set(PLATFORM_OPTION)
if (PLATFORM_DESKTOP)
Expand All @@ -59,7 +72,10 @@ elseif (PLATFORM_SWITCH)
# base lib
nx m
)
list(APPEND MAIN_SRC ${CMAKE_SOURCE_DIR}/library/lib/platforms/switch/switch_wrapper.c)
list(APPEND MAIN_SRC
${BOREALIS_LIBRARY}/lib/platforms/switch/switch_wrapper.c
app/src/switch/wrapper.c
)
elseif (PLATFORM_PSV)
add_definitions(-D__psp2__ -D__PSV__)
endif ()
Expand Down Expand Up @@ -120,6 +136,7 @@ elseif (PLATFORM_SWITCH)
COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_BINARY_DIR}/resources/font
COMMAND ${NX_ELF2NRO_EXE} ${PROJECT_NAME}.elf ${PROJECT_NAME}.nro --icon=${PROJECT_ICON} --nacp=${PROJECT_NAME}.nacp --romfsdir=${CMAKE_BINARY_DIR}/resources
)
list(APPEND APP_PLATFORM_LIB swresample dav1d z)
elseif (PLATFORM_IOS)
ios_bundle(
"${CMAKE_CURRENT_SOURCE_DIR}/app/ios/tvos/Splash.storyboard"
Expand All @@ -130,6 +147,83 @@ elseif (PLATFORM_IOS)
"${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_ALTER}")
endif ()

target_include_directories(${PROJECT_NAME} PRIVATE app ${APP_PLATFORM_INCLUDE})
target_include_directories(${PROJECT_NAME} PUBLIC
app/include
app/include/gestures
app/src/crypto
app/src/gestures
app/src/keyboards
app/src/libgamestream
app/src/streaming
app/src/streaming/audio
app/src/streaming/ffmpeg
app/src/streaming/switch
app/src/streaming/video
app/src/streaming/video/deko3d
app/src/streaming/video/OpenGL
app/src/utils
extern/CImg
extern/zeroconf
${MBEDTLS_INCLUDE_DIRS}
${APP_PLATFORM_INCLUDE})
target_compile_options(${PROJECT_NAME} PRIVATE -ffunction-sections -fdata-sections ${APP_PLATFORM_OPTION})
target_link_libraries(${PROJECT_NAME} PRIVATE borealis moonlight-common-c CImg ${APP_PLATFORM_LIB})



find_package(PkgConfig REQUIRED)

pkg_check_modules(jansson REQUIRED IMPORTED_TARGET jansson)
add_library(libjansson ALIAS PkgConfig::jansson)

pkg_check_modules(opus REQUIRED IMPORTED_TARGET opus)
add_library(libopus ALIAS PkgConfig::opus)

#pkg_check_modules(fmt REQUIRED IMPORTED_TARGET fmt)
#add_library(libfmt ALIAS PkgConfig::fmt)

pkg_check_modules(expat REQUIRED IMPORTED_TARGET expat)
add_library(libexpat ALIAS PkgConfig::expat)

find_library(PNG_LIBRARY png)
message("png: ${PNG_LIBRARY}")

find_library(CURL_LIBRARY curl)
message("curl: ${CURL_LIBRARY}")

find_library(SDL2_LIBRARY sdl2)
message("sdl2: ${SDL2_LIBRARY}")

find_library(MBEDTLS_LIBRARY mbedtls)
message("mbedtls: ${MBEDTLS_LIBRARY}")

find_library(MBEDX509_LIBRARY mbedx509)
message("mbedx509: ${MBEDTLS_LIBRARY}")

find_library(MBEDCRYPTO_LIBRARY mbedcrypto)
message("mbedcrypto: ${MBEDTLS_LIBRARY}")

find_library(AVCODEC_LIBRARY avcodec)
message("avcodec: ${AVCODEC_LIBRARY}")

find_library(AVFORMAT_LIBRARY avformat)
message("avformat: ${AVFORMAT_LIBRARY}")

find_library(AVUTIL_LIBRARY avutil)
message("avutil: ${AVUTIL_LIBRARY}")

target_link_libraries(${PROJECT_NAME} PRIVATE
borealis
moonlight-common-c
libjansson
libexpat
${CURL_LIBRARY}
${MBEDTLS_LIBRARY}
${MBEDX509_LIBRARY}
${MBEDCRYPTO_LIBRARY}
${AVCODEC_LIBRARY}
${AVUTIL_LIBRARY}
${AVFORMAT_LIBRARY}
libopus
${SDL2_LIBRARY}
${PNG_LIBRARY}
${APP_PLATFORM_LIB})
36 changes: 36 additions & 0 deletions android-project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Gradle files
.gradle/
build/

# Local configuration file (sdk path, etc)
local.properties

# Log/OS Files
*.log

# Android Studio generated files and folders
captures/
.externalNativeBuild/
.cxx/
*.apk
output.json

# IntelliJ
*.iml
.idea/
misc.xml
deploymentTargetDropDown.xml
render.experimental.xml

# Keystore files
*.jks
*.keystore

# Google Services (e.g. APIs or Firebase)
google-services.json

# Android Profiling
*.hprof

# libromfs generator
libromfs-generator
55 changes: 55 additions & 0 deletions android-project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# borealis android demo

[中文](./README_ZH.md)

The Android port of borealis follows SDL's documentation: [SDL/docs](https://github.com/libsdl-org/SDL/blob/release-2.28.x/docs/README-android.md)

Currently, this project uses SDL 2.28.*, but there are some interesting Android-specific features in SDL3. We plan to migrate to SDL3 once it is officially released.

Theoretically, it supports Android API 16 and later (Android 4.1+), but I have only tested it on devices running Android 5.0+ because I couldn't find older devices.

## Building

```shell
cd android-project
```

On the Android platform, borealis uses libromfs to package all resource files directly into the shared library. We need to build the libromfs-generator first.

> The libromfs-generator is responsible for converting resource files into C++ source code and linking them into the main program during the compilation process.
```shell
./build_libromfs_generator.sh
```

After a successful run, `libromfs-generator` will be generated in the `app/jni` directory.

Then open Android Studio and build the project.

```shell
# macOS: compile and install from the command line
export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jbr/Contents/Home
export ANDROID_SDK_ROOT=~/Library/Android/sdk
# Once built, the APK will be located in the app/build/outputs/apk/debug directory by default
./gradlew assembleDebug
# Directly install the APK (requires the device or emulator to be connected via adb)
./gradlew installDebug
```

## Migrating your borealis project to Android

1. Place your project under the `app/jni` directory.
2. Modify `app/jni/CMakeLists.txt` and replace the relevant parts of borealis with your project.
3. Modify the `applicationId` in `app/build.gradle` and change it to your package name.
4. Create your own activity by imitating `com.borealis.demo` in `app/src/main/java`.
5. Modify `app/src/main/AndroidManifest.xml` and change the package to your package name, and change `DemoActivity` to the name of your created activity.
6. Modify the app name in `app/src/main/res/values/strings.xml`.
7. Replace `app/src/main/res/mipmap-*/ic_launcher.png` with your application icon.

You can refer to the early commit history of this project to get an idea of what needs to be modified.

## Known Issues

If you delete some resource files, libromfs may not recognize it, causing a compilation error. In such cases, a temporary solution is to:

Make a minor adjustment in `app/jni/CMakeLists.txt` (add an empty line), to trigger cmake to run again.
53 changes: 53 additions & 0 deletions android-project/README_ZH.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# borealis android demo

borealis 的安卓移植遵循 SDL 的文档:[SDL/docs](https://github.com/libsdl-org/SDL/blob/release-2.28.x/docs/README-android.md)

目前本项目使用 SDL 2.28.* ,在 SDL3 中有一些关于安卓的有趣的特性,预计在其正式发布后再进行迁移

理论上支持 Android API 16 及之后机型 (Android 4.1+),但我只在 Android 5.0+ 的设备上测试过,因为我实在找不到更老的设备了。

## 构建

```shell
cd android-project
```

在安卓平台,borealis 使用 libromfs 将所有资源文件直接打包到共享库中,我们需要先构建 libromfs-generator。

> libromfs-generator 的作用是将资源文件转换为 cpp 源码,并在后续的编译过程中链接进主程序,
```shell
./build_libromfs_generator.sh
```

在成功运行后,`app/jni` 目录下会生成: `libromfs-generator`

然后打开 Android Studio 编译即可

```shell
# 在 mac 下我使用这样的命令以从命令行编译和安装
export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jbr/Contents/Home
export ANDROID_SDK_ROOT=~/Library/Android/sdk
# 构建好之后,apk 默认会在 app/build/outputs/apk/debug 目录下
./gradlew assembleDebug
# 直接进行安装 (需要手机或模拟器已经通过 adb 连接)
./gradlew installDebug
```

## 如何让你的 borealis 项目迁移到安卓

1. 将你的项目放置在 app/jni 目录下
2. 修改 app/jni/CMakeLists.txt 把原本 borealis 的部分替换成你的项目
3. 修改 app/build.gradle 中的 applicationId,把它改为你的包名
4. 模仿 app/src/main/java 下的 com.borealis.demo 创建你的 Activity
5. 修改 app/src/main/AndroidMainifest.xml,将 package 改为你的包名,将DemoActivity改为你创建的 Activity 名
6. 修改 app/src/main/res/values/strings.xml 中的软件名
7. 修改 app/src/main/res/mipmap-*/ic_launcher.png 为你的应用图标

你可以查阅这个项目的早期提交记录来获得需要修改的内容大概范围

## 存在的问题

如果删除了某些资源文件,libromfs 可能没有识别出来,导致编译报错,这时候临时的解决方法是:

微调一下 app/jni/CMakeLists.txt (随意加一空行即可),以触发 cmake 重新运行
76 changes: 76 additions & 0 deletions android-project/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
def buildAsLibrary = project.hasProperty('BUILD_AS_LIBRARY');
def buildAsApplication = !buildAsLibrary
if (buildAsApplication) {
apply plugin: 'com.android.application'
}
else {
apply plugin: 'com.android.library'
}

android {
compileSdkVersion 31
defaultConfig {
if (buildAsApplication) {
applicationId "com.borealis.demo"
}
minSdkVersion 16
targetSdkVersion 31
versionCode 1
versionName "1.0"
// borealis need at least ndk r22 (r21 don't have std::filesystem support)
// The newer NDK library will cause lower versions of Android failed to run.
// r26: support api21 and later
// r24/r25: support api19 and later
// r22/23: support api16 and later
ndkVersion "22.1.7171670"
externalNativeBuild {
cmake {
arguments "-DANDROID_APP_PLATFORM=android-16", "-DANDROID_STL=c++_static"
// abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
abiFilters 'armeabi-v7a', 'arm64-v8a'
version = '3.16.0+'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
applicationVariants.all { variant ->
tasks["merge${variant.name.capitalize()}Assets"]
.dependsOn("externalNativeBuild${variant.name.capitalize()}")
}
if (!project.hasProperty('EXCLUDE_NATIVE_LIBS')) {
sourceSets.main {
jniLibs.srcDir 'libs'
}
externalNativeBuild {
cmake {
path 'jni/CMakeLists.txt'
version = '3.16.0+'
}
}

}
lintOptions {
abortOnError false
}

if (buildAsLibrary) {
libraryVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith(".aar")) {
def fileName = "org.libsdl.app.aar";
output.outputFile = new File(outputFile.parent, fileName);
}
}
}
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
}
1 change: 1 addition & 0 deletions android-project/app/jni/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(call all-subdir-makefiles)
Loading

0 comments on commit 6c53039

Please sign in to comment.