Hello1

The attached patch tries to improve the way custom build scripts in
eggs are invoked, triggered by a discussion on #chicken regarding
how to add compiler options dynamically for egg sources.

The custom build command is now prefixed with the source directory
and made executable on UNIX. Also, PATH is set in .build.sh so that the proper
CHICKEN binaries are invoked from the build script.


felix

From 50f3aee33de22b426c2ddc0e44e81b40aa4797cd Mon Sep 17 00:00:00 2001
From: felix <[email protected]>
Date: Mon, 6 Nov 2017 17:29:20 +0100
Subject: [PATCH] Improve using custom build scripts

Refactored common "recursive" egg properties; custom build commands
are made executable and are prefixed with the source directory. Also
set PATH to bindir + "."
---
 chicken-install.scm |    1 +
 egg-compile.scm     |   74 +++++++++++++++++++++++++++++++--------------------
 manual/Extensions   |    8 ++++++
 3 files changed, 54 insertions(+), 29 deletions(-)

diff --git a/chicken-install.scm b/chicken-install.scm
index 9c2ba06..4c07c13 100644
--- a/chicken-install.scm
+++ b/chicken-install.scm
@@ -200,6 +200,7 @@
     (data #f #t #t)
     (modules #f #f #f)
     (c-include #f #f #t)
+    (only #f #t #t)
     (scheme-include #f #f #t)))
 
 (define (validate-egg-info info)
diff --git a/egg-compile.scm b/egg-compile.scm
index 55b19b2..48f5e10 100644
--- a/egg-compile.scm
+++ b/egg-compile.scm
@@ -151,8 +151,6 @@
       files)
     (define (compile-component info)
       (case (car info)
-        ((target) (when (eq? mode 'target) (for-each compile-component (cdr info))))
-        ((host) (when (eq? mode 'host) (for-each compile-component (cdr info))))
         ((extension)
           (fluid-let ((target (check-target (cadr info) exts))
                       (deps '())
@@ -269,13 +267,10 @@
 		(cons (list target dependencies: deps source: src options: opts
 			    link-options: lopts linkage: link custom: cbuild
 			    mode: mode output-file: rtarget)
-		      prgs)))))))
+		      prgs)))))
+        (else (compile-common info compile-component))))
     (define (compile-extension/program info)
       (case (car info)
-        ((target) 
-          (when (eq? mode 'target) (for-each compile-extension/program (cdr info))))
-        ((host) 
-          (when (eq? mode 'host) (for-each compile-extension/program (cdr info))))
         ((linkage) 
          (set! link (cdr info)))
         ((types-file)
@@ -303,22 +298,29 @@
         ((modules)
          (set! mods (map ->string (cdr info))))
         ((dependencies)
-         (set! deps (append deps (map ->dep (cdr info)))))))
+         (set! deps (append deps (map ->dep (cdr info)))))
+        (else (compile-common info compile-extension/program))))
+    (define (compile-common info walk)
+      (case (car info)
+        ((target) 
+         (when (eq? mode 'target) 
+           (for-each walk (cdr info))))
+        ((host) 
+         (when (eq? mode 'host)
+           (for-each walk (cdr info))))))
     (define (compile-data/include info)
       (case (car info)
-        ((target) (when (eq? mode 'target) (for-each compile-data/include (cdr info))))
-        ((host) (when (eq? mode 'host) (for-each compile-data/include (cdr info))))
         ((destination)
          (set! dest (->string (arg info 1 name?))))
         ((files) 
-         (set! files (append files (map ->string (cdr info)))))))
+         (set! files (append files (map ->string (cdr info)))))
+        (else (compile-common info compile-data/include))))
     (define (->dep x)
       (if (name? x) x (error "invalid dependency" x)))
     (define (compile info)
       (case (car info)
-        ((target) (when (eq? mode 'target) (for-each compile (cdr info))))
-        ((host) (when (eq? mode 'host) (for-each compile (cdr info))))
-        ((components) (for-each compile-component (cdr info)))))
+        ((components) (for-each compile-component (cdr info)))
+        (else (compile-common info compile))))
     (define (arg info n #!optional (pred (constantly #t)))
       (when (< (length info) n)
         (error "missing argument" info n))
@@ -440,7 +442,7 @@
                                    predefined-types
                                    custom types-file inline-file)
          srcdir platform)
-  (let* ((cmd (or (and custom (prefix-custom-command custom))
+  (let* ((cmd (or (and custom (prefix-custom-command srcdir custom))
                   default-csc))
          (sname (prefix srcdir name))
          (ssname (and source (prefix srcdir source)))
@@ -461,6 +463,8 @@
                                            (object-extension platform))
                                      mode)))
          (src (quotearg (or ssname (conc sname ".scm")))))
+    (when custom
+      (prepare-custom-command cmd platform))
     (print "\n" (slashify default-builder platform) " " out " " cmd 
            (if keep-generated-files " -k" "")
            " -setup-mode -static -I " srcdir 
@@ -478,7 +482,7 @@
                                     predefined-types
                                     custom types-file inline-file)
          srcdir platform)
-  (let* ((cmd (or (and custom (prefix-custom-command custom)) 
+  (let* ((cmd (or (and custom (prefix-custom-command srcdir custom)) 
                   default-csc))
          (sname (prefix srcdir name))
          (opts (append (if (null? options)
@@ -496,6 +500,8 @@
          (ssname (and source (prefix srcdir source)))
          (out (quotearg (target-file (conc sname ".so") mode)))
          (src (quotearg (or ssname (conc sname ".scm")))))
+    (when custom
+      (prepare-custom-command cmd platform))
     (print "\n" (slashify default-builder platform) " " out " " cmd 
            (if keep-generated-files " -k" "")
            (if (eq? mode 'host) " -host" "")
@@ -508,7 +514,7 @@
                                  (options '()) (link-options '())
                                  custom)
          srcdir platform)
-  (let* ((cmd (or (and custom (prefix-custom-command custom))
+  (let* ((cmd (or (and custom (prefix-custom-command srcdir custom))
                   default-csc))
          (sname (prefix srcdir name))
          (opts (if (null? options) 
@@ -516,6 +522,8 @@
                    options))
          (out (quotearg (target-file (conc sname ".import.so") mode)))
          (src (quotearg (conc sname ".import.scm"))))
+    (when custom
+      (prepare-custom-command cmd platform))
     (print "\n" (slashify default-builder platform) " " out " " cmd 
            (if keep-generated-files " -k" "")
            " -setup-mode -s"
@@ -528,7 +536,7 @@
                                      (options '()) (link-options '())
                                      custom)
          srcdir platform)
-  (let* ((cmd (or (and custom (prefix-custom-command custom))
+  (let* ((cmd (or (and custom (prefix-custom-command srcdir custom))
                   default-csc))
          (sname (prefix srcdir name))
          (ssname (and source (prefix srcdir source)))
@@ -539,6 +547,8 @@
                                            (executable-extension platform)) 
                                      mode)))
          (src (quotearg (or ssname (conc sname ".scm")))))
+    (when custom
+      (prepare-custom-command cmd platform))
     (print "\n" (slashify default-builder platform) " " out " " cmd 
            (if keep-generated-files " -k" "")
            " -setup-mode"
@@ -551,7 +561,7 @@
                                     (options '()) (link-options '())
                                     custom mode)
          srcdir platform)
-  (let* ((cmd (or (and custom (prefix-custom-command custom))
+  (let* ((cmd (or (and custom (prefix-custom-command srcdir custom))
                   default-csc))
          (sname (prefix srcdir name))
          (ssname (and source (prefix srcdir source)))
@@ -562,6 +572,8 @@
                                            (executable-extension platform)) 
                                      mode)))
          (src (quotearg (or ssname (conc sname ".scm")))))
+    (when custom
+      (prepare-custom-command cmd platform))
     (print "\n" (slashify default-builder platform) " " out " " cmd 
            (if keep-generated-files " -k" "")
            (if (eq? mode 'host) " -host" "")
@@ -572,10 +584,12 @@
 
 (define ((compile-generated-file name #!key dependencies source custom) 
          srcdir platform)
-  (let* ((cmd (prefix-custom-command custom))
+  (let* ((cmd (prefix-custom-command srcdir custom))
          (sname (prefix srcdir name))
          (ssname (and source (prefix srcdir source)))
          (out (quotearg (or ssname sname))))
+    (when custom
+      (prepare-custom-command cmd platform))
     (print "\n" (slashify default-builder platform)
            " " out " " cmd " : "
            #;(arglist dependencies))))
@@ -739,14 +753,16 @@
      (printf #<<EOF
 #!/bin/sh~%
 set -e
+PATH=.:"~a":$PATH
 
 EOF
-             ))
+             default-bindir))
     ((windows)
      (printf #<<EOF
 @echo off~%
+set PATH=.;~a;%PATH%
 EOF
-             ))))
+             default-bindir))))
 
 (define ((build-suffix mode name info) platform)
   (case platform
@@ -841,13 +857,13 @@ EOF
     ((unix) (string-append "${" var "}"))
     ((windows) (string-append "%" var "%"))))
   
-(define (prefix-custom-command cmd)
-  (cond ((irregex-match "^csi( .+)$" cmd) =>
-         (lambda (m) (string-append default-csi (irregex-match-substring m 1))))
-        ((irregex-match "^csc( .+)$" cmd) =>
-         (lambda (m) (string-append default-csc (irregex-match-substring m 1))))
-        ((irregex-match "^cc( .+)$" cmd) =>
+(define (prefix-custom-command srcdir cmd)
+  (cond ((irregex-match "^cc( .+)$" cmd) =>
          (lambda (m) (string-append default-cc (irregex-match-substring m 1))))
         ((irregex-match "^c\\+\\+( .+)$" cmd) =>
          (lambda (m) (string-append default-cxx (irregex-match-substring m 1))))
-        (else cmd)))
+        (else (prefix srcdir cmd))))
+
+(define (prepare-custom-command cmd platform)
+  (unless (eq? 'windows platform)
+    (print "chmod +x " (quotearg cmd))))
diff --git a/manual/Extensions b/manual/Extensions
index c73b5c4..a7930b0 100644
--- a/manual/Extensions
+++ b/manual/Extensions
@@ -77,6 +77,9 @@ can also be used to override include- and linker-paths. Each of these variables
 may contain one or more directory names, separated by {{:}} or {{;}} and will
 be passed using {{-I}} and {{-L}} to the C compiler.
 
+Finally, you can use the {{custom-build}} egg file property to use a
+custom script to compile your extension's code. See below for more information.
+
 === Creating eggs
 
 An egg can be created by placing its code and some special
@@ -319,6 +322,11 @@ instead of the default build operations. This property is mandatory
 for components of type {{generated-source-file}}. {{STRING}}
 usually contains a shell command and thus may be platform sensitive.
 
+The script will be made executable on UNIX systems, if necessary,
+and will be invoked like the {{csc}} program and
+is executed with the build directory and the location of the CHICKEN
+binaries in the {{PATH}}.
+
 ====== csc-options
 
 [egg property] (csc-options OPTION ...)
-- 
1.7.9.5

_______________________________________________
Chicken-hackers mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/chicken-hackers

Reply via email to