Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add definition to check if generated CMake is building the code #466

Merged
merged 3 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NitroConfig } from '../../config/NitroConfig.js'
import { indent, toUnixPath } from '../../utils.js'
import { indent, toLowerCamelCase, toUnixPath } from '../../utils.js'
import {
createFileMetadataString,
getRelativeDirectory,
Expand All @@ -13,6 +13,12 @@ export interface CMakeFile extends Omit<SourceFile, 'language'> {
language: 'cmake'
}

export function getBuildingWithGeneratedCmakeDefinition(): string {
const moduleName = NitroConfig.getAndroidCxxLibName()
const upper = toLowerCamelCase(moduleName).toUpperCase()
return `BUILDING_${upper}_WITH_GENERATED_CMAKE_PROJECT`
}

export function createCMakeExtension(files: SourceFile[]): CMakeFile {
const name = NitroConfig.getAndroidCxxLibName()
const sharedFiles = files
Expand All @@ -30,6 +36,7 @@ export function createCMakeExtension(files: SourceFile[]): CMakeFile {
`${name}OnLoad.cpp`
)
const autolinkingFile = toUnixPath(autolinkingFilePath)
const buildingWithDefinition = getBuildingWithGeneratedCmakeDefinition()

const code = `
${createFileMetadataString(`${name}+autolinking.cmake`, '#')}
Expand Down Expand Up @@ -61,6 +68,9 @@ target_sources(
${indent(androidFiles.join('\n'), ' ')}
)

# Define a flag to check if we are building properly
add_definitions(-D${buildingWithDefinition})

# Add all libraries required by the generated specs
find_package(fbjni REQUIRED) # <-- Used for communication between Java <-> C++
find_package(ReactAndroid REQUIRED) # <-- Used to set up React Native bindings (e.g. CallInvoker/TurboModule)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getJNINativeRegistrations } from '../../syntax/kotlin/JNINativeRegistra
import { createJNIHybridObjectRegistration } from '../../syntax/kotlin/KotlinHybridObjectRegistration.js'
import type { SourceFile, SourceImport } from '../../syntax/SourceFile.js'
import { indent } from '../../utils.js'
import { getBuildingWithGeneratedCmakeDefinition } from './createCMakeExtension.js'

export function createHybridObjectIntializer(): SourceFile[] {
const cxxNamespace = NitroConfig.getCxxNamespace('c++')
Expand Down Expand Up @@ -45,6 +46,8 @@ export function createHybridObjectIntializer(): SourceFile[] {
}
}

const buildingWithDefinition = getBuildingWithGeneratedCmakeDefinition()

const includes = [
...getJNINativeRegistrations().map((r) => includeHeader(r.import)),
...cppHybridObjectImports.map((i) => includeHeader(i)),
Expand Down Expand Up @@ -78,6 +81,10 @@ namespace ${cxxNamespace} {
const cppCode = `
${createFileMetadataString(`${autolinkingClassName}.cpp`)}

#ifndef ${buildingWithDefinition}
#error ${autolinkingClassName}.cpp is not being built with the autogenerated CMakeLists.txt project. Is a different CMakeLists.txt building this?
#endif

#include "${autolinkingClassName}.hpp"

#include <jni.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ target_sources(
../nitrogen/generated/android/c++/JHybridChildSpec.cpp
)

# Define a flag to check if we are building properly
add_definitions(-DBUILDING_NITROIMAGE_WITH_GENERATED_CMAKE_PROJECT)

# Add all libraries required by the generated specs
find_package(fbjni REQUIRED) # <-- Used for communication between Java <-> C++
find_package(ReactAndroid REQUIRED) # <-- Used to set up React Native bindings (e.g. CallInvoker/TurboModule)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
/// Copyright © 2025 Marc Rousavy @ Margelo
///

#ifndef BUILDING_NITROIMAGE_WITH_GENERATED_CMAKE_PROJECT
#error NitroImageOnLoad.cpp is not being built with the autogenerated CMakeLists.txt project. Is a different CMakeLists.txt building this?
#endif

#include "NitroImageOnLoad.hpp"

#include <jni.h>
Expand Down
Loading