Skip to content

Commit a0d0144

Browse files
committed
Build c.o.g.python.cext with CMake
1 parent f8f7da2 commit a0d0144

File tree

5 files changed

+283
-656
lines changed

5 files changed

+283
-656
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
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 ${var})
34+
message(FATAL_ERROR "${var} needs to be set")
35+
endif()
36+
endfunction()
37+
38+
function(check_var var)
39+
set(${var} PARENT_SCOPE)
40+
require_var(${var})
41+
endfunction()
42+
43+
# set variable from environement variable if the latter exists
44+
function(setFromEnv varname envname)
45+
if(DEFINED ENV{${envname}})
46+
set(${varname} $ENV{${envname}} PARENT_SCOPE)
47+
endif()
48+
endfunction()
49+
50+
require_var(LLVM_MODE)
51+
require_var(BZ2_LIB_DIR)
52+
53+
set(TARGET_LIBPYTHON "python")
54+
set(LIB_OUTPUT_DIR "${PROJECT_OUTPUT_DIR}/lib")
55+
56+
######################################################################
57+
# common variables and compile/link options (for all build targets)
58+
######################################################################
59+
60+
if(NOT WIN32)
61+
set(CFLAGS_WARNINGS -Werror -Wno-int-to-pointer-cast -Wno-int-conversion -Wno-void-pointer-to-int-cast
62+
-Wno-incompatible-pointer-types-discards-qualifiers -Wno-pointer-type-mismatch
63+
-Wno-braced-scalar-init -Wno-deprecated-declarations)
64+
else()
65+
set(CFLAGS_WARNINGS)
66+
endif()
67+
68+
# preprocessor defines for all platforms
69+
add_compile_definitions(
70+
NDEBUG
71+
GRAALVM_PYTHON_LLVM
72+
Py_BUILD_CORE_MODULE
73+
)
74+
75+
if(WIN32)
76+
add_compile_options(
77+
/Z7
78+
/O2
79+
/WX
80+
)
81+
add_compile_definitions(
82+
MS_WINDOWS
83+
Py_ENABLE_SHARED
84+
HAVE_DECLSPEC_DLL
85+
)
86+
endif()
87+
88+
if(APPLE)
89+
add_link_options(-undefined dynamic_lookup)
90+
endif()
91+
92+
# don't install into the system but into the MX project's output dir
93+
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR})
94+
95+
check_var(GRAALVM_LLVM_LIB_DIR)
96+
check_var(TRUFFLE_H_INC)
97+
98+
require_var(GRAALPY_EXT)
99+
100+
# using glob patterns is not recommended: https://cmake.org/cmake/help/latest/command/file.html#glob
101+
set(SRC_FILES src/codecs.c src/setobject.c src/compile.c src/thread.c src/moduleobject.c src/preconfig.c
102+
src/getbuildinfo.c src/object.c src/dtoa.c src/pystrhex.c src/capi.c src/complexobject.c src/capsule.c
103+
src/typeobject.c src/obmalloc.c src/descrobject.c src/memoryobject.c src/traceback.c src/unicodeobject.c
104+
src/pythonrun.c src/funcobject.c src/codeobject.c src/unicodectype.c src/structseq.c src/import.c
105+
src/pytime.c src/bytearrayobject.c src/listobject.c src/bytesobject.c src/object_shared.c src/longobject.c
106+
src/sysmodule.c src/pystrtod.c src/tupleobject.c src/iterobject.c src/sliceobject.c src/classobject.c
107+
src/floatobject.c src/namespaceobject.c src/_warnings.c src/dictobject.c src/pystate.c src/mysnprintf.c
108+
src/ceval.c src/getcompiler.c src/pyhash.c src/fileutils.c src/descrobject_shared.c src/modsupport.c
109+
src/context.c src/abstract.c src/frameobject.c src/posixmodule.c src/longobject_shared.c src/exceptions.c
110+
src/pyctype.c src/typeobject_shared.c src/mystrtoul.c src/weakrefobject.c src/modsupport_shared.c
111+
src/fileobject.c src/pystrcmp.c src/getversion.c src/genobject.c src/methodobject.c src/boolobject.c
112+
src/pylifecycle.c src/errors.c src/signals.c src/datetime.c
113+
)
114+
115+
include_directories(
116+
"src"
117+
"include"
118+
"${TRUFFLE_H_INC}"
119+
)
120+
121+
function(native_module name core src_files)
122+
add_library(${name} SHARED)
123+
target_compile_options(${name} PRIVATE ${CFLAGS_WARNINGS})
124+
if(APPLE)
125+
target_link_options(${name} PRIVATE -undefined dynamic_lookup)
126+
endif()
127+
if(${core})
128+
target_compile_definitions(${name} PRIVATE Py_BUILD_CORE)
129+
target_include_directories(${name} PRIVATE "include/internal")
130+
endif()
131+
set_target_properties(${name} PROPERTIES SUFFIX "${GRAALPY_EXT}")
132+
set_target_properties(${name} PROPERTIES PREFIX "")
133+
target_sources(${name} PRIVATE ${src_files})
134+
install(TARGETS ${name} DESTINATION "bin/modules")
135+
endfunction()
136+
137+
function(simple_native_module name)
138+
native_module(${name} TRUE "modules/${name}.c")
139+
endfunction()
140+
141+
#file(GLOB_RECURSE GLOBBED_SRC_FILES
142+
# LIST_DIRECTORIES FALSE
143+
# "src/*.c")
144+
145+
######################################################################
146+
# BUILD TARGETS
147+
######################################################################
148+
149+
add_library(${TARGET_LIBPYTHON} SHARED)
150+
simple_native_module("_mmap")
151+
simple_native_module("_cpython_sre")
152+
simple_native_module("_cpython_unicodedata")
153+
simple_native_module("_cpython_struct")
154+
155+
if(NOT WIN32)
156+
native_module("_testcapi" FALSE "modules/_testcapi.c")
157+
simple_native_module("_testmultiphase")
158+
simple_native_module("_ctypes_test")
159+
160+
###################### BZIP2 ########################
161+
set(TARGET_BZ2 "_bz2")
162+
simple_native_module(${TARGET_BZ2})
163+
set_target_properties(${TARGET_BZ2} PROPERTIES
164+
# BUILD_WITH_INSTALL_RPATH FALSE
165+
# BUILD_RPATH_USE_ORIGIN TRUE
166+
BUILD_RPATH "../lib/${LLVM_MODE}/"
167+
)
168+
target_link_directories(${TARGET_BZ2} PRIVATE "${BZ2_LIB_DIR}")
169+
target_link_libraries(${TARGET_BZ2} bz2)
170+
171+
172+
###################### PYEXPAT ######################
173+
set(TARGET_PYEXPAT "pyexpat")
174+
simple_native_module(${TARGET_PYEXPAT})
175+
set(PYEXPAT_HEADERS
176+
"expat/ascii.h" "expat/asciitab.h" "expat/expat.h" "expat/expat_config.h" "expat/expat_external.h"
177+
"expat/internal.h" "expat/latin1tab.h" "expat/utf8tab.h" "expat/xmlrole.h" "expat/xmltok.h"
178+
"expat/xmltok_impl.h"
179+
)
180+
target_sources(${TARGET_PYEXPAT} PRIVATE ${PYEXPAT_HEADERS})
181+
target_sources(${TARGET_PYEXPAT} PRIVATE "expat/xmlparse.c" "expat/xmlrole.c" "expat/xmltok.c")
182+
target_include_directories(${TARGET_PYEXPAT} PRIVATE "expat")
183+
# bpo-30947: Python uses best available entropy sources to call XML_SetHashSalt(),
184+
# expat entropy sources are not needed
185+
target_compile_definitions(${TARGET_PYEXPAT} PRIVATE
186+
HAVE_EXPAT_CONFIG_H=1
187+
XML_POOR_ENTROPY=1
188+
)
189+
endif()
190+
191+
# set file extension; e.g. "graalpy231-310-native-x86_64-darwin.so"
192+
set_target_properties(${TARGET_LIBPYTHON} PROPERTIES SUFFIX "${GRAALPY_EXT}")
193+
194+
target_sources(${TARGET_LIBPYTHON} PRIVATE ${SRC_FILES})
195+
target_include_directories(${TARGET_LIBPYTHON} PRIVATE
196+
"include/internal"
197+
)
198+
199+
######################################################################
200+
# target-specific compile and link options
201+
######################################################################
202+
203+
#target_link_directories(${TARGET_LIBPYTHON} PRIVATE ${GRAALVM_LLVM_LIB_DIR})
204+
#target_link_libraries(${TARGET_LIBPYTHON} graalvm-llvm)
205+
target_compile_options(${TARGET_LIBPYTHON} PRIVATE ${CFLAGS_WARNINGS})
206+
207+
if(WIN32)
208+
target_link_libraries(${TARGET_LIBPYTHON} sulong-native)
209+
endif()
210+
211+
#if(APPLE)
212+
# target_link_options(${TARGET_LIBPYTHON} PRIVATE -undefined dynamic_lookup)
213+
#endif()
214+
215+
install(TARGETS ${TARGET_LIBPYTHON} DESTINATION bin)

0 commit comments

Comments
 (0)