package my_project import std.process.* import std.fs.* import std.core.* // 构建前钩子:编译 C 库 func stagePreBuild(): Int64 { let root_dir = Path(".") let lib_dir = root_dir.join("lib") let build_dir = lib_dir.join("build") // 1. 确保 lib 目录存在 if (!exists(lib_dir)) { println("Error: lib directory not found at ${lib_dir}") return 1 } // 2. 创建构建目录 - 使用 try 表达式 if (!exists(build_dir)) { try { Directory.create(build_dir, recursive: true) } catch (e: Exception) { println("Error: Cannot create build directory") return 1 } } // 3. 执行 cmake 配置 println("Configuring C library with CMake...") let cmake_exit = execute( "cmake", ["-S", lib_dir.toString(), "-B", build_dir.toString()], workingDirectory: None, environment: None, stdIn: Inherit, stdOut: Inherit, stdErr: Inherit, timeout: None ) if (cmake_exit != 0) { println("CMake configuration failed! Exit code: ${cmake_exit}") return cmake_exit } // 4. 执行构建 println("Building C library...") let build_exit = execute( "cmake", ["--build", build_dir.toString(), "--parallel"], workingDirectory: None, environment: None, stdIn: Inherit, stdOut: Inherit, stdErr: Inherit, timeout: None ) if (build_exit != 0) { println("Build failed! Exit code: ${build_exit}") return build_exit } println("C library compiled successfully at: ${build_dir}") // 5. 可选:安装到输出目录 let output_dir = root_dir.join("liboutput") if (!exists(output_dir)) { try { Directory.create(output_dir, recursive: true) } catch (_: Exception) { // 忽略创建错误 } } 0 } // 构建后钩子 func stagePostBuild(): Int64 { println("Post-build: C library integration complete") 0 } // 清理前钩子 func stagePreClean(): Int64 { let build_dir = Path(".").join("lib").join("build") if (exists(build_dir)) { try { remove(build_dir, recursive: true) println("Cleaned C library build directory") } catch (_: Exception) { println("Warning: Failed to clean build directory") } } 0 } main(args: Array): Int64 { if (args.size == 0) { return 0 } match (args[0]) { case "pre-build" => stagePreBuild() case "post-build" => stagePostBuild() case "pre-clean" => stagePreClean() case _ => 0 } }