Make targets cmd able to list CPUs and features

master
Layne Gustafson 2019-12-17 09:45:30 -05:00 committed by Andrew Kelley
parent 21908e100e
commit 5bc4690d85
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
4 changed files with 139 additions and 2 deletions

View File

@ -6,9 +6,12 @@ const io = std.io;
const mem = std.mem; const mem = std.mem;
const fs = std.fs; const fs = std.fs;
const process = std.process; const process = std.process;
const feature = std.target.feature;
const cpu = std.target.cpu;
const Allocator = mem.Allocator; const Allocator = mem.Allocator;
const ArrayList = std.ArrayList; const ArrayList = std.ArrayList;
const Buffer = std.Buffer; const Buffer = std.Buffer;
const Target = std.Target;
const self_hosted_main = @import("main.zig"); const self_hosted_main = @import("main.zig");
const errmsg = @import("errmsg.zig"); const errmsg = @import("errmsg.zig");
const DepTokenizer = @import("dep_tokenizer.zig").Tokenizer; const DepTokenizer = @import("dep_tokenizer.zig").Tokenizer;
@ -527,3 +530,99 @@ export fn stage2_progress_update_node(node: *std.Progress.Node, done_count: usiz
node.activate(); node.activate();
node.context.maybeRefresh(); node.context.maybeRefresh();
} }
// ABI warning
export fn stage2_list_features_for_arch(arch_name_ptr: [*]const u8, arch_name_len: usize, show_subfeatures: bool) void {
print_features_for_arch(arch_name_ptr[0..arch_name_len], show_subfeatures) catch |err| {
std.debug.warn("Failed to list features: {}\n", .{ @errorName(err) });
};
}
fn print_features_for_arch(arch_name: []const u8, show_subfeatures: bool) !void {
const stdout_stream = &std.io.getStdOut().outStream().stream;
const arch = Target.parseArchTag(arch_name) catch {
std.debug.warn("Failed to parse arch '{}'\nInvoke 'zig targets' for a list of valid architectures\n", .{ arch_name });
return;
};
inline for (@typeInfo(@TagType(Target.Arch)).Enum.fields) |arch_enum_field| {
if (@enumToInt(arch) == arch_enum_field.value) {
const enum_arch = @intToEnum(@TagType(Target.Arch), arch_enum_field.value);
const feature_infos = feature.ArchFeature(enum_arch).feature_infos;
try stdout_stream.print("Available features for {}:\n", .{ arch_enum_field.name });
var longest_len: usize = 0;
for (feature_infos) |feature_info| {
if (feature_info.name.len > longest_len) longest_len = feature_info.name.len;
}
for (feature_infos) |feature_info| {
try stdout_stream.print(" {}", .{ feature_info.name });
var i: usize = 0;
while (i < longest_len - feature_info.name.len) : (i += 1) {
try stdout_stream.write(" ");
}
try stdout_stream.print(" - {}\n", .{ feature_info.description });
if (show_subfeatures and feature_info.subfeatures.len > 0) {
for (feature_info.subfeatures) |subfeature| {
try stdout_stream.print(" {}\n", .{ subfeature.getInfo().name });
}
}
}
}
}
}
// ABI warning
export fn stage2_list_cpus_for_arch(arch_name_ptr: [*]const u8, arch_name_len: usize, show_subfeatures: bool) void {
print_cpus_for_arch(arch_name_ptr[0..arch_name_len], show_subfeatures) catch |err| {
std.debug.warn("Failed to list features: {}\n", .{ @errorName(err) });
};
}
fn print_cpus_for_arch(arch_name: []const u8, show_subfeatures: bool) !void {
const stdout_stream = &std.io.getStdOut().outStream().stream;
const arch = Target.parseArchTag(arch_name) catch {
std.debug.warn("Failed to parse arch '{}'\nInvoke 'zig targets' for a list of valid architectures\n", .{ arch_name });
return;
};
inline for (@typeInfo(@TagType(Target.Arch)).Enum.fields) |arch_enum_field| {
if (@enumToInt(arch) == arch_enum_field.value) {
const enum_arch = @intToEnum(@TagType(Target.Arch), arch_enum_field.value);
const cpu_infos = cpu.ArchCpu(enum_arch).cpu_infos;
try stdout_stream.print("Available cpus for {}:\n", .{ arch_enum_field.name });
var longest_len: usize = 0;
for (cpu_infos) |cpu_info| {
if (cpu_info.name.len > longest_len) longest_len = cpu_info.name.len;
}
for (cpu_infos) |cpu_info| {
try stdout_stream.print(" {}", .{ cpu_info.name });
var i: usize = 0;
while (i < longest_len - cpu_info.name.len) : (i += 1) {
try stdout_stream.write(" ");
}
try stdout_stream.write("\n");
if (show_subfeatures and cpu_info.features.len > 0) {
for (cpu_info.features) |subfeature| {
try stdout_stream.print(" {}\n", .{ subfeature.getInfo().name });
}
}
}
}
}
}

View File

