smanders / externpro

build external projects with cmake
MIT License
13 stars 12 forks source link

add node-addon-api #284

Closed smanders closed 4 years ago

smanders commented 4 years ago

From: AJ Graham Sent: Thursday, October 8, 2020 11:14 AM Subject: N-API

N-API is a library provided in nodejs that is designed to insulate projects from changes in V8. V8 changes have consistently caused us problems when trying update nodejs. I would like to move to using N-API for all node C++ addons. I have started using it for the new addon that I am writing for the product database. So far, it feels pretty clunky to use as it is a C compatible API.

Thankfully, the same group that developed N-API also developed a C++ wrapper to make it much easier to work with from C++. Though it is published under the nodejs foundation github organization: https://github.com/nodejs/node-addon-api, the C++ wrapper is not native to nodejs.

smanders commented 4 years ago

current release is 3.0.2 https://github.com/nodejs/node-addon-api/tree/3.0.2 https://github.com/nodejs/node-addon-api/releases/tag/3.0.2

smanders commented 4 years ago

completed with commits to dev branch

smanders commented 4 years ago

tested by building VIVID via buildpro with the following changes

$ git status
On branch initialFunctionality
Your branch is up-to-date with 'ajgraham/initialFunctionality'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    cmake/toplevel.cmake
        deleted:    cmake/version.cmake
        deleted:    web/addons/NodeNitf/napi-inl.h
        deleted:    web/addons/NodeNitf/napi.h

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   CMakeLists.txt
        modified:   cmake/CMakeLists.txt
        modified:   web/addons/NodeNitf/CMakeLists.txt
        modified:   web/addons/NodeNitf/GetTacIdWorker.cpp
        modified:   web/addons/NodeNitf/GetTacIdWorker.hpp
        modified:   web/addons/NodeNitf/NodeNitf.cpp
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 15fd8ef..64fbe14 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,8 +1,20 @@
 cmake_minimum_required(VERSION 3.17)
-include(cmake/preproject.cmake)
-project(VIVID)
-include(cmake/version.cmake)
-include(cmake/toplevel.cmake)
+set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
+include(preproject)
+project(VIVID VERSION 0.0.0.1)
+set(PACKAGE_VENDOR "Space Dynamics Lab")
+set(PACKAGE_NAME ${PROJECT_NAME})
+set(PACKAGE_START_YEAR 2020)
+#######################################
+set(ENV{externpro_BUILD_DIR} /srcdir/externpro/_bld)
+set(externpro_REV 20.10.1-13-gbb927a7)
+set(ENV{internpro_BUILD_DIR} /srcdir/internpro/_bld)
+set(internpro_REV smanders-dirtyrepo-20.10.1-10-g49f07c4)
+find_package(externpro REQUIRED)
+find_package(internpro REQUIRED)
+xpSetFlags()
+xpFindPkg(PKGS node-addon-api palam)
+#######################################
 add_subdirectory(cmake)
 add_subdirectory(database)
 add_subdirectory(web)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
index cea6c55..d45e361 100644
--- a/cmake/CMakeLists.txt
+++ b/cmake/CMakeLists.txt
@@ -1,15 +1,14 @@
-set(utility_project make)
+set(utility_project cmake)
 ########################################
