Skip to content

Commit

Permalink
Add CMake
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Feb 20, 2024
1 parent 07cf460 commit 6188bd7
Show file tree
Hide file tree
Showing 27 changed files with 5,859 additions and 45 deletions.
51 changes: 51 additions & 0 deletions package/android/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
project(RNFilament)
cmake_minimum_required(VERSION 3.9.0)

set(PACKAGE_NAME "RNFilament")
set(BUILD_DIR ${CMAKE_SOURCE_DIR}/build)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_STANDARD 17)

# Third party libraries (Prefabs)
find_package(ReactAndroid REQUIRED CONFIG)
find_package(fbjni REQUIRED CONFIG)
find_library(LOG_LIB log)

# Add react-native-filament sources
add_library(
${PACKAGE_NAME}
SHARED
# Shared C++
../cpp/MutableRawBuffer.cpp
../cpp/FilamentProxy.cpp
../cpp/jsi/Promise.cpp
# Java JNI
src/main/cpp/AndroidFilamentProxy.cpp
src/main/cpp/Filament.cpp
src/main/cpp/FilamentInstaller.cpp
src/main/cpp/java-bindings/JFilamentProxy.cpp
)

# Header Search Paths (includes)
target_include_directories(
${PACKAGE_NAME}
PRIVATE
"../cpp"
"src/main/cpp"
"src/main/cpp/java-bindings"
"${NODE_MODULES_DIR}/react-native/ReactCommon"
"${NODE_MODULES_DIR}/react-native/ReactCommon/callinvoker"
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule" # <-- CallInvokerHolder JNI wrapper
)

# Link everything together
target_link_libraries(
${PACKAGE_NAME}
${LOG_LIB} # <-- Logcat logger
android # <-- Android JNI core
ReactAndroid::jsi # <-- RN: JSI
ReactAndroid::reactnativejni # <-- RN: React Native JNI bindings
fbjni::fbjni # <-- fbjni
GLESv2 # <-- OpenGL (Core)
EGL # <-- OpenGL (EGL)
)
52 changes: 50 additions & 2 deletions package/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.nio.file.Paths

