Add supporting tools for the GNU Build System.
[jackhill/guix/guix.git] / guix / build / utils.scm
CommitLineData
c36db98c
LC
1;;; Guix --- Nix package management from Guile. -*- coding: utf-8 -*-
2;;; Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
3;;;
4;;; This file is part of Guix.
5;;;
6;;; Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (guix build utils)
20 #:use-module (srfi srfi-1)
21 #:export (directory-exists?
22 set-path-environment-variable))
23
24(define (directory-exists? dir)
25 "Return #t if DIR exists and is a directory."
26 (pk 'dir-exists? dir
27 (let ((s (pk 'stat dir (stat dir #f))))
28 (and s
29 (eq? 'directory (stat:type s))))))
30
31(define (search-path-as-list sub-directories input-dirs)
32 "Return the list of directories among SUB-DIRECTORIES that exist in
33INPUT-DIRS. Example:
34
35 (search-path-as-list '(\"share/emacs/site-lisp\" \"share/emacs/24.1\")
36 (list \"/package1\" \"/package2\" \"/package3\"))
37 => (\"/package1/share/emacs/site-lisp\"
38 \"/package3/share/emacs/site-lisp\")
39
40"
41 (append-map (lambda (input)
42 (filter-map (lambda (dir)
43 (let ((dir (string-append input "/"
44 dir)))
45 (and (directory-exists? dir)
46 dir)))
47 sub-directories))
48 input-dirs))
49
50(define (list->search-path-as-string lst separator)
51 (string-join lst separator))
52
53(define* (set-path-environment-variable env-var sub-directories input-dirs
54 #:key (separator ":"))
55 "Look for each of SUB-DIRECTORIES in INPUT-DIRS. Set ENV-VAR to a
56SEPARATOR-separated path accordingly. Example:
57
58 (set-path-environment-variable \"PKG_CONFIG\"
59 '(\"lib/pkgconfig\")
60 (list package1 package2))
61"
62 (setenv env-var
63 (list->search-path-as-string (search-path-as-list sub-directories
64 input-dirs)
65 separator)))