Giter VIP home page Giter VIP logo

boost-lib's Introduction

boost-lib (MIT)

About

boos-lib is a Boost dependency manager for CMake.js based native modules.

Everyone knowns about Boost. It's the base of almost all C++ projects out there. But using it in native node addons was not so easy (until now). It's a huge download that can be accessible by navigating through the infamous Sourceforge Crapware Screens. After downloading, you will get the source files, bundled with Jam based build system that is impossible to integrate with node-gyp.

So, if you wanted to create a native node module with Boost dependency, then you had the only option that to write down somewhere in the readme that your module requires Boost 1.x, and that's it. Your module consumers had to install Boost 1.x, compile it, set some environment variables pointing to their installation, and hope for that works with your module. There is not a miracle that there is no Boost based native node module exists ... yet.

The good news there is hope. With CMake.js you can create native modules with Boost dependency. If your module consumer has the appropriate Boost version installed, then your module will use that. If not, then boost-lib module downloads Boost from Github, compiles the required libraries (only the required ones), and your module will use that installation. Everything is automatic and as fast as possible. A typical Boost installation with one required library takes about ~1.5 minutes, and that has to be done only once, any following module installations will use that deployment.

CMake.js

CMake.js is a Node.js/io.js native addon build tool which works exactly like node-gyp, but instead of gyp, it is based on CMake build system. It's on the npm.

Installation

npm install boost-lib

Usage

In a nutshell. (For more complete documentation please see the tutorial.)

1. Include

Install boost-lib module from npm.

npm install boost-lib --save

Enter the following into your project's root CMakeLists.txt file to include BoostLib CMake module:

# Include BoostLib module
SET(CMAKE_MODULE_PATH  
    "${CMAKE_CURRENT_SOURCE_DIR}/node_modules/boost-lib/cmake")

include(BoostLib)

2. Require

This makes require_boost_libs function available. It has two arguments:

  • in the first argument you have to specify required Boost library's semver specification like that you can use in package.json. For example to use Boost 1.58, enter 1.58.0, to use Boost 1.57 or above, enter ">= 1.57.0". See semver modules's documentation for further details.
  • in the second argument you can specify required Boost's to-be-compiled libraries separated by semicolon. Leave blank if you need header only libraries. For example to depend on Boost.coroutine and Boost.DateTime, enter coroutine;date_time.

Examples:

Boost 1.57 or above required with thread and context libraries:

require_boost_libs(">= 1.57.0" thread;context)

Boost 1.58 required with header only libraries:

require_boost_libs(1.57.0 "")

Known to-be-build Boost libraries so far:

  • chrono
  • coroutine
  • context
  • filesystem
  • graph
  • locale
  • log
  • system
  • thread
  • timer
  • wave

Their internal dependencies are handled automatically. So if you're requireing coroutine which depends on context and system, you don't have to sepcify them all, only coroutine.

3. Use

Boost's include path should be registered by entering:

include_directories(${Boost_INCLUDE_DIRS})

And you have to link your addon with Boost libraries:

target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB};${Boost_LIBRARIES})

Example and tutorial

The tutorial is about making the example module, which can be downloaded from there:

git clone https://github.com/unbornchikken/cmake-js-tut-04-boost-module

boost-lib's People

Contributors

0181532686cf4a31163be0bf3e6bb6732bf avatar unbornchikken avatar xtansia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

boost-lib's Issues

Boost download fails due to 'Unhandled stream error'

I cannot get a native module I am creating to build, since the cmake-js rebuild fails to finish downloading Boost. I tried cloning your tutorial repository and had the same experience. Included below is the cmake-js output and a link to a gist of the full npm error log.

