-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMakefile
137 lines (119 loc) · 5.59 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
SHELL := /bin/bash
# Determine the operating system of the host machine
UNAME := $(shell uname)
MACHINE := $(if $(filter Darwin,$(UNAME)),Mac,$(if $(filter Linux,$(UNAME)),Linux,UNKNOWN))
# Determine the machine architecture
UNAME_M := $(shell uname -m)
ARCH := $(if $(filter arm64 aarch64,$(UNAME_M)),arm64,amd64)
AARCH := $(if $(filter arm64 aarch64,$(UNAME_M)),aarch64,x86_64)
# Get the current user's ID and name
USERID := $(shell id -u)
USERID_N := $(shell id -u -n)
# Option to install additional packages, set to 0/false by default
OPTIONAL_PKGS ?= 0
# Option to install Macaulay2
INSTALL_M2 ?= 0
# Option to install Sage
INSTALL_SAGE ?= 0
# Default build type is set to 'build'
BUILD_TYPE := build
# Define phony targets to prevent conflicts with files of the same name
.PHONY: all build-common build build-fast build-with-root-user install uninstall run test check-not-root-user get-sudo-credentials
# Default target; displays a message to the user
all:
@echo "Please specify an instruction (e.g make install)."
# Check if the current user is root and exit if true
check-not-root-user:
@if [ "$(USERID)" = "0" ]; then \
echo "Please run make as a non-root user and without sudo!"; \
exit 1; \
fi
# Request sudo credentials from the user
get-sudo-credentials:
@sudo -n true 2>/dev/null || { \
echo -n "Building a Docker image requires sudo privileges. "; \
sudo -v; \
}
# Common build steps for non-root build types
build-common:
@echo "Building CYTools image for user $(USERID_N)..."
@{ sudo docker pull ubuntu:noble && \
sudo docker build $(DOCKER_BUILD_OPTS) -t cytools:uid-$(USERID) \
--build-arg UNAME=cytools --build-arg UID=$(USERID) \
--build-arg ARCH=$(ARCH) --build-arg AARCH=$(AARCH) \
--build-arg VIRTUAL_ENV=/home/cytools/cytools-venv/ \
--build-arg ALLOW_ROOT_ARG=" " \
--build-arg OPTIONAL_PKGS=$(OPTIONAL_PKGS) \
--build-arg INSTALL_M2=$(INSTALL_M2) \
--build-arg INSTALL_SAGE=$(INSTALL_SAGE) \
--build-arg PORT_ARG=$$(( $(USERID) + 2875 )) .; } > build.log
@echo "Successfully built CYTools image for user $(USERID_N)"
# Standard build process
build: DOCKER_BUILD_OPTS=--no-cache --force-rm
build: check-not-root-user get-sudo-credentials
@echo -n "Deleting old CYTools image... "
@sudo docker rmi cytools:uid-$(USERID) > /dev/null 2>&1 && echo "done!" || echo "old CYTools image does not exist or cannot be deleted..."
@$(MAKE) -C . --no-print-directory build-common DOCKER_BUILD_OPTS='$(DOCKER_BUILD_OPTS)'
# Fast build process, allows cached info for quicker build
build-fast: DOCKER_BUILD_OPTS=
build-fast: check-not-root-user get-sudo-credentials
@$(MAKE) -C . --no-print-directory build-common DOCKER_BUILD_OPTS='$(DOCKER_BUILD_OPTS)'
# Build process when running as root user
build-with-root-user:
@ echo " "
@ echo "********************************************************************"
@ echo "Warning: You are building an image with a root user. Any user with "
@ echo "access to this image will be able to have root access to the host "
@ echo "computer as well. Please proceed with care.";
@ echo "********************************************************************"
@ echo " "
@ read -p "Press enter to continue or ctrl+c to cancel"
@echo "Deleting old CYTools image..."
@sudo docker rmi cytools:root > /dev/null 2>&1 && echo "done!" || echo "old CYTools image does not exist or cannot be deleted..."
@echo "Building CYTools image for root user..."
@{ sudo docker pull ubuntu:noble && \
sudo docker build -t cytools:root \
--build-arg UNAME=root --build-arg UID=0 \
--build-arg ARCH=$(ARCH) --build-arg AARCH=$(AARCH) \
--build-arg VIRTUAL_ENV=/opt/cytools/cytools-venv/ \
--build-arg ALLOW_ROOT_ARG="--allow-root" \
--build-arg OPTIONAL_PKGS=$(OPTIONAL_PKGS) \
--build-arg INSTALL_M2=$(INSTALL_M2) \
--build-arg INSTALL_SAGE=$(INSTALL_SAGE) \
--build-arg PORT_ARG=2875 .; } > build.log
@echo "Successfully built CYTools image with root user."
# Installation process
install: $(BUILD_TYPE)
@echo "Copying launcher script and associated files..."
@if [ "$(MACHINE)" = "Mac" ]; then \
sudo cp scripts/macos/cytools /usr/local/bin/cytools; \
sudo chmod +x /usr/local/bin/cytools; \
sudo mkdir -p /Applications/CYTools.app/Contents/MacOS/; \
sudo cp scripts/macos/info.plist /Applications/CYTools.app/Contents/info.plist; \
sudo cp scripts/macos/CYToolsApp /Applications/CYTools.app/Contents/MacOS/CYToolsApp; \
sudo chmod +x /Applications/CYTools.app/Contents/MacOS/CYToolsApp; \
sudo cp scripts/macos/launcher.sh /Applications/CYTools.app/Contents/MacOS/launcher.sh; \
sudo chmod +x /Applications/CYTools.app/Contents/MacOS/launcher.sh; \
sudo mkdir -p /Applications/CYTools.app/Contents/Resources/; \
sudo cp scripts/macos/AppIcon.icns /Applications/CYTools.app/Contents/Resources/AppIcon.icns; \
else \
sudo cp scripts/linux/cytools /usr/local/bin/cytools; \
sudo chmod +x /usr/local/bin/cytools; \
sudo cp scripts/linux/cytools.png /usr/share/pixmaps/cytools.png; \
sudo cp scripts/linux/cytools.desktop /usr/share/applications/cytools.desktop; \
fi
@echo "Installation finished successfully!"
# Uninstallation process
uninstall: check-not-root-user
@if [ "$(MACHINE)" = "Mac" ]; then \
sudo rm -rf /Applications/CYTools.app/; \
sudo rm -f /usr/local/bin/cytools; \
else \
sudo rm -f /usr/local/bin/cytools; \
sudo rm -f /usr/share/pixmaps/cytools.png; \
sudo rm -f /usr/share/applications/cytools.desktop; \
fi
@sudo docker rmi cytools:uid-$(USERID) || true
# Test the application
test: check-not-root-user
sudo docker run --rm -it cytools:uid-$(USERID) bash -c "cd /opt/cytools/unittests/; bash /opt/cytools/unittests/run_tests.sh"