Skip to content
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
27 changes: 27 additions & 0 deletions recipes/recipes_wasm/f2cblaslapack/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 1991-2025, UChicago Argonne, LLC and the PETSc Developers and Contributors
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


This license DOES NOT apply to any software that may be obtained via the
--download-package option of the PETSc configuration. Each of those packages is
covered by its own license(s).
37 changes: 37 additions & 0 deletions recipes/recipes_wasm/f2cblaslapack/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
set -eux

# The upstream makefile includes PETSc's build-time configuration just to
# obtain CC/COPTFLAGS/AR. That file does not exist here, and everything it
# would provide is passed explicitly below.
sed -i '/^include \${PETSC_DIR}/d' makefile

make -j"${CPU_COUNT}" CC=emcc AR=emar RANLIB=emranlib COPTFLAGS="-O2" \
blas_lib lapack_lib

mkdir -p "$PREFIX/lib" "$PREFIX/lib/pkgconfig"
cp libf2cblas.a libf2clapack.a "$PREFIX/lib/"

# pkg-config files, matching the layout of libblas-static / liblapack-static so
# that pkg-config --static resolves the pair. The version comes from the recipe
# via PKG_VERSION, never a literal.
write_pc() {
local lib=$1 name=$2 kind=$3 requires=${4:-}
cat > "$PREFIX/lib/pkgconfig/${lib}.pc" <<EOF
prefix=\${pcfiledir}/../..
libdir=\${prefix}/lib
includedir=\${prefix}/include

Name: ${name}
Description: ${kind} in C (f2c translation, as bundled by PETSc)
Version: ${PKG_VERSION}
URL: https://petsc.org/
Libs: -L\${libdir} -l${lib}
EOF
if [ -n "$requires" ]; then
echo "Requires.private: ${requires}" >> "$PREFIX/lib/pkgconfig/${lib}.pc"
fi
}

write_pc f2cblas F2CBLAS BLAS
write_pc f2clapack F2CLAPACK LAPACK f2cblas
64 changes: 64 additions & 0 deletions recipes/recipes_wasm/f2cblaslapack/recipe.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
context:
name: f2cblaslapack
version: 3.8.0.q2

package:
name: ${{ name }}-static
version: ${{ version }}

source:
# The upstream tarball carries no LICENSE file: the sources are the f2c
# translations that PETSc redistributes under its own BSD-2-Clause license.
- url: https://web.cels.anl.gov/projects/petsc/download/externalpackages/f2cblaslapack-${{ version }}.tar.gz
sha256: 12fa8d5001e313aeeea22eb2054883f59e3e3c656f11129f31abb02ddbf8ad60
- path: LICENSE

build:
number: 0
script: build.sh

requirements:
build:
- ${{ compiler('c') }}
- make
- sed

tests:
- package_contents:
files:
- lib/libf2cblas.a
- lib/libf2clapack.a
- lib/pkgconfig/f2cblas.pc
- lib/pkgconfig/f2clapack.pc
- script: test_f2cblaslapack.sh
files:
recipe:
- test_f2cblaslapack.c
- test_f2cblaslapack.sh
requirements:
build:
- ${{ compiler('c') }}
- nodejs
- pkg-config
- script: generic-testing --allow-pthread
requirements:
build:
- generic-testing

about:
homepage: https://petsc.org/
repository: https://gitlab.com/petsc/petsc
license: BSD-2-Clause
license_file: LICENSE
summary: BLAS and LAPACK in C, as bundled with PETSc
description: |
f2cblaslapack is PETSc's C translation (via f2c) of the reference BLAS and
LAPACK routines. The entry points use the plain C calling convention, with no
hidden Fortran CHARACTER lengths and no Fortran runtime, so it can be linked
from C with emcc. It is the BLAS/LAPACK implementation PETSc itself uses when
built with a C compiler only.
documentation: https://petsc.org/release/

extra:
recipe-maintainers:
- Alex-PLACET
64 changes: 64 additions & 0 deletions recipes/recipes_wasm/f2cblaslapack/test_f2cblaslapack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* Links against f2cblaslapack with no Fortran runtime and no hidden
* CHARACTER length arguments, which is the whole point of this package. */