info CMD CLEAN
info RUN cmake -E remove_directory "C:\Users\toxicwolf\OneDrive\Documents\Development\cmake-js-tut-04-boost-module\build"
info CMD CONFIGURE
info RUN cmake "C:\Users\toxicwolf\OneDrive\Documents\Development\cmake-js-tut-04-boost-module" --no-warn-unused-cli -G"Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE="Release" -DCMAKE_JS_INC="C:\Users\toxicwolf\.cmake-js\iojs-x64\v2.3.1\src;C:\Users\toxicwolf\.cmake-js\iojs-x64\v2.3.1\deps\v8\include;C:\Users\toxicwolf\.cmake-js\iojs-x64\v2.3.1\deps\uv\include;C:\Users\toxicwolf\OneDrive\Documents\Development\cmake-js-tut-04-boost-module\node_modules\nan" -DNODE_RUNTIME="iojs" -DNODE_RUNTIMEVERSION="2.3.1" -DNODE_ARCH="x64" -DCMAKE_JS_LIB="C:\Users\toxicwolf\.cmake-js\iojs-x64\v2.3.1\win-x64\iojs.lib"
Not searching for unused variables given on the command line.
-- The C compiler identification is MSVC 19.0.23026.0
-- The CXX compiler identification is MSVC 19.0.23026.0
-- Check for working C compiler using: Visual Studio 14 2015 Win64
-- Check for working C compiler using: Visual Studio 14 2015 Win64 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler using: Visual Studio 14 2015 Win64
-- Check for working CXX compiler using: Visual Studio 14 2015 Win64 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Require Boost Libs module started.
-- Required Boost version: >= 1.58.0
-- Required libs: coroutine
-- Boost Lib Installer starting.
-- Invoking boost-lib to download Boost >= 1.58.0.
info BOOST Searching for Boost >= 1.58.0 in 'C:\Users\toxicwolf\.cmake-js\boost'.
info BOOST Boost found in 'C:\Users\toxicwolf\.cmake-js\boost\1.58.0'.
info BOOST Downloading submodules of director 'libs'.
info BOOST Downloading submodules of director 'numeric'.
http BOOST Downloading: https://github.com/boostorg/numeric_conversion/archive/boost-1.58.0.tar.gz
http BOOST Downloading: https://github.com/boostorg/numeric_interval/archive/boost-1.58.0.tar.gz
http BOOST Downloading: https://github.com/boostorg/numeric_odeint/archive/boost-1.58.0.tar.gz
http BOOST Downloading: https://github.com/boostorg/numeric_ublas/archive/boost-1.58.0.tar.gz
stream.js:74
      throw er; // Unhandled stream error in pipe.
            ^
Error: incorrect header check
    at Zlib._handle.onerror (zlib.js:363:17)
CMake Error at node_modules/boost-lib/cmake/DownloadBoost.cmake:10 (message):
  Download error.
Call Stack (most recent call first):
  node_modules/boost-lib/cmake/BoostLibInstaller.cmake:23 (download_boost)
  node_modules/boost-lib/cmake/BoostLib.cmake:19 (boost_lib_installer)
  CMakeLists.txt:17 (require_boost_libs)

https://gist.github.com/toxicwolf/a476630506cff18b2b72

Error on "Generating headers..." when using boost >= 1.70.0

I have a workaround for this now because I am using boost 1.69.0, but I have found that the error is connected to this issue: #boostorg/bcp#9.
Here is the error:

-- Generating headers ...
boost-install.jam: No such file or directory
CMake Error at node_modules/boost-lib/cmake/BoostLibInstaller.cmake:187 (message):
b2 error:

Jamroot:308: in boost-install

ERROR: rule "boost-install.boost-install" unknown in module
"Jamfile<C:\Users\Hrvoje.cmake-js\boost\1.74.0>".

libs\atomic\build\Jamfile.v2:58: in modules.load

C:/Users/Hrvoje/.cmake-js/boost/1.74.0/tools/build/src/build\project.jam:372:
in load-jamfile

C:/Users/Hrvoje/.cmake-js/boost/1.74.0/tools/build/src/build\project.jam:64:
in load

C:/Users/Hrvoje/.cmake-js/boost/1.74.0/tools/build/src/build\project.jam:89:
in load-used-projects

C:/Users/Hrvoje/.cmake-js/boost/1.74.0/tools/build/src/build\project.jam:75:
in load

C:/Users/Hrvoje/.cmake-js/boost/1.74.0/tools/build/src/build\project.jam:142:
in project.find

C:/Users/Hrvoje/.cmake-js/boost/1.74.0/tools/build/src\build-system.jam:618:
in load

C:/Users/Hrvoje/.cmake-js/boost/1.74.0/tools/build/src/kernel\modules.jam:295:
in import

C:/Users/Hrvoje/.cmake-js/boost/1.74.0/tools/build/src/kernel/bootstrap.jam:139:
in boost-build

C:/Users/Hrvoje/.cmake-js/boost/1.74.0/boost-build.jam:17: in module scope

Call Stack (most recent call first):
node_modules/boost-lib/cmake/BoostLib.cmake:19 (boost_lib_installer)
cmake_modules/dependencies/sb/Boost.cmake:5 (require_boost_libs)
cmake_modules/dependencies/sb/SBDependencies.cmake:4 (include)
cmake_modules/dependencies/Dependencies.cmake:3 (include)
CMakeLists.txt:27 (include)

Deprecation warning when building on recent OSX

When using the lib with recent XCode, I'm getting a bunch of deprecation warnings like this one:

