Makefile 8.51 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
3
# Setting variables

SHELL := /bin/bash
Zejun Lin's avatar
Zejun Lin committed
4
5
PIP_INSTALL := python3 -m pip install
PIP_UNINSTALL := python3 -m pip uninstall
liuzhe-lz's avatar
liuzhe-lz committed
6

7
8
9
10
11
12
## Colorful output
_INFO := $(shell echo -e '\e[1;36m')
_WARNING := $(shell echo -e '\e[1;33m')
_END := $(shell echo -e '\e[0m')


liuzhe-lz's avatar
liuzhe-lz committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
## Install directories
ifeq ($(shell id -u), 0)  # is root
    _ROOT := 1
    BIN_PATH ?= /usr/bin
    INSTALL_PREFIX ?= /usr/share
    EXAMPLES_PATH ?= $(INSTALL_PREFIX)/nni/examples
    BASH_COMP_SCRIPT ?= /usr/share/bash-completion/completions/nnictl
else  # is normal user
    BIN_PATH ?= ${HOME}/.local/bin
    INSTALL_PREFIX ?= ${HOME}/.local
    EXAMPLES_PATH ?= ${HOME}/nni/examples
    ifndef VIRTUAL_ENV
        PIP_MODE ?= --user
    endif
    BASH_COMP_SCRIPT ?= ${HOME}/.bash_completion.d/nnictl
28
endif
Deshui Yu's avatar
Deshui Yu committed
29

liuzhe-lz's avatar
liuzhe-lz committed
30
## Dependency information
Gems Guo's avatar
Gems Guo committed
31
NODE_VERSION ?= v10.10.0
liuzhe-lz's avatar
liuzhe-lz committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
NODE_TARBALL ?= node-$(NODE_VERSION)-linux-x64.tar.xz
NODE_PATH ?= $(INSTALL_PREFIX)/nni/node

YARN_VERSION ?= v1.9.4
YARN_TARBALL ?= yarn-$(YARN_VERSION).tar.gz
YARN_PATH ?= /tmp/nni-yarn

SERVE_VERSION ?= 10.0.1
SERVE_TARBALL ?= serve-$(SERVE_VERSION).tgz
SERVE_PATH ?= $(INSTALL_PREFIX)/nni/serve


## Check if dependencies have been installed globally
ifeq (, $(shell command -v node 2>/dev/null))
46
    $(info $(_INFO) Node.js not found $(_END))
liuzhe-lz's avatar
liuzhe-lz committed
47
48
49
50
51
    _MISS_DEPS := 1  # node not found
else
    _VER := $(shell node --version)
    _NEWER := $(shell echo -e "$(NODE_VERSION)\n$(_VER)" | sort -Vr | head -n 1)
    ifneq ($(_VER), $(_NEWER))
52
        $(info $(_INFO) Node.js version not match $(_END))
liuzhe-lz's avatar
liuzhe-lz committed
53
54
55
56
        _MISS_DEPS := 1  # node outdated
    endif
endif
ifeq (, $(shell command -v yarnpkg 2>/dev/null))
57
    $(info $(_INFO) Yarn not found $(_END))
liuzhe-lz's avatar
liuzhe-lz committed
58
59
60
    _MISS_DEPS := 1  # yarn not found
endif
ifeq (, $(shell command -v serve 2>/dev/null))
61
    $(info $(_INFO) Serve not found $(_END))
liuzhe-lz's avatar
liuzhe-lz committed
62
63
64
65
    _MISS_DEPS := 1  # serve not found
endif

ifdef _MISS_DEPS
66
    $(info $(_INFO) Missing dependencies, use local toolchain $(_END))
liuzhe-lz's avatar
liuzhe-lz committed
67
68
69
70
    NODE := $(NODE_PATH)/bin/node
    YARN := PATH=$${PATH}:$(NODE_PATH)/bin $(YARN_PATH)/bin/yarn
    SERVE := $(SERVE_PATH)/serve
else
71
    $(info $(_INFO) All dependencies found, use global toolchain $(_END))
liuzhe-lz's avatar
liuzhe-lz committed
72
73
74
75
76
77
78
79
80
81
82
83
    NODE := node
    YARN := yarnpkg
    SERVE := serve
endif


# Setting variables end


# Main targets

.PHONY: build
Deshui Yu's avatar
Deshui Yu committed
84
build:
liuzhe-lz's avatar
liuzhe-lz committed
85
86
	#$(_INFO) Building NNI Manager $(_END)
	cd src/nni_manager && $(YARN) && $(YARN) build
