-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsmoke_compat_portable.sh
More file actions
executable file
·470 lines (413 loc) · 14 KB
/
Copy pathsmoke_compat_portable.sh
File metadata and controls
executable file
·470 lines (413 loc) · 14 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/env bash
# Cross-platform smoke tests for compat packages that should not depend on
# Linux/X11 runtime libraries.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
MCPP_BIN="${MCPP:-}"
if [[ -z "$MCPP_BIN" ]]; then
MCPP_BIN="$(command -v mcpp || true)"
fi
to_native_path() {
if [[ "${OS:-}" == "Windows_NT" ]] && command -v cygpath >/dev/null 2>&1; then
cygpath -m "$1"
else
printf '%s\n' "$1"
fi
}
to_posix_path() {
if [[ "${OS:-}" == "Windows_NT" ]] && command -v cygpath >/dev/null 2>&1; then
cygpath -u "$1"
else
printf '%s\n' "$1"
fi
}
MCPP_BIN_POSIX=""
if [[ -n "$MCPP_BIN" ]]; then
MCPP_BIN_POSIX="$(to_posix_path "$MCPP_BIN")"
fi
if [[ -z "$MCPP_BIN_POSIX" || ! -f "$MCPP_BIN_POSIX" ]]; then
echo "FATAL: set MCPP=/path/to/mcpp or put mcpp on PATH" >&2
exit 1
fi
platform="$(uname -s)"
if [[ "${OS:-}" == "Windows_NT" ]]; then
platform="Windows_NT"
fi
case "$platform" in
Windows_NT|Darwin)
TOOLCHAIN="${MCPP_INDEX_PORTABLE_TOOLCHAIN:-llvm@20.1.7}"
;;
*)
TOOLCHAIN="${MCPP_INDEX_PORTABLE_TOOLCHAIN:-gcc@16.1.0}"
;;
esac
TMP="$(mktemp -d)"
if [[ "${MCPP_INDEX_KEEP_SMOKE_TMP:-0}" == "1" ]]; then
echo "KEEP: $TMP"
else
trap 'rm -rf "$TMP"' EXIT
fi
MCPP_HOME_POSIX="$TMP/mcpp-home"
mkdir -p "$MCPP_HOME_POSIX"
export MCPP_HOME="$(to_native_path "$MCPP_HOME_POSIX")"
INDEX_ROOT="$(to_native_path "$ROOT")"
SMOKE_CACHE_DIR="${MCPP_INDEX_SMOKE_CACHE_DIR:-}"
USER_MCPP="${MCPP_INDEX_USER_MCPP_HOME:-${HOME}/.mcpp}"
mkdir -p "$MCPP_HOME_POSIX/registry/data/xpkgs"
link_xpkgs() {
local src="$1"
[[ -d "$src" ]] || return 0
find "$src" -mindepth 1 -maxdepth 1 -type d | while read -r pkg; do
[[ "$(basename "$pkg")" == compat-x-* ]] && continue
ln -s "$pkg" "$MCPP_HOME_POSIX/registry/data/xpkgs/$(basename "$pkg")" 2>/dev/null || true
done
}
link_xpkgs "${MCPP_INDEX_SMOKE_XPKGS_DIR:-}"
link_xpkgs "$USER_MCPP/registry/data/xpkgs"
if [[ -d "$USER_MCPP/registry/data/xim-pkgindex" ]]; then
mkdir -p "$MCPP_HOME_POSIX/registry/data/xim-pkgindex"
cp -a "$USER_MCPP/registry/data/xim-pkgindex/." "$MCPP_HOME_POSIX/registry/data/xim-pkgindex/" 2>/dev/null || true
rm -f "$MCPP_HOME_POSIX/registry/data/xim-pkgindex/.xlings-index-cache.json"
fi
if [[ -d "$USER_MCPP/registry/bin" ]]; then
mkdir -p "$MCPP_HOME_POSIX/registry"
ln -s "$USER_MCPP/registry/bin" "$MCPP_HOME_POSIX/registry/bin" 2>/dev/null || true
fi
if [[ -f "$USER_MCPP/config.toml" ]]; then
cp -f "$USER_MCPP/config.toml" "$MCPP_HOME_POSIX/config.toml" 2>/dev/null || true
fi
"$MCPP_BIN_POSIX" self config --mirror "${MCPP_INDEX_MIRROR:-GLOBAL}"
copy_smoke_cache() {
[[ -n "$SMOKE_CACHE_DIR" && -d "$SMOKE_CACHE_DIR" ]] || return 0
mkdir -p .mcpp/.xlings/data/runtimedir
find "$SMOKE_CACHE_DIR" -maxdepth 1 -type f \
\( -name '*.tar.gz' -o -name '*.tar.xz' -o -name '*.zip' \) \
-exec cp -f {} .mcpp/.xlings/data/runtimedir/ \;
}
write_build_ldflags() {
case "$platform" in
Linux)
cat <<'EOF'
[build]
ldflags = ["-ldl", "-lm"]
EOF
;;
Darwin)
cat <<'EOF'
[build]
ldflags = ["-lm"]
EOF
;;
esac
}
make_project() {
local name="$1"
mkdir -p "$TMP/$name/src"
cd "$TMP/$name"
cat > mcpp.toml <<EOF
[package]
name = "$name"
version = "0.1.0"
[toolchain]
default = "$TOOLCHAIN"
[indices]
compat = { path = "$INDEX_ROOT" }
[targets.$name]
kind = "bin"
main = "src/main.cpp"
EOF
copy_smoke_cache
}
make_project "compat-portable-core-smoke"
cat >> mcpp.toml <<'EOF'
[dependencies.compat]
gtest = { version = "1.15.2", features = ["main"] }
ftxui = "6.1.9"
lua = "5.4.7"
mbedtls = "3.6.1"
opengl = "2026.05.31"
khrplatform = "2026.05.31"
EOF
write_build_ldflags >> mcpp.toml
cat > src/main.cpp <<'EOF'
#include <array>
#include <string>
#include <GL/gl.h>
#include <KHR/khrplatform.h>
#include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp>
#include <gtest/gtest.h>
#include <mbedtls/sha256.h>
extern "C" {
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
}
TEST(CompatPortableCore, UpstreamHeadersAndRuntime) {
using namespace ftxui;
Element document = hbox({text("compat"), separator(), text("ftxui")});
Screen screen = Screen::Create(Dimension::Fit(document), Dimension::Fit(document));
Render(screen, document);
const std::string rendered = screen.ToString();
EXPECT_NE(rendered.find("compat"), std::string::npos);
EXPECT_NE(rendered.find("ftxui"), std::string::npos);
lua_State* state = luaL_newstate();
ASSERT_NE(state, nullptr);
luaL_openlibs(state);
ASSERT_EQ(luaL_dostring(state, "return 20 + 22"), LUA_OK);
EXPECT_TRUE(lua_isinteger(state, -1));
EXPECT_EQ(lua_tointeger(state, -1), 42);
lua_close(state);
const unsigned char input[] = "abc";
std::array<unsigned char, 32> out{};
mbedtls_sha256(input, 3, out.data(), 0);
EXPECT_EQ(out[0], 0xba);
EXPECT_EQ(out[1], 0x78);
EXPECT_EQ(out[30], 0x15);
EXPECT_EQ(out[31], 0xad);
EXPECT_EQ(GL_TEXTURE_2D, 0x0DE1);
EXPECT_EQ(static_cast<khronos_uint32_t>(1), 1u);
}
EOF
"$MCPP_BIN_POSIX" build
"$MCPP_BIN_POSIX" run
make_project "compat-portable-archive-smoke"
cat >> mcpp.toml <<'EOF'
[dependencies.compat]
libarchive = "3.8.7"
EOF
cat > src/main.cpp <<'EOF'
#include <archive.h>
#include <archive_entry.h>
#include <bzlib.h>
#include <lz4.h>
#include <lzma.h>
#include <zlib.h>
#include <zstd.h>
int main() {
archive* writer = archive_write_new();
if (!writer) return 1;
archive_write_free(writer);
archive_entry* entry = archive_entry_new();
if (!entry) return 2;
archive_entry_free(entry);
if (!archive_version_string()) return 3;
if (!zlibVersion()) return 4;
if (!BZ2_bzlibVersion()) return 5;
if (LZ4_versionNumber() <= 0) return 6;
if (ZSTD_versionNumber() == 0) return 7;
if (lzma_version_number() == 0) return 8;
return 0;
}
EOF
"$MCPP_BIN_POSIX" build
"$MCPP_BIN_POSIX" run
make_project "compat-portable-compression-smoke"
cat >> mcpp.toml <<'EOF'
[dependencies.compat]
zlib = "1.3.2"
bzip2 = "1.0.8"
lz4 = "1.10.0"
xz = "5.8.3"
zstd = "1.5.7"
EOF
cat > src/main.cpp <<'EOF'
#include <bzlib.h>
#include <lz4.h>
#include <lzma.h>
#include <zlib.h>
#include <zstd.h>
#include <cstdint>
#include <cstring>
#include <limits>
#include <vector>
static bool same_bytes(const void* actual, size_t actual_size,
const void* expected, size_t expected_size) {
return actual_size == expected_size &&
std::memcmp(actual, expected, expected_size) == 0;
}
int main() {
const uint8_t input[] = "mcpp compat compression smoke";
const size_t input_size = sizeof(input) - 1;
uint8_t zlib_compressed[256] = {};
uLongf zlib_compressed_size = sizeof(zlib_compressed);
if (compress2(zlib_compressed, &zlib_compressed_size, input,
static_cast<uLong>(input_size), Z_BEST_SPEED) != Z_OK) {
return 1;
}
uint8_t zlib_output[sizeof(input)] = {};
uLongf zlib_output_size = input_size;
if (uncompress(zlib_output, &zlib_output_size, zlib_compressed,
zlib_compressed_size) != Z_OK ||
!same_bytes(zlib_output, zlib_output_size, input, input_size)) {
return 2;
}
char bzip2_compressed[256] = {};
unsigned int bzip2_compressed_size = sizeof(bzip2_compressed);
if (BZ2_bzBuffToBuffCompress(
bzip2_compressed, &bzip2_compressed_size,
const_cast<char*>(reinterpret_cast<const char*>(input)),
static_cast<unsigned int>(input_size), 1, 0, 30) != BZ_OK) {
return 3;
}
char bzip2_output[sizeof(input)] = {};
unsigned int bzip2_output_size = input_size;
if (BZ2_bzBuffToBuffDecompress(bzip2_output, &bzip2_output_size,
bzip2_compressed, bzip2_compressed_size,
0, 0) != BZ_OK ||
!same_bytes(bzip2_output, bzip2_output_size, input, input_size)) {
return 4;
}
char lz4_compressed[256] = {};
const int lz4_compressed_size =
LZ4_compress_default(reinterpret_cast<const char*>(input),
lz4_compressed, static_cast<int>(input_size),
sizeof(lz4_compressed));
if (lz4_compressed_size <= 0) {
return 5;
}
char lz4_output[sizeof(input)] = {};
const int lz4_output_size =
LZ4_decompress_safe(lz4_compressed, lz4_output, lz4_compressed_size,
sizeof(lz4_output));
if (lz4_output_size < 0 ||
!same_bytes(lz4_output, static_cast<size_t>(lz4_output_size), input,
input_size)) {
return 6;
}
std::vector<char> zstd_compressed(ZSTD_compressBound(input_size));
const size_t zstd_compressed_size =
ZSTD_compress(zstd_compressed.data(), zstd_compressed.size(), input,
input_size, 1);
if (ZSTD_isError(zstd_compressed_size)) {
return 7;
}
std::vector<char> zstd_output(input_size);
const size_t zstd_output_size =
ZSTD_decompress(zstd_output.data(), zstd_output.size(),
zstd_compressed.data(), zstd_compressed_size);
if (ZSTD_isError(zstd_output_size) ||
!same_bytes(zstd_output.data(), zstd_output_size, input, input_size)) {
return 8;
}
std::vector<uint8_t> xz_compressed(lzma_stream_buffer_bound(input_size));
size_t xz_compressed_pos = 0;
if (lzma_easy_buffer_encode(0, LZMA_CHECK_CRC64, nullptr, input,
input_size, xz_compressed.data(),
&xz_compressed_pos,
xz_compressed.size()) != LZMA_OK) {
return 9;
}
uint64_t xz_memlimit = (std::numeric_limits<uint64_t>::max)();
size_t xz_input_pos = 0;
size_t xz_output_pos = 0;
std::vector<uint8_t> xz_output(input_size);
if (lzma_stream_buffer_decode(&xz_memlimit, 0, nullptr,
xz_compressed.data(), &xz_input_pos,
xz_compressed_pos, xz_output.data(),
&xz_output_pos, xz_output.size()) !=
LZMA_OK ||
!same_bytes(xz_output.data(), xz_output_pos, input, input_size)) {
return 10;
}
return 0;
}
EOF
"$MCPP_BIN_POSIX" build
"$MCPP_BIN_POSIX" run
make_project "compat-portable-imgui-glfw-smoke"
cat >> mcpp.toml <<'EOF'
[dependencies.compat]
imgui = "1.92.8"
glfw = "3.4"
EOF
cat > src/main.cpp <<'EOF'
#include <cstdlib>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include "imgui_impl_glfw.cpp"
#include "imgui_impl_opengl3.cpp"
static int run_glfw_window_smoke() {
if (!glfwInit()) {
return 10;
}
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
GLFWwindow* window = glfwCreateWindow(64, 64, "mcpp compat glfw", nullptr, nullptr);
if (!window) {
glfwTerminate();
return 11;
}
glfwMakeContextCurrent(window);
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
int main() {
if (std::getenv("MCPP_INDEX_RUN_GLFW_WINDOW_SMOKE")) {
return run_glfw_window_smoke();
}
const char* glfw = glfwGetVersionString();
const char* imgui = ImGui::GetVersion();
return glfw && imgui &&
GLFW_VERSION_MAJOR == 3 &&
IMGUI_VERSION_NUM >= 19200 ? 0 : 1;
}
EOF
"$MCPP_BIN_POSIX" build
"$MCPP_BIN_POSIX" run
# compat.openblas — Windows-only build-AND-run. This is the only place that
# exercises the mcpp >= 0.0.73 runtime-DLL deployment end to end: the consumer
# links the MSVC import lib (lib/libopenblas.lib) and, at `mcpp run`, loads the
# bin/libopenblas.dll that mcpp staged beside the .exe (a build-only check would
# pass even if the DLL were never deployed). cblas_dgemm computes a 2x2 GEMM and
# the program returns nonzero unless the result is exactly [19 22; 43 50], so a
# wrong/missing BLAS fails the run. Scoped to Windows: linux/macosx link a static
# libopenblas.a (no DLL, nothing new to validate here) and macosx would add a
# multi-minute source build to every portable run.
if [[ "$platform" == "Windows_NT" ]]; then
make_project "compat-portable-openblas-smoke"
cat >> mcpp.toml <<'EOF'
[dependencies.compat]
openblas = "0.3.33"
EOF
cat > src/main.cpp <<'EOF'
#include <cblas.h>
int main() {
// A (2x2, row-major) * B (2x2) = C; expect [[19,22],[43,50]].
const double A[4] = {1, 2, 3, 4};
const double B[4] = {5, 6, 7, 8};
double C[4] = {0, 0, 0, 0};
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
2, 2, 2, 1.0, A, 2, B, 2, 0.0, C, 2);
const double expected[4] = {19, 22, 43, 50};
for (int i = 0; i < 4; ++i) {
if (C[i] != expected[i]) return 10 + i; // wrong/missing BLAS
}
return 0;
}
EOF
"$MCPP_BIN_POSIX" build
# `mcpp run` also prepends the source bin/ to PATH, so it alone would not
# prove the DLL was deployed. Run the produced .exe DIRECTLY from a neutral
# CWD: Windows searches the executable's own directory first, so a clean exit
# proves bin/libopenblas.dll was actually staged beside app.exe (the deploy).
"$MCPP_BIN_POSIX" run
exe="$(find target -type f -name '*.exe' | head -1)"
[ -n "$exe" ] || { echo "FAIL: openblas smoke produced no .exe"; exit 1; }
# Absolutise: `find` yields a path relative to the project dir, but the launch
# runs from a neutral CWD (so the DLL can only be found beside the .exe, not
# via CWD). pwd makes exedir absolute; build an absolute native exe path.
exedir="$(cd "$(dirname "$exe")" && pwd)"
exeabs="$exedir/$(basename "$exe")"
[ -f "$exedir/libopenblas.dll" ] || {
echo "FAIL: libopenblas.dll was not deployed beside the .exe ($exedir)"
ls -la "$exedir"; exit 1; }
( cd / && "$(to_native_path "$exeabs")" ) || {
echo "FAIL: direct .exe launch failed — deployed DLL not loadable"; exit 1; }
fi
echo "OK"