Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
Port build system to node-cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarex committed Nov 29, 2016
1 parent 8b49a9e commit 5667a33
Showing 12 changed files with 253 additions and 446 deletions.
26 changes: 15 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# We actually depend on this being the generic image
language: generic
language: node

git:
depth: 10
@@ -25,6 +24,7 @@ sudo: false

env:
global:
- JOBS=4
- CCACHE_TEMPDIR=/tmp/.ccache-temp
- CCACHE_COMPRESS=1
- CASHER_TIME_OUT=1000
@@ -42,52 +42,56 @@ script_default: &build_and_publish
if [[ "${TRAVIS_OS_NAME}" == "osx" ]]; then
rvm get head || true
fi
- nvm install ${NODE}
- nvm use ${NODE}
- source ./scripts/build.sh
- ./scripts/publish.sh

matrix:
include:
# Linux
- os: linux
env: NODE="4" TARGET=Release
env: NODE="4" BUILD_TYPE=Release
addons: *apt_packages
script: *build_and_publish
- os: linux
env: NODE="4" TARGET=Debug NPM_FLAGS="--debug"
env: NODE="4" BUILD_TYPE=Debug NPM_FLAGS="--debug"
addons: *apt_packages
script: *build_and_publish
- os: linux
env: NODE="6" TARGET=Release
env: NODE="6" BUILD_TYPE=Release
addons: *apt_packages
script: *build_and_publish
- os: linux
env: NODE="6" TARGET=Debug NPM_FLAGS="--debug"
env: NODE="6" BUILD_TYPE=Debug NPM_FLAGS="--debug"
addons: *apt_packages
script: *build_and_publish
# OS X
- os: osx
# https://docs.travis-ci.com/user/languages/objective-c/#Supported-OS-X-iOS-SDK-versions
osx_image: xcode7.3
env: NODE="4" TARGET=Release
env: NODE="4" BUILD_TYPE=Release
script: *build_and_publish
- os: osx
osx_image: xcode7.3
env: NODE="4" TARGET=Debug NPM_FLAGS="--debug"
env: NODE="4" BUILD_TYPE=Debug NPM_FLAGS="--debug"
script: *build_and_publish
- os: osx
# https://docs.travis-ci.com/user/languages/objective-c/#Supported-OS-X-iOS-SDK-versions
osx_image: xcode7.3
env: NODE="6" TARGET=Release
env: NODE="6" BUILD_TYPE=Release
script: *build_and_publish
- os: osx
osx_image: xcode7.3
env: NODE="6" TARGET=Debug NPM_FLAGS="--debug"
env: NODE="6" BUILD_TYPE=Debug NPM_FLAGS="--debug"
script: *build_and_publish
# Linux coverage build
- os: linux
env: NODE="4" TARGET=Debug COVERAGE=true NPM_FLAGS="--debug"
env: NODE="4" BUILD_TYPE=Debug COVERAGE=true NPM_FLAGS="--debug"
addons: *apt_packages
script:
- nvm install ${NODE}
- nvm use ${NODE}
- source ./scripts/build.sh
- curl -S -f https://codecov.io/bash -o codecov
- chmod +x codecov
106 changes: 106 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
cmake_minimum_required(VERSION 3.1)
project(node-osrm C CXX)

# Explicitly set the build type to Release if no other type is specified
# on the command line. Without this, cmake defaults to an unoptimized,
# non-debug build, which almost nobody wants.
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "No build type specified, defaulting to Release")
set(CMAKE_BUILD_TYPE Release)
endif()

option(BUILD_LIBOSRM "Download and build own libsorm version" OFF)

