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.devel Subject: [PATCH v6 01/16] ice-9: Fix 'include' when used in compilation contexts. Date: Sat, 2 Dec 2023 20:37:02 -0500 Message-ID: <20231203014115.26985-2-maxim.cournoyer@gmail.com> References: <20231203014115.26985-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="32053"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Maxim Cournoyer To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Sun Dec 03 02:43:13 2023 Return-path: Envelope-to: guile-devel@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 1r9bVp-0008Dt-CM for guile-devel@m.gmane-mx.org; Sun, 03 Dec 2023 02:43:13 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r9bUD-0001LL-22; Sat, 02 Dec 2023 20:41:33 -0500 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 1r9bU9-0001Jp-EP for guile-devel@gnu.org; Sat, 02 Dec 2023 20:41:29 -0500 Original-Received: from mail-qk1-x72b.google.com ([2607:f8b0:4864:20::72b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1r9bU5-0001Vt-JT for guile-devel@gnu.org; Sat, 02 Dec 2023 20:41:29 -0500 Original-Received: by mail-qk1-x72b.google.com with SMTP id af79cd13be357-77d66c7af31so374474885a.1 for ; Sat, 02 Dec 2023 17:41:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701567683; x=1702172483; darn=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=cRq9hLrmUsjTKcZUSBgZqAeg4OdwB5lc0HukScEEu0U=; b=bVhrsGxfKDJtlUKN9ln/OcCpIPvuJo2W3h/+1bQEkb0u3iIhdCGKmLqRHte3X1AavH 6HZwRkrpZ5yViRfk2vX+KF0XHI5VnuA+MPB/SKdaDeROPU5oM9qq/aMjJ7h6m+DLnnBm YIWalUx3ySbwsecunoNAyTVSHT7/pTQI/X/Nw+X7Rgbp28jZbgKpOh9vd/8BtKhC5dh6 PQn3Wk8DX+1j8dpV9ZmZKPe6MQZB6DP/qWcEg6cf14vL1yfEqjQbhYNN4PygvA4KiedS G4EYQAlgITk3yfhb0QsXlbT7uThutNY4yBvigf/nMQgjkXOi5JW7RzBMqE1tSPthU/5m MRCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701567683; x=1702172483; 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=cRq9hLrmUsjTKcZUSBgZqAeg4OdwB5lc0HukScEEu0U=; b=Jj+pD0VxT4LoFTcKB3O3Tvh6VmeiaCzpqpaNlbHIzdkO0abTytyKU26+vK2QKDxhjB kAIEvIxw7i35f+YkXNmwVYIzMjxeX93rWjZZj3CizET3XzsF6iEdmbIAQ7WbKJF1lngw 6fLKVV9gUVP6/rFHNziq2nYXQ1lJ23bWiCSlrUFJiONfHwJkmACz/VzxCHc4+0pR8EJU C1ir8j1/WULNJF0r8hkVywQO43ZxXSI5HeVnZsDVmdSIqrHrizLLKLQPwoOKGsP+LdPS qU8d3k7bZSRRfWQXUKLrQDqSiSZ+B6U/aT0j998uGwYV8APEJ7yjfbemY6WIuPR1R+3r DuxQ== X-Gm-Message-State: AOJu0YzEDSVUe4SUd0z81xcwNA4dvYWTPIntGizDJ9e2iXp8lR2zN2pi +tl16X79aqUm8sxSLjnsDSVzvvtviEA= X-Google-Smtp-Source: AGHT+IF5Jco+GKeBXJHjKQ8K+ba9euCzvX9YovWoDe+FlsGjhVr5ocIbevmzUFe+NWhPf9G1T3JKzA== X-Received: by 2002:a05:620a:31aa:b0:77e:fbba:6470 with SMTP id bi42-20020a05620a31aa00b0077efbba6470mr2898281qkb.79.1701567683598; Sat, 02 Dec 2023 17:41:23 -0800 (PST) Original-Received: from localhost.localdomain (dsl-10-141-196.b2b2c.ca. [72.10.141.196]) by smtp.gmail.com with ESMTPSA id t15-20020a05620a450f00b0077dca8ed9a4sm2869664qkp.133.2023.12.02.17.41.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Dec 2023 17:41:23 -0800 (PST) X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231203014115.26985-1-maxim.cournoyer@gmail.com> Received-SPF: pass client-ip=2607:f8b0:4864:20::72b; envelope-from=maxim.cournoyer@gmail.com; helo=mail-qk1-x72b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.devel:22172 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. * NEWS: Mention bug fix. --- (no changes since v1) NEWS | 3 ++ libguile/fports.c | 41 +++++++++++++++++++++++++-- module/ice-9/boot-9.scm | 61 ++++++++++++++++++++++++++++++++++++++++ module/ice-9/psyntax.scm | 8 ++---- 4 files changed, 105 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index b319404d7..6676c5715 100644 --- a/NEWS +++ b/NEWS @@ -48,6 +48,9 @@ a buffer overrun, and so might vary. This problem affected a number of other operations, given the internal use of those functions. +** Fix 'include' not finding included files when byte compiling Guile + () + Changes in 3.0.9 (since 3.0.8) diff --git a/libguile/fports.c b/libguile/fports.c index 9d4ca6ace..419e9ee3f 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" @@ -60,6 +61,7 @@ #include "ports-internal.h" #include "posix.h" #include "read.h" +#include "srfi-13.h" #include "strings.h" #include "symbols.h" #include "syscalls.h" @@ -124,6 +126,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; @@ -144,7 +147,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)) { @@ -767,4 +797,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 a5f2eea9b..a79d49ae1 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 7811f7118..0e0370457 100644 --- a/module/ice-9/psyntax.scm +++ b/module/ice-9/psyntax.scm @@ -3256,6 +3256,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)) @@ -3263,12 +3265,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.41.0