Deshui Yu's avatar
Deshui Yu committed
87
	
liuzhe-lz's avatar
liuzhe-lz committed
88
89
	#$(_INFO) Building Web UI $(_END)
	cd src/webui && $(YARN) && $(YARN) build
Deshui Yu's avatar
Deshui Yu committed
90
	
liuzhe-lz's avatar
liuzhe-lz committed
91
	#$(_INFO) Building Python SDK $(_END)
Deshui Yu's avatar
Deshui Yu committed
92
93
	cd src/sdk/pynni && python3 setup.py build
	
liuzhe-lz's avatar
liuzhe-lz committed
94
	#$(_INFO) Building nnictl $(_END)
Deshui Yu's avatar
Deshui Yu committed
95
96
	cd tools && python3 setup.py build

liuzhe-lz's avatar
liuzhe-lz committed
97
98
99
100
101
102
103
# Standard installation target
# Must be invoked after building
.PHONY: install
install: install-python-modules
install: install-node-modules
install: install-scripts
install: install-examples
Deshui Yu's avatar
Deshui Yu committed
104
install:
liuzhe-lz's avatar
liuzhe-lz committed
105
	#$(_INFO) Complete! You may want to add $(BIN_PATH) to your PATH environment $(_END)
106

liuzhe-lz's avatar
liuzhe-lz committed
107
108
109
110
111

# Target for remote machine workers
# Only installs core SDK module
.PHONY: remote-machine-install
remote-machine-install:
112
	cd src/sdk/pynni && python3 setup.py install $(PIP_MODE)
liuzhe-lz's avatar
liuzhe-lz committed
113
114


liuzhe-lz's avatar
liuzhe-lz committed
115
# All-in-one target for non-expert users
liuzhe-lz's avatar
liuzhe-lz committed
116
117
118
119
120
# Installs NNI as well as its dependencies, and update bashrc to set PATH
.PHONY: easy-install
easy-install: check-perm
easy-install: install-dependencies
easy-install: build
liuzhe-lz's avatar
liuzhe-lz committed
121
easy-install: install
liuzhe-lz's avatar
liuzhe-lz committed
122
123
easy-install: update-bash-config

liuzhe-lz's avatar
liuzhe-lz committed
124
easy-install:
Zejun Lin's avatar
Zejun Lin committed
125
	#$(_INFO) Complete! $(_END)
liuzhe-lz's avatar
liuzhe-lz committed
126
127
128
129
130
131
132
133
134
135


# Target for setup.py
# Do not invoke this manually
.PHONY: pip-install
pip-install: install-dependencies
pip-install: build
pip-install: install-node-modules
pip-install: install-scripts
pip-install: install-examples
liuzhe-lz's avatar
liuzhe-lz committed
136
pip-install: update-bash-config
liuzhe-lz's avatar
liuzhe-lz committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150


# Target for NNI developers
# Creates symlinks instead of copying files
.PHONY: dev-install
dev-install: check-dev-env
dev-install: install-dev-modules
dev-install: install-scripts
dev-install:
	#$(_INFO) Complete! You may want to add $(BIN_PATH) to your PATH environment $(_END)


.PHONY: uninstall
uninstall:
Zejun Lin's avatar
Zejun Lin committed
151
152
	-$(PIP_UNINSTALL) -y nni
	-$(PIP_UNINSTALL) -y nnictl
liuzhe-lz's avatar
liuzhe-lz committed
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
	-rm -rf $(INSTALL_PREFIX)/nni
	-rm -f $(BIN_PATH)/nnimanager
	-rm -f $(BIN_PATH)/nnictl
	-rm -f $(BASH_COMP_SCRIPT)
	-[ $(EXAMPLES_PATH) = ${PWD}/examples ] || rm -rf $(EXAMPLES_PATH)

# Main targets end


# Helper targets

$(NODE_TARBALL):
	#$(_INFO) Downloading Node.js $(_END)
	wget https://nodejs.org/dist/$(NODE_VERSION)/$(NODE_TARBALL)

$(YARN_TARBALL):
	#$(_INFO) Downloading Yarn $(_END)
	wget https://github.com/yarnpkg/yarn/releases/download/$(YARN_VERSION)/$(YARN_TARBALL)

$(SERVE_TARBALL):
	#$(_INFO) Downloading serve $(_END)
	wget https://registry.npmjs.org/serve/-/$(SERVE_TARBALL)

