commit:     cd2c79792afc8e9de4e87eb344a28ea5fd46d868
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Sun Sep 28 10:07:07 2025 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Sun Sep 28 10:07:07 2025 +0000
URL:        
https://gitweb.gentoo.org/proj/blas-lapack-aux-wrapper.git/commit/?id=cd2c7979

Rewrite to use actual libraries

Rewrite to look up the symbols present in actual libraries,
and therefore create wrappers that are actually compatible both with
Netlib LAPACK and FlexiBLAS.

Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>

 .gitignore             |   2 -
 Makefile               |  61 ----------------------------
 blas-lapack-aux-gen.py |  28 -------------
 make-symbol-files.py   |  54 +++++++++++++++++++++++++
 meson.build            | 105 +++++++++++++++++++++++++++++++++++++++++++++++++
 meson.build.in         |  36 -----------------
 6 files changed, 159 insertions(+), 127 deletions(-)

diff --git a/.gitignore b/.gitignore
deleted file mode 100644
index d5fb613..0000000
--- a/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-/blas-lapack-aux-wrapper-*
-*.tar.xz

diff --git a/Makefile b/Makefile
deleted file mode 100644
index 60d940b..0000000
--- a/Makefile
+++ /dev/null
@@ -1,61 +0,0 @@
-FLEXIBLAS_DIR ?= $(error Specify FLEXIBLAS_DIR as path to FlexiBLAS sourcess)
-VERSION ?= $(error Specify VERSION as package version)
-LAPACK_VER ?= $(error Specify LAPACK_VER as LAPACK API version)
-
-CP = cp
-MKDIR_P = mkdir -p
-PYTHON ?= python
-RM_F = rm -f
-SED = sed
-TAR_CF = tar -c -f
-XZ = xz -9 -e -f
-
-PKG = blas-lapack-aux-wrapper-$(VERSION)
-PKGTAR = $(PKG).tar
-PKGTARXZ = $(PKGTAR).xz
-OUTPUT = $(PKG)/meson.build
-OUTPUT += $(PKG)/meson.options
-OUTPUT += $(PKG)/COPYING
-OUTPUT += $(PKG)/blas.o
-OUTPUT += $(PKG)/cblas.o
-OUTPUT += $(PKG)/lapack.o
-OUTPUT += $(PKG)/lapacke.o
-
-.PHONY: all clean
-
-all: $(PKG).tar.xz
-
-clean:
-       $(RM_F) $(OUTPUT) $(PKGTAR) $(PKGTARXZ)
-
-$(PKGTAR): $(OUTPUT)
-       $(TAR_CF) $@ $^
-
-$(PKGTARXZ): $(PKGTAR)
-       $(XZ) $<
-
-$(PKG)/meson.build: meson.build.in
-       $(MKDIR_P) $(PKG)
-       $(SED) -e 's:@VERSION@:$(VERSION):' -e 's:@LAPACK_VER@:$(LAPACK_VER):' 
$< > $@
-
-$(PKG)/meson.options: meson.options
-       $(CP) $< $@
-
-$(PKG)/COPYING: COPYING
-       $(CP) $< $@
-
-$(PKG)/blas.o: $(FLEXIBLAS_DIR)/tools/codegen/blas/yaml.yaml
-       $(MKDIR_P) $(PKG)
-       $(PYTHON) blas-lapack-aux-gen.py --fortran $< > $@
-
-$(PKG)/cblas.o: $(FLEXIBLAS_DIR)/tools/codegen/cblas/yaml.yaml
-       $(MKDIR_P) $(PKG)
-       $(PYTHON) blas-lapack-aux-gen.py $< > $@
-
-$(PKG)/lapack.o: $(FLEXIBLAS_DIR)/tools/codegen/lapack/yaml/$(LAPACK_VER).yaml
-       $(MKDIR_P) $(PKG)
-       $(PYTHON) blas-lapack-aux-gen.py --fortran $< > $@
-
-$(PKG)/lapacke.o: 
$(FLEXIBLAS_DIR)/tools/codegen/lapacke/yaml/$(LAPACK_VER).yaml
-       $(MKDIR_P) $(PKG)
-       $(PYTHON) blas-lapack-aux-gen.py $< > $@

