Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Wasm32 as target for cross compiling #2404

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ortools/base/raw_logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@
#include "ortools/base/log_severity.h"
#include "ortools/base/logging_utilities.h"

#if !defined(_MSC_VER)
#if !defined(_MSC_VER) && !defined(__EMSCRIPTEN__)
#include <sys/syscall.h> // for syscall()
#define safe_write(fd, s, len) syscall(SYS_write, fd, s, len)
#else
#if defined(__EMSCRIPTEN__)
#define safe_write(fd, s, len) write(fd, s, len)
#else
#include <io.h> // _write()
// Not so safe, but what can you do?
#define safe_write(fd, s, len) _write(fd, s, len)
#endif
#endif

#if defined(_MSC_VER) && !defined(__MINGW32__)
enum { STDIN_FILENO = 0, STDOUT_FILENO = 1, STDERR_FILENO = 2 };
Expand Down
23 changes: 23 additions & 0 deletions tools/cross_compile.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,25 @@ QEMU_ARGS+=( -L "${SYSROOT_DIR}" )
QEMU_ARGS+=( -E LD_PRELOAD="${SYSROOT_DIR}/usr/lib/libstdc++.so.6:${SYSROOT_DIR}/lib/libgcc_s.so.1" )
}

function expand_wasm_config() {
local -r EMSDK_VERSION=2.0.14
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Mizux,
Is it planned for next release or will it take time? Also, is this command work with stable branch:
emcmake cmake -S . -B build -DBUILD_DEPS=ON -DBUILD_PYTHON=ON

I tried after making changes in cross_compile.sh and I am getting error as:
Could NOT find Python3 (missing: Python3_INCLUDE_DIRS Python3_LIBRARIES Development Development.Module Development.Embed) (found version "3.11.9")

However, I don't see such error when using just cmake?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't get it, what did you try to do/expect ?

BUILD_PYTHON is used to generate automatically python wrapper on top of the C++ library to have access to ortools in python (which is just a wrapper on top of the native library, since we won't reimplement ortools in pure Python/Java nor .Net).

On the other side, AFAIK, emcmake is used to transpile C++ code into wasm byte code to have a "browser compatible" source code so there is no point to try to build a python wrapper on top of this wasm library.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi there, I recently pointed @airen1986 to this issue from pyodide/pyodide#5013. If configured correctly, Pyodide would allow runtime for a Python interface for this WASM library through an Emscripten-compiled Python extension module.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I guess I understand what you want to do:
0. This is not something we support

  1. currently our wrapper is generated by swig which is currently use to build 64bit code -> don't know how it work with WASM32 ptr size etc...
  2. IIRC emcmake is working by injecting a CMake cross toolchain thus my bet is the Toolchainfile do not provide a python thus the issue.
  3. I would suggest to play with my pet project: https://github.com/Mizux/python-native which should have the same cmake stuff than or-tools so once you have something work there it should be easier to patch/fixup or-tools and dev iteration should be faster IMHO...

sorry this comment is little bit dry, will try to reformulate if you want...

local -r EMSDK_URL=https://github.com/emscripten-core/emsdk/archive/${EMSDK_VERSION}.tar.gz
local -r EMSDK_RELATIVE_DIR="emsdk-${EMSDK_VERSION}"
local -r EMSDK="${ARCHIVE_DIR}/${EMSDK_RELATIVE_DIR}"
if [ ! -d "${EMSDK}" ]; then
echo "Fetching emscripten"
unpack "${EMSDK_URL}" "${EMSDK_RELATIVE_DIR}"
echo "Installing Emscripten ..."
${EMSDK}/emsdk install ${EMSDK_VERSION}

echo "Activating Emscripten ..."
${EMSDK}/emsdk activate ${EMSDK_VERSION}
fi

declare -r TOOLCHAIN_FILE=${EMSDK}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake
CMAKE_ADDITIONAL_ARGS+=( -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" -DBUILD_SAMPLES=OFF -DBUILD_EXAMPLES=OFF -DBUILD_TESTING=OFF)
}

function build() {
cd "${PROJECT_DIR}" || exit 2
set -x
Expand Down Expand Up @@ -323,6 +342,7 @@ DESCRIPTION
\t\tmips64 mips64el (codespace)
\t\tppc (bootlin)
\t\tppc64 ppc64le (bootlin)
\t\twasm32 (emscripten)

OPTIONS
\t-h --help: show this help text
Expand Down Expand Up @@ -403,6 +423,9 @@ function main() {
ppc)
expand_bootlin_config
declare -r QEMU_ARCH=ppc ;;
wasm32)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note for myself, emscripten generate wasm32 by default
ref: https://emscripten.org/docs/tools_reference/settings_reference.html#memory64

expand_wasm_config
declare -r QEMU_ARCH=DISABLED ;;
*)
>&2 echo "Unknown TARGET '${TARGET}'..."
exit 1 ;;
Expand Down