Skip to content

Commit

Permalink
Add Windows build support (#2)
Browse files Browse the repository at this point in the history
This commit adds Windows build support by utilizing vcpkg to build
dependencies, and CMake for the build system.

These changes rely on vcpkg to be built and located in the VCPKG_ROOT
environment variable, along with having Visual Studio 2019 installed.
  • Loading branch information
VodBox authored Jun 24, 2020
1 parent 937c1a2 commit 5b3fc5c
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 28 deletions.
29 changes: 26 additions & 3 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,33 @@
"src/node-srt.cc"
],
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")",
"deps/build/include"
"<!@(node -p \"require('node-addon-api').include\")"
],
"conditions": [
[ 'OS=="win"', {
"defines": [
"WIN32_LEAN_AND_MEAN"
],
"include_dirs+": [
"deps/srt/srtcore",
"deps/build",
"deps/srt/common"
],
"libraries": [ "<(module_root_dir)/deps/build/Release/srt.lib" ],
"copies": [{
"destination": "<(module_root_dir)/build/Release",
"files": [
"<(module_root_dir)/deps/build/Release/srt.dll"
]
}]
}],
[ 'OS=="linux"', {
"libraries": [ "<(module_root_dir)/deps/build/lib/libsrt.a" ],
"include_dirs+": [
"deps/build/include"
]
}]
],
"libraries": [ "<(module_root_dir)/deps/build/lib/libsrt.a" ],
"dependencies": [
"<!(node -p \"require('node-addon-api').gyp\")"
],
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"gypfile": true,
"author": "Eyevinn Technology AB <work@eyevinn.se>",
"contributors": [
"Jonas Rydholm Birmé <jonas.birme@eyevinn.se> (Eyevinn Technology AB)"
"Jonas Rydholm Birmé <jonas.birme@eyevinn.se> (Eyevinn Technology AB)",
"Dillon Pentz <dillon@vodbox.io>"
],
"license": "MIT",
"devDependencies": {
Expand Down
93 changes: 72 additions & 21 deletions scripts/build-srt-sdk.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const path = require('path');
const fs = require('fs');
const process = require('process');
const clone = require('git-clone');
const { spawnSync } = require('child_process');

const SRT_REPO = "git@github.com:Haivision/srt.git";
const SRT_REPO = "https://github.com/Haivision/srt.git";
const SRT_VERSION = "v1.4.1";

const depsPath = path.join(__dirname, '../', 'deps');
Expand All @@ -16,28 +17,78 @@ if (!fs.existsSync(depsPath)) {
if (!fs.existsSync(buildDir)) {
console.log(`Cloning ${SRT_REPO}:${SRT_VERSION}`);
clone(SRT_REPO, srtSourcePath, { checkout: SRT_VERSION }, () => {
console.log("Running ./configure");
const configure = spawnSync('./configure', [ '--prefix', buildDir ], { cwd: srtSourcePath, shell: true } );
console.log(configure.stdout.toString());
if (configure.status) {
console.log(configure.stderr.toString());
process.exit(configure.status);
}
if (process.platform === "win32") {
process.env.SRT_ROOT = srtSourcePath;
fs.mkdirSync(buildDir);

console.log("Running make");
const make = spawnSync('make', [], { cwd: srtSourcePath, shell: true });
console.log(make.stdout.toString());
if (make.status) {
console.log(make.stderr.toString());
process.exit(make.status);
}
console.log("Building OpenSSL");
const openssl = spawnSync('vcpkg', [ 'install', 'openssl', '--triplet', `${process.arch}-windows` ], { cwd: process.env.VCPKG_ROOT, shell: true } );
if (openssl.stdout)
console.log(openssl.stdout.toString());
if (openssl.status) {
console.log(openssl.stderr.toString());
process.exit(openssl.status);
}

console.log("Building pthreads");
const pthreads = spawnSync('vcpkg', [ 'install', 'pthreads', '--triplet', `${process.arch}-windows` ], { cwd: process.env.VCPKG_ROOT, shell: true } );
if (pthreads.stdout)
console.log(pthreads.stdout.toString());
if (pthreads.status) {
console.log(pthreads.stderr.toString());
process.exit(pthreads.status);
}

console.log("Integrate vcpkg build system");
const integrate = spawnSync('vcpkg', [ 'integrate', 'install' ], { cwd: process.env.VCPKG_ROOT, shell: true } );
if (integrate.stdout)
console.log(integrate.stdout.toString());
if (integrate.status) {
console.log(integrate.stderr.toString());
process.exit(integrate.status);
}

console.log("Running cmake generator");
const generator = spawnSync('cmake', [ srtSourcePath, '-DCMAKE_BUILD_TYPE=Release', '-G"Visual Studio 16 2019"', '-A', process.arch, '-DCMAKE_TOOLCHAIN_FILE="%VCPKG_ROOT%\\scripts\\buildsystems\\vcpkg.cmake' ], { cwd: buildDir, shell: true } );
if (generator.stdout)
console.log(generator.stdout.toString());
if (generator.status) {
console.log(generator.stderr.toString());
process.exit(generator.status);
}

console.log("Running cmake build");
const build = spawnSync('cmake', [ '--build', buildDir, '--config', 'Release' ], { cwd: buildDir, shell: true } );
if (build.stdout)
console.log(build.stdout.toString());
if (build.status) {
console.log(build.stderr.toString());
process.exit(build.status);
}
} else {
console.log("Running ./configure");
const configure = spawnSync('./configure', [ '--prefix', buildDir ], { cwd: srtSourcePath, shell: true } );
console.log(configure.stdout.toString());
if (configure.status) {
console.log(configure.stderr.toString());
process.exit(configure.status);
}

console.log("Running make");
const make = spawnSync('make', [], { cwd: srtSourcePath, shell: true });
console.log(make.stdout.toString());
if (make.status) {
console.log(make.stderr.toString());
process.exit(make.status);
}

console.log("Running make install");
const install = spawnSync('make', [ 'install' ], { cwd: srtSourcePath, shell: true });
console.log(install.stdout.toString());
if (install.status) {
console.log(install.stderr.toString());
process.exit(install.status);
console.log("Running make install");
const install = spawnSync('make', [ 'install' ], { cwd: srtSourcePath, shell: true });
console.log(install.stdout.toString());
if (install.status) {
console.log(install.stderr.toString());
process.exit(install.status);
}
}
});
}
15 changes: 12 additions & 3 deletions src/node-srt.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#if defined(_WIN32)
#include <srt.h>
#pragma comment(lib, "ws2_32.lib")
#elif !defined(_WIN32)
#include <srt/srt.h>
#include <sys/syslog.h>
#endif
#include "node-srt.h"

Napi::FunctionReference NodeSRT::constructor;
Expand Down Expand Up @@ -170,15 +175,19 @@ Napi::Value NodeSRT::Read(const Napi::CallbackInfo& info) {
Napi::Number chunkSize = info[1].As<Napi::Number>();

size_t bufferSize = uint32_t(chunkSize);
uint8_t buffer[bufferSize];
memset(&buffer, 0, bufferSize);
uint8_t *buffer = (uint8_t *)malloc(bufferSize);
memset(buffer, 0, bufferSize);

int nb = srt_recvmsg(socketValue, (char *)buffer, (int)bufferSize);
if (nb == SRT_ERROR) {
Napi::Error::New(env, srt_getlasterror_str()).ThrowAsJavaScriptException();
return Napi::Number::New(env, SRT_ERROR);
}
return Napi::Buffer<uint8_t>::Copy(env, buffer, nb);

Napi::Value nbuff = Napi::Buffer<uint8_t>::Copy(env, buffer, nb);
free(buffer);

return nbuff;
}

Napi::Value NodeSRT::Write(const Napi::CallbackInfo& info) {
Expand Down

0 comments on commit 5b3fc5c

Please sign in to comment.