@ -129,6 +129,11 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
" --test-name-prefix [text] add prefix to all tests\n" " --test-name-prefix [text] add prefix to all tests\n"
" --test-cmd [arg] specify test execution command one arg at a time\n" " --test-cmd [arg] specify test execution command one arg at a time\n"
" --test-cmd-bin appends test binary path to test cmd args\n" " --test-cmd-bin appends test binary path to test cmd args\n"
"\n"
"Targets Options:\n"
" --list-features [arch] list available features for the given architecture\n"
" --list-cpus [arch] list available cpus for the given architecture\n"
" --show-subfeatures list subfeatures for each entry from --list-features or --list-cpus\n"
, arg0); , arg0);
return return_code; return return_code;
} }
@ -529,6 +534,10 @@ int main(int argc, char **argv) {
WantCSanitize want_sanitize_c = WantCSanitizeAuto; WantCSanitize want_sanitize_c = WantCSanitizeAuto;
bool function_sections = false; bool function_sections = false;
const char *targets_list_features_arch = nullptr;
const char *targets_list_cpus_arch = nullptr;
bool targets_show_subfeatures = false;
ZigList<const char *> llvm_argv = {0}; ZigList<const char *> llvm_argv = {0};
llvm_argv.append("zig (LLVM option parsing)"); llvm_argv.append("zig (LLVM option parsing)");
@ -779,6 +788,8 @@ int main(int argc, char **argv) {
cur_pkg = cur_pkg->parent; cur_pkg = cur_pkg->parent;
} else if (strcmp(arg, "-ffunction-sections") == 0) { } else if (strcmp(arg, "-ffunction-sections") == 0) {
function_sections = true; function_sections = true;
} else if (strcmp(arg, "--show-subfeatures") == 0) {
targets_show_subfeatures = true;
} else if (i + 1 >= argc) { } else if (i + 1 >= argc) {
fprintf(stderr, "Expected another argument after %s\n", arg); fprintf(stderr, "Expected another argument after %s\n", arg);
return print_error_usage(arg0); return print_error_usage(arg0);
@ -936,7 +947,11 @@ int main(int argc, char **argv) {
, argv[i]); , argv[i]);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} else { } else if (strcmp(arg, "--list-features") == 0) {
targets_list_features_arch = argv[i];
} else if (strcmp(arg, "--list-cpus") == 0) {
targets_list_cpus_arch = argv[i];
}else {
fprintf(stderr, "Invalid argument: %s\n", arg); fprintf(stderr, "Invalid argument: %s\n", arg);
return print_error_usage(arg0); return print_error_usage(arg0);
} }
@ -1413,7 +1428,21 @@ int main(int argc, char **argv) {
return main_exit(root_progress_node, EXIT_SUCCESS); return main_exit(root_progress_node, EXIT_SUCCESS);
} }
case CmdTargets: case CmdTargets:
return print_target_list(stdout); if (targets_list_features_arch != nullptr) {
stage2_list_features_for_arch(
targets_list_features_arch,
strlen(targets_list_features_arch),
targets_show_subfeatures);
return 0;
} else if (targets_list_cpus_arch != nullptr) {
stage2_list_cpus_for_arch(
targets_list_cpus_arch,
strlen(targets_list_cpus_arch),
targets_show_subfeatures);
return 0;
} else {
return print_target_list(stdout);
}
case CmdNone: case CmdNone:
return print_full_usage(arg0, stderr, EXIT_FAILURE); return print_full_usage(arg0, stderr, EXIT_FAILURE);
} }

View File

@ -88,3 +88,6 @@ void stage2_progress_end(Stage2ProgressNode *node) {}
void stage2_progress_complete_one(Stage2ProgressNode *node) {} void stage2_progress_complete_one(Stage2ProgressNode *node) {}
void stage2_progress_disable_tty(Stage2Progress *progress) {} void stage2_progress_disable_tty(Stage2Progress *progress) {}
void stage2_progress_update_node(Stage2ProgressNode *node, size_t completed_count, size_t estimated_total_items){} void stage2_progress_update_node(Stage2ProgressNode *node, size_t completed_count, size_t estimated_total_items){}
void stage2_list_features_for_arch(const char *arch_name_ptr, size_t arch_name_len, bool show_subfeatures) {}
void stage2_list_cpus_for_arch(const char *arch_name_ptr, size_t arch_name_len, bool show_subfeatures) {}

View File

@ -174,4 +174,10 @@ ZIG_EXTERN_C void stage2_progress_complete_one(Stage2ProgressNode *node);
ZIG_EXTERN_C void stage2_progress_update_node(Stage2ProgressNode *node, ZIG_EXTERN_C void stage2_progress_update_node(Stage2ProgressNode *node,
size_t completed_count, size_t estimated_total_items); size_t completed_count, size_t estimated_total_items);
// ABI warning
ZIG_EXTERN_C void stage2_list_features_for_arch(const char *arch_name_ptr, size_t arch_name_len, bool show_subfeatures);
// ABI warning
ZIG_EXTERN_C void stage2_list_cpus_for_arch(const char *arch_name_ptr, size_t arch_name_len, bool show_subfeatures);
#endif #endif