How to use a C static library(libJavaScriptCore.a) with Swift on Linux?

Yes, my local university. :smile: Anyway, questions like this don't get much attention imho, because you (and me) should really be able to figure it out on our own. Asking people to help you build something is like asking people to solve your homework, if you know what I mean :slight_smile: I'm not saying such questions should not be asked, but don't get frustrated if you don't find anyone who is willing to answer it. :slight_smile: I don't want to dive back to my lifestory again, so lets focus on:

Now, as I understand the issue at hand, we need to figure out what libraries we need to link. I have previously mentioned, that cmake should be able to tell us what libraries are being used in the project.

I have found this post on google cmake - How do I list the defined make targets from the command line? - Stack Overflow which describes how to make cmake print the structure of the project, following worked for me:

$ Tools/Scripts/build-webkit --jsc-only --cmakeargs="-DENABLE_STATIC_JSC=ON -DUSE_THIN_ARCHIVES=OFF --graphviz=test.graph "
$ cd WebKitBuild/Release/
$ dot -Tjpg test.graph > out.jpg

The result is this image which contains all of the tagets, libraries and dependencies:

The option is documented here CMakeGraphVizOptions — CMake 3.28.0-rc5 Documentation .

Now, I can immediately recognize some libraries like ICU. Ultimately, this solves nothing. But I think it is interesteng, so I've felt the need to post it here (for my own future reference :slight_smile:).

So at this point, we're where we beign: we know about some of the libraries we need to link, but some are still missing.

Next I was about to try another trick: CMAKE_EXPORT_COMPILE_COMMANDS. CMAKE_EXPORT_COMPILE_COMMANDS — CMake 3.28.0-rc5 Documentation Setting this variable to ON will result in cmake creating a file called compile_commands.json which contains all of the compile commands ... but that's not what we're looking for, right? We're looking for link commands. Turns out, such a feature is not implemented ... yet. Add Link Database Generation (#22067) · Issues · CMake / CMake · GitLab

But then I've realized, that cmake does not build anything on it's own. It is used to generate build commands for other build systems - like make, Xcode or ... Ninja in this case.

So I have opened /home/mikolas/Developer/WebKit/WebKitBuild/Release and found file called build.ninja. This file has 237K lines (and some of the lines have 9K characters) so you'll need to use some text editor, that's not going to freeze on you (MATE's Pluma in my case). I think it's safe to say, that this file contains everything there is :smiley:
I have spent next 1/2 trying to understand the contents. If you're interested in how "things get linked", simply search for # Link the .

The section # Link the executable bin/jsc [*] links the lib/libJavaScriptCore.a, so it should contain all the link statements we need. And I assume it does in LINK_LIBRARIES where we can observe which libraries are linked statically, and which dynamically.

In conclusion, I think that we're near the end. It should be sufficient to rewrite the link commands for bin/jsc for our Swift compiler.

Anyway, I'm not going to try this out today, other things require my attention (chores around the house and fun stuff like that.) I realize my posts are really - really long, but I like to describe my thought process in detail. Not only so people are able to validate my results, but for my own future reference.

[*] bin/jsc is command line interpret of JavaScript, so I fell confident it should ... "contain" all of the things required.

Edit: I have also look at this post: Linux static-executable linking errors

1 Like