buildscript {
repositories {
google()
Expand Down Expand Up @@ -36,9 +38,26 @@ def supportsNamespace() {
return (major == 7 && minor >= 3) || major >= 8
}

static def findNodeModules(baseDir) {
def basePath = baseDir.toPath().normalize()
// Node"s module resolution algorithm searches up to the root directory,
// after which the base path will be null
while (basePath) {
def nodeModulesPath = Paths.get(basePath.toString(), "node_modules")
def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native")
if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) {
return nodeModulesPath.toString()
}
basePath = basePath.getParent()
}
throw new GradleException("react-native-filament: Failed to find node_modules/ path!")
}

def nodeModules = findNodeModules(projectDir)

android {
if (supportsNamespace()) {
namespace "com.filament"
namespace "com.margelo.filament"

sourceSets {
main {
Expand All @@ -54,10 +73,24 @@ android {
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()

externalNativeBuild {
cmake {
cppFlags "-O2 -frtti -fexceptions -Wall -Wno-unused-variable -fstack-protector-all"
arguments "-DANDROID_STL=c++_shared",
"-DNODE_MODULES_DIR=${nodeModules}"
}
}
}

externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}

buildFeatures {
buildConfig true
prefab true
}

buildTypes {
Expand Down Expand Up @@ -107,6 +140,21 @@ if (isNewArchitectureEnabled()) {
react {
jsRootDir = file("../src/")
libraryName = "FilamentView"
codegenJavaPackageName = "com.filament"
codegenJavaPackageName = "com.margelo.filament"
}
}

task deleteCmakeCache() {
doFirst {
delete "${projectDir}/.cxx"
delete "${nodeModules}/react-native-filament/android/.cxx"
delete "${nodeModules}/react-native-filament/android/build"
}
}

tasks.configureEach { task ->
// C++ clean
if (task.name.contains("clean")) {
task.dependsOn(deleteCmakeCache)
}
}
2 changes: 1 addition & 1 deletion package/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.filament">
package="com.margelo.filament">
</manifest>
4 changes: 2 additions & 2 deletions package/android/src/main/cpp/AndroidFilamentProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace margelo {

using namespace facebook;

explicit AndroidFilamentProxy::AndroidFilamentProxy(jni::alias_ref<JFilamentProxy> filamentProxy):
_filamentProxy(jni::make_global(filamentProxy)) { }
explicit AndroidFilamentProxy::AndroidFilamentProxy(jni::alias_ref<JFilamentProxy> filamentProxy)
: _filamentProxy(jni::make_global(filamentProxy)) {}

AndroidFilamentProxy::~AndroidFilamentProxy() {
// Hermes GC might destroy HostObjects on an arbitrary Thread which might not be
Expand Down
4 changes: 1 addition & 3 deletions package/android/src/main/cpp/Filament.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@
#include <jni.h>

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
return facebook::jni::initialize(vm, [] {
margelo::FilamentInstaller::registerNatives();
});
return facebook::jni::initialize(vm, [] { margelo::FilamentInstaller::registerNatives(); });
}
15 changes: 6 additions & 9 deletions package/android/src/main/cpp/java-bindings/JFilamentProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ namespace margelo {

using namespace facebook;


JFilamentProxy::JFilamentProxy(const jni::alias_ref<JFilamentProxy::jhybridobject>& javaThis,
jsi::Runtime* runtime,
const std::shared_ptr<facebook::react::CallInvoker>& callInvoker):
_javaPart(make_global(javaThis)), _runtime(runtime), _callInvoker(callInvoker) { }

JFilamentProxy::JFilamentProxy(const jni::alias_ref<JFilamentProxy::jhybridobject>& javaThis, jsi::Runtime* runtime,
const std::shared_ptr<facebook::react::CallInvoker>& callInvoker)
: _javaPart(make_global(javaThis)), _runtime(runtime), _callInvoker(callInvoker) {}

JFilamentProxy::~JFilamentProxy() {
// TODO(hanno): Cleanup?
Expand All @@ -29,9 +26,9 @@ void JFilamentProxy::registerNatives() {
registerHybrid({makeNativeMethod("initHybrid", JFilamentProxy::initHybrid)});
}

jni::local_ref<JFilamentProxy::jhybriddata> JFilamentProxy::initHybrid(alias_ref<jhybridobject> jThis,
jlong jsRuntimePointer,
jni::alias_ref<facebook::react::CallInvokerHolder::javaobject> jsCallInvokerHolder) {
jni::local_ref<JFilamentProxy::jhybriddata>
JFilamentProxy::initHybrid(alias_ref<jhybridobject> jThis, jlong jsRuntimePointer,
jni::alias_ref<facebook::react::CallInvokerHolder::javaobject> jsCallInvokerHolder) {
__android_log_write(ANDROID_LOG_INFO, TAG, "Initializing JFilamentProxy...");

// cast from JNI hybrid objects to C++ instances
Expand Down
12 changes: 5 additions & 7 deletions package/android/src/main/cpp/java-bindings/JFilamentProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

#pragma once

#include <ReactCommon/CallInvokerHolder.h>
#include <fbjni/fbjni.h>
#include <jsi/jsi.h>
#include <jni.h>
#include <ReactCommon/CallInvokerHolder.h>
#include <jsi/jsi.h>

namespace margelo {

Expand Down Expand Up @@ -35,12 +35,10 @@ class JFilamentProxy : public jni::HybridClass<JFilamentProxy> {
static auto constexpr kJavaDescriptor = "Lcom/margelo/filament/FilamentProxy;";

private:
explicit JFilamentProxy(const jni::alias_ref<JFilamentProxy::jhybridobject>& javaThis,
jsi::Runtime* jsRuntime,
explicit JFilamentProxy(const jni::alias_ref<JFilamentProxy::jhybridobject>& javaThis, jsi::Runtime* jsRuntime,
const std::shared_ptr<facebook::react::CallInvoker>& jsCallInvoker);
static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jhybridobject> javaThis,
jlong jsRuntimePointer,
static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jhybridobject> javaThis, jlong jsRuntimePointer,
jni::alias_ref<facebook::react::CallInvokerHolder::javaobject> jsCallInvokerHolder);
};

} // namespace vision
} // namespace margelo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.margelo.filament;

class FilamentProxy {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.filament;
package com.margelo.filament;

import androidx.annotation.Nullable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.filament;
package com.margelo.filament;

import android.graphics.Color;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

package com.filament;
package com.margelo.filament;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
Expand Down
2 changes: 1 addition & 1 deletion package/android/src/newarch/FilamentViewManagerSpec.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.filament;
package com.margelo.filament;

import android.view.View;

Expand Down
2 changes: 1 addition & 1 deletion package/android/src/oldarch/FilamentViewManagerSpec.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.filament;
package com.margelo.filament;

import android.view.View;

Expand Down
2 changes: 1 addition & 1 deletion package/cpp/FilamentProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include "FilamentProxy.h"
#include <jsi/jsi.h>

#include "jsi/Promise.h"
#include <memory>
#include <string>
#include <vector>
#include "jsi/Promise.h"

namespace margelo {

Expand Down
2 changes: 1 addition & 1 deletion package/cpp/FilamentProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ class FilamentProxy : public jsi::HostObject {
jsi::Value get(jsi::Runtime& runtime, const jsi::PropNameID& name) override;
};

} // namespace vision
} // namespace margelo
9 changes: 3 additions & 6 deletions package/cpp/jsi/Promise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ using namespace facebook;
Promise::Promise(jsi::Runtime& runtime, jsi::Value resolver, jsi::Value rejecter)
: runtime(runtime), _resolver(std::move(resolver)), _rejecter(std::move(rejecter)) {}

jsi::Value Promise::createPromise(jsi::Runtime& runtime,
std::function<void(std::shared_ptr<Promise> promise)> run) {
jsi::Value Promise::createPromise(jsi::Runtime& runtime, std::function<void(std::shared_ptr<Promise> promise)> run) {
// Get Promise ctor from global
auto promiseCtor = runtime.global().getPropertyAsFunction(runtime, "Promise");

auto promiseCallback = jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forUtf8(runtime, "PromiseCallback"), 2,
[=](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
size_t count) -> jsi::Value {
[=](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
// Call function
auto promise = std::make_shared<Promise>(runtime, arguments[0].asObject(runtime),
arguments[1].asObject(runtime));
auto promise = std::make_shared<Promise>(runtime, arguments[0].asObject(runtime), arguments[1].asObject(runtime));
run(promise);

return jsi::Value::undefined();
Expand Down
3 changes: 1 addition & 2 deletions package/cpp/jsi/Promise.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ class Promise {
/**
Create a new Promise and runs the given `run` function.
*/
static jsi::Value createPromise(jsi::Runtime& runtime,
std::function<void(std::shared_ptr<Promise> promise)> run);
static jsi::Value createPromise(jsi::Runtime& runtime, std::function<void(std::shared_ptr<Promise> promise)> run);
};

} // namespace margelo
4 changes: 2 additions & 2 deletions package/example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ android {
buildToolsVersion rootProject.ext.buildToolsVersion
compileSdk rootProject.ext.compileSdkVersion

namespace "com.filamentexample"
namespace "com.margelo.filamentexample"
defaultConfig {
applicationId "com.filamentexample"
applicationId "com.margelo.filamentexample"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.filamentexample
package com.margelo.filamentexample

import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.filamentexample
package com.margelo.filamentexample

import android.app.Application
import com.facebook.react.PackageList
Expand Down
2 changes: 2 additions & 0 deletions package/example/ios/.xcode.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export NODE_BINARY=/Users/mrousavy/.nvm/versions/node/v18.17.0/bin/node

18 changes: 16 additions & 2 deletions package/example/ios/FilamentExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = "";
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = i386;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
Expand Down Expand Up @@ -572,14 +572,21 @@
);
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = (
"$(OTHER_CFLAGS)",
"-DFOLLY_NO_CONFIG",
"-DFOLLY_MOBILE=1",
"-DFOLLY_USE_LIBCPP=1",
"-DFOLLY_CFG_NO_COROUTINES=1",
);
OTHER_LDFLAGS = (
"$(inherited)",
" ",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
USE_HERMES = true;
};
name = Debug;
};
Expand Down Expand Up @@ -616,7 +623,7 @@
COPY_PHASE_STRIP = YES;
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = "";
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = i386;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
Expand All @@ -636,14 +643,21 @@
"\"$(inherited)\"",
);
MTL_ENABLE_DEBUG_INFO = NO;
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = (
"$(OTHER_CFLAGS)",
"-DFOLLY_NO_CONFIG",
"-DFOLLY_MOBILE=1",
"-DFOLLY_USE_LIBCPP=1",
"-DFOLLY_CFG_NO_COROUTINES=1",
);
OTHER_LDFLAGS = (
"$(inherited)",
" ",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
USE_HERMES = true;
VALIDATE_PRODUCT = YES;
};
name = Release;
Expand Down
Loading

0 comments on commit 6188bd7

Please sign in to comment.