Skip to content

Commit

Permalink
cpython: fix func compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
csegarragonz committed Feb 19, 2024
1 parent f5abbbc commit 17d201b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
10 changes: 5 additions & 5 deletions func/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(faasm-func)

set(CMAKE_CXX_STANDARD 17)

if (CMAKE_SYSTEM_NAME STREQUAL "Wasm")
if (CMAKE_SYSTEM_NAME STREQUAL "WASI")
# For Faasm functions, we add `_faasm_zygote` as an exported symbol
set(CMAKE_EXE_LINKER_FLAGS
"${CMAKE_EXE_LINKER_FLAGS} -Xlinker --export=_faasm_zygote"
Expand All @@ -12,18 +12,18 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Wasm")
set(CMAKE_EXECUTABLE_SUFFIX ".wasm")
endif ()

if (CMAKE_SYSTEM_NAME STREQUAL "Wasm")
if (CMAKE_SYSTEM_NAME STREQUAL "WASI")
set(FAASM_FUNC_LIBS faasm emscripten c-printscan-long-double)

set(PYTHON_LIBRARIES ${CMAKE_SYSROOT}/lib/wasm32-wasi/libpython3.8.a)
set(PYTHON_INCLUDE_DIRS ${CMAKE_SYSROOT}/include/python3.8)
set(PYTHON_LIBRARIES $ENV{FAASM_WASM_LIB_INSTALL_DIR}/libpython3.8.a)
set(PYTHON_INCLUDE_DIRS $ENV{FAASM_WASM_HEADER_INSTALL_DIR}/python3.8)
else ()
find_package(PythonLibs)
endif()

add_executable(py_func pyinit.c pyinit.h py_func.cpp)

if (CMAKE_SYSTEM_NAME STREQUAL "Wasm")
if (CMAKE_SYSTEM_NAME STREQUAL "WASI")
target_link_libraries(py_func ${FAASM_FUNC_LIBS} ${PYTHON_LIBRARIES} ffi)
target_include_directories(py_func PUBLIC ${PYTHON_INCLUDE_DIRS})

Expand Down
28 changes: 15 additions & 13 deletions tasks/cpython.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
from copy import copy as deep_copy
from faasmctl.util.upload import upload_wasm
from faasmtools.build import (
FAASM_BUILD_ENV_DICT,
WASM_HEADER_INSTALL,
WASM_LIB_INSTALL,
WASM_WASI_LIBC_LDFLAGS,
build_config_cmd,
get_faasm_build_env_dict
)
from faasmtools.compile_util import wasm_cmake, wasm_copy_upload
from faasmtools.env import LLVM_VERSION, WASM_DIR
Expand Down Expand Up @@ -54,16 +51,16 @@
"PATH": PATH_ENV_VAR,
}
)
ENV_VARS.update(FAASM_BUILD_ENV_DICT)
ENV_VARS.update(get_faasm_build_env_dict(is_threads=True))

LIB_SRC_DIR = join(CPYTHON_INSTALL_DIR, "lib")
LIB_DEST_DIR = join(FAASM_RUNTIME_ROOT, "lib")

LIBPYTHON_SRC_PATH = join(LIB_SRC_DIR, "libpython3.8.a")
LIBPYTHON_DEST_PATH = join(WASM_LIB_INSTALL, "libpython3.8.a")
LIBPYTHON_DEST_PATH = join(ENV_VARS["FAASM_WASM_LIB_INSTALL_DIR"], "libpython3.8.a")

INCLUDE_SRC_DIR = join(CPYTHON_INSTALL_DIR, "include", "python3.8")
INCLUDE_DEST_DIR = join(WASM_HEADER_INSTALL, "python3.8")
INCLUDE_DEST_DIR = join(ENV_VARS["FAASM_WASM_HEADER_INSTALL_DIR"], "python3.8")

# See the CPython docs for more info:
# - General: https://devguide.python.org/setup/#compile-and-build
Expand Down Expand Up @@ -96,21 +93,26 @@ def wasm(ctx, clean=False, noconf=False, nobuild=False):
# relevant in the module builds.

# Link in extra wasi-libc long double support (see wasi-libc docs)
link_libs = ["-lfaasm"] + WASM_WASI_LIBC_LDFLAGS
link_libs = " ".join(link_libs)
link_libs = "-lfaasm " + ENV_VARS["FAASM_WASM_STATIC_LINKER_FLAGS"]
# link_libs = " ".join(link_libs)

# Configure
configure_cmd = build_config_cmd(
ENV_VARS,
[
"CONFIG_SITE=./config.site",
"READELF=true",
"./configure",
'LIBS="{}"'.format(link_libs),
"--build=wasm32",
"--host={}".format(ENV_VARS["FAASM_WASM_TRIPLE"]),
"--disable-ipv6",
"--disable-shared",
"--prefix={}".format(CPYTHON_INSTALL_DIR),
"--with-system-ffi",
]
],
# Do not set the --host flag as we want to use the wasi-threads target
conf_args=False,
)

if not noconf:
Expand All @@ -133,8 +135,8 @@ def wasm(ctx, clean=False, noconf=False, nobuild=False):
_run_cpython_cmd("bininstall", ["make", "bininstall"])

# Prepare destinations
makedirs(WASM_HEADER_INSTALL, exist_ok=True)
makedirs(WASM_LIB_INSTALL, exist_ok=True)
makedirs(ENV_VARS["FAASM_WASM_HEADER_INSTALL_DIR"], exist_ok=True)
makedirs(ENV_VARS["FAASM_WASM_LIB_INSTALL_DIR"], exist_ok=True)

rmtree(INCLUDE_DEST_DIR, ignore_errors=True)

Expand Down Expand Up @@ -231,7 +233,7 @@ def func(ctx, clean=False, debug=False):
wasm_file = join(func_build_dir, "{}.wasm".format(CPYTHON_FUNC_NAME))

# Build and install the wasm
wasm_cmake(func_dir, func_build_dir, CPYTHON_FUNC_NAME, clean, debug)
wasm_cmake(func_dir, func_build_dir, CPYTHON_FUNC_NAME, clean, debug, is_threads=True)
wasm_copy_upload(CPYTHON_FUNC_USER, CPYTHON_FUNC_NAME, wasm_file)


Expand Down
2 changes: 1 addition & 1 deletion third-party/cpython

0 comments on commit 17d201b

Please sign in to comment.