From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-0.5 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,MAILING_LIST_MULTI autolearn=ham autolearn_force=no version=3.4.4 Received: from cgl.ntg.nl (Cgl.ntg.nl [5.39.185.202]) by inbox.vuxu.org (Postfix) with ESMTP id E327C2F4A2 for ; Sat, 14 Sep 2024 22:51:51 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by cgl.ntg.nl (Postfix) with ESMTP id BF408484FFE for ; Sat, 14 Sep 2024 22:49:57 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at cgl.ntg.nl Authentication-Results: cgl.ntg.nl (amavisd-new); dkim=fail (2048-bit key) reason="fail (message has been altered)" header.d=gmail.com Received: from cgl.ntg.nl ([127.0.0.1]) by localhost (cgl.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id nTV3atLWHXO3 for ; Sat, 14 Sep 2024 22:49:57 +0200 (CEST) Received: from cgl.ntg.nl (localhost [127.0.0.1]) by cgl.ntg.nl (Postfix) with ESMTP id 37123485351 for ; Sat, 14 Sep 2024 22:48:09 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by cgl.ntg.nl (Postfix) with ESMTP id AFDF3484ED7 for ; Sat, 14 Sep 2024 22:47:18 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at cgl.ntg.nl Received: from cgl.ntg.nl ([127.0.0.1]) by localhost (cgl.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id oUcMR-QIvZkC for ; Sat, 14 Sep 2024 22:47:18 +0200 (CEST) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=209.85.216.43; helo=mail-pj1-f43.google.com; envelope-from=florent.m42@gmail.com; receiver= Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by cgl.ntg.nl (Postfix) with ESMTPS id 252B3484ED4 for ; Sat, 14 Sep 2024 22:47:17 +0200 (CEST) Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-2d88c0f8e79so2719814a91.3 for ; Sat, 14 Sep 2024 13:47:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726346835; x=1726951635; darn=ntg.nl; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=tl9Na2JHI+4CrX6E2WCwdeb3QfmNFzCTjbMxr1HpzRE=; b=bJOYa0yRMuIL1LjD+HwO49K5fbn0hRjAFtRipw1ETx6Fc//FPPVhKLClcOe9f6oYg+ Q6vruYwP3pLPpYECQjZrnKeihksDXSyj6N7mqqUB9xqwcw2TIXJPx3ZqaqLKRJLOj6Dg oceCdaEybKndSm+xUUIk2HejrAqjHS3ARdtl2OkS6xxdyQNNEvx+gaSeaRCq86e6uAiw aOtbrEeYnKPvTVcR4gCCWUKazlrEX4s/nLczWIE0POPgJenFtMrHzBF0YSL+qYYfJAXP rfpUy2GzSforuajRPn9OEVc6zDoOaEFJ+a4EVF3zQWEvn8tV3WJO8Dw92A2MkOdXfxlU qaaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726346835; x=1726951635; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=tl9Na2JHI+4CrX6E2WCwdeb3QfmNFzCTjbMxr1HpzRE=; b=hdm5VUtfxqw/3QnbgnQUUYxuVn928ZV/I5f7dBpjz/0H+5iKoH8FrWedFALjIisKIx K8JAwE+3NrllTcdjLK6naVPBku2+tJ3ZYKcodEC6sfp0SHKBKTYZNhT9IfA0vO5PeX0c 76/aPXTgxyDRUPPTGqGP7xDpyKTjZQSw9f+JmPmBOi+WUmaALcfhvVBz0VRP/O2bu9vm N9GRYeUHpFZQQ9gE2UuclGMbcWXI4z+VUOQs+sera9u+ZeglbMmyYkfn+IqrbmCBag+9 3YjJ0dF+XtV5oru8QeqLzTMBH0B2GF6RVTUY4bnjd+cU3gs7gRLWu1iEIDkE1W33aKjq h2UA== X-Gm-Message-State: AOJu0YxF7zS3NgG/RkN95oNEkTwWHHFFVtAGS+If4ytTMVsWWKHHUOYY MipVydzh1phVs/Q5iESGSCR4qXipDaXA+VxTf6JdNIPqDNn9OZozqLSN7kEq9CE//7YOjwkFESh XMT/d6m9hsrQN+9cQQ3eUC/WFTC1kWMGWJhM= X-Google-Smtp-Source: AGHT+IGlSiRfcx/Daj9HRT6EaV+mh0nyWyfbxgSzuRQChYe/ONGeXAc+Y5f5GQcz3MyGUMx9i0MNGBdZh81aAiuTBCk= X-Received: by 2002:a17:90a:9a4:b0:2cd:4593:2a8e with SMTP id 98e67ed59e1d1-2db9ff9038amr12989248a91.15.1726346834968; Sat, 14 Sep 2024 13:47:14 -0700 (PDT) MIME-Version: 1.0 From: Florent Michel Date: Sat, 14 Sep 2024 21:46:59 +0100 Message-ID: To: mailing list for ConTeXt users Message-ID-Hash: LVEU2LWZXHSHELFARBNAG5RHZFPOB2JH X-Message-ID-Hash: LVEU2LWZXHSHELFARBNAG5RHZFPOB2JH X-MailFrom: florent.m42@gmail.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.9 Precedence: list Reply-To: mailing list for ConTeXt users Subject: [NTG-context] Calling C code from ConTeXt LMTX List-Id: mailing list for ConTeXt users Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: multipart/mixed; boundary="===============0466897139581989450==" --===============0466897139581989450== Content-Type: multipart/alternative; boundary="000000000000db894006221a71b3" --000000000000db894006221a71b3 Content-Type: text/plain; charset="UTF-8" Hello, TL;DR: Could building LuaMetaTeX with the `-fPIC` flag for the Lua library have negative consequences? Is there a better way to be able to call external C functions from ConTeXt? I am working on a small project where it would be convenient to call functions written in C (possibly pre-compiled in a shared library) from ConTeXt. After looking at the Lua FFI, I found a way that seems to work (with ConTeXt LMTX 2024-08-16) but requires a small tweak to the cmake file used for building the Lua library. I would be grateful to have the opinion of people more knowledgeable than I am about whether this change could have negative consequences and whether there is a more straightforward way to call external C code. What I am trying to achieve is: * Compile some C code in a shared library (for instance, it may contain a numerical solver for some partial differential equation). * Have this code run when compiling a document with ConTeXt. (For instance, solving the differential equation with parameters defined in the .tex file and retrieving the results to plot a figure with MetaPost.) One easy way to do that is: * Write wrappers for the functions that will need to be called from ConTeXt using the Lua C API. * Compile the code and wrappers in a shared library and linking to the static lua library used by LuaMetaTeX (liblua.a on Linux). * In the .tex file, import the new library using, e.g., ``` \startluacode mylib = require("mylib") -- additional code using the C functions \stopluacode ``` * Run ConTeXt with the `--permitloadlib` flag. However, the second step requires (at least on Linux) liblua.a to be compiled with the `-fPIC` flag. I could do that by adding ``` set_property(TARGET lua PROPERTY POSITION_INDEPENDENT_CODE ON) ``` at line 47 in lua.cmake. Is there any known negative effect of doing that (apart from a possible small performance drop)? (Just to be clear, this is not a feature request; I'm just wondering whether doing it on my side is fine.) On a related note, is there a standard way to call external C functions (either in a shared library or in a .c file) from ConTeXt LMTX? The SwigLib library described in https://www.pragma-ade.nl/general/manuals/swiglib-mkiv.pdf looks very promising (and maybe could be a way to achieve what I'm trying to do in a much cleaner way), but I (maybe for lack of trying hard enough) have not been able to make it work with ConTeXt LMTX so far. Best regards, Florent --000000000000db894006221a71b3 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello,=C2=A0

TL;DR: Could building LuaMetaTeX with = the `-fPIC` flag for the Lua library have negative consequences? Is there a= better way to be able=C2=A0to call external C functions from ConTeXt?
<= br>I am working on a small project where it would be convenient to call fun= ctions written in C (possibly=C2=A0pre-compiled in a shared library) from C= onTeXt. After looking at=C2=A0the Lua FFI, I found a way that=C2=A0seems to= work (with ConTeXt LMTX 2024-08-16) but requires a small tweak to the cmak= e file used for building the Lua library. I would be grateful to=C2=A0have = the=C2=A0opinion of people more knowledgeable than I am about whether this= =C2=A0change could have negative consequences and whether there=C2=A0is a m= ore straightforward=C2=A0way to call external C code.

What I am tryi= ng to achieve is:=C2=A0
* Compile some C code in a shared library (for i= nstance, it may contain a numerical solver for some partial differential eq= uation).
* Have this code run when compiling a document with ConTeXt. (F= or instance, solving the differential equation with parameters defined in t= he .tex file and retrieving=C2=A0the results to=C2=A0plot a figure with Met= aPost.)

One easy way to do that is:=C2=A0
* Write wrappers for th= e functions that will need=C2=A0to be called from ConTeXt using the Lua=C2= =A0C API.
* Compile the code and wrappers in a shared=C2=A0library and l= inking=C2=A0to=C2=A0the static lua library used by LuaMetaTeX (liblua.a on = Linux).
* In the .tex file, import the new library using, e.g.,
```\startluacode
=C2=A0 =C2=A0 mylib =3D require("mylib")
= =C2=A0 =C2=A0 -- additional code using the C functions
\stopluacode
`= ``
* Run ConTeXt with the `--permitloadlib` flag.

However, the se= cond step requires (at least on Linux) liblua.a to be compiled with the `-f= PIC` flag. I could do that by adding
```
set_property(TARGET lua PROP= ERTY POSITION_INDEPENDENT_CODE ON)
```
at line=C2=A047 in lua.cmake. = Is there any known negative effect of doing=C2=A0that (apart from a possibl= e small=C2=A0performance drop)?
(Just to be clear, this is not a feature= request; I'm just wondering whether doing=C2=A0it on my side is fine.)=

On a related note, is there a standard=C2=A0way to call external C = functions (either in a shared library or in a .c file) from ConTeXt LMTX? T= he SwigLib library described in=C2=A0https://www.pragma-ade.nl/general/manuals/= swiglib-mkiv.pdf looks very promising (and maybe could be a way to achi= eve=C2=A0what I'm trying to do in a much cleaner way), but I (maybe for= lack of trying hard enough) have not been able to make it work with ConTeX= t LMTX so far.=C2=A0

Best regards,
Florent
--000000000000db894006221a71b3-- --===============0466897139581989450== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror) archive : https://github.com/contextgarden/context wiki : https://wiki.contextgarden.net ___________________________________________________________________________________ --===============0466897139581989450==--