diff --git a/blas-lapack-aux-gen.py b/blas-lapack-aux-gen.py
deleted file mode 100755
index 13b0649..0000000
--- a/blas-lapack-aux-gen.py
+++ /dev/null
@@ -1,28 +0,0 @@
-#!/usr/bin/env python
-# (c) 2025 Michał Górny
-# SPDX-License-Identifier: GPL-2.0-or-later
-
-import argparse
-
-import yaml
-
-
-def main():
-    argp = argparse.ArgumentParser()
-    argp.add_argument("input", nargs="+")
-    argp.add_argument("--fortran", action="store_true")
-    args = argp.parse_args()
-
-    for inp in args.input:
-        with open(inp, "rb") as f:
-            yaml_data = yaml.safe_load(f)
-        for func in yaml_data:
-            assert set(func["alt_names"]).issubset(func["load_name"])
-            for fname in func["load_name"]:
-                print(f"{fname} = 0;")
-                if args.fortran:
-                    print(f"{fname}_ = 0;")
-
-
-if __name__ == "__main__":
-    main()

diff --git a/make-symbol-files.py b/make-symbol-files.py
new file mode 100755
index 0000000..f0998b9
--- /dev/null
+++ b/make-symbol-files.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+
+from __future__ import annotations
+
+import argparse
+import subprocess
+import sys
+from pathlib import Path
+from typing import Generator
+
+
+def get_symbols(path: Path) -> Generator[str]:
+    for line in subprocess.run(["nm", "-D", "-P", str(path)],
+                               stdout=subprocess.PIPE,
+                               check=True).stdout.decode().splitlines():
+        sym, typ, _ = line.split(" ", 2)
+        if typ in ("T", "W"):
+            yield sym
+
+
+def main():
+    argp = argparse.ArgumentParser()
+    argp.add_argument("--flexiblas",
+                      required=True,
+                      type=Path,
+                      help="Path to flexiblas library")
+    argp.add_argument("--lib",
+                      required=True,
+                      type=Path,
+                      action="append",
+                      help="BLAS / LAPACK library to process")
+    argp.add_argument("--output",
+                      required=True,
+                      type=Path,
+                      help="Output directory")
+    args = argp.parse_args()
+    args.output.mkdir(exist_ok=True)
+
+    flexiblas_sym = set(get_symbols(args.flexiblas))
+    for lib in args.lib:
+        lib_sym = set(get_symbols(lib))
+        common_sym = lib_sym.intersection(flexiblas_sym)
+        missing_sym = lib_sym.difference(flexiblas_sym)
+        if missing_sym:
+            print(f"{lib} symbols missing from {args.flexiblas}: "
+                  f"{sorted(missing_sym)}",
+                  file=sys.stderr)
+        with open(args.output / f"{lib.name}.o", "w") as f:
+            for sym in sorted(common_sym):
+                f.write(f"{sym} = 0;\n")
+
+
+if __name__ == "__main__":
+    main()

