packages: Add a `self-native-input?' field.
[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."
9f55cf8d
LC
26 (let ((s (stat dir #f)))
27 (and s
28 (eq? 'directory (stat:type s)))))
c36db98c
LC
29
30(define (search-path-as-list sub-directories input-dirs)
31 "Return the list of directories among SUB-DIRECTORIES that exist in
32INPUT-DIRS. Example:
33
34 (search-path-as-list '(\"share/emacs/site-lisp\" \"share/emacs/24.1\")
35 (list \"/package1\" \"/package2\" \"/package3\"))
36 => (\"/package1/share/emacs/site-lisp\"
37 \"/package3/share/emacs/site-lisp\")
38
39"
40 (append-map (lambda (input)
41 (filter-map (lambda (dir)
42 (let ((dir (string-append input "/"
43 dir)))
44 (and (directory-exists? dir)
45 dir)))
46 sub-directories))
47 input-dirs))
48
49(define (list->search-path-as-string lst separator)
50 (string-join lst separator))
51
52(define* (set-path-environment-variable env-var sub-directories input-dirs
53 #:key (separator ":"))
54 "Look for each of SUB-DIRECTORIES in INPUT-DIRS. Set ENV-VAR to a
55SEPARATOR-separated path accordingly. Example:
56
57 (set-path-environment-variable \"PKG_CONFIG\"
58 '(\"lib/pkgconfig\")
59 (list package1 package2))
60"
61 (setenv env-var
62 (list->search-path-as-string (search-path-as-list sub-directories
63 input-dirs)
64 separator)))