diff --git a/packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt b/packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt index 4d979095ccf..812919ae13a 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt +++ b/packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt @@ -67,6 +67,7 @@ add_react_common_subdir(cxxreact) add_react_common_subdir(jsc) add_react_common_subdir(jsi) add_react_common_subdir(callinvoker) +add_react_common_subdir(oscompat) add_react_common_subdir(jsinspector-modern) add_react_common_subdir(jsinspector-modern/tracing) add_react_common_subdir(hermes/executor) @@ -169,6 +170,7 @@ add_library(reactnative $ $ $ + $ $ $ $ diff --git a/packages/react-native/ReactCommon/oscompat/CMakeLists.txt b/packages/react-native/ReactCommon/oscompat/CMakeLists.txt new file mode 100644 index 00000000000..f7defd7751b --- /dev/null +++ b/packages/react-native/ReactCommon/oscompat/CMakeLists.txt @@ -0,0 +1,14 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cmake_minimum_required(VERSION 3.13) +set(CMAKE_VERBOSE_MAKEFILE on) + +add_compile_options(-fexceptions) + +file(GLOB oscompat_SRC CONFIGURE_DEPENDS *.cpp) +add_library(oscompat OBJECT ${oscompat_SRC}) + +target_include_directories(oscompat PUBLIC .) diff --git a/packages/react-native/ReactCommon/oscompat/OSCompat.h b/packages/react-native/ReactCommon/oscompat/OSCompat.h new file mode 100644 index 00000000000..2eaf9b50c7b --- /dev/null +++ b/packages/react-native/ReactCommon/oscompat/OSCompat.h @@ -0,0 +1,18 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include + +namespace facebook::react::oscompat { + +uint64_t getCurrentProcessId(); + +uint64_t getCurrentThreadId(); + +} // namespace facebook::react::oscompat diff --git a/packages/react-native/ReactCommon/oscompat/OSCompatPosix.cpp b/packages/react-native/ReactCommon/oscompat/OSCompatPosix.cpp new file mode 100644 index 00000000000..55e2ec16fc2 --- /dev/null +++ b/packages/react-native/ReactCommon/oscompat/OSCompatPosix.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#if defined(__linux__) || (defined(__APPLE__) && defined(__MACH__)) || \ + defined(__ANDROID__) + +#include "OSCompat.h" + +#include + +#include +#include + +#if defined(__MACH__) +#include +#endif // defined(__MACH__) + +#if defined(__linux__) +#include +#endif // defined(__linux__) + +namespace facebook::react::oscompat { + +uint64_t getCurrentProcessId() { + return getpid(); +} + +#if defined(__APPLE__) && defined(__MACH__) + +uint64_t getCurrentThreadId() { + uint64_t tid = 0; + auto ret = pthread_threadid_np(nullptr, &tid); + assert( + ret == 0 && + "Unexpected pthread_threadid_np failure while getting thread ID"); + (void)ret; + return tid; +} + +#elif defined(__ANDROID__) + +uint64_t getCurrentThreadId() { + return gettid(); +} + +#elif defined(__linux__) + +uint64_t getCurrentThreadId() { + return syscall(__NR_gettid); +} + +#else +#error "getCurrentThreadID() is not supported on this platform" +#endif + +} // namespace facebook::react::oscompat + +#endif // defined(__linux__) || (defined(__APPLE__) && defined(__MACH__)) || + // defined(__ANDROID__) diff --git a/packages/react-native/ReactCommon/oscompat/OSCompatWindows.cpp b/packages/react-native/ReactCommon/oscompat/OSCompatWindows.cpp new file mode 100644 index 00000000000..e70c8bdb9c5 --- /dev/null +++ b/packages/react-native/ReactCommon/oscompat/OSCompatWindows.cpp @@ -0,0 +1,26 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#if defined(_WIN32) || defined(_MSC_VER) + +#include "OSCompat.h" + +#include + +namespace facebook::react::oscompat { + +uint64_t getCurrentProcessId() { + return GetCurrentProcessId(); +} + +uint64_t getCurrentThreadId() { + return GetCurrentThreadId(); +} + +} // namespace facebook::react::oscompat + +#endif // defined(_WIN32) || defined(_MSC_VER) diff --git a/packages/react-native/ReactCommon/oscompat/React-oscompat.podspec b/packages/react-native/ReactCommon/oscompat/React-oscompat.podspec new file mode 100644 index 00000000000..1f6f1b6de01 --- /dev/null +++ b/packages/react-native/ReactCommon/oscompat/React-oscompat.podspec @@ -0,0 +1,31 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +require "json" + +package = JSON.parse(File.read(File.join(__dir__, "..", "..", "package.json"))) +version = package['version'] + +source = { :git => 'https://github.com/facebook/react-native.git' } +if version == '1000.0.0' + # This is an unpublished version, use the latest commit hash of the react-native repo, which we’re presumably in. + source[:commit] = `git rev-parse HEAD`.strip if system("git rev-parse --git-dir > /dev/null 2>&1") +else + source[:tag] = "v#{version}" +end + +Pod::Spec.new do |s| + s.name = "React-oscompat" + s.version = version + s.summary = "Small set of OS-level utilities for React Native" + s.homepage = "https://reactnative.dev/" + s.license = package["license"] + s.author = "Meta Platforms, Inc. and its affiliates" + s.platforms = min_supported_versions + s.source = source + s.source_files = "*.{cpp,h}" + s.pod_target_xcconfig = { "HEADER_SEARCH_PATHS" => "" } + s.header_dir = "oscompat" +end diff --git a/packages/react-native/scripts/cocoapods/utils.rb b/packages/react-native/scripts/cocoapods/utils.rb index 42e04fef050..12d738ededc 100644 --- a/packages/react-native/scripts/cocoapods/utils.rb +++ b/packages/react-native/scripts/cocoapods/utils.rb @@ -634,6 +634,7 @@ class ReactNativePodsUtils "React-jsinspector", "React-jsitooling", "React-logger", + "React-oscompat", "React-perflogger", "React-rncore", "React-runtimeexecutor", diff --git a/packages/react-native/scripts/react_native_pods.rb b/packages/react-native/scripts/react_native_pods.rb index 34a89192ba7..be16a3e3dde 100644 --- a/packages/react-native/scripts/react_native_pods.rb +++ b/packages/react-native/scripts/react_native_pods.rb @@ -156,6 +156,7 @@ def use_react_native! ( pod 'React-rendererdebug', :path => "#{prefix}/ReactCommon/react/renderer/debug" pod 'React-rendererconsistency', :path => "#{prefix}/ReactCommon/react/renderer/consistency" pod 'React-perflogger', :path => "#{prefix}/ReactCommon/reactperflogger" + pod 'React-oscompat', :path => "#{prefix}/ReactCommon/oscompat" pod 'React-logger', :path => "#{prefix}/ReactCommon/logger" pod 'ReactCommon/turbomodule/core', :path => "#{prefix}/ReactCommon", :modular_headers => true pod 'React-NativeModulesApple', :path => "#{prefix}/ReactCommon/react/nativemodule/core/platform/ios", :modular_headers => true diff --git a/packages/rn-tester/Podfile.lock b/packages/rn-tester/Podfile.lock index 45e52824e92..9a535b23307 100644 --- a/packages/rn-tester/Podfile.lock +++ b/packages/rn-tester/Podfile.lock @@ -1367,6 +1367,7 @@ PODS: - React-runtimeexecutor - ReactCommon/turbomodule/bridging - ReactCommon/turbomodule/core + - React-oscompat (1000.0.0) - React-perflogger (1000.0.0): - DoubleConversion - RCT-Folly (= 2024.11.18.00) @@ -1743,6 +1744,7 @@ DEPENDENCIES: - React-Mapbuffer (from `../react-native/ReactCommon`) - React-microtasksnativemodule (from `../react-native/ReactCommon/react/nativemodule/microtasks`) - React-NativeModulesApple (from `../react-native/ReactCommon/react/nativemodule/core/platform/ios`) + - React-oscompat (from `../react-native/ReactCommon/oscompat`) - React-perflogger (from `../react-native/ReactCommon/reactperflogger`) - React-performancetimeline (from `../react-native/ReactCommon/react/performance/timeline`) - React-RCTActionSheet (from `../react-native/Libraries/ActionSheetIOS`) @@ -1866,6 +1868,8 @@ EXTERNAL SOURCES: :path: "../react-native/ReactCommon/react/nativemodule/microtasks" React-NativeModulesApple: :path: "../react-native/ReactCommon/react/nativemodule/core/platform/ios" + React-oscompat: + :path: "../react-native/ReactCommon/oscompat" React-perflogger: :path: "../react-native/ReactCommon/reactperflogger" React-performancetimeline: @@ -1976,6 +1980,7 @@ SPEC CHECKSUMS: React-Mapbuffer: cd5e7720cb5fd9c0bc43ae71a952cc2a16711a5a React-microtasksnativemodule: 6af4b5b26640a12f9abc421a014f300d2a02b789 React-NativeModulesApple: 612e7d0ccf461840f010689daa86fd058aec01c5 + React-oscompat: 7133e0e945cda067ae36b22502df663d73002864 React-perflogger: 6f2b10b96094d29e1dca6be7689d975b98ba4166 React-performancetimeline: b91e898716885df30697e54eab3eb14f6b48766b React-RCTActionSheet: 1bf8cc8086ad1c15da3407dfb7bc9dd94dc7595d