Skip to content

Commit 6c5f366

Browse files
committed
cmake build
1 parent f1ef4c5 commit 6c5f366

File tree

5 files changed

+295
-0
lines changed

5 files changed

+295
-0
lines changed

CMakeLists.txt

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
############################################################################
2+
# Copyright (c) 2016, Johan Mabille and Sylvain Corlay #
3+
# #
4+
# Distributed under the terms of the BSD 3-Clause License. #
5+
# #
6+
# The full license is in the file LICENSE, distributed with this software. #
7+
############################################################################
8+
9+
cmake_minimum_required(VERSION 3.1)
10+
project(xtensor-python)
11+
12+
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
13+
set(XTENSOR_PYTHON_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
14+
15+
# Versionning
16+
# ===========
17+
18+
set(XTENSOR_PYTHON_CONFIG_FILE
19+
"${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/xtensor_python_config.hpp")
20+
file(STRINGS ${XTENSOR_PYTHON_CONFIG_FILE} xtensor_python_version_defines
21+
REGEX "#define XTENSOR_PYTHON_VERSION_(MAJOR|MINOR|PATCH)")
22+
foreach(ver ${xtensor_python_version_defines})
23+
if(ver MATCHES "#define XTENSOR_PYTHON_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$")
24+
set(XTENSOR_PYTHON_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "")
25+
endif()
26+
endforeach()
27+
set(${PROJECT_NAME}_VERSION
28+
${XTENSOR_PYTHON_VERSION_MAJOR}.${XTENSOR_PYTHON_VERSION_MINOR}.${XTENSOR_PYTHON_VERSION_PATCH})
29+
message(STATUS "xtensor-python v${${PROJECT_NAME}_VERSION}")
30+
31+
# Build
32+
# =====
33+
34+
OPTION(BUILD_TESTS "xtensor test suite" ON)
35+
36+
set(XTENSOR_PYTHON_HEADERS
37+
${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pyarray.hpp
38+
${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pybuffer_adaptor.hpp
39+
${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pycontainer.hpp
40+
${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pytensor.hpp
41+
${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pyvectorize.hpp
42+
${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/xtensor_python_config.hpp
43+
)
44+
45+
if(BUILD_TESTS)
46+
47+
include_directories(${XTENSOR_PYTHON_INCLUDE_DIR})
48+
find_package(xtensor REQUIRED)
49+
include_directories(${xtensor_INCLUDE_DIR})
50+
find_package(NumPy REQUIRED)
51+
include_directories(${NUMPY_INCLUDE_DIRS})
52+
53+
#TODO replace this with a find_package(pybind11 REQUIRED)
54+
# in parent CMakeLists when pybind11 CMakeLists has been fixed.
55+
find_package(PythonLibs REQUIRED)
56+
include_directories(${PYTHON_INCLUDE_DIRS})
57+
58+
if(MSVC)
59+
set(PYTHON_MODULE_EXTENSION ".pyd")
60+
else()
61+
set(PYTHON_MODULE_EXTENSION ".so")
62+
endif()
63+
64+
#add_subdirectory(test)
65+
add_subdirectory(benchmark)
66+
endif()
67+
68+
# Installation
69+
# ============
70+
71+
include(GNUInstallDirs)
72+
include(CMakePackageConfigHelpers)
73+
74+
install(FILES ${XTENSOR_PYTHON_HEADERS}
75+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/xtensor-python)
76+
77+
# GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share".
78+
set(XTENSOR_PYTHON_CMAKECONFIG_INSTALL_DIR "share/cmake/${PROJECT_NAME}" CACHE STRING "install path for xtensor-pythonConfig.cmake")
79+
80+
configure_package_config_file(${PROJECT_NAME}Config.cmake.in
81+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
82+
INSTALL_DESTINATION ${XTENSOR_PYTHON_CMAKECONFIG_INSTALL_DIR})
83+
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
84+
VERSION ${${PROJECT_NAME}_VERSION}
85+
COMPATIBILITY AnyNewerVersion)
86+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
87+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
88+
DESTINATION ${XTENSOR_PYTHON_CMAKECONFIG_INSTALL_DIR})
89+

benchmark/CMakeLists.txt

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
############################################################################
2+
# Copyright (c) 2016, Johan Mabille and Sylvain Corlay #
3+
# #
4+
# Distributed under the terms of the BSD 3-Clause License. #
5+
# #
6+
# The full license is in the file LICENSE, distributed with this software. #
7+
############################################################################
8+
9+
message(STATUS "Forcing tests build type to Release")
10+
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
11+
12+
include(CheckCXXCompilerFlag)
13+
14+
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
15+
16+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
17+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -Wunused-parameter -Wextra -Wreorder -Wconversion")
18+
CHECK_CXX_COMPILER_FLAG("-std=c++14" HAS_CPP14_FLAG)
19+
20+
if (HAS_CPP14_FLAG)
21+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
22+
else()
23+
message(FATAL_ERROR "Unsupported compiler -- xtensor requires C++14 support!")
24+
endif()
25+
26+
# Enable link time optimization and set the default symbol
27+
# visibility to hidden (very important to obtain small binaries)
28+
if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
29+
# Default symbol visibility
30+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
31+
32+
# Check for Link Time Optimization support
33+
# (GCC/Clang)
34+
CHECK_CXX_COMPILER_FLAG("-flto" HAS_LTO_FLAG)
35+
if (HAS_LTO_FLAG)
36+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
37+
endif()
38+
39+
# Intel equivalent to LTO is called IPO
40+
if (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
41+
CHECK_CXX_COMPILER_FLAG("-ipo" HAS_IPO_FLAG)
42+
if (HAS_IPO_FLAG)
43+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ipo")
44+
endif()
45+
endif()
46+
endif()
47+
endif()
48+
49+
if(MSVC)
50+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /MP /bigobj")
51+
set(CMAKE_EXE_LINKER_FLAGS /MANIFEST:NO)
52+
foreach(flag_var
53+
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
54+
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
55+
string(REPLACE "/MD" "-MT" ${flag_var} "${${flag_var}}")
56+
endforeach()
57+
endif()
58+
59+
set(XTENSOR_PYTHON_BENCHMARK
60+
main.cpp
61+
)
62+
63+
set(XTENSOR_PYTHON_BENCHMARK_TARGET benchmark_xtensor_python)
64+
add_library(${XTENSOR_PYTHON_BENCHMARK_TARGET} EXCLUDE_FROM_ALL
65+
${XTENSOR_PYTHON_BENCHMARK} ${XTENSOR_PYTHON_HEADERS})
66+
67+
set_target_properties(${XTENSOR_PYTHON_BENCHMARK_TARGET} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
68+
69+
if(APPLE)
70+
target_link_libraries(${XTENSOR_PYTHON_BENCHMARK_TARGET} PRIVATE "-undefined dynamic_lookup")
71+
else()
72+
target_link_libraries(${XTENSOR_PYTHON_BENCHMARK_TARGET} "-shared")
73+
endif()
74+
75+
configure_file(benchmark_pyarray.py benchmark_pyarray.py COPYONLY)
76+
configure_file(benchmark_pytensor.py benchmark_pytensor.py COPYONLY)
77+
configure_file(benchmark_pybind_array.py benchmark_pybind_array.py COPYONLY)
78+
79+
add_custom_target(xbenchmark DEPENDS ${XTENSOR_PYTHON_BENCHMARK_TARGET})
80+

cmake/FindNumPy.cmake

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# - Find the NumPy libraries
2+
# This module finds if NumPy is installed, and sets the following variables
3+
# indicating where it is.
4+
#
5+
# TODO: Update to provide the libraries and paths for linking npymath lib.
6+
#
7+
# NUMPY_FOUND - was NumPy found
8+
# NUMPY_VERSION - the version of NumPy found as a string
9+
# NUMPY_VERSION_MAJOR - the major version number of NumPy
10+
# NUMPY_VERSION_MINOR - the minor version number of NumPy
11+
# NUMPY_VERSION_PATCH - the patch version number of NumPy
12+
# NUMPY_VERSION_DECIMAL - e.g. version 1.6.1 is 10601
13+
# NUMPY_INCLUDE_DIRS - path to the NumPy include files
14+
15+
#============================================================================
16+
# Copyright 2012 Continuum Analytics, Inc.
17+
#
18+
# MIT License
19+
#
20+
# Permission is hereby granted, free of charge, to any person obtaining
21+
# a copy of this software and associated documentation files
22+
# (the "Software"), to deal in the Software without restriction, including
23+
# without limitation the rights to use, copy, modify, merge, publish,
24+
# distribute, sublicense, and/or sell copies of the Software, and to permit
25+
# persons to whom the Software is furnished to do so, subject to
26+
# the following conditions:
27+
#
28+
# The above copyright notice and this permission notice shall be included
29+
# in all copies or substantial portions of the Software.
30+
#
31+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
32+
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
34+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
35+
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
36+
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
37+
# OTHER DEALINGS IN THE SOFTWARE.
38+
#
39+
#============================================================================
40+
41+
# Finding NumPy involves calling the Python interpreter
42+
if(NumPy_FIND_REQUIRED)
43+
find_package(PythonInterp REQUIRED)
44+
else()
45+
find_package(PythonInterp)
46+
endif()
47+
48+
if(NOT PYTHONINTERP_FOUND)
49+
set(NUMPY_FOUND FALSE)
50+
endif()
51+
52+
execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
53+
"import numpy as n; print(n.__version__); print(n.get_include());"
54+
RESULT_VARIABLE _NUMPY_SEARCH_SUCCESS
55+
OUTPUT_VARIABLE _NUMPY_VALUES
56+
ERROR_VARIABLE _NUMPY_ERROR_VALUE
57+
OUTPUT_STRIP_TRAILING_WHITESPACE)
58+
59+
if(NOT _NUMPY_SEARCH_SUCCESS MATCHES 0)
60+
if(NumPy_FIND_REQUIRED)
61+
message(FATAL_ERROR
62+
"NumPy import failure:\n${_NUMPY_ERROR_VALUE}")
63+
endif()
64+
set(NUMPY_FOUND FALSE)
65+
endif()
66+
67+
# Convert the process output into a list
68+
string(REGEX REPLACE ";" "\\\\;" _NUMPY_VALUES ${_NUMPY_VALUES})
69+
string(REGEX REPLACE "\n" ";" _NUMPY_VALUES ${_NUMPY_VALUES})
70+
list(GET _NUMPY_VALUES 0 NUMPY_VERSION)
71+
list(GET _NUMPY_VALUES 1 NUMPY_INCLUDE_DIRS)
72+
73+
# Make sure all directory separators are '/'
74+
string(REGEX REPLACE "\\\\" "/" NUMPY_INCLUDE_DIRS ${NUMPY_INCLUDE_DIRS})
75+
76+
# Get the major and minor version numbers
77+
string(REGEX REPLACE "\\." ";" _NUMPY_VERSION_LIST ${NUMPY_VERSION})
78+
list(GET _NUMPY_VERSION_LIST 0 NUMPY_VERSION_MAJOR)
79+
list(GET _NUMPY_VERSION_LIST 1 NUMPY_VERSION_MINOR)
80+
list(GET _NUMPY_VERSION_LIST 2 NUMPY_VERSION_PATCH)
81+
string(REGEX MATCH "[0-9]*" NUMPY_VERSION_PATCH ${NUMPY_VERSION_PATCH})
82+
math(EXPR NUMPY_VERSION_DECIMAL
83+
"(${NUMPY_VERSION_MAJOR} * 10000) + (${NUMPY_VERSION_MINOR} * 100) + ${NUMPY_VERSION_PATCH}")
84+
85+
find_package_message(NUMPY
86+
"Found NumPy: version \"${NUMPY_VERSION}\" ${NUMPY_INCLUDE_DIRS}"
87+
"${NUMPY_INCLUDE_DIRS}${NUMPY_VERSION}")
88+
89+
set(NUMPY_FOUND TRUE)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/***************************************************************************
2+
* Copyright (c) 2016, Johan Mabille and Sylvain Corlay *
3+
* *
4+
* Distributed under the terms of the BSD 3-Clause License. *
5+
* *
6+
* The full license is in the file LICENSE, distributed with this software. *
7+
****************************************************************************/
8+
9+
#ifndef XTENSOR_PYTHON_CONFIG_HPP
10+
#define XTENSOR_PYTHON_CONFIG_HPP
11+
12+
#define XTENSOR_PYTHON_VERSION_MAJOR 0
13+
#define XTENSOR_PYTHON_VERSION_MINOR 5
14+
#define XTENSOR_PYTHON_VERSION_PATCH 0
15+
16+
#endif

xtensor-pythonConfig.cmake.in

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
############################################################################
2+
# Copyright (c) 2016, Johan Mabille and Sylvain Corlay #
3+
# #
4+
# Distributed under the terms of the BSD 3-Clause License. #
5+
# #
6+
# The full license is in the file LICENSE, distributed with this software. #
7+
############################################################################
8+
9+
# xtensor cmake module
10+
# This module sets the following variables in your project::
11+
#
12+
# xtensor_FOUND - true if xtensor found on the system
13+
# xtensor_INCLUDE_DIR - the directory containing xtensor headers
14+
# xtensor_LIBRARY - empty
15+
16+
@PACKAGE_INIT@
17+
18+
set(PN xtensor-python)
19+
set_and_check(${PN}_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/@CMAKE_INSTALL_INCLUDEDIR@")
20+
set(${PN}_LIBRARY "")
21+
check_required_components(${PN})

0 commit comments

Comments
 (0)