-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmak-cmake.pl
More file actions
executable file
·140 lines (125 loc) · 4.7 KB
/
smak-cmake.pl
File metadata and controls
executable file
·140 lines (125 loc) · 4.7 KB
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
#!/usr/bin/perl
# smak-cmake - Drive CMake builds via smak's own interpreter, with an
# optional fallback to a downloaded real cmake binary.
#
# Dispatch order:
# 1. If SMAK_CMAKE_REAL=1 (or the invocation doesn't look like a config
# run — e.g., `cmake --build`, `cmake -E`), use the real cmake.
# 2. Otherwise try `smak -cmake` (SmakCMakeInterp + generator).
# 3. On any failure, fall through to the real cmake (download if needed).
use strict;
use warnings;
use File::Basename;
use Cwd 'abs_path';
my $script_dir = dirname(abs_path($0));
my $cmake_link = "$script_dir/cmake-install";
my $cmake_bin = "$cmake_link/bin/cmake";
my $smak_pl = "$script_dir/smak.pl";
# CMake version to download (fallback only)
my $cmake_version = "3.31.4";
my $cmake_dirname = "cmake-$cmake_version-linux-x86_64";
my $cmake_archive = "$cmake_dirname.tar.gz";
my $cmake_url = "https://github.com/Kitware/CMake/releases/download/v$cmake_version/$cmake_archive";
sub _run_real {
if (-x $cmake_bin) {
exec($cmake_bin, @ARGV) or die "Failed to exec $cmake_bin: $!\n";
}
# fall through to download path
}
# Decide whether to attempt the smak interp. Non-configure invocations
# (--build, --install, -E, --version) go straight to real cmake.
my $is_config = 1;
my $is_script = 0;
for my $a (@ARGV) {
if ($a eq '--build' || $a eq '--install' || $a eq '-E'
|| $a eq '--version' || $a eq '--help') {
$is_config = 0; last;
}
if ($a eq '-P') { $is_script = 1; last; }
}
$is_config = 0 if $ENV{SMAK_CMAKE_REAL};
$is_config = 0 if $is_script;
# `cmake -P script.cmake [-D ...]`: route to the smak interp's script-eval
# mode so the bundled fallback cmake can be removed entirely. Used by build-
# time custom commands (e.g., Xyce's BuildTimeStamp.cmake).
if ($is_script && -f $smak_pl && !$ENV{SMAK_CMAKE_REAL}) {
my @args;
my $script;
for (my $i = 0; $i < @ARGV; $i++) {
my $a = $ARGV[$i];
if ($a eq '-D') { push @args, '-D', $ARGV[++$i]; }
elsif ($a =~ /^-D./) { push @args, $a; }
elsif ($a eq '-P') { $script = $ARGV[++$i]; }
# Other flags (e.g., --log-level) ignored.
}
if (defined $script) {
local $ENV{PERLLIB} = ($ENV{PERLLIB} // '') eq '' ? $script_dir : "$script_dir:$ENV{PERLLIB}";
my $rc = system('perl', $smak_pl, '-cmake-script', @args, $script);
exit 0 if $rc == 0;
warn "smak -cmake-script failed (rc=", ($rc >> 8), "); falling back to real cmake\n";
}
}
if ($is_config && -f $smak_pl) {
# Call smak.pl directly (NOT via the bash wrapper `smak`, which would
# re-enter this script and loop). smak.pl's first line checks for
# -cmake and dispatches to SmakCMakeInterp + generate_makefiles.
local $ENV{PERLLIB} = ($ENV{PERLLIB} // '') eq '' ? $script_dir : "$script_dir:$ENV{PERLLIB}";
my $rc = system('perl', $smak_pl, '-cmake', @ARGV);
if ($rc == 0) { exit 0; }
warn "smak -cmake failed (rc=", ($rc >> 8), "); falling back to real cmake\n";
}
if (-x $cmake_bin) {
exec($cmake_bin, @ARGV) or die "Failed to exec $cmake_bin: $!\n";
}
# cmake not found — auto-download, or prompt if interactive
print "CMake not found at $cmake_bin\n";
my $auto = !-t STDIN; # non-interactive (piped / script) → auto-yes
if (!$auto) {
print "Download cmake $cmake_version? [Y/n] ";
my $answer = <STDIN>;
chomp($answer);
if ($answer =~ /^n/i) {
print "Aborted.\n";
exit 1;
}
}
# Download and extract
print "Downloading $cmake_url...\n";
my $archive_path = "$script_dir/$cmake_archive";
system("curl", "-L", "-o", $archive_path, $cmake_url) == 0
or die "Download failed\n";
print "Extracting to $script_dir/$cmake_dirname...\n";
system("tar", "-xzf", $archive_path, "-C", $script_dir) == 0
or die "Extraction failed\n";
# Create symlink
if (-e $cmake_link || -l $cmake_link) {
if (-l $cmake_link) {
unlink($cmake_link);
} elsif (-d $cmake_link) {
die "Cannot create symlink: $cmake_link is a directory. Please remove it manually.\n";
} else {
unlink($cmake_link);
}
}
symlink($cmake_dirname, $cmake_link)
or die "Failed to create symlink: $!\n";
# Clean up archive (auto-delete in non-interactive mode)
if ($auto) {
unlink($archive_path);
print "Archive deleted.\n";
} else {
print "Delete archive $cmake_archive? [Y/n] ";
my $answer = <STDIN>;
chomp($answer);
if ($answer !~ /^n/i) {
unlink($archive_path);
print "Archive deleted.\n";
}
}
if (-x $cmake_bin) {
print "CMake installed successfully.\n";
print "Running: $cmake_bin @ARGV\n\n";
exec($cmake_bin, @ARGV) or die "Failed to exec $cmake_bin: $!\n";
} else {
die "Installation failed - $cmake_bin not found\n";
}