Skip to content

Commit

Permalink
feat: Clangd support (#3069)
Browse files Browse the repository at this point in the history
## Description

This pull request introduces `clangd` support. You can read more about
it in
[this](software-mansion/react-native-reanimated#6430)
awesome PR that bring it to reanimated. For now it supports iOS only as
I am not sure if we need it for android here. To make it work, you have
build `FabricExample` or `MacOSExample` first.

<!--
Description and motivation for this PR.

Include 'Fixes #<number>' if this is fixing some issue.
-->


<!--
Describe how did you test this change here.
-->
  • Loading branch information
coado authored Oct 3, 2024
1 parent 752e469 commit c7c1a89
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,8 @@ dist/

# generated by bob
/lib/

# clangd
.cache
CompilationDatabase
compile_commands.json
1 change: 1 addition & 0 deletions FabricExample/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ buildscript {
targetSdkVersion = 34
ndkVersion = "26.1.10909125"
kotlinVersion = "1.9.24"
isGHExampleApp = true
}
repositories {
google()
Expand Down
14 changes: 13 additions & 1 deletion FabricExample/ios/FabricExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
5709B34CF0A7D63546082F79 /* Pods-FabricExample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FabricExample.release.xcconfig"; path = "Target Support Files/Pods-FabricExample/Pods-FabricExample.release.xcconfig"; sourceTree = "<group>"; };
5B7EB9410499542E8C5724F5 /* Pods-FabricExample-FabricExampleTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FabricExample-FabricExampleTests.debug.xcconfig"; path = "Target Support Files/Pods-FabricExample-FabricExampleTests/Pods-FabricExample-FabricExampleTests.debug.xcconfig"; sourceTree = "<group>"; };
5DCACB8F33CDC322A6C60F78 /* libPods-FabricExample.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-FabricExample.a"; sourceTree = BUILT_PRODUCTS_DIR; };
748A2AC40A2F803BAF052F38 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; includeInIndex = 1; name = PrivacyInfo.xcprivacy; path = FabricExample/PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
748A2AC40A2F803BAF052F38 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xml; name = PrivacyInfo.xcprivacy; path = FabricExample/PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = LaunchScreen.storyboard; path = FabricExample/LaunchScreen.storyboard; sourceTree = "<group>"; };
89C6BE57DB24E9ADA2F236DE /* Pods-FabricExample-FabricExampleTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FabricExample-FabricExampleTests.release.xcconfig"; path = "Target Support Files/Pods-FabricExample-FabricExampleTests/Pods-FabricExample-FabricExampleTests.release.xcconfig"; sourceTree = "<group>"; };
ED297162215061F000B7C4FE /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -189,6 +189,7 @@
00DD1BFF1BD5951E006B06BC /* Bundle React Native code and images */,
00EEFC60759A1932668264C0 /* [CP] Embed Pods Frameworks */,
E235C05ADACE081382539298 /* [CP] Copy Pods Resources */,
BDDECB0BB2719FE896F53939 /* [CP-User] Generate metadata for clangd */,
);
buildRules = (
);
Expand Down Expand Up @@ -311,6 +312,17 @@
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
BDDECB0BB2719FE896F53939 /* [CP-User] Generate metadata for clangd */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
name = "[CP-User] Generate metadata for clangd";
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/bash;
shellScript = "../../scripts/clangd-generate-xcode-metadata.sh";
};
C38B50BA6285516D6DCD4F65 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down
5 changes: 5 additions & 0 deletions FabricExample/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ require Pod::Executable.execute_command('node', ['-p',
{paths: [process.argv[1]]},
)', __dir__]).strip

require_relative '../../scripts/clangd-add-xcode-step.rb'

platform :ios, min_ios_version_supported
prepare_react_native_project!

Expand All @@ -15,6 +17,7 @@ if linkage != nil
end

ENV['RCT_NEW_ARCH_ENABLED'] = '1'
ENV['GH_EXAMPLE_APP_NAME'] = 'FabricExample'

target 'FabricExample' do
config = use_native_modules!
Expand All @@ -39,4 +42,6 @@ target 'FabricExample' do
# :ccache_enabled => true
)
end

add_clangd_generation_step()
end
44 changes: 37 additions & 7 deletions MacOSExample/macos/MacOSExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -151,6 +151,7 @@
381D8A6E24576A4E00465D17 /* Bundle React Native code and images */,
A6395851AE71BEB7A700012B /* [CP] Copy Pods Resources */,
938E3CBCFBD697407DD017DA /* [CP] Embed Pods Frameworks */,
7E6F9CFA6FD8C90D3D4BD9DA /* [CP-User] Generate metadata for clangd */,
);
buildRules = (
);
Expand Down Expand Up @@ -291,6 +292,17 @@
shellPath = /bin/sh;
shellScript = "export RCT_METRO_PORT=\"${RCT_METRO_PORT:=8081}\"\necho \"export RCT_METRO_PORT=${RCT_METRO_PORT}\" > \"${SRCROOT}/../node_modules/react-native-macos/scripts/.packager.env\"\nif [ -z \"${RCT_NO_LAUNCH_PACKAGER+xxx}\" ] ; then\n if nc -w 5 -z localhost ${RCT_METRO_PORT} ; then\n if ! curl -s \"http://localhost:${RCT_METRO_PORT}/status\" | grep -q \"packager-status:running\" ; then\n echo \"Port ${RCT_METRO_PORT} already in use, packager is either not running or not running correctly\"\n exit 2\n fi\n else\n open \"$SRCROOT/../node_modules/react-native-macos/scripts/launchPackager.command\" || echo \"Can't start packager automatically\"\n fi\nfi\n";
};
7E6F9CFA6FD8C90D3D4BD9DA /* [CP-User] Generate metadata for clangd */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = true;
buildActionMask = 2147483647;
files = (
);
name = "[CP-User] Generate metadata for clangd";
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/bash;
shellScript = "../../scripts/clangd-generate-xcode-metadata.sh";
};
938E3CBCFBD697407DD017DA /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down Expand Up @@ -389,7 +401,10 @@
CURRENT_PROJECT_VERSION = 1;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = "MacOSExample-iOS/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
OTHER_LDFLAGS = (
"$(inherited)",
"-ObjC",
Expand All @@ -410,7 +425,10 @@
CLANG_ENABLE_MODULES = YES;
CURRENT_PROJECT_VERSION = 1;
INFOPLIST_FILE = "MacOSExample-iOS/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
OTHER_LDFLAGS = (
"$(inherited)",
"-ObjC",
Expand All @@ -431,7 +449,10 @@
CURRENT_PROJECT_VERSION = 1;
DEAD_CODE_STRIPPING = NO;
INFOPLIST_FILE = "MacOSExample-macos/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
OTHER_LDFLAGS = (
"$(inherited)",
Expand All @@ -452,7 +473,10 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CURRENT_PROJECT_VERSION = 1;
INFOPLIST_FILE = "MacOSExample-macos/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
OTHER_LDFLAGS = (
"$(inherited)",
Expand Down Expand Up @@ -520,7 +544,10 @@
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD = "";
LDPLUSPLUS = "";
LD_RUNPATH_SEARCH_PATHS = "/usr/lib/swift $(inherited)";
LD_RUNPATH_SEARCH_PATHS = (
/usr/lib/swift,
"$(inherited)",
);
LIBRARY_SEARCH_PATHS = (
"$(SDKROOT)/usr/lib/swift",
"\"$(TOOLCHAIN_DIR)/usr/lib/swift/$(PLATFORM_NAME)\"",
Expand Down Expand Up @@ -587,7 +614,10 @@
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD = "";
LDPLUSPLUS = "";
LD_RUNPATH_SEARCH_PATHS = "/usr/lib/swift $(inherited)";
LD_RUNPATH_SEARCH_PATHS = (
/usr/lib/swift,
"$(inherited)",
);
LIBRARY_SEARCH_PATHS = (
"$(SDKROOT)/usr/lib/swift",
"\"$(TOOLCHAIN_DIR)/usr/lib/swift/$(PLATFORM_NAME)\"",
Expand Down
5 changes: 5 additions & 0 deletions MacOSExample/macos/Podfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require_relative '../node_modules/react-native-macos/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'
require_relative '../../scripts/clangd-add-xcode-step.rb'

prepare_react_native_project!

ENV['GH_EXAMPLE_APP_NAME'] = 'MacOSExample'

target 'MacOSExample-macOS' do
platform :macos, '10.15'

Expand Down Expand Up @@ -34,4 +37,6 @@ target 'MacOSExample-macOS' do
end
end
end

add_clangd_generation_step()
end
16 changes: 8 additions & 8 deletions MacOSExample/macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ PODS:
- FBLazyVector (0.74.6)
- fmt (9.1.0)
- glog (0.3.5)
- hermes-engine (0.74.5):
- hermes-engine/Pre-built (= 0.74.5)
- hermes-engine/Pre-built (0.74.5)
- hermes-engine (0.74.6):
- hermes-engine/Pre-built (= 0.74.6)
- hermes-engine/Pre-built (0.74.6)
- RCT-Folly (2024.01.01.00):
- boost
- DoubleConversion
Expand Down Expand Up @@ -1443,11 +1443,11 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
boost: 0686b6af8cbd638c784fea5afb789be66699823c
DoubleConversion: 5b92c4507c560bb62e7aa1acdf2785ea3ff08b3b
DoubleConversion: acaf5db79676d2e9119015819153f0f99191de12
FBLazyVector: 8f41053475f558b29633b434bd62929813a38560
fmt: 03574da4b7ba40de39da59677ca66610ce8c4a02
glog: ba31c1afa7dcf1915a109861bccdb4421be6175b
hermes-engine: 8c1577f3fdb849cbe7729c2e7b5abc4b845e88f8
glog: 6df0a3d6e2750a50609471fd1a01fd2948d405b5
hermes-engine: 2102c92e54a031a270fd1fe84169ec8a0901b7bd
RCT-Folly: 2edbb9597acadc2312235c7ad6243d49852047a3
RCTDeprecation: 5f1d7e1f8ef6c53f0207e3ac0d0ca23575e8a6ab
RCTRequired: 200cf97fbe1ff2c9573d9f7b2243b6bd9330b6b8
Expand Down Expand Up @@ -1496,12 +1496,12 @@ SPEC CHECKSUMS:
React-utils: d1f30e28b14bea6aa6b009be03ab502bbf2cf5c6
ReactCommon: 68cae4af53cf2d34e6a26c0099f434f170495dd1
RNCAsyncStorage: ec53e44dc3e75b44aa2a9f37618a49c3bc080a7a
RNGestureHandler: 28078232f2868d3dcb4d4b6c8ab8691700187664
RNGestureHandler: 742f027eb4bca8033a2d43582358645c71d5f5eb
RNReanimated: 45553a3ae29a75a76269595f8554d07d4090e392
RNSVG: 4590aa95758149fa27c5c83e54a6a466349a1688
SocketRocket: f6c6249082c011e6de2de60ed641ef8bbe0cfac9
Yoga: 2b5a8ace4c52fd28c52de828b9a66aede21c3a9c

PODFILE CHECKSUM: a03a1b37f876fddd02813d86862de4a117e90221
PODFILE CHECKSUM: 3d6bcfcb4beca113bbf03f2652ec9272037c8111

COCOAPODS: 1.15.2
9 changes: 9 additions & 0 deletions RNGestureHandler.podspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
require "json"


is_gh_example_app = ENV["GH_EXAMPLE_APP_NAME"] != nil

compilation_metadata_dir = "CompilationDatabase"
compilation_metadata_generation_flag = is_gh_example_app ? '-gen-cdb-fragment-path ' + compilation_metadata_dir : ''

Pod::Spec.new do |s|
# NPM package specification
package = JSON.parse(File.read(File.join(File.dirname(__FILE__), "package.json")))
Expand All @@ -14,6 +20,9 @@ Pod::Spec.new do |s|
s.source_files = "apple/**/*.{h,m,mm}"
s.requires_arc = true
s.platforms = { ios: '11.0', tvos: '11.0', osx: '10.15', visionos: '1.0' }
s.xcconfig = {
"OTHER_CFLAGS" => "$(inherited) " + compilation_metadata_generation_flag
}

if defined?(install_modules_dependencies()) != nil
install_modules_dependencies(s);
Expand Down
19 changes: 19 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import groovy.json.JsonSlurper
import com.android.build.gradle.tasks.ExternalNativeBuildJsonTask

buildscript {
def kotlin_version = rootProject.ext.has('kotlinVersion') ? rootProject.ext.get('kotlinVersion') : project.properties['RNGH_kotlinVersion']
Expand Down Expand Up @@ -27,6 +28,10 @@ def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

def isGHExampleApp() {
return safeExtGet("isGHExampleApp", false)
}

def resolveReactNativeDirectory() {
def reactNativeLocation = safeExtGet("REACT_NATIVE_NODE_MODULES_DIR", null)
if (reactNativeLocation != null) {
Expand Down Expand Up @@ -170,6 +175,20 @@ android {
}
}
}

if (isGHExampleApp()) {
tasks.withType(ExternalNativeBuildJsonTask) {
compileTask ->
compileTask.doLast {
def rootDir = new File("${project.projectDir}/..")
def generated = new File("${compileTask.abi.getCxxBuildFolder()}/compile_commands.json")
def output = new File("${rootDir}/compile_commands.json")
output.text = generated.text

println("Generated clangd metadata.")
}
}
}
}

def kotlin_version = safeExtGet('kotlinVersion', project.properties['RNGH_kotlinVersion'])
Expand Down
5 changes: 5 additions & 0 deletions scripts/clangd-add-xcode-step.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def add_clangd_generation_step()
generator_script_path = '../../scripts/clangd-generate-xcode-metadata.sh'

script_phase :name => 'Generate metadata for clangd', :script => generator_script_path, :shell_path => '/bin/bash', :always_out_of_date => 'true', :execution_position => :after_compile
end
9 changes: 9 additions & 0 deletions scripts/clangd-generate-xcode-metadata.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

COMPILE_COMMANDS_PATH="../../compile_commands.json"

echo "[" >$COMPILE_COMMANDS_PATH
for f in **/CompilationDatabase/*.json; do cat "$f" >>$COMPILE_COMMANDS_PATH; done
echo "]" >>$COMPILE_COMMANDS_PATH

echo "Generated clangd metadata."

0 comments on commit c7c1a89

Please sign in to comment.