.PHONY: intall-dependencies
install-dependencies: $(NODE_TARBALL) $(YARN_TARBALL) $(SERVE_TARBALL)
	#$(_INFO) Cleaning $(_END)
	rm -rf $(NODE_PATH)
	rm -rf $(YARN_PATH)
	rm -rf $(SERVE_PATH)
	mkdir -p $(NODE_PATH)
	mkdir -p $(YARN_PATH)
	mkdir -p $(SERVE_PATH)
Deshui Yu's avatar
Deshui Yu committed
185
	
liuzhe-lz's avatar
liuzhe-lz committed
186
187
188
	#$(_INFO) Extracting Node.js $(_END)
	tar -xf $(NODE_TARBALL)
	mv -fT node-$(NODE_VERSION)-linux-x64 $(NODE_PATH)
Deshui Yu's avatar
Deshui Yu committed
189
	
liuzhe-lz's avatar
liuzhe-lz committed
190
191
192
	#$(_INFO) Extracting Yarn $(_END)
	tar -xf $(YARN_TARBALL)
	mv -fT yarn-$(YARN_VERSION) $(YARN_PATH)
Deshui Yu's avatar
Deshui Yu committed
193
	
liuzhe-lz's avatar
liuzhe-lz committed
194
195
	#$(_INFO) Installing serve $(_END)
	PATH=$${PATH}:$(NODE_PATH)/bin npm install --prefix $(SERVE_PATH) $(SERVE_TARBALL)
Deshui Yu's avatar
Deshui Yu committed
196
	
liuzhe-lz's avatar
liuzhe-lz committed
197
198
199
200
	#$(_INFO) Creating serve executable script $(_END)
	echo '#!/bin/sh' > $(SERVE_PATH)/serve
	echo '$(NODE) $(SERVE_PATH)/node_modules/serve/bin/serve.js $$@' >> $(SERVE_PATH)/serve
	chmod +x $(SERVE_PATH)/serve
201
202


liuzhe-lz's avatar
liuzhe-lz committed
203
204
205
.PHONY: install-python-modules
install-python-modules:
	#$(_INFO) Installing Python SDK $(_END)
Zejun Lin's avatar
Zejun Lin committed
206
	cd src/sdk/pynni && $(PIP_INSTALL) $(PIP_MODE) .
207
	
liuzhe-lz's avatar
liuzhe-lz committed
208
	#$(_INFO) Installing nnictl $(_END)
Zejun Lin's avatar
Zejun Lin committed
209
	cd tools && $(PIP_INSTALL) $(PIP_MODE) .
210

liuzhe-lz's avatar
liuzhe-lz committed
211
212
.PHONY: install-node-modules
install-node-modules:
213
	mkdir -p $(INSTALL_PREFIX)/nni
Zejun Lin's avatar
Zejun Lin committed
214
	rm -rf ${PWD}/src/nni_manager/dist/node_modules
215
	
liuzhe-lz's avatar
liuzhe-lz committed
216
	#$(_INFO) Installing NNI Manager $(_END)
Zejun Lin's avatar
Zejun Lin committed
217
218
	cp -rT ${PWD}/src/nni_manager/dist $(INSTALL_PREFIX)/nni/nni_manager
	cp -rT ${PWD}/src/nni_manager/node_modules $(INSTALL_PREFIX)/nni/nni_manager/node_modules
219
	
liuzhe-lz's avatar
liuzhe-lz committed
220
	#$(_INFO) Installing Web UI $(_END)
Zejun Lin's avatar
Zejun Lin committed
221
	cp -rT ${PWD}/src/webui/build $(INSTALL_PREFIX)/nni/webui
Deshui Yu's avatar
Deshui Yu committed
222
223


liuzhe-lz's avatar
liuzhe-lz committed
224
225
226
.PHONY: install-dev-modules
install-dev-modules:
	#$(_INFO) Installing Python SDK $(_END)
Zejun Lin's avatar
Zejun Lin committed
227
	cd src/sdk/pynni && $(PIP_INSTALL) $(PIP_MODE) -e .
228
	
liuzhe-lz's avatar
liuzhe-lz committed
229
	#$(_INFO) Installing nnictl $(_END)
Zejun Lin's avatar
Zejun Lin committed
230
	cd tools && $(PIP_INSTALL) $(PIP_MODE) -e .
231

liuzhe-lz's avatar
liuzhe-lz committed
232
	mkdir -p $(INSTALL_PREFIX)/nni
233
	
liuzhe-lz's avatar
liuzhe-lz committed
234
	#$(_INFO) Installing NNI Manager $(_END)
Zejun Lin's avatar
Zejun Lin committed
235
236
	ln -sf ${PWD}/src/nni_manager/dist $(INSTALL_PREFIX)/nni/nni_manager
	ln -sf ${PWD}/src/nni_manager/node_modules $(INSTALL_PREFIX)/nni/nni_manager/node_modules
