-
Notifications
You must be signed in to change notification settings - Fork 87
/
Makefile
317 lines (246 loc) · 9.4 KB
/
Makefile
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
CXXFLAGS += -Wall -Wextra -pedantic -Wnon-virtual-dtor -std=c++14
LDFLAGS +=
CXX_CLANG := $(shell $(CXX) --version 2>/dev/null | grep clang)
BREW := $(shell which brew 2>/dev/null)
ifneq ($(BREW),)
CXXFLAGS += -I$(shell brew --prefix)/include
LDFLAGS += -L$(shell brew --prefix)/lib
endif
ifeq "$(findstring debug, $(MAKECMDGOALS))" "debug"
CXXFLAGS += -O0 -g
else
ifeq "$(CXX_CLANG)" ""
CXXFLAGS += -O4
ifneq "$(findstring noomp, $(MAKECMDGOALS))" "noomp"
CXXFLAGS += -fopenmp
LDFLAGS += -fopenmp
endif
else
CXXFLAGS += -Wshadow -O3
ifneq "$(findstring noomp, $(MAKECMDGOALS))" "noomp"
CXXFLAGS += -Xpreprocessor -fopenmp
LDFLAGS += -lomp
endif
endif
endif
##################################################
# General file dependencies
##################################################
HEADERS := $(shell find src -name "*.h")
SOURCES := $(shell find src -name "*.cpp")
OBJECTS := $(SOURCES:src/%.cpp=build/Infomap/%.o)
##################################################
# Stand-alone C++ targets
##################################################
.PHONY: all noomp test debug format
all: Infomap
@true
Infomap: $(OBJECTS)
@echo "Linking object files to target $@..."
$(CXX) $(LDFLAGS) -o $@ $^
@echo "-- Link finished --"
## Generic compilation rule for object files from cpp files
build/Infomap/%.o : src/%.cpp $(HEADERS) Makefile
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) -c $< -o $@
noomp: Infomap
@true
debug: Infomap
@true
format: js-format py-format
clang-format -i $(HEADERS) $(SOURCES)
##################################################
# JavaScript through Emscripten
##################################################
.PHONY: js-worker js-clean js-test
WORKER_FILENAME := infomap.worker.js
PRE_WORKER_MODULE := interfaces/js/pre-worker-module.js
js-worker: build/js/$(WORKER_FILENAME) Infomap
@echo "Built $^"
@mkdir -p interfaces/js/src/worker
cp build/js/* interfaces/js/src/worker/
npm run build
cp interfaces/js/README.md .
js-test:
$(RM) -r package mapequation-infomap-*.tgz
npm pack
tar -xzvf mapequation-infomap-*.tgz
cp package/index.js examples/js
sed -i.'backup' -e 's/src=".*"/src="index.js"/' \
examples/js/infomap-worker.html
open examples/js/infomap-worker.html
sleep 5
$(RM) -r package
$(RM) -r mapequation-infomap-*.tgz
$(RM) -r examples/js/index.js
$(RM) -r examples/js/infomap-worker.html
mv examples/js/infomap-worker.html{.backup,}
build/js/infomap.worker.js: $(SOURCES) $(PRE_WORKER_MODULE)
@echo "Compiling Infomap to run in a worker in the browser..."
@mkdir -p $(dir $@)
em++ -std=c++14 -O3 -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s DISABLE_EXCEPTION_CATCHING=0 -s ENVIRONMENT=worker --pre-js $(PRE_WORKER_MODULE) -o build/js/$(WORKER_FILENAME) $(SOURCES)
js-clean:
$(RM) -r build/js interfaces/js/src/worker index.js *.d.ts README.md
js-format:
prettier --write interfaces/js
##################################################
# Static C++ library
##################################################
# Use separate object files to compile with definitions
# NS_INFOMAP: Wrap code in namespace infomap
# AS_LIB: Skip main function
LIB_DIR = build/lib
LIB_HEADERS := $(HEADERS:src/%.h=include/%.h)
LIB_OBJECTS := $(SOURCES:src/%.cpp=build/lib/%.o)
.PHONY: lib
lib: lib/libInfomap.a $(LIB_HEADERS)
@echo "Wrote static library to lib/ and headers to include/"
lib/libInfomap.a: $(LIB_OBJECTS) Makefile
@echo "Creating static library..."
@mkdir -p lib
ar rcs $@ $^
# Rule for $(LIB_HEADERS)
include/%.h: src/%.h
@mkdir -p $(dir $@)
@cp -a $^ $@
# Rule for $(LIB_OBJECTS)
build/lib/%.o: src/%.cpp
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) -DNS_INFOMAP -DAS_LIB -c $< -o $@
##################################################
# General SWIG helpers
##################################################
SWIG_FILES := $(shell find interfaces/swig -name "*.i")
##################################################
# Python module
##################################################
PY_BUILD_DIR = build/py
PY_ONLY_HEADERS := $(HEADERS:%.h=$(PY_BUILD_DIR)/headers/%.h)
PY_HEADERS := $(HEADERS:src/%.h=$(PY_BUILD_DIR)/src/%.h)
PY_SOURCES := $(SOURCES:src/%.cpp=$(PY_BUILD_DIR)/src/%.cpp)
.PHONY: python py-swig py-build
# Use python distutils to compile the module
python: py-swig py-build Makefile
@true
py-build: Makefile
@cp -a interfaces/python/setup.py $(PY_BUILD_DIR)/
@touch $(PY_BUILD_DIR)/__init__.py
@python utils/create-python-package-meta.py $(PY_BUILD_DIR)/package_meta.py
@cat $(PY_BUILD_DIR)/package_meta.py $(PY_BUILD_DIR)/infomap.py > $(PY_BUILD_DIR)/temp.py
@mv $(PY_BUILD_DIR)/temp.py $(PY_BUILD_DIR)/infomap.py
@autopep8 --jobs 8 --aggressive --aggressive -i $(PY_BUILD_DIR)/infomap.py
@cp -a interfaces/python/MANIFEST.in $(PY_BUILD_DIR)/
@cp -a README.rst $(PY_BUILD_DIR)/
@cp -a LICENSE_GPLv3.txt $(PY_BUILD_DIR)/LICENSE
@cd $(PY_BUILD_DIR) && CC=$(CXX) python setup.py build_ext --inplace
# Generate wrapper files from source and interface files
py-swig: $(PY_HEADERS) $(PY_SOURCES) $(PY_ONLY_HEADERS) interfaces/python/infomap.py
@mkdir -p $(PY_BUILD_DIR)
@cp -a $(SWIG_FILES) $(PY_BUILD_DIR)/
swig -c++ -python -outdir $(PY_BUILD_DIR) -o $(PY_BUILD_DIR)/infomap_wrap.cpp $(PY_BUILD_DIR)/Infomap.i
# Rule for $(PY_HEADERS) and $(PY_SOURCES)
$(PY_BUILD_DIR)/src/%: src/%
@mkdir -p $(dir $@)
@cp -a $^ $@
$(PY_BUILD_DIR)/headers/%: %
@mkdir -p $(dir $@)
@cp -a $^ $@
.PHONY: py-doc py-local-install
SPHINX_SOURCE_DIR = interfaces/python/source
SPHINX_TARGET_DIR = docs
py-test:
@cp -r examples/networks/*.net $(PY_BUILD_DIR)
python -m flake8 --count --show-source --statistics --ignore E501,F811,W503 $(PY_BUILD_DIR)/infomap.py
cd $(PY_BUILD_DIR) && python -m doctest infomap.py
cd examples/python && for f in *.py; do python "$$f" > /dev/null || exit 1; done
py-local-install:
# Run this to get 'import infomap' to always import the latest
# locally built version, so no need to run this multiple times.
pip install -e $(PY_BUILD_DIR)
py-doc: py-local-install
# Uses docstrings from the infomap available with 'import infomap'.
# Run py-local-install if you don't have pip installed it with -e
# and don't have the latest version installed
@mkdir -p $(SPHINX_TARGET_DIR)
@cp -a README.rst ${SPHINX_SOURCE_DIR}/index.rst
sphinx-build -b html $(SPHINX_SOURCE_DIR) $(SPHINX_TARGET_DIR)
@rm -r ${SPHINX_SOURCE_DIR}/index.rst
npx prettier --write docs/searchindex.js
.PHONY: pypitest-publish pypi-publish py-clean
PYPI_DIR = $(PY_BUILD_DIR)
PYPI_SDIST = $(shell find $(PYPI_DIR) -name "*.tar.gz" 2>/dev/null)
py-clean:
$(RM) -r $(PY_BUILD_DIR)/dist
py-format:
python -m isort interfaces/python examples/python
python -m black interfaces/python examples/python
pypi-dist:
cd $(PY_BUILD_DIR) && python setup.py sdist bdist_wheel
# pip -vvv --no-cache-dir install --upgrade -I --index-url https://test.pypi.org/simple/ infomap
# pip install -e build/py/pypi/infomap/
pypitest-publish:
@[ "${PYPI_SDIST}" ] && echo "Publish dist..." || ( echo "dist files not built"; exit 1 )
cd $(PYPI_DIR) && python -m twine upload -r testpypi --verbose dist/*
pypi-publish:
@[ "${PYPI_SDIST}" ] && echo "Publish dist..." || ( echo "dist files not built"; exit 1 )
cd $(PYPI_DIR) && python -m twine upload --skip-existing --verbose dist/*
##################################################
# R module
##################################################
R_BUILD_DIR = build/R
R_HEADERS := $(HEADERS:src/%.h=$(R_BUILD_DIR)/src/%.h)
R_SOURCES := $(SOURCES:src/%.cpp=$(R_BUILD_DIR)/src/%.cpp)
.PHONY: R R-build
# Use R to compile the module
R: R-build Makefile
@mkdir -p $(R_BUILD_DIR)/Infomap
@cp -a examples/R/load-infomap.R $(R_BUILD_DIR)/Infomap/
cd $(R_BUILD_DIR) && CXX="$(CXX)" PKG_CPPFLAGS="$(CXXFLAGS) -DAS_LIB" PKG_LIBS="$(LDFLAGS)" R CMD SHLIB infomap_wrap.cpp $(SOURCES)
@cp -a $(R_BUILD_DIR)/infomap.R $(R_BUILD_DIR)/Infomap/
@cp -a $(R_BUILD_DIR)/infomap_wrap.so $(R_BUILD_DIR)/Infomap/infomap.so
@cp -a examples/R/example-minimal.R $(R_BUILD_DIR)/Infomap/
@true
# Generate wrapper files from source and interface files
R-build: Makefile $(R_HEADERS) $(R_SOURCES)
@mkdir -p $(R_BUILD_DIR)
@cp -a $(SWIG_FILES) $(R_BUILD_DIR)/
swig -c++ -r -outdir $(R_BUILD_DIR) -o $(R_BUILD_DIR)/infomap_wrap.cpp $(R_BUILD_DIR)/Infomap.i
# Rule for $(R_HEADERS) and $(R_SOURCES)
$(R_BUILD_DIR)/src/%: src/%
@mkdir -p $(dir $@)
@cp -a $^ $@
##################################################
# Docker
##################################################
TAG_NAME = mapequation/infomap
docker-build: Makefile docker/infomap.Dockerfile
docker-compose build infomap
docker-run: Makefile
docker-compose run --rm infomap
docker-build-notebook: Makefile docker/notebook.Dockerfile
docker-compose build notebook
docker-run-notebook: Makefile
docker-compose up notebook
# rstudio
docker-build-rstudio: Makefile docker/rstudio.Dockerfile
docker build \
-f docker/rstudio.Dockerfile \
-t $(TAG_NAME):rstudio .
docker-run-rstudio: Makefile
docker run --rm \
$(TAG_NAME):rstudio
# ubuntu test python
docker-build-ubuntu-test-python: Makefile
docker build -f docker/ubuntu.Dockerfile -t infomap:python-test .
docker-run-ubuntu-test-python: Makefile
docker run --rm infomap:python-test
# R with RStudio
docker-build-r: Makefile
docker build -f docker/rstudio.Dockerfile -t infomap:r .
docker-run-r: Makefile
docker run --rm -p 8787:8787 -e PASSWORD=InfomapR infomap:r
##################################################
# Clean
##################################################
clean: js-clean
$(RM) -r Infomap build lib include