aboutsummaryrefslogtreecommitdiff
path: root/contexts/Makefile
blob: 530a3dc7e18ce203419c29caaa3311ab82ebf9b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# Module     : Makefile
# Copyright  : (c) 2011-2012, Galois, Inc.
#
# Maintainer :
# Stability  : Provisional
# Portability: Portable
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# Entry Point ##################################################################

.PHONY: all
all: compile package install ;


# Environment ##################################################################

BUILD_ROOT      := $(CURDIR)
REPO_ROOT       := $(CURDIR)/..
INSTALL_DIR     := $(REPO_ROOT)/binaries

ADDON_SDK       := $(REPO_ROOT)/tools/addon-sdk
BUILD_DIR       := $(REPO_ROOT)/build

CHROME_DIR      := $(BUILD_ROOT)/chrome
DATA_DIR        := $(BUILD_ROOT)/data

BIN_DIR         := $(DATA_DIR)/target
FIVEUI_DATA_DIR := $(DATA_DIR)/fiveui
LIB_DIR         := $(DATA_DIR)/lib

MV              := mv

# FiveUI project Javascript sources
js_sources := $(wildcard $(FIVEUI_DATA_DIR)/*.js)

# Namespaces, derived from the project sources.
js_namespaces := $(shell ./namespaces.awk $(js_sources))

# FiveUI project Javascript, known to contain application code, not library
# code.
js_app_sources := \
	$(FIVEUI_DATA_DIR)/options.js \
	$(FIVEUI_DATA_DIR)/firefox/firefox-options.js \
	$(FIVEUI_DATA_DIR)/chrome/chrome-options.js

# Namespaces that are known to be just applications
js_app_namespaces := $(shell ./namespaces.awk $(js_app_sources))

# Namespaces that contain only library code
js_lib_namespaces := $(foreach ns,$(js_app_namespaces),\
	$(filter-out $(ns),$(js_namespaces)))

# The path to the closure library
CLOSURE_LIB := $(DATA_DIR)/lib/closure-library


# Macros #######################################################################

# Generate a set of targets that will allow directory creation to be depended on
# by other targets.
#
# $1 - the directory name
define dir-exists
$1/.token:
	mkdir $1 && touch $$@
endef

dir-dep = $1/.token

# The ../../../../../ correpsonds to the relative path from
# goog/base.js to the context directory.
#
# $1 - The path (relative to BUILD_ROOT) to generate a root_with_prefix pair for
goog-relative = $(BUILD_ROOT)/$1 ../../../../../$1

# Remove the target of the current rule when the provided command exits on
# failure.
#
# $1 - The command to run
rm-on-failure = ( $1 || ($(RM) $@; exit 1) )

define profile
.PHONY: profile-$1
profile-$1: $(REPO_ROOT)/profiles/$1/.token

$(REPO_ROOT)/profiles/$1/.token: $(REPO_ROOT)/profiles/$1.tar
	$(MAKE) -C $(REPO_ROOT)/profiles $1

.PHONY: clean-profile-$1
clean-profile-$1:
	$(MAKE) -C $(REPO_ROOT)/profiles clean-$1

clean: clean-profile-$1
endef

# General Targets ##############################################################

.PHONY: compile
compile:

.PHONY: package
package:

.PHONY: install
install:

.PHONY: clean
clean:

# Print out the value of a variable, useful for debugging.
print-%:
	@echo "$* = $($*)"

# BIN_DIR management
$(eval $(call dir-exists,$(BIN_DIR)))

clean: clean-BIN_DIR

.PHONY: clean-BIN_DIR
clean-BIN_DIR:
	@echo "Cleaning $(BIN_DIR)"
	$(RM) -r $(BIN_DIR)


# CSS Utilities ################################################################

css_bundle := $(BUILD_DIR)/css-bundle.py

# The jquery-ui css, in a form that has its image dependencies compiled in as
# base-64 encoded data.
$(BIN_DIR)/bundled.css: $(call dir-dep,$(BIN_DIR))
	cd $(LIB_DIR)/jquery/css/ui-lightness/ && \
		$(css_bundle) jquery-ui.css $@

# The injected css, with its images compiled in.
$(BIN_DIR)/injected.css: $(call dir-dep,$(BIN_DIR)) \
    $(FIVEUI_DATA_DIR)/injected/injected.css
	cd $(FIVEUI_DATA_DIR)/injected && \
		$(css_bundle) injected.css $@


# Javascript Utilities #########################################################

cl_linter       := $(BUILD_DIR)/closure-linter
#cl_compiler     := $(BUILD_DIR)/closure-compiler
#cl_typecheck    := $(BUILD_DIR)/closure-typechecker

# Files to ignore when running the linter
lint_excludes   := lib/jquery.js

# Run the linter over the project javascript sources.
.PHONY: lint
lint:
	find data/fiveui -name "*.js" | xargs $(cl_linter) --strict --exclude_files=$(lint_excludes)

closurebuilder := $(CLOSURE_LIB)/closure/bin/build/closurebuilder.py \
	--root=$(CLOSURE_LIB) --root=$(FIVEUI_DATA_DIR) \
	--compiler_jar=$(REPO_ROOT)/tools/closure_compiler/compiler.jar \
	--compiler_flags="--jscomp_error=checkTypes"

# Typecheck all the project javascript source.
.PHONY: typecheck
typecheck: $(js_sources)
	$(closurebuilder) --output_file=$(BIN_DIR)/trash.out --output_mode=compiled \
		$(foreach ns,$(js_namespaces),--namespace=$(ns)) && \
		$(RM) $(BIN_DIR)/trash.out


depswriter := /usr/bin/env $(CLOSURE_LIB)/closure/bin/build/depswriter.py

# The dependency graph for the project javascript.
$(BIN_DIR)/deps.js: $(call dir-dep,$(BIN_DIR)) $(js_sources)
	$(depswriter) --root_with_prefix="$(call goog-relative,data/fiveui)" \
		--output_file=$@

.PHONY: deps
deps: $(BIN_DIR)/deps.js;

compile: deps

closure-js := $(CLOSURE_LIB)/closure/bin/build/closurebuilder.py \
	--root=$(CLOSURE_LIB) \
	--root=$(FIVEUI_DATA_DIR) \
	--compiler_jar=$(REPO_ROOT)/tools/closure_compiler/compiler.jar \
	--compiler_flags="--accept_const_keyword" \
	--compiler_flags="--formatting=PRETTY_PRINT" \
	--output_mode="compiled"

compile-js := $(BUILD_ROOT)/compile-js.sh --output_mode=script

# Chrome Compilation ###########################################################

CHROME_OPTS := --pack-extension=$(BIN_DIR)/src \
               --pack-extension-key=$(BIN_DIR)/fiveui.pem

compile: compile-chrome

CHROME_TARGETS := $(BIN_DIR)/chrome-background.js $(BIN_DIR)/bundled.css \
	$(BIN_DIR)/chrome-options.js $(BIN_DIR)/injected.css

.PHONY: compile-chrome
compile-chrome: $(CHROME_TARGETS)

# Build the compiled chrome backend script.
$(BIN_DIR)/chrome-background.js: $(call dir-dep,$(BIN_DIR)) $(js_sources)
	$(call rm-on-failure,$(compile-js) --namespace=fiveui.chrome.background > $@)

# Build the compiled options script.
$(BIN_DIR)/chrome-options.js: $(call dir-dep,$(BIN_DIR)) $(js_sources)
	$(call rm-on-failure,$(compile-js) \
		--namespace=fiveui.chrome.options.init > $@)

# Clean chrome extension build artifacts.
.PHONY: clean-chrome
clean-chrome:
	@echo Cleaning Chrome extension...
	$(RM) fiveui.crx

clean: clean-chrome


# Firefox Compilation ##########################################################

# Sources the correct environment before invoking the cfx command.
#
# $1 - The desired arguments to the cfx command.
cfx = ( cd $(ADDON_SDK) > /dev/null && \
        . bin/activate  > /dev/null && \
        cd -            > /dev/null && \
        cfx $1)

.PHONY: firefox
firefox: package-firefox

$(eval $(call profile,firefox))

compile: compile-firefox

FIREFOX_TARGETS := profile-firefox $(BIN_DIR)/firefox-main.js \
	$(BIN_DIR)/bundled.css $(BIN_DIR)/firefox-options.js \
	$(BIN_DIR)/injected.css

.PHONY: compile-firefox
compile-firefox: $(FIREFOX_TARGETS)

$(BIN_DIR)/fiveui.js: $(call dir-dep,$(BIN_DIR)) $(js_sources)
	$(call rm-on-failure,$(compile-js) \
	    $(foreach ns,$(js_lib_namespaces), --namespace=$(ns)) > $@)
	echo 'exports.fiveui = fiveui;' >> $@

$(BIN_DIR)/firefox-main.js: $(call dir-dep,$(BIN_DIR)) $(js_sources)
	$(call rm-on-failure,$(compile-js) --namespace=fiveui.firefox.main > $@)


# TODO: remove 2 lines below, duplicate of above?
# compile with the closure compiler instead of the script output mode
#	$(call rm-on-failure,$(closure-js) --namespace=fiveui.firefox.main > $@)

# Build the compiled options script.
$(BIN_DIR)/firefox-options.js: $(call dir-dep,$(BIN_DIR)) $(js_sources)
	$(call rm-on-failure,$(compile-js) \
		--namespace=fiveui.firefox.options.init > $@)

# Use the cfx command to debug a firefox extension.
.PHONY: run-firefox
run-firefox: $(FIREFOX_TARGETS)
	@echo Running Firefox extension...
	$(call cfx,run -p $(REPO_ROOT)/profiles/firefox)

# Use the cfx command to debug a firefox extension.
.PHONY: test-firefox
test-firefox: $(FIREFOX_TARGETS) $(BIN_DIR)/fiveui.js
	@echo Testing Firefox extension...
	$(call cfx,test -v -p $(REPO_ROOT)/profiles/firefox)

# Clean firefox extension build artifacts.
.PHONY: clean-firefox
clean-firefox:
	@echo Cleaning Firefox extension...
	rm -rf profile

clean: clean-firefox


# Package Staging ##############################################################

STAGE_DIR := $(BUILD_ROOT)/stage

$(eval $(call dir-exists,$(STAGE_DIR)))

define copy-dir
mkdir -p $(STAGE_DIR)/$(dir $1)
cp -r $(BUILD_ROOT)/$1 $(STAGE_DIR)/$(dir $1)
endef

PACKAGE_SPECS := $(addprefix $(BUILD_ROOT)/,manifest.json package.json)

.PHONY: stage
stage: $(call dir-dep,$(STAGE_DIR)) compile $(PACKAGE_SPECS)
	$(call copy-dir,data/target)
	$(call copy-dir,data/fiveui)
	$(call copy-dir,data/lib)
	cp $(PACKAGE_SPECS) $(STAGE_DIR)

.PHONY: clean-stage
clean-stage:
	$(RM) -r $(STAGE_DIR)
clean: clean-stage


# CRX Generation ###############################################################

MAKE_CRX := $(BUILD_DIR)/makecrx

$(BUILD_ROOT)/fiveui.crx: stage
	$(MAKE_CRX) $(STAGE_DIR) $(CHROME_DIR)/fiveui.pem fiveui

.PHONY: clean-crx
clean-crx:
	$(RM) $(BUILD_ROOT)/fiveui.crx
clean: clean-crx

.PHONY: package-chrome
package-chrome: $(BUILD_ROOT)/fiveui.crx
package: package-chrome


# XPI Generation ###############################################################

$(BUILD_ROOT)/fiveui.xpi: stage
	$(call cfx,xpi \
		-p $(REPO_ROOT)/profiles/firefox \
		--pkgdir=$(STAGE_DIR))

.PHONY: run-firefox-staged
run-firefox-staged: stage
	$(call cfx,run -p $(REPO_ROOT)/profiles/firefox \
		--pkgdir=$(STAGE_DIR))

.PHONY: clean-xpi
clean-xpi:
	$(RM) $(BUILD_ROOT)/fiveui.xpi
clean: clean-xpi

.PHONY: package-firefox
package-firefox: $(BUILD_ROOT)/fiveui.xpi ;
package: package-firefox

# Installation #################################################################

install:
	$(MV) $(BUILD_ROOT)/fiveui.{crx,xpi} $(INSTALL_DIR)