Deshui Yu's avatar
Deshui Yu committed
237
	
liuzhe-lz's avatar
liuzhe-lz committed
238
	#$(_INFO) Installing Web UI $(_END)
Zejun Lin's avatar
Zejun Lin committed
239
	ln -sf ${PWD}/src/webui/build $(INSTALL_PREFIX)/nni/webui
liuzhe-lz's avatar
liuzhe-lz committed
240
241
242
243
244


.PHONY: install-scripts
install-scripts:
	mkdir -p $(BIN_PATH)
245
246
	
	echo '#!/bin/sh' > $(BIN_PATH)/nnimanager
liuzhe-lz's avatar
liuzhe-lz committed
247
248
	echo 'cd $(INSTALL_PREFIX)/nni/nni_manager' >> $(BIN_PATH)/nnimanager
	echo '$(NODE) main.js $$@' >> $(BIN_PATH)/nnimanager
249
250
251
	chmod +x $(BIN_PATH)/nnimanager
	
	echo '#!/bin/sh' > $(BIN_PATH)/nnictl
liuzhe-lz's avatar
liuzhe-lz committed
252
253
254
255
	echo 'NNI_MANAGER=$(BIN_PATH)/nnimanager \' >> $(BIN_PATH)/nnictl
	echo 'NNI_SERVE=$(SERVE) \' >> $(BIN_PATH)/nnictl
	echo 'WEB_UI_FOLDER=$(INSTALL_PREFIX)/nni/webui \' >> $(BIN_PATH)/nnictl
	echo 'python3 -m nnicmd.nnictl $$@' >> $(BIN_PATH)/nnictl
256
257
	chmod +x $(BIN_PATH)/nnictl
	
liuzhe-lz's avatar
liuzhe-lz committed
258
	install -Dm644 tools/bash-completion $(BASH_COMP_SCRIPT)
Deshui Yu's avatar
Deshui Yu committed
259
260


liuzhe-lz's avatar
liuzhe-lz committed
261
262
263
264
265
266
.PHONY: install-examples
install-examples:
	mkdir -p $(EXAMPLES_PATH)
	[ $(EXAMPLES_PATH) = ${PWD}/examples ] || cp -rT examples $(EXAMPLES_PATH)


liuzhe-lz's avatar
liuzhe-lz committed
267
268
269
270
271
.PHONY: update-bash-config
ifndef _ROOT
update-bash-config:
	#$(_INFO) Updating bash configurations $(_END)
    ifeq (, $(shell echo $$PATH | tr ':' '\n' | grep -x '$(BIN_PATH)'))  # $(BIN_PATH) not in PATH
liuzhe-lz's avatar
liuzhe-lz committed
272
273
	#$(_WARNING) NOTE: adding $(BIN_PATH) to PATH in bashrc $(_END)
	echo 'export PATH="$$PATH:$(BIN_PATH)"' >> ~/.bashrc
liuzhe-lz's avatar
liuzhe-lz committed
274
275
276
277
278
279
280
    endif
    ifeq (, $(shell (source ~/.bash_completion ; command -v _nnictl) 2>/dev/null))  # completion not installed
	#$(_WARNING) NOTE: adding $(BASH_COMP_SCRIPT) to ~/.bash_completion $(_END)
	echo '[[ -f $(BASH_COMP_SCRIPT) ]] && source $(BASH_COMP_SCRIPT)' >> ~/.bash_completion
    endif
else
update-bash-config: ;
liuzhe-lz's avatar
liuzhe-lz committed
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
endif


.PHONY: check-perm
ifdef _ROOT
check-perm:
	#$(_WARNING) Run easy-install as root is not optimal $(_END)
	#$(_WARNING) Suggest run as non-privileged user or manually install instead $(_END)
	#$(_WARNING) Continue easy-install as root? (y/N) $(_END)
	@read CONFIRM && [ "$$CONFIRM" = y ]
else
check-perm: ;
endif


.PHONY: check-dev-env
check-dev-env:
	#$(_INFO) Checking developing environment... $(_END)
ifdef _ROOT
	$(error You should not develop NNI as root)
endif
ifdef _MISS_DEPS
Gems Guo's avatar
Gems Guo committed
303
#	$(error Please install Node.js, Yarn, and Serve to develop NNI)
liuzhe-lz's avatar
liuzhe-lz committed
304
305
306
307
endif
	#$(_INFO) Pass! $(_END)

# Helper targets end