Last active
June 26, 2019 19:40
-
-
Save aniongithub/f6a0a9ac5982fc8853348abc018569f6 to your computer and use it in GitHub Desktop.
Building native Node.js modules with node-cmake, nbind and conan
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.1) | |
project(nbind VERSION 1.0.0) | |
add_definitions(-DBUILDING_NODE_EXTENSION) | |
add_definitions(-DUSING_V8_SHARED) | |
add_definitions(-DUSING_UV_SHARED) | |
add_definitions(-DV8_DEPRECATION_WARNINGS) | |
include(node_modules/node-cmake/NodeJS.cmake) | |
nodejs_init() | |
include_directories(node_modules/nbind/include) | |
file(GLOB NBIND_SOURCE_FILES node_modules/nbind/src/*.cc node_modules/nbind/src/v8/*.cc) | |
# Download automatically, you can also just copy the conan.cmake file | |
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake") | |
message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan") | |
file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/master/conan.cmake" | |
"${CMAKE_BINARY_DIR}/conan.cmake") | |
endif() | |
include(${CMAKE_BINARY_DIR}/conan.cmake) | |
conan_cmake_run(REQUIRES | |
Hello/0.1@memsharded/testing | |
BASIC_SETUP NO_OUTPUT_DIRS | |
BUILD missing) | |
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) | |
SET(SRCS | |
${NBIND_SOURCE_FILES} | |
hello.cc | |
) | |
add_nodejs_module(${PROJECT_NAME} ${SRCS}) | |
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#include "hello.h" | |
struct Greeter { | |
static void sayHello( | |
std::string name | |
) { | |
std::cout << "Hello, " << name << "!" << std::endl << std::flush; | |
} | |
static void sayGoodbye(std::string name) | |
{ | |
std::cout << "Goodbye, " << name << "!" << std::endl << std::flush; | |
} | |
static void sayHelloFromConanLib() | |
{ | |
hello(); | |
} | |
}; | |
#include "nbind/nbind.h" | |
NBIND_CLASS(Greeter) { | |
method(sayHello); | |
method(sayGoodbye); | |
method(sayHelloFromConanLib); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nbind = require('nbind') | |
var lib = nbind.init().lib; | |
var username = process.env.USER | |
lib.Greeter.sayHello(username); | |
lib.Greeter.sayGoodbye(username); | |
lib.Greeter.sayHelloFromConanLib(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"scripts": { | |
"install": "ncmake rebuild", | |
"test": "node index.js" | |
}, | |
"devDependencies": { | |
"node-cmake": "2.x" | |
}, | |
"dependencies": { | |
"nbind": "^0.3.13" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment