"superbench/benchmarks/model_benchmarks/pytorch_llama.py" did not exist on "0972b223a1a0e0684ead3c1ecc202273e9442494"
CMakeLists.txt 4.96 KB
Newer Older
Wenzel Jakob's avatar
Wenzel Jakob committed
1
2
3
4
5
6
7
8
9
10
11
# CMakeLists.txt -- Build system for the pybind11 examples
#
# Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
#
# All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.

cmake_minimum_required(VERSION 2.8)

project(pybind)

12
13
14
15
# Add a CMake parameter for choosing a desired Python version
set(PYBIND_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling the example application")

# Set a default build configuration if none is specified. 'MinSizeRel' produces the smallest binaries
Wenzel Jakob's avatar
Wenzel Jakob committed
16
17
18
19
20
21
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  message(STATUS "Setting build type to 'MinSizeRel' as none was specified.")
  set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE)
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
    "MinSizeRel" "RelWithDebInfo")
endif()
22
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
23

Wenzel Jakob's avatar
Wenzel Jakob committed
24
set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6)
25
26
find_package(PythonLibs ${PYBIND_PYTHON_VERSION} REQUIRED)
find_package(PythonInterp ${PYBIND_PYTHON_VERSION} REQUIRED)
Wenzel Jakob's avatar
Wenzel Jakob committed
27
28

if (UNIX)
29
  # Enable C++11 mode
30
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
31
32
33

  # Enable link time optimization and set the default symbol
  # visibility to hidden (very important to obtain small binaries)
Wenzel Jakob's avatar
Wenzel Jakob committed
34
  if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
35
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -flto")
Wenzel Jakob's avatar
Wenzel Jakob committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  endif()
endif()

# Compile with compiler warnings turned on
if(MSVC)
  if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
    string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  else()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
  endif()
else()
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
endif()

50
51
# Include path for Python header files
include_directories(${PYTHON_INCLUDE_DIR})
Wenzel Jakob's avatar
Wenzel Jakob committed
52

53
54
55
56
# Include path for pybind11 header files
include_directories(include)

# Create the binding library
Wenzel Jakob's avatar
Wenzel Jakob committed
57
58
59
60
61
62
63
add_library(example SHARED
  include/pybind/cast.h
  include/pybind/common.h
  include/pybind/operators.h
  include/pybind/pybind.h
  include/pybind/pytypes.h
  include/pybind/typeid.h
Wenzel Jakob's avatar
Wenzel Jakob committed
64
  include/pybind/numpy.h
Wenzel Jakob's avatar
Wenzel Jakob committed
65
66
67
68
69
70
71
72
73
74
  example/example.cpp
  example/example1.cpp
  example/example2.cpp
  example/example3.cpp
  example/example4.cpp
  example/example5.cpp
  example/example6.cpp
  example/example7.cpp
  example/example8.cpp
  example/example9.cpp
Wenzel Jakob's avatar
Wenzel Jakob committed
75
  example/example10.cpp
76
  example/example11.cpp
77
  example/example12.cpp
Wenzel Jakob's avatar
Wenzel Jakob committed
78
79
)

80
# Don't add a 'lib' prefix to the shared library
Wenzel Jakob's avatar
Wenzel Jakob committed
81
set_target_properties(example PROPERTIES PREFIX "")
82
83

# Write the output file directly into the 'example' directory
Wenzel Jakob's avatar
Wenzel Jakob committed
84
85
86
87
set_target_properties(example PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/example)

if (WIN32)
  if (MSVC)
88
89
90
91
    # Enforce size-based optimization and link time code generation
    # on MSVC (~30% smaller binaries in experiments). /bigobj is needed
    # for bigger binding projects due to the limit to 64k addressable sections
    # /MP enables multithreaded builds (relevant when there are many files).
92
    set_target_properties(example PROPERTIES COMPILE_FLAGS "/Os /GL /MP /bigobj")
Wenzel Jakob's avatar
Wenzel Jakob committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    set_target_properties(example PROPERTIES LINK_FLAGS "/LTCG")
  endif()

  # .PYD file extension on Windows
  set_target_properties(example PROPERTIES SUFFIX ".pyd")

  # Link against the Python shared library
  target_link_libraries(example ${PYTHON_LIBRARY})
elseif (UNIX)
  # It's quite common to have multiple copies of the same Python version
  # installed on one's system. E.g.: one copy from the OS and another copy
  # that's statically linked into an application like Blender or Maya.
  # If we link our plugin library against the OS Python here and import it
  # into Blender or Maya later on, this will cause segfaults when multiple
  # conflicting Python instances are active at the same time.

109
110
111
112
113
  # Windows is not affected by this issue since it handles DLL imports 
  # differently. The solution for Linux and Mac OS is simple: we just don't
  # link against the Python library. The resulting shared library will have
  # missing symbols, but that's perfectly fine -- they will be resolved at
  # import time.
Wenzel Jakob's avatar
Wenzel Jakob committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

  # .SO file extension on Linux/Mac OS
  set_target_properties(example PROPERTIES SUFFIX ".so")

  # Strip unnecessary sections of the binary on Linux/Mac OS
  if(APPLE)
    set_target_properties(example PROPERTIES MACOSX_RPATH ".")
    set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip")
    if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
      add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_SOURCE_DIR}/example/example.so)
    endif()
  else()
    if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
      add_custom_command(TARGET example POST_BUILD COMMAND strip ${PROJECT_SOURCE_DIR}/example/example.so)
    endif()
  endif()
endif()

enable_testing()
set(RUN_TEST ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/example/run_test.py)
Wenzel Jakob's avatar
Wenzel Jakob committed
134
foreach(i RANGE 1 12)
Wenzel Jakob's avatar
Wenzel Jakob committed
135
136
  add_test(NAME example${i} COMMAND ${RUN_TEST} example${i})
endforeach()