Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c5c75c8

Browse files
Hacks to build with emscripten for running in a web browser
1 parent c7e5eca commit c5c75c8

32 files changed

+3556
-7
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@ msvc/Release/
3131
.*.swp
3232
tags
3333
mkmf.log
34+
35+
.vscode
3436
*.profdata
3537
*.profraw

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,10 @@ Here are the bindings to libgit2 that are currently available:
368368
If you start another language binding to libgit2, please let us know so
369369
we can add it to the list.
370370

371+
Experimental Emscripten support for running in a web-browser
372+
============================================================
373+
See [EMSCRIPTEN_HACKS](emscripten_hacks/README.md)
374+
371375
How Can I Contribute?
372376
==================================
373377

azure-pipelines.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ jobs:
123123
CMAKE_OPTIONS: -G"MinGW Makefiles" -DDEPRECATE_HARD=ON
124124
PATH: $(Agent.TempDirectory)\mingw32\bin;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\CMake\bin
125125

126+
- job: emscripten
127+
displayName: 'Emscripten (javascript)'
128+
pool:
129+
vmImage: 'Ubuntu 16.04'
130+
steps:
131+
- script: docker run -v $(Build.SourcesDirectory):/libgit2 psalomo/libgit2.js
126132
- job: documentation
127133
displayName: 'Generate Documentation'
128134
pool:

emscripten_hacks/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
src
2+
deps
3+
CMake*
4+
libgit2.pc
5+
Makefile
6+
node_modules
7+
libgit2.js
8+
libgit2.wasm

emscripten_hacks/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
A hack to build libgit2 with emscripten and run in the browser
2+
==============================================================
3+
4+
The build-setup here is tested with Ubuntu 16.04 and latest emscripten sdk (1.37.36)
5+
6+
First of all you need to source the emscripten sdk:
7+
8+
source /home/ubuntu/emsdk_portable/emsdk_env.sh
9+
10+
Then go into the jsbuild folder and run the build shell script:
11+
12+
cd emscripten_hacks
13+
sh build.sh
14+
15+
You should end up with a libgit2.js file in your jsbuild folder.
16+
17+
Because of CORS restrictions in the browser you cannot read from github directly from another domain. You need to add a proxy on your web server. You can run the githttpproxy.js script in this folder to
18+
get a local webserver with proxy to github.com:
19+
20+
node githttpproxy.js
21+
22+
Navigate your browser to `http://localhost:5000`
23+
24+
When testing with the index.html file included here you should open the web console which will prompt "ready" when loaded. Remember to switch to the libgit2 webworker for typing commands in the console.
25+
26+
Type the commands:
27+
28+
jsgitinit();
29+
jsgitclone("https://github.com/pathto/mygitrepo.git","mygitrepo");
30+
31+
You'll see the git clone process starts. This is not a small repository so it takes some time.
32+
33+
When the clone is done you can list the folder contents by typing:
34+
35+
FS.readdir("mygitrepo")
36+
37+
A simple demonstration video can be seen here: https://youtu.be/rcBluzpUWE4

emscripten_hacks/build.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
emcmake cmake -DCMAKE_C_FLAGS="-Oz" -DAPPLY_EMSCRIPTEN_HACKS=ON -DSONAME=OFF -DUSE_HTTPS=OFF -DBUILD_SHARED_LIBS=OFF -DTHREADSAFE=OFF -DBUILD_CLAR=OFF -DUSE_SSH=OFF ..
2+
emcmake cmake -build .
3+
emmake make
4+
echo "building libgit2.js"
5+
emcc -s ALLOW_MEMORY_GROWTH=1 --post-js jsinit.js -s "EXTRA_EXPORTED_RUNTIME_METHODS=['FS']" -Oz jslib.c libgit2.a -Isrc -I../src -I../include -o libgit2.js

emscripten_hacks/docker/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM ubuntu:18.04
2+
COPY . .
3+
RUN bash setup.sh
4+
ENTRYPOINT [ "bash", "run.sh" ]

emscripten_hacks/docker/run.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
source ./emsdk/emsdk_env.sh
2+
cd /libgit2/emscripten_hacks
3+
rm libgit2.*
4+
sh build.sh
5+
node nodetest.js
6+
node nodefstest.js

emscripten_hacks/docker/setup.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apt update
2+
apt install -y python
3+
apt install -y git
4+
apt install -y cmake
5+
apt install -y nodejs
6+
git clone https://github.com/juj/emsdk.git
7+
cd emsdk
8+
./emsdk install latest
9+
./emsdk activate latest

emscripten_hacks/git_worker.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// "Stupid" web worker that accepts scripts
2+
const startTime = new Date().getTime();
3+
importScripts("libgit2.js");
4+
Module['onRuntimeInitialized'] = () => {
5+
6+
const dir="workdir";
7+
FS.mkdir(dir,"0777");
8+
FS.mount(IDBFS, {}, '/'+dir);
9+
FS.chdir("/"+dir);
10+
11+
console.log('libgit2 ready in webworker.',(new Date().getTime() - startTime),'ms startup time');
12+
};

0 commit comments

Comments
 (0)