From 2398e79764e1a52179f6c44b53734d5de7aeb510 Mon Sep 17 00:00:00 2001 From: kjartanm Date: Sat, 20 Feb 2021 10:37:57 +0100 Subject: [PATCH 1/4] Fence off Emscripten-incompatible elements To enable Emscripten builds, we need to fence off a couple of elements that creates unrecoverable errors during build process. --- ortools/base/raw_logging.cc | 6 +++++- ortools/glop/lp_solver.cc | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ortools/base/raw_logging.cc b/ortools/base/raw_logging.cc index a91971902eb..abc2b3d846e 100644 --- a/ortools/base/raw_logging.cc +++ b/ortools/base/raw_logging.cc @@ -25,13 +25,17 @@ #include "ortools/base/logging.h" #include "ortools/base/logging_utilities.h" -#if !defined(_MSC_VER) +#if !defined(_MSC_VER) && !defined(__EMSCRIPTEN__) #include // for syscall() #define safe_write(fd, s, len) syscall(SYS_write, fd, s, len) #else +#if !defined(__EMSCRIPTEN__) #include // _write() // Not so safe, but what can you do? #define safe_write(fd, s, len) _write(fd, s, len) +#else +#define safe_write(fd, s, len) write(fd, s, len) +#endif #endif #if defined(_MSC_VER) && !defined(__MINGW32__) diff --git a/ortools/glop/lp_solver.cc b/ortools/glop/lp_solver.cc index 460c7430f6b..1805f45e1b3 100644 --- a/ortools/glop/lp_solver.cc +++ b/ortools/glop/lp_solver.cc @@ -160,6 +160,7 @@ ProblemStatus LPSolver::SolveWithTimeLimit(const LinearProgram& lp, // Note that we only activate the floating-point exceptions after we are sure // that the program is valid. This way, if we have input NaNs, we will not // crash. +#ifndef __EMSCRIPTEN__ ScopedFloatingPointEnv scoped_fenv; if (absl::GetFlag(FLAGS_lp_solver_enable_fp_exceptions)) { #ifdef _MSC_VER @@ -168,6 +169,7 @@ ProblemStatus LPSolver::SolveWithTimeLimit(const LinearProgram& lp, scoped_fenv.EnableExceptions(FE_DIVBYZERO | FE_INVALID); #endif } +#endif // Make an internal copy of the problem for the preprocessing. const bool log_info = parameters_.log_search_progress() || VLOG_IS_ON(1); From d3d6fb730619c104fea8b5a1f68d9e7337a06470 Mon Sep 17 00:00:00 2001 From: kjartanm Date: Sat, 20 Feb 2021 13:27:39 +0100 Subject: [PATCH 2/4] Add Emscripten setup and Wasm32 as target This adds Wasm32 as target together with Emscripten setup to enable cross compiling of or-tools into wasm. --- tools/cross_compile.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tools/cross_compile.sh b/tools/cross_compile.sh index 7d5c62b7b8d..c7e42b9d02a 100755 --- a/tools/cross_compile.sh +++ b/tools/cross_compile.sh @@ -95,6 +95,19 @@ function clean_build() { mkdir -p "${BUILD_DIR}" } +function expand_wasm_config() { + declare -r EMSDK_VERSION=2.0.14 + declare -r EMSDK_URL=https://github.com/emscripten-core/emsdk/archive/${EMSDK_VERSION}.tar.gz + declare -r EMSDK_RELATIVE_DIR="emsdk-${EMSDK_VERSION}" + declare -r EMSDK="${ARCHIVE_DIR}/${EMSDK_RELATIVE_DIR}" + if [ ! -d "${EMSDK}" ]; then + install_emscripten + 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 expand_linaro_config() { #ref: https://releases.linaro.org/components/toolchain/binaries/ local -r LINARO_VERSION=7.5-2019.12 @@ -322,6 +335,9 @@ function main() { mips64el) expand_codescape_config declare -r QEMU_ARCH=mips64el ;; + wasm32) + expand_wasm_config + declare -r QEMU_ARCH=DISABLED ;; *) >&2 echo "Unknown TARGET '${TARGET}'..." exit 1 ;; From 66e0f02d403831b26f3f1f1fe0aeb6d6a4c8b9d7 Mon Sep 17 00:00:00 2001 From: kjartanm Date: Sat, 20 Feb 2021 13:36:45 +0100 Subject: [PATCH 3/4] Move Emscripten setup --- tools/cross_compile.sh | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tools/cross_compile.sh b/tools/cross_compile.sh index c7e42b9d02a..52d0a43fbd3 100755 --- a/tools/cross_compile.sh +++ b/tools/cross_compile.sh @@ -96,12 +96,18 @@ function clean_build() { } function expand_wasm_config() { - declare -r EMSDK_VERSION=2.0.14 - declare -r EMSDK_URL=https://github.com/emscripten-core/emsdk/archive/${EMSDK_VERSION}.tar.gz - declare -r EMSDK_RELATIVE_DIR="emsdk-${EMSDK_VERSION}" - declare -r EMSDK="${ARCHIVE_DIR}/${EMSDK_RELATIVE_DIR}" + local -r EMSDK_VERSION=2.0.14 + 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 - install_emscripten + 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 From 231bebbc4852a951f23274974f4f3562a0c5352f Mon Sep 17 00:00:00 2001 From: kjartanm Date: Sat, 20 Feb 2021 19:10:20 +0100 Subject: [PATCH 4/4] Reverse _EMSCRIPTEN_ test, add wasm32 in usage() --- ortools/base/raw_logging.cc | 6 +++--- tools/cross_compile.sh | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ortools/base/raw_logging.cc b/ortools/base/raw_logging.cc index abc2b3d846e..c8ff6a20fa1 100644 --- a/ortools/base/raw_logging.cc +++ b/ortools/base/raw_logging.cc @@ -29,12 +29,12 @@ #include // for syscall() #define safe_write(fd, s, len) syscall(SYS_write, fd, s, len) #else -#if !defined(__EMSCRIPTEN__) +#if defined(__EMSCRIPTEN__) +#define safe_write(fd, s, len) write(fd, s, len) +#else #include // _write() // Not so safe, but what can you do? #define safe_write(fd, s, len) _write(fd, s, len) -#else -#define safe_write(fd, s, len) write(fd, s, len) #endif #endif diff --git a/tools/cross_compile.sh b/tools/cross_compile.sh index 52d0a43fbd3..cb3d3ad8061 100755 --- a/tools/cross_compile.sh +++ b/tools/cross_compile.sh @@ -270,7 +270,7 @@ DESCRIPTION \tYou MUST define the following variables before running this script: \t* PROJECT: glop or-tools -\t* TARGET: x86_64 aarch64-linux-gnu aarch64_be-linux-gnu mips64 mips64el +\t* TARGET: x86_64 aarch64-linux-gnu aarch64_be-linux-gnu mips64 mips64el wasm32 OPTIONS \t-h --help: show this help text