In file included from /Users/administrator/.cmake-js/boost/1.61.0/boost/asio/ip/tcp.hpp:19:
In file included from /Users/administrator/.cmake-js/boost/1.61.0/boost/asio/basic_socket_acceptor.hpp:19:
In file included from /Users/administrator/.cmake-js/boost/1.61.0/boost/asio/basic_io_object.hpp:19:
In file included from /Users/administrator/.cmake-js/boost/1.61.0/boost/asio/io_service.hpp:767:
In file included from /Users/administrator/.cmake-js/boost/1.61.0/boost/asio/impl/io_service.hpp:71:
In file included from /Users/administrator/.cmake-js/boost/1.61.0/boost/asio/detail/task_io_service.hpp:196:
In file included from /Users/administrator/.cmake-js/boost/1.61.0/boost/asio/detail/impl/task_io_service.hpp:19:
In file included from /Users/administrator/.cmake-js/boost/1.61.0/boost/asio/detail/completion_handler.hpp:20:
In file included from /Users/administrator/.cmake-js/boost/1.61.0/boost/asio/detail/fenced_block.hpp:24:
/Users/administrator/.cmake-js/boost/1.61.0/boost/asio/detail/macos_fenced_block.hpp:45:5: warning: 'OSMemoryBarrier' is deprecated: first deprecated in macOS 10.12
      - Use std::atomic_thread_fence() from <atomic> instead [-Wdeprecated-declarations]
    OSMemoryBarrier();
    ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/libkern/OSAtomicDeprecated.h:749:9: note: 
      'OSMemoryBarrier' has been explicitly marked deprecated here
void    OSMemoryBarrier( void );
        ^

Linking against wrong library path

Hi!

Using

Node 16.13.1 and CMake 3.24.3 I fail to link against boost libraries via

include(BoostLib)

require_boost_libs("1.69.0" "date_time")
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})

I get error:

make[2]: *** No rule to make target '~/.cmake-js/boost/1.69.0/stage/lib/libboost_iostreams-mt-d.a', needed by 'Debug/CppAddon.node'.  Stop.

for both OSX and Linux.

The issue is that BoostLibInstaller.cmake is setting the lib_path:

if((CMAKE_BUILD_TYPE STREQUAL "Debug") OR (CMAKE_BUILD_TYPE STREQUAL "") OR (NOT DEFINED CMAKE_BUILD_TYPE))
    set(lib_path "${install_dir}/${stage_dir}/lib/libboost_${lib_name}-mt-d.a")
else()
    set(lib_path "${install_dir}/${stage_dir}/lib/libboost_${lib_name}-mt.a")
endif()

that is used:

set_target_properties(${boost_lib} PROPERTIES
    IMPORTED_LOCATION "${lib_path}"
    LINKER_LANGUAGE CXX)

However, using the b2 args:

Linux:

link=static;threading=multi;runtime-link=shared;--build-dir=Build;stage;--stagedir=stage;-d+2;--hash;--ignore-site-config;variant=debug;--layout=tagged;-sNO_BZIP2=1;cxxflags=-fPIC;cxxflags=-std=c++11;toolset=gcc

OSX:

link=static;threading=multi;runtime-link=shared;--build-dir=Build;stage;--stagedir=stage;-d+2;--hash;--ignore-site-config;variant=debug;toolset=clang;cxxflags=-fPIC;cxxflags=-std=c++11;cxxflags=-stdlib=libc++;linkflags=-stdlib=libc++;architecture=combined;address-model=32_64;--layout=tagged

will generate binaries like:

Linux:

libboost_chrono-mt-d-x64.a

OSX:

libboost_chrono-mt-d-c32_64.a

Linking will later fail, as the path is set to the non existing libboost_chrono-mt-d.a lib.

I am not sure why these suffixes are added by BOOST build system, but the BoostLibInstaller.cmake can be patched like so:

if(UNIX)
    set(LIBSUFFIX "-x32")
    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
            set(LIBSUFFIX "-x64")
    endif()
endif()
if(APPLE)
    string(REGEX MATCH "address-model\\=([0-9_]+)" model "${b2Args}")
    set(ADDRESS_MODEL ${CMAKE_MATCH_1})
    set(LIBSUFFIX "-c${ADDRESS_MODEL}")
endif()
if((CMAKE_BUILD_TYPE STREQUAL "Debug") OR (CMAKE_BUILD_TYPE STREQUAL "") OR (NOT DEFINED CMAKE_BUILD_TYPE))
    set(lib_path "${install_dir}/${stage_dir}/lib/libboost_${lib_name}-mt-d${LIBSUFFIX}.a")
else()
    set(lib_path "${install_dir}/${stage_dir}/lib/libboost_${lib_name}-mt${LIBSUFFIX}.a")
endif()

which is a bit better than the symlink step users of the BoostLib can use instead:

