1
1
mirror of https://github.com/nmattia/niv.git synced 2024-12-01 15:56:03 +03:00

add cpp example

This commit is contained in:
brano543 2019-12-19 14:53:18 +01:00
parent abd0de3269
commit 67af18c332
10 changed files with 613 additions and 0 deletions

View File

@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 3.6)
set (CMAKE_CXX_STANDARD 17)
# ---[ Project name
project(hello-world)
# ---[ Output directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# ---[ Using cmake scripts and modules
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
# ---[ Includes
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/src/include)
include_directories(${EXTERNAL_INCLUDE_DIRECTORIES})
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
# ---[ Compiler flags
set(CMAKE_CXX_FLAGS_DEBUG "-g -pthread")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -pthread")
add_compile_options(-Wall -Wextra -pedantic -Werror)
# ---[ Handle dependencies
include(cmake/Dependencies.cmake)
# ---[ Subdirectories
add_subdirectory(src)

View File

@ -0,0 +1,3 @@
# ---[ LibOsmium
find_package(Osmium REQUIRED COMPONENTS pbf)
include_directories(SYSTEM ${OSMIUM_INCLUDE_DIRS})

View File

@ -0,0 +1,317 @@
#----------------------------------------------------------------------
#
# FindOsmium.cmake
#
# Find the Libosmium headers and, optionally, several components needed for
# different Libosmium functions.
#
#----------------------------------------------------------------------
#
# Usage:
#
# Copy this file somewhere into your project directory, where cmake can
# find it. Usually this will be a directory called "cmake" which you can
# add to the CMake module search path with the following line in your
# CMakeLists.txt:
#
# list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
#
# Then add the following in your CMakeLists.txt:
#
# find_package(Osmium REQUIRED COMPONENTS <XXX>)
# include_directories(SYSTEM ${OSMIUM_INCLUDE_DIRS})
#
# For the <XXX> substitute a space separated list of one or more of the
# following components:
#
# pbf - include libraries needed for PBF input and output
# xml - include libraries needed for XML input and output
# io - include libraries needed for any type of input/output
# geos - include if you want to use any of the GEOS functions
# gdal - include if you want to use any of the OGR functions
# proj - include if you want to use any of the Proj.4 functions
# sparsehash - include if you use the sparsehash index
#
# You can check for success with something like this:
#
# if(NOT OSMIUM_FOUND)
# message(WARNING "Libosmium not found!\n")
# endif()
#
#----------------------------------------------------------------------
#
# Variables:
#
# OSMIUM_FOUND - True if Osmium found.
# OSMIUM_INCLUDE_DIRS - Where to find include files.
# OSMIUM_XML_LIBRARIES - Libraries needed for XML I/O.
# OSMIUM_PBF_LIBRARIES - Libraries needed for PBF I/O.
# OSMIUM_IO_LIBRARIES - Libraries needed for XML or PBF I/O.
# OSMIUM_LIBRARIES - All libraries Osmium uses somewhere.
#
#----------------------------------------------------------------------
# Look for the header file.
find_path(OSMIUM_INCLUDE_DIR osmium/osm.hpp
PATH_SUFFIXES include
PATHS
../libosmium
~/Library/Frameworks
/Library/Frameworks
/opt/local # DarwinPorts
/opt
)
set(OSMIUM_INCLUDE_DIRS "${OSMIUM_INCLUDE_DIR}")
#----------------------------------------------------------------------
#
# Check for optional components
#
#----------------------------------------------------------------------
if(Osmium_FIND_COMPONENTS)
foreach(_component ${Osmium_FIND_COMPONENTS})
string(TOUPPER ${_component} _component_uppercase)
set(Osmium_USE_${_component_uppercase} TRUE)
endforeach()
endif()
#----------------------------------------------------------------------
# Component 'io' is an alias for 'pbf' and 'xml'
if(Osmium_USE_IO)
set(Osmium_USE_PBF TRUE)
set(Osmium_USE_XML TRUE)
endif()
#----------------------------------------------------------------------
# Component 'ogr' is an alias for 'gdal'
if(Osmium_USE_OGR)
set(Osmium_USE_GDAL TRUE)
endif()
#----------------------------------------------------------------------
# Component 'pbf'
if(Osmium_USE_PBF)
find_package(ZLIB)
find_package(Threads)
list(APPEND OSMIUM_EXTRA_FIND_VARS ZLIB_FOUND Threads_FOUND)
if(ZLIB_FOUND AND Threads_FOUND)
list(APPEND OSMIUM_PBF_LIBRARIES
${ZLIB_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
)
if(WIN32)
list(APPEND OSMIUM_PBF_LIBRARIES ws2_32)
endif()
list(APPEND OSMIUM_INCLUDE_DIRS
${ZLIB_INCLUDE_DIR}
)
else()
message(WARNING "Osmium: Can not find some libraries for PBF input/output, please install them or configure the paths.")
endif()
endif()
#----------------------------------------------------------------------
# Component 'xml'
if(Osmium_USE_XML)
find_package(EXPAT)
find_package(BZip2)
find_package(ZLIB)
find_package(Threads)
list(APPEND OSMIUM_EXTRA_FIND_VARS EXPAT_FOUND BZIP2_FOUND ZLIB_FOUND Threads_FOUND)
if(EXPAT_FOUND AND BZIP2_FOUND AND ZLIB_FOUND AND Threads_FOUND)
list(APPEND OSMIUM_XML_LIBRARIES
${EXPAT_LIBRARIES}
${BZIP2_LIBRARIES}
${ZLIB_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
)
list(APPEND OSMIUM_INCLUDE_DIRS
${EXPAT_INCLUDE_DIR}
${BZIP2_INCLUDE_DIR}
${ZLIB_INCLUDE_DIR}
)
else()
message(WARNING "Osmium: Can not find some libraries for XML input/output, please install them or configure the paths.")
endif()
endif()
#----------------------------------------------------------------------
list(APPEND OSMIUM_IO_LIBRARIES
${OSMIUM_PBF_LIBRARIES}
${OSMIUM_XML_LIBRARIES}
)
list(APPEND OSMIUM_LIBRARIES
${OSMIUM_IO_LIBRARIES}
)
#----------------------------------------------------------------------
# Component 'geos'
if(Osmium_USE_GEOS)
find_path(GEOS_INCLUDE_DIR geos/geom.h)
find_library(GEOS_LIBRARY NAMES geos)
list(APPEND OSMIUM_EXTRA_FIND_VARS GEOS_INCLUDE_DIR GEOS_LIBRARY)
if(GEOS_INCLUDE_DIR AND GEOS_LIBRARY)
SET(GEOS_FOUND 1)
list(APPEND OSMIUM_LIBRARIES ${GEOS_LIBRARY})
list(APPEND OSMIUM_INCLUDE_DIRS ${GEOS_INCLUDE_DIR})
else()
message(WARNING "Osmium: GEOS library is required but not found, please install it or configure the paths.")
endif()
endif()
#----------------------------------------------------------------------
# Component 'gdal' (alias 'ogr')
if(Osmium_USE_GDAL)
find_package(GDAL)
list(APPEND OSMIUM_EXTRA_FIND_VARS GDAL_FOUND)
if(GDAL_FOUND)
list(APPEND OSMIUM_LIBRARIES ${GDAL_LIBRARIES})
list(APPEND OSMIUM_INCLUDE_DIRS ${GDAL_INCLUDE_DIRS})
else()
message(WARNING "Osmium: GDAL library is required but not found, please install it or configure the paths.")
endif()
endif()
#----------------------------------------------------------------------
# Component 'proj'
if(Osmium_USE_PROJ)
find_path(PROJ_INCLUDE_DIR proj_api.h)
find_library(PROJ_LIBRARY NAMES proj)
list(APPEND OSMIUM_EXTRA_FIND_VARS PROJ_INCLUDE_DIR PROJ_LIBRARY)
if(PROJ_INCLUDE_DIR AND PROJ_LIBRARY)
set(PROJ_FOUND 1)
list(APPEND OSMIUM_LIBRARIES ${PROJ_LIBRARY})
list(APPEND OSMIUM_INCLUDE_DIRS ${PROJ_INCLUDE_DIR})
else()
message(WARNING "Osmium: PROJ.4 library is required but not found, please install it or configure the paths.")
endif()
endif()
#----------------------------------------------------------------------
# Component 'sparsehash'
if(Osmium_USE_SPARSEHASH)
find_path(SPARSEHASH_INCLUDE_DIR google/sparsetable)
list(APPEND OSMIUM_EXTRA_FIND_VARS SPARSEHASH_INCLUDE_DIR)
if(SPARSEHASH_INCLUDE_DIR)
# Find size of sparsetable::size_type. This does not work on older
# CMake versions because they can do this check only in C, not in C++.
if (NOT CMAKE_VERSION VERSION_LESS 3.0)
include(CheckTypeSize)
set(CMAKE_REQUIRED_INCLUDES ${SPARSEHASH_INCLUDE_DIR})
set(CMAKE_EXTRA_INCLUDE_FILES "google/sparsetable")
check_type_size("google::sparsetable<int>::size_type" SPARSETABLE_SIZE_TYPE LANGUAGE CXX)
set(CMAKE_EXTRA_INCLUDE_FILES)
set(CMAKE_REQUIRED_INCLUDES)
else()
set(SPARSETABLE_SIZE_TYPE ${CMAKE_SIZEOF_VOID_P})
endif()
# Sparsetable::size_type must be at least 8 bytes (64bit), otherwise
# OSM object IDs will not fit.
if(SPARSETABLE_SIZE_TYPE GREATER 7)
set(SPARSEHASH_FOUND 1)
add_definitions(-DOSMIUM_WITH_SPARSEHASH=${SPARSEHASH_FOUND})
list(APPEND OSMIUM_INCLUDE_DIRS ${SPARSEHASH_INCLUDE_DIR})
else()
message(WARNING "Osmium: Disabled Google SparseHash library on 32bit system (size_type=${SPARSETABLE_SIZE_TYPE}).")
endif()
else()
message(WARNING "Osmium: Google SparseHash library is required but not found, please install it or configure the paths.")
endif()
endif()
#----------------------------------------------------------------------
list(REMOVE_DUPLICATES OSMIUM_INCLUDE_DIRS)
if(OSMIUM_XML_LIBRARIES)
list(REMOVE_DUPLICATES OSMIUM_XML_LIBRARIES)
endif()
if(OSMIUM_PBF_LIBRARIES)
list(REMOVE_DUPLICATES OSMIUM_PBF_LIBRARIES)
endif()
if(OSMIUM_IO_LIBRARIES)
list(REMOVE_DUPLICATES OSMIUM_IO_LIBRARIES)
endif()
if(OSMIUM_LIBRARIES)
list(REMOVE_DUPLICATES OSMIUM_LIBRARIES)
endif()
#----------------------------------------------------------------------
#
# Check that all required libraries are available
#
#----------------------------------------------------------------------
if (OSMIUM_EXTRA_FIND_VARS)
list(REMOVE_DUPLICATES OSMIUM_EXTRA_FIND_VARS)
endif()
# Handle the QUIETLY and REQUIRED arguments and set OSMIUM_FOUND to TRUE if
# all listed variables are TRUE.
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Osmium REQUIRED_VARS OSMIUM_INCLUDE_DIR ${OSMIUM_EXTRA_FIND_VARS})
unset(OSMIUM_EXTRA_FIND_VARS)
#----------------------------------------------------------------------
#
# Add compiler flags
#
#----------------------------------------------------------------------
add_definitions(-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64)
if(MSVC)
add_definitions(-wd4996)
# Disable warning C4068: "unknown pragma" because we want it to ignore
# pragmas for other compilers.
add_definitions(-wd4068)
# Disable warning C4715: "not all control paths return a value" because
# it generates too many false positives.
add_definitions(-wd4715)
# Disable warning C4351: new behavior: elements of array '...' will be
# default initialized. The new behaviour is correct and we don't support
# old compilers anyway.
add_definitions(-wd4351)
add_definitions(-DNOMINMAX -DWIN32_LEAN_AND_MEAN -D_CRT_SECURE_NO_WARNINGS)
endif()
if(APPLE)
# following only available from cmake 2.8.12:
# add_compile_options(-stdlib=libc++)
# so using this instead:
add_definitions(-stdlib=libc++)
set(LDFLAGS ${LDFLAGS} -stdlib=libc++)
endif()
#----------------------------------------------------------------------
# This is a set of recommended warning options that can be added when compiling
# libosmium code.
if(MSVC)
set(OSMIUM_WARNING_OPTIONS "/W3 /wd4514" CACHE STRING "Recommended warning options for libosmium")
else()
set(OSMIUM_WARNING_OPTIONS "-Wall -Wextra -pedantic -Wredundant-decls -Wdisabled-optimization -Wctor-dtor-privacy -Wnon-virtual-dtor -Woverloaded-virtual -Wsign-promo -Wold-style-cast" CACHE STRING "Recommended warning options for libosmium")
endif()
set(OSMIUM_DRACONIC_CLANG_OPTIONS "-Wdocumentation -Wunused-exception-parameter -Wmissing-declarations -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-unused-macros -Wno-exit-time-destructors -Wno-global-constructors -Wno-padded -Wno-switch-enum -Wno-missing-prototypes -Wno-weak-vtables -Wno-cast-align -Wno-float-equal")
if(Osmium_DEBUG)
message(STATUS "OSMIUM_XML_LIBRARIES=" ${OSMIUM_XML_LIBRARIES})
message(STATUS "OSMIUM_PBF_LIBRARIES=" ${OSMIUM_PBF_LIBRARIES})
message(STATUS "OSMIUM_IO_LIBRARIES=" ${OSMIUM_IO_LIBRARIES})
message(STATUS "OSMIUM_LIBRARIES=" ${OSMIUM_LIBRARIES})
message(STATUS "OSMIUM_INCLUDE_DIRS=" ${OSMIUM_INCLUDE_DIRS})
endif()

37
examples/cpp/default.nix Normal file
View File

@ -0,0 +1,37 @@
{
# import the sources
sources ? import ./nix/sources.nix
}:
let
pkgs = import sources.nixpkgs {
overlays = [ (import ./overlay.nix) ];
};
in
with pkgs;
stdenv.mkDerivation {
name = "nix-cpp-demo";
nativeBuildInputs = [ cmake pkgconfig];
src = ./.;
# tell Cmake location of all headers
cmakeFlags = [
"-DEXTERNAL_INCLUDE_DIRECTORIES=${stdenv.lib.strings.makeSearchPathOutput "dev" "include" libosmium.buildInputs}"
];
buildInputs = stdenv.lib.lists.concatLists[
# We want to check if dependencies exist using find_package
[
libosmium.buildInputs
]
# dependencies
[
libosmium
]
];
installPhase = ''
mkdir -p $out/bin
cp bin/hello-world $out/bin
'';
}

View File

@ -0,0 +1,38 @@
{
"libosmium": {
"branch": "master",
"description": "Fast and flexible C++ library for working with OpenStreetMap data.",
"homepage": "https://osmcode.org/libosmium/",
"owner": "osmcode",
"repo": "libosmium",
"rev": "a43ffb61cba34a07c240bcc591d88c9a863e9618",
"sha256": "0mlcvqrhp40bzj5r5j9nfc5vbis8hmzcq9xi8jylkciyydaynhz4",
"type": "tarball",
"url": "https://github.com/osmcode/libosmium/archive/a43ffb61cba34a07c240bcc591d88c9a863e9618.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"niv": {
"branch": "master",
"description": "Easy dependency management for Nix projects",
"homepage": "https://github.com/nmattia/niv",
"owner": "nmattia",
"repo": "niv",
"rev": "983e7a049aea1d2033e619368b9a5d6d394d51db",
"sha256": "1d3p53q8anl6x9f8wg1wbznq4vwrp2ns044w0gc03drp8nh0dfdm",
"type": "tarball",
"url": "https://github.com/nmattia/niv/archive/983e7a049aea1d2033e619368b9a5d6d394d51db.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"nixpkgs": {
"branch": "nixos-19.09",
"description": "A read-only mirror of NixOS/nixpkgs tracking the released channels. Send issues and PRs to",
"homepage": "https://github.com/NixOS/nixpkgs",
"owner": "NixOS",
"repo": "nixpkgs-channels",
"rev": "d85e435b7bded2596d7b201bcd938c94d8a921c1",
"sha256": "1msjm4kx1z73v444i1iybvmc7z0kfkbn9nzr21rn5yc4ql1jwf99",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs-channels/archive/d85e435b7bded2596d7b201bcd938c94d8a921c1.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
}
}

View File

@ -0,0 +1,136 @@
# This file has been generated by Niv.
let
#
# The fetchers. fetch_<type> fetches specs of type <type>.
#
fetch_file = pkgs: spec:
if spec.builtin or true then
builtins_fetchurl { inherit (spec) url sha256; }
else
pkgs.fetchurl { inherit (spec) url sha256; };
fetch_tarball = pkgs: spec:
if spec.builtin or true then
builtins_fetchTarball { inherit (spec) url sha256; }
else
pkgs.fetchzip { inherit (spec) url sha256; };
fetch_git = spec:
builtins.fetchGit { url = spec.repo; inherit (spec) rev ref; };
fetch_builtin-tarball = spec:
builtins.trace
''
WARNING:
The niv type "builtin-tarball" will soon be deprecated. You should
instead use `builtin = true`.
$ niv modify <package> -a type=tarball -a builtin=true
''
builtins_fetchTarball { inherit (spec) url sha256; };
fetch_builtin-url = spec:
builtins.trace
''
WARNING:
The niv type "builtin-url" will soon be deprecated. You should
instead use `builtin = true`.
$ niv modify <package> -a type=file -a builtin=true
''
(builtins_fetchurl { inherit (spec) url sha256; });
#
# Various helpers
#
# The set of packages used when specs are fetched using non-builtins.
mkPkgs = sources:
if hasNixpkgsPath
then
if hasThisAsNixpkgsPath
then import (builtins_fetchTarball { inherit (mkNixpkgs sources) url sha256; }) {}
else import <nixpkgs> {}
else
import (builtins_fetchTarball { inherit (mkNixpkgs sources) url sha256; }) {};
mkNixpkgs = sources:
if builtins.hasAttr "nixpkgs" sources
then sources.nixpkgs
else abort
''
Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
add a package called "nixpkgs" to your sources.json.
'';
hasNixpkgsPath = (builtins.tryEval <nixpkgs>).success;
hasThisAsNixpkgsPath =
(builtins.tryEval <nixpkgs>).success && <nixpkgs> == ./.;
# The actual fetching function.
fetch = pkgs: name: spec:
if ! builtins.hasAttr "type" spec then
abort "ERROR: niv spec ${name} does not have a 'type' attribute"
else if spec.type == "file" then fetch_file pkgs spec
else if spec.type == "tarball" then fetch_tarball pkgs spec
else if spec.type == "git" then fetch_git spec
else if spec.type == "builtin-tarball" then fetch_builtin-tarball spec
else if spec.type == "builtin-url" then fetch_builtin-url spec
else
abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";
# Ports of functions for older nix versions
# a Nix version of mapAttrs if the built-in doesn't exist
mapAttrs = builtins.mapAttrs or (
f: set: with builtins;
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))
);
# fetchTarball version that is compatible between all the versions of Nix
builtins_fetchTarball = { url, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchTarball;
in
if lessThan nixVersion "1.12" then
fetchTarball { inherit url; }
else
fetchTarball attrs;
# fetchurl version that is compatible between all the versions of Nix
builtins_fetchurl = { url, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchurl;
in
if lessThan nixVersion "1.12" then
fetchurl { inherit url; }
else
fetchurl attrs;
# Create the final "sources" from the config
mkSources = config:
mapAttrs (
name: spec:
if builtins.hasAttr "outPath" spec
then abort
"The values in sources.json should not have an 'outPath' attribute"
else
spec // { outPath = fetch config.pkgs name spec; }
) config.sources;
# The "config" used by the fetchers
mkConfig =
{ sourcesFile ? ./sources.json
}: rec {
# The sources, i.e. the attribute set of spec name to spec
sources = builtins.fromJSON (builtins.readFile sourcesFile);
# The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
pkgs = mkPkgs sources;
};
in
mkSources (mkConfig {}) //
{ __functor = _: settings: mkSources (mkConfig settings); }

3
examples/cpp/overlay.nix Normal file
View File

@ -0,0 +1,3 @@
self: super: {
libosmium = self.callPackage ./overlay/libosmium.nix {};
}

View File

@ -0,0 +1,22 @@
{ stdenv, fetchFromGitHub, cmake, protozero, expat, zlib, bzip2, boost, sources ? import ../nix/sources.nix }:
stdenv.mkDerivation rec {
name = "libosmium";
src = sources.libosmium;
nativeBuildInputs = [ cmake ];
cmakeFlags = [
"-DBUILD_BENCHMARKS=OFF"
"-DBUILD_DATA_TESTS=OFF"
"-DBUILD_EXAMPLES=OFF"
"-DBUILD_HEADERS=OFF"
"-DBUILD_TESTING=OFF"
];
buildInputs = [
protozero
expat
zlib
bzip2
boost
];
}

View File

@ -0,0 +1,16 @@
# ---[ Include sources
set(SOURCES_CPP
main.cpp
)
# ---[ Fetch all include headers
file(GLOB_RECURSE INCLUDE_HEADERS include/*.h)
# ---[ Add the executable
add_executable(${PROJECT_NAME} ${SOURCES_CPP} ${INCLUDE_HEADERS})
#link executable to dependency libraries
target_link_libraries (
${PROJECT_NAME}
${OSMIUM_LIBRARIES}
)

View File

@ -0,0 +1,9 @@
#include <iostream>
#include <osmium/io/pbf_input.hpp>
using namespace std;
int main()
{
cout << "Hello, World!\n";
return 0;
}