|
| 1 | +# |
| 2 | +# Copyright (c) 2023, Oracle and/or its affiliates. |
| 3 | +# |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without modification, are |
| 7 | +# permitted provided that the following conditions are met: |
| 8 | +# |
| 9 | +# 1. Redistributions of source code must retain the above copyright notice, this list of |
| 10 | +# conditions and the following disclaimer. |
| 11 | +# |
| 12 | +# 2. Redistributions in binary form must reproduce the above copyright notice, this list of |
| 13 | +# conditions and the following disclaimer in the documentation and/or other materials provided |
| 14 | +# with the distribution. |
| 15 | +# 3. Neither the name of the copyright holder nor the names of its contributors may be used to |
| 16 | +# endorse or promote products derived from this software without specific prior written |
| 17 | +# permission. |
| 18 | +# |
| 19 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
| 20 | +# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | +# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 22 | +# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 24 | +# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 25 | +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 | +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 27 | +# OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | +# |
| 29 | +cmake_minimum_required(VERSION 3.22) |
| 30 | +project(com.oracle.graal.python.cext) |
| 31 | + |
| 32 | +function(require_var var) |
| 33 | + if (NOT DEFINED ${var}) |
| 34 | + message(FATAL_ERROR "${var} needs to be set") |
| 35 | + endif() |
| 36 | +endfunction() |
| 37 | + |
| 38 | +if(MSVC) |
| 39 | + message(FATAL_ERROR "C API cannot be built with MSVC") |
| 40 | +endif() |
| 41 | + |
| 42 | +if(WIN32) |
| 43 | + require_var(GRAALVM_LLVM_LIB_DIR) |
| 44 | +endif() |
| 45 | +require_var(TRUFFLE_H_INC) |
| 46 | +require_var(LLVM_MODE) |
| 47 | +require_var(GRAALPY_EXT) |
| 48 | + |
| 49 | +if(NOT DEFINED SRC_DIR) |
| 50 | + set(SRC_DIR "${CMAKE_SOURCE_DIR}") |
| 51 | +endif() |
| 52 | + |
| 53 | +set(TARGET_LIBPYTHON "python-${LLVM_MODE}") |
| 54 | + |
| 55 | +###################################################################### |
| 56 | +# common variables and compile/link options (for all build targets) |
| 57 | +###################################################################### |
| 58 | + |
| 59 | +set(CFLAGS_WARNINGS -Wno-int-to-pointer-cast -Wno-int-conversion -Wno-void-pointer-to-int-cast |
| 60 | + -Wno-incompatible-pointer-types-discards-qualifiers -Wno-pointer-type-mismatch |
| 61 | + -Wno-braced-scalar-init -Wno-deprecated-declarations) |
| 62 | + |
| 63 | +# preprocessor defines for all platforms |
| 64 | +add_compile_definitions( |
| 65 | + NDEBUG |
| 66 | + GRAALVM_PYTHON_LLVM |
| 67 | +) |
| 68 | + |
| 69 | +if(WIN32) |
| 70 | + add_compile_definitions( |
| 71 | + MS_WINDOWS |
| 72 | + Py_ENABLE_SHARED |
| 73 | + HAVE_DECLSPEC_DLL |
| 74 | + ) |
| 75 | +endif() |
| 76 | + |
| 77 | +if(APPLE) |
| 78 | + add_link_options(-undefined dynamic_lookup) |
| 79 | +endif() |
| 80 | + |
| 81 | +# don't install into the system but into the MX project's output dir |
| 82 | +set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}) |
| 83 | + |
| 84 | +set(CAPI_SRC "${SRC_DIR}/src") |
| 85 | + |
| 86 | +# using glob patterns is not recommended: https://cmake.org/cmake/help/latest/command/file.html#glob |
| 87 | +set(SRC_FILES ${CAPI_SRC}/codecs.c ${CAPI_SRC}/setobject.c ${CAPI_SRC}/compile.c ${CAPI_SRC}/thread.c |
| 88 | + ${CAPI_SRC}/moduleobject.c ${CAPI_SRC}/preconfig.c ${CAPI_SRC}/getbuildinfo.c ${CAPI_SRC}/object.c |
| 89 | + ${CAPI_SRC}/dtoa.c ${CAPI_SRC}/pystrhex.c ${CAPI_SRC}/capi.c ${CAPI_SRC}/complexobject.c |
| 90 | + ${CAPI_SRC}/capsule.c ${CAPI_SRC}/typeobject.c ${CAPI_SRC}/obmalloc.c ${CAPI_SRC}/descrobject.c |
| 91 | + ${CAPI_SRC}/memoryobject.c ${CAPI_SRC}/traceback.c ${CAPI_SRC}/unicodeobject.c ${CAPI_SRC}/pythonrun.c |
| 92 | + ${CAPI_SRC}/funcobject.c ${CAPI_SRC}/codeobject.c ${CAPI_SRC}/unicodectype.c ${CAPI_SRC}/structseq.c |
| 93 | + ${CAPI_SRC}/import.c ${CAPI_SRC}/pytime.c ${CAPI_SRC}/bytearrayobject.c ${CAPI_SRC}/listobject.c |
| 94 | + ${CAPI_SRC}/bytesobject.c ${CAPI_SRC}/object_shared.c ${CAPI_SRC}/longobject.c ${CAPI_SRC}/sysmodule.c |
| 95 | + ${CAPI_SRC}/pystrtod.c ${CAPI_SRC}/tupleobject.c ${CAPI_SRC}/iterobject.c ${CAPI_SRC}/sliceobject.c |
| 96 | + ${CAPI_SRC}/classobject.c ${CAPI_SRC}/floatobject.c ${CAPI_SRC}/namespaceobject.c ${CAPI_SRC}/_warnings.c |
| 97 | + ${CAPI_SRC}/dictobject.c ${CAPI_SRC}/pystate.c ${CAPI_SRC}/mysnprintf.c ${CAPI_SRC}/ceval.c |
| 98 | + ${CAPI_SRC}/getcompiler.c ${CAPI_SRC}/pyhash.c ${CAPI_SRC}/fileutils.c ${CAPI_SRC}/descrobject_shared.c |
| 99 | + ${CAPI_SRC}/modsupport.c ${CAPI_SRC}/context.c ${CAPI_SRC}/abstract.c ${CAPI_SRC}/frameobject.c |
| 100 | + ${CAPI_SRC}/posixmodule.c ${CAPI_SRC}/longobject_shared.c ${CAPI_SRC}/exceptions.c ${CAPI_SRC}/pyctype.c |
| 101 | + ${CAPI_SRC}/typeobject_shared.c ${CAPI_SRC}/mystrtoul.c ${CAPI_SRC}/weakrefobject.c |
| 102 | + ${CAPI_SRC}/modsupport_shared.c ${CAPI_SRC}/fileobject.c ${CAPI_SRC}/pystrcmp.c ${CAPI_SRC}/getversion.c |
| 103 | + ${CAPI_SRC}/genobject.c ${CAPI_SRC}/methodobject.c ${CAPI_SRC}/boolobject.c ${CAPI_SRC}/pylifecycle.c |
| 104 | + ${CAPI_SRC}/errors.c ${CAPI_SRC}/signals.c ${CAPI_SRC}/datetime.c ${CAPI_SRC}/bytearrayobject_shared.c |
| 105 | +) |
| 106 | + |
| 107 | +file(GLOB_RECURSE ACTUAL_SRC_FILES |
| 108 | + LIST_DIRECTORIES FALSE |
| 109 | + "${CAPI_SRC}/*.c") |
| 110 | + |
| 111 | + |
| 112 | +function(list_equals lst0 lst1) |
| 113 | + list(LENGTH ${lst0} len0) |
| 114 | + list(LENGTH ${lst1} len1) |
| 115 | + if(NOT len0 EQUAL len1) |
| 116 | + message(FATAL_ERROR "The list of source files does not match the current file system. " |
| 117 | + "Different number of files: given len = ${len0} ;; actual len = ${len1}") |
| 118 | + endif() |
| 119 | + |
| 120 | + # sort lists |
| 121 | + list(SORT ${lst0}) |
| 122 | + list(SORT ${lst1}) |
| 123 | + |
| 124 | + # iterate over both lists in simultaneously |
| 125 | + foreach(item IN ZIP_LISTS ${lst0} ${lst1}) |
| 126 | + if(NOT ${item_0} STREQUAL ${item_1}) |
| 127 | + message(VERBOSE "given src files = ${lst0}") |
| 128 | + message(VERBOSE "actual src files = ${lst1}") |
| 129 | + message(FATAL_ERROR "The list of source files does not match the current file system. " |
| 130 | + "Different items: given file = ${item_0} ;; actual file = ${item_1}") |
| 131 | + endif() |
| 132 | + endforeach() |
| 133 | +endfunction() |
| 134 | + |
| 135 | +list_equals(SRC_FILES ACTUAL_SRC_FILES) |
| 136 | + |
| 137 | +include_directories( |
| 138 | + ${CAPI_SRC} |
| 139 | + "${SRC_DIR}/include" |
| 140 | + "${TRUFFLE_H_INC}" |
| 141 | +) |
| 142 | + |
| 143 | +function(native_module name core src_files) |
| 144 | + add_library(${name} SHARED) |
| 145 | + target_compile_options(${name} PRIVATE ${CFLAGS_WARNINGS}) |
| 146 | + if(APPLE) |
| 147 | + target_link_options(${name} PRIVATE -undefined dynamic_lookup) |
| 148 | + endif() |
| 149 | + if(${core}) |
| 150 | + target_compile_definitions(${name} PRIVATE Py_BUILD_CORE) |
| 151 | + target_include_directories(${name} PRIVATE "${SRC_DIR}/include/internal") |
| 152 | + endif() |
| 153 | + target_compile_definitions(${name} PRIVATE Py_BUILD_CORE_MODULE) |
| 154 | + set_target_properties(${name} PROPERTIES SUFFIX "${GRAALPY_EXT}" |
| 155 | + PREFIX "") |
| 156 | + target_sources(${name} PRIVATE ${src_files}) |
| 157 | + if(WIN32) |
| 158 | + target_link_directories(${name} PRIVATE ${CMAKE_BINARY_DIR}) |
| 159 | + target_link_libraries(${name} PRIVATE ${TARGET_LIBPYTHON}) |
| 160 | + endif() |
| 161 | + install(TARGETS ${name} DESTINATION "bin/modules") |
| 162 | +endfunction() |
| 163 | + |
| 164 | +function(simple_native_module name) |
| 165 | + native_module(${name} TRUE "${SRC_DIR}/modules/${name}.c") |
| 166 | +endfunction() |
| 167 | + |
| 168 | +###################################################################### |
| 169 | +# BUILD TARGETS |
| 170 | +###################################################################### |
| 171 | + |
| 172 | +add_library(${TARGET_LIBPYTHON} SHARED) |
| 173 | +simple_native_module("_mmap") |
| 174 | +simple_native_module("_cpython_sre") |
| 175 | +simple_native_module("_cpython_unicodedata") |
| 176 | +simple_native_module("_cpython_struct") |
| 177 | + |
| 178 | +if(NOT WIN32) |
| 179 | + native_module("_testcapi" FALSE "${SRC_DIR}/modules/_testcapi.c") |
| 180 | + simple_native_module("_testmultiphase") |
| 181 | + simple_native_module("_ctypes_test") |
| 182 | + |
| 183 | + ###################### BZIP2 ######################## |
| 184 | + if(DEFINED LIBBZ2_BUILD_FILE) |
| 185 | + include("${LIBBZ2_BUILD_FILE}") |
| 186 | + set(TARGET_BZ2 "_bz2") |
| 187 | + simple_native_module(${TARGET_BZ2}) |
| 188 | + # variable 'BZIP2_SRC' is defined in file 'LIBBZ2_BUILD_FILE' |
| 189 | + target_include_directories(${TARGET_BZ2} PRIVATE ${BZIP2_SRC}) |
| 190 | + # variable 'TARGET_LIBBZ2' is defined in file 'LIBBZ2_BUILD_FILE' |
| 191 | + target_link_libraries(${TARGET_BZ2} ${TARGET_LIBBZ2}) |
| 192 | + endif() |
| 193 | + |
| 194 | + |
| 195 | + ###################### PYEXPAT ###################### |
| 196 | + set(TARGET_PYEXPAT "pyexpat") |
| 197 | + simple_native_module(${TARGET_PYEXPAT}) |
| 198 | + set(EXPAT_SRC "${SRC_DIR}/expat") |
| 199 | + set(PYEXPAT_HEADERS |
| 200 | + ${EXPAT_SRC}/ascii.h ${EXPAT_SRC}/asciitab.h ${EXPAT_SRC}/expat.h ${EXPAT_SRC}/expat_config.h |
| 201 | + ${EXPAT_SRC}/expat_external.h ${EXPAT_SRC}/internal.h ${EXPAT_SRC}/latin1tab.h ${EXPAT_SRC}/utf8tab.h |
| 202 | + ${EXPAT_SRC}/xmlrole.h ${EXPAT_SRC}/xmltok.h ${EXPAT_SRC}/xmltok_impl.h |
| 203 | + ) |
| 204 | + target_sources(${TARGET_PYEXPAT} PRIVATE ${PYEXPAT_HEADERS}) |
| 205 | + target_sources(${TARGET_PYEXPAT} PRIVATE ${EXPAT_SRC}/xmlparse.c ${EXPAT_SRC}/xmlrole.c ${EXPAT_SRC}/xmltok.c) |
| 206 | + target_include_directories(${TARGET_PYEXPAT} PRIVATE ${EXPAT_SRC}) |
| 207 | + # bpo-30947: Python uses best available entropy sources to call XML_SetHashSalt(), |
| 208 | + # expat entropy sources are not needed |
| 209 | + target_compile_definitions(${TARGET_PYEXPAT} PRIVATE |
| 210 | + HAVE_EXPAT_CONFIG_H=1 |
| 211 | + XML_POOR_ENTROPY=1 |
| 212 | + ) |
| 213 | +endif() |
| 214 | + |
| 215 | +target_sources(${TARGET_LIBPYTHON} PRIVATE ${SRC_FILES}) |
| 216 | +target_include_directories(${TARGET_LIBPYTHON} PRIVATE |
| 217 | + "${SRC_DIR}/include/internal" |
| 218 | +) |
| 219 | + |
| 220 | +###################################################################### |
| 221 | +# target-specific compile and link options |
| 222 | +###################################################################### |
| 223 | + |
| 224 | +target_compile_definitions(${TARGET_LIBPYTHON} PRIVATE Py_BUILD_CORE) |
| 225 | +target_compile_options(${TARGET_LIBPYTHON} PRIVATE ${CFLAGS_WARNINGS}) |
| 226 | + |
| 227 | +if(WIN32) |
| 228 | + target_link_directories(${TARGET_LIBPYTHON} PRIVATE ${GRAALVM_LLVM_LIB_DIR}) |
| 229 | + target_link_libraries(${TARGET_LIBPYTHON} sulong-native graalvm-llvm) |
| 230 | +endif() |
| 231 | + |
| 232 | +install(TARGETS ${TARGET_LIBPYTHON} DESTINATION bin) |
0 commit comments