set(OSRM_BINARIES "")
if(BUILD_LIBOSRM)
set(OSRM_INSTALL_DIR "/tmp/osrm-backend")
set(OSRM_DEPS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/deps/osrm-backend-${CMAKE_BUILD_TYPE}")

execute_process(COMMAND node -p "require('../package.json').osrm_release" OUTPUT_VARIABLE OSRM_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "Using osrm ${OSRM_VERSION}")
message(STATUS "Downloading to ${OSRM_DEPS_DIR}")
message(STATUS "Installing to ${OSRM_INSTALL_DIR}")

execute_process(COMMAND git clone https://github.com/Project-OSRM/osrm-backend.git ${OSRM_DEPS_DIR})
execute_process(COMMAND git checkout ${OSRM_VERSION} WORKING_DIRECTORY ${OSRM_DEPS_DIR})

# These will be set by the subproject
# LibOSRM_INCLUDE_DIRS
# LibOSRM_LIBRARIES
# LibOSRM_DEPENDENT_LIBRARIES
# LibOSRM_CXXFLAGS
set(ENABLE_MASON ON CACHE INTERNAL "Always use mason for building osrm-backend" FORCE)
add_subdirectory(${OSRM_DEPS_DIR})
include(${OSRM_DEPS_DIR}/third_party/mason/mason.cmake)

# FIXME we can remove this double versioning after the merge
set(MASON_TBB_VERSION "43_20150316")
mason_use(tbb VERSION ${MASON_TBB_VERSION})

# We need this because we depend on the shared libraries from TBB
foreach(libpath ${MASON_PACKAGE_tbb_LIBRARY_DIRS})
file(GLOB TBBGlob ${libpath}/*.*)
foreach(filepath ${TBBGlob})
get_filename_component(filename ${filepath} NAME)
set(targetfile "${CMAKE_SOURCE_DIR}/lib/binding/${filename}")
add_custom_command(OUTPUT ${targetfile}
COMMAND ${CMAKE_COMMAND} -E copy ${filepath} ${CMAKE_SOURCE_DIR}/lib/binding/
DEPENDS ${filepath})
list(APPEND OSRM_BINARIES "${targetfile}")
endforeach()
endforeach()

add_custom_command(OUTPUT ${CMAKE_SOURCE_DIR}/lib/binding/osrm-extract
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:osrm-extract> ${CMAKE_SOURCE_DIR}/lib/binding/
DEPENDS osrm-extract)
add_custom_command(OUTPUT ${CMAKE_SOURCE_DIR}/lib/binding/osrm-contract
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:osrm-contract> ${CMAKE_SOURCE_DIR}/lib/binding/
DEPENDS osrm-contract)
add_custom_command(OUTPUT ${CMAKE_SOURCE_DIR}/lib/binding/osrm-routed
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:osrm-routed> ${CMAKE_SOURCE_DIR}/lib/binding/
DEPENDS osrm-routed)
add_custom_command(OUTPUT ${CMAKE_SOURCE_DIR}/lib/binding/osrm-datastore
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:osrm-datastore> ${CMAKE_SOURCE_DIR}/lib/binding/
DEPENDS osrm-datastore)
list(APPEND OSRM_BINARIES "${CMAKE_SOURCE_DIR}/lib/binding/osrm-extract")
list(APPEND OSRM_BINARIES "${CMAKE_SOURCE_DIR}/lib/binding/osrm-contract")
list(APPEND OSRM_BINARIES "${CMAKE_SOURCE_DIR}/lib/binding/osrm-routed")
list(APPEND OSRM_BINARIES "${CMAKE_SOURCE_DIR}/lib/binding/osrm-datastore")
else()
# These will be set by the find script
# LibOSRM_INCLUDE_DIRS
# LibOSRM_LIBRARIES
# LibOSRM_DEPENDENT_LIBRARIES
# LibOSRM_CXXFLAGS
find_package(LibOSRM REQUIRED)
endif()

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/node_modules/node-cmake")

set(NodeJS_CXX_STANDARD 14 CACHE INTERNAL "Use C++14" FORCE)
set(NodeJS_DOWNLOAD ON CACHE INTERNAL "Download node.js sources" FORCE)
set(NodeJS_USE_CLANG_STDLIB OFF CACHE BOOL "Don't use libc++ by default" FORCE)
find_package(NodeJS REQUIRED)
add_nodejs_module(node-osrm src/node_osrm.cpp)

include_directories(SYSTEM ${LibOSRM_INCLUDE_DIRS})
link_directories(${LibOSRM_LIBRARY_DIRS})
target_link_libraries(node-osrm ${LibOSRM_LIBRARIES} ${LibOSRM_DEPENDENT_LIBRARIES})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LibOSRM_CXXFLAGS}")

# Enforce proper rpath for osrm.node
if(APPLE)
set(LINKER_FLAGS "-Wl,-rpath,@loader_path/")
elseif(UNIX)
set(LINKER_FLAGS "'-Wl,-rpath,$ORIGIN' -Wl,-z,origin")
endif()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINKER_FLAGS}")

add_custom_command(TARGET node-osrm POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:node-osrm> ${CMAKE_SOURCE_DIR}/lib/binding/osrm.node)
add_custom_target(copy_osrm_binaries ALL
DEPENDS ${OSRM_BINARIES}
)

86 changes: 16 additions & 70 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,73 +1,19 @@
# http://www.gnu.org/prep/standards/html_node/Standard-Targets.html#Standard-Targets
all: release

export TMP_PREFIX ?= /tmp/osrm-backend
node_modules: package.json
npm install --ignore-scripts # Install dependencies but don't run our own install script.

all: build/Release/osrm.node
# build the node module and libosrm using cmake
build/%/osrm.node: ./node_modules
mkdir -p build &&\
cd build &&\
cmake .. -DCMAKE_BUILD_TYPE=$* -DBUILD_LIBOSRM=On &&\
VERBOSE=1 make -j${JOBS} &&\
cd ..

# osrm-backend provides libosrm.pc for use with
# pkg-config and the node-osrm build (via binding.gyp)
# calls out to pkg-config to figure out details of the
# osrm-backend install. So, here we stop builds quickly
# if pkg-config is not found.
ifeq (, $(shell which pkg-config))
$(error "No pkg-config found: please ensure you have pkg-config installed")
endif
release: build/Release/osrm.node

# Build osrm-backend in release mode
# locally in the deps/ directory using
# mason dependencies. The goal here is
# to have a portable build that can be
# packaged and published for all Linux
# and OS X users
./deps/osrm-backend-Release:
./bootstrap.sh

# Build osrm-backend in debug mode
./deps/osrm-backend-Debug:
export BUILD_TYPE=Debug && ./bootstrap.sh

# This target (all the way down to the verbose target) use a customized
# way of installing that is roughly equivalent to just running
# "npm install" but faster for rebuilds. The idea is to allow you to
# make changes to src/node-osrm.cpp and then retype "make" in order to
# iterate quickly.

# Install all dependencies and devDependencies
# without actually triggering a node-osrm compile
# TODO: there should be a cleaner way to do this?
# Note: the `--clang` is harmless when building with gcc
# but removes known compiler flags that break with clang
./node_modules:
npm install `node -e "console.log(Object.keys(require('./package.json').dependencies).join(' '))"` \
`node -e "console.log(Object.keys(require('./package.json').devDependencies).join(' '))"` --clang=1

# put the local osrm-backend on PKG_CONFIG_PATH, show the developer that
# the local version is being used by default, and build using node-pre-gyp
# directly (this is more direct than "npm install" which just calls node-pre-gyp anyway)
# We use "--loglevel=error" to quiet the verbosity to make it easier to see compiler errors quickly
build/Release/osrm.node: ./node_modules ./deps/osrm-backend-Release
@export PKG_CONFIG_PATH="$(TMP_PREFIX)/lib/pkgconfig" && \
echo "*** Using osrm installed at `pkg-config libosrm --variable=prefix` ***" && \
./node_modules/.bin/node-pre-gyp configure build --loglevel=error --clang=1 $(NPM_FLAGS)

# put the local debug-built osrm-backend on PKG_CONFIG_PATH and build as normal
debug: ./node_modules ./deps/osrm-backend-Debug
@export PKG_CONFIG_PATH="$(TMP_PREFIX)/lib/pkgconfig" && \
echo "*** Using osrm installed at `pkg-config libosrm --variable=prefix` ***" && \
./node_modules/.bin/node-pre-gyp configure build --debug --clang=1 $(NPM_FLAGS)

coverage: ./node_modules ./deps/osrm-backend-Debug
@export PKG_CONFIG_PATH="$(TMP_PREFIX)/lib/pkgconfig" && \
export LDFLAGS="--coverage" && export CXXFLAGS="--coverage" && \
echo "*** Using osrm installed at `pkg-config libosrm --variable=prefix` ***" && \
./node_modules/.bin/node-pre-gyp configure build --debug --clang=1 $(NPM_FLAGS)

# same as typing "make" (which hits the "build/Release/osrm.node" target) except that
# "--loglevel=verbose" shows the actual compiler arguments
verbose: ./node_modules ./deps/osrm-backend-Release
@export PKG_CONFIG_PATH="$(TMP_PREFIX)/lib/pkgconfig" && \
echo "*** Using osrm installed at `pkg-config libosrm --variable=prefix` ***" && \
./node_modules/.bin/node-pre-gyp configure build --loglevel=verbose --clang=1 $(NPM_FLAGS)
debug: build/Debug/osrm.node

clean:
(cd test/data/ && $(MAKE) clean)
@@ -83,11 +29,11 @@ grind:
# Note: this PATH setting is used to allow the localized tools to be used
# but your locally installed osrm-backend tool, if on PATH should override
shm: ./test/data/Makefile
@PATH="${PATH}:./lib/binding" && echo "*** Using osrm-datastore from `which osrm-datastore` ***"
@PATH="${PATH}:./lib/binding" && $(MAKE) -C ./test/data
@PATH="${PATH}:./lib/binding" && osrm-datastore ./test/data/berlin-latest.osrm
@PATH="$(PATH):./lib/binding" && echo "*** Using osrm-datastore from `which osrm-datastore` ***"
$(MAKE) -C ./test/data
PATH="$(PATH):./lib/binding" && osrm-datastore ./test/data/berlin-latest.osrm

test: shm
npm test

.PHONY: test clean build shm
.PHONY: test clean build shm debug release
63 changes: 17 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -121,63 +121,36 @@ make && make test
## Using an existing local osrm-backend
If you do wish to build node-osrm against an existing osrm-backend that you have on your system you will need:
If you do wish to build node-osrm against an existing osrm-backend we assume it is installed and will be found by pkg-config.
- OSRM develop branch cloned, built from source, and installed
- The test data initialized: `make -C test/data` inside the `osrm-backend` directory
To check if you installed it correctly the following command and verify the output:
See [Project-OSRM wiki](https://github.com/Project-OSRM/osrm-backend/wiki/Building%20OSRM) for details.
which osrm-extract
which osrm-contract
which osrm-datastore
pkg-config libosrm --variable=prefix
Once Project-OSRM is built you should be able to run:
pkg-config libosrm --variable=prefix
Which should return the path to where you installed Project-OSRM.
See the [Project-OSRM wiki](https://github.com/Project-OSRM/osrm-backend/wiki/Building%20OSRM) for details in how to build osrm-backend from source.
Now you can build `node-osrm`:
git clone https://github.com/Project-OSRM/node-osrm.git
cd node-osrm
npm install --build-from-source
To run the tests against your local osrm-backend's data you will need to
set the `OSRM_DATA_PATH` variable:
export OSRM_DATA_PATH=/path/to/osrm-backend/test/data
And you will need to remove, if they exist, any previous builds that created local binaries of osrm-backend because the `osrm-extract` and other tools here will be used in preference of your global installation:
rm -rf lib/binding/
Then you can run `npm test`.
To recap, here is a full example of building against an osrm-backend that is cloned beside node-osrm but installed into a custom location:
```
export PATH=/opt/osrm/bin:${PATH}
export PKG_CONFIG_PATH=/opt/osrm/lib/pkgconfig
pkg-config libosrm --variable=prefix
# if boost headers are in a custom location give a hint about that
# here we assume the are in `/opt/boost`
export CXXFLAGS="-I/opt/boost/include"
npm install --build-from-source
# build the osrm-backend test data
make -C ../osrm-backend/test/data
export OSRM_DATA_PATH=../osrm-backend/test/data
npm test
```
mkdir build
cd build
cmake ..
make clean
make
make test
# Developing
After setting up a [Source Build](#source-build) you can make changes to the code and rebuild like:
npm install --build-from-source
After setting up a [Source Build](#source-build) you can make changes to the code and rebuild like any other cmake project:
But that will trigger a full re-configure if any changes occurred to dependencies.
cd build
make
However you can optionally use the Makefile which simplifies some common needs.
To rebuild using cached data:
To rebuild using with a full re-configuration do:
make
@@ -189,8 +162,6 @@ If you want to build in debug mode (-DDEBUG -O0) then do:
make debug
Under the hood this uses [node-pre-gyp](https://github.com/mapbox/node-pre-gyp) (which itself used [node-gyp](https://github.com/TooTallNate/node-gyp)) to compile the source code.
# Testing
Run the tests like:
Loading

0 comments on commit 5667a33

Please sign in to comment.