if (Boost_FOUND)
    # https://github.com/cmake-js/boost-lib/blob/master/cmake/GetBoostLibB2Args.cmake
    get_boots_lib_b2_args()
    string(REGEX MATCH "address-model\\=([0-9_]+)" model "${b2Args}")
    set(ADDRESS_MODEL ${CMAKE_MATCH_1})
    if(APPLE)
        set(ADDRESS_MODEL "c${ADDRESS_MODEL}")
    endif()
    if (NOT ADDRESS_MODEL OR ADDRESS_MODEL STREQUAL "")
        set(ADDRESS_MODEL "x64") # Lets assume x64 for linux
    endif()
    string(REGEX MATCH "variant\\=([A-Za-z0-9_])" variant "${b2Args}")
    set(VARIANT "-${CMAKE_MATCH_1}")

    foreach(lib ${Boost_LIBRARIES})
        set(want_lib "lib${lib}-mt${VARIANT}.a")
        set(have_lib "lib${lib}-mt${VARIANT}-${ADDRESS_MODEL}.a")
        if(NOT EXISTS "${Boost_INCLUDE_DIR}/stage/lib/${want_lib}")
            EXECUTE_PROCESS(COMMAND ln -sf ${have_lib} ${want_lib}
                WORKING_DIRECTORY "${Boost_INCLUDE_DIR}/stage/lib"
            )
            message(STATUS "Symlinked ${Boost_INCLUDE_DIR}/stage/lib/${have_lib} -> ${Boost_INCLUDE_DIR}/stage/lib/${want_lib}")
        endif()
    endforeach()
endif()

Best would be if the b2 output lib did not get prefixed...

Embedding

Hi, I have to ship my addon as a precompiled binary (I think via node-pre-gyp). As far as I can see, boost-lib is compiled as static now, what are my steps to make the installation redistributable?

node_modules directory empty at install time

I have a module i'm developing that depends on boost and I'm to get this working.

It works fine when i run npm install from the root of the directory itself. But when i actually push the module to github and npm install this module from another project the install script fails because it can't find the boost-lib directory anywhere. These seems to be because the node_modules directory is not available at the time npm runs the install script.

Here is the relevant bit of my CMakeLists.txt:

...
message("Searching In: ${CMAKE_CURRENT_SOURCE_DIR}")
file(GLOB_RECURSE boostlib_cmake_path "${CMAKE_CURRENT_SOURCE_DIR}/node_modules" "BoostLib.cmake")
list(GET boostlib_cmake_path 0 boostlib_cmake_path)
get_filename_component(boostlib_cmake_path "${boostlib_cmake_path}" DIRECTORY)
SET(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${boostlib_cmake_path}")
include(BoostLib)
require_boost_libs(">= 1.6.0" system)
...

The package.json of the module itself has the following:

{
// ...
  "scripts": {
    "install": "ls node_modules && cmake-js compile"
  },
  "dependencies": {
    "boost-lib": "^0.11.3",
    "cmake-js": "^3.4.0",
    "nan": "^2.5.1"
  },
}

The error CMake gives when i try to npm install the module from another project:

CMake Error at CMakeLists.txt:22 (list):
  list GET given empty list


CMake Error at CMakeLists.txt:25 (include):
  include could not find load file:

    BoostLib


CMake Error at CMakeLists.txt:26 (require_boost_libs):
  Unknown CMake command "require_boost_libs".

The result of running ls node_modules at the time the module's npm install script gets called:

ls: node_modules: No such file or directory

Not sure where i should be looking for boost-lib if there's no node_modules directory yet. Thoughts?

GCC version fix

While building example repo, got another error (after #3 ):

[30/42] Performing build step for 'boost_coroutine_jam'
FAILED: cd /home/lyssdod/.cmake-js/boost/1.61.0 && /usr/bin/cmake -P /storage/work/cmake-js-tut-04-boost-module/build/boost-1.61.0/boost_coroutine_jam-build-Release.cmake && /usr/bin/cmake -E touch /storage/work/cmake-js-tut-04-boost-module/build/boost-1.61.0/boost_coroutine_jam-build
CMake Error at /storage/work/cmake-js-tut-04-boost-module/build/boost-1.61.0/boost_coroutine_jam-build-Release.cmake:16 (message):
  Command failed: 1

   './b2' 'link=static' 'threading=multi' 'runtime-link=shared' '--build-dir=Build' '--ignore-site-config' 'stage' '--stagedir=stage' '-d+2' '--hash' 'variant=release' '--layout=tagged' '-sNO_BZIP2=1' 'cxxflags=-fPIC' 'cxxflags=-std=c++11' 'toolset=gcc-5.4' '--with-coroutine'

  See also

    /storage/work/cmake-js-tut-04-boost-module/build/boost-1.61.0/boost_coroutine_jam-build-*.log

Solution: https://safenetforum.org/t/build-on-arch-x64-fails/2209/2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.