aboutsummaryrefslogtreecommitdiff
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018 Marius Bakke <mbakke@fastmail.com>
;;; Copyright © 2020 Tobias Geerinckx-Rice <me@tobias.gr>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu packages stb)
  #:use-module (guix packages)
  #:use-module (guix git-download)
  #:use-module (guix build-system gnu)
  #:use-module (guix build-system trivial)
  #:use-module ((guix licenses) #:select (expat public-domain)))

(define stb
  ;; stb is a collection of libraries developed within the same repository.
  ;; When updating this, remember to change versions below as appropriate.
  (let ((commit "b42009b3b9d4ca35bc703f5310eedc74f584be58")
        (revision "2"))
    (package
      (name "stb")
      (home-page "https://github.com/nothings/stb")
      (version (git-version "0.0" revision commit))
      (source (origin
                (method git-fetch)
                (uri (git-reference
                      (url home-page)
                      (commit commit)))
                (sha256
                 (base32
                  "1gmcjhmj62mfdscrsg2hv4j4j9v447y8zj3rbrm7mqn94cx73z1i"))
                (file-name (git-file-name name version))))
      (build-system gnu-build-system)
      (arguments
       `(#:modules ((ice-9 ftw)
                    (ice-9 regex)
                    (srfi srfi-26)
                    ,@%default-gnu-modules)
         #:phases (modify-phases %standard-phases
                    (delete 'configure)
                    (delete 'build)
                    (replace 'check
                      (lambda _
                        (invoke "make" "-C" "tests" "CC=gcc")))
                    (replace 'install
                      (lambda* (#:key outputs #:allow-other-keys)
                        (let ((out (assoc-ref outputs "out"))
                              (files (make-regexp "\\.(c|h|md)$")))
                          (for-each (lambda (file)
                                      (install-file file out))
                                    (scandir "." (cut regexp-exec files <>)))
                          #t))))))
      (synopsis "Single file libraries for C/C++")
      (description
       "This package contains a variety of small independent libraries for
the C programming language.")
      ;; The user can choose either license.
      (license (list expat public-domain)))))

(define (make-stb-header-package name version description)
  (package
    (inherit stb)
    (name name)
    (version version)
    (source #f)
    (inputs (list stb))
    (build-system trivial-build-system)
    (arguments
     `(#:modules ((guix build utils))
       #:builder (begin
                   (use-modules (guix build utils))
                   (let ((stb (assoc-ref %build-inputs "stb"))
                         (lib (string-join (string-split ,name #\-) "_"))
                         (out (assoc-ref %outputs "out")))
                     (install-file (string-append stb "/" lib ".h")
                                   (string-append out "/include"))
                     #t))))
    (description description)))

;; TODO: These descriptions are not translatable!  They should be
;; converted to macros as outlined in <https://bugs.gnu.org/32155>.
(define-public stb-image
  (make-stb-header-package
   "stb-image" "2.26"
   "stb-image is a small and self-contained library for image loading or
decoding from file or memory.  A variety of formats are supported."))

(define-public stb-image-resize
  (make-stb-header-package
   "stb-image-resize" "0.96"
   "stb-image-resize is a library that supports scaling and translation of
images."))

(define-public stb-image-write
  (make-stb-header-package
   "stb-image-write" "1.15"
   "stb-image-write is a small library for writing image files to the
C@tie{}@code{stdio} interface."))

(define-public stb-rect-pack
  (make-stb-header-package
   "stb-rect-pack" "1.00"
   "stb-rect-pack is a small rectangle packing library useful for, e.g., packing
rectangular textures into an atlas.  It does not do rotation."))

(define-public stb-sprintf
  (make-stb-header-package
   "stb-sprintf" "1.09"
   "stb-sprintf implements fast @code{sprintf}, @code{snprintf} for C/C++."))

(define-public stb-truetype
  (make-stb-header-package
   "stb-truetype" "1.24"
   "stb-truetype is a library for parsing, decoding, and rasterizing
characters from TrueType fonts."))
href='#n158'>158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2022 Mathieu Othacehe <othacehe@gnu.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu tests image)
  #:use-module (gnu)
  #:use-module (gnu image)
  #:use-module (gnu tests)
  #:autoload   (gnu system image) (system-image root-offset image-with-os efi-disk-image)
  #:use-module (gnu system uuid)
  #:use-module (gnu system vm)
  #:use-module (gnu packages guile)
  #:use-module (gnu packages guile-xyz)
  #:use-module (guix gexp)
  #:use-module (guix monads)
  #:use-module (ice-9 format)
  #:export (%test-images))

;;; Commentary:
;;;
;;; This module provides tests for the image creation process that is
;;; performed by "genimage" under the hood.
;;;
;;; The image partitionment is checked using Guile-Parted.  The content of the
;;; images is out of the scope of this module.  Other test modules such as
;;; (gnu tests installation) make sure that the produced images are viable.
;;;
;;; Code:

;; A dummy initializer creating a simple file in the partition.
(define dummy-initializer
  #~(lambda* (root . rest)
      (mkdir root)
      (call-with-output-file
          (string-append root "/test")
        (lambda (port)
          (format port "content")))))

(define %simple-efi-os
  (operating-system
    (inherit %simple-os)
    (bootloader (bootloader-configuration
                 (bootloader grub-efi-bootloader)
                 (targets '("/boot/efi"))))))

;; An MBR disk image with a single ext4 partition.
(define i1
  (image
   (format 'disk-image)
   (operating-system %simple-os)
   (partitions
    (list
     (partition
      (size (* 1024 1024)) ;1MiB
      (offset root-offset)
      (label "test")
      (file-system "ext4")
      (flags '(boot))
      (initializer dummy-initializer))))))

;; A GPT disk image with a single ext4 partition.
(define i2
  (image
   (format 'disk-image)
   (operating-system %simple-efi-os)
   (partition-table-type 'gpt)
   (partitions
    (list
     (partition
      (size (* 1024 1024)) ;1MiB
      (offset root-offset)
      (label "test")
      (file-system "ext4")
      (flags '(boot))
      (initializer dummy-initializer))))))

;; An MBR disk image with multiple ext4 partitions.
(define i3
  (image
   (format 'disk-image)
   (operating-system %simple-os)
   (partitions
    (list
     (partition
      (size (* 1024 1024)) ;1MiB
      (offset root-offset)
      (label "test")
      (file-system "ext4")
      (flags '(boot))
      (initializer dummy-initializer))
     (partition
      (size (* 1024 1024)) ;1MiB
      (label "test2")
      (file-system "ext4")
      (flags '())
      (initializer dummy-initializer))))))

;; A GPT disk image with multiple ext4 partitions.
(define i4
  (image
   (format 'disk-image)
   (operating-system %simple-efi-os)
   (partition-table-type 'gpt)
   (partitions
    (list
     (partition
      (size (* 1024 1024)) ;1MiB
      (offset root-offset)
      (label "test")
      (file-system "ext4")
      (flags '(boot))
      (initializer dummy-initializer))
     (partition
      (size (* 1024 1024)) ;1MiB
      (label "test2")
      (file-system "ext4")
      (flags '())
      (initializer dummy-initializer))))))

;; A GPT disk image with fat32 and ext4 partitions.
(define i5
  (image
   (format 'disk-image)
   (operating-system %simple-efi-os)
   (partition-table-type 'gpt)
   (partitions
    (list
     (partition
      (size (* 1024 1024 128)) ;128MiB
      (offset root-offset)
      (label "test")
      (file-system "fat32")
      (flags '(esp))
      (initializer dummy-initializer))
     (partition
      (size (* 1024 1024 256)) ;256MiB
      (label "test2")
      (file-system "ext4")
      (flags '(boot))
      (initializer dummy-initializer))))))

;; A efi disk image with default partitions
(define i6
  (image-with-os efi-disk-image %simple-efi-os))

(define (run-images-test)
  (define test
    (with-imported-modules '((srfi srfi-64)
                             (gnu build marionette))
      (with-extensions (list guile-parted guile-bytestructures)
        #~(begin
            (use-modules (gnu build marionette)
                         (srfi srfi-1)
                         (srfi srfi-26)
                         (srfi srfi-64)
                         (parted))

            (define (image->disk img)
              (disk-new (get-device img)))

            (test-runner-current (system-test-runner #$output))
            (test-begin "images")

            ;; Image i1.
            (define i1-image
              #$(system-image i1))
            (define d1-device
              (get-device i1-image))

            (test-equal "msdos"
              (disk-type-name (disk-probe d1-device)))

            (test-equal 1
              (disk-get-primary-partition-count (disk-new d1-device)))

            (test-assert
                (let* ((disk (disk-new d1-device))
                       (partitions (disk-partitions disk))
                       (boot-partition (find normal-partition? partitions)))
                  (partition-get-flag boot-partition PARTITION-FLAG-BOOT)))

            ;; Image i2.
            (define i2-image
              #$(system-image i2))
            (define d2-device
              (get-device i2-image))

            (test-equal "gpt"
              (disk-type-name (disk-probe d2-device)))

            (test-equal 1
              (disk-get-primary-partition-count (disk-new d2-device)))

            (test-equal "test"
              (let* ((disk (disk-new d2-device))
                     (partitions (disk-partitions disk))
                     (boot-partition (find normal-partition? partitions)))
                (partition-get-name boot-partition)))

            ;; Image i3.
            (define i3-image
              #$(system-image i3))
            (define d3-device
              (get-device i3-image))

            (test-equal "msdos"
              (disk-type-name (disk-probe d3-device)))

            (test-equal 2
              (disk-get-primary-partition-count (disk-new d3-device)))

            ;; Image i4.
            (define i4-image
              #$(system-image i4))
            (define d4-device
              (get-device i4-image))

            (test-equal "gpt"
              (disk-type-name (disk-probe d4-device)))

            (test-equal 2
              (disk-get-primary-partition-count (disk-new d4-device)))

            ;; Image i5.
            (define i5-image
              #$(system-image i5))
            (define d5-device
              (get-device i5-image))

            (define (sector->byte sector)
              (/ (* sector (device-sector-size d5-device))
                 MEBIBYTE-SIZE))

            (test-equal "gpt"
              (disk-type-name (disk-probe d5-device)))

            (test-equal 2
              (disk-get-primary-partition-count (disk-new d5-device)))

            (test-equal '("fat32" "ext4")
              (map (compose filesystem-type-name partition-fs-type)
                   (filter data-partition?
                           (disk-partitions (disk-new d5-device)))))

            ;; The first partition has a 1MiB offset has a 128MiB size. The
            ;; second partition should then start at 129MiB.
            (test-equal '(1 129)
              (map (compose sector->byte partition-start)
                   (filter data-partition?
                           (disk-partitions (disk-new d5-device)))))

            ;; Image i6.
            (define i6-image
              #$(system-image i6))
            (define d6-device
              (get-device i6-image))

            (test-equal "gpt"
              (disk-type-name (disk-probe d6-device)))

            (test-end)))))

  (gexp->derivation "images-test" test))

(define %test-images
  (system-test
   (name "images")
   (description "Build and test basic system images.")
   (value (run-images-test))))