Skip to content

Commit

Permalink
refactor(): initial move of android impl into runtime libary
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed May 20, 2024
1 parent 6ee86d0 commit 757b93a
Show file tree
Hide file tree
Showing 145 changed files with 11,006 additions and 6,911 deletions.
4 changes: 2 additions & 2 deletions api/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export function getCurrentWindowIndex () {
* @param {boolean=} [opts.resizable=true] - whether the window is resizable
* @param {boolean=} [opts.frameless=false] - whether the window is frameless
* @param {boolean=} [opts.utility=false] - whether the window is utility (macOS only)
* @param {boolean=} [opts.canExit=false] - whether the window can exit the app
* @param {boolean=} [opts.shouldExitApplicationOnClose=false] - whether the window can exit the app
* @param {boolean=} [opts.headless=false] - whether the window will be headless or not (no frame)
* @param {string=} [opts.userScript=null] - A user script that will be injected into the window (desktop only)
* @param {string[]=} [opts.protocolHandlers] - An array of protocol handler schemes to register with the new window (requires service worker)
Expand Down Expand Up @@ -218,7 +218,7 @@ export async function createWindow (opts) {
backgroundColorDark: opts.backgroundColorDark ?? '',
backgroundColorLight: opts.backgroundColorLight ?? '',
utility: opts.utility ?? false,
canExit: opts.canExit ?? false,
shouldExitApplicationOnClose: opts.shouldExitApplicationOnClose ?? false,
/**
* @private
* @type {number}
Expand Down
5 changes: 3 additions & 2 deletions api/commonjs/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -896,9 +896,10 @@ export class Package {
}

if (info.main) {
this.#exports['.'].require = info.main
if (info.type === 'module') {
this.#exports['.'].import = info.main
} else {
this.#exports['.'].require = info.main
}
}

Expand Down Expand Up @@ -1053,7 +1054,6 @@ export class Package {

const { info } = this
const manifest = options?.manifest ?? DEFAULT_PACKAGE_MANIFEST_FILE_NAME
const extname = path.extname(pathname)
const type = options?.type ?? this.type

if (info?.addon === true) {
Expand Down Expand Up @@ -1111,6 +1111,7 @@ export class Package {
}
}

const extname = path.extname(pathname)
const extensions = extname !== '' && this.loader.extensions.has(extname)
? new Set([extname])
: new Set(Array
Expand Down
20 changes: 20 additions & 0 deletions api/url/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,26 @@ export function format (input) {
return formatted
}

export function fileURLToPath (url) {
if (typeof url === 'string') {
url = new URL(url, globalThis.location.origin)
}

if (!(url instanceof URL)) {
throw new TypeError(
`Expecting 'url' to be a URL or string. Received: ${url}`
)
}

if (url.protocol !== 'file:' && url.protocol !== 'socket:') {
throw new TypeError(
`Expecting 'url' to have a 'file:' or 'socket:' URL scheme. Received: ${url.protocol}`
)
}

return url.pathname
}

url.serializeURLOrigin = function (input) {
const { scheme, protocol, host } = input

Expand Down
2 changes: 2 additions & 0 deletions bin/android-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,9 @@ function android_fte() {
fi

NDK_BUILD="$ANDROID_HOME/ndk/$NDK_VERSION/ndk-build$(use_bin_ext ".cmd")"
NDK_TOOLCHAINS="$ANDROID_HOME/ndk/$NDK_VERSION/toolchains"
export NDK_BUILD
export NDK_TOOLCHAINS

rc=$?
[[ -n "$set_exit_code" ]] && exit $rc
Expand Down
15 changes: 10 additions & 5 deletions bin/build-runtime-library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ declare objects=()
declare sources=(
$(find "$root"/src/app/*.cc)
$(find "$root"/src/core/*.cc)
$(find "$root"/src/ipc/*.cc)
$(find "$root"/src/core/modules/*.cc)
$(find "$root"/src/extension/*.cc)
$(find "$root"/src/ipc/*.cc)
$(find "$root"/src/platform/*.cc)
$(find "$root"/src/serviceworker/*.cc)
"$root/src/window/manager.cc"
"$root/src/window/dialog.cc"
"$root/src/window/hotkey.cc"
Expand All @@ -121,21 +124,23 @@ if [[ "$platform" = "android" ]]; then

clang="$(android_clang "$ANDROID_HOME" "$NDK_VERSION" "$host" "$host_arch" "++")"
clang_target="$(android_clang_target "$arch")"
sources+=("$root/src/process/unix.cc")
sources+=("$root/src/core/process/unix.cc")
sources+=($(find "$root/src/platform/android"/*.cc))
sources+=("$root/src/window/android.cc")
elif [[ "$host" = "Darwin" ]]; then
sources+=("$root/src/window/apple.mm")
if (( TARGET_OS_IPHONE)) || (( TARGET_IPHONE_SIMULATOR )); then
cflags=("-sdk" "iphoneos" "$clang")
clang="xcrun"
else
sources+=("$root/src/process/unix.cc")
sources+=("$root/src/core/process/unix.cc")
fi
elif [[ "$host" = "Linux" ]]; then
sources+=("$root/src/window/linux.cc")
sources+=("$root/src/process/unix.cc")
sources+=("$root/src/core/process/unix.cc")
elif [[ "$host" = "Win32" ]]; then
sources+=("$root/src/window/win.cc")
sources+=("$root/src/process/win.cc")
sources+=("$root/src/core/process/win.cc")
fi

cflags+=($(ARCH="$arch" "$root/bin/cflags.sh"))
Expand Down
7 changes: 3 additions & 4 deletions bin/cflags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ cflags+=(
$CFLAG
$CXXFLAGS
-std=c++2a
-fvisibility=hidden
-I"$root/include"
-I"$root/build/uv/include"
-I"$root/build/include"
-DSSC_BUILD_TIME="$(date '+%s')"
-DSSC_VERSION_HASH=$(git rev-parse --short=8 HEAD)
-DSSC_VERSION=$(cat "$root/VERSION.txt")
-DSOCKET_RUNTIME_BUILD_TIME="$(date '+%s')"
-DSOCKET_RUNTIME_VERSION_HASH=$(git rev-parse --short=8 HEAD)
-DSOCKET_RUNTIME_VERSION=$(cat "$root/VERSION.txt")
)

if (( TARGET_OS_IPHONE )) || (( TARGET_IPHONE_SIMULATOR )); then
Expand Down
12 changes: 12 additions & 0 deletions bin/generate-compile-flags-txt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ declare platform="desktop"
declare force=0
declare args=()

source "$root/bin/android-functions.sh"

if (( TARGET_OS_IPHONE )); then
arch="arm64"
platform="iPhoneOS"
Expand Down Expand Up @@ -43,12 +45,22 @@ while (( $# > 0 )); do
arch="aarch64"
platform="Android";
export TARGET_OS_ANDROID=1
android_fte > /dev/null
args+=("-U__APPLE__")
args+=("-D__ANDROID__=1")
args+=("--sysroot=$NDK_TOOLCHAINS/llvm/prebuilt/$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m )/sysroot")
args+=("-I$NDK_TOOLCHAINS/llvm/prebuilt/$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m )/sysroot/usr/include/$(uname -m)-$(uname -s | tr '[:upper:]' '[:lower:]')-android")
args+=("-I$NDK_TOOLCHAINS/llvm/prebuilt/$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m )/sysroot/usr/include/c++/v1")
elif [[ "$1" = "android-emulator" ]] || [[ "$1" = "AndroidEmulator" ]]; then
arch="x86_64"
platform="AndroidEmulator";
export TARGET_ANDROID_EMULATOR=1
android_fte > /dev/null
args+=("-U__APPLE__")
args+=("-D__ANDROID__=1")
args+=("--sysroot=$NDK_TOOLCHAINS/llvm/prebuilt/$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m )/sysroot")
args+=("-I$NDK_TOOLCHAINS/llvm/prebuilt/$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m )/sysroot/usr/include/$(uname -m)-$(uname -s | tr '[:upper:]' '[:lower:]')-android")
args+=("-I$NDK_TOOLCHAINS/llvm/prebuilt/$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m )/sysroot/usr/include/c++/v1")
else
platform="$1";
fi
Expand Down
30 changes: 23 additions & 7 deletions bin/generate-gradle-files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,32 @@ rm -f "$root/build.gradle" "$root/gradle.properties"
## build.gradle
cat > "$root/build.gradle" << GRADLE
buildscript {
ext.kotlin_version = '1.9.10'
ext.kotlin_version = '1.9.20'
repositories {
google()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath 'com.android.tools.build:gradle:7.4.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:\$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:\$kotlin_version"
}
}
allprojects {
ext.kotlin_version = '1.9.20'
repositories {
google()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}
}
apply plugin: 'org.jetbrains.kotlin.android'
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
android {
Expand All @@ -38,21 +44,31 @@ android {
flavorDimensions "default"
defaultConfig {
applicationId "__BUNDLE_IDENTIFIER__"
applicationId "socket.runtime"
minSdkVersion 24
targetSdkVersion 34
versionCode 1
versionName "0.0.1"
}
sourceSets {
main {
java {
srcDir 'src'
}
}
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.73'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:\$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3'
implementation 'androidx.fragment:fragment-ktx:1.7.1'
implementation 'androidx.lifecycle:lifecycle-process:2.7.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.core:core-ktx:2.2.0'
implementation 'androidx.webkit:webkit:1.8.0'
implementation 'androidx.core:core-ktx:1.13.0'
implementation 'androidx.webkit:webkit:1.9.0'
}
GRADLE

Expand All @@ -68,4 +84,4 @@ android.experimental.legacyTransform.forceNonIncremental=true
kotlin.code.style=official
GRADLE

gradle wrapper
gradle wrapper && "$root/gradlew" androidDependencies
1 change: 1 addition & 0 deletions include/socket/_user-config-bytes.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
constexpr unsigned char __socket_runtime_user_config_bytes[0] = {};
43 changes: 32 additions & 11 deletions include/socket/platform.h
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
#ifndef SOCKET_RUNTIME_PLATFORM_H
#define SOCKET_RUNTIME_PLATFORM_H

#ifndef SOCKET_RUNTIME_PLATFORM
#define SOCKET_RUNTIME_PLATFORM 1

// when this header is included, this is always `0`
#define SOCKET_RUNTIME_PLATFORM_WASM 0

#if defined(__x86_64__) || defined(_M_X64)
# define SOCKET_RUNTIME_ARCH_x64 1
# define SOCKET_RUNTIME_ARCH_ARM64 0
# define SOCKET_RUNTIME_ARCH_UNKNOWN 0
# define SOCKET_RUNTIME_PLATFORM_ARCH_x64 1
# define SOCKET_RUNTIME_PLATFORM_ARCH_ARM64 0
# define SOCKET_RUNTIME_PLATFORM_ARCH_UNKNOWN 0
#elif defined(__aarch64__) || defined(_M_ARM64)
# define SOCKET_RUNTIME_ARCH_x64 0
# define SOCKET_RUNTIME_ARCH_ARM64 1
# define SOCKET_RUNTIME_ARCH_UNKNOWN 0
# define SOCKET_RUNTIME_PLATFORM_ARCH_x64 0
# define SOCKET_RUNTIME_PLATFORM_ARCH_ARM64 1
# define SOCKET_RUNTIME_PLATFORM_ARCH_UNKNOWN 0
#elif defined(__i386__) && !defined(__ANDROID__)
#error Socket is not supported on i386.
#else
# define SOCKET_RUNTIME_ARCH_x64 0
# define SOCKET_RUNTIME_ARCH_ARM64 0
# define SOCKET_RUNTIME_ARCH_UNKNOWN 1
# define SOCKET_RUNTIME_PLATFORM_ARCH_x64 0
# define SOCKET_RUNTIME_PLATFORM_ARCH_ARM64 0
# define SOCKET_RUNTIME_PLATFORM_ARCH_UNKNOWN 1
#endif

#if defined(_WIN32)
# define SOCKET_RUNTIME_PLATFORM_NAME "win32"
# define SOCKET_RUNTIME_PLATFORM_OS "win32"
# define SOCKET_RUNTIME_PLATFORM_ANDROID 0
# define SOCKET_RUNTIME_PLATFORM_APPLE 0
# define SOCKET_RUNTIME_PLATFORM_IOS 0
# define SOCKET_RUNTIME_PLATFORM_IOS_SIMULATOR 0
# define SOCKET_RUNTIME_PLATFORM_LINUX 0
Expand All @@ -32,6 +36,7 @@
# define SOCKET_RUNTIME_PLATFORM_WINDOWS 1
#elif defined(__APPLE__)
# include <TargetConditionals.h>
# define SOCKET_RUNTIME_PLATFORM_APPLE 1
# define SOCKET_RUNTIME_PLATFORM_NAME "darwin"
# define SOCKET_RUNTIME_PLATFORM_ANDROID 0
# define SOCKET_RUNTIME_PLATFORM_IOS_SIMULATOR 0
Expand All @@ -56,26 +61,28 @@
#endif

#if defined(__unix__) || defined(unix) || defined(__unix)
# define SOCKET_RUNTIME_PLATFORM_UXIX 0
# define SOCKET_RUNTIME_PLATFORM_UXIX 1
#else
# define SOCKET_RUNTIME_PLATFORM_UXIX 0
#endif

#elif defined(__linux__)
# undef linux
# define SOCKET_RUNTIME_PLATFORM_NAME "linux"
# define SOCKET_RUNTIME_PLATFORM_APPLE 0
# define SOCKET_RUNTIME_PLATFORM_IOS 0
# define SOCKET_RUNTIME_PLATFORM_IOS_SIMULATOR 0
# define SOCKET_RUNTIME_PLATFORM_LINUX 1
# define SOCKET_RUNTIME_PLATFORM_MACOS 0
# define SOCKET_RUNTIME_PLATFORM_WINDOWS 0

#ifdef __ANDROID__
# define SOCKET_RUNTIME_PLATFORM_OS "android"
# define SOCKET_RUNTIME_PLATFORM_ANDROID 1
# define SOCKET_RUNTIME_PLATFORM_LINUX 0
#else
# define SOCKET_RUNTIME_PLATFORM_OS "linux"
# define SOCKET_RUNTIME_PLATFORM_ANDROID 0
# define SOCKET_RUNTIME_PLATFORM_LINUX 1
#endif

#if defined(__unix__) || defined(unix) || defined(__unix)
Expand Down Expand Up @@ -104,6 +111,7 @@
# define SOCKET_RUNTIME_PLATFORM_NAME "openbsd"
# define SOCKET_RUNTIME_PLATFORM_OS "openbsd"
# define SOCKET_RUNTIME_PLATFORM_ANDROID 0
# define SOCKET_RUNTIME_PLATFORM_APPLE 0
# define SOCKET_RUNTIME_PLATFORM_IOS 0
# define SOCKET_RUNTIME_PLATFORM_IOS_SIMULATOR 0
# define SOCKET_RUNTIME_PLATFORM_LINUX 0
Expand All @@ -117,4 +125,17 @@
#endif
#endif

#if SOCKET_RUNTIME_PLATFORM_ANDROID || SOCKET_RUNTIME_PLATFORM_IOS
#define SOCKET_RUNTIME_PLATFORM_MOBILE 1
#define SOCKET_RUNTIME_PLATFORM_DESKTOP 0
#else
#define SOCKET_RUNTIME_PLATFORM_MOBILE 0
#define SOCKET_RUNTIME_PLATFORM_DESKTOP 1
#endif

#if !defined(SOCKET_RUNTIME_PLATFORM_SANDBOXED)
#define SOCKET_RUNTIME_PLATFORM_SANDBOXED 0
#endif

#endif
#endif
6 changes: 3 additions & 3 deletions include/socket/webassembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#define SOCKET_RUNTIME_PLATFORM_WASM 1

// arch
#define SOCKET_RUNTIME_ARCH_x64 0
#define SOCKET_RUNTIME_ARCH_ARM64 0
#define SOCKET_RUNTIME_ARCH_UNKNOWN 1
#define SOCKET_RUNTIME_PLATFORM_ARCH_x64 0
#define SOCKET_RUNTIME_PLATFORM_ARCH_ARM64 0
#define SOCKET_RUNTIME_PLATFORM_ARCH_UNKNOWN 1

#endif
10 changes: 10 additions & 0 deletions src/android/app.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// vim: set sw=2:
package __BUNDLE_IDENTIFIER__

open class App : socket.runtime.app.App() {
companion object {
init {
socket.runtime.app.App.loadSocketRuntime()
}
}
}
Loading

0 comments on commit 757b93a

Please sign in to comment.