Skip to content

Commit 3a351a1

Browse files
committed
add install_cmake script
1 parent 7137c98 commit 3a351a1

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

scripts/install_cmake.m

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
%% INSTALL_CMAKE downloads and extracts a specific version of CMake for the
2+
% current platform.
3+
%
4+
% install_cmake(prefix, version)
5+
%
6+
% Inputs:
7+
% prefix: The destination path to install CMake.
8+
% version: The version number of CMake to download (e.g., '3.29.3').
9+
%
10+
% The script will download the appropriate CMake archive and extract it
11+
% into a subfolder within the specified prefix. It then prints the necessary
12+
% PATH modification to the command window.
13+
14+
function install_cmake(prefix, cmake_version)
15+
arguments
16+
prefix (1,1) string
17+
cmake_version (1,1) string
18+
end
19+
20+
stub = '';
21+
ext = ".tar.gz";
22+
name_arch = '';
23+
24+
if ispc()
25+
os = 'windows';
26+
switch getenv("PROCESSOR_ARCHITECTURE")
27+
case 'ARM64', name_arch = 'arm64';
28+
case 'AMD64', name_arch = 'x86_64';
29+
end
30+
ext = ".zip";
31+
elseif ismac()
32+
os = 'macos';
33+
name_arch = 'universal';
34+
stub = 'CMake.app/Contents/';
35+
elseif isunix()
36+
os = 'linux';
37+
[s, m] = system('uname -m');
38+
assert(s==0, "could not determine CPU architecture")
39+
switch m
40+
case {'aarch64', 'arm64'}, name_arch = 'aarch64';
41+
case 'x86_64', name_arch = 'x86_64';
42+
end
43+
end
44+
mustBeNonempty(name_arch)
45+
46+
% Compose the URL for the download.
47+
archive_name = sprintf("cmake-%s-%s-%s", cmake_version, os, name_arch);
48+
archive_file = archive_name + ext;
49+
archive_path = fullfile(prefix, archive_file);
50+
url = sprintf("https://github.com/Kitware/CMake/releases/download/v%s/%s", cmake_version, archive_file);
51+
52+
fprintf('%s => %s\n', url, archive_path);
53+
if ~isfolder(prefix)
54+
mkdir(prefix);
55+
end
56+
websave(archive_path, url);
57+
58+
% Extract the archive based on its type.
59+
dest_dir = fullfile(prefix, archive_name);
60+
fprintf('Extracting to %s\n', dest_dir);
61+
switch ext
62+
case ".zip"
63+
unzip(archive_path, prefix);
64+
case ".tar.gz"
65+
untar(archive_path, prefix);
66+
end
67+
delete(archive_path);
68+
69+
disp("Installation complete in " + prefix)
70+
71+
newpath = fullfile(dest_dir, stub, 'bin');
72+
if isunix()
73+
disp("Please add the following line to your shell configuration file (e.g., .bashrc, .zshrc, .profile) to update your PATH:")
74+
fprintf(' export PATH="%s":$PATH\n', newpath);
75+
else
76+
disp("Please add to user environment variable PATH")
77+
disp(newpath)
78+
end
79+
80+
end

0 commit comments

Comments
 (0)