Makefile 6.37 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
## Install directories
ifeq ($(shell id -u), 0)  # is root
    _ROOT := 1
16
17
    BIN_FOLDER ?= /usr/local/bin
    NNI_PKG_PATH ?= /usr/local/nni_pkg
liuzhe-lz's avatar
liuzhe-lz committed
18
19
    BASH_COMP_SCRIPT ?= /usr/share/bash-completion/completions/nnictl
else  # is normal user
20
21
    BIN_FOLDER ?= ${HOME}/.local/bin
    NNI_PKG_PATH ?= ${HOME}/.local/nni_pkg
liuzhe-lz's avatar
liuzhe-lz committed
22
23
24
25
    ifndef VIRTUAL_ENV
        PIP_MODE ?= --user
    endif
    BASH_COMP_SCRIPT ?= ${HOME}/.bash_completion.d/nnictl
26
endif
Deshui Yu's avatar
Deshui Yu committed
27

liuzhe-lz's avatar
liuzhe-lz committed
28
## Dependency information
29
30
31
32
33
34
35
$(info $(_INFO) Installing dependencies, use local toolchain $(_END))
NNI_NODE_TARBALL ?= /tmp/nni-node-linux-x64.tar.xz
NNI_NODE_FOLDER = /tmp/nni-node-linux-x64
NNI_NODE ?= $(BIN_FOLDER)/node
NNI_YARN_TARBALL ?= /tmp/nni-yarn.tar.gz
NNI_YARN_FOLDER ?= /tmp/nni-yarn
NNI_YARN := PATH=$(BIN_FOLDER):$${PATH} $(NNI_YARN_FOLDER)/bin/yarn
liuzhe-lz's avatar
liuzhe-lz committed
36
37
38
39

# Main targets

.PHONY: build
Deshui Yu's avatar
Deshui Yu committed
40
build:
liuzhe-lz's avatar
liuzhe-lz committed
41
	#$(_INFO) Building NNI Manager $(_END)
Zejun Lin's avatar
Zejun Lin committed
42
	cd src/nni_manager && $(NNI_YARN) && $(NNI_YARN) build
Deshui Yu's avatar
Deshui Yu committed
43
	
goooxu's avatar
goooxu committed
44
	#$(_INFO) Building WebUI $(_END)
Zejun Lin's avatar
Zejun Lin committed
45
	cd src/webui && $(NNI_YARN) && $(NNI_YARN) build
Deshui Yu's avatar
Deshui Yu committed
46
	
liuzhe-lz's avatar
liuzhe-lz committed
47
	#$(_INFO) Building Python SDK $(_END)
Deshui Yu's avatar
Deshui Yu committed
48
49
	cd src/sdk/pynni && python3 setup.py build
	
liuzhe-lz's avatar
liuzhe-lz committed
50
	#$(_INFO) Building nnictl $(_END)
Deshui Yu's avatar
Deshui Yu committed
51
52
	cd tools && python3 setup.py build

liuzhe-lz's avatar
liuzhe-lz committed
53
54
55
56
57
58
# Standard installation target
# Must be invoked after building
.PHONY: install
install: install-python-modules
install: install-node-modules
install: install-scripts
Deshui Yu's avatar
Deshui Yu committed
59
install:
60
	#$(_INFO) Complete! You may want to add $(BIN_FOLDER) to your PATH environment $(_END)
61

liuzhe-lz's avatar
liuzhe-lz committed
62
63
64
65
66

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


liuzhe-lz's avatar
liuzhe-lz committed
70
# All-in-one target for non-expert users
liuzhe-lz's avatar
liuzhe-lz committed
71
72
73
74
75
# 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
76
easy-install: install
liuzhe-lz's avatar
liuzhe-lz committed
77
78
easy-install: update-bash-config

liuzhe-lz's avatar
liuzhe-lz committed
79
easy-install:
Zejun Lin's avatar
Zejun Lin committed
80
	#$(_INFO) Complete! $(_END)
liuzhe-lz's avatar
liuzhe-lz committed
81
82
83
84
85
86
87
88
89


# 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
liuzhe-lz's avatar
liuzhe-lz committed
90
pip-install: update-bash-config
liuzhe-lz's avatar
liuzhe-lz committed
91
92
93
94
95
96
97
98
99


# 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:
100
	#$(_INFO) Complete! You may want to add $(BIN_FOLDER) to your PATH environment $(_END)
liuzhe-lz's avatar
liuzhe-lz committed
101
102
103
104


.PHONY: uninstall
uninstall:
Zejun Lin's avatar
Zejun Lin committed
105
106
	-$(PIP_UNINSTALL) -y nni
	-$(PIP_UNINSTALL) -y nnictl
107
108
109
	-rm -rf $(NNI_PKG_PATH)
	-rm -f $(BIN_FOLDER)/node
	-rm -f $(BIN_FOLDER)/nnictl
liuzhe-lz's avatar
liuzhe-lz committed
110
111
112
113
114
115
116
	-rm -f $(BASH_COMP_SCRIPT)

# Main targets end


# Helper targets

Zejun Lin's avatar
Zejun Lin committed
117
$(NNI_NODE_TARBALL):
liuzhe-lz's avatar
liuzhe-lz committed
118
	#$(_INFO) Downloading Node.js $(_END)
119
	wget https://aka.ms/nodejs-download -O $(NNI_NODE_TARBALL)
liuzhe-lz's avatar
liuzhe-lz committed
120

Zejun Lin's avatar
Zejun Lin committed
121
$(NNI_YARN_TARBALL):
liuzhe-lz's avatar
liuzhe-lz committed
122
	#$(_INFO) Downloading Yarn $(_END)
