-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
187 lines (163 loc) · 6.93 KB
/
Copy pathCMakeLists.txt
File metadata and controls
187 lines (163 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
cmake_minimum_required(VERSION 3.20)
project(CppJIT LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
include(ExternalProject)
include(GNUInstallDirs)
# This option won't make a lot of sense since we only ship the shared library in site-packages
# Perhaps this should permanently be OFF and users can build their own CppInterOp if they want to run the tests?
option(CPPJIT_ENABLE_CPPINTEROP_TESTS "enable CppInterOp tests" OFF)
set(CPPINTEROP_GIT_REPOSITORY "https://github.com/compiler-research/CppInterOp.git" CACHE STRING "")
set(CPPINTEROP_GIT_TAG "8d624c621a4b95e36ff73ac708c85a768287478f" CACHE STRING "")
set(CPPINTEROP_SOURCE_DIR "" CACHE PATH
"Override default CppInterOp built by ExternalProject_Add, with a path to local CppInterOp source")
# Development.Module, not Development: the full component additionally
# requires libpython (Development.Embed), which manylinux images do not
# ship — extension modules only need headers and module-link rules.
set(Python_FIND_VIRTUALENV ONLY)
find_package(Python COMPONENTS Interpreter Development.Module)
if(NOT Python_FOUND)
set(Python_FIND_VIRTUALENV STANDARD)
find_package(Python COMPONENTS Interpreter Development.Module)
endif()
if(NOT Python_Development.Module_FOUND)
message(FATAL_ERROR "Python development headers not found")
endif()
# The LLVM range the pinned CppInterOp supports; update together with the tag.
set(CPPJIT_LLVM_VERSION_MIN 20)
set(CPPJIT_LLVM_VERSION_MAX 22)
set(_llvm_hints "")
if(DEFINED ENV{CONDA_PREFIX})
list(APPEND _llvm_hints "$ENV{CONDA_PREFIX}/lib/cmake/llvm")
endif()
if(DEFINED LLVM_DIR)
# An explicit LLVM_DIR is authoritative: fail instead of falling back to a
# different LLVM than the one requested. A failed find_package resets
# LLVM_DIR to -NOTFOUND, so keep the requested value for the message.
set(_llvm_dir_arg "${LLVM_DIR}")
find_package(LLVM CONFIG PATHS "${LLVM_DIR}" NO_DEFAULT_PATH)
if(NOT LLVM_FOUND)
message(FATAL_ERROR
"No LLVMConfig.cmake under LLVM_DIR (${_llvm_dir_arg}); expected "
"<install prefix or build tree>/lib/cmake/llvm")
endif()
else()
find_package(LLVM CONFIG QUIET HINTS ${_llvm_hints})
if(NOT LLVM_FOUND)
message(FATAL_ERROR
"No LLVM CMake package found. Install LLVM "
"${CPPJIT_LLVM_VERSION_MIN}-${CPPJIT_LLVM_VERSION_MAX} development packages "
"(apt: llvm-${CPPJIT_LLVM_VERSION_MAX}-dev libclang-${CPPJIT_LLVM_VERSION_MAX}-dev; "
"conda: llvmdev clangdev), or point cppjit at your own LLVM build with "
"-DLLVM_DIR=<prefix or build tree>/lib/cmake/llvm "
"(pip: --config-settings=cmake.define.LLVM_DIR=...)")
endif()
endif()
message(STATUS "Found LLVM ${LLVM_VERSION} at ${LLVM_DIR}")
if(LLVM_VERSION_MAJOR LESS CPPJIT_LLVM_VERSION_MIN OR
LLVM_VERSION_MAJOR GREATER CPPJIT_LLVM_VERSION_MAX)
message(FATAL_ERROR
"LLVM ${LLVM_VERSION} is unsupported: the currently supported "
"CppInterOp version (${CPPINTEROP_GIT_TAG}) only supports LLVM "
"${CPPJIT_LLVM_VERSION_MIN}-${CPPJIT_LLVM_VERSION_MAX}")
endif()
if(DEFINED Clang_DIR)
# An explicit Clang_DIR is authoritative, like LLVM_DIR above.
set(_clang_dir_arg "${Clang_DIR}")
find_package(Clang CONFIG PATHS "${Clang_DIR}" NO_DEFAULT_PATH)
if(NOT Clang_FOUND)
message(FATAL_ERROR
"No ClangConfig.cmake under Clang_DIR (${_clang_dir_arg}); expected "
"<install prefix or build tree>/lib/cmake/clang")
endif()
else()
# Clang's package sits beside LLVM's in every supported layout; search
# only there so an unrelated system clang cannot satisfy the lookup.
find_package(Clang CONFIG QUIET HINTS "${LLVM_DIR}/../clang" NO_DEFAULT_PATH)
endif()
if(Clang_FOUND)
message(STATUS "Found Clang at ${Clang_DIR}")
endif()
# CppInterOp is installed at the location cppjit ships at runtime: ask for the
# site-packages path; fall back to CMAKE_INSTALL_PREFIX for standalone builds.
execute_process(
COMMAND ${Python_EXECUTABLE} -c "import sysconfig; print(sysconfig.get_path('platlib'))"
OUTPUT_VARIABLE _python_platlib
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(_python_platlib)
set(CPPINTEROP_INSTALL_PREFIX "${_python_platlib}")
else()
set(CPPINTEROP_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
endif()
set(CPPINTEROP_INSTALL_DIR "${CPPINTEROP_INSTALL_PREFIX}/cppjit_backend")
# Include cmake for CppInterOp config and build using ExternalProject.
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/AddCppInterOp.cmake)
cppjit_add_cppinterop()
# this libcppjit.so merges both cpyrt and the interop wrapper
file(GLOB CPYRT_SOURCES CONFIGURE_DEPENDS src/cpyrt/*.cxx)
set(INTEROP_SOURCES
src/interop/interop_wrapper.cxx
src/interop/cppinterop_dispatch.cxx
)
add_library(cppjit SHARED ${CPYRT_SOURCES} ${INTEROP_SOURCES})
add_dependencies(cppjit CppInterOp)
# The wrapper anchors these relative spellings at its own load location,
# falling back to the install prefix (see cppinterop_paths()); the clang
# major names the versioned compiler probed for the runtime resource dir.
target_compile_definitions(cppjit PRIVATE
CPPINTEROP_INSTALL_PREFIX="${CPPINTEROP_INSTALL_PREFIX}"
CPPINTEROP_LIBRARY="cppjit_backend/lib/libclangCppInterOp${CMAKE_SHARED_LIBRARY_SUFFIX}"
CPPINTEROP_INCLUDE_DIR="cppjit_backend/include"
CPPJIT_CLANG_MAJOR="${LLVM_VERSION_MAJOR}"
)
target_include_directories(cppjit PRIVATE
# src/ itself resolves the public "cpyrt/*.h" spellings against the
# flattened src/cpyrt/ directory
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/src/cpyrt
${CMAKE_CURRENT_SOURCE_DIR}/src/interop
${CPPINTEROP_INSTALL_DIR}/include
${Python_INCLUDE_DIRS}
)
target_compile_options(cppjit PRIVATE
-Wall -Wno-strict-aliasing -Wno-register
)
if(CMAKE_COMPILER_IS_GNUCXX)
target_link_options(cppjit PRIVATE -Wl,-Bsymbolic-functions)
endif()
# macOS needs to allow undefined Python symbols
if(APPLE)
set_target_properties(cppjit PROPERTIES
LINK_FLAGS "-undefined dynamic_lookup"
SUFFIX ".so"
)
endif()
set_target_properties(cppjit PROPERTIES
OUTPUT_NAME "cppjit"
PREFIX "lib"
)
# libcppjit.so is installed at the site-packages root (import libcppjit)
install(TARGETS cppjit
LIBRARY DESTINATION .
)
# install CppInterOp libraries and headers
install(CODE "
file(GLOB _interop_libs \"${CPPINTEROP_INSTALL_DIR}/lib/libclangCppInterOp*\")
foreach(_lib \${_interop_libs})
file(INSTALL \${_lib} DESTINATION \${CMAKE_INSTALL_PREFIX}/cppjit_backend/lib)
endforeach()
")
install(CODE "
file(INSTALL \"${CPPINTEROP_INSTALL_DIR}/include/\" DESTINATION \${CMAKE_INSTALL_PREFIX}/cppjit_backend/include)
")
# the public cpyrt API headers keep their installed cpyrt/ prefix
install(FILES
src/cpyrt/API.h
src/cpyrt/CommonDefs.h
src/cpyrt/DispatchPtr.h
src/cpyrt/PyException.h
src/cpyrt/Reflex.h
DESTINATION cppjit_backend/include/cpyrt
)