-file(GLOB cmake_srcs *.cmake ${CMAKE_SOURCE_DIR}/*.cmake)
-source_group(.cmake FILES ${cmake_srcs})
-list(APPEND ${utility_project}_srcs ${cmake_srcs})
+file(GLOB dotcmake_srcs *.cmake ${CMAKE_SOURCE_DIR}/*.cmake)
+source_group(.cmake FILES ${dotcmake_srcs})
+list(APPEND ${utility_project}_srcs ${dotcmake_srcs})
 ###########
 file(GLOB externpro_cmake ${externpro_DIR}/share/cmake/*.cmake)
 source_group(externpro.cmake FILES ${externpro_cmake})
-# TODO: do I need the .in files?
-# file(GLOB externpro_in ${externpro_DIR}/share/cmake/*.in)
-# source_group(externpro.in FILES ${externpro_in})
-list(APPEND ${utility_project}_srcs ${externpro_cmake}) #${externpro_in})
+file(GLOB externpro_in ${externpro_DIR}/share/cmake/*.in)
+source_group(externpro.in FILES ${externpro_in})
+list(APPEND ${utility_project}_srcs ${externpro_cmake} ${externpro_in})
 ###########
 file(GLOB internpro_cmake ${internpro_DIR}/share/cmake/*.cmake)
 source_group(internpro.cmake FILES ${internpro_cmake})
diff --git a/web/addons/NodeNitf/CMakeLists.txt b/web/addons/NodeNitf/CMakeLists.txt
index 7808868..8128ca8 100644
--- a/web/addons/NodeNitf/CMakeLists.txt
+++ b/web/addons/NodeNitf/CMakeLists.txt
@@ -2,10 +2,6 @@ set(dll_name NodeNitf)
 set(dll_srcs
   GetTacIdWorker.cpp
   GetTacIdWorker.hpp
-  # TODO: remove napi files when the are in externpro
-  # https://github.com/smanders/externpro/issues/284
-  napi-inl.h
-  napi.h
   NodeNitf.cpp
   TacId.hpp
   TacId.cpp
@@ -15,7 +11,7 @@ list(APPEND ${dll_name}_libsrcs ${dll_srcs})
 ########################################
 add_library(${dll_name} SHARED ${${dll_name}_libsrcs})
 target_include_directories(${dll_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
-target_link_libraries(${dll_name} PRIVATE palam::nitfinc xpro::node Boost::filesystem)
+target_link_libraries(${dll_name} PRIVATE palam::nitfinc xpro::node-addon-api)
 set_property(TARGET ${dll_name} PROPERTY FOLDER ${folder})
 set_target_properties(${dll_name} PROPERTIES PREFIX "" SUFFIX ".node")
 ########################################
diff --git a/web/addons/NodeNitf/GetTacIdWorker.cpp b/web/addons/NodeNitf/GetTacIdWorker.cpp
index 138d72d..7d6e56e 100644
--- a/web/addons/NodeNitf/GetTacIdWorker.cpp
+++ b/web/addons/NodeNitf/GetTacIdWorker.cpp
@@ -9,8 +9,6 @@

 #include <fstream>

-#include <boost/filesystem/operations.hpp>
-
 #include <palam/nitf/Nitf21.hpp>

 #include "TacId.hpp"
@@ -19,7 +17,6 @@ namespace
 {
   node_nitf::TacId getTacId(const std::string& fileName)
   {
-    // auto size = boost::filesystem::file_size(fileName);
     std::ifstream ifs(fileName, std::ifstream::binary);
     if (!ifs)
     {
diff --git a/web/addons/NodeNitf/GetTacIdWorker.hpp b/web/addons/NodeNitf/GetTacIdWorker.hpp
index 37a4a69..c79e724 100644
--- a/web/addons/NodeNitf/GetTacIdWorker.hpp
+++ b/web/addons/NodeNitf/GetTacIdWorker.hpp
@@ -9,9 +9,7 @@
 #define NODE_NITF_GET_TAC_ID_WORKER_HPP

 #include <string>
-#define NODE_ADDON_API_DISABLE_DEPRECATED
-#define NAPI_CPP_EXCEPTIONS
-#include <napi.h>
+#include <node-addon-api/napi.h>

 #include "TacId.hpp"

diff --git a/web/addons/NodeNitf/NodeNitf.cpp b/web/addons/NodeNitf/NodeNitf.cpp
index 8292eb9..91d7ed1 100644
--- a/web/addons/NodeNitf/NodeNitf.cpp
+++ b/web/addons/NodeNitf/NodeNitf.cpp
@@ -5,12 +5,10 @@
  * (Civilian Agencies) or DFARS 252.227-7014 (Defense Agencies).
  */

-#define NODE_ADDON_API_DISABLE_DEPRECATED
-#define NAPI_CPP_EXCEPTIONS
 #include <memory>
 #include <string>

-#include <napi.h>
+#include <node-addon-api/napi.h>

 #include "GetTacIdWorker.hpp"
smanders commented 3 years ago

tested by building development branch of VIVID via buildpro with the following changes

$ git status
On branch development
Your branch is up to date with 'origin/development'.

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   CMakeLists.txt
        modified:   Dockerfile
        modified:   docker-compose.yml
    modified:   web/addons/NodeNitf/CMakeLists.txt
    modified:   web/addons/NodeNitf/GetTacIdWorker.hpp
    modified:   web/addons/NodeNitf/NodeNitf.cpp
    deleted:    web/addons/NodeNitf/napi-inl.h
    deleted:    web/addons/NodeNitf/napi.h

no changes added to commit (use "git add" and/or "git commit -a")
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0270628..ca7f9bc 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -6,13 +6,13 @@ set(PACKAGE_VENDOR "Space Dynamics Lab")
 set(PACKAGE_NAME ${PROJECT_NAME})
 set(PACKAGE_START_YEAR 2020)
 #######
-set(externpro_REV 20.10.1)
+set(externpro_REV 21.02)
 find_package(externpro REQUIRED)
-set(internpro_REV 20.10.1)
+set(internpro_REV 21.01)
 find_package(internpro REQUIRED)
 xpSetFlags()
 set(wx_libs base)
-xpFindPkg(PKGS node palam)
+xpFindPkg(PKGS node-addon-api palam)
 #######
 set(root_srcs
   .clang-format
diff --git a/Dockerfile b/Dockerfile
index eaae3f0..70f1d88 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,26 +1,11 @@
-FROM centos:7 as build
-# yum install
+FROM ghcr.io/smanders/buildpro/centos7-bld:21.03 as build
 USER root
-RUN yum -y install centos-release-scl \
-  && yum -y install --setopt=tsflags=nodocs \
-  devtoolset-7 \
-  git \
-  wget \
-  && yum -y clean all
-# install cmake
-RUN wget -qO- https://github.com/Kitware/CMake/releases/download/v3.17.5/cmake-3.17.5-Linux-x86_64.tar.gz \
-  | tar --strip-components=1 -xz -C /usr/local/
-# install externpro
 ENV extern_DIR=/opt/extern
 WORKDIR $extern_DIR
-ENV externpro=externpro-20.10.1-gcc731-64-Linux
-ENV externpro_DIR=$extern_DIR/$externpro
-RUN wget -qO- https://github.com/smanders/externpro/releases/download/20.10.1/$externpro.tar.xz \
-  | tar -xJ -C $extern_DIR
 # install internpro
-ENV internpro=internpro-20.10.1-gcc731-64-Linux
+ENV internpro=internpro-21.01-gcc731-64-Linux
 ENV internpro_DIR=$extern_DIR/$internpro
-RUN wget -qO- https://isrhub.usurf.usu.edu/smanders/internpro/releases/download/20.10.1/$internpro.tar.xz \
+RUN wget -qO- https://isrhub.usurf.usu.edu/smanders/internpro/releases/download/21.01/$internpro.tar.xz \
   | tar -xJ -C $extern_DIR
 # install webpro
 ENV webpro=webpro-20.10.3-gcc731-64-Linux
@@ -32,8 +17,7 @@ ENV code_DIR=/code
 WORKDIR $code_DIR
 COPY . .
 WORKDIR /build
-RUN source scl_source enable devtoolset-7 \
-  && cmake $code_DIR \
+RUN cmake $code_DIR \
   && make install
 USER default

diff --git a/docker-compose.yml b/docker-compose.yml
index 8ff155d..7a13e71 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -1,7 +1,10 @@
 version: "3.8"
 services:
   app:
-    build: .
+    build:
+      context: .
+      network: host
     container_name: vivid_app
     networks:
       - vivid_net
diff --git a/web/addons/NodeNitf/CMakeLists.txt b/web/addons/NodeNitf/CMakeLists.txt
index 5f8d0fa..9741923 100644
--- a/web/addons/NodeNitf/CMakeLists.txt
+++ b/web/addons/NodeNitf/CMakeLists.txt
@@ -2,10 +2,6 @@ set(dll_name NodeNitf)
 set(dll_srcs
   GetTacIdWorker.cpp
   GetTacIdWorker.hpp
-  # TODO: remove napi files when the are in externpro
-  # https://github.com/smanders/externpro/issues/284
-  napi-inl.h
-  napi.h
   NodeNitf.cpp
   TacId.hpp
   TacId.cpp
@@ -14,7 +10,7 @@ source_group("" FILES ${dll_srcs})
 list(APPEND ${dll_name}_libsrcs ${dll_srcs})
 ########################################
 add_library(${dll_name} SHARED ${${dll_name}_libsrcs})
-target_link_libraries(${dll_name} PRIVATE palam::nitfinc xpro::node)
+target_link_libraries(${dll_name} PRIVATE palam::nitfinc xpro::node-addon-api)
 set_property(TARGET ${dll_name} PROPERTY FOLDER ${folder})
 set_target_properties(${dll_name} PROPERTIES PREFIX "" SUFFIX ".node")
 ########################################
diff --git a/web/addons/NodeNitf/GetTacIdWorker.hpp b/web/addons/NodeNitf/GetTacIdWorker.hpp
index bc3577e..0e4c6ba 100644
--- a/web/addons/NodeNitf/GetTacIdWorker.hpp
+++ b/web/addons/NodeNitf/GetTacIdWorker.hpp
@@ -9,9 +9,7 @@
 #define NODE_NITF_GET_TAC_ID_WORKER_HPP

 #include <string>
-#define NODE_ADDON_API_DISABLE_DEPRECATED
-#define NAPI_CPP_EXCEPTIONS
-#include "napi.h"
+#include <node-addon-api/napi.h>

 #include "TacId.hpp"

diff --git a/web/addons/NodeNitf/NodeNitf.cpp b/web/addons/NodeNitf/NodeNitf.cpp
index 01c4009..e2982b9 100644
--- a/web/addons/NodeNitf/NodeNitf.cpp
+++ b/web/addons/NodeNitf/NodeNitf.cpp
@@ -5,12 +5,10 @@
  * (Civilian Agencies) or DFARS 252.227-7014 (Defense Agencies).
  */

-#define NODE_ADDON_API_DISABLE_DEPRECATED
-#define NAPI_CPP_EXCEPTIONS
 #include <memory>
 #include <string>

-#include "napi.h"
+#include <node-addon-api/napi.h>

 #include "GetTacIdWorker.hpp"