diff --git a/packages/backend-embed/build.rs b/packages/backend-embed/build.rs index bf2b22cf..48a9c875 100644 --- a/packages/backend-embed/build.rs +++ b/packages/backend-embed/build.rs @@ -1,5 +1,4 @@ use anyhow::*; -use fs_extra::dir::{copy, CopyOptions}; use merkle_hash::MerkleTree; use std::fs; use std::path::{Path, PathBuf}; @@ -30,11 +29,33 @@ async fn main() -> Result<()> { } // Copy backend directory to out_dir - let mut copy_options = CopyOptions::new(); - copy_options.overwrite = true; - copy_options.copy_inside = true; - copy(&backend_path, &out_backend_path, ©_options) - .with_context(|| format!("failed to copy directory from {} to {}", backend_path.display(), out_backend_path.display()))?; + // TODO: This breaks deno check`` + // let mut copy_options = CopyOptions::new(); + // copy_options.overwrite = true; + // copy_options.copy_inside = true; + // copy(&backend_path, &out_backend_path, ©_options).with_context(|| { + // format!( + // "failed to copy directory from {} to {}", + // backend_path.display(), + // out_backend_path.display() + // ) + // })?; + + let status = std::process::Command::new("cp") + .arg("-R") + .arg(&backend_path) + .arg(&out_backend_path) + .status() + .with_context(|| { + format!( + "failed to copy directory from {} to {}", + backend_path.display(), + out_backend_path.display() + ) + })?; + if !status.success() { + return Err(anyhow!("cp command failed")); + } // Install deno let deno_dir = Path::new(&out_dir).join("deno"); @@ -61,7 +82,10 @@ async fn main() -> Result<()> { .arg("task") .arg("prepare") // Deno runs out of memory on Windows - .env("DENO_V8_FLAGS", "--max-heap-size=8192,--max-old-space-size=8192") + .env( + "DENO_V8_FLAGS", + "--max-heap-size=8192,--max-old-space-size=8192", + ) .current_dir(&out_backend_path) .status()?; if !status.success() { diff --git a/packages/backend/deno.jsonc b/packages/backend/deno.jsonc index 62d4df79..e6b5da5c 100644 --- a/packages/backend/deno.jsonc +++ b/packages/backend/deno.jsonc @@ -7,19 +7,14 @@ "check": "deno check cli/**/*.ts toolchain/**/*.ts runtime/**/*.ts", // Remove old vendored files in order to ensure a consistent cache. - // - // The deno.lock needs to be cleared to prevent from caching unused - // dependencies. - // - // strip_cross_platform removes files that are not cross-platform & deletes - // broken symlinks. "cache": "deno task cache:purge && deno task cache:download", - "cache:purge": "rm -rf deno.lock vendor node_modules", + "cache:purge": "rm -rf vendor node_modules", "cache:download": "deno cache cli/tasks/**/*.ts", - // Cache must be the last step in order to prevent unwanted extra cached - // files - "prepare": "deno task format && deno task check && deno task cache", + // Clear cache first in order to have as clean an environment as possible. + // Cache must be the last step in order to prevent unwanted extra cached + // files. Need to download cache before check in order to prevent error. + "prepare": "deno clean && deno task format && deno task check && deno task cache", "lint": "deno lint cli/**/*.ts toolchain/**/*.ts runtime/**/*.ts", "lint:fix": "deno lint cli/**/*.ts toolchain/**/*.ts runtime/**/*.ts", diff --git a/packages/backend/deno.lock b/packages/backend/deno.lock index b2f7bcbf..e0a33241 100644 --- a/packages/backend/deno.lock +++ b/packages/backend/deno.lock @@ -42,7 +42,9 @@ "jsr:@ts-morph/common@^0.24.0": "jsr:@ts-morph/common@0.24.0", "jsr:@ts-morph/ts-morph@^23.0.0": "jsr:@ts-morph/ts-morph@23.0.0", "npm:@asteasolutions/zod-to-openapi@^7.1.1": "npm:@asteasolutions/zod-to-openapi@7.1.1_zod@3.23.8", + "npm:@cloudflare/workers-types": "npm:@cloudflare/workers-types@4.20240919.0", "npm:dedent@^1.5.3": "npm:dedent@1.5.3", + "npm:drizzle-orm@0.33.0": "npm:drizzle-orm@0.33.0_@cloudflare+workers-types@4.20240919.0", "npm:esbuild-plugins-node-modules-polyfill@1.6.4": "npm:esbuild-plugins-node-modules-polyfill@1.6.4_esbuild@0.20.2", "npm:esbuild@^0.20.2": "npm:esbuild@0.20.2", "npm:glob@^11.0.0": "npm:glob@11.0.0", @@ -218,6 +220,10 @@ "zod": "zod@3.23.8" } }, + "@cloudflare/workers-types@4.20240919.0": { + "integrity": "sha512-DZwTpZVAV+fKTLxo6ntC2zMNRL/UJwvtMKUt/U7ZyJdR+t0qcBUZGx8jLi9gOFWYxkzO3s7slajwkR2hQRPXYQ==", + "dependencies": {} + }, "@esbuild/aix-ppc64@0.20.2": { "integrity": "sha512-D+EBOJHXdNZcLJRBkhENNG8Wji2kgc9AZ9KiPr1JuZjsNtyHzrsfLRrY0tk2H2aoFu6RANO1y1iPPUCDYWkb5g==", "dependencies": {} @@ -383,6 +389,12 @@ "integrity": "sha512-NHQtfOOW68WD8lgypbLA5oT+Bt0xXJhiYvoR6SmmNXZfpzOGXwdKWmcwG8N7PwVVWV3eF/68nmD9BaJSsTBhyQ==", "dependencies": {} }, + "drizzle-orm@0.33.0_@cloudflare+workers-types@4.20240919.0": { + "integrity": "sha512-SHy72R2Rdkz0LEq0PSG/IdvnT3nGiWuRk+2tXZQ90GVq/XQhpCzu/EFT3V2rox+w8MlkBQxifF8pCStNYnERfA==", + "dependencies": { + "@cloudflare/workers-types": "@cloudflare/workers-types@4.20240919.0" + } + }, "eastasianwidth@0.2.0": { "integrity": "sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==", "dependencies": {} @@ -619,7 +631,42 @@ } } }, - "remote": {}, + "remote": { + "https://deno.land/std@0.224.0/assert/_constants.ts": "a271e8ef5a573f1df8e822a6eb9d09df064ad66a4390f21b3e31f820a38e0975", + "https://deno.land/std@0.224.0/assert/assert.ts": "09d30564c09de846855b7b071e62b5974b001bb72a4b797958fe0660e7849834", + "https://deno.land/std@0.224.0/assert/assert_almost_equals.ts": "9e416114322012c9a21fa68e187637ce2d7df25bcbdbfd957cd639e65d3cf293", + "https://deno.land/std@0.224.0/assert/assert_array_includes.ts": "14c5094471bc8e4a7895fc6aa5a184300d8a1879606574cb1cd715ef36a4a3c7", + "https://deno.land/std@0.224.0/assert/assert_equals.ts": "3bbca947d85b9d374a108687b1a8ba3785a7850436b5a8930d81f34a32cb8c74", + "https://deno.land/std@0.224.0/assert/assert_exists.ts": "43420cf7f956748ae6ed1230646567b3593cb7a36c5a5327269279c870c5ddfd", + "https://deno.land/std@0.224.0/assert/assert_false.ts": "3e9be8e33275db00d952e9acb0cd29481a44fa0a4af6d37239ff58d79e8edeff", + "https://deno.land/std@0.224.0/assert/assert_greater.ts": "5e57b201fd51b64ced36c828e3dfd773412c1a6120c1a5a99066c9b261974e46", + "https://deno.land/std@0.224.0/assert/assert_greater_or_equal.ts": "9870030f997a08361b6f63400273c2fb1856f5db86c0c3852aab2a002e425c5b", + "https://deno.land/std@0.224.0/assert/assert_instance_of.ts": "e22343c1fdcacfaea8f37784ad782683ec1cf599ae9b1b618954e9c22f376f2c", + "https://deno.land/std@0.224.0/assert/assert_is_error.ts": "f856b3bc978a7aa6a601f3fec6603491ab6255118afa6baa84b04426dd3cc491", + "https://deno.land/std@0.224.0/assert/assert_less.ts": "60b61e13a1982865a72726a5fa86c24fad7eb27c3c08b13883fb68882b307f68", + "https://deno.land/std@0.224.0/assert/assert_less_or_equal.ts": "d2c84e17faba4afe085e6c9123a63395accf4f9e00150db899c46e67420e0ec3", + "https://deno.land/std@0.224.0/assert/assert_match.ts": "ace1710dd3b2811c391946954234b5da910c5665aed817943d086d4d4871a8b7", + "https://deno.land/std@0.224.0/assert/assert_not_equals.ts": "78d45dd46133d76ce624b2c6c09392f6110f0df9b73f911d20208a68dee2ef29", + "https://deno.land/std@0.224.0/assert/assert_not_instance_of.ts": "3434a669b4d20cdcc5359779301a0588f941ffdc2ad68803c31eabdb4890cf7a", + "https://deno.land/std@0.224.0/assert/assert_not_match.ts": "df30417240aa2d35b1ea44df7e541991348a063d9ee823430e0b58079a72242a", + "https://deno.land/std@0.224.0/assert/assert_not_strict_equals.ts": "37f73880bd672709373d6dc2c5f148691119bed161f3020fff3548a0496f71b8", + "https://deno.land/std@0.224.0/assert/assert_object_match.ts": "411450fd194fdaabc0089ae68f916b545a49d7b7e6d0026e84a54c9e7eed2693", + "https://deno.land/std@0.224.0/assert/assert_rejects.ts": "4bee1d6d565a5b623146a14668da8f9eb1f026a4f338bbf92b37e43e0aa53c31", + "https://deno.land/std@0.224.0/assert/assert_strict_equals.ts": "b4f45f0fd2e54d9029171876bd0b42dd9ed0efd8f853ab92a3f50127acfa54f5", + "https://deno.land/std@0.224.0/assert/assert_string_includes.ts": "496b9ecad84deab72c8718735373feb6cdaa071eb91a98206f6f3cb4285e71b8", + "https://deno.land/std@0.224.0/assert/assert_throws.ts": "c6508b2879d465898dab2798009299867e67c570d7d34c90a2d235e4553906eb", + "https://deno.land/std@0.224.0/assert/assertion_error.ts": "ba8752bd27ebc51f723702fac2f54d3e94447598f54264a6653d6413738a8917", + "https://deno.land/std@0.224.0/assert/equal.ts": "bddf07bb5fc718e10bb72d5dc2c36c1ce5a8bdd3b647069b6319e07af181ac47", + "https://deno.land/std@0.224.0/assert/fail.ts": "0eba674ffb47dff083f02ced76d5130460bff1a9a68c6514ebe0cdea4abadb68", + "https://deno.land/std@0.224.0/assert/mod.ts": "48b8cb8a619ea0b7958ad7ee9376500fe902284bb36f0e32c598c3dc34cbd6f3", + "https://deno.land/std@0.224.0/assert/unimplemented.ts": "8c55a5793e9147b4f1ef68cd66496b7d5ba7a9e7ca30c6da070c1a58da723d73", + "https://deno.land/std@0.224.0/assert/unreachable.ts": "5ae3dbf63ef988615b93eb08d395dda771c96546565f9e521ed86f6510c29e19", + "https://deno.land/std@0.224.0/fmt/colors.ts": "508563c0659dd7198ba4bbf87e97f654af3c34eb56ba790260f252ad8012e1c5", + "https://deno.land/std@0.224.0/internal/diff.ts": "6234a4b493ebe65dc67a18a0eb97ef683626a1166a1906232ce186ae9f65f4e6", + "https://deno.land/std@0.224.0/internal/format.ts": "0a98ee226fd3d43450245b1844b47003419d34d210fa989900861c79820d21c2", + "https://deno.land/std@0.224.0/internal/mod.ts": "534125398c8e7426183e12dc255bb635d94e06d0f93c60a297723abe69d3b22e", + "https://esm.sh/zod-validation-error@3.3.0": "d8825ca67952b6adff6b35026dc465f9638d4923dbd54fe9e8e81fbfddca9630" + }, "workspace": { "dependencies": [ "jsr:@bartlomieju/postgres@^0.17.2", diff --git a/scripts/build/build_cross.ts b/scripts/build/build_cross.ts index 291c9f3f..55a39340 100755 --- a/scripts/build/build_cross.ts +++ b/scripts/build/build_cross.ts @@ -4,9 +4,9 @@ import { resolve } from "jsr:@std/path"; import { ensureDir } from "jsr:@std/fs"; interface Platform { - name: string; - target: string; - files: string[]; + name: string; + target: string; + files: string[]; } const REPO_DIR = resolve(import.meta.dirname!, "..", ".."); @@ -87,130 +87,141 @@ ar = "aarch64-apple-darwin20.4-ar"\\n\\ `; async function buildDockerImage() { - console.log("Building Docker image..."); - const command = new Deno.Command("docker", { - args: ["build", "-t", DOCKER_IMAGE, "-"], - stdin: "piped", - }); - const process = command.spawn(); - const writer = process.stdin.getWriter(); - await writer.write(new TextEncoder().encode(DOCKERFILE)); - await writer.close(); - const { code } = await process.output(); - if (code !== 0) { - throw new Error("Docker build failed"); - } + console.log("Building Docker image..."); + const command = new Deno.Command("docker", { + args: ["build", "-t", DOCKER_IMAGE, "-"], + stdin: "piped", + }); + const process = command.spawn(); + const writer = process.stdin.getWriter(); + await writer.write(new TextEncoder().encode(DOCKERFILE)); + await writer.close(); + const { code } = await process.output(); + if (code !== 0) { + throw new Error("Docker build failed"); + } } -async function buildAndCopyCrossPlatform(outDir: string, packages: string[] = []) { - console.log("Building and copying cross-platform..."); - await Deno.remove(outDir, { recursive: true }).catch(() => {}); - - const platforms: Platform[] = [ - // { - // name: "linux_x86_64", - // target: "x86_64-unknown-linux-gnu", - // files: [], - // }, - { - name: "windows_x86_64", - target: "x86_64-pc-windows-gnu", - files: [], - }, - // { - // name: "macos_x86_64", - // target: "x86_64-apple-darwin", - // files: [], - // }, - { - name: "macos_arm64", - target: "aarch64-apple-darwin", - files: [], - }, - ]; - - // Determine which files to include based on the packages - const includeAll = packages.length === 0 || packages.includes("all"); - for (const platform of platforms) { - if (includeAll || packages.includes("rivet-cli")) { - platform.files.push(platform.name.includes("windows") ? "rivet.exe" : "rivet"); - } - if (includeAll || packages.includes("rivet-toolchain-ffi")) { - let ffiLibrary: string; - if (platform.name.includes("windows")) { - ffiLibrary = "rivet_toolchain_ffi.dll"; - } else if (platform.name.includes("linux")) { - ffiLibrary = "librivet_toolchain_ffi.so"; - } else if (platform.name.includes("macos")) { - ffiLibrary = "librivet_toolchain_ffi.dylib"; - } else { - throw new Error(`Unsupported platform: ${platform.name}`); - } - platform.files.push(ffiLibrary); - } - } - - for (const platform of platforms) { - console.log(`Building for ${platform.name}...`); - const dockerArgs = [ - "run", - "--rm", - "-v", - `${REPO_DIR}:/app`, - "-e", - `OVERRIDE_TARGET=${platform.target}`, - ]; - - // Add GITHUB_TOKEN if it exists - const githubToken = Deno.env.get("GITHUB_TOKEN"); - if (githubToken) { - dockerArgs.push("-e", `GITHUB_TOKEN=${githubToken}`); - } - - dockerArgs.push( - DOCKER_IMAGE, - "/bin/sh", - "-c", - `cargo build --manifest-path Cargo.toml --target ${platform.target} --release && chown -R ${Deno.uid()}:${Deno.gid()} /app/target`, - ); - - const command = new Deno.Command("docker", { - args: dockerArgs, - stdin: "inherit", - stdout: "inherit", - stderr: "inherit", - }); - - const { code } = await command.output(); - - if (code !== 0) { - throw new Error(`Build failed for ${platform.name}`); - } - - for (const file of platform.files) { - const srcPath = resolve( - REPO_DIR, - "target", - platform.target, - "release", - file, - ); - const destPath = resolve(REPO_DIR, outDir, platform.name, file); - await ensureDir(resolve(REPO_DIR, outDir, platform.name)); - await Deno.copyFile(srcPath, destPath); - console.log(`Copied ${srcPath} to ${destPath}`); - } - - // Delete target if needed - if (Deno.env.get("CROSS_DELETE_TARGET") == "1") { - const targetPath = resolve(REPO_DIR, "target", platform.target); - await Deno.remove(targetPath, { recursive: true }); - console.log(`Deleted ${targetPath}`); - } - } +async function buildAndCopyCrossPlatform( + outDir: string, + packages: string[] = [], +) { + console.log("Building and copying cross-platform..."); + await Deno.remove(outDir, { recursive: true }).catch(() => {}); + + const platforms: Platform[] = [ + // { + // name: "linux_x86_64", + // target: "x86_64-unknown-linux-gnu", + // files: [], + // }, + { + name: "windows_x86_64", + target: "x86_64-pc-windows-gnu", + files: [], + }, + // { + // name: "macos_x86_64", + // target: "x86_64-apple-darwin", + // files: [], + // }, + { + name: "macos_arm64", + target: "aarch64-apple-darwin", + files: [], + }, + ]; + + // Determine which files to include based on the packages + const includeAll = packages.length === 0 || packages.includes("all"); + for (const platform of platforms) { + if (includeAll || packages.includes("rivet-cli")) { + platform.files.push( + platform.name.includes("windows") ? "rivet.exe" : "rivet", + ); + } + if (includeAll || packages.includes("rivet-toolchain-ffi")) { + let ffiLibrary: string; + if (platform.name.includes("windows")) { + ffiLibrary = "rivet_toolchain_ffi.dll"; + } else if (platform.name.includes("linux")) { + ffiLibrary = "librivet_toolchain_ffi.so"; + } else if (platform.name.includes("macos")) { + ffiLibrary = "librivet_toolchain_ffi.dylib"; + } else { + throw new Error(`Unsupported platform: ${platform.name}`); + } + platform.files.push(ffiLibrary); + } + } + + for (const platform of platforms) { + console.log(`Building for ${platform.name}...`); + const dockerArgs = [ + "run", + "--rm", + "-v", + `${REPO_DIR}:/app`, + "-e", + `OVERRIDE_TARGET=${platform.target}`, + ]; + + // Add GITHUB_TOKEN if it exists + const githubToken = Deno.env.get("GITHUB_TOKEN"); + if (githubToken) { + dockerArgs.push("-e", `GITHUB_TOKEN=${githubToken}`); + } + + dockerArgs.push( + DOCKER_IMAGE, + "/bin/sh", + "-c", + `cargo build -vv --manifest-path Cargo.toml --target ${platform.target} --release && chown -R ${Deno.uid()}:${Deno.gid()} /app/target`, + ); + + const command = new Deno.Command("docker", { + args: dockerArgs, + stdin: "inherit", + stdout: "inherit", + stderr: "inherit", + }); + + const { code } = await command.output(); + + if (code !== 0) { + throw new Error(`Build failed for ${platform.name}`); + } + + for (const file of platform.files) { + const srcPath = resolve( + REPO_DIR, + "target", + platform.target, + "release", + file, + ); + const destPath = resolve(REPO_DIR, outDir, platform.name, file); + await ensureDir(resolve(REPO_DIR, outDir, platform.name)); + await Deno.copyFile(srcPath, destPath); + console.log(`Copied ${srcPath} to ${destPath}`); + } + + // Delete target if needed + if (Deno.env.get("CROSS_DELETE_TARGET") == "1") { + const targetPath = resolve(REPO_DIR, "target", platform.target); + await Deno.remove(targetPath, { recursive: true }); + console.log(`Deleted ${targetPath}`); + } + } } export async function buildCross(outDir: string, packages: string[] = []) { - await buildDockerImage(); - await buildAndCopyCrossPlatform(outDir, packages); + await buildDockerImage(); + await buildAndCopyCrossPlatform(outDir, packages); +} + +if (import.meta.main) { + const dir = await Deno.makeTempDir(); + console.log(dir); + await buildCross(dir); }