extern void dgemm_(const char *, const char *, const int *, const int *,
const int *, const double *, const double *, const int *,
const double *, const int *, const double *, double *,
const int *);
extern void dgetrf_(const int *, const int *, double *, const int *, int *, int *);
extern void dgetrs_(const char *, const int *, const int *, const double *,
const int *, const int *, double *, const int *, int *);
extern double dnrm2_(const int *, const double *, const int *);

#include <math.h>
#include <stdio.h>

static int check(const char *what, double got, double want)
{
double err = fabs(got - want);
printf("%s: got %.12g want %.12g (%s)\n", what, got, want, err < 1e-12 ? "ok" : "FAIL");
return err < 1e-12;
}

int main(void)
{
int ok = 1;

/* BLAS: C = A*B with A = [[1,2],[3,4]], B = [[5,6],[7,8]] -> [[19,22],[43,50]] */
{
double a[4] = {1, 3, 2, 4}; /* column-major */
double b[4] = {5, 7, 6, 8};
double c[4] = {0, 0, 0, 0};
double alpha = 1.0, beta = 0.0;
int n = 2;
dgemm_("N", "N", &n, &n, &n, &alpha, a, &n, b, &n, &beta, c, &n);
ok &= check("dgemm C[0,0]", c[0], 19.0);
ok &= check("dgemm C[1,0]", c[1], 43.0);
ok &= check("dgemm C[0,1]", c[2], 22.0);
ok &= check("dgemm C[1,1]", c[3], 50.0);
}

/* BLAS: ||[3,4]|| = 5 */
{
double v[2] = {3, 4};
int n = 2, inc = 1;
ok &= check("dnrm2", dnrm2_(&n, v, &inc), 5.0);
}

/* LAPACK: solve [[4,-1],[-1,4]] x = [1,1] -> x = [1/3, 1/3] */
{
double m[4] = {4, -1, -1, 4}; /* column-major */
double rhs[2] = {1, 1};
int n = 2, nrhs = 1, info = 0;
int ipiv[2];
dgetrf_(&n, &n, m, &n, ipiv, &info);
if (info != 0) { printf("dgetrf info=%d FAIL\n", info); return 1; }
dgetrs_("N", &n, &nrhs, m, &n, ipiv, rhs, &n, &info);
if (info != 0) { printf("dgetrs info=%d FAIL\n", info); return 1; }
ok &= check("dgetrs x[0]", rhs[0], 1.0 / 3.0);
ok &= check("dgetrs x[1]", rhs[1], 1.0 / 3.0);
}

printf("%s\n", ok ? "f2cblaslapack ALL OK" : "f2cblaslapack FAILED");
return ok ? 0 : 1;
}
12 changes: 12 additions & 0 deletions recipes/recipes_wasm/f2cblaslapack/test_f2cblaslapack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
set -eux

export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig"

emcc test_f2cblaslapack.c -o test_f2cblaslapack.js \
-sEXIT_RUNTIME=1 \
-sALLOW_MEMORY_GROWTH=1 \
$(pkg-config --cflags f2clapack) \
$(pkg-config --static --libs f2clapack)

node test_f2cblaslapack.js
57 changes: 57 additions & 0 deletions recipes/recipes_wasm/petsc/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash
set -eux

export PYTHON=${PYTHON:-$(command -v python3)}
export PETSC_DIR=$SRC_DIR
export PETSC_ARCH=arch-emscripten

# BLAS/LAPACK comes from f2cblaslapack-static (host dependency): PETSc's own C
# translation of BLAS/LAPACK, packaged separately so other C consumers can use it.

# 1. Configure PETSc in batch (cross-compile) mode against the archives
# provided by f2cblaslapack-static (host dependency).
test -f "$PREFIX/lib/libf2cblas.a"
test -f "$PREFIX/lib/libf2clapack.a"

cd "$PETSC_DIR"
"$PYTHON" ./configure \
--with-cc=emcc \
--with-cxx=0 \
--with-fc=0 \
--with-ar=emar \
--with-ranlib=emranlib \
--with-batch \
--with-mpi=0 \
--with-x=0 \
--with-shared-libraries=0 \
--with-debugging=0 \
COPTFLAGS="-O2" \
--with-blas-lib="[$PREFIX/lib/libf2cblas.a]" \
--with-lapack-lib="[$PREFIX/lib/libf2clapack.a]" \
--prefix="$PREFIX"

