Debian Codegraph @

Makefile

raw
1 # Makefile for fscrypt
2 #
3 # Copyright 2017 Google Inc.
4 # Author: Joe Richey ([email protected])
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
7 # use this file except in compliance with the License. You may obtain a copy of
8 # the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations under
16 # the License.
17
18 # Update this on each new release, along with the NEWS.md file.
19 VERSION := v0.3.7
20
21 NAME := fscrypt
22 PAM_NAME := pam_$(NAME)
23
24 ###### Makefile Command Line Flags ######
25 #
26 # BIN: The location where binaries will be built. Default: bin
27 # DESTDIR: Installation destination directory. Default: ""
28 # PREFIX: Installation path prefix. Default: /usr/local
29 # BINDIR: Where to install the fscrypt binary. Default: $(PREFIX)/bin
30 # PAM_MODULE_DIR: Where to install pam_fscrypt.so. Default: $(PREFIX)/lib/security
31 # PAM_CONFIG_DIR: Where to install Ubuntu PAM config. Default: $(PREFIX)/share/pam-configs
32 # If the empty string, then the Ubuntu PAM config will not be installed.
33 #
34 # MOUNT: The filesystem where our tests are run. Default: /mnt/fscrypt_mount
35 # Ex: make test-setup MOUNT=/foo/bar
36 # Creates a test filesystem at that location.
37 # Ex: make test-teardown MOUNT=/foo/bar
38 # Cleans up a test filesystem created with "make test-setup".
39 # Ex: make test MOUNT=/foo/bar
40 # Run all integration tests on that filesystem. This can be an existing
41 # filesystem, or one created with "make test-setup" (this is the default).
42 #
43 # CFLAGS: The flags passed to the C compiler. Default: -O2 -Wall
44 # Ex: make fscrypt "CFLAGS = -O3 -Werror"
45 # Builds fscrypt with the C code failing on warnings and highly optimized.
46 #
47 # LDFLAGS: The flags passed to the C linker. Default empty
48 # Ex: make fscrypt "LDFLAGS = -static -ldl -laudit -lcap-ng"
49 # Builds fscrypt as a static binary.
50 #
51 # GO_FLAGS: The flags passed to "go build". Default empty
52 # Ex: make fscrypt "GO_FLAGS = -race"
53 # Builds fscrypt with race detection for the go code.
54 #
55 # GO_LINK_FLAGS: The flags passed to the go linker. Default: -s -w
56 # Ex: make fscrypt GO_LINK_FLAGS=""
57 # Builds fscrypt without stripping the binary.
58
59 BIN := bin
60 export PATH := $(BIN):$(PATH)
61 PAM_MODULE := $(BIN)/$(PAM_NAME).so
62
63 ###### Setup Build Flags #####
64 CFLAGS := -O2 -Wall
65 # Pass CFLAGS to each cgo invocation.
66 export CGO_CFLAGS = $(CFLAGS)
67 # By default, we strip the binary to reduce size.
68 GO_LINK_FLAGS := -s -w
69
70 # Flag to embed the version (pulled from tags) into the binary.
71 TAG_VERSION := $(shell git describe --tags)
72 VERSION_FLAG := -X "main.version=$(if $(TAG_VERSION),$(TAG_VERSION),$(VERSION))"
73
74 override GO_LINK_FLAGS += $(VERSION_FLAG) -extldflags "$(LDFLAGS)"
75 override GO_FLAGS += --ldflags '$(GO_LINK_FLAGS)'
76 # Use -trimpath if available
77 ifneq "" "$(shell go help build | grep trimpath)"
78 override GO_FLAGS += -trimpath
79 endif
80
81 ###### Find All Files and Directories ######
82 FILES := $(shell find . -path '*/.git*' -prune -o -type f -printf "%P\n")
83 GO_FILES := $(filter %.go,$(FILES))
84 GO_NONGEN_FILES := $(filter-out %.pb.go,$(GO_FILES))
85 GO_DIRS := $(sort $(dir $(GO_FILES)))
86 C_FILES := $(filter %.c %.h,$(FILES))
87 PROTO_FILES := $(filter %.proto,$(FILES))
88
89 ###### Build, Formatting, and Linting Commands ######
90 .PHONY: default all gen format lint clean
91 default: $(BIN)/$(NAME) $(PAM_MODULE)
92 all: tools gen default format lint test
93
94 $(BIN)/$(NAME): $(GO_FILES) $(C_FILES)
95 go build $(GO_FLAGS) -o $@ ./cmd/$(NAME)
96
97 $(PAM_MODULE): $(GO_FILES) $(C_FILES)
98 go build $(GO_FLAGS) -buildmode=c-shared -o $@ ./$(PAM_NAME)
99 rm -f $(BIN)/$(PAM_NAME).h
100
101 gen: $(BIN)/protoc $(BIN)/protoc-gen-go $(PROTO_FILES)
102 protoc --go_out=. --go_opt=paths=source_relative $(PROTO_FILES)
103
104 format: $(BIN)/goimports
105 goimports -w $(GO_NONGEN_FILES)
106 clang-format -i -style=Google $(C_FILES)
107
108 lint: $(BIN)/staticcheck $(BIN)/misspell
109 go vet ./...
110 staticcheck ./...
111 misspell -source=text $(FILES)
112 shellcheck -s bash cmd/fscrypt/fscrypt_bash_completion
113 ( cd cli-tests && shellcheck -x *.sh)
114
115 clean:
116 rm -f $(BIN)/$(NAME) $(PAM_MODULE) $(TOOLS) coverage.out $(COVERAGE_FILES) $(PAM_CONFIG)
117
118 ###### Cgroup testdata ######
119 .PHONY: gen-cgroup-testdata
120 gen-cgroup-testdata:
121 bin/gen-cgroup-testdata
122
123 ###### Go tests ######
124 .PHONY: test test-setup test-teardown
125
126 # If MOUNT exists signal that we should run integration tests.
127 MOUNT := /tmp/$(NAME)-mount
128 IMAGE := /tmp/$(NAME)-image
129 ifneq ("$(wildcard $(MOUNT))","")
130 export TEST_FILESYSTEM_ROOT = $(MOUNT)
131 endif
132
133 test:
134 go test -p 1 ./...
135
136 test-setup:
137 dd if=/dev/zero of=$(IMAGE) bs=1M count=20
138 mkfs.ext4 -b 4096 -O encrypt $(IMAGE) -F
139 mkdir -p $(MOUNT)
140 sudo mount -o rw,loop,user $(IMAGE) $(MOUNT)
141 sudo chmod +777 $(MOUNT)
142
143 test-teardown:
144 sudo umount $(MOUNT)
145 rmdir $(MOUNT)
146 rm -f $(IMAGE)
147
148 ###### Command-line interface tests ######
149 .PHONY: cli-test cli-test-update
150
151 cli-test: $(BIN)/$(NAME)
152 sudo cli-tests/run.sh
153
154 cli-test-update: $(BIN)/$(NAME)
155 sudo cli-tests/run.sh --update-output
156
157 # Runs tests and generates coverage
158 COVERAGE_FILES := $(addsuffix coverage.out,$(GO_DIRS))
159 coverage.out: $(BIN)/gocovmerge $(COVERAGE_FILES)
160 @gocovmerge $(COVERAGE_FILES) > $@
161
162 %/coverage.out: $(GO_FILES) $(C_FILES)
163 @go test -coverpkg=./... -covermode=count -coverprofile=$@ -p 1 ./$* 2> /dev/null
164
165 ###### Installation Commands (require sudo) #####
166 .PHONY: install install-bin install-pam uninstall install-completion
167 install: install-bin install-pam install-completion
168
169 PREFIX := /usr/local
170 BINDIR := $(PREFIX)/bin
171
172 install-bin: $(BIN)/$(NAME)
173 install -d $(DESTDIR)$(BINDIR)
174 install $< $(DESTDIR)$(BINDIR)
175
176 PAM_MODULE_DIR := $(PREFIX)/lib/security
177 PAM_INSTALL_PATH := $(PAM_MODULE_DIR)/$(PAM_NAME).so
178 PAM_CONFIG := $(BIN)/config
179 PAM_CONFIG_DIR := $(PREFIX)/share/pam-configs
180
181 install-pam: $(PAM_MODULE)
182 install -d $(DESTDIR)$(PAM_MODULE_DIR)
183 install $(PAM_MODULE) $(DESTDIR)$(PAM_MODULE_DIR)
184 ifdef PAM_CONFIG_DIR
185 m4 --define=PAM_INSTALL_PATH=$(PAM_INSTALL_PATH) < $(PAM_NAME)/config > $(PAM_CONFIG)
186 install -d $(DESTDIR)$(PAM_CONFIG_DIR)
187 install $(PAM_CONFIG) $(DESTDIR)$(PAM_CONFIG_DIR)/$(NAME)
188 endif
189
190 COMPLETION_INSTALL_DIR := $(PREFIX)/share/bash-completion/completions
191
192 install-completion: cmd/fscrypt/fscrypt_bash_completion
193 install -Dm644 $< $(DESTDIR)$(COMPLETION_INSTALL_DIR)/fscrypt
194
195 uninstall:
196 rm -f $(DESTDIR)$(BINDIR)/$(NAME) \
197 $(DESTDIR)$(PAM_INSTALL_PATH) \
198 $(DESTDIR)$(COMPLETION_INSTALL_DIR)/fscrypt
199 ifdef PAM_CONFIG_DIR
200 rm -f $(DESTDIR)$(PAM_CONFIG_DIR)/$(NAME)
201 endif
202
203 #### Tool Building Commands ####
204 TOOLS := $(addprefix $(BIN)/,protoc protoc-gen-go goimports staticcheck gocovmerge misspell)
205 .PHONY: tools
206 tools: $(TOOLS)
207
208 $(BIN)/protoc-gen-go:
209 go build -o $@ google.golang.org/protobuf/cmd/protoc-gen-go
210 $(BIN)/goimports:
211 go build -o $@ golang.org/x/tools/cmd/goimports
212 $(BIN)/staticcheck:
213 go build -o $@ honnef.co/go/tools/cmd/staticcheck
214 $(BIN)/gocovmerge:
215 go build -o $@ github.com/wadey/gocovmerge
216 $(BIN)/misspell:
217 go build -o $@ github.com/client9/misspell/cmd/misspell
218
219 # Non-go tools downloaded from appropriate repository
220 PROTOC_VERSION := 3.6.1
221 ARCH := $(shell uname -m)
222 ifeq (x86_64,$(ARCH))
223 PROTOC_ARCH := x86_64
224 else ifneq ($(filter i386 i686,$(ARCH)),)
225 PROTOC_ARCH := x86_32
226 else ifneq ($(filter aarch64 armv8l,$(ARCH)),)
227 PROTOC_ARCH := aarch_64
228 endif
229 ifdef PROTOC_ARCH
230 PROTOC_URL := https://github.com/google/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-linux-$(PROTOC_ARCH).zip
231 $(BIN)/protoc:
232 wget -q $(PROTOC_URL) -O /tmp/protoc.zip
233 unzip -q -j /tmp/protoc.zip bin/protoc -d $(BIN)
234 else
235 PROTOC_PATH := $(shell which protoc)
236 $(BIN)/protoc: $(PROTOC_PATH)
237 ifneq ($(wildcard $(PROTOC_PATH)),)
238 cp $< $@
239 else
240 $(error Could not download protoc binary or locate it on the system. Please install it)
241 endif
242 endif