I have a very small C wrapper for the linux system...
# kotlin-native
s
I have a very small C wrapper for the linux system (Ubuntu is the build system) call getrandom, The C code looks like this:
Copy code
#include "getrandom_wrapper.h"

ssize_t call_getrandom(void *buf, size_t buflen, unsigned int flags) {
    return getrandom(buf, buflen, flags);
}
and the header is this:
Copy code
#include <sys/random.h>

// Wrapper function to call getrandom()
ssize_t wrapper_getrandom(void *buf, size_t buflen, unsigned int flags);
This compiles and links fine using gcc and produces a small static library. The .def file looks like this:
Copy code
headers = getrandom_wrapper.h
package = com.oldguy.crypto
linkerOpts = -L.,/usr/include/x86_64-linux-gnu/ -lgetrandom_wrapper
The gradle setup for linuxX64 cinterop is this:
Copy code
compilations.getByName("main") {
            val path = "${project.rootDir}/KmpCrypto/src/linuxMain/cinterop"
            cinterops {
                val myLibraryCinterop by creating {
                    defFile(project.file("$path/getrandom_wrapper.def"))
                    includeDirs(path, "/usr/include/x86_64-linux-gnu") // Ubuntu location of sys/random.h
                }
            }
        }
At cinterop (gradle sync) time, this error pops:
Copy code
/usr/include/x86_64-linux-gnu/sys/cdefs.h:153:34: error: function-like macro '__glibc_clang_prereq' is not defined
Has anyone else seen this and knows what causes it? This macro should be defined by the sys/features.h that gets pulled in by sys/random.h. The code snippet from features.h that isn't working looks like this:
Copy code
/* Similarly for clang.  Features added to GCC after version 4.2 may
   or may not also be available in clang, and clang's definitions of
   __GNUC(_MINOR)__ are fixed at 4 and 2 respectively.  Not all such
   features can be queried via __has_extension/__has_feature.  */
#if defined __clang_major__ && defined __clang_minor__
# define __glibc_clang_prereq(maj, min) \
  ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
#else
# define __glibc_clang_prereq(maj, min) 0
#endif
Thanks in advance for any ideas on how to diagnose/fix this.
a
when I've encountered similar errors it's because Kotlin Native uses a very specific gcc/llvm versions that aren't widely available. I had to compile & link the library I wanted to use in KN using the
run_konan
tool. https://github.com/JetBrains/kotlin/blob/v2.1.21/kotlin-native/HACKING.md#running-clang-the-same-way-kotlinnative-compiler-does
I made some Gradle tasks that will help - either use them, or use them as inspiration to compile manually https://gist.github.com/aSemy/076591d565867839b6009f36c3b8b3ae
s
Thanks!!
So I think I'm still hosed. After digging through your good info, I tried this command using run_konan:
Copy code
run_konan clang clang linux_x64 getrandom_wrapper.c -I /usr/include/x86_64-linux-gnu
I didn't see anywhere in the konan stuff where they have their own headers, so I pointed it at the standard ones. It still gets the same sort of error in features.h as the regular gcc:
Copy code
In file included from getrandom_wrapper.c:1:
In file included from ./getrandom_wrapper.h:1:
In file included from /usr/include/x86_64-linux-gnu/sys/random.h:22:
In file included from /home/steve-olson/.konan/dependencies/x86_64-unknown-linux-gnu-gcc-8.3.0-glibc-2.19-kernel-4.9-2/x86_64-unknown-linux-gnu/sysroot/usr/include/features.h:378:
/usr/include/x86_64-linux-gnu/sys/cdefs.h:153:34: error: function-like macro '__glibc_clang_prereq' is not defined
#if __USE_FORTIFY_LEVEL == 3 && (__glibc_clang_prereq (9, 0)                  \
                                 ^
/usr/include/x86_64-linux-gnu/sys/cdefs.h:652:28: error: function-like macro '__glibc_clang_prereq' is not defined
#if __GNUC_PREREQ (4,8) || __glibc_clang_prereq (3,5)
                           ^
2 errors generated.
I also got your custom build tasks working and tried them to see if the options they used made a difference. Same result. I think I'm gonna file an issue and see what Jetbrains has to say. Thanks again for your help!
cinterop is doing more work with compiled c libs than I thought. I've been trying a number of different workarounds, and none of them get past the error at sync time (call stack below). one of the approaches I tried was to remove all system headers from the .h file referenced in the .def file. So all the .h file has is the wrapper function def. I also changed out the ssize_t and size_t references to avoid needing any #includes in my header. ssize_t is a long int, and size_t is a long unsigned int in linuxX64, so with a new header that looks like this (no sys includes at all in the header):
Copy code
// Wrapper function to call getrandom()
long int wrapper_getrandom(void *buf, long unsigned int buflen, unsigned int flags);
And matching C code that looks like this:
Copy code
#include "getrandom_wrapper.h"
#include <sys/random.h>

long int wrapper_getrandom(void *buf, long unsigned int buflen, unsigned int flags) {
    return getrandom(buf, buflen, flags);
}
I recompiled the new library (with either gcc or clang, both work, konan_tools still gets the similar compile-time error) and attempted a sync. Cinterop STILL gets the error below, even though the only reference to system headers is in the compiled C code in the library. How is it figuring out what the includes are in the compiled C? Anyway I've punted at this point. I filed issue KT-78637, and am waiting to see if Jetbrains has a comment or something 🙂. The macro isn't defined in features.h (included by random.h) because the compiler major/minor/ versions in cinterop aren't up-to-date enough to get it defined.
Copy code
Exception in thread "main" java.lang.Error: /usr/include/x86_64-linux-gnu/sys/cdefs.h:153:34: error: function-like macro '__glibc_clang_prereq' is not defined
	at org.jetbrains.kotlin.native.interop.indexer.UtilsKt.ensureNoCompileErrors(Utils.kt:292)
	at org.jetbrains.kotlin.native.interop.indexer.UtilsKt.precompileHeaders(Utils.kt:435)
	at org.jetbrains.kotlin.native.interop.gen.StubIrContext.<init>(StubIrDriver.kt:46)
	at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.processCLib(main.kt:347)
	at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.processCLibSafe(main.kt:243)
	at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.access$processCLibSafe(main.kt:1)
	at org.jetbrains.kotlin.native.interop.gen.jvm.Interop.interop(main.kt:101)
	at org.jetbrains.kotlin.cli.utilities.InteropCompilerKt.invokeInterop(InteropCompiler.kt:48)