| 1 | module pref |
| 2 | |
| 3 | fn test_comptime_flag_value_uses_target_os_preference() { |
| 4 | mut prefs := new_preferences() |
| 5 | prefs.target_os = 'windows' |
| 6 | assert comptime_flag_value(&prefs, 'windows') |
| 7 | assert !comptime_flag_value(&prefs, 'linux') |
| 8 | assert !comptime_flag_value(&prefs, 'macos') |
| 9 | |
| 10 | prefs.target_os = 'darwin' |
| 11 | assert comptime_flag_value(&prefs, 'macos') |
| 12 | assert comptime_flag_value(&prefs, 'darwin') |
| 13 | assert comptime_flag_value(&prefs, 'bsd') |
| 14 | assert !comptime_flag_value(&prefs, 'windows') |
| 15 | |
| 16 | prefs.target_os = 'termux' |
| 17 | assert comptime_flag_value(&prefs, 'termux') |
| 18 | assert !comptime_flag_value(&prefs, 'android') |
| 19 | assert !comptime_flag_value(&prefs, 'linux') |
| 20 | } |
| 21 | |
| 22 | fn test_comptime_flag_value_allows_nil_preferences() { |
| 23 | prefs := unsafe { &Preferences(nil) } |
| 24 | assert comptime_flag_value(prefs, 'linux') == (prefs.target_os_or_host() == 'linux') |
| 25 | assert !comptime_flag_value(prefs, 'definitely_missing_flag') |
| 26 | } |
| 27 | |
| 28 | fn test_comptime_pkgconfig_value_reports_missing_packages_as_false() { |
| 29 | assert !comptime_pkgconfig_value('__v2_definitely_missing_pkgconfig_test_package__') |
| 30 | } |
| 31 | |
| 32 | fn test_comptime_optional_flag_value_uses_user_defines_only() { |
| 33 | mut prefs := new_preferences() |
| 34 | prefs.target_os = 'linux' |
| 35 | assert comptime_flag_value(&prefs, 'linux') |
| 36 | assert !comptime_optional_flag_value(&prefs, 'linux') |
| 37 | |
| 38 | prefs.user_defines = ['linux'] |
| 39 | prefs.explicit_user_defines = ['linux'] |
| 40 | assert comptime_optional_flag_value(&prefs, 'linux') |
| 41 | |
| 42 | prefs.target_os = 'windows' |
| 43 | assert !comptime_flag_value(&prefs, 'linux') |
| 44 | assert comptime_optional_flag_value(&prefs, 'linux') |
| 45 | } |
| 46 | |
| 47 | fn test_comptime_optional_target_modes_ignore_synthesized_defines() { |
| 48 | mut cross_prefs := new_preferences() |
| 49 | cross_prefs.target_os = 'cross' |
| 50 | cross_prefs.output_cross_c = true |
| 51 | cross_prefs.user_defines = ['cross'] |
| 52 | assert comptime_flag_value(&cross_prefs, 'cross') |
| 53 | assert !comptime_optional_flag_value(&cross_prefs, 'cross') |
| 54 | cross_prefs.explicit_user_defines = ['cross'] |
| 55 | assert comptime_optional_flag_value(&cross_prefs, 'cross') |
| 56 | |
| 57 | mut free_prefs := new_preferences() |
| 58 | free_prefs.freestanding = true |
| 59 | free_prefs.user_defines = ['freestanding'] |
| 60 | assert comptime_flag_value(&free_prefs, 'freestanding') |
| 61 | assert !comptime_optional_flag_value(&free_prefs, 'freestanding') |
| 62 | free_prefs.explicit_user_defines = ['freestanding'] |
| 63 | assert comptime_optional_flag_value(&free_prefs, 'freestanding') |
| 64 | |
| 65 | mut none_prefs := new_preferences() |
| 66 | none_prefs.freestanding = true |
| 67 | none_prefs.target_os = 'none' |
| 68 | none_prefs.user_defines = ['freestanding'] |
| 69 | assert comptime_flag_value(&none_prefs, 'none') |
| 70 | assert !comptime_optional_flag_value(&none_prefs, 'none') |
| 71 | none_prefs.explicit_user_defines = ['none'] |
| 72 | assert comptime_optional_flag_value(&none_prefs, 'none') |
| 73 | } |
| 74 | |
| 75 | fn test_comptime_optional_bare_preserves_explicit_user_define() { |
| 76 | mut prefs := new_preferences() |
| 77 | prefs.user_defines = ['bare'] |
| 78 | assert !comptime_optional_flag_value(&prefs, 'bare') |
| 79 | |
| 80 | prefs.explicit_user_defines = ['bare'] |
| 81 | assert comptime_optional_flag_value(&prefs, 'bare') |
| 82 | } |
| 83 | |
| 84 | fn test_comptime_optional_flag_value_keeps_internal_capability_flags() { |
| 85 | mut prefs := new_preferences() |
| 86 | prefs.backend = .x64 |
| 87 | prefs.arch = .x64 |
| 88 | prefs.target_os = 'windows' |
| 89 | assert comptime_flag_value(&prefs, 'v2_native_windows_pe_minimal') |
| 90 | assert comptime_optional_flag_value(&prefs, 'v2_native_windows_pe_minimal') |
| 91 | assert comptime_flag_value(&prefs, 'builtin_write_buf_to_fd_should_use_c_write') |
| 92 | assert comptime_optional_flag_value(&prefs, 'builtin_write_buf_to_fd_should_use_c_write') |
| 93 | } |
| 94 | |
| 95 | fn test_comptime_flag_value_native_backend_backtrace_guards() { |
| 96 | mut prefs := new_preferences() |
| 97 | prefs.backend = .x64 |
| 98 | assert comptime_flag_value(&prefs, 'no_backtrace') |
| 99 | assert comptime_flag_value(&prefs, 'tinyc') |
| 100 | |
| 101 | prefs.backend = .arm64 |
| 102 | assert comptime_flag_value(&prefs, 'no_backtrace') |
| 103 | assert comptime_flag_value(&prefs, 'tinyc') |
| 104 | |
| 105 | prefs.backend = .cleanc |
| 106 | assert !comptime_flag_value(&prefs, 'no_backtrace') |
| 107 | assert !comptime_flag_value(&prefs, 'tinyc') |
| 108 | |
| 109 | prefs.user_defines = ['no_backtrace'] |
| 110 | assert comptime_flag_value(&prefs, 'no_backtrace') |
| 111 | assert !comptime_flag_value(&prefs, 'tinyc') |
| 112 | } |
| 113 | |
| 114 | fn test_effective_arch_uses_target_os_preference() { |
| 115 | mut prefs := new_preferences() |
| 116 | prefs.arch = .auto |
| 117 | prefs.target_os = 'macos' |
| 118 | assert prefs.get_effective_arch() == .arm64 |
| 119 | |
| 120 | prefs.target_os = 'windows' |
| 121 | assert prefs.get_effective_arch() == .x64 |
| 122 | } |
| 123 | |
| 124 | fn test_v2_native_windows_pe_minimal_flag() { |
| 125 | mut prefs := new_preferences() |
| 126 | prefs.backend = .x64 |
| 127 | prefs.arch = .x64 |
| 128 | prefs.target_os = 'windows' |
| 129 | assert comptime_flag_value(&prefs, 'v2_native_windows_pe_minimal') |
| 130 | |
| 131 | prefs.target_os = 'linux' |
| 132 | assert !comptime_flag_value(&prefs, 'v2_native_windows_pe_minimal') |
| 133 | prefs.target_os = 'windows' |
| 134 | prefs.backend = .arm64 |
| 135 | assert !comptime_flag_value(&prefs, 'v2_native_windows_pe_minimal') |
| 136 | } |
| 137 | |
| 138 | fn test_freestanding_hook_comptime_flags() { |
| 139 | mut prefs := new_preferences() |
| 140 | prefs.freestanding = true |
| 141 | prefs.freestanding_hooks = ['output', 'alloc'] |
| 142 | assert comptime_flag_value(&prefs, 'freestanding') |
| 143 | assert comptime_flag_value(&prefs, 'freestanding_hooks') |
| 144 | assert comptime_flag_value(&prefs, 'freestanding_output') |
| 145 | assert comptime_flag_value(&prefs, 'freestanding_alloc') |
| 146 | assert !comptime_flag_value(&prefs, 'freestanding_panic') |
| 147 | } |
| 148 | |
| 149 | fn test_freestanding_none_has_no_concrete_os_comptime_flags() { |
| 150 | mut prefs := new_preferences() |
| 151 | prefs.freestanding = true |
| 152 | prefs.target_os = 'none' |
| 153 | assert comptime_flag_value(&prefs, 'freestanding') |
| 154 | assert !comptime_flag_value(&prefs, 'cross') |
| 155 | assert !comptime_flag_value(&prefs, 'linux') |
| 156 | assert !comptime_flag_value(&prefs, 'macos') |
| 157 | assert !comptime_flag_value(&prefs, 'darwin') |
| 158 | assert !comptime_flag_value(&prefs, 'windows') |
| 159 | assert !comptime_flag_value(&prefs, 'bsd') |
| 160 | } |
| 161 | |