123
	wget https://aka.ms/yarn-download -O $(NNI_YARN_TARBALL)
liuzhe-lz's avatar
liuzhe-lz committed
124
125

.PHONY: intall-dependencies
Zejun Lin's avatar
Zejun Lin committed
126
install-dependencies: $(NNI_NODE_TARBALL) $(NNI_YARN_TARBALL)
liuzhe-lz's avatar
liuzhe-lz committed
127
	#$(_INFO) Extracting Node.js $(_END)
128
129
130
131
132
133
	rm -rf $(NNI_NODE_FOLDER)
	mkdir $(NNI_NODE_FOLDER)
	tar -xf $(NNI_NODE_TARBALL) -C $(NNI_NODE_FOLDER) --strip-components 1
	mkdir -p $(BIN_FOLDER)
	rm -f $(NNI_NODE)
	cp $(NNI_NODE_FOLDER)/bin/node $(NNI_NODE)
Deshui Yu's avatar
Deshui Yu committed
134
	
liuzhe-lz's avatar
liuzhe-lz committed
135
	#$(_INFO) Extracting Yarn $(_END)
136
137
138
	rm -rf $(NNI_YARN_FOLDER)
	mkdir $(NNI_YARN_FOLDER)
	tar -xf $(NNI_YARN_TARBALL) -C $(NNI_YARN_FOLDER) --strip-components 1
139

liuzhe-lz's avatar
liuzhe-lz committed
140
141
142
.PHONY: install-python-modules
install-python-modules:
	#$(_INFO) Installing Python SDK $(_END)
Zejun Lin's avatar
Zejun Lin committed
143
	cd src/sdk/pynni && $(PIP_INSTALL) $(PIP_MODE) .
144
	
liuzhe-lz's avatar
liuzhe-lz committed
145
	#$(_INFO) Installing nnictl $(_END)
Zejun Lin's avatar
Zejun Lin committed
146
	cd tools && $(PIP_INSTALL) $(PIP_MODE) .
147

liuzhe-lz's avatar
liuzhe-lz committed
148
149
.PHONY: install-node-modules
install-node-modules:
150
151
152
153
154
155
	#$(_INFO) Installing NNI Package $(_END)
	rm -rf $(NNI_PKG_PATH)
	cp -r src/nni_manager/dist $(NNI_PKG_PATH)
	cp src/nni_manager/package.json $(NNI_PKG_PATH)
	$(NNI_YARN) --prod --cwd $(NNI_PKG_PATH)
	cp -r src/webui/build $(NNI_PKG_PATH)/static
Deshui Yu's avatar
Deshui Yu committed
156

liuzhe-lz's avatar
liuzhe-lz committed
157
158
159
.PHONY: install-dev-modules
install-dev-modules:
	#$(_INFO) Installing Python SDK $(_END)
Zejun Lin's avatar
Zejun Lin committed
160
	cd src/sdk/pynni && $(PIP_INSTALL) $(PIP_MODE) -e .
161
	
liuzhe-lz's avatar
liuzhe-lz committed
162
	#$(_INFO) Installing nnictl $(_END)
Zejun Lin's avatar
Zejun Lin committed
163
	cd tools && $(PIP_INSTALL) $(PIP_MODE) -e .
164

165
	rm -rf $(NNI_PKG_PATH)
166
	
167
168
169
170
	#$(_INFO) Installing NNI Package $(_END)
	ln -sf ${PWD}/src/nni_manager/dist $(NNI_PKG_PATH)
	ln -sf ${PWD}/src/nni_manager/node_modules $(NNI_PKG_PATH)/node_modules
	ln -sf ${PWD}/src/webui/build $(NNI_PKG_PATH)/static
liuzhe-lz's avatar
liuzhe-lz committed
171
172
173
174


.PHONY: install-scripts
install-scripts:
175
176
177
178
179
180
181
182
183
184
185
186
187
	mkdir -p $(BIN_FOLDER)

	touch $(BIN_FOLDER)/nnictl
	echo "#!/usr/bin/python3" >>$(BIN_FOLDER)/nnictl
	echo "# -*- coding: utf-8 -*-" >>$(BIN_FOLDER)/nnictl
	echo "import re" >>$(BIN_FOLDER)/nnictl
	echo "import sys" >>$(BIN_FOLDER)/nnictl
	echo "from nnicmd.nnictl import parse_args" >>$(BIN_FOLDER)/nnictl
	echo "if __name__ == '__main__':" >>$(BIN_FOLDER)/nnictl
	echo "    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$$', '', sys.argv[0])" >>$(BIN_FOLDER)/nnictl
	echo "    sys.exit(parse_args())" >>$(BIN_FOLDER)/nnictl

	chmod +x $(BIN_FOLDER)/nnictl
liuzhe-lz's avatar
liuzhe-lz committed
188
	install -Dm644 tools/bash-completion $(BASH_COMP_SCRIPT)
Deshui Yu's avatar
Deshui Yu committed
189

liuzhe-lz's avatar
liuzhe-lz committed
190
191
192
193
.PHONY: update-bash-config
ifndef _ROOT
update-bash-config:
	#$(_INFO) Updating bash configurations $(_END)
194
195
196
    ifeq (, $(shell echo $$PATH | tr ':' '\n' | grep -x '$(BIN_FOLDER)'))  # $(BIN_FOLDER) not in PATH
	#$(_WARNING) NOTE: adding $(BIN_FOLDER) to PATH in bashrc $(_END)
	echo 'export PATH="$$PATH:$(BIN_FOLDER)"' >> ~/.bashrc
liuzhe-lz's avatar
liuzhe-lz committed
197
198
199
200
201
202
203
    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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
	#$(_INFO) Pass! $(_END)

# Helper targets end