Kheiron-Medical / ktbind

C++ and Kotlin interoperability
MIT License
44 stars 4 forks source link

Unable to build using g++12 / apple clang 14 #5

Open H4kt opened 1 year ago

H4kt commented 1 year ago

Hey there, I'm really curious about this project because it really allows me to get rid of all of the pain when working with JNI

Environment:

OS: macOS 13.0 Processor/Arch: Apple M1 Pro

Use case

I was attempting to build a modified sample from your readme file just to get started

File structure

image

src/main.cpp

#include "ktbind/ktbind.hpp"

struct Sample {
    Sample();
    Sample(const char*);
    Sample(std::string);
};

DECLARE_NATIVE_CLASS(Sample, "io.h4kt.sample.Sample")

JAVA_EXTENSION_MODULE() {
    using namespace java;
    native_class<Sample>()
        .constructor<Sample()>("create")
        .constructor<Sample(std::string)>("create")
    ;
    print_registered_bindings();
}

I tried building it using Gradle with cpp-library plugin and even tried a manual build using g++ in a command line. Both ways were unsuccessful

Apple clang using Gradle

buid.gradle.kts

plugins {
    id("cpp-library")
}

val javaHome: String by project

repositories {
    mavenCentral()
}

library {

    source.from("src")
    privateHeaders.from("src")

    publicHeaders.from(
        "include",
        "$javaHome/include",
        "$javaHome/include/darwin"
    )

}

tasks.withType<CppCompile> {
    compilerArgs.add("-std=c++17")
}

output

clang: error: unable to execute command: Segmentation fault: 11
clang: error: clang frontend command failed due to signal (use -v to see invocation)
Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: arm64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
clang: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /var/folders/fn/g44nnh2x78d08v068fs6l8n80000gn/T/main-345300.cpp
clang: note: diagnostic msg: /var/folders/fn/g44nnh2x78d08v068fs6l8n80000gn/T/main-345300.sh
clang: note: diagnostic msg: Crash backtrace is located in
clang: note: diagnostic msg: /Users/h4kt/Library/Logs/DiagnosticReports/clang_<YYYY-MM-DD-HHMMSS>_<hostname>.crash
clang: note: diagnostic msg: (choose the .crash file that corresponds to your crash)
clang: note: diagnostic msg: 

********************

Manual build using g++

build.sh

/opt/homebrew/Cellar/gcc/12.2.0/bin/g++-12 -std=c++17 -I include -I /Users/h4kt/Library/Java/JavaVirtualMachines/azul-11.0.16.1/Contents/Home/include -I /Users/h4kt/Library/Java/JavaVirtualMachines/azul-11.0.16.1/Contents/Home/include/darwin src/main.cpp

output

0  0x100b2c1a0  __assert_rtn + 140
1  0x1009b3a8c  mach_o::relocatable::Parser<arm64>::parse(mach_o::relocatable::ParserOptions const&) + 4536
2  0x100985d38  mach_o::relocatable::Parser<arm64>::parse(unsigned char const*, unsigned long long, char const*, long, ld::File::Ordinal, mach_o::relocatable::ParserOptions const&) + 148
3  0x1009ee4ac  ld::tool::InputFiles::makeFile(Options::FileInfo const&, bool) + 1468
4  0x1009f1360  ___ZN2ld4tool10InputFilesC2ER7Options_block_invoke + 56
5  0x19092f544  _dispatch_client_callout2 + 20
6  0x190944654  _dispatch_apply_invoke_and_wait + 224
7  0x190943938  _dispatch_apply_with_attr_f + 1152
8  0x190943b48  dispatch_apply + 108
9  0x1009f11f4  ld::tool::InputFiles::InputFiles(Options&) + 616
10  0x1009736c0  main + 552
A linker snapshot was created at:
        /tmp/a.out-2022-12-11-041054.ld-snapshot
ld: Assertion failed: (_file->_atomsArrayCount == computedAtomCount && "more atoms allocated than expected"), function parse, file macho_relocatable_file.cpp, line 2061.
collect2: error: ld returned 1 exit status
hunyadi commented 1 year ago

Initially, I had experienced the same error message when running CMake on macOS Ventura 13.1 (Processor: 2,3 GHz 8-Core Intel Core i9):

$ build % java --version
openjdk 17.0.3 2022-04-19 LTS
OpenJDK Runtime Environment Corretto-17.0.3.6.1 (build 17.0.3+6-LTS)
OpenJDK 64-Bit Server VM Corretto-17.0.3.6.1 (build 17.0.3+6-LTS, mixed mode, sharing)
$ build % cmake ..
-- The CXX compiler identification is AppleClang 14.0.0.14000029
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found JNI: /Users/levente.hunyadi/Library/Java/JavaVirtualMachines/corretto-17.0.3/Contents/Home/include  found components: AWT JVM 
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/levente.hunyadi/Documents/ktbind/cpp/build
$ build % make
[ 50%] Building CXX object CMakeFiles/ktbind_java.dir/test/java.cpp.o
clang: fatal error: unable to execute command: Segmentation fault: 11
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: x86_64-apple-darwin22.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
make[2]: *** [CMakeFiles/ktbind_java.dir/test/java.cpp.o] Error 254
make[1]: *** [CMakeFiles/ktbind_java.dir/all] Error 2
make: *** [all] Error 2

I had also tried setting the compiler to Clang version 15.0.6 (installed with Homebrew), to no avail. It turns out the culprit is in struct ArgType<T>:

// returns the corresponding array type for the base type
constexpr static std::string_view array_type_prefix = "[";
constexpr static std::string_view array_type_sig = join_v<array_type_prefix, ArgType<T>::type_sig>;

After these (unused) constant expressions have been removed, the project compiles without error, and unit tests in the Kotlin project succeed.

The changes are pushed to my fork of KtBind. (I am no longer affiliated with Kheiron Medical and don't control this repository.)