# 2. Batch mode probes headers with the preprocessor only, so it "finds"
# <sys/procfs.h>, which exists in emscripten's sysroot but is unusable
# (undeclared elf_gregset_t) and breaks src/sys/memory/mem.c.
sed -i '/PETSC_HAVE_SYS_PROCFS_H/d' "$PETSC_ARCH/include/petscconf.h"

# 3. Build and install (install-lib skips ~60 MB of examples/data).
make -j"${CPU_COUNT}" all
make install-lib

# 4. Strip build-env tool paths; keep the tool names so they resolve on PATH.
for f in $(grep -rl "$BUILD_PREFIX/bin/" "$PREFIX/lib/petsc" 2>/dev/null); do
sed -i "s|$BUILD_PREFIX/bin/||g" "$f"
done

# 5. PETSc's .pc files expand ${PETSC_DIR} without defining it; strict
# pkg-config (0.29.2, as opposed to 3.x) then rejects the file outright.
for pc in "$PREFIX"/lib/pkgconfig/petsc.pc "$PREFIX"/lib/pkgconfig/PETSc.pc; do
sed -i '/^prefix=/a PETSC_DIR=${prefix}' "$pc"
done

# 6. Drop build-only leftovers.
rm -f "$PREFIX"/lib/petsc/conf/configure-hash \
"$PREFIX"/lib/petsc/conf/pkg.conf.f2cblaslapack
rm -rf "$PREFIX"/share/petsc/examples/src
find "$PREFIX" -name '*.pyc' -delete
13 changes: 13 additions & 0 deletions recipes/recipes_wasm/petsc/build_petsc4py.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
set -eux

# petsc4py lives in the PETSc sdist and builds against the installed PETSc.
export PETSC_DIR="$PREFIX"
unset PETSC_ARCH

# numpy is a wasm wheel, so it cannot be imported by the build interpreter;
# petsc4py takes the include dir from this variable instead of numpy.get_include().
export NUMPY_INCLUDE="$PREFIX/lib/python${PY_VER}/site-packages/numpy/_core/include"

cd "$SRC_DIR/src/binding/petsc4py"
"$PYTHON" -m pip install . --no-deps --no-build-isolation -vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: omp <noreply@example.com>
Date: Fri, 28 Nov 2026 10:00:00 +0100
Subject: [PATCH] Skip rpath flags for static PETSc

distutils turns ``runtime_library_dirs`` into ``-Wl,--enable-new-dtags
-Wl,-rpath,<dir>``. Under emscripten the compiler is ``emcc`` and the
linker step is ``wasm-ld``, which rejects ``--enable-new-dtags``:

wasm-ld: error: unknown argument: --enable-new-dtags

The guard upstream uses is ``sys.platform != 'win32'``, but under
cross-python ``sys.platform`` is ``unknown`` (the host interpreter reports
the build machine), so it does not trigger. ``sysconfig.get_platform()``
here is ``unknown-wasm32``, and there is no ``sys.platform`` value for
emscripten at all.

An rpath is meaningless for this build anyway: PETSc is installed static
(``BUILDSHAREDLIB = no``) and the extension modules are emscripten side
modules, which do not resolve libraries through the dynamic loader. So
gate the rpath block on PETSc actually being shared, which is also correct
for the non-emscripten static case.

--- a/src/binding/petsc4py/conf/confpetsc.py
+++ b/src/binding/petsc4py/conf/confpetsc.py
@@ -317,8 +317,10 @@
'-L{} {}'.format(self['PETSC_LIB_DIR'], self['PETSC_LIB_BASIC']),
)
petsc_lib = flaglist(lib_flags)
- # runtime_library_dirs is not supported on Windows
- if sys.platform != 'win32':
+ # runtime_library_dirs is not supported on Windows, and it is
+ # pointless for static PETSc: distutils would pass
+ # -Wl,--enable-new-dtags, which wasm-ld rejects.
+ if sys.platform != 'win32' and self['BUILDSHAREDLIB'] == 'yes':
# if DESTDIR is set, then we're building against PETSc in a staging
# directory, but rpath needs to point to final install directory.
rpath = [strip_prefix(self.DESTDIR, self['PETSC_LIB_DIR'])]
Loading