github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/packaging/snapd.mk (about)

     1  # This makefile is designed to be invoked from a build tree, not from the
     2  # source tree. This is particularly suited for building RPM packages. The goal
     3  # of this makefile is to *reduce* the duplication, *avoid* unintended
     4  # inconsistency between packaging of snapd across distributions.
     5  #
     6  # -----------------------------------------------------
     7  # Interface between snapd.mk and distribution packaging
     8  # -----------------------------------------------------
     9  #
    10  # Distribution packaging must generate snapd.defines.mk with a set of makefile
    11  # variable definitions that are discussed below. This allows the packaging
    12  # world to define directory layout and build configuration in one place and
    13  # this makefile to simply obey and implement that configuration.
    14  
    15  include snapd.defines.mk
    16  
    17  # There are two sets of definitions expected:
    18  # 1) variables defining various directory names
    19  vars += bindir sbindir libexecdir mandir datadir localstatedir sharedstatedir unitdir
    20  # 2) variables defining build options:
    21  #   with_testkeys: set to 1 to build snapd with test key built in
    22  #   with_apparmor: set to 1 to build snapd with apparmor support
    23  #   with_core_bits: set to 1 to build snapd with things needed for the core/snapd snap
    24  #   with_alt_snap_mount_dir: set to 1 to build snapd with alternate snap mount directory
    25  vars += with_testkeys with_apparmor with_core_bits with_alt_snap_mount_dir
    26  # Verify that none of the variables are empty. This may happen if snapd.mk and
    27  # distribution packaging generating snapd.defines.mk get out of sync.
    28  
    29  $(foreach var,$(vars),$(if $(value $(var)),,$(error $(var) is empty or unset, check snapd.defines.mk)))
    30  
    31  # ------------------------------------------------
    32  # There are no more control knobs after this point
    33  # ------------------------------------------------
    34  
    35  # Import path of snapd.
    36  import_path = github.com/snapcore/snapd
    37  
    38  
    39  # This is usually set by %make_install. It is defined here to avoid warnings or
    40  # errors from referencing undefined variables.
    41  DESTDIR? =
    42  
    43  # Decide which of the two snap mount directories to use. This is not
    44  # referencing localstatedir because the code copes with only those two values
    45  # explicitly.
    46  ifeq ($(with_alt_snap_mount_dir),1)
    47  snap_mount_dir = /var/lib/snapd/snap
    48  else
    49  snap_mount_dir = /snap
    50  endif
    51  
    52  # The list of go binaries we are expected to build.
    53  go_binaries = snap snapctl snap-seccomp snap-update-ns snap-exec snapd
    54  
    55  # The snapd package does not support Go modules. Since Go 1.16 those are on by
    56  # default, so make sure to disable them.
    57  export GO111MODULE = off
    58  
    59  # NOTE: This *depends* on building out of tree. Some of the built binaries
    60  # conflict with directory names in the tree.
    61  .PHONY: all
    62  all: $(go_binaries) 
    63  
    64  snap: GO_TAGS += nomanagers
    65  snap snap-seccomp:
    66  	go build $(if $(GO_TAGS),-tags $(GO_TAGS)) -buildmode=pie $(import_path)/cmd/$@
    67  
    68  # Those three need to be built as static binaries. They run on the inside of a
    69  # nearly-arbitrary mount namespace that does not contain anything we can depend
    70  # on (no standard library, for example).
    71  snap-update-ns snap-exec snapctl:
    72  	# Explicit request to use an external linker, otherwise extldflags may not be
    73  	# used
    74  	go build -buildmode=default -ldflags '-linkmode external -extldflags "-static"' $(import_path)/cmd/$@
    75  
    76  # Snapd can be built with test keys. This is only used by the internal test
    77  # suite to add test assertions. Do not enable this in distribution packages.
    78  snapd:
    79  ifeq ($(with_testkeys),1)
    80  	go build -buildmode=pie -tags withtestkeys $(import_path)/cmd/$@
    81  else
    82  	go build -buildmode=pie $(import_path)/cmd/$@
    83  endif
    84  
    85  # Know how to create certain directories.
    86  $(addprefix $(DESTDIR),$(libexecdir)/snapd $(bindir) $(mandir)/man8 /$(sharedstatedir)/snapd $(localstatedir)/cache/snapd $(snap_mount_dir)):
    87  	install -m 755 -d $@
    88  
    89  .PHONY: install
    90  
    91  # Install snap into /usr/bin/.
    92  install:: snap | $(DESTDIR)$(bindir)
    93  	install -m 755 $^ $|
    94  
    95  # Install snapctl snapd, snap-{exec,update-ns,seccomp} into /usr/lib/snapd/
    96  install:: snapctl snapd snap-exec snap-update-ns snap-seccomp | $(DESTDIR)$(libexecdir)/snapd
    97  	install -m 755 $^ $|
    98  
    99  # Ensure /usr/bin/snapctl is a symlink to /usr/lib/snapd/snapctl
   100  install:: | $(DESTDIR)$(bindir)
   101  	ln -s $(libexecdir)/snapd/snapctl $|/snapctl
   102  
   103  # Generate and install man page for snap command
   104  install:: snap | $(DESTDIR)$(mandir)/man8
   105  	./snap help --man > $|/snap.8
   106  
   107  # Install the directory structure in /var/lib/snapd
   108  install::
   109  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/apparmor/profiles
   110  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/apparmor/snap-confine
   111  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/assertions
   112  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/cache
   113  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/cookie
   114  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/dbus-1/services
   115  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/dbus-1/system-services
   116  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/desktop/applications
   117  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/device
   118  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/hostfs
   119  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/inhibit
   120  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/lib/gl
   121  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/lib/gl32
   122  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/lib/glvnd
   123  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/lib/vulkan
   124  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/mount
   125  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/seccomp/bpf
   126  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/sequence
   127  	install -m 755 -d $(DESTDIR)/$(sharedstatedir)/snapd/snaps
   128  
   129  # Touch files that are ghosted by the package. Those are _NOT_ installed but
   130  # this way the package manager knows about them belonging to the package.
   131  install:: | $(DESTDIR)/$(sharedstatedir)/snapd
   132  	touch $|/state.json
   133  	touch $|/system-key
   134  
   135  install:: | $(DESTDIR)$(localstatedir)/cache/snapd
   136  	touch $|/sections
   137  	touch $|/names
   138  	touch $|/commands
   139  
   140  install:: | $(DESTDIR)$(snap_mount_dir)
   141  	touch $|/README
   142  
   143  
   144  # Install the /snap/bin directory
   145  install::
   146  	install -m 755 -d $(DESTDIR)$(snap_mount_dir)/bin
   147  
   148  # Install misc directories: 
   149  install::
   150  	install -m 755 -d $(DESTDIR)$(localstatedir)/cache/snapd
   151  	install -m 755 -d $(DESTDIR)$(datadir)/polkit-1/actions
   152  
   153  # Remove traces of ubuntu-core-launcher. It is a phased-out executable that is
   154  # still partially present in the tree but should be removed in the subsequent
   155  # release.
   156  install::
   157  	rm -f $(DESTDIR)$(bindir)/ubuntu-core-launcher
   158  
   159  # Do not ship snap-preseed. It is currently only useful on ubuntu and tailored
   160  # for preseeding of ubuntu cloud images due to certain assumptions about
   161  # runtime environment of the host and of the preseeded image.
   162  install::
   163  	rm -f $(DESTDIR)$(bindir)/snap-preseed
   164  
   165  ifeq ($(with_core_bits),0)
   166  # Remove systemd units that are only used on core devices.
   167  install::
   168  	rm -f $(addprefix $(DESTDIR)$(unitdir)/,snapd.autoimport.service snapd.system-shutdown.service snapd.snap-repair.timer snapd.snap-repair.service snapd.core-fixup.service snapd.recovery-chooser-trigger.service)
   169  
   170  # Remove fixup script that is only used on core devices.
   171  install::
   172  	rm -f $(DESTDIR)$(libexecdir)/snapd/snapd.core-fixup.sh
   173  
   174  # Remove system-shutdown helper that is only used on core devices.
   175  install::
   176  	rm -f $(DESTDIR)$(libexecdir)/snapd/system-shutdown
   177  endif
   178  
   179  ifeq ($(with_apparmor),0)
   180  # Don't ship apparmor helper service when AppArmor is not enabled.
   181  install::
   182  	rm -f $(DESTDIR)$(unitdir)/snapd.apparmor.service
   183  	rm -f $(DESTDIR)$(libexecdir)/snapd/snapd-apparmor
   184  endif
   185  
   186  # Tests use C.UTF-8 because some some code depend on this for fancy Unicode
   187  # output that unit tests do not mock.
   188  .PHONY: check
   189  check:
   190  	LC_ALL=C.UTF-8 go test $(import_path)/...
   191  
   192  .PHONY: clean
   193  clean:
   194  	rm -f $(go_binaries)