diff --git a/meson.build b/meson.build
new file mode 100644
index 0000000..320f502
--- /dev/null
+++ b/meson.build
@@ -0,0 +1,105 @@
+project(
+  'blas-lapack-aux-wrapper',
+  'c',
+  license : 'GPL-2.0-or-later',
+  version : '0',
+  meson_version : '>=1.1',
+)
+
+fs = import('fs')
+pkg = import('pkgconfig')
+py = import('python').find_installation()
+cc = meson.get_compiler('c')
+
+suffixes = ['']
+if get_option('ilp64')
+  suffixes += ['64']
+endif
+
+libraries = ['blas', 'cblas', 'lapack', 'lapacke']
+sysroot = run_command(
+  [cc, '--print-sysroot'],
+  capture : true,
+  check : true,
+).stdout()
+prefix = get_option('prefix')
+libdir = get_option('libdir')
+if fs.is_absolute(libdir)
+  sys_libs = sysroot + libdir
+else
+  sys_libs = sysroot + prefix + '/' + libdir
+endif
+
+dummy_obj = configure_file(
+  output : 'dummy.o',
+  command : ['true'],
+  capture : true,
+)
+
+foreach suffix : suffixes
+  flexiblas_dep = dependency(f'flexiblas@suffix@', required : true)
+  flexiblas_ver = flexiblas_dep.version().split('.')
+  flexiblas_fakelib = shared_library(
+    f'flexiblas@suffix@',
+    build_by_default : false,
+    objects : [dummy_obj],
+    # we need to pass x.y to get libflexiblas.so.x, i.e. to the SONAME
+    version : '.'.join(flexiblas_ver[0], flexiblas_ver[1]),
+  )
+  flexiblas_lib = fs.stem(flexiblas_fakelib.full_path())
+  flexiblas_syslib = sys_libs + '/' + flexiblas_lib
+  assert(fs.exists(flexiblas_syslib), f'@flexiblas_syslib@ does not exist')
+
+  script = [
+    py,
+    '@CURRENT_SOURCE_DIR@/make-symbol-files.py',
+    '--output',
+    '@OUTDIR@',
+    '--flexiblas',
+    flexiblas_syslib,
+  ]
+  outputs = []
+  versions = []
+
+  foreach lib : libraries
+    dep = dependency(f'@lib@@suffix@-reference', required : true)
+    dep_ver = dep.version().split('.')
+    fakelib = shared_library(
+      f'@lib@@suffix@-reference',
+      build_by_default : false,
+      objects : [dummy_obj],
+      version : '.'.join(dep_ver[0], dep_ver[1]),
+    )
+    libname = fs.stem(fakelib.full_path())
+    syslib = sys_libs + '/' + libname
+    assert(fs.exists(syslib), f'@syslib@ does not exist')
+
+    script += ['--lib', syslib]
+    outputs += [f'@[email protected]']
+    versions += [dep.version()]
+  endforeach
+
+  obj_files = custom_target(
+    output : outputs,
+    command : script,
+  )
+
+  idx = 0
+  foreach lib : libraries
+    lib_tgt = shared_library(
+      f'@lib@@suffix@',
+      objects : [obj_files[idx]],
+      version : versions[idx],
+      link_args : [f'-Wl,--auxiliary,@flexiblas_lib@'],
+      install : true,
+    )
+
+    pkg.generate(
+      lib_tgt,
+      version : versions[idx],
+      description : f'@lib@@suffix@ wrapper for FlexiBLAS',
+    )
+
+    idx += 1
+  endforeach
+endforeach

diff --git a/meson.build.in b/meson.build.in
deleted file mode 100644
index 37cfca0..0000000
--- a/meson.build.in
+++ /dev/null
@@ -1,36 +0,0 @@
-project(
-  'blas-lapack-aux-wrapper',
-  'c',
-  license : 'GPL-2.0-or-later',
-  version : '@VERSION@',
-)
-
-pkg = import('pkgconfig')
-lapack_ver = '@LAPACK_VER@'
-sover = lapack_ver.split('.')[0]
-
-suffixes = ['']
-if get_option('ilp64')
-  suffixes += ['64']
-endif
-
-foreach suffix : suffixes
-  # TODO: get proper prefix and suffix?
-  flexiblas_lib = f'libflexiblas@[email protected].@sover@'
-
-  foreach lib : ['blas', 'cblas', 'lapack', 'lapacke']
-    lib_tgt = shared_library(
-      f'@lib@@suffix@',
-      objects : [f'@[email protected]'],
-      soversion : sover,
-      link_args : [f'-Wl,--auxiliary,@flexiblas_lib@'],
-      install : true,
-    )
-
-    pkg.generate(
-      lib_tgt,
-      version : lapack_ver,
-      description : f'@lib@@suffix@ wrapper for FlexiBLAS',
-    )
-  endforeach
-endforeach

Reply via email to