From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Maxim Cournoyer Newsgroups: gmane.lisp.guile.bugs Subject: bug#66046: [PATCH v4 3/3] ice-9: Fix 'include' when used in compilation contexts. Date: Sat, 14 Sep 2024 10:34:29 +0900 Message-ID: <20240914013501.6445-3-maxim.cournoyer@gmail.com> References: <20240914013501.6445-1-maxim.cournoyer@gmail.com> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25391"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Timothy Sample , Amirouche , Maxim Cournoyer , Daphne Preston-Kendal To: 66046@debbugs.gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Sat Sep 14 03:37:37 2024 Return-path: Envelope-to: guile-bugs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1spHjE-0006RZ-TY for guile-bugs@m.gmane-mx.org; Sat, 14 Sep 2024 03:37:37 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1spHid-0003ld-Rj; Fri, 13 Sep 2024 21:36:59 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1spHiY-0003ks-33 for bug-guile@gnu.org; Fri, 13 Sep 2024 21:36:55 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1spHiX-0001Yf-CH for bug-guile@gnu.org; Fri, 13 Sep 2024 21:36:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:References:In-Reply-To:Date:From:To:Subject; bh=XtN6jyhz3HEO8LSqqtB0MoKEim0CLIk/N+iFpFFXCYc=; b=crYyXYJ9FmZR2YjkSsf/AhT0rkDAbfAEjv2dyDDN7FT8x9ZuIsQAdvf0DdK0XXqNT2Re7FQWPTH2hFX/UaylDORBPGhja2FhIWJR1mieQoVhPRjS1p3sxzB3H95YhI+JhkJd0d49KsqayMRu0Qfd59D75ZyS0hlQSvIeBIwmRK72uq7Ej3CkV9yAM3K7+gv7YDZC02ggksd5lP07rpy0DMOxZqgOjxatUTSBr6NZAsqNxyB6KeAJExvX2nEEEuQErNN47D3uxDjSUT0p1+DQNKIepfsoQob7o2txKtUz8mYRp3o4X13vih25fwS/1e7wkX0rp7JgpQQpTpuq0OozfA==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1spHih-0007oc-5g for bug-guile@gnu.org; Fri, 13 Sep 2024 21:37:03 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Sat, 14 Sep 2024 01:37:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 66046 X-GNU-PR-Package: guile Original-Received: via spool by 66046-submit@debbugs.gnu.org id=B66046.172627779729993 (code B ref 66046); Sat, 14 Sep 2024 01:37:03 +0000 Original-Received: (at 66046) by debbugs.gnu.org; 14 Sep 2024 01:36:37 +0000 Original-Received: from localhost ([127.0.0.1]:44485 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1spHiG-0007ng-PB for submit@debbugs.gnu.org; Fri, 13 Sep 2024 21:36:37 -0400 Original-Received: from mail-pl1-f179.google.com ([209.85.214.179]:59522) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1spHiD-0007n2-IY for 66046@debbugs.gnu.org; Fri, 13 Sep 2024 21:36:34 -0400 Original-Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2053f6b8201so25896885ad.2 for <66046@debbugs.gnu.org>; Fri, 13 Sep 2024 18:36:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726277717; x=1726882517; darn=debbugs.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XtN6jyhz3HEO8LSqqtB0MoKEim0CLIk/N+iFpFFXCYc=; b=liGAAJfURTbAJlzGHzcRWK8+mANCIgxtBptwLn8B7Kzu05JJhaNierPrECGgiyXG11 Ap+iSwNZUreqQk1s1b9oMd5gPWOfI9+A4TTHb4TUJ8r6Yv8dZng83eh78feJYsvWyys9 e3/iAg40v8SKzb4dXc4X8nsOJJIlAt0QXKPL5LXucrDxCcQgjAbO9hy0XWWr+8mrg6CT eON6I/pPF+8RKenK1YsyPTW93QhCn+DLs2YkssbfEUd99jLRJs1+9atUJwbRruPL17RU 4I342/A/vSkBagTkrgqHu6n1VK8XFhucKmFV5iKfvOHdUfJYte/CygpEPsOfBPtN5NPV iXcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726277717; x=1726882517; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XtN6jyhz3HEO8LSqqtB0MoKEim0CLIk/N+iFpFFXCYc=; b=pinoUIIksNKL2k1AfOPDscflNmmMf7iySmoL6rqAU92E/cxNIlGEIIqTdCRtI2UNp9 jZWgyxpmZloSqe7OjBJOcSh/cXBZKYCuMxZF2baR2efC9KV79oo5WgzK0tc3CdcTDEUs CBKAMo/aMdLizT1X5FrSJmO8IZ87bX/SfC52EYaRuUoizCS/2bIaK8jpyjyqo2oD1RAf /UWFRHX19x11zoyO0b7WfVESASYKll9ajjmhqvnyBuIQoX3VocO9GJwErnqS7uzWiDDo YSf3GSXcS0nZQk5BVWokard+oJ0hWoQg6GxKqhy5iNhSLuetcwjcd6AbZYChincdYpwP aFBw== X-Gm-Message-State: AOJu0YyfWHpgiKyzKUWcUsqniUtFcvLdkNP/pFPV9nM0bhZLYAAxOWGC 80SMG0QuAVjDaltSxaOD/jIttdx7tLHORNF3AvJPlWtyGK0qfc2hLylpKJVN X-Google-Smtp-Source: AGHT+IFlaYg4nU9zTcJnnhrTos14yVtSMfX7+skpWSBDNFqB8sOTnv75hAj4iM6pdaih34aPPDRhpg== X-Received: by 2002:a17:902:d510:b0:206:b4cf:3107 with SMTP id d9443c01a7336-2076e422055mr135988795ad.49.1726277716851; Fri, 13 Sep 2024 18:35:16 -0700 (PDT) Original-Received: from hurd.lan ([2405:6586:be0:0:c8ff:1707:9b9:af89]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-207945da802sm2164505ad.50.2024.09.13.18.35.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Sep 2024 18:35:16 -0700 (PDT) X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240914013501.6445-1-maxim.cournoyer@gmail.com> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Original-Sender: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.bugs:10977 Archived-At: Fixes bug #66046. Introduce a '%file-port-stripped-prefixes' fluid that captures the pre-canonicalized file name used when compiling a file, before it gets modified in fport_canonicalize_filename. That reference that can then used by 'include' when searching for included files. * libguile/fports.c (sys_file_port_stripped_prefixes): New C fluid. (fport_canonicalize_filename): Register dirnames / stripped prefixes pairs in. (%file-port-stripped-prefixes): New corresponding Scheme fluid. * module/ice-9/boot-9.scm (call-with-include-port): New procedure, shadowing that from psyntax, that extends it to use the above fluid to compute a fallback include file directory name to try. * module/ice-9/psyntax.scm (call-with-include-port): Add comment. Strip documentation, as it's now an internal. --- Changes in v4: - Rebase & add NEWS entry Changes in v3: - Move tests hunks to test commit Changes in v2: - Move fluid to where the file name stripping happens, in libguile - Make the fluid value an alist of the last 100 stripped prefixes - Expound test to catch edge case (include in an include) NEWS | 2 ++ libguile/fports.c | 41 +++++++++++++++++++++++++-- module/ice-9/boot-9.scm | 61 ++++++++++++++++++++++++++++++++++++++++ module/ice-9/psyntax.scm | 8 ++---- 4 files changed, 104 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index 03bc819bc..8fe6ff6f9 100644 --- a/NEWS +++ b/NEWS @@ -20,6 +20,8 @@ Changes in 3.0.11 (since 3.0.10) ** test-hashing should now work on 32-bit systems ** GUILE-VERSION changes should propagate to .version and relevant Makefiles () +** Fix 'include' not finding included files when byte compiling Guile + () Changes in 3.0.10 (since 3.0.9) diff --git a/libguile/fports.c b/libguile/fports.c index 8f19216b7..12048828a 100644 --- a/libguile/fports.c +++ b/libguile/fports.c @@ -1,4 +1,4 @@ -/* Copyright 1995-2004,2006-2015,2017-2020,2022 +/* Copyright 1995-2004,2006-2015,2017-2020,2022-2023 Free Software Foundation, Inc. This file is part of Guile. @@ -43,6 +43,7 @@ #include #include +#include "alist.h" #include "async.h" #include "boolean.h" #include "dynwind.h" @@ -59,6 +60,7 @@ #include "ports-internal.h" #include "posix.h" #include "read.h" +#include "srfi-13.h" #include "strings.h" #include "symbols.h" #include "syscalls.h" @@ -123,6 +125,7 @@ SCM_DEFINE (scm_file_port_p, "file-port?", 1, 0, 0, static SCM sys_file_port_name_canonicalization; +static SCM sys_file_port_stripped_prefixes; static SCM sym_relative; static SCM sym_absolute; @@ -143,7 +146,34 @@ fport_canonicalize_filename (SCM filename) "%load-path")); rel = scm_i_relativize_path (filename, path); - return scm_is_true (rel) ? rel : filename; + if (scm_is_true (rel)) + { + SCM relative_dir = scm_dirname (rel); + SCM stripped_prefixes = scm_fluid_ref + (sys_file_port_stripped_prefixes); + + /* Extend the association list if needed, but keep its size + capped to limit memory usage. */ + if (scm_is_false (scm_assoc_ref(stripped_prefixes, relative_dir))) + { + SCM stripped_prefix = scm_string_drop_right + (filename, scm_string_length (rel)); + + stripped_prefixes = scm_cons (scm_cons (relative_dir, + stripped_prefix), + stripped_prefixes); + + if (scm_to_int (scm_length (stripped_prefixes)) > 100) + stripped_prefixes = scm_list_head (stripped_prefixes, + scm_from_int(100)); + + scm_fluid_set_x (sys_file_port_stripped_prefixes, + stripped_prefixes); + } + + return rel; + } + return filename; } else if (scm_is_eq (mode, sym_absolute)) { @@ -766,4 +796,11 @@ scm_init_fports () sys_file_port_name_canonicalization = scm_make_fluid (); scm_c_define ("%file-port-name-canonicalization", sys_file_port_name_canonicalization); + + /* Used by `include' to locate the true source when relative + canonicalization strips a leading part of the source file. */ + sys_file_port_stripped_prefixes = scm_make_fluid_with_default (SCM_EOL); + + scm_c_define ("%file-port-stripped-prefixes", + sys_file_port_stripped_prefixes); } diff --git a/module/ice-9/boot-9.scm b/module/ice-9/boot-9.scm index 627910ad9..9da5a4a74 100644 --- a/module/ice-9/boot-9.scm +++ b/module/ice-9/boot-9.scm @@ -2030,6 +2030,67 @@ non-locally, that exit determines the continuation." +;;; {Include} +;;; + +;;; This redefined version of call-with-include-port (first defined in +;;; psyntax.scm) also try to locate an included file using the +;;; %file-port-stripped-prefixes fluid. +(define call-with-include-port + (let ((syntax-dirname (lambda (stx) + (define src (syntax-source stx)) + (define filename (and src (assq-ref src 'filename))) + (and (string? filename) + (dirname filename))))) + (lambda* (filename proc #:key (dirname (syntax-dirname filename))) + "Like @code{call-with-input-file}, except relative paths are +searched relative to @var{dirname} instead of the current working +directory. Also, @var{filename} can be a syntax object; in that case, +and if @var{dirname} is not specified, the @code{syntax-source} of +@var{filename} is used to obtain a base directory for relative file +names. As a special case, when the @var{%file-port-stripped-prefixes} +fluid is set, its value is searched for a directory matching the dirname +inferred from FILENAME." + (let* ((filename (syntax->datum filename)) + (candidates + (cond ((absolute-file-name? filename) + (list filename)) + (dirname ;filename is relative + (let* ((rel-names (fluid-ref %file-port-stripped-prefixes)) + (stripped-prefix (and rel-names + (assoc-ref rel-names dirname))) + (fallback (and stripped-prefix + (string-append stripped-prefix + dirname)))) + (map (lambda (d) + (in-vicinity d filename)) + `(,dirname ,@(if fallback + (list fallback) + '()))))) + (else + (error + "attempt to include relative file name \ +but could not determine base dir")))) + (p (let loop ((files candidates)) + (when (null? files) + (error "could not open any of" candidates)) + (catch 'system-error + (lambda _ + (open-input-file (car files))) + (lambda _ + (loop (cdr files)))))) + (enc (file-encoding p))) + + ;; Choose the input encoding deterministically. + (set-port-encoding! p (or enc "UTF-8")) + + (call-with-values (lambda () (proc p)) + (lambda results + (close-port p) + (apply values results))))))) + + + ;;; {Time Structures} ;;; diff --git a/module/ice-9/psyntax.scm b/module/ice-9/psyntax.scm index 5fbd8f458..34207b38f 100644 --- a/module/ice-9/psyntax.scm +++ b/module/ice-9/psyntax.scm @@ -3270,6 +3270,8 @@ ;; Scheme code corresponding to the intermediate language forms. ((_ e) (emit (quasi #'e 0))))))) +;; Note: this procedure is later refined in ice-9/boot-9.scm after we +;; have basic exception handling. (define call-with-include-port (let ((syntax-dirname (lambda (stx) (define src (syntax-source stx)) @@ -3277,12 +3279,6 @@ (and (string? filename) (dirname filename))))) (lambda* (filename proc #:key (dirname (syntax-dirname filename))) - "Like @code{call-with-input-file}, except relative paths are -searched relative to the @var{dirname} instead of the current working -directory. Also, @var{filename} can be a syntax object; in that case, -and if @var{dirname} is not specified, the @code{syntax-source} of -@var{filename} is used to obtain a base directory for relative file -names." (let* ((filename (syntax->datum filename)) (p (open-input-file (cond ((absolute-file-name? filename) -- 2.46.0