Skip to content

Commit 0cf9ed0

Browse files
committed
Fix platform detection
1 parent 9443c69 commit 0cf9ed0

File tree

2 files changed

+48
-37
lines changed

2 files changed

+48
-37
lines changed

build.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,8 @@ pub fn build(b: *std.Build) void {
598598
const is_native = target.query.isNativeCpu() and target.query.isNativeOs() and (target.query.isNativeAbi() or target.result.abi.isMusl());
599599
const is_windows = target.result.os.tag == .windows;
600600

601-
// fx platform effectful functions test - only run on native builds
602-
if (is_native) {
601+
// fx platform effectful functions test - only run when not cross-compiling
602+
if (target.query.isNativeCpu() and target.query.isNativeOs() and target.query.isNativeAbi()) {
603603
// Create fx test platform host static library
604604
const test_platform_fx_host_lib = createTestPlatformHostLib(
605605
b,

src/cli/main.zig

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,63 +2166,77 @@ fn rocBuild(allocs: *Allocators, args: cli_args.BuildArgs) !void {
21662166
},
21672167
}
21682168

2169-
// Only support int test platform for cross-compilation
2170-
// Check if path contains "int" directory using cross-platform path handling
2171-
const path_contains_int = blk: {
2172-
var iter = std.fs.path.componentIterator(args.path) catch break :blk false;
2169+
// Check if we're actually cross-compiling
2170+
const is_cross_compiling = host_target != target;
2171+
2172+
// Get platform paths from the app file
2173+
const platform_paths = if (is_cross_compiling) blk: {
2174+
// Only support int test platform for cross-compilation
2175+
// Check if path contains "int" directory using cross-platform path handling
2176+
var iter = std.fs.path.componentIterator(args.path) catch {
2177+
std.log.err("roc build cross-compilation currently only supports the int test platform", .{});
2178+
std.log.err("Your app path: {s}", .{args.path});
2179+
return error.UnsupportedPlatform;
2180+
};
21732181
while (iter.next()) |component| {
21742182
if (std.mem.eql(u8, component.name, "int")) {
2175-
break :blk true;
2183+
const platform_dir = try std.fs.path.join(allocs.arena, &.{ "test", "int", "platform" });
2184+
const host_lib_filename = if (target.toOsTag() == .windows) "host.lib" else "libhost.a";
2185+
const host_lib_path = try std.fs.path.join(allocs.arena, &.{ platform_dir, host_lib_filename });
2186+
const platform_source_path = try std.fs.path.join(allocs.arena, &.{ platform_dir, "main.roc" });
2187+
break :blk PlatformPaths{
2188+
.host_lib_path = host_lib_path,
2189+
.platform_source_path = platform_source_path,
2190+
};
21762191
}
21772192
}
2178-
break :blk false;
2179-
};
2180-
2181-
const platform_type = if (path_contains_int)
2182-
"int"
2183-
else {
21842193
std.log.err("roc build cross-compilation currently only supports the int test platform", .{});
21852194
std.log.err("Your app path: {s}", .{args.path});
21862195
std.log.err("For str platform and other platforms, please use regular 'roc' command", .{});
21872196
return error.UnsupportedPlatform;
2197+
} else resolvePlatformPaths(allocs, args.path) catch |err| {
2198+
std.log.err("Failed to resolve platform paths for {s}: {}", .{ args.path, err });
2199+
return err;
21882200
};
21892201

2190-
std.log.info("Detected platform type: {s}", .{platform_type});
2191-
2192-
// Get platform directory path
2193-
const platform_dir = if (std.mem.eql(u8, platform_type, "int"))
2194-
try std.fs.path.join(allocs.arena, &.{ "test", "int", "platform" })
2195-
else
2196-
try std.fs.path.join(allocs.arena, &.{ "test", "str", "platform" });
2197-
2198-
// Check that platform exists
2199-
std.fs.cwd().access(platform_dir, .{}) catch |err| {
2200-
std.log.err("Platform directory not found: {s} ({})", .{ platform_dir, err });
2201-
return err;
2202+
const platform_dir = std.fs.path.dirname(platform_paths.host_lib_path) orelse {
2203+
std.log.err("Invalid platform host library path", .{});
2204+
return error.InvalidPlatform;
22022205
};
22032206

2204-
// Get target-specific host library path
2205-
// Use target OS to determine library filename, not host OS
2206-
const host_lib_filename = if (target.toOsTag() == .windows) "host.lib" else "libhost.a";
2207-
const host_lib_path = blk: {
2207+
// For cross-compilation, try target-specific host library
2208+
const host_lib_path = if (is_cross_compiling) blk: {
2209+
const host_lib_filename = if (target.toOsTag() == .windows) "host.lib" else "libhost.a";
22082210
// Try target-specific host library first
22092211
const target_specific_path = try std.fs.path.join(allocs.arena, &.{ platform_dir, "targets", @tagName(target), host_lib_filename });
22102212
std.fs.cwd().access(target_specific_path, .{}) catch {
22112213
// Fallback to generic host library
22122214
std.log.warn("Target-specific host library not found, falling back to generic: {s}", .{target_specific_path});
2213-
break :blk try std.fs.path.join(allocs.arena, &.{ platform_dir, host_lib_filename });
2215+
break :blk platform_paths.host_lib_path;
22142216
};
22152217
break :blk target_specific_path;
2216-
};
2218+
} else platform_paths.host_lib_path;
22172219

22182220
std.fs.cwd().access(host_lib_path, .{}) catch |err| {
22192221
std.log.err("Host library not found: {s} ({})", .{ host_lib_path, err });
22202222
return err;
22212223
};
22222224

2223-
// Get expected entrypoints for this platform
2224-
const entrypoints = try app_stub.getTestPlatformEntrypoints(allocs.gpa, platform_type);
2225-
defer allocs.gpa.free(entrypoints);
2225+
// Get expected entrypoints by parsing the platform's main.roc file
2226+
const platform_source_path = platform_paths.platform_source_path orelse {
2227+
std.log.err("Platform source file not found for: {s}", .{args.path});
2228+
return error.NoPlatformFound;
2229+
};
2230+
var entrypoints_list = std.array_list.Managed([]const u8).init(allocs.arena);
2231+
defer entrypoints_list.deinit();
2232+
2233+
try extractEntrypointsFromPlatform(allocs, platform_source_path, &entrypoints_list);
2234+
2235+
// Convert to PlatformEntrypoint array for generateAppStubObject
2236+
const entrypoints = try allocs.arena.alloc(app_stub.PlatformEntrypoint, entrypoints_list.items.len);
2237+
for (entrypoints_list.items, 0..) |name, i| {
2238+
entrypoints[i] = app_stub.PlatformEntrypoint{ .name = name };
2239+
}
22262240

22272241
std.log.info("Expected entrypoints: {}", .{entrypoints.len});
22282242
for (entrypoints, 0..) |ep, i| {
@@ -2271,9 +2285,6 @@ fn rocBuild(allocs: *Allocators, args: cli_args.BuildArgs) !void {
22712285
try platform_files_post.append(libc);
22722286
} else if (target.isDynamic()) {
22732287
// For dynamic linking with glibc, generate stub library for cross-compilation
2274-
// Check if we're doing actual cross-compilation
2275-
const is_cross_compiling = host_target != target;
2276-
22772288
if (is_cross_compiling) {
22782289
// For cross-compilation, use pre-built vendored stubs from the platform targets folder
22792290
const target_name = switch (target) {

0 commit comments

Comments
 (0)