Hi, I have looked into it and I think I have found something useful. I think I'm on to something, but I wasnt able to get it fully working.
TL;DR The swiftc should be able to link the static libraries. It just need to know what libraries are required. The issue is therefore not with Swift, but WebKit. Either provide Swift the full list of dependencies, or build the JSC differencly.
Before I start, a little discalimer. I looked into this as a challange. I'm really not that competent when it comes to "compiling and linking stuff," but I trying to improve myself in that regard. Therefore this post will be long, because I want to present my thought process so you're able to validate my conclusions.
I'll describe my today's story.
Setting things up
I have built the JSC as you have instruced me, and done all the following steps involving Vaport and the JavaScriptKit repo. Then I realized, I didn't need to install Vapor at all, so if I'm missing something, let me know.
I have created a new swift package via swift package init --type executable
and created new target cjsc
with the same structure as the CJavaScriptCore
target but I didn't include the modulemap. It confuses me why the modulemap is there in the first place. It looks to me like some weird combination of .systemLibrary target and .target, but I've decided to let it go.
In the main.swift
file in my executable target, I have added following statements:
import cjsc
JSGarbageCollect(nil)
so I am sure, that symbols are being resolved.
Getting on track
First I have tried to use Package.swift
cSettings, linkerSettings etc. but it didn't seem to work, so I have went back to good 'ol swift package cli.
I have tried to specify header path using -Xcc -I/home/mikolas/Developer/WebKit/WebKitBuild/Release/JavaScriptCore/Headers \
which worked. Then I have tried to provide library search path via -Xcc -L/home/mikolas/Developer/WebKit/WebKitBuild/Release/lib/
which did not work. The console output stated, that the -L
was ignored. Using -v
I have found out, that all of the commands are passed to the swiftc
. It makes sence, that the -L
was ignored, I assume, that the swiftc runs clang
and finally the linker and passing some linking options to clang won't have any effect.
I have therefore replaced -Xcc
with -Xlinker
which was a step in the right direction ... but. No libraries were linked. I have passed the --help
command to the linker via -Xlinker --help
and then -Xlinker --verbose
. The verbose output of the linker contained all of the files the linker attempted to open - including ing the /home/mikolas/Developer/WebKit/WebKitBuild/Release/lib/
- but it tried to open libraries like libgcc.a
or libc.a
. I would expect, that it would try libJavaScriptCore.a
...
Then I realized, that the linker searches all of the library search paths and tries to open all of the libraries it's looking for. And it wasn't looking for "libJavaScriptCore.a" anywhere - because nobody told to the linker to do so.
Reducing the number of errors
It was clear to me, that I need to add a -Xlinker -lJavaScriptCore
so the linker knows what it should look for. But this is where the fun starts. Turns out (to nobody's surprise), that the libJavaScriptCore.a contains a lot of symbols that need to be linked. The linker returned about 1610 errors.
I have then started to add various other libraries. I will now list the libraries I have added and the number of errors:
library ; err ; note
-lJavaScriptCore ; 1610 ; the base library
-lWTF ; 828 ; same directory as jsc
-lstdc++ ; 719 ; I have tried it, since a lot of std:: errors were present
-lbmalloc ; 522 ; same directory as jsc
-lLowLevelInterpreterLib ; 487 ; same directory as jsc
-lm ; 381 ; I have tried bunch of libraries from $ pkg-config --libs --static javascriptcoregtk-4.0
-licuuc ; 230 ; Some symbols I have googled were from this library, which should be something related to international component for unicde...
230 missing symbols down from 1610 looks quite promising, but at that point I gave up.
My last build command:
swift build \
-Xcc -I/home/mikolas/Developer/WebKit/WebKitBuild/Release/JavaScriptCore/Headers \
-Xlinker -L/home/mikolas/Developer/WebKit/WebKitBuild/Release/lib/ \
-Xlinker -lJavaScriptCore -Xlinker -lWTF -Xlinker -lstdc++ \
-Xlinker -lbmalloc -Xlinker -lLowLevelInterpreterLib -Xlinker -lm \
-Xlinker -licuuc
My conclusion is, that the problem is not in the Swift compiler. We just don't know how to link JavaScriptCore statically properly. I think this might be a better question for some "Web Kit forums" or someone with general knowledge of "where to look for the list of dependencies".
(At this point I've briefly meditated on my renewed appreciation for pkg-config and all the problems it solves for me.)
(Thinking about it, .pc files for pkg-config can be generated ... it should be somehow possible to dump the full list of dependencies using make, right?...)