Skip to content

Commit a4dec02

Browse files
committed
Build libhpy (native) with CMake
1 parent eb3b6ce commit a4dec02

33 files changed

+166
-22
lines changed

graalpython/com.oracle.graal.python.cext/setup.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
, "-Wno-deprecated-declarations"
5555
]
5656
libpython_name = "libpython"
57-
libhpy_name = "libhpy"
5857
libposix_name = "libposix"
5958

6059
MACOS = sys.platform == "darwin"
@@ -513,24 +512,6 @@ def build_libpython(capi_home):
513512
get_config_vars()["LDSHARED"] = get_config_vars()["LDSHARED_WINDOWS"] + llvm_libs
514513

515514

516-
def build_libhpy(capi_home):
517-
src_dir = os.path.join(__dir__, "hpy")
518-
files = [os.path.abspath(os.path.join(src_dir, f)) for f in os.listdir(src_dir) if f.endswith(".c")]
519-
module = Extension(libhpy_name,
520-
sources=files,
521-
define_macros=[("HPY_UNIVERSAL_ABI", 1)],
522-
extra_compile_args=cflags_warnings,
523-
)
524-
args = [verbosity, 'build', 'install_lib', '-f', '--install-dir=%s' % capi_home, "clean", "--all"]
525-
setup(
526-
script_name='setup' + libhpy_name,
527-
script_args=args,
528-
name=libhpy_name,
529-
version='1.0',
530-
description="Graal Python's HPy C API",
531-
ext_modules=[module],
532-
)
533-
534515
def build_nativelibsupport(capi_home, subdir, libname, deps=[], **kwargs):
535516
if not is_managed:
536517
src_dir = os.path.join(__dir__, subdir)
@@ -602,7 +583,6 @@ def stripped_object_filenames(*args, **kwargs):
602583
try:
603584
build_libpython(capi_home)
604585
build_builtin_exts(capi_home)
605-
build_libhpy(capi_home)
606586
if WIN32:
607587
return # TODO: ...
608588
build_nativelibsupport(capi_home,
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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.hpy.llvm)
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+
52+
set(TARGET_LIB "hpy-${LLVM_MODE}")
53+
# set(LIB_OUTPUT_DIR "${PROJECT_OUTPUT_DIR}/lib")
54+
55+
# don't install into the system but into the MX project's output dir
56+
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR})
57+
58+
check_var(GRAALVM_HPY_INCLUDE_DIR)
59+
check_var(GRAALVM_PYTHON_INCLUDE_DIR)
60+
check_var(TRUFFLE_H_INC)
61+
62+
file(GLOB_RECURSE SRC_FILES
63+
LIST_DIRECTORIES TRUE
64+
"${CMAKE_CURRENT_LIST_DIR}/src/*.c")
65+
66+
# only for Windows but to avoid a CMake warning
67+
check_var(GRAALVM_LLVM_LIB_DIR)
68+
69+
# using glob patterns is not recommended: https://cmake.org/cmake/help/latest/command/file.html#glob
70+
add_library(${TARGET_LIB} SHARED)
71+
72+
target_sources(${TARGET_LIB} PRIVATE ${SRC_FILES})
73+
target_include_directories(${TARGET_LIB} PRIVATE
74+
"${GRAALVM_HPY_INCLUDE_DIR}"
75+
"${GRAALVM_PYTHON_INCLUDE_DIR}"
76+
"${TRUFFLE_H_INC}"
77+
)
78+
79+
if(WIN32)
80+
target_link_directories(${TARGET_LIB} PRIVATE ${GRAALVM_LLVM_LIB_DIR})
81+
target_link_libraries(${TARGET_LIB} graalvm-llvm)
82+
target_compile_options(${TARGET_LIB} PRIVATE /Z7 /O2 /WX)
83+
else()
84+
target_compile_options(${TARGET_LIB} PRIVATE
85+
-DHPY_UNIVERSAL_ABI
86+
-DNDEBUG
87+
-DGRAALVM_PYTHON_LLVM
88+
-Werror
89+
-Wno-int-to-pointer-cast
90+
-Wno-int-conversion
91+
-Wno-void-pointer-to-int-cast
92+
-Wno-incompatible-pointer-types-discards-qualifiers
93+
-Wno-pointer-type-mismatch
94+
-Wno-braced-scalar-init
95+
-Wno-deprecated-declarations
96+
)
97+
endif()
98+
99+
if(APPLE)
100+
target_link_options(${TARGET_LIB} PRIVATE -undefined dynamic_lookup)
101+
endif()
102+
103+
install(TARGETS ${TARGET_LIB} DESTINATION bin)

0 commit comments

Comments
 (0)