Changeset 35670 in project


Ignore:
Timestamp:
06/22/18 02:27:13 (6 months ago)
Author:
iraikov
Message:

added useful function try-compile to compile-file

Location:
release/5/compile-file/trunk
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • release/5/compile-file/trunk/compile-file.scm

    r35590 r35670  
    66
    77(module compile-file
    8   (compile-file compile-file-options)
     8  (compile-file compile-file-options try-compile)
    99
    1010(import (chicken base)
     
    5454                       (load-file f)
    5555                       f))))
    56                 (else #f))))))))
     56                (else #f)))))))
     57
     58(define default-cxx (foreign-value "C_TARGET_CXX" c-string))
     59(define default-cc (foreign-value "C_TARGET_CC" c-string))
     60(define default-cflags (foreign-value "C_TARGET_CFLAGS" c-string))
     61(define default-ldflags (foreign-value "C_TARGET_LDFLAGS" c-string))
     62(define default-libdir (foreign-value "C_TARGET_LIB_HOME" c-string))
     63(define default-libs (foreign-value "C_TARGET_MORE_LIBS" c-string))
     64
     65(define remove-file-command
     66  (case (software-type)
     67    ((unix) "rm -f")
     68    ((windows) "del /f /q")
     69    (else "rm -f")))
     70
     71(define (shellpath str)
     72  (qs (normalize-pathname str)))
     73
     74(define (try-compile code #!key c++ (cc (if c++ default-cxx default-cc)) (cflags "") (ldflags "")
     75                     (verbose #f) (compile-only #f))
     76  (let* ((fname (create-temporary-file "c"))
     77         (oname (pathname-replace-extension fname "o"))
     78         (r (begin
     79              (with-output-to-file fname (cut display code))
     80              (system
     81               (let ((cmd (conc
     82                           cc " "
     83                           (if compile-only "-c" "") " "
     84                           cflags " " default-cflags " "
     85                           (shellpath fname) " -o " (shellpath oname) " "
     86                           (if compile-only
     87                               ""
     88                               (conc "-L" default-libdir " " ldflags " " default-libs) )
     89                           (case (software-type) ((windows) " >nul: " " >/dev/null ") (else ""))
     90                           (if verbose "" "2>&1") ) ) )
     91                 (when verbose (print cmd " ..."))
     92                 cmd) ) ) ) )
     93    (when verbose (print (if (zero? r) "succeeded." "failed.")))
     94    (system (sprintf "~A ~A" remove-file-command (shellpath fname)))
     95    (system (sprintf "~A ~A" remove-file-command (shellpath oname)))
     96    (zero? r) ) )
     97
     98)
  • release/5/compile-file/trunk/compile-file.wiki

    r33882 r35670  
    55[[toc:]]
    66
    7 {{compile-file}} provides a way to compile Scheme files
    8 programmatically.
     7{{compile-file}} provides a way to compile Scheme and C/C++ files programmatically.
    98
    109==== compile-file
     
    4039default options are {{-O2 -d2}}.
    4140
     41==== try-compile
     42
     43<procedure>(try-compile CODE #!key cc cflags ldflags compile-only c++)</procedure>
     44
     45Returns {{#t}} if the C code in {{CODE}} compiles and links successfully, or {{#f}} otherwise.
     46The keyword parameters {{cc}} (compiler name, defaults to the C compiler used to build this system),
     47{{cflags}} and {{ldflags}} accept additional compilation and
     48linking options. If {{compile-only}} is true, then no linking step takes place.
     49If the keyword argument {{c++}} is given and true, then the code will be compiled in C++ mode.
     50
    4251=== Author
    4352
     
    4655=== License
    4756
    48   Copyright (c) 2017, The CHICKEN Team
     57  Copyright (c) 2017-2018, The CHICKEN Team
    4958  All rights reserved.
    5059 
Note: See TracChangeset for help on using the changeset viewer.